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;
20 template<class ConstantClass, class TypeClass>
21 struct ConvertConstantType;
24 //===---------------------------------------------------------------------------
25 /// ConstantIntegral - Shared superclass of boolean and integer constants.
27 /// This class just defines some common interfaces to be implemented.
29 class ConstantIntegral : public Constant {
31 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
34 /// isNullValue - Return true if this is the value that would be returned by
37 virtual bool isNullValue() const = 0;
39 /// isMaxValue - Return true if this is the largest value that may be
40 /// represented by this type.
42 virtual bool isMaxValue() const = 0;
44 /// isMinValue - Return true if this is the smallest value that may be
45 /// represented by this type.
47 virtual bool isMinValue() const = 0;
49 /// isAllOnesValue - Return true if every bit in this constant is set to true.
51 virtual bool isAllOnesValue() const = 0;
53 /// Static constructor to get the maximum/minimum/allones constant of
54 /// specified (integral) type...
56 static ConstantIntegral *getMaxValue(const Type *Ty);
57 static ConstantIntegral *getMinValue(const Type *Ty);
58 static ConstantIntegral *getAllOnesValue(const Type *Ty);
60 /// Methods for support type inquiry through isa, cast, and dyn_cast:
61 static inline bool classof(const ConstantIntegral *) { return true; }
62 static bool classof(const Constant *CPV); // defined in Constants.cpp
63 static inline bool classof(const Value *V) {
64 return isa<Constant>(V) && classof(cast<Constant>(V));
69 //===---------------------------------------------------------------------------
70 /// ConstantBool - Boolean Values
72 class ConstantBool : public ConstantIntegral {
76 static ConstantBool *True, *False; // The True & False values
78 /// get() - Static factory methods - Return objects of the specified value
79 static ConstantBool *get(bool Value) { return Value ? True : False; }
80 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
82 /// inverted - Return the opposite value of the current value.
83 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
85 /// getValue - return the boolean value of this constant.
87 inline bool getValue() const { return Val; }
89 /// isNullValue - Return true if this is the value that would be returned by
92 virtual bool isNullValue() const { return this == False; }
93 virtual bool isMaxValue() const { return this == True; }
94 virtual bool isMinValue() const { return this == False; }
95 virtual bool isAllOnesValue() const { return this == True; }
97 /// Methods for support type inquiry through isa, cast, and dyn_cast:
98 static inline bool classof(const ConstantBool *) { return true; }
99 static bool classof(const Constant *CPV) {
100 return (CPV == True) | (CPV == False);
102 static inline bool classof(const Value *V) {
103 return isa<Constant>(V) && classof(cast<Constant>(V));
108 //===---------------------------------------------------------------------------
109 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
110 /// with integral constants easier.
112 class ConstantInt : public ConstantIntegral {
118 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
119 ConstantInt(const Type *Ty, uint64_t V);
121 /// equalsInt - Provide a helper method that can be used to determine if the
122 /// constant contained within is equal to a constant. This only works for
123 /// very small values, because this is all that can be represented with all
126 bool equalsInt(unsigned char V) const {
128 "equals: Can only be used with very small positive constants!");
129 return Val.Unsigned == V;
132 /// ConstantInt::get static method: return a ConstantInt with the specified
133 /// value. as above, we work only with very small values here.
135 static ConstantInt *get(const Type *Ty, unsigned char V);
137 /// getRawValue - return the underlying value of this constant as a 64-bit
138 /// unsigned integer value.
140 inline uint64_t getRawValue() const { return Val.Unsigned; }
142 /// isNullValue - Return true if this is the value that would be returned by
144 virtual bool isNullValue() const { return Val.Unsigned == 0; }
145 virtual bool isMaxValue() const = 0;
146 virtual bool isMinValue() const = 0;
148 /// Methods for support type inquiry through isa, cast, and dyn_cast:
149 static inline bool classof(const ConstantInt *) { return true; }
150 static bool classof(const Constant *CPV); // defined in Constants.cpp
151 static inline bool classof(const Value *V) {
152 return isa<Constant>(V) && classof(cast<Constant>(V));
157 //===---------------------------------------------------------------------------
158 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
160 class ConstantSInt : public ConstantInt {
161 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
162 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
165 ConstantSInt(const Type *Ty, int64_t V);
167 /// get() - Static factory methods - Return objects of the specified value
169 static ConstantSInt *get(const Type *Ty, int64_t V);
171 /// isValueValidForType - return true if Ty is big enough to represent V.
173 static bool isValueValidForType(const Type *Ty, int64_t V);
175 /// getValue - return the underlying value of this constant.
177 inline int64_t getValue() const { return Val.Signed; }
179 virtual bool isAllOnesValue() const { return getValue() == -1; }
181 /// isMaxValue - Return true if this is the largest value that may be
182 /// represented by this type.
184 virtual bool isMaxValue() const {
185 int64_t V = getValue();
186 if (V < 0) return false; // Be careful about wrap-around on 'long's
188 return !isValueValidForType(getType(), V) || V < 0;
191 /// isMinValue - Return true if this is the smallest value that may be
192 /// represented by this type.
194 virtual bool isMinValue() const {
195 int64_t V = getValue();
196 if (V > 0) return false; // Be careful about wrap-around on 'long's
198 return !isValueValidForType(getType(), V) || V > 0;
201 /// Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const ConstantSInt *) { return true; }
204 static bool classof(const Constant *CPV); // defined in Constants.cpp
205 static inline bool classof(const Value *V) {
206 return isa<Constant>(V) && classof(cast<Constant>(V));
210 //===---------------------------------------------------------------------------
211 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
213 class ConstantUInt : public ConstantInt {
214 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
215 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
217 ConstantUInt(const Type *Ty, uint64_t V);
219 /// get() - Static factory methods - Return objects of the specified value
221 static ConstantUInt *get(const Type *Ty, uint64_t V);
223 /// isValueValidForType - return true if Ty is big enough to represent V.
225 static bool isValueValidForType(const Type *Ty, uint64_t V);
227 /// getValue - return the underlying value of this constant.
229 inline uint64_t getValue() const { return Val.Unsigned; }
231 /// isMaxValue - Return true if this is the largest value that may be
232 /// represented by this type.
234 virtual bool isAllOnesValue() const;
235 virtual bool isMaxValue() const { return isAllOnesValue(); }
236 virtual bool isMinValue() const { return getValue() == 0; }
238 /// Methods for support type inquiry through isa, cast, and dyn_cast:
239 static inline bool classof(const ConstantUInt *) { return true; }
240 static bool classof(const Constant *CPV); // defined in Constants.cpp
241 static inline bool classof(const Value *V) {
242 return isa<Constant>(V) && classof(cast<Constant>(V));
247 //===---------------------------------------------------------------------------
248 /// ConstantFP - Floating Point Values [float, double]
250 class ConstantFP : public Constant {
252 friend struct ConstantCreator<ConstantFP, Type, double>;
253 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
255 ConstantFP(const Type *Ty, double V);
257 /// get() - Static factory methods - Return objects of the specified value
258 static ConstantFP *get(const Type *Ty, double V);
260 /// isValueValidForType - return true if Ty is big enough to represent V.
261 static bool isValueValidForType(const Type *Ty, double V);
262 inline double getValue() const { return Val; }
264 /// isNullValue - Return true if this is the value that would be returned by
266 virtual bool isNullValue() const { return Val == 0; }
268 /// Methods for support type inquiry through isa, cast, and dyn_cast:
269 static inline bool classof(const ConstantFP *) { return true; }
270 static bool classof(const Constant *CPV); // defined in Constants.cpp
271 static inline bool classof(const Value *V) {
272 return isa<Constant>(V) && classof(cast<Constant>(V));
277 //===---------------------------------------------------------------------------
278 /// ConstantArray - Constant Array Declarations
280 class ConstantArray : public Constant {
281 friend struct ConstantCreator<ConstantArray, ArrayType,
282 std::vector<Constant*> >;
283 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
285 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
287 /// get() - Static factory methods - Return objects of the specified value
288 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
289 static ConstantArray *get(const std::string &Initializer);
291 /// getType - Specialize the getType() method to always return an ArrayType,
292 /// which reduces the amount of casting needed in parts of the compiler.
294 inline const ArrayType *getType() const {
295 return (ArrayType*)Value::getType();
298 /// getAsString - If the sub-element type of this array is either sbyte or
299 /// ubyte, then this method converts the array to an std::string and returns
300 /// it. Otherwise, it asserts out.
302 std::string getAsString() const;
304 /// getValues - Return a vector of the component constants that make up this
306 inline const std::vector<Use> &getValues() const { return Operands; }
308 /// isNullValue - Return true if this is the value that would be returned by
310 virtual bool isNullValue() const {
311 // FIXME: This should be made to be MUCH faster. Just check against well
313 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
314 if (!cast<Constant>(getOperand(i))->isNullValue())
319 virtual void destroyConstant();
320 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
321 bool DisableChecking = false);
323 /// Methods for support type inquiry through isa, cast, and dyn_cast:
324 static inline bool classof(const ConstantArray *) { return true; }
325 static bool classof(const Constant *CPV); // defined in Constants.cpp
326 static inline bool classof(const Value *V) {
327 return isa<Constant>(V) && classof(cast<Constant>(V));
332 //===---------------------------------------------------------------------------
333 // ConstantStruct - Constant Struct Declarations
335 class ConstantStruct : public Constant {
336 friend struct ConstantCreator<ConstantStruct, StructType,
337 std::vector<Constant*> >;
338 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
340 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
342 /// get() - Static factory methods - Return objects of the specified value
343 static ConstantStruct *get(const StructType *T,
344 const std::vector<Constant*> &V);
346 /// getType() specialization - Reduce amount of casting...
347 inline const StructType *getType() const {
348 return (StructType*)Value::getType();
351 /// getValues - Return a vector of the component constants that make up this
353 inline const std::vector<Use> &getValues() const { return Operands; }
355 /// isNullValue - Return true if this is the value that would be returned by
357 virtual bool isNullValue() const {
358 // FIXME: This should be made to be MUCH faster. Just check against well
360 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
361 if (!cast<Constant>(getOperand(i))->isNullValue())
366 virtual void destroyConstant();
367 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
368 bool DisableChecking = false);
370 /// Methods for support type inquiry through isa, cast, and dyn_cast:
371 static inline bool classof(const ConstantStruct *) { return true; }
372 static bool classof(const Constant *CPV); // defined in Constants.cpp
373 static inline bool classof(const Value *V) {
374 return isa<Constant>(V) && classof(cast<Constant>(V));
378 //===---------------------------------------------------------------------------
379 /// ConstantPointer - Constant Pointer Declarations
381 /// The ConstantPointer class represents a null pointer of a specific type. For
382 /// a more specific/useful instance, a subclass of ConstantPointer should be
385 class ConstantPointer : public Constant {
386 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
388 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
390 inline const PointerType *getType() const {
391 return (PointerType*)Value::getType();
394 /// isNullValue - Return true if this is the value that would be returned by
396 virtual bool isNullValue() const { return false; }
398 /// Methods for support type inquiry through isa, cast, and dyn_cast:
399 static inline bool classof(const ConstantPointer *) { return true; }
400 static bool classof(const Constant *CPV); // defined in Constants.cpp
401 static inline bool classof(const Value *V) {
402 return isa<Constant>(V) && classof(cast<Constant>(V));
406 /// ConstantPointerNull - a constant pointer value that points to null
408 class ConstantPointerNull : public ConstantPointer {
409 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
410 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
412 ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
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*> > >;
488 friend struct ConvertConstantType<ConstantExpr, Type>;
491 // Cast creation ctor
492 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
493 // Binary/Shift instruction creation ctor
494 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
495 // GEP instruction creation ctor
496 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
499 // These private methods are used by the type resolution code to create
500 // ConstantExprs in intermediate forms.
501 static Constant *getTy(const Type *Ty, unsigned Opcode,
502 Constant *C1, Constant *C2);
503 static Constant *getShiftTy(const Type *Ty,
504 unsigned Opcode, Constant *C1, Constant *C2);
505 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
506 const std::vector<Constant*> &IdxList);
509 // Static methods to construct a ConstantExpr of different kinds. Note that
510 // these methods may return a object that is not an instance of the
511 // ConstantExpr class, because they will attempt to fold the constant
512 // expression into something simpler if possible.
514 /// Cast constant expr
516 static Constant *getCast(Constant *C, const Type *Ty);
518 /// Binary constant expr - Use with binary operators...
520 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
521 return getTy(C1->getType(), Opcode, C1, C2);
524 /// getShift - Return a shift left or shift right constant expr
526 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2) {
527 return getShiftTy(C1->getType(), Opcode, C1, C2);
530 /// Getelementptr form...
532 static Constant *getGetElementPtr(Constant *C,
533 const std::vector<Constant*> &IdxList);
535 /// isNullValue - Return true if this is the value that would be returned by
537 virtual bool isNullValue() const { return false; }
539 /// getOpcode - Return the opcode at the root of this constant expression
540 unsigned getOpcode() const { return iType; }
542 /// getOpcodeName - Return a string representation for an opcode.
543 const char *getOpcodeName() const;
545 /// isConstantExpr - Return true if this is a ConstantExpr
546 virtual bool isConstantExpr() const { return true; }
548 virtual void destroyConstant();
549 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
550 bool DisableChecking = false);
552 /// Override methods to provide more type information...
553 inline Constant *getOperand(unsigned i) {
554 return cast<Constant>(User::getOperand(i));
556 inline Constant *getOperand(unsigned i) const {
557 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
561 /// Methods for support type inquiry through isa, cast, and dyn_cast:
562 static inline bool classof(const ConstantExpr *) { return true; }
563 static inline bool classof(const Constant *CPV) {
564 return CPV->isConstantExpr();
566 static inline bool classof(const Value *V) {
567 return isa<Constant>(V) && classof(cast<Constant>(V));