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 integer value for the specified
106 /// type. If the type is wider than 64 bits, the value will be zero-extended
107 /// to fit the type, unless isSigned is true, in which case the value will
108 /// be interpreted as a 64-bit signed integer and sign-extended to fit
110 /// @brief Get a ConstantInt for a specific value.
111 static ConstantInt *get(const IntegerType *Ty,
112 uint64_t V, bool isSigned = false);
114 /// If Ty is a vector type, return a Constant with a splat of the given
115 /// value. Otherwise return a ConstantInt for the given value.
116 static Constant *get(const Type *Ty, uint64_t V, bool isSigned = false);
118 /// Return a ConstantInt with the specified value for the specified type. The
119 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
120 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
121 /// signed value for the type Ty.
122 /// @brief Get a ConstantInt for a specific signed value.
123 static ConstantInt *getSigned(const IntegerType *Ty, int64_t V) {
124 return get(Ty, V, true);
126 static Constant *getSigned(const Type *Ty, int64_t V) {
127 return get(Ty, V, true);
130 /// Return a ConstantInt with the specified value and an implied Type. The
131 /// type is the integer type that corresponds to the bit width of the value.
132 static ConstantInt *get(const APInt &V);
134 /// If Ty is a vector type, return a Constant with a splat of the given
135 /// value. Otherwise return a ConstantInt for the given value.
136 static Constant *get(const Type *Ty, const APInt &V);
138 /// getType - Specialize the getType() method to always return an IntegerType,
139 /// which reduces the amount of casting needed in parts of the compiler.
141 inline const IntegerType *getType() const {
142 return reinterpret_cast<const IntegerType*>(Value::getType());
145 /// This static method returns true if the type Ty is big enough to
146 /// represent the value V. This can be used to avoid having the get method
147 /// assert when V is larger than Ty can represent. Note that there are two
148 /// versions of this method, one for unsigned and one for signed integers.
149 /// Although ConstantInt canonicalizes everything to an unsigned integer,
150 /// the signed version avoids callers having to convert a signed quantity
151 /// to the appropriate unsigned type before calling the method.
152 /// @returns true if V is a valid value for type Ty
153 /// @brief Determine if the value is in range for the given type.
154 static bool isValueValidForType(const Type *Ty, uint64_t V);
155 static bool isValueValidForType(const Type *Ty, int64_t V);
157 /// This function will return true iff this constant represents the "null"
158 /// value that would be returned by the getNullValue method.
159 /// @returns true if this is the null integer value.
160 /// @brief Determine if the value is null.
161 virtual bool isNullValue() const {
165 /// This is just a convenience method to make client code smaller for a
166 /// common code. It also correctly performs the comparison without the
167 /// potential for an assertion from getZExtValue().
168 bool isZero() const {
172 /// This is just a convenience method to make client code smaller for a
173 /// common case. It also correctly performs the comparison without the
174 /// potential for an assertion from getZExtValue().
175 /// @brief Determine if the value is one.
180 /// This function will return true iff every bit in this constant is set
182 /// @returns true iff this constant's bits are all set to true.
183 /// @brief Determine if the value is all ones.
184 bool isAllOnesValue() const {
185 return Val.isAllOnesValue();
188 /// This function will return true iff this constant represents the largest
189 /// value that may be represented by the constant's type.
190 /// @returns true iff this is the largest value that may be represented
192 /// @brief Determine if the value is maximal.
193 bool isMaxValue(bool isSigned) const {
195 return Val.isMaxSignedValue();
197 return Val.isMaxValue();
200 /// This function will return true iff this constant represents the smallest
201 /// value that may be represented by this constant's type.
202 /// @returns true if this is the smallest value that may be represented by
204 /// @brief Determine if the value is minimal.
205 bool isMinValue(bool isSigned) const {
207 return Val.isMinSignedValue();
209 return Val.isMinValue();
212 /// This function will return true iff this constant represents a value with
213 /// active bits bigger than 64 bits or a value greater than the given uint64_t
215 /// @returns true iff this constant is greater or equal to the given number.
216 /// @brief Determine if the value is greater or equal to the given number.
217 bool uge(uint64_t Num) {
218 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
221 /// getLimitedValue - If the value is smaller than the specified limit,
222 /// return it, otherwise return the limit value. This causes the value
223 /// to saturate to the limit.
224 /// @returns the min of the value of the constant and the specified value
225 /// @brief Get the constant's value with a saturation limit
226 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
227 return Val.getLimitedValue(Limit);
230 /// @returns the value for an integer constant of the given type that has all
231 /// its bits set to true.
232 /// @brief Get the all ones value
233 static ConstantInt *getAllOnesValue(const Type *Ty);
235 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
236 static inline bool classof(const ConstantInt *) { return true; }
237 static bool classof(const Value *V) {
238 return V->getValueID() == ConstantIntVal;
240 static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
242 static ConstantInt *CreateTrueFalseVals(bool WhichOne);
246 //===----------------------------------------------------------------------===//
247 /// ConstantFP - Floating Point Values [float, double]
249 class ConstantFP : public Constant {
251 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
252 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
254 ConstantFP(const Type *Ty, const APFloat& V);
256 // allocate space for exactly zero operands
257 void *operator new(size_t s) {
258 return User::operator new(s, 0);
261 /// get() - Static factory methods - Return objects of the specified value
262 static ConstantFP *get(const APFloat &V);
264 /// get() - This returns a ConstantFP, or a vector containing a splat of a
265 /// ConstantFP, for the specified value in the specified type. This should
266 /// only be used for simple constant values like 2.0/1.0 etc, that are
267 /// known-valid both as host double and as the target format.
268 static Constant *get(const Type *Ty, double V);
270 /// isValueValidForType - return true if Ty is big enough to represent V.
271 static bool isValueValidForType(const Type *Ty, const APFloat& V);
272 inline const APFloat& getValueAPF() const { return Val; }
274 /// isNullValue - Return true if this is the value that would be returned by
275 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
276 /// considers -0.0 to be null as well as 0.0. :(
277 virtual bool isNullValue() const;
279 // Get a negative zero.
280 static ConstantFP *getNegativeZero(const Type* Ty);
282 /// isExactlyValue - We don't rely on operator== working on double values, as
283 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
284 /// As such, this method can be used to do an exact bit-for-bit comparison of
285 /// two floating point values. The version with a double operand is retained
286 /// because it's so convenient to write isExactlyValue(2.0), but please use
287 /// it only for simple constants.
288 bool isExactlyValue(const APFloat& V) const;
290 bool isExactlyValue(double V) const {
292 // convert is not supported on this type
293 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
296 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
297 return isExactlyValue(FV);
299 /// Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const ConstantFP *) { return true; }
301 static bool classof(const Value *V) {
302 return V->getValueID() == ConstantFPVal;
306 //===----------------------------------------------------------------------===//
307 /// ConstantAggregateZero - All zero aggregate value
309 class ConstantAggregateZero : public Constant {
310 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
311 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
312 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
314 explicit ConstantAggregateZero(const Type *ty)
315 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
317 // allocate space for exactly zero operands
318 void *operator new(size_t s) {
319 return User::operator new(s, 0);
322 /// get() - static factory method for creating a null aggregate. It is
323 /// illegal to call this method with a non-aggregate type.
324 static ConstantAggregateZero *get(const Type *Ty);
326 /// isNullValue - Return true if this is the value that would be returned by
328 virtual bool isNullValue() const { return true; }
330 virtual void destroyConstant();
332 /// Methods for support type inquiry through isa, cast, and dyn_cast:
334 static bool classof(const ConstantAggregateZero *) { return true; }
335 static bool classof(const Value *V) {
336 return V->getValueID() == ConstantAggregateZeroVal;
341 //===----------------------------------------------------------------------===//
342 /// ConstantArray - Constant Array Declarations
344 class ConstantArray : public Constant {
345 friend struct ConstantCreator<ConstantArray, ArrayType,
346 std::vector<Constant*> >;
347 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
349 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
351 /// get() - Static factory methods - Return objects of the specified value
352 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
353 static Constant *get(const ArrayType *T,
354 Constant*const*Vals, unsigned NumVals) {
355 // FIXME: make this the primary ctor method.
356 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
359 /// This method constructs a ConstantArray and initializes it with a text
360 /// string. The default behavior (AddNull==true) causes a null terminator to
361 /// be placed at the end of the array. This effectively increases the length
362 /// of the array by one (you've been warned). However, in some situations
363 /// this is not desired so if AddNull==false then the string is copied without
364 /// null termination.
365 static Constant *get(const std::string &Initializer, bool AddNull = true);
367 /// Transparently provide more efficient getOperand methods.
368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
370 /// getType - Specialize the getType() method to always return an ArrayType,
371 /// which reduces the amount of casting needed in parts of the compiler.
373 inline const ArrayType *getType() const {
374 return reinterpret_cast<const ArrayType*>(Value::getType());
377 /// isString - This method returns true if the array is an array of i8 and
378 /// the elements of the array are all ConstantInt's.
379 bool isString() const;
381 /// isCString - This method returns true if the array is a string (see
383 /// isString) and it ends in a null byte \0 and does not contains any other
385 /// null bytes except its terminator.
386 bool isCString() const;
388 /// getAsString - If this array is isString(), then this method converts the
389 /// array to an std::string and returns it. Otherwise, it asserts out.
391 std::string getAsString() const;
393 /// isNullValue - Return true if this is the value that would be returned by
394 /// getNullValue. This always returns false because zero arrays are always
395 /// created as ConstantAggregateZero objects.
396 virtual bool isNullValue() const { return false; }
398 virtual void destroyConstant();
399 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
401 /// Methods for support type inquiry through isa, cast, and dyn_cast:
402 static inline bool classof(const ConstantArray *) { return true; }
403 static bool classof(const Value *V) {
404 return V->getValueID() == ConstantArrayVal;
409 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
412 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
414 //===----------------------------------------------------------------------===//
415 // ConstantStruct - Constant Struct Declarations
417 class ConstantStruct : public Constant {
418 friend struct ConstantCreator<ConstantStruct, StructType,
419 std::vector<Constant*> >;
420 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
422 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
424 /// get() - Static factory methods - Return objects of the specified value
426 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
427 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
428 static Constant *get(Constant*const* Vals, unsigned NumVals,
429 bool Packed = false) {
430 // FIXME: make this the primary ctor method.
431 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
434 /// Transparently provide more efficient getOperand methods.
435 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
437 /// getType() specialization - Reduce amount of casting...
439 inline const StructType *getType() const {
440 return reinterpret_cast<const StructType*>(Value::getType());
443 /// isNullValue - Return true if this is the value that would be returned by
444 /// getNullValue. This always returns false because zero structs are always
445 /// created as ConstantAggregateZero objects.
446 virtual bool isNullValue() const {
450 virtual void destroyConstant();
451 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
453 /// Methods for support type inquiry through isa, cast, and dyn_cast:
454 static inline bool classof(const ConstantStruct *) { return true; }
455 static bool classof(const Value *V) {
456 return V->getValueID() == ConstantStructVal;
461 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
464 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
466 //===----------------------------------------------------------------------===//
467 /// ConstantVector - Constant Vector Declarations
469 class ConstantVector : public Constant {
470 friend struct ConstantCreator<ConstantVector, VectorType,
471 std::vector<Constant*> >;
472 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
474 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
476 /// get() - Static factory methods - Return objects of the specified value
477 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
478 static Constant *get(const std::vector<Constant*> &V);
479 static Constant *get(Constant*const* Vals, unsigned NumVals) {
480 // FIXME: make this the primary ctor method.
481 return get(std::vector<Constant*>(Vals, Vals+NumVals));
484 /// Transparently provide more efficient getOperand methods.
485 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
487 /// getType - Specialize the getType() method to always return a VectorType,
488 /// which reduces the amount of casting needed in parts of the compiler.
490 inline const VectorType *getType() const {
491 return reinterpret_cast<const VectorType*>(Value::getType());
494 /// @returns the value for a vector integer constant of the given type that
495 /// has all its bits set to true.
496 /// @brief Get the all ones value
497 static ConstantVector *getAllOnesValue(const VectorType *Ty);
499 /// isNullValue - Return true if this is the value that would be returned by
500 /// getNullValue. This always returns false because zero vectors are always
501 /// created as ConstantAggregateZero objects.
502 virtual bool isNullValue() const { return false; }
504 /// This function will return true iff every element in this vector constant
505 /// is set to all ones.
506 /// @returns true iff this constant's emements are all set to all ones.
507 /// @brief Determine if the value is all ones.
508 bool isAllOnesValue() const;
510 /// getSplatValue - If this is a splat constant, meaning that all of the
511 /// elements have the same value, return that value. Otherwise return NULL.
512 Constant *getSplatValue();
514 virtual void destroyConstant();
515 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
517 /// Methods for support type inquiry through isa, cast, and dyn_cast:
518 static inline bool classof(const ConstantVector *) { return true; }
519 static bool classof(const Value *V) {
520 return V->getValueID() == ConstantVectorVal;
525 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
528 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
530 //===----------------------------------------------------------------------===//
531 /// ConstantPointerNull - a constant pointer value that points to null
533 class ConstantPointerNull : public Constant {
534 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
535 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
536 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
538 explicit ConstantPointerNull(const PointerType *T)
539 : Constant(reinterpret_cast<const Type*>(T),
540 Value::ConstantPointerNullVal, 0, 0) {}
543 // allocate space for exactly zero operands
544 void *operator new(size_t s) {
545 return User::operator new(s, 0);
548 /// get() - Static factory methods - Return objects of the specified value
549 static ConstantPointerNull *get(const PointerType *T);
551 /// isNullValue - Return true if this is the value that would be returned by
553 virtual bool isNullValue() const { return true; }
555 virtual void destroyConstant();
557 /// getType - Specialize the getType() method to always return an PointerType,
558 /// which reduces the amount of casting needed in parts of the compiler.
560 inline const PointerType *getType() const {
561 return reinterpret_cast<const PointerType*>(Value::getType());
564 /// Methods for support type inquiry through isa, cast, and dyn_cast:
565 static inline bool classof(const ConstantPointerNull *) { return true; }
566 static bool classof(const Value *V) {
567 return V->getValueID() == ConstantPointerNullVal;
572 /// ConstantExpr - a constant value that is initialized with an expression using
573 /// other constant values.
575 /// This class uses the standard Instruction opcodes to define the various
576 /// constant expressions. The Opcode field for the ConstantExpr class is
577 /// maintained in the Value::SubclassData field.
578 class ConstantExpr : public Constant {
579 friend struct ConstantCreator<ConstantExpr,Type,
580 std::pair<unsigned, std::vector<Constant*> > >;
581 friend struct ConvertConstantType<ConstantExpr, Type>;
584 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
585 : Constant(ty, ConstantExprVal, Ops, NumOps) {
586 // Operation type (an Instruction opcode) is stored as the SubclassData.
587 SubclassData = Opcode;
590 // These private methods are used by the type resolution code to create
591 // ConstantExprs in intermediate forms.
592 static Constant *getTy(const Type *Ty, unsigned Opcode,
593 Constant *C1, Constant *C2);
594 static Constant *getCompareTy(unsigned short pred, Constant *C1,
596 static Constant *getSelectTy(const Type *Ty,
597 Constant *C1, Constant *C2, Constant *C3);
598 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
599 Value* const *Idxs, unsigned NumIdxs);
600 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
602 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
603 Constant *Elt, Constant *Idx);
604 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
605 Constant *V2, Constant *Mask);
606 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
607 const unsigned *Idxs, unsigned NumIdxs);
608 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
610 const unsigned *Idxs, unsigned NumIdxs);
613 // Static methods to construct a ConstantExpr of different kinds. Note that
614 // these methods may return a object that is not an instance of the
615 // ConstantExpr class, because they will attempt to fold the constant
616 // expression into something simpler if possible.
618 /// Cast constant expr
620 static Constant *getTrunc (Constant *C, const Type *Ty);
621 static Constant *getSExt (Constant *C, const Type *Ty);
622 static Constant *getZExt (Constant *C, const Type *Ty);
623 static Constant *getFPTrunc (Constant *C, const Type *Ty);
624 static Constant *getFPExtend(Constant *C, const Type *Ty);
625 static Constant *getUIToFP (Constant *C, const Type *Ty);
626 static Constant *getSIToFP (Constant *C, const Type *Ty);
627 static Constant *getFPToUI (Constant *C, const Type *Ty);
628 static Constant *getFPToSI (Constant *C, const Type *Ty);
629 static Constant *getPtrToInt(Constant *C, const Type *Ty);
630 static Constant *getIntToPtr(Constant *C, const Type *Ty);
631 static Constant *getBitCast (Constant *C, const Type *Ty);
633 /// Transparently provide more efficient getOperand methods.
634 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
636 // @brief Convenience function for getting one of the casting operations
637 // using a CastOps opcode.
638 static Constant *getCast(
639 unsigned ops, ///< The opcode for the conversion
640 Constant *C, ///< The constant to be converted
641 const Type *Ty ///< The type to which the constant is converted
644 // @brief Create a ZExt or BitCast cast constant expression
645 static Constant *getZExtOrBitCast(
646 Constant *C, ///< The constant to zext or bitcast
647 const Type *Ty ///< The type to zext or bitcast C to
650 // @brief Create a SExt or BitCast cast constant expression
651 static Constant *getSExtOrBitCast(
652 Constant *C, ///< The constant to sext or bitcast
653 const Type *Ty ///< The type to sext or bitcast C to
656 // @brief Create a Trunc or BitCast cast constant expression
657 static Constant *getTruncOrBitCast(
658 Constant *C, ///< The constant to trunc or bitcast
659 const Type *Ty ///< The type to trunc or bitcast C to
662 /// @brief Create a BitCast or a PtrToInt cast constant expression
663 static Constant *getPointerCast(
664 Constant *C, ///< The pointer value to be casted (operand 0)
665 const Type *Ty ///< The type to which cast should be made
668 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
669 static Constant *getIntegerCast(
670 Constant *C, ///< The integer constant to be casted
671 const Type *Ty, ///< The integer type to cast to
672 bool isSigned ///< Whether C should be treated as signed or not
675 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
676 static Constant *getFPCast(
677 Constant *C, ///< The integer constant to be casted
678 const Type *Ty ///< The integer type to cast to
681 /// @brief Return true if this is a convert constant expression
684 /// @brief Return true if this is a compare constant expression
685 bool isCompare() const;
687 /// @brief Return true if this is an insertvalue or extractvalue expression,
688 /// and the getIndices() method may be used.
689 bool hasIndices() const;
691 /// Select constant expr
693 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
694 return getSelectTy(V1->getType(), C, V1, V2);
697 /// getAlignOf constant expr - computes the alignment of a type in a target
698 /// independent way (Note: the return type is an i32; Note: assumes that i8
699 /// is byte aligned).
701 static Constant *getAlignOf(const Type *Ty);
703 /// getSizeOf constant expr - computes the size of a type in a target
704 /// independent way (Note: the return type is an i64).
706 static Constant *getSizeOf(const Type *Ty);
708 /// ConstantExpr::get - Return a binary or shift operator constant expression,
709 /// folding if possible.
711 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
713 /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
715 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
717 /// ConstantExpr::get* - Return some common constants without having to
718 /// specify the full Instruction::OPCODE identifier.
720 static Constant *getNeg(Constant *C);
721 static Constant *getFNeg(Constant *C);
722 static Constant *getNot(Constant *C);
723 static Constant *getAdd(Constant *C1, Constant *C2);
724 static Constant *getFAdd(Constant *C1, Constant *C2);
725 static Constant *getSub(Constant *C1, Constant *C2);
726 static Constant *getFSub(Constant *C1, Constant *C2);
727 static Constant *getMul(Constant *C1, Constant *C2);
728 static Constant *getFMul(Constant *C1, Constant *C2);
729 static Constant *getUDiv(Constant *C1, Constant *C2);
730 static Constant *getSDiv(Constant *C1, Constant *C2);
731 static Constant *getFDiv(Constant *C1, Constant *C2);
732 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
733 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
734 static Constant *getFRem(Constant *C1, Constant *C2);
735 static Constant *getAnd(Constant *C1, Constant *C2);
736 static Constant *getOr(Constant *C1, Constant *C2);
737 static Constant *getXor(Constant *C1, Constant *C2);
738 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
739 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
740 static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
741 static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
742 static Constant *getShl(Constant *C1, Constant *C2);
743 static Constant *getLShr(Constant *C1, Constant *C2);
744 static Constant *getAShr(Constant *C1, Constant *C2);
746 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
747 /// all elements must be Constant's.
749 static Constant *getGetElementPtr(Constant *C,
750 Constant* const *IdxList, unsigned NumIdx);
751 static Constant *getGetElementPtr(Constant *C,
752 Value* const *IdxList, unsigned NumIdx);
754 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
755 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
756 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
757 static Constant *getExtractValue(Constant *Agg,
758 const unsigned *IdxList, unsigned NumIdx);
759 static Constant *getInsertValue(Constant *Agg, Constant *Val,
760 const unsigned *IdxList, unsigned NumIdx);
762 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
763 /// method returns the negative zero constant for floating point or vector
764 /// floating point types; for all other types, it returns the null value.
765 static Constant *getZeroValueForNegationExpr(const Type *Ty);
767 /// isNullValue - Return true if this is the value that would be returned by
769 virtual bool isNullValue() const { return false; }
771 /// getOpcode - Return the opcode at the root of this constant expression
772 unsigned getOpcode() const { return SubclassData; }
774 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
775 /// not an ICMP or FCMP constant expression.
776 unsigned getPredicate() const;
778 /// getIndices - Assert that this is an insertvalue or exactvalue
779 /// expression and return the list of indices.
780 const SmallVector<unsigned, 4> &getIndices() const;
782 /// getOpcodeName - Return a string representation for an opcode.
783 const char *getOpcodeName() const;
785 /// getWithOperandReplaced - Return a constant expression identical to this
786 /// one, but with the specified operand set to the specified value.
787 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
789 /// getWithOperands - This returns the current constant expression with the
790 /// operands replaced with the specified values. The specified operands must
791 /// match count and type with the existing ones.
792 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
793 return getWithOperands(&Ops[0], (unsigned)Ops.size());
795 Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
797 virtual void destroyConstant();
798 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
800 /// Methods for support type inquiry through isa, cast, and dyn_cast:
801 static inline bool classof(const ConstantExpr *) { return true; }
802 static inline bool classof(const Value *V) {
803 return V->getValueID() == ConstantExprVal;
808 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
811 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
813 //===----------------------------------------------------------------------===//
814 /// UndefValue - 'undef' values are things that do not have specified contents.
815 /// These are used for a variety of purposes, including global variable
816 /// initializers and operands to instructions. 'undef' values can occur with
819 class UndefValue : public Constant {
820 friend struct ConstantCreator<UndefValue, Type, char>;
821 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
822 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
824 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
826 // allocate space for exactly zero operands
827 void *operator new(size_t s) {
828 return User::operator new(s, 0);
831 /// get() - Static factory methods - Return an 'undef' object of the specified
834 static UndefValue *get(const Type *T);
836 /// isNullValue - Return true if this is the value that would be returned by
838 virtual bool isNullValue() const { return false; }
840 virtual void destroyConstant();
842 /// Methods for support type inquiry through isa, cast, and dyn_cast:
843 static inline bool classof(const UndefValue *) { return true; }
844 static bool classof(const Value *V) {
845 return V->getValueID() == UndefValueVal;
849 //===----------------------------------------------------------------------===//
850 /// MDString - a single uniqued string.
851 /// These are used to efficiently contain a byte sequence for metadata.
853 class MDString : public Constant {
854 MDString(const MDString &); // DO NOT IMPLEMENT
855 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
856 MDString(const char *begin, const char *end);
858 const char *StrBegin, *StrEnd;
860 // allocate space for exactly zero operands
861 void *operator new(size_t s) {
862 return User::operator new(s, 0);
865 /// get() - Static factory methods - Return objects of the specified value.
867 static MDString *get(const char *StrBegin, const char *StrEnd);
868 static MDString *get(const std::string &Str);
870 /// size() - The length of this string.
872 intptr_t size() const { return StrEnd - StrBegin; }
874 /// begin() - Pointer to the first byte of the string.
876 const char *begin() const { return StrBegin; }
878 /// end() - Pointer to one byte past the end of the string.
880 const char *end() const { return StrEnd; }
882 /// getType() specialization - Type is always MetadataTy.
884 inline const Type *getType() const {
885 return Type::MetadataTy;
888 /// isNullValue - Return true if this is the value that would be returned by
889 /// getNullValue. This always returns false because getNullValue will never
890 /// produce metadata.
891 virtual bool isNullValue() const {
895 virtual void destroyConstant();
897 /// Methods for support type inquiry through isa, cast, and dyn_cast:
898 static inline bool classof(const MDString *) { return true; }
899 static bool classof(const Value *V) {
900 return V->getValueID() == MDStringVal;
904 } // End llvm namespace