1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
18 template<class ConstantClass, class TypeClass, class ValType>
19 struct ConstantCreator;
21 //===---------------------------------------------------------------------------
22 /// ConstantIntegral - Shared superclass of boolean and integer constants.
24 /// This class just defines some common interfaces to be implemented.
26 class ConstantIntegral : public Constant {
28 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
31 /// isNullValue - Return true if this is the value that would be returned by
34 virtual bool isNullValue() const = 0;
36 /// isMaxValue - Return true if this is the largest value that may be
37 /// represented by this type.
39 virtual bool isMaxValue() const = 0;
41 /// isMinValue - Return true if this is the smallest value that may be
42 /// represented by this type.
44 virtual bool isMinValue() const = 0;
46 /// isAllOnesValue - Return true if every bit in this constant is set to true.
48 virtual bool isAllOnesValue() const = 0;
50 /// Static constructor to get the maximum/minimum/allones constant of
51 /// specified (integral) type...
53 static ConstantIntegral *getMaxValue(const Type *Ty);
54 static ConstantIntegral *getMinValue(const Type *Ty);
55 static ConstantIntegral *getAllOnesValue(const Type *Ty);
57 /// Methods for support type inquiry through isa, cast, and dyn_cast:
58 static inline bool classof(const ConstantIntegral *) { return true; }
59 static bool classof(const Constant *CPV); // defined in Constants.cpp
60 static inline bool classof(const Value *V) {
61 return isa<Constant>(V) && classof(cast<Constant>(V));
66 //===---------------------------------------------------------------------------
67 /// ConstantBool - Boolean Values
69 class ConstantBool : public ConstantIntegral {
73 static ConstantBool *True, *False; // The True & False values
75 /// get() - Static factory methods - Return objects of the specified value
76 static ConstantBool *get(bool Value) { return Value ? True : False; }
77 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
79 /// inverted - Return the opposite value of the current value.
80 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
82 /// getValue - return the boolean value of this constant.
84 inline bool getValue() const { return Val; }
86 /// isNullValue - Return true if this is the value that would be returned by
89 virtual bool isNullValue() const { return this == False; }
90 virtual bool isMaxValue() const { return this == True; }
91 virtual bool isMinValue() const { return this == False; }
92 virtual bool isAllOnesValue() const { return this == True; }
94 /// Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const ConstantBool *) { return true; }
96 static bool classof(const Constant *CPV) {
97 return (CPV == True) | (CPV == False);
99 static inline bool classof(const Value *V) {
100 return isa<Constant>(V) && classof(cast<Constant>(V));
105 //===---------------------------------------------------------------------------
106 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
107 /// with integral constants easier.
109 class ConstantInt : public ConstantIntegral {
115 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
116 ConstantInt(const Type *Ty, uint64_t V);
118 /// equalsInt - Provide a helper method that can be used to determine if the
119 /// constant contained within is equal to a constant. This only works for
120 /// very small values, because this is all that can be represented with all
123 bool equalsInt(unsigned char V) const {
125 "equals: Can only be used with very small positive constants!");
126 return Val.Unsigned == V;
129 /// ConstantInt::get static method: return a ConstantInt with the specified
130 /// value. as above, we work only with very small values here.
132 static ConstantInt *get(const Type *Ty, unsigned char V);
134 /// getRawValue - return the underlying value of this constant as a 64-bit
135 /// unsigned integer value.
137 inline uint64_t getRawValue() const { return Val.Unsigned; }
139 /// isNullValue - Return true if this is the value that would be returned by
141 virtual bool isNullValue() const { return Val.Unsigned == 0; }
142 virtual bool isMaxValue() const = 0;
143 virtual bool isMinValue() const = 0;
145 /// Methods for support type inquiry through isa, cast, and dyn_cast:
146 static inline bool classof(const ConstantInt *) { return true; }
147 static bool classof(const Constant *CPV); // defined in Constants.cpp
148 static inline bool classof(const Value *V) {
149 return isa<Constant>(V) && classof(cast<Constant>(V));
154 //===---------------------------------------------------------------------------
155 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
157 class ConstantSInt : public ConstantInt {
158 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
159 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
162 ConstantSInt(const Type *Ty, int64_t V);
164 /// get() - Static factory methods - Return objects of the specified value
166 static ConstantSInt *get(const Type *Ty, int64_t V);
168 /// isValueValidForType - return true if Ty is big enough to represent V.
170 static bool isValueValidForType(const Type *Ty, int64_t V);
172 /// getValue - return the underlying value of this constant.
174 inline int64_t getValue() const { return Val.Signed; }
176 virtual bool isAllOnesValue() const { return getValue() == -1; }
178 /// isMaxValue - Return true if this is the largest value that may be
179 /// represented by this type.
181 virtual bool isMaxValue() const {
182 int64_t V = getValue();
183 if (V < 0) return false; // Be careful about wrap-around on 'long's
185 return !isValueValidForType(getType(), V) || V < 0;
188 /// isMinValue - Return true if this is the smallest value that may be
189 /// represented by this type.
191 virtual bool isMinValue() const {
192 int64_t V = getValue();
193 if (V > 0) return false; // Be careful about wrap-around on 'long's
195 return !isValueValidForType(getType(), V) || V > 0;
198 /// Methods for support type inquiry through isa, cast, and dyn_cast:
200 static inline bool classof(const ConstantSInt *) { return true; }
201 static bool classof(const Constant *CPV); // defined in Constants.cpp
202 static inline bool classof(const Value *V) {
203 return isa<Constant>(V) && classof(cast<Constant>(V));
207 //===---------------------------------------------------------------------------
208 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
210 class ConstantUInt : public ConstantInt {
211 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
212 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
214 ConstantUInt(const Type *Ty, uint64_t V);
216 /// get() - Static factory methods - Return objects of the specified value
218 static ConstantUInt *get(const Type *Ty, uint64_t V);
220 /// isValueValidForType - return true if Ty is big enough to represent V.
222 static bool isValueValidForType(const Type *Ty, uint64_t V);
224 /// getValue - return the underlying value of this constant.
226 inline uint64_t getValue() const { return Val.Unsigned; }
228 /// isMaxValue - Return true if this is the largest value that may be
229 /// represented by this type.
231 virtual bool isAllOnesValue() const;
232 virtual bool isMaxValue() const { return isAllOnesValue(); }
233 virtual bool isMinValue() const { return getValue() == 0; }
235 /// Methods for support type inquiry through isa, cast, and dyn_cast:
236 static inline bool classof(const ConstantUInt *) { return true; }
237 static bool classof(const Constant *CPV); // defined in Constants.cpp
238 static inline bool classof(const Value *V) {
239 return isa<Constant>(V) && classof(cast<Constant>(V));
244 //===---------------------------------------------------------------------------
245 /// ConstantFP - Floating Point Values [float, double]
247 class ConstantFP : public Constant {
249 friend struct ConstantCreator<ConstantFP, Type, double>;
250 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
252 ConstantFP(const Type *Ty, double V);
254 /// get() - Static factory methods - Return objects of the specified value
255 static ConstantFP *get(const Type *Ty, double V);
257 /// isValueValidForType - return true if Ty is big enough to represent V.
258 static bool isValueValidForType(const Type *Ty, double V);
259 inline double getValue() const { return Val; }
261 /// isNullValue - Return true if this is the value that would be returned by
263 virtual bool isNullValue() const { return Val == 0; }
265 /// Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const ConstantFP *) { return true; }
267 static bool classof(const Constant *CPV); // defined in Constants.cpp
268 static inline bool classof(const Value *V) {
269 return isa<Constant>(V) && classof(cast<Constant>(V));
274 //===---------------------------------------------------------------------------
275 /// ConstantArray - Constant Array Declarations
277 class ConstantArray : public Constant {
278 friend struct ConstantCreator<ConstantArray, ArrayType,
279 std::vector<Constant*> >;
280 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
282 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
283 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
285 /// get() - Static factory methods - Return objects of the specified value
286 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
287 static ConstantArray *get(const std::string &Initializer);
289 /// getType - Specialize the getType() method to always return an ArrayType,
290 /// which reduces the amount of casting needed in parts of the compiler.
292 inline const ArrayType *getType() const {
293 return (ArrayType*)Value::getType();
296 /// getAsString - If the sub-element type of this array is either sbyte or
297 /// ubyte, then this method converts the array to an std::string and returns
298 /// it. Otherwise, it asserts out.
300 std::string getAsString() const;
302 /// getValues - Return a vector of the component constants that make up this
304 inline const std::vector<Use> &getValues() const { return Operands; }
306 /// isNullValue - Return true if this is the value that would be returned by
308 virtual bool isNullValue() const {
309 // FIXME: This should be made to be MUCH faster. Just check against well
311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
312 if (!cast<Constant>(getOperand(i))->isNullValue())
317 virtual void destroyConstant();
318 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
320 /// Methods for support type inquiry through isa, cast, and dyn_cast:
321 static inline bool classof(const ConstantArray *) { return true; }
322 static bool classof(const Constant *CPV); // defined in Constants.cpp
323 static inline bool classof(const Value *V) {
324 return isa<Constant>(V) && classof(cast<Constant>(V));
329 //===---------------------------------------------------------------------------
330 // ConstantStruct - Constant Struct Declarations
332 class ConstantStruct : public Constant {
333 friend struct ConstantCreator<ConstantStruct, StructType,
334 std::vector<Constant*> >;
335 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
337 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
338 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
340 /// get() - Static factory methods - Return objects of the specified value
341 static ConstantStruct *get(const StructType *T,
342 const std::vector<Constant*> &V);
344 /// getType() specialization - Reduce amount of casting...
345 inline const StructType *getType() const {
346 return (StructType*)Value::getType();
349 /// getValues - Return a vector of the component constants that make up this
351 inline const std::vector<Use> &getValues() const { return Operands; }
353 /// isNullValue - Return true if this is the value that would be returned by
355 virtual bool isNullValue() const {
356 // FIXME: This should be made to be MUCH faster. Just check against well
358 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
359 if (!cast<Constant>(getOperand(i))->isNullValue())
364 virtual void destroyConstant();
365 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
367 /// Methods for support type inquiry through isa, cast, and dyn_cast:
368 static inline bool classof(const ConstantStruct *) { return true; }
369 static bool classof(const Constant *CPV); // defined in Constants.cpp
370 static inline bool classof(const Value *V) {
371 return isa<Constant>(V) && classof(cast<Constant>(V));
375 //===---------------------------------------------------------------------------
376 /// ConstantPointer - Constant Pointer Declarations
378 /// The ConstantPointer class represents a null pointer of a specific type. For
379 /// a more specific/useful instance, a subclass of ConstantPointer should be
382 class ConstantPointer : public Constant {
383 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
385 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
387 inline const PointerType *getType() const {
388 return (PointerType*)Value::getType();
391 /// isNullValue - Return true if this is the value that would be returned by
393 virtual bool isNullValue() const { return false; }
395 /// Methods for support type inquiry through isa, cast, and dyn_cast:
396 static inline bool classof(const ConstantPointer *) { return true; }
397 static bool classof(const Constant *CPV); // defined in Constants.cpp
398 static inline bool classof(const Value *V) {
399 return isa<Constant>(V) && classof(cast<Constant>(V));
403 /// ConstantPointerNull - a constant pointer value that points to null
405 class ConstantPointerNull : public ConstantPointer {
406 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
407 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
409 ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
410 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
414 /// get() - Static factory methods - Return objects of the specified value
415 static ConstantPointerNull *get(const PointerType *T);
417 /// isNullValue - Return true if this is the value that would be returned by
419 virtual bool isNullValue() const { return true; }
421 virtual void destroyConstant();
423 /// Methods for support type inquiry through isa, cast, and dyn_cast:
424 static inline bool classof(const ConstantPointerNull *) { return true; }
425 static inline bool classof(const ConstantPointer *P) {
426 return (P->getNumOperands() == 0 && P->isNullValue());
428 static inline bool classof(const Constant *CPV) {
429 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
431 static inline bool classof(const Value *V) {
432 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
437 /// ConstantPointerRef - a constant pointer value that is initialized to
438 /// point to a global value, which lies at a constant, fixed address.
440 class ConstantPointerRef : public ConstantPointer {
441 friend class Module; // Modules maintain these references
442 ConstantPointerRef(const ConstantPointerRef &); // DNI!
445 ConstantPointerRef(GlobalValue *GV);
447 /// get() - Static factory methods - Return objects of the specified value
448 static ConstantPointerRef *get(GlobalValue *GV);
450 const GlobalValue *getValue() const {
451 return cast<GlobalValue>(Operands[0].get());
454 GlobalValue *getValue() {
455 return cast<GlobalValue>(Operands[0].get());
458 virtual void destroyConstant();
459 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
461 /// Methods for support type inquiry through isa, cast, and dyn_cast:
462 static inline bool classof(const ConstantPointerRef *) { return true; }
463 static inline bool classof(const ConstantPointer *CPV) {
464 // check for a single operand (the target value)
465 return (CPV->getNumOperands() == 1);
467 static inline bool classof(const Constant *CPV) {
468 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
470 static inline bool classof(const Value *V) {
471 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
475 // ConstantExpr - a constant value that is initialized with an expression using
476 // other constant values. This is only used to represent values that cannot be
477 // evaluated at compile-time (e.g., something derived from an address) because
478 // it does not have a mechanism to store the actual value. Use the appropriate
479 // Constant subclass above for known constants.
481 class ConstantExpr : public Constant {
482 unsigned iType; // Operation type (an Instruction opcode)
483 friend struct ConstantCreator<ConstantExpr,Type,
484 std::pair<unsigned, std::vector<Constant*> > >;
487 // Cast creation ctor
488 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
489 // Binary/Shift instruction creation ctor
490 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
491 // GEP instruction creation ctor
492 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
494 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
497 // Static methods to construct a ConstantExpr of different kinds. Note that
498 // these methods may return a object that is not an instance of the
499 // ConstantExpr class, because they will attempt to fold the constant
500 // expression into something simpler if possible.
502 /// Cast constant expr
503 static Constant *getCast(Constant *C, const Type *Ty);
505 /// Binary constant expr - Use with binary operators...
506 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
508 /// getShift - Return a shift left or shift right constant expr
509 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2);
511 /// Getelementptr form...
512 static Constant *getGetElementPtr(Constant *C,
513 const std::vector<Constant*> &IdxList);
515 /// isNullValue - Return true if this is the value that would be returned by
517 virtual bool isNullValue() const { return false; }
519 /// getOpcode - Return the opcode at the root of this constant expression
520 unsigned getOpcode() const { return iType; }
522 /// getOpcodeName - Return a string representation for an opcode.
523 const char *getOpcodeName() const;
525 /// isConstantExpr - Return true if this is a ConstantExpr
526 virtual bool isConstantExpr() const { return true; }
528 virtual void destroyConstant();
529 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
531 /// Override methods to provide more type information...
532 inline Constant *getOperand(unsigned i) {
533 return cast<Constant>(User::getOperand(i));
535 inline Constant *getOperand(unsigned i) const {
536 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
540 /// Methods for support type inquiry through isa, cast, and dyn_cast:
541 static inline bool classof(const ConstantExpr *) { return true; }
542 static inline bool classof(const Constant *CPV) {
543 return CPV->isConstantExpr();
545 static inline bool classof(const Value *V) {
546 return isa<Constant>(V) && classof(cast<Constant>(V));