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 flavors of constant values that live in LLVM. Note
12 // that Constants are immutable (once created they never change) and are fully
13 // shared by structural equivalence. This means that two structurally
14 // equivalent constants will always have the same address. Constant's are
15 // created on demand as needed and never deleted: thus clients don't have to
16 // worry about the lifetime of the objects.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CONSTANTS_H
21 #define LLVM_CONSTANTS_H
23 #include "llvm/Constant.h"
24 #include "llvm/Type.h"
25 #include "llvm/Support/DataTypes.h"
34 template<class ConstantClass, class TypeClass, class ValType>
35 struct ConstantCreator;
36 template<class ConstantClass, class TypeClass>
37 struct ConvertConstantType;
39 //===----------------------------------------------------------------------===//
40 /// ConstantIntegral - Shared superclass of boolean and integer constants.
42 /// This class just defines some common interfaces to be implemented.
44 class ConstantIntegral : public Constant {
50 ConstantIntegral(const Type *Ty, uint64_t V);
53 /// getRawValue - return the underlying value of this constant as a 64-bit
54 /// unsigned integer value.
56 inline uint64_t getRawValue() const { return Val.Unsigned; }
58 /// isNullValue - Return true if this is the value that would be returned by
61 virtual bool isNullValue() const = 0;
63 /// isMaxValue - Return true if this is the largest value that may be
64 /// represented by this type.
66 virtual bool isMaxValue() const = 0;
68 /// isMinValue - Return true if this is the smallest value that may be
69 /// represented by this type.
71 virtual bool isMinValue() const = 0;
73 /// isAllOnesValue - Return true if every bit in this constant is set to true.
75 virtual bool isAllOnesValue() const = 0;
77 /// Static constructor to get the maximum/minimum/allones constant of
78 /// specified (integral) type...
80 static ConstantIntegral *getMaxValue(const Type *Ty);
81 static ConstantIntegral *getMinValue(const Type *Ty);
82 static ConstantIntegral *getAllOnesValue(const Type *Ty);
84 /// Methods for support type inquiry through isa, cast, and dyn_cast:
85 static inline bool classof(const ConstantIntegral *) { return true; }
86 static bool classof(const Value *V) {
87 return V->getValueType() == SimpleConstantVal &&
88 V->getType()->isIntegral();
93 //===----------------------------------------------------------------------===//
94 /// ConstantBool - Boolean Values
96 class ConstantBool : public ConstantIntegral {
99 static ConstantBool *True, *False; // The True & False values
101 /// get() - Static factory methods - Return objects of the specified value
102 static ConstantBool *get(bool Value) { return Value ? True : False; }
103 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
105 /// inverted - Return the opposite value of the current value.
106 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
108 /// getValue - return the boolean value of this constant.
110 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
112 /// isNullValue - Return true if this is the value that would be returned by
115 virtual bool isNullValue() const { return this == False; }
116 virtual bool isMaxValue() const { return this == True; }
117 virtual bool isMinValue() const { return this == False; }
118 virtual bool isAllOnesValue() const { return this == True; }
120 /// Methods for support type inquiry through isa, cast, and dyn_cast:
121 static inline bool classof(const ConstantBool *) { return true; }
122 static bool classof(const Value *V) {
123 return (V == True) | (V == False);
128 //===----------------------------------------------------------------------===//
129 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
130 /// with integral constants easier.
132 class ConstantInt : public ConstantIntegral {
134 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
135 ConstantInt(const Type *Ty, uint64_t V);
137 /// equalsInt - Provide a helper method that can be used to determine if the
138 /// constant contained within is equal to a constant. This only works for
139 /// very small values, because this is all that can be represented with all
142 bool equalsInt(unsigned char V) const {
144 "equalsInt: Can only be used with very small positive constants!");
145 return Val.Unsigned == V;
148 /// ConstantInt::get static method: return a ConstantInt with the specified
149 /// value. as above, we work only with very small values here.
151 static ConstantInt *get(const Type *Ty, unsigned char V);
153 /// isNullValue - Return true if this is the value that would be returned by
155 virtual bool isNullValue() const { return Val.Unsigned == 0; }
156 virtual bool isMaxValue() const = 0;
157 virtual bool isMinValue() const = 0;
159 /// Methods for support type inquiry through isa, cast, and dyn_cast:
160 static inline bool classof(const ConstantInt *) { return true; }
161 static bool classof(const Value *V) {
162 return V->getValueType() == SimpleConstantVal &&
163 V->getType()->isInteger();
168 //===----------------------------------------------------------------------===//
169 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
171 class ConstantSInt : public ConstantInt {
172 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
173 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
176 ConstantSInt(const Type *Ty, int64_t V);
178 /// get() - Static factory methods - Return objects of the specified value
180 static ConstantSInt *get(const Type *Ty, int64_t V);
182 /// isValueValidForType - return true if Ty is big enough to represent V.
184 static bool isValueValidForType(const Type *Ty, int64_t V);
186 /// getValue - return the underlying value of this constant.
188 inline int64_t getValue() const { return Val.Signed; }
190 virtual bool isAllOnesValue() const { return getValue() == -1; }
192 /// isMaxValue - Return true if this is the largest value that may be
193 /// represented by this type.
195 virtual bool isMaxValue() const {
196 int64_t V = getValue();
197 if (V < 0) return false; // Be careful about wrap-around on 'long's
199 return !isValueValidForType(getType(), V) || V < 0;
202 /// isMinValue - Return true if this is the smallest value that may be
203 /// represented by this type.
205 virtual bool isMinValue() const {
206 int64_t V = getValue();
207 if (V > 0) return false; // Be careful about wrap-around on 'long's
209 return !isValueValidForType(getType(), V) || V > 0;
212 /// Methods for support type inquiry through isa, cast, and dyn_cast:
214 static inline bool classof(const ConstantSInt *) { return true; }
215 static bool classof(const Value *V) {
216 return V->getValueType() == SimpleConstantVal &&
217 V->getType()->isSigned();
221 //===----------------------------------------------------------------------===//
222 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
224 class ConstantUInt : public ConstantInt {
225 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
226 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
228 ConstantUInt(const Type *Ty, uint64_t V);
230 /// get() - Static factory methods - Return objects of the specified value
232 static ConstantUInt *get(const Type *Ty, uint64_t V);
234 /// isValueValidForType - return true if Ty is big enough to represent V.
236 static bool isValueValidForType(const Type *Ty, uint64_t V);
238 /// getValue - return the underlying value of this constant.
240 inline uint64_t getValue() const { return Val.Unsigned; }
242 /// isMaxValue - Return true if this is the largest value that may be
243 /// represented by this type.
245 virtual bool isAllOnesValue() const;
246 virtual bool isMaxValue() const { return isAllOnesValue(); }
247 virtual bool isMinValue() const { return getValue() == 0; }
249 /// Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const ConstantUInt *) { return true; }
251 static bool classof(const Value *V) {
252 return V->getValueType() == SimpleConstantVal &&
253 V->getType()->isUnsigned();
258 //===----------------------------------------------------------------------===//
259 /// ConstantFP - Floating Point Values [float, double]
261 class ConstantFP : public Constant {
263 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
264 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
265 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
267 ConstantFP(const Type *Ty, double V);
269 /// get() - Static factory methods - Return objects of the specified value
270 static ConstantFP *get(const Type *Ty, double V);
272 /// isValueValidForType - return true if Ty is big enough to represent V.
273 static bool isValueValidForType(const Type *Ty, double V);
274 inline double getValue() const { return Val; }
276 /// isNullValue - Return true if this is the value that would be returned by
277 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
278 /// considers -0.0 to be null as well as 0.0. :(
279 virtual bool isNullValue() const {
288 /// isExactlyValue - We don't rely on operator== working on double values, as
289 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
290 /// As such, this method can be used to do an exact bit-for-bit comparison of
291 /// two floating point values.
292 bool isExactlyValue(double V) const {
306 /// Methods for support type inquiry through isa, cast, and dyn_cast:
307 static inline bool classof(const ConstantFP *) { return true; }
308 static bool classof(const Value *V) {
309 return V->getValueType() == SimpleConstantVal &&
310 V->getType()->isFloatingPoint();
314 //===----------------------------------------------------------------------===//
315 /// ConstantAggregateZero - All zero aggregate value
317 class ConstantAggregateZero : public Constant {
318 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
319 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
321 ConstantAggregateZero(const Type *Ty)
322 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
324 /// get() - static factory method for creating a null aggregate. It is
325 /// illegal to call this method with a non-aggregate type.
326 static Constant *get(const Type *Ty);
328 /// isNullValue - Return true if this is the value that would be returned by
330 virtual bool isNullValue() const { return true; }
332 virtual void destroyConstant();
333 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
334 bool DisableChecking = false);
336 /// Methods for support type inquiry through isa, cast, and dyn_cast:
338 static bool classof(const ConstantAggregateZero *) { return true; }
339 static bool classof(const Value *V) {
340 return V->getValueType() == ConstantAggregateZeroVal;
345 //===----------------------------------------------------------------------===//
346 /// ConstantArray - Constant Array Declarations
348 class ConstantArray : public Constant {
349 friend struct ConstantCreator<ConstantArray, ArrayType,
350 std::vector<Constant*> >;
351 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
353 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
356 /// get() - Static factory methods - Return objects of the specified value
357 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
358 static Constant *get(const std::string &Initializer);
360 /// getType - Specialize the getType() method to always return an ArrayType,
361 /// which reduces the amount of casting needed in parts of the compiler.
363 inline const ArrayType *getType() const {
364 return reinterpret_cast<const ArrayType*>(Value::getType());
367 /// isString - This method returns true if the array is an array of sbyte or
368 /// ubyte, and if the elements of the array are all ConstantInt's.
369 bool isString() const;
371 /// getAsString - If this array is isString(), then this method converts the
372 /// array to an std::string and returns it. Otherwise, it asserts out.
374 std::string getAsString() const;
376 /// isNullValue - Return true if this is the value that would be returned by
377 /// getNullValue. This always returns false because zero arrays are always
378 /// created as ConstantAggregateZero objects.
379 virtual bool isNullValue() const { return false; }
381 virtual void destroyConstant();
382 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
383 bool DisableChecking = false);
385 /// Methods for support type inquiry through isa, cast, and dyn_cast:
386 static inline bool classof(const ConstantArray *) { return true; }
387 static bool classof(const Value *V) {
388 return V->getValueType() == SimpleConstantVal &&
389 V->getType()->getTypeID() == Type::ArrayTyID;
394 //===----------------------------------------------------------------------===//
395 // ConstantStruct - Constant Struct Declarations
397 class ConstantStruct : public Constant {
398 friend struct ConstantCreator<ConstantStruct, StructType,
399 std::vector<Constant*> >;
400 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
402 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
405 /// get() - Static factory methods - Return objects of the specified value
407 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
408 static Constant *get(const std::vector<Constant*> &V);
410 /// getType() specialization - Reduce amount of casting...
412 inline const StructType *getType() const {
413 return reinterpret_cast<const StructType*>(Value::getType());
416 /// isNullValue - Return true if this is the value that would be returned by
417 /// getNullValue. This always returns false because zero structs are always
418 /// created as ConstantAggregateZero objects.
419 virtual bool isNullValue() const {
423 virtual void destroyConstant();
424 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
425 bool DisableChecking = false);
427 /// Methods for support type inquiry through isa, cast, and dyn_cast:
428 static inline bool classof(const ConstantStruct *) { return true; }
429 static bool classof(const Value *V) {
430 return V->getValueType() == SimpleConstantVal &&
431 V->getType()->getTypeID() == Type::StructTyID;
435 //===----------------------------------------------------------------------===//
436 /// ConstantPacked - Constant Packed Declarations
438 class ConstantPacked : public Constant {
439 friend struct ConstantCreator<ConstantPacked, PackedType,
440 std::vector<Constant*> >;
441 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
443 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
446 /// get() - Static factory methods - Return objects of the specified value
447 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
448 static Constant *get(const std::vector<Constant*> &V);
450 /// getType - Specialize the getType() method to always return an PackedType,
451 /// which reduces the amount of casting needed in parts of the compiler.
453 inline const PackedType *getType() const {
454 return reinterpret_cast<const PackedType*>(Value::getType());
457 /// isNullValue - Return true if this is the value that would be returned by
458 /// getNullValue. This always returns false because zero arrays are always
459 /// created as ConstantAggregateZero objects.
460 virtual bool isNullValue() const { return false; }
462 virtual void destroyConstant();
463 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
464 bool DisableChecking = false);
466 /// Methods for support type inquiry through isa, cast, and dyn_cast:
467 static inline bool classof(const ConstantPacked *) { return true; }
468 static bool classof(const Value *V) {
469 return V->getValueType() == SimpleConstantVal &&
470 V->getType()->getTypeID() == Type::PackedTyID;
474 //===----------------------------------------------------------------------===//
475 /// ConstantPointerNull - a constant pointer value that points to null
477 class ConstantPointerNull : public Constant {
478 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
479 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
481 ConstantPointerNull(const PointerType *T)
482 : Constant(reinterpret_cast<const Type*>(T),
483 Value::SimpleConstantVal, 0, 0) {}
487 /// get() - Static factory methods - Return objects of the specified value
488 static ConstantPointerNull *get(const PointerType *T);
490 /// isNullValue - Return true if this is the value that would be returned by
492 virtual bool isNullValue() const { return true; }
494 virtual void destroyConstant();
496 /// getType - Specialize the getType() method to always return an PointerType,
497 /// which reduces the amount of casting needed in parts of the compiler.
499 inline const PointerType *getType() const {
500 return reinterpret_cast<const PointerType*>(Value::getType());
503 /// Methods for support type inquiry through isa, cast, and dyn_cast:
504 static inline bool classof(const ConstantPointerNull *) { return true; }
505 static bool classof(const Value *V) {
506 return V->getValueType() == SimpleConstantVal &&
507 isa<PointerType>(V->getType());
512 /// ConstantExpr - a constant value that is initialized with an expression using
513 /// other constant values.
515 /// This class uses the standard Instruction opcodes to define the various
516 /// constant expressions. The Opcode field for the ConstantExpr class is
517 /// maintained in the Value::SubclassData field.
518 class ConstantExpr : public Constant {
519 friend struct ConstantCreator<ConstantExpr,Type,
520 std::pair<unsigned, std::vector<Constant*> > >;
521 friend struct ConvertConstantType<ConstantExpr, Type>;
524 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
525 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
526 // Operation type (an Instruction opcode) is stored as the SubclassData.
527 SubclassData = Opcode;
530 // These private methods are used by the type resolution code to create
531 // ConstantExprs in intermediate forms.
532 static Constant *getTy(const Type *Ty, unsigned Opcode,
533 Constant *C1, Constant *C2);
534 static Constant *getShiftTy(const Type *Ty,
535 unsigned Opcode, Constant *C1, Constant *C2);
536 static Constant *getSelectTy(const Type *Ty,
537 Constant *C1, Constant *C2, Constant *C3);
538 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
539 const std::vector<Value*> &IdxList);
542 // Static methods to construct a ConstantExpr of different kinds. Note that
543 // these methods may return a object that is not an instance of the
544 // ConstantExpr class, because they will attempt to fold the constant
545 // expression into something simpler if possible.
547 /// Cast constant expr
549 static Constant *getCast(Constant *C, const Type *Ty);
550 static Constant *getSignExtend(Constant *C, const Type *Ty);
551 static Constant *getZeroExtend(Constant *C, const Type *Ty);
553 /// Select constant expr
555 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
556 return getSelectTy(V1->getType(), C, V1, V2);
559 /// getSizeOf constant expr - computes the size of a type in a target
560 /// independent way (Note: the return type is ULong but the object is not
561 /// necessarily a ConstantUInt).
563 static Constant *getSizeOf(const Type *Ty);
565 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
566 /// return a pointer to a pointer of the array element type.
567 static Constant *getPtrPtrFromArrayPtr(Constant *C);
569 /// ConstantExpr::get - Return a binary or shift operator constant expression,
570 /// folding if possible.
572 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
574 /// ConstantExpr::get* - Return some common constants without having to
575 /// specify the full Instruction::OPCODE identifier.
577 static Constant *getNeg(Constant *C);
578 static Constant *getNot(Constant *C);
579 static Constant *getAdd(Constant *C1, Constant *C2);
580 static Constant *getSub(Constant *C1, Constant *C2);
581 static Constant *getMul(Constant *C1, Constant *C2);
582 static Constant *getDiv(Constant *C1, Constant *C2);
583 static Constant *getRem(Constant *C1, Constant *C2);
584 static Constant *getAnd(Constant *C1, Constant *C2);
585 static Constant *getOr(Constant *C1, Constant *C2);
586 static Constant *getXor(Constant *C1, Constant *C2);
587 static Constant *getSetEQ(Constant *C1, Constant *C2);
588 static Constant *getSetNE(Constant *C1, Constant *C2);
589 static Constant *getSetLT(Constant *C1, Constant *C2);
590 static Constant *getSetGT(Constant *C1, Constant *C2);
591 static Constant *getSetLE(Constant *C1, Constant *C2);
592 static Constant *getSetGE(Constant *C1, Constant *C2);
593 static Constant *getShl(Constant *C1, Constant *C2);
594 static Constant *getShr(Constant *C1, Constant *C2);
596 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
597 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
599 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
600 /// all elements must be Constant's.
602 static Constant *getGetElementPtr(Constant *C,
603 const std::vector<Constant*> &IdxList);
604 static Constant *getGetElementPtr(Constant *C,
605 const std::vector<Value*> &IdxList);
607 /// isNullValue - Return true if this is the value that would be returned by
609 virtual bool isNullValue() const { return false; }
611 /// getOpcode - Return the opcode at the root of this constant expression
612 unsigned getOpcode() const { return SubclassData; }
614 /// getOpcodeName - Return a string representation for an opcode.
615 const char *getOpcodeName() const;
617 virtual void destroyConstant();
618 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
619 bool DisableChecking = false);
621 /// Override methods to provide more type information...
622 inline Constant *getOperand(unsigned i) {
623 return cast<Constant>(User::getOperand(i));
625 inline Constant *getOperand(unsigned i) const {
626 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
630 /// Methods for support type inquiry through isa, cast, and dyn_cast:
631 static inline bool classof(const ConstantExpr *) { return true; }
632 static inline bool classof(const Value *V) {
633 return V->getValueType() == ConstantExprVal;
638 //===----------------------------------------------------------------------===//
639 /// UndefValue - 'undef' values are things that do not have specified contents.
640 /// These are used for a variety of purposes, including global variable
641 /// initializers and operands to instructions. 'undef' values can occur with
644 class UndefValue : public Constant {
645 friend struct ConstantCreator<UndefValue, Type, char>;
646 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
648 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
650 /// get() - Static factory methods - Return an 'undef' object of the specified
653 static UndefValue *get(const Type *T);
655 /// isNullValue - Return true if this is the value that would be returned by
657 virtual bool isNullValue() const { return false; }
659 virtual void destroyConstant();
661 /// Methods for support type inquiry through isa, cast, and dyn_cast:
662 static inline bool classof(const UndefValue *) { return true; }
663 static bool classof(const Value *V) {
664 return V->getValueType() == UndefValueVal;
668 } // End llvm namespace