1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations for the subclasses of Constant, which
11 // represent the different type of constant pool values
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CONSTANTS_H
16 #define LLVM_CONSTANTS_H
18 #include "llvm/Constant.h"
19 #include "Support/DataTypes.h"
27 template<class ConstantClass, class TypeClass, class ValType>
28 struct ConstantCreator;
29 template<class ConstantClass, class TypeClass>
30 struct ConvertConstantType;
33 //===---------------------------------------------------------------------------
34 /// ConstantIntegral - Shared superclass of boolean and integer constants.
36 /// This class just defines some common interfaces to be implemented.
38 class ConstantIntegral : public Constant {
40 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
43 /// isNullValue - Return true if this is the value that would be returned by
46 virtual bool isNullValue() const = 0;
48 /// isMaxValue - Return true if this is the largest value that may be
49 /// represented by this type.
51 virtual bool isMaxValue() const = 0;
53 /// isMinValue - Return true if this is the smallest value that may be
54 /// represented by this type.
56 virtual bool isMinValue() const = 0;
58 /// isAllOnesValue - Return true if every bit in this constant is set to true.
60 virtual bool isAllOnesValue() const = 0;
62 /// Static constructor to get the maximum/minimum/allones constant of
63 /// specified (integral) type...
65 static ConstantIntegral *getMaxValue(const Type *Ty);
66 static ConstantIntegral *getMinValue(const Type *Ty);
67 static ConstantIntegral *getAllOnesValue(const Type *Ty);
69 /// Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const ConstantIntegral *) { return true; }
71 static bool classof(const Constant *CPV); // defined in Constants.cpp
72 static inline bool classof(const Value *V) {
73 return isa<Constant>(V) && classof(cast<Constant>(V));
78 //===---------------------------------------------------------------------------
79 /// ConstantBool - Boolean Values
81 class ConstantBool : public ConstantIntegral {
85 static ConstantBool *True, *False; // The True & False values
87 /// get() - Static factory methods - Return objects of the specified value
88 static ConstantBool *get(bool Value) { return Value ? True : False; }
89 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
91 /// inverted - Return the opposite value of the current value.
92 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
94 /// getValue - return the boolean value of this constant.
96 inline bool getValue() const { return Val; }
98 /// isNullValue - Return true if this is the value that would be returned by
101 virtual bool isNullValue() const { return this == False; }
102 virtual bool isMaxValue() const { return this == True; }
103 virtual bool isMinValue() const { return this == False; }
104 virtual bool isAllOnesValue() const { return this == True; }
106 /// Methods for support type inquiry through isa, cast, and dyn_cast:
107 static inline bool classof(const ConstantBool *) { return true; }
108 static bool classof(const Constant *CPV) {
109 return (CPV == True) | (CPV == False);
111 static inline bool classof(const Value *V) {
112 return isa<Constant>(V) && classof(cast<Constant>(V));
117 //===---------------------------------------------------------------------------
118 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
119 /// with integral constants easier.
121 class ConstantInt : public ConstantIntegral {
127 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
128 ConstantInt(const Type *Ty, uint64_t V);
130 /// equalsInt - Provide a helper method that can be used to determine if the
131 /// constant contained within is equal to a constant. This only works for
132 /// very small values, because this is all that can be represented with all
135 bool equalsInt(unsigned char V) const {
137 "equals: Can only be used with very small positive constants!");
138 return Val.Unsigned == V;
141 /// ConstantInt::get static method: return a ConstantInt with the specified
142 /// value. as above, we work only with very small values here.
144 static ConstantInt *get(const Type *Ty, unsigned char V);
146 /// getRawValue - return the underlying value of this constant as a 64-bit
147 /// unsigned integer value.
149 inline uint64_t getRawValue() const { return Val.Unsigned; }
151 /// isNullValue - Return true if this is the value that would be returned by
153 virtual bool isNullValue() const { return Val.Unsigned == 0; }
154 virtual bool isMaxValue() const = 0;
155 virtual bool isMinValue() const = 0;
157 /// Methods for support type inquiry through isa, cast, and dyn_cast:
158 static inline bool classof(const ConstantInt *) { return true; }
159 static bool classof(const Constant *CPV); // defined in Constants.cpp
160 static inline bool classof(const Value *V) {
161 return isa<Constant>(V) && classof(cast<Constant>(V));
166 //===---------------------------------------------------------------------------
167 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
169 class ConstantSInt : public ConstantInt {
170 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
171 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
174 ConstantSInt(const Type *Ty, int64_t V);
176 /// get() - Static factory methods - Return objects of the specified value
178 static ConstantSInt *get(const Type *Ty, int64_t V);
180 /// isValueValidForType - return true if Ty is big enough to represent V.
182 static bool isValueValidForType(const Type *Ty, int64_t V);
184 /// getValue - return the underlying value of this constant.
186 inline int64_t getValue() const { return Val.Signed; }
188 virtual bool isAllOnesValue() const { return getValue() == -1; }
190 /// isMaxValue - Return true if this is the largest value that may be
191 /// represented by this type.
193 virtual bool isMaxValue() const {
194 int64_t V = getValue();
195 if (V < 0) return false; // Be careful about wrap-around on 'long's
197 return !isValueValidForType(getType(), V) || V < 0;
200 /// isMinValue - Return true if this is the smallest value that may be
201 /// represented by this type.
203 virtual bool isMinValue() const {
204 int64_t V = getValue();
205 if (V > 0) return false; // Be careful about wrap-around on 'long's
207 return !isValueValidForType(getType(), V) || V > 0;
210 /// Methods for support type inquiry through isa, cast, and dyn_cast:
212 static inline bool classof(const ConstantSInt *) { return true; }
213 static bool classof(const Constant *CPV); // defined in Constants.cpp
214 static inline bool classof(const Value *V) {
215 return isa<Constant>(V) && classof(cast<Constant>(V));
219 //===---------------------------------------------------------------------------
220 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
222 class ConstantUInt : public ConstantInt {
223 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
224 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
226 ConstantUInt(const Type *Ty, uint64_t V);
228 /// get() - Static factory methods - Return objects of the specified value
230 static ConstantUInt *get(const Type *Ty, uint64_t V);
232 /// isValueValidForType - return true if Ty is big enough to represent V.
234 static bool isValueValidForType(const Type *Ty, uint64_t V);
236 /// getValue - return the underlying value of this constant.
238 inline uint64_t getValue() const { return Val.Unsigned; }
240 /// isMaxValue - Return true if this is the largest value that may be
241 /// represented by this type.
243 virtual bool isAllOnesValue() const;
244 virtual bool isMaxValue() const { return isAllOnesValue(); }
245 virtual bool isMinValue() const { return getValue() == 0; }
247 /// Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const ConstantUInt *) { return true; }
249 static bool classof(const Constant *CPV); // defined in Constants.cpp
250 static inline bool classof(const Value *V) {
251 return isa<Constant>(V) && classof(cast<Constant>(V));
256 //===---------------------------------------------------------------------------
257 /// ConstantFP - Floating Point Values [float, double]
259 class ConstantFP : public Constant {
261 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
262 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
263 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
265 ConstantFP(const Type *Ty, double V);
267 /// get() - Static factory methods - Return objects of the specified value
268 static ConstantFP *get(const Type *Ty, double V);
270 /// isValueValidForType - return true if Ty is big enough to represent V.
271 static bool isValueValidForType(const Type *Ty, double V);
272 inline double getValue() const { return Val; }
274 /// isNullValue - Return true if this is the value that would be returned by
275 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
276 /// considers -0.0 to be null as well as 0.0. :(
277 virtual bool isNullValue() const {
286 /// Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const ConstantFP *) { return true; }
288 static bool classof(const Constant *CPV); // defined in Constants.cpp
289 static inline bool classof(const Value *V) {
290 return isa<Constant>(V) && classof(cast<Constant>(V));
295 //===---------------------------------------------------------------------------
296 /// ConstantArray - Constant Array Declarations
298 class ConstantArray : public Constant {
299 friend struct ConstantCreator<ConstantArray, ArrayType,
300 std::vector<Constant*> >;
301 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
303 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
305 /// get() - Static factory methods - Return objects of the specified value
306 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
307 static ConstantArray *get(const std::string &Initializer);
309 /// getType - Specialize the getType() method to always return an ArrayType,
310 /// which reduces the amount of casting needed in parts of the compiler.
312 inline const ArrayType *getType() const {
313 return reinterpret_cast<const ArrayType*>(Value::getType());
316 /// isString - This method returns true if the array is an array of sbyte or
317 /// ubyte, and if the elements of the array are all ConstantInt's.
318 bool isString() const;
320 /// getAsString - If this array is isString(), then this method converts the
321 /// array to an std::string and returns it. Otherwise, it asserts out.
323 std::string getAsString() const;
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 {
332 // FIXME: This should be made to be MUCH faster. Just check against well
334 if (getNumOperands()) {
335 const Constant *First = cast<Constant>(getOperand(0));
336 if (!First->isNullValue()) return false;
337 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
338 if (cast<Constant>(getOperand(i)) != First)
344 virtual void destroyConstant();
345 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
346 bool DisableChecking = false);
348 /// Methods for support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const ConstantArray *) { return true; }
350 static bool classof(const Constant *CPV); // defined in Constants.cpp
351 static inline bool classof(const Value *V) {
352 return isa<Constant>(V) && classof(cast<Constant>(V));
357 //===---------------------------------------------------------------------------
358 // ConstantStruct - Constant Struct Declarations
360 class ConstantStruct : public Constant {
361 friend struct ConstantCreator<ConstantStruct, StructType,
362 std::vector<Constant*> >;
363 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
365 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
367 /// get() - Static factory methods - Return objects of the specified value
368 static ConstantStruct *get(const StructType *T,
369 const std::vector<Constant*> &V);
371 /// getType() specialization - Reduce amount of casting...
372 inline const StructType *getType() const {
373 return reinterpret_cast<const StructType*>(Value::getType());
376 /// getValues - Return a vector of the component constants that make up this
378 inline const std::vector<Use> &getValues() const { return Operands; }
380 /// isNullValue - Return true if this is the value that would be returned by
382 virtual bool isNullValue() const {
383 // FIXME: This should be made to be MUCH faster. Just check against well
385 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
386 if (!cast<Constant>(getOperand(i))->isNullValue())
391 virtual void destroyConstant();
392 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
393 bool DisableChecking = false);
395 /// Methods for support type inquiry through isa, cast, and dyn_cast:
396 static inline bool classof(const ConstantStruct *) { return true; }
397 static bool classof(const Constant *CPV); // defined in Constants.cpp
398 static inline bool classof(const Value *V) {
399 return isa<Constant>(V) && classof(cast<Constant>(V));
403 //===---------------------------------------------------------------------------
404 /// ConstantPointerNull - a constant pointer value that points to null
406 class ConstantPointerNull : public Constant {
407 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
408 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
410 ConstantPointerNull(const PointerType *T)
411 : Constant(reinterpret_cast<const Type*>(T)) {}
415 /// get() - Static factory methods - Return objects of the specified value
416 static ConstantPointerNull *get(const PointerType *T);
418 /// isNullValue - Return true if this is the value that would be returned by
420 virtual bool isNullValue() const { return true; }
422 virtual void destroyConstant();
424 /// Methods for support type inquiry through isa, cast, and dyn_cast:
425 static inline bool classof(const ConstantPointerNull *) { return true; }
426 static bool classof(const Constant *CPV);
427 static inline bool classof(const Value *V) {
428 return isa<Constant>(V) && classof(cast<Constant>(V));
433 //===---------------------------------------------------------------------------
434 /// ConstantPointerRef - a constant pointer value that is initialized to
435 /// point to a global value, which lies at a constant, fixed address.
437 class ConstantPointerRef : public Constant {
438 friend class Module; // Modules maintain these references
439 ConstantPointerRef(const ConstantPointerRef &); // DNI!
442 ConstantPointerRef(GlobalValue *GV);
444 /// get() - Static factory methods - Return objects of the specified value
445 static ConstantPointerRef *get(GlobalValue *GV);
447 const GlobalValue *getValue() const {
448 return cast<GlobalValue>(Operands[0].get());
451 GlobalValue *getValue() {
452 return cast<GlobalValue>(Operands[0].get());
455 /// isNullValue - Return true if this is the value that would be returned by
457 virtual bool isNullValue() const { return false; }
459 virtual void destroyConstant();
460 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
461 bool DisableChecking = false);
463 /// Methods for support type inquiry through isa, cast, and dyn_cast:
464 static inline bool classof(const ConstantPointerRef *) { return true; }
465 static bool classof(const Constant *CPV);
466 static inline bool classof(const Value *V) {
467 return isa<Constant>(V) && classof(cast<Constant>(V));
471 // ConstantExpr - a constant value that is initialized with an expression using
472 // other constant values. This is only used to represent values that cannot be
473 // evaluated at compile-time (e.g., something derived from an address) because
474 // it does not have a mechanism to store the actual value. Use the appropriate
475 // Constant subclass above for known constants.
477 class ConstantExpr : public Constant {
478 unsigned iType; // Operation type (an Instruction opcode)
479 friend struct ConstantCreator<ConstantExpr,Type,
480 std::pair<unsigned, std::vector<Constant*> > >;
481 friend struct ConvertConstantType<ConstantExpr, Type>;
484 // Cast creation ctor
485 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
486 // Binary/Shift instruction creation ctor
487 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
488 // GEP instruction creation ctor
489 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
492 // These private methods are used by the type resolution code to create
493 // ConstantExprs in intermediate forms.
494 static Constant *getTy(const Type *Ty, unsigned Opcode,
495 Constant *C1, Constant *C2);
496 static Constant *getShiftTy(const Type *Ty,
497 unsigned Opcode, Constant *C1, Constant *C2);
498 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
499 const std::vector<Constant*> &IdxList);
502 // Static methods to construct a ConstantExpr of different kinds. Note that
503 // these methods may return a object that is not an instance of the
504 // ConstantExpr class, because they will attempt to fold the constant
505 // expression into something simpler if possible.
507 /// Cast constant expr
509 static Constant *getCast(Constant *C, const Type *Ty);
511 /// ConstantExpr::get - Return a binary or shift operator constant expression,
512 /// folding if possible.
514 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
515 return getTy(C1->getType(), Opcode, C1, C2);
518 /// Getelementptr form...
520 static Constant *getGetElementPtr(Constant *C,
521 const std::vector<Constant*> &IdxList);
523 /// isNullValue - Return true if this is the value that would be returned by
525 virtual bool isNullValue() const { return false; }
527 /// getOpcode - Return the opcode at the root of this constant expression
528 unsigned getOpcode() const { return iType; }
530 /// getOpcodeName - Return a string representation for an opcode.
531 const char *getOpcodeName() const;
533 /// isConstantExpr - Return true if this is a ConstantExpr
534 virtual bool isConstantExpr() const { return true; }
536 virtual void destroyConstant();
537 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
538 bool DisableChecking = false);
540 /// Override methods to provide more type information...
541 inline Constant *getOperand(unsigned i) {
542 return cast<Constant>(User::getOperand(i));
544 inline Constant *getOperand(unsigned i) const {
545 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
549 /// Methods for support type inquiry through isa, cast, and dyn_cast:
550 static inline bool classof(const ConstantExpr *) { return true; }
551 static inline bool classof(const Constant *CPV) {
552 return CPV->isConstantExpr();
554 static inline bool classof(const Value *V) {
555 return isa<Constant>(V) && classof(cast<Constant>(V));
559 } // End llvm namespace