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, ValueTy VT, 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 /// getZExtValue - Return the constant zero extended as appropriate for this
60 inline uint64_t getZExtValue() const {
61 unsigned Size = getType()->getPrimitiveSizeInBits();
62 return Val.Unsigned & (~0ULL >> (64-Size));
65 /// getSExtValue - Return the constant sign extended as appropriate for this
67 inline int64_t getSExtValue() const {
68 unsigned Size = getType()->getPrimitiveSizeInBits();
69 return (Val.Signed << (64-Size)) >> (64-Size);
72 /// isNullValue - Return true if this is the value that would be returned by
75 virtual bool isNullValue() const = 0;
77 /// isMaxValue - Return true if this is the largest value that may be
78 /// represented by this type.
80 virtual bool isMaxValue() const = 0;
82 /// isMinValue - Return true if this is the smallest value that may be
83 /// represented by this type.
85 virtual bool isMinValue() const = 0;
87 /// isAllOnesValue - Return true if every bit in this constant is set to true.
89 virtual bool isAllOnesValue() const = 0;
91 /// Static constructor to get the maximum/minimum/allones constant of
92 /// specified (integral) type...
94 static ConstantIntegral *getMaxValue(const Type *Ty);
95 static ConstantIntegral *getMinValue(const Type *Ty);
96 static ConstantIntegral *getAllOnesValue(const Type *Ty);
98 /// Methods for support type inquiry through isa, cast, and dyn_cast:
99 static inline bool classof(const ConstantIntegral *) { return true; }
100 static bool classof(const Value *V) {
101 return V->getValueType() == ConstantBoolVal ||
102 V->getValueType() == ConstantSIntVal ||
103 V->getValueType() == ConstantUIntVal;
108 //===----------------------------------------------------------------------===//
109 /// ConstantBool - Boolean Values
111 class ConstantBool : public ConstantIntegral {
112 ConstantBool(bool V);
114 static ConstantBool *True, *False; // The True & False values
116 /// get() - Static factory methods - Return objects of the specified value
117 static ConstantBool *get(bool Value) { return Value ? True : False; }
118 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
120 /// inverted - Return the opposite value of the current value.
121 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
123 /// getValue - return the boolean value of this constant.
125 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
127 /// isNullValue - Return true if this is the value that would be returned by
130 virtual bool isNullValue() const { return this == False; }
131 virtual bool isMaxValue() const { return this == True; }
132 virtual bool isMinValue() const { return this == False; }
133 virtual bool isAllOnesValue() const { return this == True; }
135 /// Methods for support type inquiry through isa, cast, and dyn_cast:
136 static inline bool classof(const ConstantBool *) { return true; }
137 static bool classof(const Value *V) {
138 return V->getValueType() == ConstantBoolVal;
143 //===----------------------------------------------------------------------===//
144 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
145 /// with integral constants easier.
147 class ConstantInt : public ConstantIntegral {
149 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
150 ConstantInt(const Type *Ty, ValueTy VT, uint64_t V);
152 /// equalsInt - Provide a helper method that can be used to determine if the
153 /// constant contained within is equal to a constant. This only works for
154 /// very small values, because this is all that can be represented with all
157 bool equalsInt(unsigned char V) const {
159 "equalsInt: Can only be used with very small positive constants!");
160 return Val.Unsigned == V;
163 /// ConstantInt::get static method: return a ConstantInt with the specified
164 /// value. as above, we work only with very small values here.
166 static ConstantInt *get(const Type *Ty, unsigned char V);
168 /// isNullValue - Return true if this is the value that would be returned by
170 virtual bool isNullValue() const { return Val.Unsigned == 0; }
171 virtual bool isMaxValue() const = 0;
172 virtual bool isMinValue() const = 0;
174 /// Methods for support type inquiry through isa, cast, and dyn_cast:
175 static inline bool classof(const ConstantInt *) { return true; }
176 static bool classof(const Value *V) {
177 return V->getValueType() == ConstantSIntVal ||
178 V->getValueType() == ConstantUIntVal;
183 //===----------------------------------------------------------------------===//
184 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
186 class ConstantSInt : public ConstantInt {
187 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
188 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
191 ConstantSInt(const Type *Ty, int64_t V);
193 /// get() - Static factory methods - Return objects of the specified value
195 static ConstantSInt *get(const Type *Ty, int64_t V);
197 /// isValueValidForType - return true if Ty is big enough to represent V.
199 static bool isValueValidForType(const Type *Ty, int64_t V);
201 /// getValue - return the underlying value of this constant.
203 inline int64_t getValue() const { return Val.Signed; }
205 virtual bool isAllOnesValue() const { return getValue() == -1; }
207 /// isMaxValue - Return true if this is the largest value that may be
208 /// represented by this type.
210 virtual bool isMaxValue() const {
211 int64_t V = getValue();
212 if (V < 0) return false; // Be careful about wrap-around on 'long's
214 return !isValueValidForType(getType(), V) || V < 0;
217 /// isMinValue - Return true if this is the smallest value that may be
218 /// represented by this type.
220 virtual bool isMinValue() const {
221 int64_t V = getValue();
222 if (V > 0) return false; // Be careful about wrap-around on 'long's
224 return !isValueValidForType(getType(), V) || V > 0;
227 /// Methods for support type inquiry through isa, cast, and dyn_cast:
229 static inline bool classof(const ConstantSInt *) { return true; }
230 static bool classof(const Value *V) {
231 return V->getValueType() == ConstantSIntVal;
235 //===----------------------------------------------------------------------===//
236 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
238 class ConstantUInt : public ConstantInt {
239 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
240 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
242 ConstantUInt(const Type *Ty, uint64_t V);
244 /// get() - Static factory methods - Return objects of the specified value
246 static ConstantUInt *get(const Type *Ty, uint64_t V);
248 /// isValueValidForType - return true if Ty is big enough to represent V.
250 static bool isValueValidForType(const Type *Ty, uint64_t V);
252 /// getValue - return the underlying value of this constant.
254 inline uint64_t getValue() const { return Val.Unsigned; }
256 /// isMaxValue - Return true if this is the largest value that may be
257 /// represented by this type.
259 virtual bool isAllOnesValue() const;
260 virtual bool isMaxValue() const { return isAllOnesValue(); }
261 virtual bool isMinValue() const { return getValue() == 0; }
263 /// Methods for support type inquiry through isa, cast, and dyn_cast:
264 static inline bool classof(const ConstantUInt *) { return true; }
265 static bool classof(const Value *V) {
266 return V->getValueType() == ConstantUIntVal;
271 //===----------------------------------------------------------------------===//
272 /// ConstantFP - Floating Point Values [float, double]
274 class ConstantFP : public Constant {
276 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
277 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
278 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
280 ConstantFP(const Type *Ty, double V);
282 /// get() - Static factory methods - Return objects of the specified value
283 static ConstantFP *get(const Type *Ty, double V);
285 /// isValueValidForType - return true if Ty is big enough to represent V.
286 static bool isValueValidForType(const Type *Ty, double V);
287 inline double getValue() const { return Val; }
289 /// isNullValue - Return true if this is the value that would be returned by
290 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
291 /// considers -0.0 to be null as well as 0.0. :(
292 virtual bool isNullValue() const;
294 /// isExactlyValue - We don't rely on operator== working on double values, as
295 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
296 /// As such, this method can be used to do an exact bit-for-bit comparison of
297 /// two floating point values.
298 bool isExactlyValue(double V) const;
300 /// Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool classof(const ConstantFP *) { return true; }
302 static bool classof(const Value *V) {
303 return V->getValueType() == ConstantFPVal;
307 //===----------------------------------------------------------------------===//
308 /// ConstantAggregateZero - All zero aggregate value
310 class ConstantAggregateZero : public Constant {
311 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
312 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
314 ConstantAggregateZero(const Type *Ty)
315 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
317 /// get() - static factory method for creating a null aggregate. It is
318 /// illegal to call this method with a non-aggregate type.
319 static Constant *get(const Type *Ty);
321 /// isNullValue - Return true if this is the value that would be returned by
323 virtual bool isNullValue() const { return true; }
325 virtual void destroyConstant();
327 /// Methods for support type inquiry through isa, cast, and dyn_cast:
329 static bool classof(const ConstantAggregateZero *) { return true; }
330 static bool classof(const Value *V) {
331 return V->getValueType() == ConstantAggregateZeroVal;
336 //===----------------------------------------------------------------------===//
337 /// ConstantArray - Constant Array Declarations
339 class ConstantArray : public Constant {
340 friend struct ConstantCreator<ConstantArray, ArrayType,
341 std::vector<Constant*> >;
342 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
344 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
347 /// get() - Static factory methods - Return objects of the specified value
348 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
349 static Constant *get(const std::string &Initializer);
351 /// getType - Specialize the getType() method to always return an ArrayType,
352 /// which reduces the amount of casting needed in parts of the compiler.
354 inline const ArrayType *getType() const {
355 return reinterpret_cast<const ArrayType*>(Value::getType());
358 /// isString - This method returns true if the array is an array of sbyte or
359 /// ubyte, and if the elements of the array are all ConstantInt's.
360 bool isString() const;
362 /// getAsString - If this array is isString(), then this method converts the
363 /// array to an std::string and returns it. Otherwise, it asserts out.
365 std::string getAsString() const;
367 /// isNullValue - Return true if this is the value that would be returned by
368 /// getNullValue. This always returns false because zero arrays are always
369 /// created as ConstantAggregateZero objects.
370 virtual bool isNullValue() const { return false; }
372 virtual void destroyConstant();
373 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
375 /// Methods for support type inquiry through isa, cast, and dyn_cast:
376 static inline bool classof(const ConstantArray *) { return true; }
377 static bool classof(const Value *V) {
378 return V->getValueType() == ConstantArrayVal;
383 //===----------------------------------------------------------------------===//
384 // ConstantStruct - Constant Struct Declarations
386 class ConstantStruct : public Constant {
387 friend struct ConstantCreator<ConstantStruct, StructType,
388 std::vector<Constant*> >;
389 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
391 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
394 /// get() - Static factory methods - Return objects of the specified value
396 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
397 static Constant *get(const std::vector<Constant*> &V);
399 /// getType() specialization - Reduce amount of casting...
401 inline const StructType *getType() const {
402 return reinterpret_cast<const StructType*>(Value::getType());
405 /// isNullValue - Return true if this is the value that would be returned by
406 /// getNullValue. This always returns false because zero structs are always
407 /// created as ConstantAggregateZero objects.
408 virtual bool isNullValue() const {
412 virtual void destroyConstant();
413 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
415 /// Methods for support type inquiry through isa, cast, and dyn_cast:
416 static inline bool classof(const ConstantStruct *) { return true; }
417 static bool classof(const Value *V) {
418 return V->getValueType() == ConstantStructVal;
422 //===----------------------------------------------------------------------===//
423 /// ConstantPacked - Constant Packed Declarations
425 class ConstantPacked : public Constant {
426 friend struct ConstantCreator<ConstantPacked, PackedType,
427 std::vector<Constant*> >;
428 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
430 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
433 /// get() - Static factory methods - Return objects of the specified value
434 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
435 static Constant *get(const std::vector<Constant*> &V);
437 /// getType - Specialize the getType() method to always return an PackedType,
438 /// which reduces the amount of casting needed in parts of the compiler.
440 inline const PackedType *getType() const {
441 return reinterpret_cast<const PackedType*>(Value::getType());
444 /// isNullValue - Return true if this is the value that would be returned by
445 /// getNullValue. This always returns false because zero arrays are always
446 /// created as ConstantAggregateZero objects.
447 virtual bool isNullValue() const { return false; }
449 virtual void destroyConstant();
450 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
452 /// Methods for support type inquiry through isa, cast, and dyn_cast:
453 static inline bool classof(const ConstantPacked *) { return true; }
454 static bool classof(const Value *V) {
455 return V->getValueType() == ConstantPackedVal;
459 //===----------------------------------------------------------------------===//
460 /// ConstantPointerNull - a constant pointer value that points to null
462 class ConstantPointerNull : public Constant {
463 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
464 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
466 ConstantPointerNull(const PointerType *T)
467 : Constant(reinterpret_cast<const Type*>(T),
468 Value::ConstantPointerNullVal, 0, 0) {}
472 /// get() - Static factory methods - Return objects of the specified value
473 static ConstantPointerNull *get(const PointerType *T);
475 /// isNullValue - Return true if this is the value that would be returned by
477 virtual bool isNullValue() const { return true; }
479 virtual void destroyConstant();
481 /// getType - Specialize the getType() method to always return an PointerType,
482 /// which reduces the amount of casting needed in parts of the compiler.
484 inline const PointerType *getType() const {
485 return reinterpret_cast<const PointerType*>(Value::getType());
488 /// Methods for support type inquiry through isa, cast, and dyn_cast:
489 static inline bool classof(const ConstantPointerNull *) { return true; }
490 static bool classof(const Value *V) {
491 return V->getValueType() == ConstantPointerNullVal;
496 /// ConstantExpr - a constant value that is initialized with an expression using
497 /// other constant values.
499 /// This class uses the standard Instruction opcodes to define the various
500 /// constant expressions. The Opcode field for the ConstantExpr class is
501 /// maintained in the Value::SubclassData field.
502 class ConstantExpr : public Constant {
503 friend struct ConstantCreator<ConstantExpr,Type,
504 std::pair<unsigned, std::vector<Constant*> > >;
505 friend struct ConvertConstantType<ConstantExpr, Type>;
508 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
509 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
510 // Operation type (an Instruction opcode) is stored as the SubclassData.
511 SubclassData = Opcode;
514 // These private methods are used by the type resolution code to create
515 // ConstantExprs in intermediate forms.
516 static Constant *getTy(const Type *Ty, unsigned Opcode,
517 Constant *C1, Constant *C2);
518 static Constant *getShiftTy(const Type *Ty,
519 unsigned Opcode, Constant *C1, Constant *C2);
520 static Constant *getSelectTy(const Type *Ty,
521 Constant *C1, Constant *C2, Constant *C3);
522 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
523 const std::vector<Value*> &IdxList);
524 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
528 // Static methods to construct a ConstantExpr of different kinds. Note that
529 // these methods may return a object that is not an instance of the
530 // ConstantExpr class, because they will attempt to fold the constant
531 // expression into something simpler if possible.
533 /// Cast constant expr
535 static Constant *getCast(Constant *C, const Type *Ty);
536 static Constant *getSignExtend(Constant *C, const Type *Ty);
537 static Constant *getZeroExtend(Constant *C, const Type *Ty);
539 /// Select constant expr
541 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
542 return getSelectTy(V1->getType(), C, V1, V2);
545 /// getSizeOf constant expr - computes the size of a type in a target
546 /// independent way (Note: the return type is ULong but the object is not
547 /// necessarily a ConstantUInt).
549 static Constant *getSizeOf(const Type *Ty);
551 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
552 /// return a pointer to a pointer of the array element type.
553 static Constant *getPtrPtrFromArrayPtr(Constant *C);
555 /// ConstantExpr::get - Return a binary or shift operator constant expression,
556 /// folding if possible.
558 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
560 /// ConstantExpr::get* - Return some common constants without having to
561 /// specify the full Instruction::OPCODE identifier.
563 static Constant *getNeg(Constant *C);
564 static Constant *getNot(Constant *C);
565 static Constant *getAdd(Constant *C1, Constant *C2);
566 static Constant *getSub(Constant *C1, Constant *C2);
567 static Constant *getMul(Constant *C1, Constant *C2);
568 static Constant *getDiv(Constant *C1, Constant *C2);
569 static Constant *getRem(Constant *C1, Constant *C2);
570 static Constant *getAnd(Constant *C1, Constant *C2);
571 static Constant *getOr(Constant *C1, Constant *C2);
572 static Constant *getXor(Constant *C1, Constant *C2);
573 static Constant *getSetEQ(Constant *C1, Constant *C2);
574 static Constant *getSetNE(Constant *C1, Constant *C2);
575 static Constant *getSetLT(Constant *C1, Constant *C2);
576 static Constant *getSetGT(Constant *C1, Constant *C2);
577 static Constant *getSetLE(Constant *C1, Constant *C2);
578 static Constant *getSetGE(Constant *C1, Constant *C2);
579 static Constant *getShl(Constant *C1, Constant *C2);
580 static Constant *getShr(Constant *C1, Constant *C2);
582 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
583 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
585 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
586 /// all elements must be Constant's.
588 static Constant *getGetElementPtr(Constant *C,
589 const std::vector<Constant*> &IdxList);
590 static Constant *getGetElementPtr(Constant *C,
591 const std::vector<Value*> &IdxList);
593 /// Extractelement form.
595 static Constant *getExtractElement(Constant *Val, Constant *Idx);
597 /// isNullValue - Return true if this is the value that would be returned by
599 virtual bool isNullValue() const { return false; }
601 /// getOpcode - Return the opcode at the root of this constant expression
602 unsigned getOpcode() const { return SubclassData; }
604 /// getOpcodeName - Return a string representation for an opcode.
605 const char *getOpcodeName() const;
607 virtual void destroyConstant();
608 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
610 /// Override methods to provide more type information...
611 inline Constant *getOperand(unsigned i) {
612 return cast<Constant>(User::getOperand(i));
614 inline Constant *getOperand(unsigned i) const {
615 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
619 /// Methods for support type inquiry through isa, cast, and dyn_cast:
620 static inline bool classof(const ConstantExpr *) { return true; }
621 static inline bool classof(const Value *V) {
622 return V->getValueType() == ConstantExprVal;
627 //===----------------------------------------------------------------------===//
628 /// UndefValue - 'undef' values are things that do not have specified contents.
629 /// These are used for a variety of purposes, including global variable
630 /// initializers and operands to instructions. 'undef' values can occur with
633 class UndefValue : public Constant {
634 friend struct ConstantCreator<UndefValue, Type, char>;
635 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
637 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
639 /// get() - Static factory methods - Return an 'undef' object of the specified
642 static UndefValue *get(const Type *T);
644 /// isNullValue - Return true if this is the value that would be returned by
646 virtual bool isNullValue() const { return false; }
648 virtual void destroyConstant();
650 /// Methods for support type inquiry through isa, cast, and dyn_cast:
651 static inline bool classof(const UndefValue *) { return true; }
652 static bool classof(const Value *V) {
653 return V->getValueType() == UndefValueVal;
657 } // End llvm namespace