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 isMaxValue() const = 0;
138 virtual bool isMinValue() const = 0;
140 /// Methods for support type inquiry through isa, cast, and dyn_cast:
141 static inline bool classof(const ConstantInt *) { return true; }
142 static bool classof(const Constant *CPV); // defined in Constants.cpp
143 static inline bool classof(const Value *V) {
144 return isa<Constant>(V) && classof(cast<Constant>(V));
149 //===---------------------------------------------------------------------------
150 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
152 class ConstantSInt : public ConstantInt {
153 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
155 ConstantSInt(const Type *Ty, int64_t V);
158 /// get() - Static factory methods - Return objects of the specified value
159 static ConstantSInt *get(const Type *Ty, int64_t V);
161 /// isValueValidForType - return true if Ty is big enough to represent V.
162 static bool isValueValidForType(const Type *Ty, int64_t V);
164 /// getValue - return the underlying value of this constant.
165 inline int64_t getValue() const { return Val.Signed; }
167 virtual bool isAllOnesValue() const { return getValue() == -1; }
169 /// isMaxValue - Return true if this is the largest value that may be
170 /// represented by this type.
172 virtual bool isMaxValue() const {
173 int64_t V = getValue();
174 if (V < 0) return false; // Be careful about wrap-around on 'long's
176 return !isValueValidForType(getType(), V) || V < 0;
179 /// isMinValue - Return true if this is the smallest value that may be
180 /// represented by this type.
182 virtual bool isMinValue() const {
183 int64_t V = getValue();
184 if (V > 0) return false; // Be careful about wrap-around on 'long's
186 return !isValueValidForType(getType(), V) || V > 0;
189 /// Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const ConstantSInt *) { return true; }
191 static bool classof(const Constant *CPV); // defined in Constants.cpp
192 static inline bool classof(const Value *V) {
193 return isa<Constant>(V) && classof(cast<Constant>(V));
197 //===---------------------------------------------------------------------------
198 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
200 class ConstantUInt : public ConstantInt {
201 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
203 ConstantUInt(const Type *Ty, uint64_t V);
206 /// get() - Static factory methods - Return objects of the specified value
207 static ConstantUInt *get(const Type *Ty, uint64_t V);
209 /// isValueValidForType - return true if Ty is big enough to represent V.
210 static bool isValueValidForType(const Type *Ty, uint64_t V);
212 /// getValue - return the underlying value of this constant.
213 inline uint64_t getValue() const { return Val.Unsigned; }
215 /// isMaxValue - Return true if this is the largest value that may be
216 /// represented by this type.
218 virtual bool isAllOnesValue() const;
219 virtual bool isMaxValue() const { return isAllOnesValue(); }
220 virtual bool isMinValue() const { return getValue() == 0; }
222 /// Methods for support type inquiry through isa, cast, and dyn_cast:
223 static inline bool classof(const ConstantUInt *) { return true; }
224 static bool classof(const Constant *CPV); // defined in Constants.cpp
225 static inline bool classof(const Value *V) {
226 return isa<Constant>(V) && classof(cast<Constant>(V));
231 //===---------------------------------------------------------------------------
232 /// ConstantFP - Floating Point Values [float, double]
234 class ConstantFP : public Constant {
236 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
238 ConstantFP(const Type *Ty, double V);
241 /// get() - Static factory methods - Return objects of the specified value
242 static ConstantFP *get(const Type *Ty, double V);
244 /// isValueValidForType - return true if Ty is big enough to represent V.
245 static bool isValueValidForType(const Type *Ty, double V);
246 inline double getValue() const { return Val; }
248 /// isNullValue - Return true if this is the value that would be returned by
250 virtual bool isNullValue() const { return Val == 0; }
252 /// Methods for support type inquiry through isa, cast, and dyn_cast:
253 static inline bool classof(const ConstantFP *) { return true; }
254 static bool classof(const Constant *CPV); // defined in Constants.cpp
255 static inline bool classof(const Value *V) {
256 return isa<Constant>(V) && classof(cast<Constant>(V));
261 //===---------------------------------------------------------------------------
262 /// ConstantArray - Constant Array Declarations
264 class ConstantArray : public Constant {
265 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
267 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
271 /// get() - Static factory methods - Return objects of the specified value
272 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
273 static ConstantArray *get(const std::string &Initializer);
275 /// getType - Specialize the getType() method to always return an ArrayType,
276 /// which reduces the amount of casting needed in parts of the compiler.
278 inline const ArrayType *getType() const {
279 return (ArrayType*)Value::getType();
282 /// getAsString - If the sub-element type of this array is either sbyte or
283 /// ubyte, then this method converts the array to an std::string and returns
284 /// it. Otherwise, it asserts out.
286 std::string getAsString() const;
288 /// getValues - Return a vector of the component constants that make up this
290 inline const std::vector<Use> &getValues() const { return Operands; }
292 /// isNullValue - Return true if this is the value that would be returned by
294 virtual bool isNullValue() const {
295 // FIXME: This should be made to be MUCH faster. Just check against well
297 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
298 if (!cast<Constant>(getOperand(i))->isNullValue())
303 virtual void destroyConstant();
304 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
306 /// Methods for support type inquiry through isa, cast, and dyn_cast:
307 static inline bool classof(const ConstantArray *) { return true; }
308 static bool classof(const Constant *CPV); // defined in Constants.cpp
309 static inline bool classof(const Value *V) {
310 return isa<Constant>(V) && classof(cast<Constant>(V));
315 //===---------------------------------------------------------------------------
316 // ConstantStruct - Constant Struct Declarations
318 class ConstantStruct : public Constant {
319 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
321 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
325 /// get() - Static factory methods - Return objects of the specified value
326 static ConstantStruct *get(const StructType *T,
327 const std::vector<Constant*> &V);
329 /// getType() specialization - Reduce amount of casting...
330 inline const StructType *getType() const {
331 return (StructType*)Value::getType();
334 /// getValues - Return a vector of the component constants that make up this
336 inline const std::vector<Use> &getValues() const { return Operands; }
338 /// isNullValue - Return true if this is the value that would be returned by
340 virtual bool isNullValue() const {
341 // FIXME: This should be made to be MUCH faster. Just check against well
343 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
344 if (!cast<Constant>(getOperand(i))->isNullValue())
349 virtual void destroyConstant();
350 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
352 /// Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const ConstantStruct *) { return true; }
354 static bool classof(const Constant *CPV); // defined in Constants.cpp
355 static inline bool classof(const Value *V) {
356 return isa<Constant>(V) && classof(cast<Constant>(V));
360 //===---------------------------------------------------------------------------
361 /// ConstantPointer - Constant Pointer Declarations
363 /// The ConstantPointer class represents a null pointer of a specific type. For
364 /// a more specific/useful instance, a subclass of ConstantPointer should be
367 class ConstantPointer : public Constant {
368 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
370 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
371 ~ConstantPointer() {}
373 inline const PointerType *getType() const {
374 return (PointerType*)Value::getType();
377 /// isNullValue - Return true if this is the value that would be returned by
379 virtual bool isNullValue() const { return false; }
381 /// Methods for support type inquiry through isa, cast, and dyn_cast:
382 static inline bool classof(const ConstantPointer *) { return true; }
383 static bool classof(const Constant *CPV); // defined in Constants.cpp
384 static inline bool classof(const Value *V) {
385 return isa<Constant>(V) && classof(cast<Constant>(V));
389 /// ConstantPointerNull - a constant pointer value that points to null
391 class ConstantPointerNull : public ConstantPointer {
392 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
394 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
395 inline ~ConstantPointerNull() {}
398 /// get() - Static factory methods - Return objects of the specified value
399 static ConstantPointerNull *get(const PointerType *T);
401 /// isNullValue - Return true if this is the value that would be returned by
403 virtual bool isNullValue() const { return true; }
405 virtual void destroyConstant();
407 /// Methods for support type inquiry through isa, cast, and dyn_cast:
408 static inline bool classof(const ConstantPointerNull *) { return true; }
409 static inline bool classof(const ConstantPointer *P) {
410 return (P->getNumOperands() == 0 && P->isNullValue());
412 static inline bool classof(const Constant *CPV) {
413 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
415 static inline bool classof(const Value *V) {
416 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
421 /// ConstantPointerRef - a constant pointer value that is initialized to
422 /// point to a global value, which lies at a constant, fixed address.
424 class ConstantPointerRef : public ConstantPointer {
425 friend class Module; // Modules maintain these references
426 ConstantPointerRef(const ConstantPointerRef &); // DNI!
429 ConstantPointerRef(GlobalValue *GV);
430 ~ConstantPointerRef() {}
432 /// get() - Static factory methods - Return objects of the specified value
433 static ConstantPointerRef *get(GlobalValue *GV);
435 const GlobalValue *getValue() const {
436 return cast<GlobalValue>(Operands[0].get());
439 GlobalValue *getValue() {
440 return cast<GlobalValue>(Operands[0].get());
443 virtual void destroyConstant();
444 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
446 /// Methods for support type inquiry through isa, cast, and dyn_cast:
447 static inline bool classof(const ConstantPointerRef *) { return true; }
448 static inline bool classof(const ConstantPointer *CPV) {
449 // check for a single operand (the target value)
450 return (CPV->getNumOperands() == 1);
452 static inline bool classof(const Constant *CPV) {
453 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
455 static inline bool classof(const Value *V) {
456 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
461 // ConstantExpr - a constant value that is initialized with an expression using
462 // other constant values. This is only used to represent values that cannot be
463 // evaluated at compile-time (e.g., something derived from an address) because
464 // it does not have a mechanism to store the actual value. Use the appropriate
465 // Constant subclass above for known constants.
467 class ConstantExpr : public Constant {
468 unsigned iType; // Operation type
471 // Cast creation ctor
472 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
473 // Binary/Shift instruction creation ctor
474 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
475 // GEP instruction creation ctor
476 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
480 // Static methods to construct a ConstantExpr of different kinds. Note that
481 // these methods may return a object that is not an instance of the
482 // ConstantExpr class, because they will attempt to fold the constant
483 // expression into something simpler if possible.
485 /// Cast constant expr
486 static Constant *getCast(Constant *C, const Type *Ty);
488 /// Binary constant expr - Use with binary operators...
489 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
491 /// getShift - Return a shift left or shift right constant expr
492 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2);
494 /// Getelementptr form...
495 static Constant *getGetElementPtr(Constant *C,
496 const std::vector<Constant*> &IdxList);
498 /// isNullValue - Return true if this is the value that would be returned by
500 virtual bool isNullValue() const { return false; }
502 /// getOpcode - Return the opcode at the root of this constant expression
503 unsigned getOpcode() const { return iType; }
505 /// getOpcodeName - Return a string representation for an opcode.
506 const char *getOpcodeName() const;
508 /// isConstantExpr - Return true if this is a ConstantExpr
509 virtual bool isConstantExpr() const { return true; }
511 virtual void destroyConstant();
512 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
514 /// Override methods to provide more type information...
515 inline Constant *getOperand(unsigned i) {
516 return cast<Constant>(User::getOperand(i));
518 inline Constant *getOperand(unsigned i) const {
519 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
523 /// Methods for support type inquiry through isa, cast, and dyn_cast:
524 static inline bool classof(const ConstantExpr *) { return true; }
525 static inline bool classof(const Constant *CPV) {
526 return CPV->isConstantExpr();
528 static inline bool classof(const Value *V) {
529 return isa<Constant>(V) && classof(cast<Constant>(V));