1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for the subclasses of Constant,
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are
14 /// fully shared by structural equivalence. This means that two structurally
15 /// equivalent constants will always have the same address. Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CONSTANTS_H
22 #define LLVM_CONSTANTS_H
24 #include "llvm/Constant.h"
25 #include "llvm/Type.h"
26 #include "llvm/OperandTraits.h"
27 #include "llvm/ADT/APInt.h"
28 #include "llvm/ADT/APFloat.h"
29 #include "llvm/ADT/SmallVector.h"
38 template<class ConstantClass, class TypeClass, class ValType>
39 struct ConstantCreator;
40 template<class ConstantClass, class TypeClass>
41 struct ConvertConstantType;
43 //===----------------------------------------------------------------------===//
44 /// This is the shared class of boolean and integer constants. This class
45 /// represents both boolean and integral constants.
46 /// @brief Class for constant integers.
47 class ConstantInt : public Constant {
48 static ConstantInt *TheTrueVal, *TheFalseVal;
49 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
50 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
51 ConstantInt(const IntegerType *Ty, const APInt& V);
54 // allocate space for exactly zero operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 0);
59 /// Return the constant as an APInt value reference. This allows clients to
60 /// obtain a copy of the value, with all its precision in tact.
61 /// @brief Return the constant's value.
62 inline const APInt& getValue() const {
66 /// getBitWidth - Return the bitwidth of this constant.
67 unsigned getBitWidth() const { return Val.getBitWidth(); }
69 /// Return the constant as a 64-bit unsigned integer value after it
70 /// has been zero extended as appropriate for the type of this constant. Note
71 /// that this method can assert if the value does not fit in 64 bits.
73 /// @brief Return the zero extended value.
74 inline uint64_t getZExtValue() const {
75 return Val.getZExtValue();
78 /// Return the constant as a 64-bit integer value after it has been sign
79 /// extended as appropriate for the type of this constant. Note that
80 /// this method can assert if the value does not fit in 64 bits.
82 /// @brief Return the sign extended value.
83 inline int64_t getSExtValue() const {
84 return Val.getSExtValue();
87 /// A helper method that can be used to determine if the constant contained
88 /// within is equal to a constant. This only works for very small values,
89 /// because this is all that can be represented with all types.
90 /// @brief Determine if this constant's value is same as an unsigned char.
91 bool equalsInt(uint64_t V) const {
95 /// getTrue/getFalse - Return the singleton true/false values.
96 static inline ConstantInt *getTrue() {
97 if (TheTrueVal) return TheTrueVal;
98 return CreateTrueFalseVals(true);
100 static inline ConstantInt *getFalse() {
101 if (TheFalseVal) return TheFalseVal;
102 return CreateTrueFalseVals(false);
105 /// Return a ConstantInt with the specified value for the specified type. The
106 /// value V will be canonicalized to an unsigned APInt. Accessing it with
107 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
108 /// signed value for the type Ty.
109 /// @brief Get a ConstantInt for a specific value.
110 static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
112 /// Return a ConstantInt with the specified value for the specified type. The
113 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
114 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
115 /// signed value for the type Ty.
116 /// @brief Get a ConstantInt for a specific signed value.
117 static ConstantInt *getSigned(const Type *Ty, int64_t V) {
118 return get(Ty, V, true);
121 /// Return a ConstantInt with the specified value and an implied Type. The
122 /// type is the integer type that corresponds to the bit width of the value.
123 static ConstantInt *get(const APInt &V);
125 /// getType - Specialize the getType() method to always return an IntegerType,
126 /// which reduces the amount of casting needed in parts of the compiler.
128 inline const IntegerType *getType() const {
129 return reinterpret_cast<const IntegerType*>(Value::getType());
132 /// This static method returns true if the type Ty is big enough to
133 /// represent the value V. This can be used to avoid having the get method
134 /// assert when V is larger than Ty can represent. Note that there are two
135 /// versions of this method, one for unsigned and one for signed integers.
136 /// Although ConstantInt canonicalizes everything to an unsigned integer,
137 /// the signed version avoids callers having to convert a signed quantity
138 /// to the appropriate unsigned type before calling the method.
139 /// @returns true if V is a valid value for type Ty
140 /// @brief Determine if the value is in range for the given type.
141 static bool isValueValidForType(const Type *Ty, uint64_t V);
142 static bool isValueValidForType(const Type *Ty, int64_t V);
144 /// This function will return true iff this constant represents the "null"
145 /// value that would be returned by the getNullValue method.
146 /// @returns true if this is the null integer value.
147 /// @brief Determine if the value is null.
148 virtual bool isNullValue() const {
152 /// This is just a convenience method to make client code smaller for a
153 /// common code. It also correctly performs the comparison without the
154 /// potential for an assertion from getZExtValue().
155 bool isZero() const {
159 /// This is just a convenience method to make client code smaller for a
160 /// common case. It also correctly performs the comparison without the
161 /// potential for an assertion from getZExtValue().
162 /// @brief Determine if the value is one.
167 /// This function will return true iff every bit in this constant is set
169 /// @returns true iff this constant's bits are all set to true.
170 /// @brief Determine if the value is all ones.
171 bool isAllOnesValue() const {
172 return Val.isAllOnesValue();
175 /// This function will return true iff this constant represents the largest
176 /// value that may be represented by the constant's type.
177 /// @returns true iff this is the largest value that may be represented
179 /// @brief Determine if the value is maximal.
180 bool isMaxValue(bool isSigned) const {
182 return Val.isMaxSignedValue();
184 return Val.isMaxValue();
187 /// This function will return true iff this constant represents the smallest
188 /// value that may be represented by this constant's type.
189 /// @returns true if this is the smallest value that may be represented by
191 /// @brief Determine if the value is minimal.
192 bool isMinValue(bool isSigned) const {
194 return Val.isMinSignedValue();
196 return Val.isMinValue();
199 /// This function will return true iff this constant represents a value with
200 /// active bits bigger than 64 bits or a value greater than the given uint64_t
202 /// @returns true iff this constant is greater or equal to the given number.
203 /// @brief Determine if the value is greater or equal to the given number.
204 bool uge(uint64_t Num) {
205 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
208 /// getLimitedValue - If the value is smaller than the specified limit,
209 /// return it, otherwise return the limit value. This causes the value
210 /// to saturate to the limit.
211 /// @returns the min of the value of the constant and the specified value
212 /// @brief Get the constant's value with a saturation limit
213 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
214 return Val.getLimitedValue(Limit);
217 /// @returns the value for an integer constant of the given type that has all
218 /// its bits set to true.
219 /// @brief Get the all ones value
220 static ConstantInt *getAllOnesValue(const Type *Ty);
222 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
223 static inline bool classof(const ConstantInt *) { return true; }
224 static bool classof(const Value *V) {
225 return V->getValueID() == ConstantIntVal;
227 static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
229 static ConstantInt *CreateTrueFalseVals(bool WhichOne);
233 //===----------------------------------------------------------------------===//
234 /// ConstantFP - Floating Point Values [float, double]
236 class ConstantFP : public Constant {
238 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
239 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
241 ConstantFP(const Type *Ty, const APFloat& V);
243 // allocate space for exactly zero operands
244 void *operator new(size_t s) {
245 return User::operator new(s, 0);
248 /// get() - Static factory methods - Return objects of the specified value
249 static ConstantFP *get(const APFloat &V);
251 /// get() - This returns a constant fp for the specified value in the
252 /// specified type. This should only be used for simple constant values like
253 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
254 static ConstantFP *get(const Type *Ty, double V);
256 /// isValueValidForType - return true if Ty is big enough to represent V.
257 static bool isValueValidForType(const Type *Ty, const APFloat& V);
258 inline const APFloat& getValueAPF() const { return Val; }
260 /// isNullValue - Return true if this is the value that would be returned by
261 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
262 /// considers -0.0 to be null as well as 0.0. :(
263 virtual bool isNullValue() const;
265 // Get a negative zero.
266 static ConstantFP *getNegativeZero(const Type* Ty);
268 /// isExactlyValue - We don't rely on operator== working on double values, as
269 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
270 /// As such, this method can be used to do an exact bit-for-bit comparison of
271 /// two floating point values. The version with a double operand is retained
272 /// because it's so convenient to write isExactlyValue(2.0), but please use
273 /// it only for simple constants.
274 bool isExactlyValue(const APFloat& V) const;
276 bool isExactlyValue(double V) const {
278 // convert is not supported on this type
279 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
282 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
283 return isExactlyValue(FV);
285 /// Methods for support type inquiry through isa, cast, and dyn_cast:
286 static inline bool classof(const ConstantFP *) { return true; }
287 static bool classof(const Value *V) {
288 return V->getValueID() == ConstantFPVal;
292 //===----------------------------------------------------------------------===//
293 /// ConstantAggregateZero - All zero aggregate value
295 class ConstantAggregateZero : public Constant {
296 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
297 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
298 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
300 explicit ConstantAggregateZero(const Type *ty)
301 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
303 // allocate space for exactly zero operands
304 void *operator new(size_t s) {
305 return User::operator new(s, 0);
308 /// get() - static factory method for creating a null aggregate. It is
309 /// illegal to call this method with a non-aggregate type.
310 static ConstantAggregateZero *get(const Type *Ty);
312 /// isNullValue - Return true if this is the value that would be returned by
314 virtual bool isNullValue() const { return true; }
316 virtual void destroyConstant();
318 /// Methods for support type inquiry through isa, cast, and dyn_cast:
320 static bool classof(const ConstantAggregateZero *) { return true; }
321 static bool classof(const Value *V) {
322 return V->getValueID() == ConstantAggregateZeroVal;
327 //===----------------------------------------------------------------------===//
328 /// ConstantArray - Constant Array Declarations
330 class ConstantArray : public Constant {
331 friend struct ConstantCreator<ConstantArray, ArrayType,
332 std::vector<Constant*> >;
333 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
335 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
337 /// get() - Static factory methods - Return objects of the specified value
338 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
339 static Constant *get(const ArrayType *T,
340 Constant*const*Vals, unsigned NumVals) {
341 // FIXME: make this the primary ctor method.
342 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
345 /// This method constructs a ConstantArray and initializes it with a text
346 /// string. The default behavior (AddNull==true) causes a null terminator to
347 /// be placed at the end of the array. This effectively increases the length
348 /// of the array by one (you've been warned). However, in some situations
349 /// this is not desired so if AddNull==false then the string is copied without
350 /// null termination.
351 static Constant *get(const std::string &Initializer, bool AddNull = true);
353 /// Transparently provide more efficient getOperand methods.
354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
356 /// getType - Specialize the getType() method to always return an ArrayType,
357 /// which reduces the amount of casting needed in parts of the compiler.
359 inline const ArrayType *getType() const {
360 return reinterpret_cast<const ArrayType*>(Value::getType());
363 /// isString - This method returns true if the array is an array of i8 and
364 /// the elements of the array are all ConstantInt's.
365 bool isString() const;
367 /// isCString - This method returns true if the array is a string (see
369 /// isString) and it ends in a null byte \0 and does not contains any other
371 /// null bytes except its terminator.
372 bool isCString() const;
374 /// getAsString - If this array is isString(), then this method converts the
375 /// array to an std::string and returns it. Otherwise, it asserts out.
377 std::string getAsString() const;
379 /// isNullValue - Return true if this is the value that would be returned by
380 /// getNullValue. This always returns false because zero arrays are always
381 /// created as ConstantAggregateZero objects.
382 virtual bool isNullValue() const { return false; }
384 virtual void destroyConstant();
385 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
387 /// Methods for support type inquiry through isa, cast, and dyn_cast:
388 static inline bool classof(const ConstantArray *) { return true; }
389 static bool classof(const Value *V) {
390 return V->getValueID() == ConstantArrayVal;
395 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
398 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
400 //===----------------------------------------------------------------------===//
401 // ConstantStruct - Constant Struct Declarations
403 class ConstantStruct : public Constant {
404 friend struct ConstantCreator<ConstantStruct, StructType,
405 std::vector<Constant*> >;
406 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
408 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
410 /// get() - Static factory methods - Return objects of the specified value
412 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
413 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
414 static Constant *get(Constant*const* Vals, unsigned NumVals,
415 bool Packed = false) {
416 // FIXME: make this the primary ctor method.
417 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
420 /// Transparently provide more efficient getOperand methods.
421 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
423 /// getType() specialization - Reduce amount of casting...
425 inline const StructType *getType() const {
426 return reinterpret_cast<const StructType*>(Value::getType());
429 /// isNullValue - Return true if this is the value that would be returned by
430 /// getNullValue. This always returns false because zero structs are always
431 /// created as ConstantAggregateZero objects.
432 virtual bool isNullValue() const {
436 virtual void destroyConstant();
437 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
439 /// Methods for support type inquiry through isa, cast, and dyn_cast:
440 static inline bool classof(const ConstantStruct *) { return true; }
441 static bool classof(const Value *V) {
442 return V->getValueID() == ConstantStructVal;
447 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
450 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
452 //===----------------------------------------------------------------------===//
453 /// ConstantVector - Constant Vector Declarations
455 class ConstantVector : public Constant {
456 friend struct ConstantCreator<ConstantVector, VectorType,
457 std::vector<Constant*> >;
458 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
460 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
462 /// get() - Static factory methods - Return objects of the specified value
463 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
464 static Constant *get(const std::vector<Constant*> &V);
465 static Constant *get(Constant*const* Vals, unsigned NumVals) {
466 // FIXME: make this the primary ctor method.
467 return get(std::vector<Constant*>(Vals, Vals+NumVals));
470 /// Transparently provide more efficient getOperand methods.
471 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
473 /// getType - Specialize the getType() method to always return a VectorType,
474 /// which reduces the amount of casting needed in parts of the compiler.
476 inline const VectorType *getType() const {
477 return reinterpret_cast<const VectorType*>(Value::getType());
480 /// @returns the value for a vector integer constant of the given type that
481 /// has all its bits set to true.
482 /// @brief Get the all ones value
483 static ConstantVector *getAllOnesValue(const VectorType *Ty);
485 /// isNullValue - Return true if this is the value that would be returned by
486 /// getNullValue. This always returns false because zero vectors are always
487 /// created as ConstantAggregateZero objects.
488 virtual bool isNullValue() const { return false; }
490 /// This function will return true iff every element in this vector constant
491 /// is set to all ones.
492 /// @returns true iff this constant's emements are all set to all ones.
493 /// @brief Determine if the value is all ones.
494 bool isAllOnesValue() const;
496 /// getSplatValue - If this is a splat constant, meaning that all of the
497 /// elements have the same value, return that value. Otherwise return NULL.
498 Constant *getSplatValue();
500 virtual void destroyConstant();
501 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
503 /// Methods for support type inquiry through isa, cast, and dyn_cast:
504 static inline bool classof(const ConstantVector *) { return true; }
505 static bool classof(const Value *V) {
506 return V->getValueID() == ConstantVectorVal;
511 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
514 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
516 //===----------------------------------------------------------------------===//
517 /// ConstantPointerNull - a constant pointer value that points to null
519 class ConstantPointerNull : public Constant {
520 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
521 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
522 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
524 explicit ConstantPointerNull(const PointerType *T)
525 : Constant(reinterpret_cast<const Type*>(T),
526 Value::ConstantPointerNullVal, 0, 0) {}
529 // allocate space for exactly zero operands
530 void *operator new(size_t s) {
531 return User::operator new(s, 0);
534 /// get() - Static factory methods - Return objects of the specified value
535 static ConstantPointerNull *get(const PointerType *T);
537 /// isNullValue - Return true if this is the value that would be returned by
539 virtual bool isNullValue() const { return true; }
541 virtual void destroyConstant();
543 /// getType - Specialize the getType() method to always return an PointerType,
544 /// which reduces the amount of casting needed in parts of the compiler.
546 inline const PointerType *getType() const {
547 return reinterpret_cast<const PointerType*>(Value::getType());
550 /// Methods for support type inquiry through isa, cast, and dyn_cast:
551 static inline bool classof(const ConstantPointerNull *) { return true; }
552 static bool classof(const Value *V) {
553 return V->getValueID() == ConstantPointerNullVal;
558 /// ConstantExpr - a constant value that is initialized with an expression using
559 /// other constant values.
561 /// This class uses the standard Instruction opcodes to define the various
562 /// constant expressions. The Opcode field for the ConstantExpr class is
563 /// maintained in the Value::SubclassData field.
564 class ConstantExpr : public Constant {
565 friend struct ConstantCreator<ConstantExpr,Type,
566 std::pair<unsigned, std::vector<Constant*> > >;
567 friend struct ConvertConstantType<ConstantExpr, Type>;
570 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
571 : Constant(ty, ConstantExprVal, Ops, NumOps) {
572 // Operation type (an Instruction opcode) is stored as the SubclassData.
573 SubclassData = Opcode;
576 // These private methods are used by the type resolution code to create
577 // ConstantExprs in intermediate forms.
578 static Constant *getTy(const Type *Ty, unsigned Opcode,
579 Constant *C1, Constant *C2);
580 static Constant *getCompareTy(unsigned short pred, Constant *C1,
582 static Constant *getSelectTy(const Type *Ty,
583 Constant *C1, Constant *C2, Constant *C3);
584 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
585 Value* const *Idxs, unsigned NumIdxs);
586 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
588 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
589 Constant *Elt, Constant *Idx);
590 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
591 Constant *V2, Constant *Mask);
592 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
593 const unsigned *Idxs, unsigned NumIdxs);
594 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
596 const unsigned *Idxs, unsigned NumIdxs);
599 // Static methods to construct a ConstantExpr of different kinds. Note that
600 // these methods may return a object that is not an instance of the
601 // ConstantExpr class, because they will attempt to fold the constant
602 // expression into something simpler if possible.
604 /// Cast constant expr
606 static Constant *getTrunc (Constant *C, const Type *Ty);
607 static Constant *getSExt (Constant *C, const Type *Ty);
608 static Constant *getZExt (Constant *C, const Type *Ty);
609 static Constant *getFPTrunc (Constant *C, const Type *Ty);
610 static Constant *getFPExtend(Constant *C, const Type *Ty);
611 static Constant *getUIToFP (Constant *C, const Type *Ty);
612 static Constant *getSIToFP (Constant *C, const Type *Ty);
613 static Constant *getFPToUI (Constant *C, const Type *Ty);
614 static Constant *getFPToSI (Constant *C, const Type *Ty);
615 static Constant *getPtrToInt(Constant *C, const Type *Ty);
616 static Constant *getIntToPtr(Constant *C, const Type *Ty);
617 static Constant *getBitCast (Constant *C, const Type *Ty);
619 /// Transparently provide more efficient getOperand methods.
620 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
622 // @brief Convenience function for getting one of the casting operations
623 // using a CastOps opcode.
624 static Constant *getCast(
625 unsigned ops, ///< The opcode for the conversion
626 Constant *C, ///< The constant to be converted
627 const Type *Ty ///< The type to which the constant is converted
630 // @brief Create a ZExt or BitCast cast constant expression
631 static Constant *getZExtOrBitCast(
632 Constant *C, ///< The constant to zext or bitcast
633 const Type *Ty ///< The type to zext or bitcast C to
636 // @brief Create a SExt or BitCast cast constant expression
637 static Constant *getSExtOrBitCast(
638 Constant *C, ///< The constant to sext or bitcast
639 const Type *Ty ///< The type to sext or bitcast C to
642 // @brief Create a Trunc or BitCast cast constant expression
643 static Constant *getTruncOrBitCast(
644 Constant *C, ///< The constant to trunc or bitcast
645 const Type *Ty ///< The type to trunc or bitcast C to
648 /// @brief Create a BitCast or a PtrToInt cast constant expression
649 static Constant *getPointerCast(
650 Constant *C, ///< The pointer value to be casted (operand 0)
651 const Type *Ty ///< The type to which cast should be made
654 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
655 static Constant *getIntegerCast(
656 Constant *C, ///< The integer constant to be casted
657 const Type *Ty, ///< The integer type to cast to
658 bool isSigned ///< Whether C should be treated as signed or not
661 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
662 static Constant *getFPCast(
663 Constant *C, ///< The integer constant to be casted
664 const Type *Ty ///< The integer type to cast to
667 /// @brief Return true if this is a convert constant expression
670 /// @brief Return true if this is a compare constant expression
671 bool isCompare() const;
673 /// @brief Return true if this is an insertvalue or extractvalue expression,
674 /// and the getIndices() method may be used.
675 bool hasIndices() const;
677 /// Select constant expr
679 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
680 return getSelectTy(V1->getType(), C, V1, V2);
683 /// getAlignOf constant expr - computes the alignment of a type in a target
684 /// independent way (Note: the return type is an i32; Note: assumes that i8
685 /// is byte aligned).
687 static Constant *getAlignOf(const Type *Ty);
689 /// getSizeOf constant expr - computes the size of a type in a target
690 /// independent way (Note: the return type is an i64).
692 static Constant *getSizeOf(const Type *Ty);
694 /// ConstantExpr::get - Return a binary or shift operator constant expression,
695 /// folding if possible.
697 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
699 /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
701 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
703 /// ConstantExpr::get* - Return some common constants without having to
704 /// specify the full Instruction::OPCODE identifier.
706 static Constant *getNeg(Constant *C);
707 static Constant *getFNeg(Constant *C);
708 static Constant *getNot(Constant *C);
709 static Constant *getAdd(Constant *C1, Constant *C2);
710 static Constant *getFAdd(Constant *C1, Constant *C2);
711 static Constant *getSub(Constant *C1, Constant *C2);
712 static Constant *getFSub(Constant *C1, Constant *C2);
713 static Constant *getMul(Constant *C1, Constant *C2);
714 static Constant *getFMul(Constant *C1, Constant *C2);
715 static Constant *getUDiv(Constant *C1, Constant *C2);
716 static Constant *getSDiv(Constant *C1, Constant *C2);
717 static Constant *getFDiv(Constant *C1, Constant *C2);
718 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
719 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
720 static Constant *getFRem(Constant *C1, Constant *C2);
721 static Constant *getAnd(Constant *C1, Constant *C2);
722 static Constant *getOr(Constant *C1, Constant *C2);
723 static Constant *getXor(Constant *C1, Constant *C2);
724 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
725 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
726 static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
727 static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
728 static Constant *getShl(Constant *C1, Constant *C2);
729 static Constant *getLShr(Constant *C1, Constant *C2);
730 static Constant *getAShr(Constant *C1, Constant *C2);
732 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
733 /// all elements must be Constant's.
735 static Constant *getGetElementPtr(Constant *C,
736 Constant* const *IdxList, unsigned NumIdx);
737 static Constant *getGetElementPtr(Constant *C,
738 Value* const *IdxList, unsigned NumIdx);
740 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
741 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
742 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
743 static Constant *getExtractValue(Constant *Agg,
744 const unsigned *IdxList, unsigned NumIdx);
745 static Constant *getInsertValue(Constant *Agg, Constant *Val,
746 const unsigned *IdxList, unsigned NumIdx);
748 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
749 /// method returns the negative zero constant for floating point or vector
750 /// floating point types; for all other types, it returns the null value.
751 static Constant *getZeroValueForNegationExpr(const Type *Ty);
753 /// isNullValue - Return true if this is the value that would be returned by
755 virtual bool isNullValue() const { return false; }
757 /// getOpcode - Return the opcode at the root of this constant expression
758 unsigned getOpcode() const { return SubclassData; }
760 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
761 /// not an ICMP or FCMP constant expression.
762 unsigned getPredicate() const;
764 /// getIndices - Assert that this is an insertvalue or exactvalue
765 /// expression and return the list of indices.
766 const SmallVector<unsigned, 4> &getIndices() const;
768 /// getOpcodeName - Return a string representation for an opcode.
769 const char *getOpcodeName() const;
771 /// getWithOperandReplaced - Return a constant expression identical to this
772 /// one, but with the specified operand set to the specified value.
773 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
775 /// getWithOperands - This returns the current constant expression with the
776 /// operands replaced with the specified values. The specified operands must
777 /// match count and type with the existing ones.
778 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
779 return getWithOperands(&Ops[0], (unsigned)Ops.size());
781 Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
783 virtual void destroyConstant();
784 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
786 /// Methods for support type inquiry through isa, cast, and dyn_cast:
787 static inline bool classof(const ConstantExpr *) { return true; }
788 static inline bool classof(const Value *V) {
789 return V->getValueID() == ConstantExprVal;
794 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
797 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
799 //===----------------------------------------------------------------------===//
800 /// UndefValue - 'undef' values are things that do not have specified contents.
801 /// These are used for a variety of purposes, including global variable
802 /// initializers and operands to instructions. 'undef' values can occur with
805 class UndefValue : public Constant {
806 friend struct ConstantCreator<UndefValue, Type, char>;
807 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
808 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
810 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
812 // allocate space for exactly zero operands
813 void *operator new(size_t s) {
814 return User::operator new(s, 0);
817 /// get() - Static factory methods - Return an 'undef' object of the specified
820 static UndefValue *get(const Type *T);
822 /// isNullValue - Return true if this is the value that would be returned by
824 virtual bool isNullValue() const { return false; }
826 virtual void destroyConstant();
828 /// Methods for support type inquiry through isa, cast, and dyn_cast:
829 static inline bool classof(const UndefValue *) { return true; }
830 static bool classof(const Value *V) {
831 return V->getValueID() == UndefValueVal;
835 //===----------------------------------------------------------------------===//
836 /// MDString - a single uniqued string.
837 /// These are used to efficiently contain a byte sequence for metadata.
839 class MDString : public Constant {
840 MDString(const MDString &); // DO NOT IMPLEMENT
841 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
842 MDString(const char *begin, const char *end);
844 const char *StrBegin, *StrEnd;
846 // allocate space for exactly zero operands
847 void *operator new(size_t s) {
848 return User::operator new(s, 0);
851 /// get() - Static factory methods - Return objects of the specified value.
853 static MDString *get(const char *StrBegin, const char *StrEnd);
855 /// size() - The length of this string.
857 intptr_t size() const { return StrEnd - StrBegin; }
859 /// begin() - Pointer to the first byte of the string.
861 const char *begin() const { return StrBegin; }
863 /// end() - Pointer to one byte past the end of the string.
865 const char *end() const { return StrEnd; }
867 /// getType() specialization - Type is always MetadataTy.
869 inline const Type *getType() const {
870 return Type::MetadataTy;
873 /// isNullValue - Return true if this is the value that would be returned by
874 /// getNullValue. This always returns false because getNullValue will never
875 /// produce metadata.
876 virtual bool isNullValue() const {
880 virtual void destroyConstant();
882 /// Methods for support type inquiry through isa, cast, and dyn_cast:
883 static inline bool classof(const MDString *) { return true; }
884 static bool classof(const Value *V) {
885 return V->getValueID() == MDStringVal;
889 } // End llvm namespace