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,
319 bool DisableChecking = false);
321 /// Methods for support type inquiry through isa, cast, and dyn_cast:
322 static inline bool classof(const ConstantArray *) { return true; }
323 static bool classof(const Constant *CPV); // defined in Constants.cpp
324 static inline bool classof(const Value *V) {
325 return isa<Constant>(V) && classof(cast<Constant>(V));
330 //===---------------------------------------------------------------------------
331 // ConstantStruct - Constant Struct Declarations
333 class ConstantStruct : public Constant {
334 friend struct ConstantCreator<ConstantStruct, StructType,
335 std::vector<Constant*> >;
336 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
338 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
339 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
341 /// get() - Static factory methods - Return objects of the specified value
342 static ConstantStruct *get(const StructType *T,
343 const std::vector<Constant*> &V);
345 /// getType() specialization - Reduce amount of casting...
346 inline const StructType *getType() const {
347 return (StructType*)Value::getType();
350 /// getValues - Return a vector of the component constants that make up this
352 inline const std::vector<Use> &getValues() const { return Operands; }
354 /// isNullValue - Return true if this is the value that would be returned by
356 virtual bool isNullValue() const {
357 // FIXME: This should be made to be MUCH faster. Just check against well
359 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
360 if (!cast<Constant>(getOperand(i))->isNullValue())
365 virtual void destroyConstant();
366 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
367 bool DisableChecking = false);
369 /// Methods for support type inquiry through isa, cast, and dyn_cast:
370 static inline bool classof(const ConstantStruct *) { return true; }
371 static bool classof(const Constant *CPV); // defined in Constants.cpp
372 static inline bool classof(const Value *V) {
373 return isa<Constant>(V) && classof(cast<Constant>(V));
377 //===---------------------------------------------------------------------------
378 /// ConstantPointer - Constant Pointer Declarations
380 /// The ConstantPointer class represents a null pointer of a specific type. For
381 /// a more specific/useful instance, a subclass of ConstantPointer should be
384 class ConstantPointer : public Constant {
385 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
387 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
389 inline const PointerType *getType() const {
390 return (PointerType*)Value::getType();
393 /// isNullValue - Return true if this is the value that would be returned by
395 virtual bool isNullValue() const { return false; }
397 /// Methods for support type inquiry through isa, cast, and dyn_cast:
398 static inline bool classof(const ConstantPointer *) { return true; }
399 static bool classof(const Constant *CPV); // defined in Constants.cpp
400 static inline bool classof(const Value *V) {
401 return isa<Constant>(V) && classof(cast<Constant>(V));
405 /// ConstantPointerNull - a constant pointer value that points to null
407 class ConstantPointerNull : public ConstantPointer {
408 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
409 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
411 ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
412 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
416 /// get() - Static factory methods - Return objects of the specified value
417 static ConstantPointerNull *get(const PointerType *T);
419 /// isNullValue - Return true if this is the value that would be returned by
421 virtual bool isNullValue() const { return true; }
423 virtual void destroyConstant();
425 /// Methods for support type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const ConstantPointerNull *) { return true; }
427 static inline bool classof(const ConstantPointer *P) {
428 return (P->getNumOperands() == 0 && P->isNullValue());
430 static inline bool classof(const Constant *CPV) {
431 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
433 static inline bool classof(const Value *V) {
434 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
439 /// ConstantPointerRef - a constant pointer value that is initialized to
440 /// point to a global value, which lies at a constant, fixed address.
442 class ConstantPointerRef : public ConstantPointer {
443 friend class Module; // Modules maintain these references
444 ConstantPointerRef(const ConstantPointerRef &); // DNI!
447 ConstantPointerRef(GlobalValue *GV);
449 /// get() - Static factory methods - Return objects of the specified value
450 static ConstantPointerRef *get(GlobalValue *GV);
452 const GlobalValue *getValue() const {
453 return cast<GlobalValue>(Operands[0].get());
456 GlobalValue *getValue() {
457 return cast<GlobalValue>(Operands[0].get());
460 virtual void destroyConstant();
461 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
462 bool DisableChecking = false);
464 /// Methods for support type inquiry through isa, cast, and dyn_cast:
465 static inline bool classof(const ConstantPointerRef *) { return true; }
466 static inline bool classof(const ConstantPointer *CPV) {
467 // check for a single operand (the target value)
468 return (CPV->getNumOperands() == 1);
470 static inline bool classof(const Constant *CPV) {
471 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
473 static inline bool classof(const Value *V) {
474 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
478 // ConstantExpr - a constant value that is initialized with an expression using
479 // other constant values. This is only used to represent values that cannot be
480 // evaluated at compile-time (e.g., something derived from an address) because
481 // it does not have a mechanism to store the actual value. Use the appropriate
482 // Constant subclass above for known constants.
484 class ConstantExpr : public Constant {
485 unsigned iType; // Operation type (an Instruction opcode)
486 friend struct ConstantCreator<ConstantExpr,Type,
487 std::pair<unsigned, std::vector<Constant*> > >;
490 // Cast creation ctor
491 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
492 // Binary/Shift instruction creation ctor
493 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
494 // GEP instruction creation ctor
495 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
497 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
500 // Static methods to construct a ConstantExpr of different kinds. Note that
501 // these methods may return a object that is not an instance of the
502 // ConstantExpr class, because they will attempt to fold the constant
503 // expression into something simpler if possible.
505 /// Cast constant expr
506 static Constant *getCast(Constant *C, const Type *Ty);
508 /// Binary constant expr - Use with binary operators...
509 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
511 /// getShift - Return a shift left or shift right constant expr
512 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2);
514 /// Getelementptr form...
515 static Constant *getGetElementPtr(Constant *C,
516 const std::vector<Constant*> &IdxList);
518 /// isNullValue - Return true if this is the value that would be returned by
520 virtual bool isNullValue() const { return false; }
522 /// getOpcode - Return the opcode at the root of this constant expression
523 unsigned getOpcode() const { return iType; }
525 /// getOpcodeName - Return a string representation for an opcode.
526 const char *getOpcodeName() const;
528 /// isConstantExpr - Return true if this is a ConstantExpr
529 virtual bool isConstantExpr() const { return true; }
531 virtual void destroyConstant();
532 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
533 bool DisableChecking = false);
535 /// Override methods to provide more type information...
536 inline Constant *getOperand(unsigned i) {
537 return cast<Constant>(User::getOperand(i));
539 inline Constant *getOperand(unsigned i) const {
540 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
544 /// Methods for support type inquiry through isa, cast, and dyn_cast:
545 static inline bool classof(const ConstantExpr *) { return true; }
546 static inline bool classof(const Constant *CPV) {
547 return CPV->isConstantExpr();
549 static inline bool classof(const Value *V) {
550 return isa<Constant>(V) && classof(cast<Constant>(V));