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 "Support/DataTypes.h"
28 template<class ConstantClass, class TypeClass, class ValType>
29 struct ConstantCreator;
30 template<class ConstantClass, class TypeClass>
31 struct ConvertConstantType;
34 //===---------------------------------------------------------------------------
35 /// ConstantIntegral - Shared superclass of boolean and integer constants.
37 /// This class just defines some common interfaces to be implemented.
39 class ConstantIntegral : public Constant {
45 ConstantIntegral(const Type *Ty, uint64_t V);
48 /// getRawValue - return the underlying value of this constant as a 64-bit
49 /// unsigned integer value.
51 inline uint64_t getRawValue() const { return Val.Unsigned; }
53 /// isNullValue - Return true if this is the value that would be returned by
56 virtual bool isNullValue() const = 0;
58 /// isMaxValue - Return true if this is the largest value that may be
59 /// represented by this type.
61 virtual bool isMaxValue() const = 0;
63 /// isMinValue - Return true if this is the smallest value that may be
64 /// represented by this type.
66 virtual bool isMinValue() const = 0;
68 /// isAllOnesValue - Return true if every bit in this constant is set to true.
70 virtual bool isAllOnesValue() const = 0;
72 /// Static constructor to get the maximum/minimum/allones constant of
73 /// specified (integral) type...
75 static ConstantIntegral *getMaxValue(const Type *Ty);
76 static ConstantIntegral *getMinValue(const Type *Ty);
77 static ConstantIntegral *getAllOnesValue(const Type *Ty);
79 /// Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const ConstantIntegral *) { return true; }
81 static bool classof(const Value *V) {
82 return V->getValueType() == SimpleConstantVal &&
83 V->getType()->isIntegral();
88 //===---------------------------------------------------------------------------
89 /// ConstantBool - Boolean Values
91 class ConstantBool : public ConstantIntegral {
94 static ConstantBool *True, *False; // The True & False values
96 /// get() - Static factory methods - Return objects of the specified value
97 static ConstantBool *get(bool Value) { return Value ? True : False; }
98 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
100 /// inverted - Return the opposite value of the current value.
101 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
103 /// getValue - return the boolean value of this constant.
105 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
107 /// isNullValue - Return true if this is the value that would be returned by
110 virtual bool isNullValue() const { return this == False; }
111 virtual bool isMaxValue() const { return this == True; }
112 virtual bool isMinValue() const { return this == False; }
113 virtual bool isAllOnesValue() const { return this == True; }
115 /// Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const ConstantBool *) { return true; }
117 static bool classof(const Value *V) {
118 return (V == True) | (V == False);
123 //===---------------------------------------------------------------------------
124 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
125 /// with integral constants easier.
127 class ConstantInt : public ConstantIntegral {
129 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
130 ConstantInt(const Type *Ty, uint64_t V);
132 /// equalsInt - Provide a helper method that can be used to determine if the
133 /// constant contained within is equal to a constant. This only works for
134 /// very small values, because this is all that can be represented with all
137 bool equalsInt(unsigned char V) const {
139 "equalsInt: Can only be used with very small positive constants!");
140 return Val.Unsigned == V;
143 /// ConstantInt::get static method: return a ConstantInt with the specified
144 /// value. as above, we work only with very small values here.
146 static ConstantInt *get(const Type *Ty, unsigned char V);
148 /// isNullValue - Return true if this is the value that would be returned by
150 virtual bool isNullValue() const { return Val.Unsigned == 0; }
151 virtual bool isMaxValue() const = 0;
152 virtual bool isMinValue() const = 0;
154 /// Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const ConstantInt *) { return true; }
156 static bool classof(const Value *V) {
157 return V->getValueType() == SimpleConstantVal &&
158 V->getType()->isInteger();
163 //===---------------------------------------------------------------------------
164 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
166 class ConstantSInt : public ConstantInt {
167 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
168 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
171 ConstantSInt(const Type *Ty, int64_t V);
173 /// get() - Static factory methods - Return objects of the specified value
175 static ConstantSInt *get(const Type *Ty, int64_t V);
177 /// isValueValidForType - return true if Ty is big enough to represent V.
179 static bool isValueValidForType(const Type *Ty, int64_t V);
181 /// getValue - return the underlying value of this constant.
183 inline int64_t getValue() const { return Val.Signed; }
185 virtual bool isAllOnesValue() const { return getValue() == -1; }
187 /// isMaxValue - Return true if this is the largest value that may be
188 /// represented by this type.
190 virtual bool isMaxValue() const {
191 int64_t V = getValue();
192 if (V < 0) return false; // Be careful about wrap-around on 'long's
194 return !isValueValidForType(getType(), V) || V < 0;
197 /// isMinValue - Return true if this is the smallest value that may be
198 /// represented by this type.
200 virtual bool isMinValue() const {
201 int64_t V = getValue();
202 if (V > 0) return false; // Be careful about wrap-around on 'long's
204 return !isValueValidForType(getType(), V) || V > 0;
207 /// Methods for support type inquiry through isa, cast, and dyn_cast:
209 static inline bool classof(const ConstantSInt *) { return true; }
210 static bool classof(const Value *V) {
211 return V->getValueType() == SimpleConstantVal &&
212 V->getType()->isSigned();
216 //===---------------------------------------------------------------------------
217 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
219 class ConstantUInt : public ConstantInt {
220 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
221 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
223 ConstantUInt(const Type *Ty, uint64_t V);
225 /// get() - Static factory methods - Return objects of the specified value
227 static ConstantUInt *get(const Type *Ty, uint64_t V);
229 /// isValueValidForType - return true if Ty is big enough to represent V.
231 static bool isValueValidForType(const Type *Ty, uint64_t V);
233 /// getValue - return the underlying value of this constant.
235 inline uint64_t getValue() const { return Val.Unsigned; }
237 /// isMaxValue - Return true if this is the largest value that may be
238 /// represented by this type.
240 virtual bool isAllOnesValue() const;
241 virtual bool isMaxValue() const { return isAllOnesValue(); }
242 virtual bool isMinValue() const { return getValue() == 0; }
244 /// Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const ConstantUInt *) { return true; }
246 static bool classof(const Value *V) {
247 return V->getValueType() == SimpleConstantVal &&
248 V->getType()->isUnsigned();
253 //===---------------------------------------------------------------------------
254 /// ConstantFP - Floating Point Values [float, double]
256 class ConstantFP : public Constant {
258 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
259 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
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
272 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
273 /// considers -0.0 to be null as well as 0.0. :(
274 virtual bool isNullValue() const {
283 /// isExactlyValue - We don't rely on operator== working on double values, as
284 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
285 /// As such, this method can be used to do an exact bit-for-bit comparison of
286 /// two floating point values.
287 bool isExactlyValue(double V) const {
301 /// Methods for support type inquiry through isa, cast, and dyn_cast:
302 static inline bool classof(const ConstantFP *) { return true; }
303 static bool classof(const Value *V) {
304 return V->getValueType() == SimpleConstantVal &&
305 V->getType()->isFloatingPoint();
309 //===---------------------------------------------------------------------------
310 /// ConstantAggregateZero - All zero aggregate value
312 class ConstantAggregateZero : public Constant {
313 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
314 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
316 ConstantAggregateZero(const Type *Ty)
317 : Constant(Ty, ConstantAggregateZeroVal) {}
319 /// get() - static factory method for creating a null aggregate. It is
320 /// illegal to call this method with a non-aggregate type.
321 static Constant *get(const Type *Ty);
323 /// isNullValue - Return true if this is the value that would be returned by
325 virtual bool isNullValue() const { return true; }
327 virtual void destroyConstant();
328 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
329 bool DisableChecking = false);
331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
333 static bool classof(const ConstantAggregateZero *) { return true; }
334 static bool classof(const Value *V) {
335 return V->getValueType() == ConstantAggregateZeroVal;
340 //===---------------------------------------------------------------------------
341 /// ConstantArray - Constant Array Declarations
343 class ConstantArray : public Constant {
344 friend struct ConstantCreator<ConstantArray, ArrayType,
345 std::vector<Constant*> >;
346 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
348 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
350 /// get() - Static factory methods - Return objects of the specified value
351 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
352 static Constant *get(const std::string &Initializer);
354 /// getType - Specialize the getType() method to always return an ArrayType,
355 /// which reduces the amount of casting needed in parts of the compiler.
357 inline const ArrayType *getType() const {
358 return reinterpret_cast<const ArrayType*>(Value::getType());
361 /// isString - This method returns true if the array is an array of sbyte or
362 /// ubyte, and if the elements of the array are all ConstantInt's.
363 bool isString() const;
365 /// getAsString - If this array is isString(), then this method converts the
366 /// array to an std::string and returns it. Otherwise, it asserts out.
368 std::string getAsString() const;
370 /// getValues - Return a vector of the component constants that make up this
372 inline const std::vector<Use> &getValues() const { return Operands; }
374 /// isNullValue - Return true if this is the value that would be returned by
375 /// getNullValue. This always returns false because zero arrays are always
376 /// created as ConstantAggregateZero objects.
377 virtual bool isNullValue() const { return false; }
379 virtual void destroyConstant();
380 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
381 bool DisableChecking = false);
383 /// Methods for support type inquiry through isa, cast, and dyn_cast:
384 static inline bool classof(const ConstantArray *) { return true; }
385 static bool classof(const Value *V) {
386 return V->getValueType() == SimpleConstantVal &&
387 V->getType()->getTypeID() == Type::ArrayTyID;
392 //===---------------------------------------------------------------------------
393 // ConstantStruct - Constant Struct Declarations
395 class ConstantStruct : public Constant {
396 friend struct ConstantCreator<ConstantStruct, StructType,
397 std::vector<Constant*> >;
398 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
400 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
402 /// get() - Static factory methods - Return objects of the specified value
404 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
405 static Constant *get(const std::vector<Constant*> &V);
407 /// getType() specialization - Reduce amount of casting...
409 inline const StructType *getType() const {
410 return reinterpret_cast<const StructType*>(Value::getType());
413 /// getValues - Return a vector of the component constants that make up this
415 inline const std::vector<Use> &getValues() const { return Operands; }
417 /// isNullValue - Return true if this is the value that would be returned by
418 /// getNullValue. This always returns false because zero structs are always
419 /// created as ConstantAggregateZero objects.
420 virtual bool isNullValue() const {
424 virtual void destroyConstant();
425 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
426 bool DisableChecking = false);
428 /// Methods for support type inquiry through isa, cast, and dyn_cast:
429 static inline bool classof(const ConstantStruct *) { return true; }
430 static bool classof(const Value *V) {
431 return V->getValueType() == SimpleConstantVal &&
432 V->getType()->getTypeID() == Type::StructTyID;
436 //===---------------------------------------------------------------------------
437 /// ConstantPointerNull - a constant pointer value that points to null
439 class ConstantPointerNull : public Constant {
440 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
441 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
443 ConstantPointerNull(const PointerType *T)
444 : Constant(reinterpret_cast<const Type*>(T)) {}
448 /// get() - Static factory methods - Return objects of the specified value
449 static ConstantPointerNull *get(const PointerType *T);
451 /// isNullValue - Return true if this is the value that would be returned by
453 virtual bool isNullValue() const { return true; }
455 virtual void destroyConstant();
457 /// Methods for support type inquiry through isa, cast, and dyn_cast:
458 static inline bool classof(const ConstantPointerNull *) { return true; }
459 static bool classof(const Value *V) {
460 return V->getValueType() == SimpleConstantVal &&
461 isa<PointerType>(V->getType());
466 // ConstantExpr - a constant value that is initialized with an expression using
467 // other constant values. This is only used to represent values that cannot be
468 // evaluated at compile-time (e.g., something derived from an address) because
469 // it does not have a mechanism to store the actual value. Use the appropriate
470 // Constant subclass above for known constants.
472 class ConstantExpr : public Constant {
473 unsigned iType; // Operation type (an Instruction opcode)
474 friend struct ConstantCreator<ConstantExpr,Type,
475 std::pair<unsigned, std::vector<Constant*> > >;
476 friend struct ConvertConstantType<ConstantExpr, Type>;
479 // Cast creation ctor
480 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
481 // Binary/Shift instruction creation ctor
482 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
483 // Select instruction creation ctor
484 ConstantExpr(Constant *C, Constant *V1, Constant *V2);
485 // GEP instruction creation ctor
486 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
489 // These private methods are used by the type resolution code to create
490 // ConstantExprs in intermediate forms.
491 static Constant *getTy(const Type *Ty, unsigned Opcode,
492 Constant *C1, Constant *C2);
493 static Constant *getShiftTy(const Type *Ty,
494 unsigned Opcode, Constant *C1, Constant *C2);
495 static Constant *getSelectTy(const Type *Ty,
496 Constant *C1, Constant *C2, Constant *C3);
497 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
498 const std::vector<Constant*> &IdxList);
501 // Static methods to construct a ConstantExpr of different kinds. Note that
502 // these methods may return a object that is not an instance of the
503 // ConstantExpr class, because they will attempt to fold the constant
504 // expression into something simpler if possible.
506 /// Cast constant expr
508 static Constant *getCast(Constant *C, const Type *Ty);
509 static Constant *getSignExtend(Constant *C, const Type *Ty);
510 static Constant *getZeroExtend(Constant *C, const Type *Ty);
512 /// Select constant expr
514 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
515 return getSelectTy(V1->getType(), C, V1, V2);
519 /// ConstantExpr::get - Return a binary or shift operator constant expression,
520 /// folding if possible.
522 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
523 return getTy(C1->getType(), Opcode, C1, C2);
526 /// ConstantExpr::get* - Return some common constants without having to
527 /// specify the full Instruction::OPCODE identifier.
529 static Constant *getNeg(Constant *C);
530 static Constant *getNot(Constant *C);
531 static Constant *getAdd(Constant *C1, Constant *C2);
532 static Constant *getSub(Constant *C1, Constant *C2);
533 static Constant *getMul(Constant *C1, Constant *C2);
534 static Constant *getDiv(Constant *C1, Constant *C2);
535 static Constant *getRem(Constant *C1, Constant *C2);
536 static Constant *getAnd(Constant *C1, Constant *C2);
537 static Constant *getOr(Constant *C1, Constant *C2);
538 static Constant *getXor(Constant *C1, Constant *C2);
539 static Constant *getSetEQ(Constant *C1, Constant *C2);
540 static Constant *getSetNE(Constant *C1, Constant *C2);
541 static Constant *getSetLT(Constant *C1, Constant *C2);
542 static Constant *getSetGT(Constant *C1, Constant *C2);
543 static Constant *getSetLE(Constant *C1, Constant *C2);
544 static Constant *getSetGE(Constant *C1, Constant *C2);
545 static Constant *getShl(Constant *C1, Constant *C2);
546 static Constant *getShr(Constant *C1, Constant *C2);
548 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
549 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
551 /// Getelementptr form...
553 static Constant *getGetElementPtr(Constant *C,
554 const std::vector<Constant*> &IdxList);
556 /// isNullValue - Return true if this is the value that would be returned by
558 virtual bool isNullValue() const { return false; }
560 /// getOpcode - Return the opcode at the root of this constant expression
561 unsigned getOpcode() const { return iType; }
563 /// getOpcodeName - Return a string representation for an opcode.
564 const char *getOpcodeName() const;
566 virtual void destroyConstant();
567 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
568 bool DisableChecking = false);
570 /// Override methods to provide more type information...
571 inline Constant *getOperand(unsigned i) {
572 return cast<Constant>(User::getOperand(i));
574 inline Constant *getOperand(unsigned i) const {
575 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
579 /// Methods for support type inquiry through isa, cast, and dyn_cast:
580 static inline bool classof(const ConstantExpr *) { return true; }
581 static inline bool classof(const Value *V) {
582 return V->getValueType() == ConstantExprVal;
586 } // End llvm namespace