1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations for the subclasses of Constant, which
11 // represent the different flavors of constant values that live in LLVM. Note
12 // that Constants are immutable (once created they never change) and are fully
13 // shared by structural equivalence. This means that two structurally
14 // equivalent constants will always have the same address. Constant's are
15 // created on demand as needed and never deleted: thus clients don't have to
16 // worry about the lifetime of the objects.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CONSTANTS_H
21 #define LLVM_CONSTANTS_H
23 #include "llvm/Constant.h"
24 #include "llvm/Type.h"
25 #include "llvm/Support/DataTypes.h"
34 template<class ConstantClass, class TypeClass, class ValType>
35 struct ConstantCreator;
36 template<class ConstantClass, class TypeClass>
37 struct ConvertConstantType;
39 //===----------------------------------------------------------------------===//
40 /// ConstantIntegral - Shared superclass of boolean and integer constants.
42 /// This class just defines some common interfaces to be implemented.
44 class ConstantIntegral : public Constant {
50 ConstantIntegral(const Type *Ty, uint64_t V);
53 /// getRawValue - return the underlying value of this constant as a 64-bit
54 /// unsigned integer value.
56 inline uint64_t getRawValue() const { return Val.Unsigned; }
58 /// getZExtValue - Return the constant zero extended as appropriate for this
60 inline uint64_t getZExtValue() const {
61 unsigned Size = getType()->getPrimitiveSizeInBits();
62 return Val.Unsigned & (~0ULL >> (64-Size));
65 /// getSExtValue - Return the constant sign extended as appropriate for this
67 inline int64_t getSExtValue() const {
68 unsigned Size = getType()->getPrimitiveSizeInBits();
69 return (Val.Signed << (64-Size)) >> (64-Size);
72 /// isNullValue - Return true if this is the value that would be returned by
75 virtual bool isNullValue() const = 0;
77 /// isMaxValue - Return true if this is the largest value that may be
78 /// represented by this type.
80 virtual bool isMaxValue() const = 0;
82 /// isMinValue - Return true if this is the smallest value that may be
83 /// represented by this type.
85 virtual bool isMinValue() const = 0;
87 /// isAllOnesValue - Return true if every bit in this constant is set to true.
89 virtual bool isAllOnesValue() const = 0;
91 /// Static constructor to get the maximum/minimum/allones constant of
92 /// specified (integral) type...
94 static ConstantIntegral *getMaxValue(const Type *Ty);
95 static ConstantIntegral *getMinValue(const Type *Ty);
96 static ConstantIntegral *getAllOnesValue(const Type *Ty);
98 /// Methods for support type inquiry through isa, cast, and dyn_cast:
99 static inline bool classof(const ConstantIntegral *) { return true; }
100 static bool classof(const Value *V) {
101 return V->getValueType() == SimpleConstantVal &&
102 V->getType()->isIntegral();
107 //===----------------------------------------------------------------------===//
108 /// ConstantBool - Boolean Values
110 class ConstantBool : public ConstantIntegral {
111 ConstantBool(bool V);
113 static ConstantBool *True, *False; // The True & False values
115 /// get() - Static factory methods - Return objects of the specified value
116 static ConstantBool *get(bool Value) { return Value ? True : False; }
117 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
119 /// inverted - Return the opposite value of the current value.
120 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
122 /// getValue - return the boolean value of this constant.
124 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
126 /// isNullValue - Return true if this is the value that would be returned by
129 virtual bool isNullValue() const { return this == False; }
130 virtual bool isMaxValue() const { return this == True; }
131 virtual bool isMinValue() const { return this == False; }
132 virtual bool isAllOnesValue() const { return this == True; }
134 /// Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const ConstantBool *) { return true; }
136 static bool classof(const Value *V) {
137 return (V == True) | (V == False);
142 //===----------------------------------------------------------------------===//
143 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
144 /// with integral constants easier.
146 class ConstantInt : public ConstantIntegral {
148 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
149 ConstantInt(const Type *Ty, uint64_t V);
151 /// equalsInt - Provide a helper method that can be used to determine if the
152 /// constant contained within is equal to a constant. This only works for
153 /// very small values, because this is all that can be represented with all
156 bool equalsInt(unsigned char V) const {
158 "equalsInt: Can only be used with very small positive constants!");
159 return Val.Unsigned == V;
162 /// ConstantInt::get static method: return a ConstantInt with the specified
163 /// value. as above, we work only with very small values here.
165 static ConstantInt *get(const Type *Ty, unsigned char V);
167 /// isNullValue - Return true if this is the value that would be returned by
169 virtual bool isNullValue() const { return Val.Unsigned == 0; }
170 virtual bool isMaxValue() const = 0;
171 virtual bool isMinValue() const = 0;
173 /// Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const ConstantInt *) { return true; }
175 static bool classof(const Value *V) {
176 return V->getValueType() == SimpleConstantVal &&
177 V->getType()->isInteger();
182 //===----------------------------------------------------------------------===//
183 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
185 class ConstantSInt : public ConstantInt {
186 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
187 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
190 ConstantSInt(const Type *Ty, int64_t V);
192 /// get() - Static factory methods - Return objects of the specified value
194 static ConstantSInt *get(const Type *Ty, int64_t V);
196 /// isValueValidForType - return true if Ty is big enough to represent V.
198 static bool isValueValidForType(const Type *Ty, int64_t V);
200 /// getValue - return the underlying value of this constant.
202 inline int64_t getValue() const { return Val.Signed; }
204 virtual bool isAllOnesValue() const { return getValue() == -1; }
206 /// isMaxValue - Return true if this is the largest value that may be
207 /// represented by this type.
209 virtual bool isMaxValue() const {
210 int64_t V = getValue();
211 if (V < 0) return false; // Be careful about wrap-around on 'long's
213 return !isValueValidForType(getType(), V) || V < 0;
216 /// isMinValue - Return true if this is the smallest value that may be
217 /// represented by this type.
219 virtual bool isMinValue() const {
220 int64_t V = getValue();
221 if (V > 0) return false; // Be careful about wrap-around on 'long's
223 return !isValueValidForType(getType(), V) || V > 0;
226 /// Methods for support type inquiry through isa, cast, and dyn_cast:
228 static inline bool classof(const ConstantSInt *) { return true; }
229 static bool classof(const Value *V) {
230 return V->getValueType() == SimpleConstantVal &&
231 V->getType()->isSigned();
235 //===----------------------------------------------------------------------===//
236 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
238 class ConstantUInt : public ConstantInt {
239 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
240 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
242 ConstantUInt(const Type *Ty, uint64_t V);
244 /// get() - Static factory methods - Return objects of the specified value
246 static ConstantUInt *get(const Type *Ty, uint64_t V);
248 /// isValueValidForType - return true if Ty is big enough to represent V.
250 static bool isValueValidForType(const Type *Ty, uint64_t V);
252 /// getValue - return the underlying value of this constant.
254 inline uint64_t getValue() const { return Val.Unsigned; }
256 /// isMaxValue - Return true if this is the largest value that may be
257 /// represented by this type.
259 virtual bool isAllOnesValue() const;
260 virtual bool isMaxValue() const { return isAllOnesValue(); }
261 virtual bool isMinValue() const { return getValue() == 0; }
263 /// Methods for support type inquiry through isa, cast, and dyn_cast:
264 static inline bool classof(const ConstantUInt *) { return true; }
265 static bool classof(const Value *V) {
266 return V->getValueType() == SimpleConstantVal &&
267 V->getType()->isUnsigned();
272 //===----------------------------------------------------------------------===//
273 /// ConstantFP - Floating Point Values [float, double]
275 class ConstantFP : public Constant {
277 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
278 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
279 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
281 ConstantFP(const Type *Ty, double V);
283 /// get() - Static factory methods - Return objects of the specified value
284 static ConstantFP *get(const Type *Ty, double V);
286 /// isValueValidForType - return true if Ty is big enough to represent V.
287 static bool isValueValidForType(const Type *Ty, double V);
288 inline double getValue() const { return Val; }
290 /// isNullValue - Return true if this is the value that would be returned by
291 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
292 /// considers -0.0 to be null as well as 0.0. :(
293 virtual bool isNullValue() const;
295 /// isExactlyValue - We don't rely on operator== working on double values, as
296 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
297 /// As such, this method can be used to do an exact bit-for-bit comparison of
298 /// two floating point values.
299 bool isExactlyValue(double V) const;
301 /// Methods for support type inquiry through isa, cast, and dyn_cast:
302 static inline bool classof(const ConstantFP *) { return true; }
303 static bool classof(const Value *V) {
304 return V->getValueType() == SimpleConstantVal &&
305 V->getType()->isFloatingPoint();
309 //===----------------------------------------------------------------------===//
310 /// ConstantAggregateZero - All zero aggregate value
312 class ConstantAggregateZero : public Constant {
313 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
314 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
316 ConstantAggregateZero(const Type *Ty)
317 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
319 /// get() - static factory method for creating a null aggregate. It is
320 /// illegal to call this method with a non-aggregate type.
321 static Constant *get(const Type *Ty);
323 /// isNullValue - Return true if this is the value that would be returned by
325 virtual bool isNullValue() const { return true; }
327 virtual void destroyConstant();
328 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
329 bool DisableChecking = false);
331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
333 static bool classof(const ConstantAggregateZero *) { return true; }
334 static bool classof(const Value *V) {
335 return V->getValueType() == ConstantAggregateZeroVal;
340 //===----------------------------------------------------------------------===//
341 /// ConstantArray - Constant Array Declarations
343 class ConstantArray : public Constant {
344 friend struct ConstantCreator<ConstantArray, ArrayType,
345 std::vector<Constant*> >;
346 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
348 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
351 /// get() - Static factory methods - Return objects of the specified value
352 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
353 static Constant *get(const std::string &Initializer);
355 /// getType - Specialize the getType() method to always return an ArrayType,
356 /// which reduces the amount of casting needed in parts of the compiler.
358 inline const ArrayType *getType() const {
359 return reinterpret_cast<const ArrayType*>(Value::getType());
362 /// isString - This method returns true if the array is an array of sbyte or
363 /// ubyte, and if the elements of the array are all ConstantInt's.
364 bool isString() const;
366 /// getAsString - If this array is isString(), then this method converts the
367 /// array to an std::string and returns it. Otherwise, it asserts out.
369 std::string getAsString() const;
371 /// isNullValue - Return true if this is the value that would be returned by
372 /// getNullValue. This always returns false because zero arrays are always
373 /// created as ConstantAggregateZero objects.
374 virtual bool isNullValue() const { return false; }
376 virtual void destroyConstant();
377 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
378 bool DisableChecking = false);
380 /// Methods for support type inquiry through isa, cast, and dyn_cast:
381 static inline bool classof(const ConstantArray *) { return true; }
382 static bool classof(const Value *V) {
383 return V->getValueType() == SimpleConstantVal &&
384 V->getType()->getTypeID() == Type::ArrayTyID;
389 //===----------------------------------------------------------------------===//
390 // ConstantStruct - Constant Struct Declarations
392 class ConstantStruct : public Constant {
393 friend struct ConstantCreator<ConstantStruct, StructType,
394 std::vector<Constant*> >;
395 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
397 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
400 /// get() - Static factory methods - Return objects of the specified value
402 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
403 static Constant *get(const std::vector<Constant*> &V);
405 /// getType() specialization - Reduce amount of casting...
407 inline const StructType *getType() const {
408 return reinterpret_cast<const StructType*>(Value::getType());
411 /// isNullValue - Return true if this is the value that would be returned by
412 /// getNullValue. This always returns false because zero structs are always
413 /// created as ConstantAggregateZero objects.
414 virtual bool isNullValue() const {
418 virtual void destroyConstant();
419 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
420 bool DisableChecking = false);
422 /// Methods for support type inquiry through isa, cast, and dyn_cast:
423 static inline bool classof(const ConstantStruct *) { return true; }
424 static bool classof(const Value *V) {
425 return V->getValueType() == SimpleConstantVal &&
426 V->getType()->getTypeID() == Type::StructTyID;
430 //===----------------------------------------------------------------------===//
431 /// ConstantPacked - Constant Packed Declarations
433 class ConstantPacked : public Constant {
434 friend struct ConstantCreator<ConstantPacked, PackedType,
435 std::vector<Constant*> >;
436 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
438 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
441 /// get() - Static factory methods - Return objects of the specified value
442 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
443 static Constant *get(const std::vector<Constant*> &V);
445 /// getType - Specialize the getType() method to always return an PackedType,
446 /// which reduces the amount of casting needed in parts of the compiler.
448 inline const PackedType *getType() const {
449 return reinterpret_cast<const PackedType*>(Value::getType());
452 /// isNullValue - Return true if this is the value that would be returned by
453 /// getNullValue. This always returns false because zero arrays are always
454 /// created as ConstantAggregateZero objects.
455 virtual bool isNullValue() const { return false; }
457 virtual void destroyConstant();
458 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
459 bool DisableChecking = false);
461 /// Methods for support type inquiry through isa, cast, and dyn_cast:
462 static inline bool classof(const ConstantPacked *) { return true; }
463 static bool classof(const Value *V) {
464 return V->getValueType() == SimpleConstantVal &&
465 V->getType()->getTypeID() == Type::PackedTyID;
469 //===----------------------------------------------------------------------===//
470 /// ConstantPointerNull - a constant pointer value that points to null
472 class ConstantPointerNull : public Constant {
473 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
474 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
476 ConstantPointerNull(const PointerType *T)
477 : Constant(reinterpret_cast<const Type*>(T),
478 Value::SimpleConstantVal, 0, 0) {}
482 /// get() - Static factory methods - Return objects of the specified value
483 static ConstantPointerNull *get(const PointerType *T);
485 /// isNullValue - Return true if this is the value that would be returned by
487 virtual bool isNullValue() const { return true; }
489 virtual void destroyConstant();
491 /// getType - Specialize the getType() method to always return an PointerType,
492 /// which reduces the amount of casting needed in parts of the compiler.
494 inline const PointerType *getType() const {
495 return reinterpret_cast<const PointerType*>(Value::getType());
498 /// Methods for support type inquiry through isa, cast, and dyn_cast:
499 static inline bool classof(const ConstantPointerNull *) { return true; }
500 static bool classof(const Value *V) {
501 return V->getValueType() == SimpleConstantVal &&
502 isa<PointerType>(V->getType());
507 /// ConstantExpr - a constant value that is initialized with an expression using
508 /// other constant values.
510 /// This class uses the standard Instruction opcodes to define the various
511 /// constant expressions. The Opcode field for the ConstantExpr class is
512 /// maintained in the Value::SubclassData field.
513 class ConstantExpr : public Constant {
514 friend struct ConstantCreator<ConstantExpr,Type,
515 std::pair<unsigned, std::vector<Constant*> > >;
516 friend struct ConvertConstantType<ConstantExpr, Type>;
519 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
520 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
521 // Operation type (an Instruction opcode) is stored as the SubclassData.
522 SubclassData = Opcode;
525 // These private methods are used by the type resolution code to create
526 // ConstantExprs in intermediate forms.
527 static Constant *getTy(const Type *Ty, unsigned Opcode,
528 Constant *C1, Constant *C2);
529 static Constant *getShiftTy(const Type *Ty,
530 unsigned Opcode, Constant *C1, Constant *C2);
531 static Constant *getSelectTy(const Type *Ty,
532 Constant *C1, Constant *C2, Constant *C3);
533 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
534 const std::vector<Value*> &IdxList);
537 // Static methods to construct a ConstantExpr of different kinds. Note that
538 // these methods may return a object that is not an instance of the
539 // ConstantExpr class, because they will attempt to fold the constant
540 // expression into something simpler if possible.
542 /// Cast constant expr
544 static Constant *getCast(Constant *C, const Type *Ty);
545 static Constant *getSignExtend(Constant *C, const Type *Ty);
546 static Constant *getZeroExtend(Constant *C, const Type *Ty);
548 /// Select constant expr
550 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
551 return getSelectTy(V1->getType(), C, V1, V2);
554 /// getSizeOf constant expr - computes the size of a type in a target
555 /// independent way (Note: the return type is ULong but the object is not
556 /// necessarily a ConstantUInt).
558 static Constant *getSizeOf(const Type *Ty);
560 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
561 /// return a pointer to a pointer of the array element type.
562 static Constant *getPtrPtrFromArrayPtr(Constant *C);
564 /// ConstantExpr::get - Return a binary or shift operator constant expression,
565 /// folding if possible.
567 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
569 /// ConstantExpr::get* - Return some common constants without having to
570 /// specify the full Instruction::OPCODE identifier.
572 static Constant *getNeg(Constant *C);
573 static Constant *getNot(Constant *C);
574 static Constant *getAdd(Constant *C1, Constant *C2);
575 static Constant *getSub(Constant *C1, Constant *C2);
576 static Constant *getMul(Constant *C1, Constant *C2);
577 static Constant *getDiv(Constant *C1, Constant *C2);
578 static Constant *getRem(Constant *C1, Constant *C2);
579 static Constant *getAnd(Constant *C1, Constant *C2);
580 static Constant *getOr(Constant *C1, Constant *C2);
581 static Constant *getXor(Constant *C1, Constant *C2);
582 static Constant *getSetEQ(Constant *C1, Constant *C2);
583 static Constant *getSetNE(Constant *C1, Constant *C2);
584 static Constant *getSetLT(Constant *C1, Constant *C2);
585 static Constant *getSetGT(Constant *C1, Constant *C2);
586 static Constant *getSetLE(Constant *C1, Constant *C2);
587 static Constant *getSetGE(Constant *C1, Constant *C2);
588 static Constant *getShl(Constant *C1, Constant *C2);
589 static Constant *getShr(Constant *C1, Constant *C2);
591 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
592 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
594 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
595 /// all elements must be Constant's.
597 static Constant *getGetElementPtr(Constant *C,
598 const std::vector<Constant*> &IdxList);
599 static Constant *getGetElementPtr(Constant *C,
600 const std::vector<Value*> &IdxList);
602 /// isNullValue - Return true if this is the value that would be returned by
604 virtual bool isNullValue() const { return false; }
606 /// getOpcode - Return the opcode at the root of this constant expression
607 unsigned getOpcode() const { return SubclassData; }
609 /// getOpcodeName - Return a string representation for an opcode.
610 const char *getOpcodeName() const;
612 virtual void destroyConstant();
613 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
614 bool DisableChecking = false);
616 /// Override methods to provide more type information...
617 inline Constant *getOperand(unsigned i) {
618 return cast<Constant>(User::getOperand(i));
620 inline Constant *getOperand(unsigned i) const {
621 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
625 /// Methods for support type inquiry through isa, cast, and dyn_cast:
626 static inline bool classof(const ConstantExpr *) { return true; }
627 static inline bool classof(const Value *V) {
628 return V->getValueType() == ConstantExprVal;
633 //===----------------------------------------------------------------------===//
634 /// UndefValue - 'undef' values are things that do not have specified contents.
635 /// These are used for a variety of purposes, including global variable
636 /// initializers and operands to instructions. 'undef' values can occur with
639 class UndefValue : public Constant {
640 friend struct ConstantCreator<UndefValue, Type, char>;
641 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
643 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
645 /// get() - Static factory methods - Return an 'undef' object of the specified
648 static UndefValue *get(const Type *T);
650 /// isNullValue - Return true if this is the value that would be returned by
652 virtual bool isNullValue() const { return false; }
654 virtual void destroyConstant();
656 /// Methods for support type inquiry through isa, cast, and dyn_cast:
657 static inline bool classof(const UndefValue *) { return true; }
658 static bool classof(const Value *V) {
659 return V->getValueType() == UndefValueVal;
663 } // End llvm namespace