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"
19 //===---------------------------------------------------------------------------
20 /// ConstantIntegral - Shared superclass of boolean and integer constants.
22 /// This class just defines some common interfaces to be implemented.
24 class ConstantIntegral : public Constant {
26 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
29 /// isNullValue - Return true if this is the value that would be returned by
32 virtual bool isNullValue() const = 0;
34 /// isMaxValue - Return true if this is the largest value that may be
35 /// represented by this type.
37 virtual bool isMaxValue() const = 0;
39 /// isMinValue - Return true if this is the smallest value that may be
40 /// represented by this type.
42 virtual bool isMinValue() const = 0;
44 /// isAllOnesValue - Return true if every bit in this constant is set to true.
46 virtual bool isAllOnesValue() const = 0;
48 /// Static constructor to get the maximum/minimum/allones constant of
49 /// specified (integral) type...
51 static ConstantIntegral *getMaxValue(const Type *Ty);
52 static ConstantIntegral *getMinValue(const Type *Ty);
53 static ConstantIntegral *getAllOnesValue(const Type *Ty);
55 /// Methods for support type inquiry through isa, cast, and dyn_cast:
56 static inline bool classof(const ConstantIntegral *) { return true; }
57 static bool classof(const Constant *CPV); // defined in Constants.cpp
58 static inline bool classof(const Value *V) {
59 return isa<Constant>(V) && classof(cast<Constant>(V));
64 //===---------------------------------------------------------------------------
65 /// ConstantBool - Boolean Values
67 class ConstantBool : public ConstantIntegral {
72 static ConstantBool *True, *False; // The True & False values
74 /// get() - Static factory methods - Return objects of the specified value
75 static ConstantBool *get(bool Value) { return Value ? True : False; }
76 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
78 /// inverted - Return the opposite value of the current value.
79 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
81 /// getValue - return the boolean value of this constant.
83 inline bool getValue() const { return Val; }
85 /// isNullValue - Return true if this is the value that would be returned by
88 virtual bool isNullValue() const { return this == False; }
89 virtual bool isMaxValue() const { return this == True; }
90 virtual bool isMinValue() const { return this == False; }
91 virtual bool isAllOnesValue() const { return this == True; }
93 /// Methods for support type inquiry through isa, cast, and dyn_cast:
94 static inline bool classof(const ConstantBool *) { return true; }
95 static bool classof(const Constant *CPV) {
96 return (CPV == True) | (CPV == False);
98 static inline bool classof(const Value *V) {
99 return isa<Constant>(V) && classof(cast<Constant>(V));
104 //===---------------------------------------------------------------------------
105 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
106 /// with integral constants easier.
108 class ConstantInt : public ConstantIntegral {
114 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
115 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 isAllOnesValue() const { return Val.Signed == -1; }
138 virtual bool isMaxValue() const = 0;
139 virtual bool isMinValue() const = 0;
141 /// Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const ConstantInt *) { return true; }
143 static bool classof(const Constant *CPV); // defined in Constants.cpp
144 static inline bool classof(const Value *V) {
145 return isa<Constant>(V) && classof(cast<Constant>(V));
150 //===---------------------------------------------------------------------------
151 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
153 class ConstantSInt : public ConstantInt {
154 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
156 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 /// isMaxValue - Return true if this is the largest value that may be
169 /// represented by this type.
171 virtual bool isMaxValue() const {
172 int64_t V = getValue();
173 if (V < 0) return false; // Be careful about wrap-around on 'long's
175 return !isValueValidForType(getType(), V) || V < 0;
178 /// isMinValue - Return true if this is the smallest value that may be
179 /// represented by this type.
181 virtual bool isMinValue() 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 /// Methods for support type inquiry through isa, cast, and dyn_cast:
189 static inline bool classof(const ConstantSInt *) { return true; }
190 static bool classof(const Constant *CPV); // defined in Constants.cpp
191 static inline bool classof(const Value *V) {
192 return isa<Constant>(V) && classof(cast<Constant>(V));
196 //===---------------------------------------------------------------------------
197 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
199 class ConstantUInt : public ConstantInt {
200 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
202 ConstantUInt(const Type *Ty, uint64_t V);
205 /// get() - Static factory methods - Return objects of the specified value
206 static ConstantUInt *get(const Type *Ty, uint64_t V);
208 /// isValueValidForType - return true if Ty is big enough to represent V.
209 static bool isValueValidForType(const Type *Ty, uint64_t V);
211 /// getValue - return the underlying value of this constant.
212 inline uint64_t getValue() const { return Val.Unsigned; }
214 /// isMaxValue - Return true if this is the largest value that may be
215 /// represented by this type.
217 virtual bool isMaxValue() const { return isAllOnesValue(); }
218 virtual bool isMinValue() const { return getValue() == 0; }
220 /// Methods for support type inquiry through isa, cast, and dyn_cast:
221 static inline bool classof(const ConstantUInt *) { return true; }
222 static bool classof(const Constant *CPV); // defined in Constants.cpp
223 static inline bool classof(const Value *V) {
224 return isa<Constant>(V) && classof(cast<Constant>(V));
229 //===---------------------------------------------------------------------------
230 /// ConstantFP - Floating Point Values [float, double]
232 class ConstantFP : public Constant {
234 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
236 ConstantFP(const Type *Ty, double V);
239 /// get() - Static factory methods - Return objects of the specified value
240 static ConstantFP *get(const Type *Ty, double V);
242 /// isValueValidForType - return true if Ty is big enough to represent V.
243 static bool isValueValidForType(const Type *Ty, double V);
244 inline double getValue() const { return Val; }
246 /// isNullValue - Return true if this is the value that would be returned by
248 virtual bool isNullValue() const { return Val == 0; }
250 /// Methods for support type inquiry through isa, cast, and dyn_cast:
251 static inline bool classof(const ConstantFP *) { return true; }
252 static bool classof(const Constant *CPV); // defined in Constants.cpp
253 static inline bool classof(const Value *V) {
254 return isa<Constant>(V) && classof(cast<Constant>(V));
259 //===---------------------------------------------------------------------------
260 /// ConstantArray - Constant Array Declarations
262 class ConstantArray : public Constant {
263 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
265 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
269 /// get() - Static factory methods - Return objects of the specified value
270 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
271 static ConstantArray *get(const std::string &Initializer);
273 /// getType - Specialize the getType() method to always return an ArrayType,
274 /// which reduces the amount of casting needed in parts of the compiler.
276 inline const ArrayType *getType() const {
277 return (ArrayType*)Value::getType();
280 /// getAsString - If the sub-element type of this array is either sbyte or
281 /// ubyte, then this method converts the array to an std::string and returns
282 /// it. Otherwise, it asserts out.
284 std::string getAsString() const;
286 /// getValues - Return a vector of the component constants that make up this
288 inline const std::vector<Use> &getValues() const { return Operands; }
290 /// isNullValue - Return true if this is the value that would be returned by
292 virtual bool isNullValue() const {
293 // FIXME: This should be made to be MUCH faster. Just check against well
295 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
296 if (!cast<Constant>(getOperand(i))->isNullValue())
301 virtual void destroyConstant();
302 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
304 /// Methods for support type inquiry through isa, cast, and dyn_cast:
305 static inline bool classof(const ConstantArray *) { return true; }
306 static bool classof(const Constant *CPV); // defined in Constants.cpp
307 static inline bool classof(const Value *V) {
308 return isa<Constant>(V) && classof(cast<Constant>(V));
313 //===---------------------------------------------------------------------------
314 // ConstantStruct - Constant Struct Declarations
316 class ConstantStruct : public Constant {
317 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
319 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
323 /// get() - Static factory methods - Return objects of the specified value
324 static ConstantStruct *get(const StructType *T,
325 const std::vector<Constant*> &V);
327 /// getType() specialization - Reduce amount of casting...
328 inline const StructType *getType() const {
329 return (StructType*)Value::getType();
332 /// getValues - Return a vector of the component constants that make up this
334 inline const std::vector<Use> &getValues() const { return Operands; }
336 /// isNullValue - Return true if this is the value that would be returned by
338 virtual bool isNullValue() const {
339 // FIXME: This should be made to be MUCH faster. Just check against well
341 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
342 if (!cast<Constant>(getOperand(i))->isNullValue())
347 virtual void destroyConstant();
348 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
350 /// Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const ConstantStruct *) { return true; }
352 static bool classof(const Constant *CPV); // defined in Constants.cpp
353 static inline bool classof(const Value *V) {
354 return isa<Constant>(V) && classof(cast<Constant>(V));
358 //===---------------------------------------------------------------------------
359 /// ConstantPointer - Constant Pointer Declarations
361 /// The ConstantPointer class represents a null pointer of a specific type. For
362 /// a more specific/useful instance, a subclass of ConstantPointer should be
365 class ConstantPointer : public Constant {
366 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
368 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
369 ~ConstantPointer() {}
371 inline const PointerType *getType() const {
372 return (PointerType*)Value::getType();
375 /// isNullValue - Return true if this is the value that would be returned by
377 virtual bool isNullValue() const { return false; }
379 /// Methods for support type inquiry through isa, cast, and dyn_cast:
380 static inline bool classof(const ConstantPointer *) { return true; }
381 static bool classof(const Constant *CPV); // defined in Constants.cpp
382 static inline bool classof(const Value *V) {
383 return isa<Constant>(V) && classof(cast<Constant>(V));
387 /// ConstantPointerNull - a constant pointer value that points to null
389 class ConstantPointerNull : public ConstantPointer {
390 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
392 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
393 inline ~ConstantPointerNull() {}
396 /// get() - Static factory methods - Return objects of the specified value
397 static ConstantPointerNull *get(const PointerType *T);
399 /// isNullValue - Return true if this is the value that would be returned by
401 virtual bool isNullValue() const { return true; }
403 virtual void destroyConstant();
405 /// Methods for support type inquiry through isa, cast, and dyn_cast:
406 static inline bool classof(const ConstantPointerNull *) { return true; }
407 static inline bool classof(const ConstantPointer *P) {
408 return (P->getNumOperands() == 0 && P->isNullValue());
410 static inline bool classof(const Constant *CPV) {
411 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
413 static inline bool classof(const Value *V) {
414 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
419 /// ConstantPointerRef - a constant pointer value that is initialized to
420 /// point to a global value, which lies at a constant, fixed address.
422 class ConstantPointerRef : public ConstantPointer {
423 friend class Module; // Modules maintain these references
424 ConstantPointerRef(const ConstantPointerRef &); // DNI!
427 ConstantPointerRef(GlobalValue *GV);
428 ~ConstantPointerRef() {}
430 /// get() - Static factory methods - Return objects of the specified value
431 static ConstantPointerRef *get(GlobalValue *GV);
433 const GlobalValue *getValue() const {
434 return cast<GlobalValue>(Operands[0].get());
437 GlobalValue *getValue() {
438 return cast<GlobalValue>(Operands[0].get());
441 virtual void destroyConstant();
442 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
444 /// Methods for support type inquiry through isa, cast, and dyn_cast:
445 static inline bool classof(const ConstantPointerRef *) { return true; }
446 static inline bool classof(const ConstantPointer *CPV) {
447 // check for a single operand (the target value)
448 return (CPV->getNumOperands() == 1);
450 static inline bool classof(const Constant *CPV) {
451 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
453 static inline bool classof(const Value *V) {
454 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
459 // ConstantExpr - a constant value that is initialized with
460 // an expression using other constant values. This is only used
461 // to represent values that cannot be evaluated at compile-time
462 // (e.g., something derived from an address) because it does
463 // not have a mechanism to store the actual value.
464 // Use the appropriate Constant subclass above for known constants.
466 class ConstantExpr : public Constant {
467 unsigned iType; // Operation type
470 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
471 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
472 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
477 // Static methods to construct a ConstantExpr of different kinds.
479 /// Cast constant expr
480 static ConstantExpr *getCast(Constant *C, const Type *Ty);
482 /// Binary constant expr - Use with binary operators...
483 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
485 /// Getelementptr form...
486 static ConstantExpr *getGetElementPtr(Constant *C,
487 const std::vector<Constant*> &IdxList);
489 /// isNullValue - Return true if this is the value that would be returned by
491 virtual bool isNullValue() const { return false; }
493 /// getOpcode - Return the opcode at the root of this constant expression
494 unsigned getOpcode() const { return iType; }
496 /// getOpcodeName - Return a string representation for an opcode.
497 const char *getOpcodeName() const;
499 /// isConstantExpr - Return true if this is a ConstantExpr
500 virtual bool isConstantExpr() const { return true; }
502 virtual void destroyConstant();
503 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
505 /// Methods for support type inquiry through isa, cast, and dyn_cast:
506 static inline bool classof(const ConstantExpr *) { return true; }
507 static inline bool classof(const Constant *CPV) {
508 return CPV->isConstantExpr();
510 static inline bool classof(const Value *V) {
511 return isa<Constant>(V) && classof(cast<Constant>(V));