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 { return false; }
294 virtual void destroyConstant();
295 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
297 /// Methods for support type inquiry through isa, cast, and dyn_cast:
298 static inline bool classof(const ConstantArray *) { return true; }
299 static bool classof(const Constant *CPV); // defined in Constants.cpp
300 static inline bool classof(const Value *V) {
301 return isa<Constant>(V) && classof(cast<Constant>(V));
306 //===---------------------------------------------------------------------------
307 // ConstantStruct - Constant Struct Declarations
309 class ConstantStruct : public Constant {
310 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
312 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
316 /// get() - Static factory methods - Return objects of the specified value
317 static ConstantStruct *get(const StructType *T,
318 const std::vector<Constant*> &V);
320 /// getType() specialization - Reduce amount of casting...
321 inline const StructType *getType() const {
322 return (StructType*)Value::getType();
325 /// getValues - Return a vector of the component constants that make up this
327 inline const std::vector<Use> &getValues() const { return Operands; }
329 /// isNullValue - Return true if this is the value that would be returned by
331 virtual bool isNullValue() const { return false; }
333 virtual void destroyConstant();
334 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
336 /// Methods for support type inquiry through isa, cast, and dyn_cast:
337 static inline bool classof(const ConstantStruct *) { return true; }
338 static bool classof(const Constant *CPV); // defined in Constants.cpp
339 static inline bool classof(const Value *V) {
340 return isa<Constant>(V) && classof(cast<Constant>(V));
344 //===---------------------------------------------------------------------------
345 /// ConstantPointer - Constant Pointer Declarations
347 /// The ConstantPointer class represents a null pointer of a specific type. For
348 /// a more specific/useful instance, a subclass of ConstantPointer should be
351 class ConstantPointer : public Constant {
352 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
354 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
355 ~ConstantPointer() {}
357 inline const PointerType *getType() const {
358 return (PointerType*)Value::getType();
361 /// isNullValue - Return true if this is the value that would be returned by
363 virtual bool isNullValue() const { return false; }
365 /// Methods for support type inquiry through isa, cast, and dyn_cast:
366 static inline bool classof(const ConstantPointer *) { return true; }
367 static bool classof(const Constant *CPV); // defined in Constants.cpp
368 static inline bool classof(const Value *V) {
369 return isa<Constant>(V) && classof(cast<Constant>(V));
373 /// ConstantPointerNull - a constant pointer value that points to null
375 class ConstantPointerNull : public ConstantPointer {
376 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
378 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
379 inline ~ConstantPointerNull() {}
382 /// get() - Static factory methods - Return objects of the specified value
383 static ConstantPointerNull *get(const PointerType *T);
385 /// isNullValue - Return true if this is the value that would be returned by
387 virtual bool isNullValue() const { return true; }
389 virtual void destroyConstant();
391 /// Methods for support type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const ConstantPointerNull *) { return true; }
393 static inline bool classof(const ConstantPointer *P) {
394 return (P->getNumOperands() == 0 && P->isNullValue());
396 static inline bool classof(const Constant *CPV) {
397 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
399 static inline bool classof(const Value *V) {
400 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
405 /// ConstantPointerRef - a constant pointer value that is initialized to
406 /// point to a global value, which lies at a constant, fixed address.
408 class ConstantPointerRef : public ConstantPointer {
409 friend class Module; // Modules maintain these references
410 ConstantPointerRef(const ConstantPointerRef &); // DNI!
413 ConstantPointerRef(GlobalValue *GV);
414 ~ConstantPointerRef() {}
416 /// get() - Static factory methods - Return objects of the specified value
417 static ConstantPointerRef *get(GlobalValue *GV);
419 const GlobalValue *getValue() const {
420 return cast<GlobalValue>(Operands[0].get());
423 GlobalValue *getValue() {
424 return cast<GlobalValue>(Operands[0].get());
427 virtual void destroyConstant();
428 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
430 /// Methods for support type inquiry through isa, cast, and dyn_cast:
431 static inline bool classof(const ConstantPointerRef *) { return true; }
432 static inline bool classof(const ConstantPointer *CPV) {
433 // check for a single operand (the target value)
434 return (CPV->getNumOperands() == 1);
436 static inline bool classof(const Constant *CPV) {
437 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
439 static inline bool classof(const Value *V) {
440 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
443 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
445 // Returns the number of uses of OldV that were replaced.
446 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
451 // ConstantExpr - a constant value that is initialized with
452 // an expression using other constant values. This is only used
453 // to represent values that cannot be evaluated at compile-time
454 // (e.g., something derived from an address) because it does
455 // not have a mechanism to store the actual value.
456 // Use the appropriate Constant subclass above for known constants.
458 class ConstantExpr : public Constant {
459 unsigned iType; // Operation type
462 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
463 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
464 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
469 // Static methods to construct a ConstantExpr of different kinds.
471 /// Cast constant expr
472 static ConstantExpr *getCast(Constant *C, const Type *Ty);
474 /// Binary constant expr - Use with binary operators...
475 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
477 /// Getelementptr form...
478 static ConstantExpr *getGetElementPtr(Constant *C,
479 const std::vector<Constant*> &IdxList);
481 /// isNullValue - Return true if this is the value that would be returned by
483 virtual bool isNullValue() const { return false; }
485 /// getOpcode - Return the opcode at the root of this constant expression
486 unsigned getOpcode() const { return iType; }
488 /// getOpcodeName - Return a string representation for an opcode.
489 const char *getOpcodeName() const;
491 /// isConstantExpr - Return true if this is a ConstantExpr
492 virtual bool isConstantExpr() const { return true; }
494 virtual void destroyConstant();
495 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
497 /// Methods for support type inquiry through isa, cast, and dyn_cast:
498 static inline bool classof(const ConstantExpr *) { return true; }
499 static inline bool classof(const Constant *CPV) {
500 return CPV->isConstantExpr();
502 static inline bool classof(const Value *V) {
503 return isa<Constant>(V) && classof(cast<Constant>(V));
507 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
509 // Returns the number of uses of OldV that were replaced.
510 virtual unsigned mutateReferences(Value* OldV, Value *NewV);