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 /// @file This file contains the declarations for the subclasses of Constant,
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are
13 /// fully 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"
33 template<class ConstantClass, class TypeClass, class ValType>
34 struct ConstantCreator;
35 template<class ConstantClass, class TypeClass>
36 struct ConvertConstantType;
38 //===----------------------------------------------------------------------===//
39 /// This is the shared superclass of boolean and integer constants. This class
40 /// just defines some common interfaces to be implemented by the subclasses.
41 /// @brief An abstract class for integer constants.
42 class ConstantIntegral : public Constant {
45 ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V);
47 /// Return the constant as a 64-bit unsigned integer value after it
48 /// has been zero extended as appropriate for the type of this constant.
49 /// @brief Return the zero extended value.
50 inline uint64_t getZExtValue() const {
54 /// Return the constant as a 64-bit integer value after it has been sign
55 /// sign extended as appropriate for the type of this constant.
56 /// @brief Return the sign extended value.
57 inline int64_t getSExtValue() const {
58 unsigned Size = getType()->getPrimitiveSizeInBits();
59 return (int64_t(Val) << (64-Size)) >> (64-Size);
62 /// This function is implemented by subclasses and will return true iff this
63 /// constant represents the the "null" value that would be returned by the
64 /// getNullValue method.
65 /// @returns true if the constant's value is 0.
66 /// @brief Determine if the value is null.
67 virtual bool isNullValue() const = 0;
69 /// This function is implemented by sublcasses and will return true iff this
70 /// constant represents the the largest value that may be represented by this
72 /// @returns true if the constant's value is maximal.
73 /// @brief Determine if the value is maximal.
74 virtual bool isMaxValue() const = 0;
76 /// This function is implemented by subclasses and will return true iff this
77 /// constant represents the smallest value that may be represented by this
79 /// @returns true if the constant's value is minimal
80 /// @brief Determine if the value is minimal.
81 virtual bool isMinValue() const = 0;
83 /// This function is implemented by subclasses and will return true iff every
84 /// bit in this constant is set to true.
85 /// @returns true if all bits of the constant are ones.
86 /// @brief Determine if the value is all ones.
87 virtual bool isAllOnesValue() const = 0;
89 /// @returns the largest value for an integer constant of the given type
90 /// @brief Get the maximal value
91 static ConstantIntegral *getMaxValue(const Type *Ty);
93 /// @returns the smallest value for an integer constant of the given type
94 /// @brief Get the minimal value
95 static ConstantIntegral *getMinValue(const Type *Ty);
97 /// @returns the value for an integer constant of the given type that has all
98 /// its bits set to true.
99 /// @brief Get the all ones value
100 static ConstantIntegral *getAllOnesValue(const Type *Ty);
102 /// Methods to support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const ConstantIntegral *) { return true; }
104 static bool classof(const Value *V) {
105 return V->getValueType() == ConstantBoolVal ||
106 V->getValueType() == ConstantIntVal;
111 //===----------------------------------------------------------------------===//
112 /// This concrete class represents constant values of type BoolTy. There are
113 /// only two instances of this class constructed: the True and False static
114 /// members. The constructor is hidden to ensure this invariant.
115 /// @brief Constant Boolean class
116 class ConstantBool : public ConstantIntegral {
117 ConstantBool(bool V);
119 /// getTrue/getFalse - Return the singleton true/false values.
120 static ConstantBool *getTrue();
121 static ConstantBool *getFalse();
123 /// This method is provided mostly for compatibility with the other
124 /// ConstantIntegral subclasses.
125 /// @brief Static factory method for getting a ConstantBool instance.
126 static ConstantBool *get(bool Value) { return Value ? getTrue() : getFalse();}
128 /// This method is provided mostly for compatibility with the other
129 /// ConstantIntegral subclasses.
130 /// @brief Static factory method for getting a ConstantBool instance.
131 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
133 /// Returns the opposite value of this ConstantBool value.
134 /// @brief Get inverse value.
135 inline ConstantBool *inverted() const {
136 return getValue() ? getFalse() : getTrue();
139 /// @returns the value of this ConstantBool
140 /// @brief return the boolean value of this constant.
141 inline bool getValue() const { return static_cast<bool>(getZExtValue()); }
143 /// @see ConstantIntegral for details
144 /// @brief Implement overrides
145 virtual bool isNullValue() const { return getValue() == false; }
146 virtual bool isMaxValue() const { return getValue() == true; }
147 virtual bool isMinValue() const { return getValue() == false; }
148 virtual bool isAllOnesValue() const { return getValue() == true; }
150 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast:
151 static inline bool classof(const ConstantBool *) { return true; }
152 static bool classof(const Value *V) {
153 return V->getValueType() == ConstantBoolVal;
158 //===----------------------------------------------------------------------===//
159 /// This is concrete integer subclass of ConstantIntegral that represents
160 /// both signed and unsigned integral constants, other than boolean.
161 /// @brief Class for constant integers.
162 class ConstantInt : public ConstantIntegral {
164 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
165 ConstantInt(const Type *Ty, uint64_t V);
166 ConstantInt(const Type *Ty, int64_t V);
167 friend struct ConstantCreator<ConstantInt, Type, uint64_t>;
169 /// A helper method that can be used to determine if the constant contained
170 /// within is equal to a constant. This only works for very small values,
171 /// because this is all that can be represented with all types.
172 /// @brief Determine if this constant's value is same as an unsigned char.
173 bool equalsInt(unsigned char V) const {
175 "equalsInt: Can only be used with very small positive constants!");
179 /// Return a ConstantInt with the specified value for the specified type. The
180 /// value V will be canonicalized to a uint64_t but accessing it with either
181 /// getSExtValue() or getZExtValue() (ConstantIntegral) will yield the correct
182 /// sized/signed value for the type Ty.
183 /// @brief Get a ConstantInt for a specific value.
184 static ConstantInt *get(const Type *Ty, int64_t V);
186 /// This static method returns true if the type Ty is big enough to
187 /// represent the value V. This can be used to avoid having the get method
188 /// assert when V is larger than Ty can represent.
189 /// @returns true if V is a valid value for type Ty
190 /// @brief Determine if the value is in range for the given type.
191 static bool isValueValidForType(const Type *Ty, int64_t V);
193 /// @returns true if this is the null integer value.
194 /// @see ConstantIntegral for details
195 /// @brief Implement override.
196 virtual bool isNullValue() const { return Val == 0; }
198 /// @returns true iff this constant's bits are all set to true.
199 /// @see ConstantIntegral
200 /// @brief Override implementation
201 virtual bool isAllOnesValue() const { return getSExtValue() == -1; }
203 /// @returns true iff this is the largest value that may be represented
205 /// @see ConstantIntegeral
206 /// @brief Override implementation
207 virtual bool isMaxValue() const {
208 if (getType()->isSigned()) {
209 int64_t V = getSExtValue();
210 if (V < 0) return false; // Be careful about wrap-around on 'long's
212 return !isValueValidForType(getType(), V) || V < 0;
214 return isAllOnesValue();
217 /// @returns true if this is the smallest value that may be represented by
219 /// @see ConstantIntegral
220 /// @brief Override implementation
221 virtual bool isMinValue() const {
222 if (getType()->isSigned()) {
223 int64_t V = getSExtValue();
224 if (V > 0) return false; // Be careful about wrap-around on 'long's
226 return !isValueValidForType(getType(), V) || V > 0;
228 return getZExtValue() == 0;
231 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
232 static inline bool classof(const ConstantInt *) { return true; }
233 static bool classof(const Value *V) {
234 return V->getValueType() == ConstantIntVal;
239 //===----------------------------------------------------------------------===//
240 /// ConstantFP - Floating Point Values [float, double]
242 class ConstantFP : public Constant {
244 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
245 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
246 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
248 ConstantFP(const Type *Ty, double V);
250 /// get() - Static factory methods - Return objects of the specified value
251 static ConstantFP *get(const Type *Ty, double V);
253 /// isValueValidForType - return true if Ty is big enough to represent V.
254 static bool isValueValidForType(const Type *Ty, double V);
255 inline double getValue() const { return Val; }
257 /// isNullValue - Return true if this is the value that would be returned by
258 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
259 /// considers -0.0 to be null as well as 0.0. :(
260 virtual bool isNullValue() const;
262 /// isExactlyValue - We don't rely on operator== working on double values, as
263 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
264 /// As such, this method can be used to do an exact bit-for-bit comparison of
265 /// two floating point values.
266 bool isExactlyValue(double V) const;
268 /// Methods for support type inquiry through isa, cast, and dyn_cast:
269 static inline bool classof(const ConstantFP *) { return true; }
270 static bool classof(const Value *V) {
271 return V->getValueType() == ConstantFPVal;
275 //===----------------------------------------------------------------------===//
276 /// ConstantAggregateZero - All zero aggregate value
278 class ConstantAggregateZero : public Constant {
279 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
280 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
282 ConstantAggregateZero(const Type *Ty)
283 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
285 /// get() - static factory method for creating a null aggregate. It is
286 /// illegal to call this method with a non-aggregate type.
287 static Constant *get(const Type *Ty);
289 /// isNullValue - Return true if this is the value that would be returned by
291 virtual bool isNullValue() const { return true; }
293 virtual void destroyConstant();
295 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static bool classof(const ConstantAggregateZero *) { return true; }
298 static bool classof(const Value *V) {
299 return V->getValueType() == ConstantAggregateZeroVal;
304 //===----------------------------------------------------------------------===//
305 /// ConstantArray - Constant Array Declarations
307 class ConstantArray : public Constant {
308 friend struct ConstantCreator<ConstantArray, ArrayType,
309 std::vector<Constant*> >;
310 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
312 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
315 /// get() - Static factory methods - Return objects of the specified value
316 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
318 /// This method constructs a ConstantArray and initializes it with a text
319 /// string. The default behavior (AddNull==true) causes a null terminator to
320 /// be placed at the end of the array. This effectively increases the length
321 /// of the array by one (you've been warned). However, in some situations
322 /// this is not desired so if AddNull==false then the string is copied without
323 /// null termination.
324 static Constant *get(const std::string &Initializer, bool AddNull = true);
326 /// getType - Specialize the getType() method to always return an ArrayType,
327 /// which reduces the amount of casting needed in parts of the compiler.
329 inline const ArrayType *getType() const {
330 return reinterpret_cast<const ArrayType*>(Value::getType());
333 /// isString - This method returns true if the array is an array of sbyte or
334 /// ubyte, and if the elements of the array are all ConstantInt's.
335 bool isString() const;
337 /// isCString - This method returns true if the array is a string (see
338 /// isString) and it ends in a null byte \0 and does not contains any other
339 /// null bytes except its terminator.
340 bool isCString() const;
342 /// getAsString - If this array is isString(), then this method converts the
343 /// array to an std::string and returns it. Otherwise, it asserts out.
345 std::string getAsString() const;
347 /// isNullValue - Return true if this is the value that would be returned by
348 /// getNullValue. This always returns false because zero arrays are always
349 /// created as ConstantAggregateZero objects.
350 virtual bool isNullValue() const { return false; }
352 virtual void destroyConstant();
353 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
355 /// Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const ConstantArray *) { return true; }
357 static bool classof(const Value *V) {
358 return V->getValueType() == ConstantArrayVal;
363 //===----------------------------------------------------------------------===//
364 // ConstantStruct - Constant Struct Declarations
366 class ConstantStruct : public Constant {
367 friend struct ConstantCreator<ConstantStruct, StructType,
368 std::vector<Constant*> >;
369 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
371 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
374 /// get() - Static factory methods - Return objects of the specified value
376 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
377 static Constant *get(const std::vector<Constant*> &V);
379 /// getType() specialization - Reduce amount of casting...
381 inline const StructType *getType() const {
382 return reinterpret_cast<const StructType*>(Value::getType());
385 /// isNullValue - Return true if this is the value that would be returned by
386 /// getNullValue. This always returns false because zero structs are always
387 /// created as ConstantAggregateZero objects.
388 virtual bool isNullValue() const {
392 virtual void destroyConstant();
393 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
395 /// Methods for support type inquiry through isa, cast, and dyn_cast:
396 static inline bool classof(const ConstantStruct *) { return true; }
397 static bool classof(const Value *V) {
398 return V->getValueType() == ConstantStructVal;
402 //===----------------------------------------------------------------------===//
403 /// ConstantPacked - Constant Packed Declarations
405 class ConstantPacked : public Constant {
406 friend struct ConstantCreator<ConstantPacked, PackedType,
407 std::vector<Constant*> >;
408 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
410 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
413 /// get() - Static factory methods - Return objects of the specified value
414 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
415 static Constant *get(const std::vector<Constant*> &V);
417 /// getType - Specialize the getType() method to always return an PackedType,
418 /// which reduces the amount of casting needed in parts of the compiler.
420 inline const PackedType *getType() const {
421 return reinterpret_cast<const PackedType*>(Value::getType());
424 /// isNullValue - Return true if this is the value that would be returned by
425 /// getNullValue. This always returns false because zero arrays are always
426 /// created as ConstantAggregateZero objects.
427 virtual bool isNullValue() const { return false; }
429 virtual void destroyConstant();
430 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
432 /// Methods for support type inquiry through isa, cast, and dyn_cast:
433 static inline bool classof(const ConstantPacked *) { return true; }
434 static bool classof(const Value *V) {
435 return V->getValueType() == ConstantPackedVal;
439 //===----------------------------------------------------------------------===//
440 /// ConstantPointerNull - a constant pointer value that points to null
442 class ConstantPointerNull : public Constant {
443 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
444 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
446 ConstantPointerNull(const PointerType *T)
447 : Constant(reinterpret_cast<const Type*>(T),
448 Value::ConstantPointerNullVal, 0, 0) {}
452 /// get() - Static factory methods - Return objects of the specified value
453 static ConstantPointerNull *get(const PointerType *T);
455 /// isNullValue - Return true if this is the value that would be returned by
457 virtual bool isNullValue() const { return true; }
459 virtual void destroyConstant();
461 /// getType - Specialize the getType() method to always return an PointerType,
462 /// which reduces the amount of casting needed in parts of the compiler.
464 inline const PointerType *getType() const {
465 return reinterpret_cast<const PointerType*>(Value::getType());
468 /// Methods for support type inquiry through isa, cast, and dyn_cast:
469 static inline bool classof(const ConstantPointerNull *) { return true; }
470 static bool classof(const Value *V) {
471 return V->getValueType() == ConstantPointerNullVal;
476 /// ConstantExpr - a constant value that is initialized with an expression using
477 /// other constant values.
479 /// This class uses the standard Instruction opcodes to define the various
480 /// constant expressions. The Opcode field for the ConstantExpr class is
481 /// maintained in the Value::SubclassData field.
482 class ConstantExpr : public Constant {
483 friend struct ConstantCreator<ConstantExpr,Type,
484 std::pair<unsigned, std::vector<Constant*> > >;
485 friend struct ConvertConstantType<ConstantExpr, Type>;
488 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
489 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
490 // Operation type (an Instruction opcode) is stored as the SubclassData.
491 SubclassData = Opcode;
494 // These private methods are used by the type resolution code to create
495 // ConstantExprs in intermediate forms.
496 static Constant *getTy(const Type *Ty, unsigned Opcode,
497 Constant *C1, Constant *C2);
498 static Constant *getShiftTy(const Type *Ty,
499 unsigned Opcode, Constant *C1, Constant *C2);
500 static Constant *getSelectTy(const Type *Ty,
501 Constant *C1, Constant *C2, Constant *C3);
502 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
503 const std::vector<Value*> &IdxList);
504 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
506 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
507 Constant *Elt, Constant *Idx);
508 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
509 Constant *V2, Constant *Mask);
512 // Static methods to construct a ConstantExpr of different kinds. Note that
513 // these methods may return a object that is not an instance of the
514 // ConstantExpr class, because they will attempt to fold the constant
515 // expression into something simpler if possible.
517 /// Cast constant expr
519 static Constant *getCast(Constant *C, const Type *Ty);
520 static Constant *getSignExtend(Constant *C, const Type *Ty);
521 static Constant *getZeroExtend(Constant *C, const Type *Ty);
523 /// Select constant expr
525 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
526 return getSelectTy(V1->getType(), C, V1, V2);
529 /// getSizeOf constant expr - computes the size of a type in a target
530 /// independent way (Note: the return type is a ULong).
532 static Constant *getSizeOf(const Type *Ty);
534 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
535 /// return a pointer to a pointer of the array element type.
536 static Constant *getPtrPtrFromArrayPtr(Constant *C);
538 /// ConstantExpr::get - Return a binary or shift operator constant expression,
539 /// folding if possible.
541 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
543 /// ConstantExpr::get* - Return some common constants without having to
544 /// specify the full Instruction::OPCODE identifier.
546 static Constant *getNeg(Constant *C);
547 static Constant *getNot(Constant *C);
548 static Constant *getAdd(Constant *C1, Constant *C2);
549 static Constant *getSub(Constant *C1, Constant *C2);
550 static Constant *getMul(Constant *C1, Constant *C2);
551 static Constant *getUDiv(Constant *C1, Constant *C2);
552 static Constant *getSDiv(Constant *C1, Constant *C2);
553 static Constant *getFDiv(Constant *C1, Constant *C2);
554 static Constant *getRem(Constant *C1, Constant *C2);
555 static Constant *getAnd(Constant *C1, Constant *C2);
556 static Constant *getOr(Constant *C1, Constant *C2);
557 static Constant *getXor(Constant *C1, Constant *C2);
558 static Constant *getSetEQ(Constant *C1, Constant *C2);
559 static Constant *getSetNE(Constant *C1, Constant *C2);
560 static Constant *getSetLT(Constant *C1, Constant *C2);
561 static Constant *getSetGT(Constant *C1, Constant *C2);
562 static Constant *getSetLE(Constant *C1, Constant *C2);
563 static Constant *getSetGE(Constant *C1, Constant *C2);
564 static Constant *getShl(Constant *C1, Constant *C2);
565 static Constant *getShr(Constant *C1, Constant *C2);
567 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
568 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
570 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
571 /// all elements must be Constant's.
573 static Constant *getGetElementPtr(Constant *C,
574 const std::vector<Constant*> &IdxList);
575 static Constant *getGetElementPtr(Constant *C,
576 const std::vector<Value*> &IdxList);
578 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
579 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
580 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
582 /// isNullValue - Return true if this is the value that would be returned by
584 virtual bool isNullValue() const { return false; }
586 /// getOpcode - Return the opcode at the root of this constant expression
587 unsigned getOpcode() const { return SubclassData; }
589 /// getOpcodeName - Return a string representation for an opcode.
590 const char *getOpcodeName() const;
592 /// getWithOperandReplaced - Return a constant expression identical to this
593 /// one, but with the specified operand set to the specified value.
594 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
596 /// getWithOperands - This returns the current constant expression with the
597 /// operands replaced with the specified values. The specified operands must
598 /// match count and type with the existing ones.
599 Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
601 virtual void destroyConstant();
602 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
604 /// Override methods to provide more type information...
605 inline Constant *getOperand(unsigned i) {
606 return cast<Constant>(User::getOperand(i));
608 inline Constant *getOperand(unsigned i) const {
609 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
613 /// Methods for support type inquiry through isa, cast, and dyn_cast:
614 static inline bool classof(const ConstantExpr *) { return true; }
615 static inline bool classof(const Value *V) {
616 return V->getValueType() == ConstantExprVal;
621 //===----------------------------------------------------------------------===//
622 /// UndefValue - 'undef' values are things that do not have specified contents.
623 /// These are used for a variety of purposes, including global variable
624 /// initializers and operands to instructions. 'undef' values can occur with
627 class UndefValue : public Constant {
628 friend struct ConstantCreator<UndefValue, Type, char>;
629 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
631 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
633 /// get() - Static factory methods - Return an 'undef' object of the specified
636 static UndefValue *get(const Type *T);
638 /// isNullValue - Return true if this is the value that would be returned by
640 virtual bool isNullValue() const { return false; }
642 virtual void destroyConstant();
644 /// Methods for support type inquiry through isa, cast, and dyn_cast:
645 static inline bool classof(const UndefValue *) { return true; }
646 static bool classof(const Value *V) {
647 return V->getValueType() == UndefValueVal;
651 } // End llvm namespace