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();
296 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static inline bool classof(const ConstantArray *) { return true; }
298 static bool classof(const Constant *CPV); // defined in Constants.cpp
299 static inline bool classof(const Value *V) {
300 return isa<Constant>(V) && classof(cast<Constant>(V));
305 //===---------------------------------------------------------------------------
306 // ConstantStruct - Constant Struct Declarations
308 class ConstantStruct : public Constant {
309 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
311 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
315 /// get() - Static factory methods - Return objects of the specified value
316 static ConstantStruct *get(const StructType *T,
317 const std::vector<Constant*> &V);
319 /// getType() specialization - Reduce amount of casting...
320 inline const StructType *getType() const {
321 return (StructType*)Value::getType();
324 /// getValues - Return a vector of the component constants that make up this
326 inline const std::vector<Use> &getValues() const { return Operands; }
328 /// isNullValue - Return true if this is the value that would be returned by
330 virtual bool isNullValue() const { return false; }
332 virtual void destroyConstant();
334 /// Methods for support type inquiry through isa, cast, and dyn_cast:
335 static inline bool classof(const ConstantStruct *) { return true; }
336 static bool classof(const Constant *CPV); // defined in Constants.cpp
337 static inline bool classof(const Value *V) {
338 return isa<Constant>(V) && classof(cast<Constant>(V));
342 //===---------------------------------------------------------------------------
343 /// ConstantPointer - Constant Pointer Declarations
345 /// The ConstantPointer class represents a null pointer of a specific type. For
346 /// a more specific/useful instance, a subclass of ConstantPointer should be
349 class ConstantPointer : public Constant {
350 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
352 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
353 ~ConstantPointer() {}
355 inline const PointerType *getType() const {
356 return (PointerType*)Value::getType();
359 /// isNullValue - Return true if this is the value that would be returned by
361 virtual bool isNullValue() const { return false; }
363 /// Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const ConstantPointer *) { return true; }
365 static bool classof(const Constant *CPV); // defined in Constants.cpp
366 static inline bool classof(const Value *V) {
367 return isa<Constant>(V) && classof(cast<Constant>(V));
371 /// ConstantPointerNull - a constant pointer value that points to null
373 class ConstantPointerNull : public ConstantPointer {
374 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
376 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
377 inline ~ConstantPointerNull() {}
380 /// get() - Static factory methods - Return objects of the specified value
381 static ConstantPointerNull *get(const PointerType *T);
383 /// isNullValue - Return true if this is the value that would be returned by
385 virtual bool isNullValue() const { return true; }
387 virtual void destroyConstant();
389 /// Methods for support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const ConstantPointerNull *) { return true; }
391 static inline bool classof(const ConstantPointer *P) {
392 return (P->getNumOperands() == 0 && P->isNullValue());
394 static inline bool classof(const Constant *CPV) {
395 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
397 static inline bool classof(const Value *V) {
398 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
403 /// ConstantPointerRef - a constant pointer value that is initialized to
404 /// point to a global value, which lies at a constant, fixed address.
406 class ConstantPointerRef : public ConstantPointer {
407 friend class Module; // Modules maintain these references
408 ConstantPointerRef(const ConstantPointerRef &); // DNI!
411 ConstantPointerRef(GlobalValue *GV);
412 ~ConstantPointerRef() {}
414 /// get() - Static factory methods - Return objects of the specified value
415 static ConstantPointerRef *get(GlobalValue *GV);
417 const GlobalValue *getValue() const {
418 return cast<GlobalValue>(Operands[0].get());
421 GlobalValue *getValue() {
422 return cast<GlobalValue>(Operands[0].get());
425 virtual void destroyConstant();
427 /// Methods for support type inquiry through isa, cast, and dyn_cast:
428 static inline bool classof(const ConstantPointerRef *) { return true; }
429 static inline bool classof(const ConstantPointer *CPV) {
430 // check for a single operand (the target value)
431 return (CPV->getNumOperands() == 1);
433 static inline bool classof(const Constant *CPV) {
434 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
436 static inline bool classof(const Value *V) {
437 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
440 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
442 // Returns the number of uses of OldV that were replaced.
443 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
448 // ConstantExpr - a constant value that is initialized with
449 // an expression using other constant values. This is only used
450 // to represent values that cannot be evaluated at compile-time
451 // (e.g., something derived from an address) because it does
452 // not have a mechanism to store the actual value.
453 // Use the appropriate Constant subclass above for known constants.
455 class ConstantExpr : public Constant {
456 unsigned iType; // Operation type
459 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
460 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
461 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
466 // Static methods to construct a ConstantExpr of different kinds.
468 /// Cast constant expr
469 static ConstantExpr *getCast(Constant *C, const Type *Ty);
471 /// Binary constant expr - Use with binary operators...
472 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
474 /// Getelementptr form...
475 static ConstantExpr *getGetElementPtr(Constant *C,
476 const std::vector<Constant*> &IdxList);
478 /// isNullValue - Return true if this is the value that would be returned by
480 virtual bool isNullValue() const { return false; }
482 /// getOpcode - Return the opcode at the root of this constant expression
483 unsigned getOpcode() const { return iType; }
485 /// getOpcodeName - Return a string representation for an opcode.
486 const char *getOpcodeName() const;
488 /// isConstantExpr - Return true if this is a ConstantExpr
489 virtual bool isConstantExpr() const { return true; }
491 virtual void destroyConstant();
493 /// Methods for support type inquiry through isa, cast, and dyn_cast:
494 static inline bool classof(const ConstantExpr *) { return true; }
495 static inline bool classof(const Constant *CPV) {
496 return CPV->isConstantExpr();
498 static inline bool classof(const Value *V) {
499 return isa<Constant>(V) && classof(cast<Constant>(V));
503 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
505 // Returns the number of uses of OldV that were replaced.
506 virtual unsigned mutateReferences(Value* OldV, Value *NewV);