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 /// isNullValue - Return true if this is the value that would be returned by
136 virtual bool isNullValue() const { return Val.Unsigned == 0; }
137 virtual bool isMaxValue() const = 0;
138 virtual bool isMinValue() const = 0;
140 /// Methods for support type inquiry through isa, cast, and dyn_cast:
141 static inline bool classof(const ConstantInt *) { return true; }
142 static bool classof(const Constant *CPV); // defined in Constants.cpp
143 static inline bool classof(const Value *V) {
144 return isa<Constant>(V) && classof(cast<Constant>(V));
149 //===---------------------------------------------------------------------------
150 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
152 class ConstantSInt : public ConstantInt {
153 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
154 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
157 ConstantSInt(const Type *Ty, int64_t V);
159 /// get() - Static factory methods - Return objects of the specified value
160 static ConstantSInt *get(const Type *Ty, int64_t V);
162 /// isValueValidForType - return true if Ty is big enough to represent V.
163 static bool isValueValidForType(const Type *Ty, int64_t V);
165 /// getValue - return the underlying value of this constant.
166 inline int64_t getValue() const { return Val.Signed; }
168 virtual bool isAllOnesValue() const { return getValue() == -1; }
170 /// isMaxValue - Return true if this is the largest value that may be
171 /// represented by this type.
173 virtual bool isMaxValue() const {
174 int64_t V = getValue();
175 if (V < 0) return false; // Be careful about wrap-around on 'long's
177 return !isValueValidForType(getType(), V) || V < 0;
180 /// isMinValue - Return true if this is the smallest value that may be
181 /// represented by this type.
183 virtual bool isMinValue() const {
184 int64_t V = getValue();
185 if (V > 0) return false; // Be careful about wrap-around on 'long's
187 return !isValueValidForType(getType(), V) || V > 0;
190 /// Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const ConstantSInt *) { return true; }
192 static bool classof(const Constant *CPV); // defined in Constants.cpp
193 static inline bool classof(const Value *V) {
194 return isa<Constant>(V) && classof(cast<Constant>(V));
198 //===---------------------------------------------------------------------------
199 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
201 class ConstantUInt : public ConstantInt {
202 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
203 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
205 ConstantUInt(const Type *Ty, uint64_t V);
207 /// get() - Static factory methods - Return objects of the specified value
208 static ConstantUInt *get(const Type *Ty, uint64_t V);
210 /// isValueValidForType - return true if Ty is big enough to represent V.
211 static bool isValueValidForType(const Type *Ty, uint64_t V);
213 /// getValue - return the underlying value of this constant.
214 inline uint64_t getValue() const { return Val.Unsigned; }
216 /// isMaxValue - Return true if this is the largest value that may be
217 /// represented by this type.
219 virtual bool isAllOnesValue() const;
220 virtual bool isMaxValue() const { return isAllOnesValue(); }
221 virtual bool isMinValue() const { return getValue() == 0; }
223 /// Methods for support type inquiry through isa, cast, and dyn_cast:
224 static inline bool classof(const ConstantUInt *) { return true; }
225 static bool classof(const Constant *CPV); // defined in Constants.cpp
226 static inline bool classof(const Value *V) {
227 return isa<Constant>(V) && classof(cast<Constant>(V));
232 //===---------------------------------------------------------------------------
233 /// ConstantFP - Floating Point Values [float, double]
235 class ConstantFP : public Constant {
237 friend struct ConstantCreator<ConstantFP, Type, double>;
238 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
240 ConstantFP(const Type *Ty, double V);
242 /// get() - Static factory methods - Return objects of the specified value
243 static ConstantFP *get(const Type *Ty, double V);
245 /// isValueValidForType - return true if Ty is big enough to represent V.
246 static bool isValueValidForType(const Type *Ty, double V);
247 inline double getValue() const { return Val; }
249 /// isNullValue - Return true if this is the value that would be returned by
251 virtual bool isNullValue() const { return Val == 0; }
253 /// Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const ConstantFP *) { return true; }
255 static bool classof(const Constant *CPV); // defined in Constants.cpp
256 static inline bool classof(const Value *V) {
257 return isa<Constant>(V) && classof(cast<Constant>(V));
262 //===---------------------------------------------------------------------------
263 /// ConstantArray - Constant Array Declarations
265 class ConstantArray : public Constant {
266 friend struct ConstantCreator<ConstantArray, ArrayType,
267 std::vector<Constant*> >;
268 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
270 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
271 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
273 /// get() - Static factory methods - Return objects of the specified value
274 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
275 static ConstantArray *get(const std::string &Initializer);
277 /// getType - Specialize the getType() method to always return an ArrayType,
278 /// which reduces the amount of casting needed in parts of the compiler.
280 inline const ArrayType *getType() const {
281 return (ArrayType*)Value::getType();
284 /// getAsString - If the sub-element type of this array is either sbyte or
285 /// ubyte, then this method converts the array to an std::string and returns
286 /// it. Otherwise, it asserts out.
288 std::string getAsString() const;
290 /// getValues - Return a vector of the component constants that make up this
292 inline const std::vector<Use> &getValues() const { return Operands; }
294 /// isNullValue - Return true if this is the value that would be returned by
296 virtual bool isNullValue() const {
297 // FIXME: This should be made to be MUCH faster. Just check against well
299 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
300 if (!cast<Constant>(getOperand(i))->isNullValue())
305 virtual void destroyConstant();
306 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
308 /// Methods for support type inquiry through isa, cast, and dyn_cast:
309 static inline bool classof(const ConstantArray *) { return true; }
310 static bool classof(const Constant *CPV); // defined in Constants.cpp
311 static inline bool classof(const Value *V) {
312 return isa<Constant>(V) && classof(cast<Constant>(V));
317 //===---------------------------------------------------------------------------
318 // ConstantStruct - Constant Struct Declarations
320 class ConstantStruct : public Constant {
321 friend struct ConstantCreator<ConstantStruct, StructType,
322 std::vector<Constant*> >;
323 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
325 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
326 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
328 /// get() - Static factory methods - Return objects of the specified value
329 static ConstantStruct *get(const StructType *T,
330 const std::vector<Constant*> &V);
332 /// getType() specialization - Reduce amount of casting...
333 inline const StructType *getType() const {
334 return (StructType*)Value::getType();
337 /// getValues - Return a vector of the component constants that make up this
339 inline const std::vector<Use> &getValues() const { return Operands; }
341 /// isNullValue - Return true if this is the value that would be returned by
343 virtual bool isNullValue() const {
344 // FIXME: This should be made to be MUCH faster. Just check against well
346 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
347 if (!cast<Constant>(getOperand(i))->isNullValue())
352 virtual void destroyConstant();
353 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
355 /// Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const ConstantStruct *) { return true; }
357 static bool classof(const Constant *CPV); // defined in Constants.cpp
358 static inline bool classof(const Value *V) {
359 return isa<Constant>(V) && classof(cast<Constant>(V));
363 //===---------------------------------------------------------------------------
364 /// ConstantPointer - Constant Pointer Declarations
366 /// The ConstantPointer class represents a null pointer of a specific type. For
367 /// a more specific/useful instance, a subclass of ConstantPointer should be
370 class ConstantPointer : public Constant {
371 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
373 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
375 inline const PointerType *getType() const {
376 return (PointerType*)Value::getType();
379 /// isNullValue - Return true if this is the value that would be returned by
381 virtual bool isNullValue() const { return false; }
383 /// Methods for support type inquiry through isa, cast, and dyn_cast:
384 static inline bool classof(const ConstantPointer *) { return true; }
385 static bool classof(const Constant *CPV); // defined in Constants.cpp
386 static inline bool classof(const Value *V) {
387 return isa<Constant>(V) && classof(cast<Constant>(V));
391 /// ConstantPointerNull - a constant pointer value that points to null
393 class ConstantPointerNull : public ConstantPointer {
394 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
395 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
397 ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
398 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
402 /// get() - Static factory methods - Return objects of the specified value
403 static ConstantPointerNull *get(const PointerType *T);
405 /// isNullValue - Return true if this is the value that would be returned by
407 virtual bool isNullValue() const { return true; }
409 virtual void destroyConstant();
411 /// Methods for support type inquiry through isa, cast, and dyn_cast:
412 static inline bool classof(const ConstantPointerNull *) { return true; }
413 static inline bool classof(const ConstantPointer *P) {
414 return (P->getNumOperands() == 0 && P->isNullValue());
416 static inline bool classof(const Constant *CPV) {
417 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
419 static inline bool classof(const Value *V) {
420 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
425 /// ConstantPointerRef - a constant pointer value that is initialized to
426 /// point to a global value, which lies at a constant, fixed address.
428 class ConstantPointerRef : public ConstantPointer {
429 friend class Module; // Modules maintain these references
430 ConstantPointerRef(const ConstantPointerRef &); // DNI!
433 ConstantPointerRef(GlobalValue *GV);
435 /// get() - Static factory methods - Return objects of the specified value
436 static ConstantPointerRef *get(GlobalValue *GV);
438 const GlobalValue *getValue() const {
439 return cast<GlobalValue>(Operands[0].get());
442 GlobalValue *getValue() {
443 return cast<GlobalValue>(Operands[0].get());
446 virtual void destroyConstant();
447 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
449 /// Methods for support type inquiry through isa, cast, and dyn_cast:
450 static inline bool classof(const ConstantPointerRef *) { return true; }
451 static inline bool classof(const ConstantPointer *CPV) {
452 // check for a single operand (the target value)
453 return (CPV->getNumOperands() == 1);
455 static inline bool classof(const Constant *CPV) {
456 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
458 static inline bool classof(const Value *V) {
459 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
463 // ConstantExpr - a constant value that is initialized with an expression using
464 // other constant values. This is only used to represent values that cannot be
465 // evaluated at compile-time (e.g., something derived from an address) because
466 // it does not have a mechanism to store the actual value. Use the appropriate
467 // Constant subclass above for known constants.
469 class ConstantExpr : public Constant {
470 unsigned iType; // Operation type (an Instruction opcode)
471 friend struct ConstantCreator<ConstantExpr,Type,
472 std::pair<unsigned, std::vector<Constant*> > >;
475 // Cast creation ctor
476 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
477 // Binary/Shift instruction creation ctor
478 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
479 // GEP instruction creation ctor
480 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
482 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
485 // Static methods to construct a ConstantExpr of different kinds. Note that
486 // these methods may return a object that is not an instance of the
487 // ConstantExpr class, because they will attempt to fold the constant
488 // expression into something simpler if possible.
490 /// Cast constant expr
491 static Constant *getCast(Constant *C, const Type *Ty);
493 /// Binary constant expr - Use with binary operators...
494 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
496 /// getShift - Return a shift left or shift right constant expr
497 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2);
499 /// Getelementptr form...
500 static Constant *getGetElementPtr(Constant *C,
501 const std::vector<Constant*> &IdxList);
503 /// isNullValue - Return true if this is the value that would be returned by
505 virtual bool isNullValue() const { return false; }
507 /// getOpcode - Return the opcode at the root of this constant expression
508 unsigned getOpcode() const { return iType; }
510 /// getOpcodeName - Return a string representation for an opcode.
511 const char *getOpcodeName() const;
513 /// isConstantExpr - Return true if this is a ConstantExpr
514 virtual bool isConstantExpr() const { return true; }
516 virtual void destroyConstant();
517 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
519 /// Override methods to provide more type information...
520 inline Constant *getOperand(unsigned i) {
521 return cast<Constant>(User::getOperand(i));
523 inline Constant *getOperand(unsigned i) const {
524 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
528 /// Methods for support type inquiry through isa, cast, and dyn_cast:
529 static inline bool classof(const ConstantExpr *) { return true; }
530 static inline bool classof(const Constant *CPV) {
531 return CPV->isConstantExpr();
533 static inline bool classof(const Value *V) {
534 return isa<Constant>(V) && classof(cast<Constant>(V));