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 "llvm/Type.h"
20 #include "llvm/Support/DataTypes.h"
29 template<class ConstantClass, class TypeClass, class ValType>
30 struct ConstantCreator;
31 template<class ConstantClass, class TypeClass>
32 struct ConvertConstantType;
35 //===---------------------------------------------------------------------------
36 /// ConstantIntegral - Shared superclass of boolean and integer constants.
38 /// This class just defines some common interfaces to be implemented.
40 class ConstantIntegral : public Constant {
46 ConstantIntegral(const Type *Ty, uint64_t V);
49 /// getRawValue - return the underlying value of this constant as a 64-bit
50 /// unsigned integer value.
52 inline uint64_t getRawValue() const { return Val.Unsigned; }
54 /// isNullValue - Return true if this is the value that would be returned by
57 virtual bool isNullValue() const = 0;
59 /// isMaxValue - Return true if this is the largest value that may be
60 /// represented by this type.
62 virtual bool isMaxValue() const = 0;
64 /// isMinValue - Return true if this is the smallest value that may be
65 /// represented by this type.
67 virtual bool isMinValue() const = 0;
69 /// isAllOnesValue - Return true if every bit in this constant is set to true.
71 virtual bool isAllOnesValue() const = 0;
73 /// Static constructor to get the maximum/minimum/allones constant of
74 /// specified (integral) type...
76 static ConstantIntegral *getMaxValue(const Type *Ty);
77 static ConstantIntegral *getMinValue(const Type *Ty);
78 static ConstantIntegral *getAllOnesValue(const Type *Ty);
80 /// Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const ConstantIntegral *) { return true; }
82 static bool classof(const Value *V) {
83 return V->getValueType() == SimpleConstantVal &&
84 V->getType()->isIntegral();
89 //===---------------------------------------------------------------------------
90 /// ConstantBool - Boolean Values
92 class ConstantBool : public ConstantIntegral {
95 static ConstantBool *True, *False; // The True & False values
97 /// get() - Static factory methods - Return objects of the specified value
98 static ConstantBool *get(bool Value) { return Value ? True : False; }
99 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
101 /// inverted - Return the opposite value of the current value.
102 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
104 /// getValue - return the boolean value of this constant.
106 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
108 /// isNullValue - Return true if this is the value that would be returned by
111 virtual bool isNullValue() const { return this == False; }
112 virtual bool isMaxValue() const { return this == True; }
113 virtual bool isMinValue() const { return this == False; }
114 virtual bool isAllOnesValue() const { return this == True; }
116 /// Methods for support type inquiry through isa, cast, and dyn_cast:
117 static inline bool classof(const ConstantBool *) { return true; }
118 static bool classof(const Value *V) {
119 return (V == True) | (V == False);
124 //===---------------------------------------------------------------------------
125 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
126 /// with integral constants easier.
128 class ConstantInt : public ConstantIntegral {
130 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
131 ConstantInt(const Type *Ty, uint64_t V);
133 /// equalsInt - Provide a helper method that can be used to determine if the
134 /// constant contained within is equal to a constant. This only works for
135 /// very small values, because this is all that can be represented with all
138 bool equalsInt(unsigned char V) const {
140 "equalsInt: Can only be used with very small positive constants!");
141 return Val.Unsigned == V;
144 /// ConstantInt::get static method: return a ConstantInt with the specified
145 /// value. as above, we work only with very small values here.
147 static ConstantInt *get(const Type *Ty, unsigned char V);
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 Value *V) {
158 return V->getValueType() == SimpleConstantVal &&
159 V->getType()->isInteger();
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 Value *V) {
212 return V->getValueType() == SimpleConstantVal &&
213 V->getType()->isSigned();
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 Value *V) {
248 return V->getValueType() == SimpleConstantVal &&
249 V->getType()->isUnsigned();
254 //===---------------------------------------------------------------------------
255 /// ConstantFP - Floating Point Values [float, double]
257 class ConstantFP : public Constant {
259 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
260 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
261 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
263 ConstantFP(const Type *Ty, double V);
265 /// get() - Static factory methods - Return objects of the specified value
266 static ConstantFP *get(const Type *Ty, double V);
268 /// isValueValidForType - return true if Ty is big enough to represent V.
269 static bool isValueValidForType(const Type *Ty, double V);
270 inline double getValue() const { return Val; }
272 /// isNullValue - Return true if this is the value that would be returned by
273 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
274 /// considers -0.0 to be null as well as 0.0. :(
275 virtual bool isNullValue() const {
284 /// isExactlyValue - We don't rely on operator== working on double values, as
285 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
286 /// As such, this method can be used to do an exact bit-for-bit comparison of
287 /// two floating point values.
288 bool isExactlyValue(double V) const {
302 /// Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const ConstantFP *) { return true; }
304 static bool classof(const Value *V) {
305 return V->getValueType() == SimpleConstantVal &&
306 V->getType()->isFloatingPoint();
310 //===---------------------------------------------------------------------------
311 /// ConstantAggregateZero - All zero aggregate value
313 class ConstantAggregateZero : public Constant {
314 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
315 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
317 ConstantAggregateZero(const Type *Ty)
318 : Constant(Ty, ConstantAggregateZeroVal) {}
320 /// get() - static factory method for creating a null aggregate. It is
321 /// illegal to call this method with a non-aggregate type.
322 static Constant *get(const Type *Ty);
324 /// isNullValue - Return true if this is the value that would be returned by
326 virtual bool isNullValue() const { return true; }
328 virtual void destroyConstant();
329 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
330 bool DisableChecking = false);
332 /// Methods for support type inquiry through isa, cast, and dyn_cast:
334 static bool classof(const ConstantAggregateZero *) { return true; }
335 static bool classof(const Value *V) {
336 return V->getValueType() == ConstantAggregateZeroVal;
341 //===---------------------------------------------------------------------------
342 /// ConstantArray - Constant Array Declarations
344 class ConstantArray : public Constant {
345 friend struct ConstantCreator<ConstantArray, ArrayType,
346 std::vector<Constant*> >;
347 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
349 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
351 /// get() - Static factory methods - Return objects of the specified value
352 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
353 static Constant *get(const std::string &Initializer);
355 /// getType - Specialize the getType() method to always return an ArrayType,
356 /// which reduces the amount of casting needed in parts of the compiler.
358 inline const ArrayType *getType() const {
359 return reinterpret_cast<const ArrayType*>(Value::getType());
362 /// isString - This method returns true if the array is an array of sbyte or
363 /// ubyte, and if the elements of the array are all ConstantInt's.
364 bool isString() const;
366 /// getAsString - If this array is isString(), then this method converts the
367 /// array to an std::string and returns it. Otherwise, it asserts out.
369 std::string getAsString() const;
371 /// isNullValue - Return true if this is the value that would be returned by
372 /// getNullValue. This always returns false because zero arrays are always
373 /// created as ConstantAggregateZero objects.
374 virtual bool isNullValue() const { return false; }
376 virtual void destroyConstant();
377 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
378 bool DisableChecking = false);
380 /// Methods for support type inquiry through isa, cast, and dyn_cast:
381 static inline bool classof(const ConstantArray *) { return true; }
382 static bool classof(const Value *V) {
383 return V->getValueType() == SimpleConstantVal &&
384 V->getType()->getTypeID() == Type::ArrayTyID;
389 //===---------------------------------------------------------------------------
390 // ConstantStruct - Constant Struct Declarations
392 class ConstantStruct : public Constant {
393 friend struct ConstantCreator<ConstantStruct, StructType,
394 std::vector<Constant*> >;
395 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
397 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
399 /// get() - Static factory methods - Return objects of the specified value
401 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
402 static Constant *get(const std::vector<Constant*> &V);
404 /// getType() specialization - Reduce amount of casting...
406 inline const StructType *getType() const {
407 return reinterpret_cast<const StructType*>(Value::getType());
410 /// isNullValue - Return true if this is the value that would be returned by
411 /// getNullValue. This always returns false because zero structs are always
412 /// created as ConstantAggregateZero objects.
413 virtual bool isNullValue() const {
417 virtual void destroyConstant();
418 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
419 bool DisableChecking = false);
421 /// Methods for support type inquiry through isa, cast, and dyn_cast:
422 static inline bool classof(const ConstantStruct *) { return true; }
423 static bool classof(const Value *V) {
424 return V->getValueType() == SimpleConstantVal &&
425 V->getType()->getTypeID() == Type::StructTyID;
429 //===---------------------------------------------------------------------------
430 /// ConstantPacked - Constant Packed Declarations
432 class ConstantPacked : public Constant {
433 friend struct ConstantCreator<ConstantPacked, PackedType,
434 std::vector<Constant*> >;
435 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
437 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
439 /// get() - Static factory methods - Return objects of the specified value
440 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
441 static Constant *get(const std::vector<Constant*> &V);
443 /// getType - Specialize the getType() method to always return an PackedType,
444 /// which reduces the amount of casting needed in parts of the compiler.
446 inline const PackedType *getType() const {
447 return reinterpret_cast<const PackedType*>(Value::getType());
450 /// isNullValue - Return true if this is the value that would be returned by
451 /// getNullValue. This always returns false because zero arrays are always
452 /// created as ConstantAggregateZero objects.
453 virtual bool isNullValue() const { return false; }
455 virtual void destroyConstant();
456 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
457 bool DisableChecking = false);
459 /// Methods for support type inquiry through isa, cast, and dyn_cast:
460 static inline bool classof(const ConstantPacked *) { return true; }
461 static bool classof(const Value *V) {
462 return V->getValueType() == SimpleConstantVal &&
463 V->getType()->getTypeID() == Type::PackedTyID;
467 //===---------------------------------------------------------------------------
468 /// ConstantPointerNull - a constant pointer value that points to null
470 class ConstantPointerNull : public Constant {
471 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
472 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
474 ConstantPointerNull(const PointerType *T)
475 : Constant(reinterpret_cast<const Type*>(T)) {}
479 /// get() - Static factory methods - Return objects of the specified value
480 static ConstantPointerNull *get(const PointerType *T);
482 /// isNullValue - Return true if this is the value that would be returned by
484 virtual bool isNullValue() const { return true; }
486 virtual void destroyConstant();
488 /// getType - Specialize the getType() method to always return an PointerType,
489 /// which reduces the amount of casting needed in parts of the compiler.
491 inline const PointerType *getType() const {
492 return reinterpret_cast<const PointerType*>(Value::getType());
495 /// Methods for support type inquiry through isa, cast, and dyn_cast:
496 static inline bool classof(const ConstantPointerNull *) { return true; }
497 static bool classof(const Value *V) {
498 return V->getValueType() == SimpleConstantVal &&
499 isa<PointerType>(V->getType());
504 // ConstantExpr - a constant value that is initialized with an expression using
505 // other constant values. This is only used to represent values that cannot be
506 // evaluated at compile-time (e.g., something derived from an address) because
507 // it does not have a mechanism to store the actual value. Use the appropriate
508 // Constant subclass above for known constants.
510 class ConstantExpr : public Constant {
511 unsigned iType; // Operation type (an Instruction opcode)
512 friend struct ConstantCreator<ConstantExpr,Type,
513 std::pair<unsigned, std::vector<Constant*> > >;
514 friend struct ConvertConstantType<ConstantExpr, Type>;
517 // Cast creation ctor
518 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
519 // Binary/Shift instruction creation ctor
520 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
521 // Select instruction creation ctor
522 ConstantExpr(Constant *C, Constant *V1, Constant *V2);
523 // GEP instruction creation ctor
524 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
527 // These private methods are used by the type resolution code to create
528 // ConstantExprs in intermediate forms.
529 static Constant *getTy(const Type *Ty, unsigned Opcode,
530 Constant *C1, Constant *C2);
531 static Constant *getShiftTy(const Type *Ty,
532 unsigned Opcode, Constant *C1, Constant *C2);
533 static Constant *getSelectTy(const Type *Ty,
534 Constant *C1, Constant *C2, Constant *C3);
535 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
536 const std::vector<Constant*> &IdxList);
539 // Static methods to construct a ConstantExpr of different kinds. Note that
540 // these methods may return a object that is not an instance of the
541 // ConstantExpr class, because they will attempt to fold the constant
542 // expression into something simpler if possible.
544 /// Cast constant expr
546 static Constant *getCast(Constant *C, const Type *Ty);
547 static Constant *getSignExtend(Constant *C, const Type *Ty);
548 static Constant *getZeroExtend(Constant *C, const Type *Ty);
550 /// Select constant expr
552 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
553 return getSelectTy(V1->getType(), C, V1, V2);
557 /// ConstantExpr::get - Return a binary or shift operator constant expression,
558 /// folding if possible.
560 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
562 /// ConstantExpr::get* - Return some common constants without having to
563 /// specify the full Instruction::OPCODE identifier.
565 static Constant *getNeg(Constant *C);
566 static Constant *getNot(Constant *C);
567 static Constant *getAdd(Constant *C1, Constant *C2);
568 static Constant *getSub(Constant *C1, Constant *C2);
569 static Constant *getMul(Constant *C1, Constant *C2);
570 static Constant *getDiv(Constant *C1, Constant *C2);
571 static Constant *getRem(Constant *C1, Constant *C2);
572 static Constant *getAnd(Constant *C1, Constant *C2);
573 static Constant *getOr(Constant *C1, Constant *C2);
574 static Constant *getXor(Constant *C1, Constant *C2);
575 static Constant *getSetEQ(Constant *C1, Constant *C2);
576 static Constant *getSetNE(Constant *C1, Constant *C2);
577 static Constant *getSetLT(Constant *C1, Constant *C2);
578 static Constant *getSetGT(Constant *C1, Constant *C2);
579 static Constant *getSetLE(Constant *C1, Constant *C2);
580 static Constant *getSetGE(Constant *C1, Constant *C2);
581 static Constant *getShl(Constant *C1, Constant *C2);
582 static Constant *getShr(Constant *C1, Constant *C2);
584 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
585 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
587 /// Getelementptr form...
589 static Constant *getGetElementPtr(Constant *C,
590 const std::vector<Constant*> &IdxList);
592 /// isNullValue - Return true if this is the value that would be returned by
594 virtual bool isNullValue() const { return false; }
596 /// getOpcode - Return the opcode at the root of this constant expression
597 unsigned getOpcode() const { return iType; }
599 /// getOpcodeName - Return a string representation for an opcode.
600 const char *getOpcodeName() const;
602 virtual void destroyConstant();
603 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
604 bool DisableChecking = false);
606 /// Override methods to provide more type information...
607 inline Constant *getOperand(unsigned i) {
608 return cast<Constant>(User::getOperand(i));
610 inline Constant *getOperand(unsigned i) const {
611 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
615 /// Methods for support type inquiry through isa, cast, and dyn_cast:
616 static inline bool classof(const ConstantExpr *) { return true; }
617 static inline bool classof(const Value *V) {
618 return V->getValueType() == ConstantExprVal;
622 } // End llvm namespace