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) {}
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);
355 /// get() - Static factory methods - Return objects of the specified value
356 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
357 static Constant *get(const std::string &Initializer);
359 /// getType - Specialize the getType() method to always return an ArrayType,
360 /// which reduces the amount of casting needed in parts of the compiler.
362 inline const ArrayType *getType() const {
363 return reinterpret_cast<const ArrayType*>(Value::getType());
366 /// isString - This method returns true if the array is an array of sbyte or
367 /// ubyte, and if the elements of the array are all ConstantInt's.
368 bool isString() const;
370 /// getAsString - If this array is isString(), then this method converts the
371 /// array to an std::string and returns it. Otherwise, it asserts out.
373 std::string getAsString() const;
375 /// isNullValue - Return true if this is the value that would be returned by
376 /// getNullValue. This always returns false because zero arrays are always
377 /// created as ConstantAggregateZero objects.
378 virtual bool isNullValue() const { return false; }
380 virtual void destroyConstant();
381 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
382 bool DisableChecking = false);
384 /// Methods for support type inquiry through isa, cast, and dyn_cast:
385 static inline bool classof(const ConstantArray *) { return true; }
386 static bool classof(const Value *V) {
387 return V->getValueType() == SimpleConstantVal &&
388 V->getType()->getTypeID() == Type::ArrayTyID;
393 //===----------------------------------------------------------------------===//
394 // ConstantStruct - Constant Struct Declarations
396 class ConstantStruct : public Constant {
397 friend struct ConstantCreator<ConstantStruct, StructType,
398 std::vector<Constant*> >;
399 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
401 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
403 /// get() - Static factory methods - Return objects of the specified value
405 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
406 static Constant *get(const std::vector<Constant*> &V);
408 /// getType() specialization - Reduce amount of casting...
410 inline const StructType *getType() const {
411 return reinterpret_cast<const StructType*>(Value::getType());
414 /// isNullValue - Return true if this is the value that would be returned by
415 /// getNullValue. This always returns false because zero structs are always
416 /// created as ConstantAggregateZero objects.
417 virtual bool isNullValue() const {
421 virtual void destroyConstant();
422 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
423 bool DisableChecking = false);
425 /// Methods for support type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const ConstantStruct *) { return true; }
427 static bool classof(const Value *V) {
428 return V->getValueType() == SimpleConstantVal &&
429 V->getType()->getTypeID() == Type::StructTyID;
433 //===----------------------------------------------------------------------===//
434 /// ConstantPacked - Constant Packed Declarations
436 class ConstantPacked : public Constant {
437 friend struct ConstantCreator<ConstantPacked, PackedType,
438 std::vector<Constant*> >;
439 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
441 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
443 /// get() - Static factory methods - Return objects of the specified value
444 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
445 static Constant *get(const std::vector<Constant*> &V);
447 /// getType - Specialize the getType() method to always return an PackedType,
448 /// which reduces the amount of casting needed in parts of the compiler.
450 inline const PackedType *getType() const {
451 return reinterpret_cast<const PackedType*>(Value::getType());
454 /// isNullValue - Return true if this is the value that would be returned by
455 /// getNullValue. This always returns false because zero arrays are always
456 /// created as ConstantAggregateZero objects.
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 ConstantPacked *) { return true; }
465 static bool classof(const Value *V) {
466 return V->getValueType() == SimpleConstantVal &&
467 V->getType()->getTypeID() == Type::PackedTyID;
471 //===----------------------------------------------------------------------===//
472 /// ConstantPointerNull - a constant pointer value that points to null
474 class ConstantPointerNull : public Constant {
475 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
476 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
478 ConstantPointerNull(const PointerType *T)
479 : Constant(reinterpret_cast<const Type*>(T)) {}
483 /// get() - Static factory methods - Return objects of the specified value
484 static ConstantPointerNull *get(const PointerType *T);
486 /// isNullValue - Return true if this is the value that would be returned by
488 virtual bool isNullValue() const { return true; }
490 virtual void destroyConstant();
492 /// getType - Specialize the getType() method to always return an PointerType,
493 /// which reduces the amount of casting needed in parts of the compiler.
495 inline const PointerType *getType() const {
496 return reinterpret_cast<const PointerType*>(Value::getType());
499 /// Methods for support type inquiry through isa, cast, and dyn_cast:
500 static inline bool classof(const ConstantPointerNull *) { return true; }
501 static bool classof(const Value *V) {
502 return V->getValueType() == SimpleConstantVal &&
503 isa<PointerType>(V->getType());
508 /// ConstantExpr - a constant value that is initialized with an expression using
509 /// other constant values. This is only used to represent values that cannot be
510 /// evaluated at compile-time (e.g., something derived from an address) because
511 /// it does not have a mechanism to store the actual value. Use the appropriate
512 /// Constant subclass above for known constants.
514 class ConstantExpr : public Constant {
515 unsigned iType; // Operation type (an Instruction opcode)
516 friend struct ConstantCreator<ConstantExpr,Type,
517 std::pair<unsigned, std::vector<Constant*> > >;
518 friend struct ConvertConstantType<ConstantExpr, Type>;
521 // Cast creation ctor
522 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
523 // Binary/Shift instruction creation ctor
524 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
525 // Select instruction creation ctor
526 ConstantExpr(Constant *C, Constant *V1, Constant *V2);
527 // GEP instruction creation ctor
528 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
531 // These private methods are used by the type resolution code to create
532 // ConstantExprs in intermediate forms.
533 static Constant *getTy(const Type *Ty, unsigned Opcode,
534 Constant *C1, Constant *C2);
535 static Constant *getShiftTy(const Type *Ty,
536 unsigned Opcode, Constant *C1, Constant *C2);
537 static Constant *getSelectTy(const Type *Ty,
538 Constant *C1, Constant *C2, Constant *C3);
539 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
540 const std::vector<Value*> &IdxList);
543 // Static methods to construct a ConstantExpr of different kinds. Note that
544 // these methods may return a object that is not an instance of the
545 // ConstantExpr class, because they will attempt to fold the constant
546 // expression into something simpler if possible.
548 /// Cast constant expr
550 static Constant *getCast(Constant *C, const Type *Ty);
551 static Constant *getSignExtend(Constant *C, const Type *Ty);
552 static Constant *getZeroExtend(Constant *C, const Type *Ty);
554 /// Select constant expr
556 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
557 return getSelectTy(V1->getType(), C, V1, V2);
560 /// getSizeOf constant expr - computes the size of a type in a target
561 /// independent way (Note: the return type is UInt but the object is not
562 /// necessarily a ConstantUInt).
564 static Constant *getSizeOf(const Type *Ty);
566 /// ConstantExpr::get - Return a binary or shift operator constant expression,
567 /// folding if possible.
569 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
571 /// ConstantExpr::get* - Return some common constants without having to
572 /// specify the full Instruction::OPCODE identifier.
574 static Constant *getNeg(Constant *C);
575 static Constant *getNot(Constant *C);
576 static Constant *getAdd(Constant *C1, Constant *C2);
577 static Constant *getSub(Constant *C1, Constant *C2);
578 static Constant *getMul(Constant *C1, Constant *C2);
579 static Constant *getDiv(Constant *C1, Constant *C2);
580 static Constant *getRem(Constant *C1, Constant *C2);
581 static Constant *getAnd(Constant *C1, Constant *C2);
582 static Constant *getOr(Constant *C1, Constant *C2);
583 static Constant *getXor(Constant *C1, Constant *C2);
584 static Constant *getSetEQ(Constant *C1, Constant *C2);
585 static Constant *getSetNE(Constant *C1, Constant *C2);
586 static Constant *getSetLT(Constant *C1, Constant *C2);
587 static Constant *getSetGT(Constant *C1, Constant *C2);
588 static Constant *getSetLE(Constant *C1, Constant *C2);
589 static Constant *getSetGE(Constant *C1, Constant *C2);
590 static Constant *getShl(Constant *C1, Constant *C2);
591 static Constant *getShr(Constant *C1, Constant *C2);
593 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
594 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
596 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
597 /// all elements must be Constant's.
599 static Constant *getGetElementPtr(Constant *C,
600 const std::vector<Constant*> &IdxList);
601 static Constant *getGetElementPtr(Constant *C,
602 const std::vector<Value*> &IdxList);
604 /// isNullValue - Return true if this is the value that would be returned by
606 virtual bool isNullValue() const { return false; }
608 /// getOpcode - Return the opcode at the root of this constant expression
609 unsigned getOpcode() const { return iType; }
611 /// getOpcodeName - Return a string representation for an opcode.
612 const char *getOpcodeName() const;
614 virtual void destroyConstant();
615 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
616 bool DisableChecking = false);
618 /// Override methods to provide more type information...
619 inline Constant *getOperand(unsigned i) {
620 return cast<Constant>(User::getOperand(i));
622 inline Constant *getOperand(unsigned i) const {
623 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
627 /// Methods for support type inquiry through isa, cast, and dyn_cast:
628 static inline bool classof(const ConstantExpr *) { return true; }
629 static inline bool classof(const Value *V) {
630 return V->getValueType() == ConstantExprVal;
635 //===----------------------------------------------------------------------===//
636 /// UndefValue - 'undef' values are things that do not have specified contents.
637 /// These are used for a variety of purposes, including global variable
638 /// initializers and operands to instructions. 'undef' values can occur with
641 class UndefValue : public Constant {
642 friend struct ConstantCreator<UndefValue, Type, char>;
643 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
645 UndefValue(const Type *T) : Constant(T, UndefValueVal) {}
647 /// get() - Static factory methods - Return an 'undef' object of the specified
650 static UndefValue *get(const Type *T);
652 /// isNullValue - Return true if this is the value that would be returned by
654 virtual bool isNullValue() const { return false; }
656 virtual void destroyConstant();
658 /// Methods for support type inquiry through isa, cast, and dyn_cast:
659 static inline bool classof(const UndefValue *) { return true; }
660 static bool classof(const Value *V) {
661 return V->getValueType() == UndefValueVal;
665 } // End llvm namespace