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();
326 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
327 bool DisableChecking = false);
329 /// Methods for support type inquiry through isa, cast, and dyn_cast:
331 static bool classof(const ConstantAggregateZero *) { return true; }
332 static bool classof(const Value *V) {
333 return V->getValueType() == ConstantAggregateZeroVal;
338 //===----------------------------------------------------------------------===//
339 /// ConstantArray - Constant Array Declarations
341 class ConstantArray : public Constant {
342 friend struct ConstantCreator<ConstantArray, ArrayType,
343 std::vector<Constant*> >;
344 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
346 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
349 /// get() - Static factory methods - Return objects of the specified value
350 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
351 static Constant *get(const std::string &Initializer);
353 /// getType - Specialize the getType() method to always return an ArrayType,
354 /// which reduces the amount of casting needed in parts of the compiler.
356 inline const ArrayType *getType() const {
357 return reinterpret_cast<const ArrayType*>(Value::getType());
360 /// isString - This method returns true if the array is an array of sbyte or
361 /// ubyte, and if the elements of the array are all ConstantInt's.
362 bool isString() const;
364 /// getAsString - If this array is isString(), then this method converts the
365 /// array to an std::string and returns it. Otherwise, it asserts out.
367 std::string getAsString() const;
369 /// isNullValue - Return true if this is the value that would be returned by
370 /// getNullValue. This always returns false because zero arrays are always
371 /// created as ConstantAggregateZero objects.
372 virtual bool isNullValue() const { return false; }
374 virtual void destroyConstant();
375 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
376 bool DisableChecking = false);
378 /// Methods for support type inquiry through isa, cast, and dyn_cast:
379 static inline bool classof(const ConstantArray *) { return true; }
380 static bool classof(const Value *V) {
381 return V->getValueType() == ConstantArrayVal;
386 //===----------------------------------------------------------------------===//
387 // ConstantStruct - Constant Struct Declarations
389 class ConstantStruct : public Constant {
390 friend struct ConstantCreator<ConstantStruct, StructType,
391 std::vector<Constant*> >;
392 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
394 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
397 /// get() - Static factory methods - Return objects of the specified value
399 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
400 static Constant *get(const std::vector<Constant*> &V);
402 /// getType() specialization - Reduce amount of casting...
404 inline const StructType *getType() const {
405 return reinterpret_cast<const StructType*>(Value::getType());
408 /// isNullValue - Return true if this is the value that would be returned by
409 /// getNullValue. This always returns false because zero structs are always
410 /// created as ConstantAggregateZero objects.
411 virtual bool isNullValue() const {
415 virtual void destroyConstant();
416 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
417 bool DisableChecking = false);
419 /// Methods for support type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const ConstantStruct *) { return true; }
421 static bool classof(const Value *V) {
422 return V->getValueType() == ConstantStructVal;
426 //===----------------------------------------------------------------------===//
427 /// ConstantPacked - Constant Packed Declarations
429 class ConstantPacked : public Constant {
430 friend struct ConstantCreator<ConstantPacked, PackedType,
431 std::vector<Constant*> >;
432 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
434 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
437 /// get() - Static factory methods - Return objects of the specified value
438 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
439 static Constant *get(const std::vector<Constant*> &V);
441 /// getType - Specialize the getType() method to always return an PackedType,
442 /// which reduces the amount of casting needed in parts of the compiler.
444 inline const PackedType *getType() const {
445 return reinterpret_cast<const PackedType*>(Value::getType());
448 /// isNullValue - Return true if this is the value that would be returned by
449 /// getNullValue. This always returns false because zero arrays are always
450 /// created as ConstantAggregateZero objects.
451 virtual bool isNullValue() const { return false; }
453 virtual void destroyConstant();
454 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
455 bool DisableChecking = false);
457 /// Methods for support type inquiry through isa, cast, and dyn_cast:
458 static inline bool classof(const ConstantPacked *) { return true; }
459 static bool classof(const Value *V) {
460 return V->getValueType() == ConstantPackedVal;
464 //===----------------------------------------------------------------------===//
465 /// ConstantPointerNull - a constant pointer value that points to null
467 class ConstantPointerNull : public Constant {
468 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
469 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
471 ConstantPointerNull(const PointerType *T)
472 : Constant(reinterpret_cast<const Type*>(T),
473 Value::ConstantPointerNullVal, 0, 0) {}
477 /// get() - Static factory methods - Return objects of the specified value
478 static ConstantPointerNull *get(const PointerType *T);
480 /// isNullValue - Return true if this is the value that would be returned by
482 virtual bool isNullValue() const { return true; }
484 virtual void destroyConstant();
486 /// getType - Specialize the getType() method to always return an PointerType,
487 /// which reduces the amount of casting needed in parts of the compiler.
489 inline const PointerType *getType() const {
490 return reinterpret_cast<const PointerType*>(Value::getType());
493 /// Methods for support type inquiry through isa, cast, and dyn_cast:
494 static inline bool classof(const ConstantPointerNull *) { return true; }
495 static bool classof(const Value *V) {
496 return V->getValueType() == ConstantPointerNullVal;
501 /// ConstantExpr - a constant value that is initialized with an expression using
502 /// other constant values.
504 /// This class uses the standard Instruction opcodes to define the various
505 /// constant expressions. The Opcode field for the ConstantExpr class is
506 /// maintained in the Value::SubclassData field.
507 class ConstantExpr : public Constant {
508 friend struct ConstantCreator<ConstantExpr,Type,
509 std::pair<unsigned, std::vector<Constant*> > >;
510 friend struct ConvertConstantType<ConstantExpr, Type>;
513 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
514 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
515 // Operation type (an Instruction opcode) is stored as the SubclassData.
516 SubclassData = Opcode;
519 // These private methods are used by the type resolution code to create
520 // ConstantExprs in intermediate forms.
521 static Constant *getTy(const Type *Ty, unsigned Opcode,
522 Constant *C1, Constant *C2);
523 static Constant *getShiftTy(const Type *Ty,
524 unsigned Opcode, Constant *C1, Constant *C2);
525 static Constant *getSelectTy(const Type *Ty,
526 Constant *C1, Constant *C2, Constant *C3);
527 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
528 const std::vector<Value*> &IdxList);
531 // Static methods to construct a ConstantExpr of different kinds. Note that
532 // these methods may return a object that is not an instance of the
533 // ConstantExpr class, because they will attempt to fold the constant
534 // expression into something simpler if possible.
536 /// Cast constant expr
538 static Constant *getCast(Constant *C, const Type *Ty);
539 static Constant *getSignExtend(Constant *C, const Type *Ty);
540 static Constant *getZeroExtend(Constant *C, const Type *Ty);
542 /// Select constant expr
544 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
545 return getSelectTy(V1->getType(), C, V1, V2);
548 /// getSizeOf constant expr - computes the size of a type in a target
549 /// independent way (Note: the return type is ULong but the object is not
550 /// necessarily a ConstantUInt).
552 static Constant *getSizeOf(const Type *Ty);
554 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
555 /// return a pointer to a pointer of the array element type.
556 static Constant *getPtrPtrFromArrayPtr(Constant *C);
558 /// ConstantExpr::get - Return a binary or shift operator constant expression,
559 /// folding if possible.
561 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
563 /// ConstantExpr::get* - Return some common constants without having to
564 /// specify the full Instruction::OPCODE identifier.
566 static Constant *getNeg(Constant *C);
567 static Constant *getNot(Constant *C);
568 static Constant *getAdd(Constant *C1, Constant *C2);
569 static Constant *getSub(Constant *C1, Constant *C2);
570 static Constant *getMul(Constant *C1, Constant *C2);
571 static Constant *getDiv(Constant *C1, Constant *C2);
572 static Constant *getRem(Constant *C1, Constant *C2);
573 static Constant *getAnd(Constant *C1, Constant *C2);
574 static Constant *getOr(Constant *C1, Constant *C2);
575 static Constant *getXor(Constant *C1, Constant *C2);
576 static Constant *getSetEQ(Constant *C1, Constant *C2);
577 static Constant *getSetNE(Constant *C1, Constant *C2);
578 static Constant *getSetLT(Constant *C1, Constant *C2);
579 static Constant *getSetGT(Constant *C1, Constant *C2);
580 static Constant *getSetLE(Constant *C1, Constant *C2);
581 static Constant *getSetGE(Constant *C1, Constant *C2);
582 static Constant *getShl(Constant *C1, Constant *C2);
583 static Constant *getShr(Constant *C1, Constant *C2);
585 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
586 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
588 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
589 /// all elements must be Constant's.
591 static Constant *getGetElementPtr(Constant *C,
592 const std::vector<Constant*> &IdxList);
593 static Constant *getGetElementPtr(Constant *C,
594 const std::vector<Value*> &IdxList);
596 /// isNullValue - Return true if this is the value that would be returned by
598 virtual bool isNullValue() const { return false; }
600 /// getOpcode - Return the opcode at the root of this constant expression
601 unsigned getOpcode() const { return SubclassData; }
603 /// getOpcodeName - Return a string representation for an opcode.
604 const char *getOpcodeName() const;
606 virtual void destroyConstant();
607 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
608 bool DisableChecking = false);
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