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"
25 template<class ConstantClass, class TypeClass, class ValType>
26 struct ConstantCreator;
27 template<class ConstantClass, class TypeClass>
28 struct ConvertConstantType;
31 //===---------------------------------------------------------------------------
32 /// ConstantIntegral - Shared superclass of boolean and integer constants.
34 /// This class just defines some common interfaces to be implemented.
36 class ConstantIntegral : public Constant {
38 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
41 /// isNullValue - Return true if this is the value that would be returned by
44 virtual bool isNullValue() const = 0;
46 /// isMaxValue - Return true if this is the largest value that may be
47 /// represented by this type.
49 virtual bool isMaxValue() const = 0;
51 /// isMinValue - Return true if this is the smallest value that may be
52 /// represented by this type.
54 virtual bool isMinValue() const = 0;
56 /// isAllOnesValue - Return true if every bit in this constant is set to true.
58 virtual bool isAllOnesValue() const = 0;
60 /// Static constructor to get the maximum/minimum/allones constant of
61 /// specified (integral) type...
63 static ConstantIntegral *getMaxValue(const Type *Ty);
64 static ConstantIntegral *getMinValue(const Type *Ty);
65 static ConstantIntegral *getAllOnesValue(const Type *Ty);
67 /// Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const ConstantIntegral *) { return true; }
69 static bool classof(const Constant *CPV); // defined in Constants.cpp
70 static inline bool classof(const Value *V) {
71 return isa<Constant>(V) && classof(cast<Constant>(V));
76 //===---------------------------------------------------------------------------
77 /// ConstantBool - Boolean Values
79 class ConstantBool : public ConstantIntegral {
83 static ConstantBool *True, *False; // The True & False values
85 /// get() - Static factory methods - Return objects of the specified value
86 static ConstantBool *get(bool Value) { return Value ? True : False; }
87 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
89 /// inverted - Return the opposite value of the current value.
90 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
92 /// getValue - return the boolean value of this constant.
94 inline bool getValue() const { return Val; }
96 /// isNullValue - Return true if this is the value that would be returned by
99 virtual bool isNullValue() const { return this == False; }
100 virtual bool isMaxValue() const { return this == True; }
101 virtual bool isMinValue() const { return this == False; }
102 virtual bool isAllOnesValue() const { return this == True; }
104 /// Methods for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const ConstantBool *) { return true; }
106 static bool classof(const Constant *CPV) {
107 return (CPV == True) | (CPV == False);
109 static inline bool classof(const Value *V) {
110 return isa<Constant>(V) && classof(cast<Constant>(V));
115 //===---------------------------------------------------------------------------
116 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
117 /// with integral constants easier.
119 class ConstantInt : public ConstantIntegral {
125 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
126 ConstantInt(const Type *Ty, uint64_t V);
128 /// equalsInt - Provide a helper method that can be used to determine if the
129 /// constant contained within is equal to a constant. This only works for
130 /// very small values, because this is all that can be represented with all
133 bool equalsInt(unsigned char V) const {
135 "equals: Can only be used with very small positive constants!");
136 return Val.Unsigned == V;
139 /// ConstantInt::get static method: return a ConstantInt with the specified
140 /// value. as above, we work only with very small values here.
142 static ConstantInt *get(const Type *Ty, unsigned char V);
144 /// getRawValue - return the underlying value of this constant as a 64-bit
145 /// unsigned integer value.
147 inline uint64_t getRawValue() const { return Val.Unsigned; }
149 /// isNullValue - Return true if this is the value that would be returned by
151 virtual bool isNullValue() const { return Val.Unsigned == 0; }
152 virtual bool isMaxValue() const = 0;
153 virtual bool isMinValue() const = 0;
155 /// Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const ConstantInt *) { return true; }
157 static bool classof(const Constant *CPV); // defined in Constants.cpp
158 static inline bool classof(const Value *V) {
159 return isa<Constant>(V) && classof(cast<Constant>(V));
164 //===---------------------------------------------------------------------------
165 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
167 class ConstantSInt : public ConstantInt {
168 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
169 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
172 ConstantSInt(const Type *Ty, int64_t V);
174 /// get() - Static factory methods - Return objects of the specified value
176 static ConstantSInt *get(const Type *Ty, int64_t V);
178 /// isValueValidForType - return true if Ty is big enough to represent V.
180 static bool isValueValidForType(const Type *Ty, int64_t V);
182 /// getValue - return the underlying value of this constant.
184 inline int64_t getValue() const { return Val.Signed; }
186 virtual bool isAllOnesValue() const { return getValue() == -1; }
188 /// isMaxValue - Return true if this is the largest value that may be
189 /// represented by this type.
191 virtual bool isMaxValue() const {
192 int64_t V = getValue();
193 if (V < 0) return false; // Be careful about wrap-around on 'long's
195 return !isValueValidForType(getType(), V) || V < 0;
198 /// isMinValue - Return true if this is the smallest value that may be
199 /// represented by this type.
201 virtual bool isMinValue() const {
202 int64_t V = getValue();
203 if (V > 0) return false; // Be careful about wrap-around on 'long's
205 return !isValueValidForType(getType(), V) || V > 0;
208 /// Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const ConstantSInt *) { return true; }
211 static bool classof(const Constant *CPV); // defined in Constants.cpp
212 static inline bool classof(const Value *V) {
213 return isa<Constant>(V) && classof(cast<Constant>(V));
217 //===---------------------------------------------------------------------------
218 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
220 class ConstantUInt : public ConstantInt {
221 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
222 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
224 ConstantUInt(const Type *Ty, uint64_t V);
226 /// get() - Static factory methods - Return objects of the specified value
228 static ConstantUInt *get(const Type *Ty, uint64_t V);
230 /// isValueValidForType - return true if Ty is big enough to represent V.
232 static bool isValueValidForType(const Type *Ty, uint64_t V);
234 /// getValue - return the underlying value of this constant.
236 inline uint64_t getValue() const { return Val.Unsigned; }
238 /// isMaxValue - Return true if this is the largest value that may be
239 /// represented by this type.
241 virtual bool isAllOnesValue() const;
242 virtual bool isMaxValue() const { return isAllOnesValue(); }
243 virtual bool isMinValue() const { return getValue() == 0; }
245 /// Methods for support type inquiry through isa, cast, and dyn_cast:
246 static inline bool classof(const ConstantUInt *) { return true; }
247 static bool classof(const Constant *CPV); // defined in Constants.cpp
248 static inline bool classof(const Value *V) {
249 return isa<Constant>(V) && classof(cast<Constant>(V));
254 //===---------------------------------------------------------------------------
255 /// ConstantFP - Floating Point Values [float, double]
257 class ConstantFP : public Constant {
259 friend struct ConstantCreator<ConstantFP, Type, double>;
260 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
262 ConstantFP(const Type *Ty, double V);
264 /// get() - Static factory methods - Return objects of the specified value
265 static ConstantFP *get(const Type *Ty, double V);
267 /// isValueValidForType - return true if Ty is big enough to represent V.
268 static bool isValueValidForType(const Type *Ty, double V);
269 inline double getValue() const { return Val; }
271 /// isNullValue - Return true if this is the value that would be returned by
273 virtual bool isNullValue() const { return Val == 0; }
275 /// Methods for support type inquiry through isa, cast, and dyn_cast:
276 static inline bool classof(const ConstantFP *) { return true; }
277 static bool classof(const Constant *CPV); // defined in Constants.cpp
278 static inline bool classof(const Value *V) {
279 return isa<Constant>(V) && classof(cast<Constant>(V));
284 //===---------------------------------------------------------------------------
285 /// ConstantArray - Constant Array Declarations
287 class ConstantArray : public Constant {
288 friend struct ConstantCreator<ConstantArray, ArrayType,
289 std::vector<Constant*> >;
290 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
292 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
294 /// get() - Static factory methods - Return objects of the specified value
295 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
296 static ConstantArray *get(const std::string &Initializer);
298 /// getType - Specialize the getType() method to always return an ArrayType,
299 /// which reduces the amount of casting needed in parts of the compiler.
301 inline const ArrayType *getType() const {
302 return (ArrayType*)Value::getType();
305 /// getAsString - If the sub-element type of this array is either sbyte or
306 /// ubyte, then this method converts the array to an std::string and returns
307 /// it. Otherwise, it asserts out.
309 std::string getAsString() const;
311 /// getValues - Return a vector of the component constants that make up this
313 inline const std::vector<Use> &getValues() const { return Operands; }
315 /// isNullValue - Return true if this is the value that would be returned by
317 virtual bool isNullValue() const {
318 // FIXME: This should be made to be MUCH faster. Just check against well
320 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
321 if (!cast<Constant>(getOperand(i))->isNullValue())
326 virtual void destroyConstant();
327 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
328 bool DisableChecking = false);
330 /// Methods for support type inquiry through isa, cast, and dyn_cast:
331 static inline bool classof(const ConstantArray *) { return true; }
332 static bool classof(const Constant *CPV); // defined in Constants.cpp
333 static inline bool classof(const Value *V) {
334 return isa<Constant>(V) && classof(cast<Constant>(V));
339 //===---------------------------------------------------------------------------
340 // ConstantStruct - Constant Struct Declarations
342 class ConstantStruct : public Constant {
343 friend struct ConstantCreator<ConstantStruct, StructType,
344 std::vector<Constant*> >;
345 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
347 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
349 /// get() - Static factory methods - Return objects of the specified value
350 static ConstantStruct *get(const StructType *T,
351 const std::vector<Constant*> &V);
353 /// getType() specialization - Reduce amount of casting...
354 inline const StructType *getType() const {
355 return (StructType*)Value::getType();
358 /// getValues - Return a vector of the component constants that make up this
360 inline const std::vector<Use> &getValues() const { return Operands; }
362 /// isNullValue - Return true if this is the value that would be returned by
364 virtual bool isNullValue() const {
365 // FIXME: This should be made to be MUCH faster. Just check against well
367 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
368 if (!cast<Constant>(getOperand(i))->isNullValue())
373 virtual void destroyConstant();
374 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
375 bool DisableChecking = false);
377 /// Methods for support type inquiry through isa, cast, and dyn_cast:
378 static inline bool classof(const ConstantStruct *) { return true; }
379 static bool classof(const Constant *CPV); // defined in Constants.cpp
380 static inline bool classof(const Value *V) {
381 return isa<Constant>(V) && classof(cast<Constant>(V));
385 //===---------------------------------------------------------------------------
386 /// ConstantPointer - Constant Pointer Declarations
388 /// The ConstantPointer class represents a null pointer of a specific type. For
389 /// a more specific/useful instance, a subclass of ConstantPointer should be
392 class ConstantPointer : public Constant {
393 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
395 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
397 inline const PointerType *getType() const {
398 return (PointerType*)Value::getType();
401 /// isNullValue - Return true if this is the value that would be returned by
403 virtual bool isNullValue() const { return false; }
405 /// Methods for support type inquiry through isa, cast, and dyn_cast:
406 static inline bool classof(const ConstantPointer *) { return true; }
407 static bool classof(const Constant *CPV); // defined in Constants.cpp
408 static inline bool classof(const Value *V) {
409 return isa<Constant>(V) && classof(cast<Constant>(V));
413 /// ConstantPointerNull - a constant pointer value that points to null
415 class ConstantPointerNull : public ConstantPointer {
416 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
417 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
419 ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
423 /// get() - Static factory methods - Return objects of the specified value
424 static ConstantPointerNull *get(const PointerType *T);
426 /// isNullValue - Return true if this is the value that would be returned by
428 virtual bool isNullValue() const { return true; }
430 virtual void destroyConstant();
432 /// Methods for support type inquiry through isa, cast, and dyn_cast:
433 static inline bool classof(const ConstantPointerNull *) { return true; }
434 static inline bool classof(const ConstantPointer *P) {
435 return (P->getNumOperands() == 0 && P->isNullValue());
437 static inline bool classof(const Constant *CPV) {
438 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
440 static inline bool classof(const Value *V) {
441 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
446 /// ConstantPointerRef - a constant pointer value that is initialized to
447 /// point to a global value, which lies at a constant, fixed address.
449 class ConstantPointerRef : public ConstantPointer {
450 friend class Module; // Modules maintain these references
451 ConstantPointerRef(const ConstantPointerRef &); // DNI!
454 ConstantPointerRef(GlobalValue *GV);
456 /// get() - Static factory methods - Return objects of the specified value
457 static ConstantPointerRef *get(GlobalValue *GV);
459 const GlobalValue *getValue() const {
460 return cast<GlobalValue>(Operands[0].get());
463 GlobalValue *getValue() {
464 return cast<GlobalValue>(Operands[0].get());
467 virtual void destroyConstant();
468 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
469 bool DisableChecking = false);
471 /// Methods for support type inquiry through isa, cast, and dyn_cast:
472 static inline bool classof(const ConstantPointerRef *) { return true; }
473 static inline bool classof(const ConstantPointer *CPV) {
474 // check for a single operand (the target value)
475 return (CPV->getNumOperands() == 1);
477 static inline bool classof(const Constant *CPV) {
478 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
480 static inline bool classof(const Value *V) {
481 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
485 // ConstantExpr - a constant value that is initialized with an expression using
486 // other constant values. This is only used to represent values that cannot be
487 // evaluated at compile-time (e.g., something derived from an address) because
488 // it does not have a mechanism to store the actual value. Use the appropriate
489 // Constant subclass above for known constants.
491 class ConstantExpr : public Constant {
492 unsigned iType; // Operation type (an Instruction opcode)
493 friend struct ConstantCreator<ConstantExpr,Type,
494 std::pair<unsigned, std::vector<Constant*> > >;
495 friend struct ConvertConstantType<ConstantExpr, Type>;
498 // Cast creation ctor
499 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
500 // Binary/Shift instruction creation ctor
501 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
502 // GEP instruction creation ctor
503 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
506 // These private methods are used by the type resolution code to create
507 // ConstantExprs in intermediate forms.
508 static Constant *getTy(const Type *Ty, unsigned Opcode,
509 Constant *C1, Constant *C2);
510 static Constant *getShiftTy(const Type *Ty,
511 unsigned Opcode, Constant *C1, Constant *C2);
512 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
513 const std::vector<Constant*> &IdxList);
516 // Static methods to construct a ConstantExpr of different kinds. Note that
517 // these methods may return a object that is not an instance of the
518 // ConstantExpr class, because they will attempt to fold the constant
519 // expression into something simpler if possible.
521 /// Cast constant expr
523 static Constant *getCast(Constant *C, const Type *Ty);
525 /// Binary constant expr - Use with binary operators...
527 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
528 return getTy(C1->getType(), Opcode, C1, C2);
531 /// getShift - Return a shift left or shift right constant expr
533 static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2) {
534 return getShiftTy(C1->getType(), Opcode, C1, C2);
537 /// Getelementptr form...
539 static Constant *getGetElementPtr(Constant *C,
540 const std::vector<Constant*> &IdxList);
542 /// isNullValue - Return true if this is the value that would be returned by
544 virtual bool isNullValue() const { return false; }
546 /// getOpcode - Return the opcode at the root of this constant expression
547 unsigned getOpcode() const { return iType; }
549 /// getOpcodeName - Return a string representation for an opcode.
550 const char *getOpcodeName() const;
552 /// isConstantExpr - Return true if this is a ConstantExpr
553 virtual bool isConstantExpr() const { return true; }
555 virtual void destroyConstant();
556 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
557 bool DisableChecking = false);
559 /// Override methods to provide more type information...
560 inline Constant *getOperand(unsigned i) {
561 return cast<Constant>(User::getOperand(i));
563 inline Constant *getOperand(unsigned i) const {
564 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
568 /// Methods for support type inquiry through isa, cast, and dyn_cast:
569 static inline bool classof(const ConstantExpr *) { return true; }
570 static inline bool classof(const Constant *CPV) {
571 return CPV->isConstantExpr();
573 static inline bool classof(const Value *V) {
574 return isa<Constant>(V) && classof(cast<Constant>(V));