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 class of boolean and integrer constants. This class
40 /// represents both boolean and integral constants.
41 /// @brief Class for constant integers.
42 class ConstantInt : public Constant {
46 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
47 ConstantInt(const Type *Ty, uint64_t V);
48 ConstantInt(const Type *Ty, int64_t V);
50 friend struct ConstantCreator<ConstantInt, Type, uint64_t>;
52 /// Return the constant as a 64-bit unsigned integer value after it
53 /// has been zero extended as appropriate for the type of this constant.
54 /// @brief Return the zero extended value.
55 inline uint64_t getZExtValue() const {
59 /// Return the constant as a 64-bit integer value after it has been sign
60 /// sign extended as appropriate for the type of this constant.
61 /// @brief Return the sign extended value.
62 inline int64_t getSExtValue() const {
63 unsigned Size = getType()->getPrimitiveSizeInBits();
64 return (int64_t(Val) << (64-Size)) >> (64-Size);
66 /// A helper method that can be used to determine if the constant contained
67 /// within is equal to a constant. This only works for very small values,
68 /// because this is all that can be represented with all types.
69 /// @brief Determine if this constant's value is same as an unsigned char.
70 bool equalsInt(unsigned char V) const {
72 "equalsInt: Can only be used with very small positive constants!");
76 /// getTrue/getFalse - Return the singleton true/false values.
77 static inline ConstantInt *getTrue() {
78 static ConstantInt *T = 0;
80 return T = new ConstantInt(true);
82 static inline ConstantInt *getFalse() {
83 static ConstantInt *F = 0;
85 return F = new ConstantInt(false);
88 /// Return a ConstantInt with the specified value for the specified type. The
89 /// value V will be canonicalized to a uint64_t but accessing it with either
90 /// getSExtValue() or getZExtValue() (ConstantInt) will yield the correct
91 /// sized/signed value for the type Ty.
92 /// @brief Get a ConstantInt for a specific value.
93 static ConstantInt *get(const Type *Ty, int64_t V);
95 /// This static method returns true if the type Ty is big enough to
96 /// represent the value V. This can be used to avoid having the get method
97 /// assert when V is larger than Ty can represent. Note that there are two
98 /// versions of this method, one for unsigned and one for signed integers.
99 /// Although ConstantInt canonicalizes everything to an unsigned integer,
100 /// the signed version avoids callers having to convert a signed quantity
101 /// to the appropriate unsigned type before calling the method.
102 /// @returns true if V is a valid value for type Ty
103 /// @brief Determine if the value is in range for the given type.
104 static bool isValueValidForType(const Type *Ty, uint64_t V);
105 static bool isValueValidForType(const Type *Ty, int64_t V);
107 /// This function will return true iff this constant represents the "null"
108 /// value that would be returned by the getNullValue method.
109 /// @returns true if this is the null integer value.
110 /// @brief Determine if the value is null.
111 virtual bool isNullValue() const {
115 /// This function will return true iff every bit in this constant is set
117 /// @returns true iff this constant's bits are all set to true.
118 /// @brief Determine if the value is all ones.
119 bool isAllOnesValue() const {
120 return getSExtValue() == -1;
123 /// This function will return true iff this constant represents the largest
124 /// value that may be represented by the constant's type.
125 /// @returns true iff this is the largest value that may be represented
127 /// @brief Determine if the value is maximal.
128 bool isMaxValue(bool isSigned) const {
130 int64_t V = getSExtValue();
131 if (V < 0) return false; // Be careful about wrap-around on 'long's
133 return !isValueValidForType(getType(), V) || V < 0;
135 return isAllOnesValue();
138 /// This function will return true iff this constant represents the smallest
139 /// value that may be represented by this constant's type.
140 /// @returns true if this is the smallest value that may be represented by
142 /// @brief Determine if the value is minimal.
143 bool isMinValue(bool isSigned) const {
145 int64_t V = getSExtValue();
146 if (V > 0) return false; // Be careful about wrap-around on 'long's
148 return !isValueValidForType(getType(), V) || V > 0;
150 return getZExtValue() == 0;
153 /// @returns the value for an integer constant of the given type that has all
154 /// its bits set to true.
155 /// @brief Get the all ones value
156 static ConstantInt *getAllOnesValue(const Type *Ty);
158 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
159 static inline bool classof(const ConstantInt *) { return true; }
160 static bool classof(const Value *V) {
161 return V->getValueType() == ConstantIntVal;
166 //===----------------------------------------------------------------------===//
167 /// ConstantFP - Floating Point Values [float, double]
169 class ConstantFP : public Constant {
171 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
172 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
173 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
175 ConstantFP(const Type *Ty, double V);
177 /// get() - Static factory methods - Return objects of the specified value
178 static ConstantFP *get(const Type *Ty, double V);
180 /// isValueValidForType - return true if Ty is big enough to represent V.
181 static bool isValueValidForType(const Type *Ty, double V);
182 inline double getValue() const { return Val; }
184 /// isNullValue - Return true if this is the value that would be returned by
185 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
186 /// considers -0.0 to be null as well as 0.0. :(
187 virtual bool isNullValue() const;
189 /// isExactlyValue - We don't rely on operator== working on double values, as
190 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
191 /// As such, this method can be used to do an exact bit-for-bit comparison of
192 /// two floating point values.
193 bool isExactlyValue(double V) const;
195 /// Methods for support type inquiry through isa, cast, and dyn_cast:
196 static inline bool classof(const ConstantFP *) { return true; }
197 static bool classof(const Value *V) {
198 return V->getValueType() == ConstantFPVal;
202 //===----------------------------------------------------------------------===//
203 /// ConstantAggregateZero - All zero aggregate value
205 class ConstantAggregateZero : public Constant {
206 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
207 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
209 ConstantAggregateZero(const Type *Ty)
210 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
212 /// get() - static factory method for creating a null aggregate. It is
213 /// illegal to call this method with a non-aggregate type.
214 static Constant *get(const Type *Ty);
216 /// isNullValue - Return true if this is the value that would be returned by
218 virtual bool isNullValue() const { return true; }
220 virtual void destroyConstant();
222 /// Methods for support type inquiry through isa, cast, and dyn_cast:
224 static bool classof(const ConstantAggregateZero *) { return true; }
225 static bool classof(const Value *V) {
226 return V->getValueType() == ConstantAggregateZeroVal;
231 //===----------------------------------------------------------------------===//
232 /// ConstantArray - Constant Array Declarations
234 class ConstantArray : public Constant {
235 friend struct ConstantCreator<ConstantArray, ArrayType,
236 std::vector<Constant*> >;
237 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
239 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
242 /// get() - Static factory methods - Return objects of the specified value
243 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
245 /// This method constructs a ConstantArray and initializes it with a text
246 /// string. The default behavior (AddNull==true) causes a null terminator to
247 /// be placed at the end of the array. This effectively increases the length
248 /// of the array by one (you've been warned). However, in some situations
249 /// this is not desired so if AddNull==false then the string is copied without
250 /// null termination.
251 static Constant *get(const std::string &Initializer, bool AddNull = true);
253 /// getType - Specialize the getType() method to always return an ArrayType,
254 /// which reduces the amount of casting needed in parts of the compiler.
256 inline const ArrayType *getType() const {
257 return reinterpret_cast<const ArrayType*>(Value::getType());
260 /// isString - This method returns true if the array is an array of sbyte or
261 /// ubyte, and if the elements of the array are all ConstantInt's.
262 bool isString() const;
264 /// isCString - This method returns true if the array is a string (see
265 /// isString) and it ends in a null byte \0 and does not contains any other
266 /// null bytes except its terminator.
267 bool isCString() const;
269 /// getAsString - If this array is isString(), then this method converts the
270 /// array to an std::string and returns it. Otherwise, it asserts out.
272 std::string getAsString() const;
274 /// isNullValue - Return true if this is the value that would be returned by
275 /// getNullValue. This always returns false because zero arrays are always
276 /// created as ConstantAggregateZero objects.
277 virtual bool isNullValue() const { return false; }
279 virtual void destroyConstant();
280 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
282 /// Methods for support type inquiry through isa, cast, and dyn_cast:
283 static inline bool classof(const ConstantArray *) { return true; }
284 static bool classof(const Value *V) {
285 return V->getValueType() == ConstantArrayVal;
290 //===----------------------------------------------------------------------===//
291 // ConstantStruct - Constant Struct Declarations
293 class ConstantStruct : public Constant {
294 friend struct ConstantCreator<ConstantStruct, StructType,
295 std::vector<Constant*> >;
296 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
298 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
301 /// get() - Static factory methods - Return objects of the specified value
303 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
304 static Constant *get(const std::vector<Constant*> &V, bool packed = false);
306 /// getType() specialization - Reduce amount of casting...
308 inline const StructType *getType() const {
309 return reinterpret_cast<const StructType*>(Value::getType());
312 /// isNullValue - Return true if this is the value that would be returned by
313 /// getNullValue. This always returns false because zero structs are always
314 /// created as ConstantAggregateZero objects.
315 virtual bool isNullValue() const {
319 virtual void destroyConstant();
320 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
322 /// Methods for support type inquiry through isa, cast, and dyn_cast:
323 static inline bool classof(const ConstantStruct *) { return true; }
324 static bool classof(const Value *V) {
325 return V->getValueType() == ConstantStructVal;
329 //===----------------------------------------------------------------------===//
330 /// ConstantPacked - Constant Packed Declarations
332 class ConstantPacked : public Constant {
333 friend struct ConstantCreator<ConstantPacked, PackedType,
334 std::vector<Constant*> >;
335 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
337 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
340 /// get() - Static factory methods - Return objects of the specified value
341 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
342 static Constant *get(const std::vector<Constant*> &V);
344 /// getType - Specialize the getType() method to always return an PackedType,
345 /// which reduces the amount of casting needed in parts of the compiler.
347 inline const PackedType *getType() const {
348 return reinterpret_cast<const PackedType*>(Value::getType());
351 /// @returns the value for an packed integer constant of the given type that
352 /// has all its bits set to true.
353 /// @brief Get the all ones value
354 static ConstantPacked *getAllOnesValue(const PackedType *Ty);
356 /// isNullValue - Return true if this is the value that would be returned by
357 /// getNullValue. This always returns false because zero arrays are always
358 /// created as ConstantAggregateZero objects.
359 virtual bool isNullValue() const { return false; }
361 virtual void destroyConstant();
362 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
364 /// Methods for support type inquiry through isa, cast, and dyn_cast:
365 static inline bool classof(const ConstantPacked *) { return true; }
366 static bool classof(const Value *V) {
367 return V->getValueType() == ConstantPackedVal;
371 //===----------------------------------------------------------------------===//
372 /// ConstantPointerNull - a constant pointer value that points to null
374 class ConstantPointerNull : public Constant {
375 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
376 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
378 ConstantPointerNull(const PointerType *T)
379 : Constant(reinterpret_cast<const Type*>(T),
380 Value::ConstantPointerNullVal, 0, 0) {}
384 /// get() - Static factory methods - Return objects of the specified value
385 static ConstantPointerNull *get(const PointerType *T);
387 /// isNullValue - Return true if this is the value that would be returned by
389 virtual bool isNullValue() const { return true; }
391 virtual void destroyConstant();
393 /// getType - Specialize the getType() method to always return an PointerType,
394 /// which reduces the amount of casting needed in parts of the compiler.
396 inline const PointerType *getType() const {
397 return reinterpret_cast<const PointerType*>(Value::getType());
400 /// Methods for support type inquiry through isa, cast, and dyn_cast:
401 static inline bool classof(const ConstantPointerNull *) { return true; }
402 static bool classof(const Value *V) {
403 return V->getValueType() == ConstantPointerNullVal;
408 /// ConstantExpr - a constant value that is initialized with an expression using
409 /// other constant values.
411 /// This class uses the standard Instruction opcodes to define the various
412 /// constant expressions. The Opcode field for the ConstantExpr class is
413 /// maintained in the Value::SubclassData field.
414 class ConstantExpr : public Constant {
415 friend struct ConstantCreator<ConstantExpr,Type,
416 std::pair<unsigned, std::vector<Constant*> > >;
417 friend struct ConvertConstantType<ConstantExpr, Type>;
420 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
421 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
422 // Operation type (an Instruction opcode) is stored as the SubclassData.
423 SubclassData = Opcode;
426 // These private methods are used by the type resolution code to create
427 // ConstantExprs in intermediate forms.
428 static Constant *getTy(const Type *Ty, unsigned Opcode,
429 Constant *C1, Constant *C2);
430 static Constant *getCompareTy(unsigned short pred, Constant *C1,
432 static Constant *getShiftTy(const Type *Ty,
433 unsigned Opcode, Constant *C1, Constant *C2);
434 static Constant *getSelectTy(const Type *Ty,
435 Constant *C1, Constant *C2, Constant *C3);
436 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
437 const std::vector<Value*> &IdxList);
438 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
440 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
441 Constant *Elt, Constant *Idx);
442 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
443 Constant *V2, Constant *Mask);
446 // Static methods to construct a ConstantExpr of different kinds. Note that
447 // these methods may return a object that is not an instance of the
448 // ConstantExpr class, because they will attempt to fold the constant
449 // expression into something simpler if possible.
451 /// Cast constant expr
453 static Constant *getTrunc (Constant *C, const Type *Ty);
454 static Constant *getSExt (Constant *C, const Type *Ty);
455 static Constant *getZExt (Constant *C, const Type *Ty);
456 static Constant *getFPTrunc (Constant *C, const Type *Ty);
457 static Constant *getFPExtend(Constant *C, const Type *Ty);
458 static Constant *getUIToFP (Constant *C, const Type *Ty);
459 static Constant *getSIToFP (Constant *C, const Type *Ty);
460 static Constant *getFPToUI (Constant *C, const Type *Ty);
461 static Constant *getFPToSI (Constant *C, const Type *Ty);
462 static Constant *getPtrToInt(Constant *C, const Type *Ty);
463 static Constant *getIntToPtr(Constant *C, const Type *Ty);
464 static Constant *getBitCast (Constant *C, const Type *Ty);
466 // @brief Convenience function for getting one of the casting operations
467 // using a CastOps opcode.
468 static Constant *getCast(
469 unsigned ops, ///< The opcode for the conversion
470 Constant *C, ///< The constant to be converted
471 const Type *Ty ///< The type to which the constant is converted
474 // @brief Create a ZExt or BitCast cast constant expression
475 static Constant *getZExtOrBitCast(
476 Constant *C, ///< The constant to zext or bitcast
477 const Type *Ty ///< The type to zext or bitcast C to
480 // @brief Create a SExt or BitCast cast constant expression
481 static Constant *getSExtOrBitCast(
482 Constant *C, ///< The constant to sext or bitcast
483 const Type *Ty ///< The type to sext or bitcast C to
486 // @brief Create a Trunc or BitCast cast constant expression
487 static Constant *getTruncOrBitCast(
488 Constant *C, ///< The constant to trunc or bitcast
489 const Type *Ty ///< The type to trunc or bitcast C to
492 /// @brief Create a BitCast or a PtrToInt cast constant expression
493 static Constant *getPointerCast(
494 Constant *C, ///< The pointer value to be casted (operand 0)
495 const Type *Ty ///< The type to which cast should be made
498 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
499 static Constant *getIntegerCast(
500 Constant *C, ///< The integer constant to be casted
501 const Type *Ty, ///< The integer type to cast to
502 bool isSigned ///< Whether C should be treated as signed or not
505 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
506 static Constant *getFPCast(
507 Constant *C, ///< The integer constant to be casted
508 const Type *Ty ///< The integer type to cast to
511 /// @brief Return true if this is a convert constant expression
514 /// @brief Return true if this is a compare constant expression
515 bool isCompare() const;
517 /// Select constant expr
519 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
520 return getSelectTy(V1->getType(), C, V1, V2);
523 /// getSizeOf constant expr - computes the size of a type in a target
524 /// independent way (Note: the return type is a ULong).
526 static Constant *getSizeOf(const Type *Ty);
528 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
529 /// return a pointer to a pointer of the array element type.
530 static Constant *getPtrPtrFromArrayPtr(Constant *C);
532 /// ConstantExpr::get - Return a binary or shift operator constant expression,
533 /// folding if possible.
535 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
537 /// @brief Return an ICmp or FCmp comparison operator constant expression.
538 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
540 /// ConstantExpr::get* - Return some common constants without having to
541 /// specify the full Instruction::OPCODE identifier.
543 static Constant *getNeg(Constant *C);
544 static Constant *getNot(Constant *C);
545 static Constant *getAdd(Constant *C1, Constant *C2);
546 static Constant *getSub(Constant *C1, Constant *C2);
547 static Constant *getMul(Constant *C1, Constant *C2);
548 static Constant *getUDiv(Constant *C1, Constant *C2);
549 static Constant *getSDiv(Constant *C1, Constant *C2);
550 static Constant *getFDiv(Constant *C1, Constant *C2);
551 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
552 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
553 static Constant *getFRem(Constant *C1, Constant *C2);
554 static Constant *getAnd(Constant *C1, Constant *C2);
555 static Constant *getOr(Constant *C1, Constant *C2);
556 static Constant *getXor(Constant *C1, Constant *C2);
557 static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS);
558 static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS);
559 static Constant *getShl(Constant *C1, Constant *C2);
560 static Constant *getLShr(Constant *C1, Constant *C2);
561 static Constant *getAShr(Constant *C1, Constant *C2);
563 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
564 /// all elements must be Constant's.
566 static Constant *getGetElementPtr(Constant *C,
567 const std::vector<Constant*> &IdxList);
568 static Constant *getGetElementPtr(Constant *C,
569 const std::vector<Value*> &IdxList);
571 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
572 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
573 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
575 /// isNullValue - Return true if this is the value that would be returned by
577 virtual bool isNullValue() const { return false; }
579 /// getOpcode - Return the opcode at the root of this constant expression
580 unsigned getOpcode() const { return SubclassData; }
582 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
583 /// not an ICMP or FCMP constant expression.
584 unsigned getPredicate() const;
586 /// getOpcodeName - Return a string representation for an opcode.
587 const char *getOpcodeName() const;
589 /// getWithOperandReplaced - Return a constant expression identical to this
590 /// one, but with the specified operand set to the specified value.
591 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
593 /// getWithOperands - This returns the current constant expression with the
594 /// operands replaced with the specified values. The specified operands must
595 /// match count and type with the existing ones.
596 Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
598 virtual void destroyConstant();
599 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
601 /// Override methods to provide more type information...
602 inline Constant *getOperand(unsigned i) {
603 return cast<Constant>(User::getOperand(i));
605 inline Constant *getOperand(unsigned i) const {
606 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
610 /// Methods for support type inquiry through isa, cast, and dyn_cast:
611 static inline bool classof(const ConstantExpr *) { return true; }
612 static inline bool classof(const Value *V) {
613 return V->getValueType() == ConstantExprVal;
618 //===----------------------------------------------------------------------===//
619 /// UndefValue - 'undef' values are things that do not have specified contents.
620 /// These are used for a variety of purposes, including global variable
621 /// initializers and operands to instructions. 'undef' values can occur with
624 class UndefValue : public Constant {
625 friend struct ConstantCreator<UndefValue, Type, char>;
626 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
628 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
630 /// get() - Static factory methods - Return an 'undef' object of the specified
633 static UndefValue *get(const Type *T);
635 /// isNullValue - Return true if this is the value that would be returned by
637 virtual bool isNullValue() const { return false; }
639 virtual void destroyConstant();
641 /// Methods for support type inquiry through isa, cast, and dyn_cast:
642 static inline bool classof(const UndefValue *) { return true; }
643 static bool classof(const Value *V) {
644 return V->getValueType() == UndefValueVal;
648 } // End llvm namespace