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/FoldingSet.h"
30 #include "llvm/ADT/SmallVector.h"
39 template<class ConstantClass, class TypeClass, class ValType>
40 struct ConstantCreator;
41 template<class ConstantClass, class TypeClass>
42 struct ConvertConstantType;
44 //===----------------------------------------------------------------------===//
45 /// This is the shared class of boolean and integer constants. This class
46 /// represents both boolean and integral constants.
47 /// @brief Class for constant integers.
48 class ConstantInt : public Constant {
49 static ConstantInt *TheTrueVal, *TheFalseVal;
50 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
51 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
52 ConstantInt(const IntegerType *Ty, const APInt& V);
55 // allocate space for exactly zero operands
56 void *operator new(size_t s) {
57 return User::operator new(s, 0);
60 /// Return the constant as an APInt value reference. This allows clients to
61 /// obtain a copy of the value, with all its precision in tact.
62 /// @brief Return the constant's value.
63 inline const APInt& getValue() const {
67 /// getBitWidth - Return the bitwidth of this constant.
68 unsigned getBitWidth() const { return Val.getBitWidth(); }
70 /// Return the constant as a 64-bit unsigned integer value after it
71 /// has been zero extended as appropriate for the type of this constant. Note
72 /// that this method can assert if the value does not fit in 64 bits.
74 /// @brief Return the zero extended value.
75 inline uint64_t getZExtValue() const {
76 return Val.getZExtValue();
79 /// Return the constant as a 64-bit integer value after it has been sign
80 /// extended as appropriate for the type of this constant. Note that
81 /// this method can assert if the value does not fit in 64 bits.
83 /// @brief Return the sign extended value.
84 inline int64_t getSExtValue() const {
85 return Val.getSExtValue();
88 /// A helper method that can be used to determine if the constant contained
89 /// within is equal to a constant. This only works for very small values,
90 /// because this is all that can be represented with all types.
91 /// @brief Determine if this constant's value is same as an unsigned char.
92 bool equalsInt(uint64_t V) const {
96 /// getTrue/getFalse - Return the singleton true/false values.
97 static inline ConstantInt *getTrue() {
98 if (TheTrueVal) return TheTrueVal;
99 return CreateTrueFalseVals(true);
101 static inline ConstantInt *getFalse() {
102 if (TheFalseVal) return TheFalseVal;
103 return CreateTrueFalseVals(false);
106 /// Return a ConstantInt with the specified value for the specified type. The
107 /// value V will be canonicalized to an unsigned APInt. Accessing it with
108 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
109 /// signed value for the type Ty.
110 /// @brief Get a ConstantInt for a specific value.
111 static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
113 /// Return a ConstantInt with the specified value for the specified type. The
114 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
115 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
116 /// signed value for the type Ty.
117 /// @brief Get a ConstantInt for a specific signed value.
118 static ConstantInt *getSigned(const Type *Ty, int64_t V) {
119 return get(Ty, V, true);
122 /// Return a ConstantInt with the specified value and an implied Type. The
123 /// type is the integer type that corresponds to the bit width of the value.
124 static ConstantInt *get(const APInt &V);
126 /// getType - Specialize the getType() method to always return an IntegerType,
127 /// which reduces the amount of casting needed in parts of the compiler.
129 inline const IntegerType *getType() const {
130 return reinterpret_cast<const IntegerType*>(Value::getType());
133 /// This static method returns true if the type Ty is big enough to
134 /// represent the value V. This can be used to avoid having the get method
135 /// assert when V is larger than Ty can represent. Note that there are two
136 /// versions of this method, one for unsigned and one for signed integers.
137 /// Although ConstantInt canonicalizes everything to an unsigned integer,
138 /// the signed version avoids callers having to convert a signed quantity
139 /// to the appropriate unsigned type before calling the method.
140 /// @returns true if V is a valid value for type Ty
141 /// @brief Determine if the value is in range for the given type.
142 static bool isValueValidForType(const Type *Ty, uint64_t V);
143 static bool isValueValidForType(const Type *Ty, int64_t V);
145 /// This function will return true iff this constant represents the "null"
146 /// value that would be returned by the getNullValue method.
147 /// @returns true if this is the null integer value.
148 /// @brief Determine if the value is null.
149 virtual bool isNullValue() const {
153 /// This is just a convenience method to make client code smaller for a
154 /// common code. It also correctly performs the comparison without the
155 /// potential for an assertion from getZExtValue().
156 bool isZero() const {
160 /// This is just a convenience method to make client code smaller for a
161 /// common case. It also correctly performs the comparison without the
162 /// potential for an assertion from getZExtValue().
163 /// @brief Determine if the value is one.
168 /// This function will return true iff every bit in this constant is set
170 /// @returns true iff this constant's bits are all set to true.
171 /// @brief Determine if the value is all ones.
172 bool isAllOnesValue() const {
173 return Val.isAllOnesValue();
176 /// This function will return true iff this constant represents the largest
177 /// value that may be represented by the constant's type.
178 /// @returns true iff this is the largest value that may be represented
180 /// @brief Determine if the value is maximal.
181 bool isMaxValue(bool isSigned) const {
183 return Val.isMaxSignedValue();
185 return Val.isMaxValue();
188 /// This function will return true iff this constant represents the smallest
189 /// value that may be represented by this constant's type.
190 /// @returns true if this is the smallest value that may be represented by
192 /// @brief Determine if the value is minimal.
193 bool isMinValue(bool isSigned) const {
195 return Val.isMinSignedValue();
197 return Val.isMinValue();
200 /// This function will return true iff this constant represents a value with
201 /// active bits bigger than 64 bits or a value greater than the given uint64_t
203 /// @returns true iff this constant is greater or equal to the given number.
204 /// @brief Determine if the value is greater or equal to the given number.
205 bool uge(uint64_t Num) {
206 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
209 /// getLimitedValue - If the value is smaller than the specified limit,
210 /// return it, otherwise return the limit value. This causes the value
211 /// to saturate to the limit.
212 /// @returns the min of the value of the constant and the specified value
213 /// @brief Get the constant's value with a saturation limit
214 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
215 return Val.getLimitedValue(Limit);
218 /// @returns the value for an integer constant of the given type that has all
219 /// its bits set to true.
220 /// @brief Get the all ones value
221 static ConstantInt *getAllOnesValue(const Type *Ty);
223 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
224 static inline bool classof(const ConstantInt *) { return true; }
225 static bool classof(const Value *V) {
226 return V->getValueID() == ConstantIntVal;
228 static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
230 static ConstantInt *CreateTrueFalseVals(bool WhichOne);
234 //===----------------------------------------------------------------------===//
235 /// ConstantFP - Floating Point Values [float, double]
237 class ConstantFP : public Constant {
239 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
240 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
242 ConstantFP(const Type *Ty, const APFloat& V);
244 // allocate space for exactly zero operands
245 void *operator new(size_t s) {
246 return User::operator new(s, 0);
249 /// get() - Static factory methods - Return objects of the specified value
250 static ConstantFP *get(const APFloat &V);
252 /// get() - This returns a constant fp for the specified value in the
253 /// specified type. This should only be used for simple constant values like
254 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
255 static ConstantFP *get(const Type *Ty, double V);
257 /// isValueValidForType - return true if Ty is big enough to represent V.
258 static bool isValueValidForType(const Type *Ty, const APFloat& V);
259 inline const APFloat& getValueAPF() const { return Val; }
261 /// isNullValue - Return true if this is the value that would be returned by
262 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
263 /// considers -0.0 to be null as well as 0.0. :(
264 virtual bool isNullValue() const;
266 // Get a negative zero.
267 static ConstantFP *getNegativeZero(const Type* Ty);
269 /// isExactlyValue - We don't rely on operator== working on double values, as
270 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
271 /// As such, this method can be used to do an exact bit-for-bit comparison of
272 /// two floating point values. The version with a double operand is retained
273 /// because it's so convenient to write isExactlyValue(2.0), but please use
274 /// it only for simple constants.
275 bool isExactlyValue(const APFloat& V) const;
277 bool isExactlyValue(double V) const {
279 // convert is not supported on this type
280 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
283 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
284 return isExactlyValue(FV);
286 /// Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const ConstantFP *) { return true; }
288 static bool classof(const Value *V) {
289 return V->getValueID() == ConstantFPVal;
293 //===----------------------------------------------------------------------===//
294 /// ConstantAggregateZero - All zero aggregate value
296 class ConstantAggregateZero : public Constant {
297 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
298 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
299 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
301 explicit ConstantAggregateZero(const Type *ty)
302 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
304 // allocate space for exactly zero operands
305 void *operator new(size_t s) {
306 return User::operator new(s, 0);
309 /// get() - static factory method for creating a null aggregate. It is
310 /// illegal to call this method with a non-aggregate type.
311 static ConstantAggregateZero *get(const Type *Ty);
313 /// isNullValue - Return true if this is the value that would be returned by
315 virtual bool isNullValue() const { return true; }
317 virtual void destroyConstant();
319 /// Methods for support type inquiry through isa, cast, and dyn_cast:
321 static bool classof(const ConstantAggregateZero *) { return true; }
322 static bool classof(const Value *V) {
323 return V->getValueID() == ConstantAggregateZeroVal;
328 //===----------------------------------------------------------------------===//
329 /// ConstantArray - Constant Array Declarations
331 class ConstantArray : public Constant {
332 friend struct ConstantCreator<ConstantArray, ArrayType,
333 std::vector<Constant*> >;
334 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
336 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
338 /// get() - Static factory methods - Return objects of the specified value
339 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
340 static Constant *get(const ArrayType *T,
341 Constant*const*Vals, unsigned NumVals) {
342 // FIXME: make this the primary ctor method.
343 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
346 /// This method constructs a ConstantArray and initializes it with a text
347 /// string. The default behavior (AddNull==true) causes a null terminator to
348 /// be placed at the end of the array. This effectively increases the length
349 /// of the array by one (you've been warned). However, in some situations
350 /// this is not desired so if AddNull==false then the string is copied without
351 /// null termination.
352 static Constant *get(const std::string &Initializer, bool AddNull = true);
354 /// Transparently provide more efficient getOperand methods.
355 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
357 /// getType - Specialize the getType() method to always return an ArrayType,
358 /// which reduces the amount of casting needed in parts of the compiler.
360 inline const ArrayType *getType() const {
361 return reinterpret_cast<const ArrayType*>(Value::getType());
364 /// isString - This method returns true if the array is an array of i8 and
365 /// the elements of the array are all ConstantInt's.
366 bool isString() const;
368 /// isCString - This method returns true if the array is a string (see
370 /// isString) and it ends in a null byte \0 and does not contains any other
372 /// null bytes except its terminator.
373 bool isCString() const;
375 /// getAsString - If this array is isString(), then this method converts the
376 /// array to an std::string and returns it. Otherwise, it asserts out.
378 std::string getAsString() const;
380 /// isNullValue - Return true if this is the value that would be returned by
381 /// getNullValue. This always returns false because zero arrays are always
382 /// created as ConstantAggregateZero objects.
383 virtual bool isNullValue() const { return false; }
385 virtual void destroyConstant();
386 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
388 /// Methods for support type inquiry through isa, cast, and dyn_cast:
389 static inline bool classof(const ConstantArray *) { return true; }
390 static bool classof(const Value *V) {
391 return V->getValueID() == ConstantArrayVal;
396 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
399 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
401 //===----------------------------------------------------------------------===//
402 // ConstantStruct - Constant Struct Declarations
404 class ConstantStruct : public Constant {
405 friend struct ConstantCreator<ConstantStruct, StructType,
406 std::vector<Constant*> >;
407 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
409 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
411 /// get() - Static factory methods - Return objects of the specified value
413 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
414 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
415 static Constant *get(Constant*const* Vals, unsigned NumVals,
416 bool Packed = false) {
417 // FIXME: make this the primary ctor method.
418 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
421 /// Transparently provide more efficient getOperand methods.
422 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
424 /// getType() specialization - Reduce amount of casting...
426 inline const StructType *getType() const {
427 return reinterpret_cast<const StructType*>(Value::getType());
430 /// isNullValue - Return true if this is the value that would be returned by
431 /// getNullValue. This always returns false because zero structs are always
432 /// created as ConstantAggregateZero objects.
433 virtual bool isNullValue() const {
437 virtual void destroyConstant();
438 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
440 /// Methods for support type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const ConstantStruct *) { return true; }
442 static bool classof(const Value *V) {
443 return V->getValueID() == ConstantStructVal;
448 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
451 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
453 //===----------------------------------------------------------------------===//
454 /// ConstantVector - Constant Vector Declarations
456 class ConstantVector : public Constant {
457 friend struct ConstantCreator<ConstantVector, VectorType,
458 std::vector<Constant*> >;
459 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
461 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
463 /// get() - Static factory methods - Return objects of the specified value
464 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
465 static Constant *get(const std::vector<Constant*> &V);
466 static Constant *get(Constant*const* Vals, unsigned NumVals) {
467 // FIXME: make this the primary ctor method.
468 return get(std::vector<Constant*>(Vals, Vals+NumVals));
471 /// Transparently provide more efficient getOperand methods.
472 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
474 /// getType - Specialize the getType() method to always return a VectorType,
475 /// which reduces the amount of casting needed in parts of the compiler.
477 inline const VectorType *getType() const {
478 return reinterpret_cast<const VectorType*>(Value::getType());
481 /// @returns the value for a vector integer constant of the given type that
482 /// has all its bits set to true.
483 /// @brief Get the all ones value
484 static ConstantVector *getAllOnesValue(const VectorType *Ty);
486 /// isNullValue - Return true if this is the value that would be returned by
487 /// getNullValue. This always returns false because zero vectors are always
488 /// created as ConstantAggregateZero objects.
489 virtual bool isNullValue() const { return false; }
491 /// This function will return true iff every element in this vector constant
492 /// is set to all ones.
493 /// @returns true iff this constant's emements are all set to all ones.
494 /// @brief Determine if the value is all ones.
495 bool isAllOnesValue() const;
497 /// getSplatValue - If this is a splat constant, meaning that all of the
498 /// elements have the same value, return that value. Otherwise return NULL.
499 Constant *getSplatValue();
501 virtual void destroyConstant();
502 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
504 /// Methods for support type inquiry through isa, cast, and dyn_cast:
505 static inline bool classof(const ConstantVector *) { return true; }
506 static bool classof(const Value *V) {
507 return V->getValueID() == ConstantVectorVal;
512 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
515 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
517 //===----------------------------------------------------------------------===//
518 /// ConstantPointerNull - a constant pointer value that points to null
520 class ConstantPointerNull : public Constant {
521 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
522 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
523 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
525 explicit ConstantPointerNull(const PointerType *T)
526 : Constant(reinterpret_cast<const Type*>(T),
527 Value::ConstantPointerNullVal, 0, 0) {}
530 // allocate space for exactly zero operands
531 void *operator new(size_t s) {
532 return User::operator new(s, 0);
535 /// get() - Static factory methods - Return objects of the specified value
536 static ConstantPointerNull *get(const PointerType *T);
538 /// isNullValue - Return true if this is the value that would be returned by
540 virtual bool isNullValue() const { return true; }
542 virtual void destroyConstant();
544 /// getType - Specialize the getType() method to always return an PointerType,
545 /// which reduces the amount of casting needed in parts of the compiler.
547 inline const PointerType *getType() const {
548 return reinterpret_cast<const PointerType*>(Value::getType());
551 /// Methods for support type inquiry through isa, cast, and dyn_cast:
552 static inline bool classof(const ConstantPointerNull *) { return true; }
553 static bool classof(const Value *V) {
554 return V->getValueID() == ConstantPointerNullVal;
559 /// ConstantExpr - a constant value that is initialized with an expression using
560 /// other constant values.
562 /// This class uses the standard Instruction opcodes to define the various
563 /// constant expressions. The Opcode field for the ConstantExpr class is
564 /// maintained in the Value::SubclassData field.
565 class ConstantExpr : public Constant {
566 friend struct ConstantCreator<ConstantExpr,Type,
567 std::pair<unsigned, std::vector<Constant*> > >;
568 friend struct ConvertConstantType<ConstantExpr, Type>;
571 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
572 : Constant(ty, ConstantExprVal, Ops, NumOps) {
573 // Operation type (an Instruction opcode) is stored as the SubclassData.
574 SubclassData = Opcode;
577 // These private methods are used by the type resolution code to create
578 // ConstantExprs in intermediate forms.
579 static Constant *getTy(const Type *Ty, unsigned Opcode,
580 Constant *C1, Constant *C2);
581 static Constant *getCompareTy(unsigned short pred, Constant *C1,
583 static Constant *getSelectTy(const Type *Ty,
584 Constant *C1, Constant *C2, Constant *C3);
585 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
586 Value* const *Idxs, unsigned NumIdxs);
587 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
589 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
590 Constant *Elt, Constant *Idx);
591 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
592 Constant *V2, Constant *Mask);
593 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
594 const unsigned *Idxs, unsigned NumIdxs);
595 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
597 const unsigned *Idxs, unsigned NumIdxs);
600 // Static methods to construct a ConstantExpr of different kinds. Note that
601 // these methods may return a object that is not an instance of the
602 // ConstantExpr class, because they will attempt to fold the constant
603 // expression into something simpler if possible.
605 /// Cast constant expr
607 static Constant *getTrunc (Constant *C, const Type *Ty);
608 static Constant *getSExt (Constant *C, const Type *Ty);
609 static Constant *getZExt (Constant *C, const Type *Ty);
610 static Constant *getFPTrunc (Constant *C, const Type *Ty);
611 static Constant *getFPExtend(Constant *C, const Type *Ty);
612 static Constant *getUIToFP (Constant *C, const Type *Ty);
613 static Constant *getSIToFP (Constant *C, const Type *Ty);
614 static Constant *getFPToUI (Constant *C, const Type *Ty);
615 static Constant *getFPToSI (Constant *C, const Type *Ty);
616 static Constant *getPtrToInt(Constant *C, const Type *Ty);
617 static Constant *getIntToPtr(Constant *C, const Type *Ty);
618 static Constant *getBitCast (Constant *C, const Type *Ty);
620 /// Transparently provide more efficient getOperand methods.
621 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
623 // @brief Convenience function for getting one of the casting operations
624 // using a CastOps opcode.
625 static Constant *getCast(
626 unsigned ops, ///< The opcode for the conversion
627 Constant *C, ///< The constant to be converted
628 const Type *Ty ///< The type to which the constant is converted
631 // @brief Create a ZExt or BitCast cast constant expression
632 static Constant *getZExtOrBitCast(
633 Constant *C, ///< The constant to zext or bitcast
634 const Type *Ty ///< The type to zext or bitcast C to
637 // @brief Create a SExt or BitCast cast constant expression
638 static Constant *getSExtOrBitCast(
639 Constant *C, ///< The constant to sext or bitcast
640 const Type *Ty ///< The type to sext or bitcast C to
643 // @brief Create a Trunc or BitCast cast constant expression
644 static Constant *getTruncOrBitCast(
645 Constant *C, ///< The constant to trunc or bitcast
646 const Type *Ty ///< The type to trunc or bitcast C to
649 /// @brief Create a BitCast or a PtrToInt cast constant expression
650 static Constant *getPointerCast(
651 Constant *C, ///< The pointer value to be casted (operand 0)
652 const Type *Ty ///< The type to which cast should be made
655 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
656 static Constant *getIntegerCast(
657 Constant *C, ///< The integer constant to be casted
658 const Type *Ty, ///< The integer type to cast to
659 bool isSigned ///< Whether C should be treated as signed or not
662 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
663 static Constant *getFPCast(
664 Constant *C, ///< The integer constant to be casted
665 const Type *Ty ///< The integer type to cast to
668 /// @brief Return true if this is a convert constant expression
671 /// @brief Return true if this is a compare constant expression
672 bool isCompare() const;
674 /// @brief Return true if this is an insertvalue or extractvalue expression,
675 /// and the getIndices() method may be used.
676 bool hasIndices() const;
678 /// Select constant expr
680 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
681 return getSelectTy(V1->getType(), C, V1, V2);
684 /// getSizeOf constant expr - computes the size of a type in a target
685 /// independent way (Note: the return type is an i64).
687 static Constant *getSizeOf(const Type *Ty);
689 /// ConstantExpr::get - Return a binary or shift operator constant expression,
690 /// folding if possible.
692 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
694 /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
696 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
698 /// ConstantExpr::get* - Return some common constants without having to
699 /// specify the full Instruction::OPCODE identifier.
701 static Constant *getNeg(Constant *C);
702 static Constant *getNot(Constant *C);
703 static Constant *getAdd(Constant *C1, Constant *C2);
704 static Constant *getSub(Constant *C1, Constant *C2);
705 static Constant *getMul(Constant *C1, Constant *C2);
706 static Constant *getUDiv(Constant *C1, Constant *C2);
707 static Constant *getSDiv(Constant *C1, Constant *C2);
708 static Constant *getFDiv(Constant *C1, Constant *C2);
709 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
710 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
711 static Constant *getFRem(Constant *C1, Constant *C2);
712 static Constant *getAnd(Constant *C1, Constant *C2);
713 static Constant *getOr(Constant *C1, Constant *C2);
714 static Constant *getXor(Constant *C1, Constant *C2);
715 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
716 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
717 static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
718 static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
719 static Constant *getShl(Constant *C1, Constant *C2);
720 static Constant *getLShr(Constant *C1, Constant *C2);
721 static Constant *getAShr(Constant *C1, Constant *C2);
723 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
724 /// all elements must be Constant's.
726 static Constant *getGetElementPtr(Constant *C,
727 Constant* const *IdxList, unsigned NumIdx);
728 static Constant *getGetElementPtr(Constant *C,
729 Value* const *IdxList, unsigned NumIdx);
731 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
732 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
733 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
734 static Constant *getExtractValue(Constant *Agg,
735 const unsigned *IdxList, unsigned NumIdx);
736 static Constant *getInsertValue(Constant *Agg, Constant *Val,
737 const unsigned *IdxList, unsigned NumIdx);
739 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
740 /// method returns the negative zero constant for floating point or vector
741 /// floating point types; for all other types, it returns the null value.
742 static Constant *getZeroValueForNegationExpr(const Type *Ty);
744 /// isNullValue - Return true if this is the value that would be returned by
746 virtual bool isNullValue() const { return false; }
748 /// getOpcode - Return the opcode at the root of this constant expression
749 unsigned getOpcode() const { return SubclassData; }
751 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
752 /// not an ICMP or FCMP constant expression.
753 unsigned getPredicate() const;
755 /// getIndices - Assert that this is an insertvalue or exactvalue
756 /// expression and return the list of indices.
757 const SmallVector<unsigned, 4> &getIndices() const;
759 /// getOpcodeName - Return a string representation for an opcode.
760 const char *getOpcodeName() const;
762 /// getWithOperandReplaced - Return a constant expression identical to this
763 /// one, but with the specified operand set to the specified value.
764 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
766 /// getWithOperands - This returns the current constant expression with the
767 /// operands replaced with the specified values. The specified operands must
768 /// match count and type with the existing ones.
769 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
770 return getWithOperands(&Ops[0], (unsigned)Ops.size());
772 Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
774 virtual void destroyConstant();
775 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
777 /// Methods for support type inquiry through isa, cast, and dyn_cast:
778 static inline bool classof(const ConstantExpr *) { return true; }
779 static inline bool classof(const Value *V) {
780 return V->getValueID() == ConstantExprVal;
785 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
788 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
790 //===----------------------------------------------------------------------===//
791 /// UndefValue - 'undef' values are things that do not have specified contents.
792 /// These are used for a variety of purposes, including global variable
793 /// initializers and operands to instructions. 'undef' values can occur with
796 class UndefValue : public Constant {
797 friend struct ConstantCreator<UndefValue, Type, char>;
798 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
799 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
801 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
803 // allocate space for exactly zero operands
804 void *operator new(size_t s) {
805 return User::operator new(s, 0);
808 /// get() - Static factory methods - Return an 'undef' object of the specified
811 static UndefValue *get(const Type *T);
813 /// isNullValue - Return true if this is the value that would be returned by
815 virtual bool isNullValue() const { return false; }
817 virtual void destroyConstant();
819 /// Methods for support type inquiry through isa, cast, and dyn_cast:
820 static inline bool classof(const UndefValue *) { return true; }
821 static bool classof(const Value *V) {
822 return V->getValueID() == UndefValueVal;
826 //===----------------------------------------------------------------------===//
827 /// MDString - a single uniqued string.
828 /// These are used to efficiently contain a byte sequence for metadata.
830 class MDString : public Constant {
831 MDString(const MDString &); // DO NOT IMPLEMENT
832 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
833 MDString(const char *begin, const char *end);
835 const char *StrBegin, *StrEnd;
837 // allocate space for exactly zero operands
838 void *operator new(size_t s) {
839 return User::operator new(s, 0);
842 /// get() - Static factory methods - Return objects of the specified value.
844 static MDString *get(const char *StrBegin, const char *StrEnd);
846 /// size() - The length of this string.
848 intptr_t size() const { return StrEnd - StrBegin; }
850 /// begin() - Pointer to the first byte of the string.
852 const char *begin() const { return StrBegin; }
854 /// end() - Pointer to one byte past the end of the string.
856 const char *end() const { return StrEnd; }
858 /// getType() specialization - Type is always an empty struct.
860 inline const Type *getType() const {
861 return Type::EmptyStructTy;
864 /// isNullValue - Return true if this is the value that would be returned by
865 /// getNullValue. This always returns false because getNullValue will never
866 /// produce metadata.
867 virtual bool isNullValue() const {
871 virtual void destroyConstant();
873 /// Methods for support type inquiry through isa, cast, and dyn_cast:
874 static inline bool classof(const MDString *) { return true; }
875 static bool classof(const Value *V) {
876 return V->getValueID() == MDStringVal;
880 //===----------------------------------------------------------------------===//
881 /// MDNode - a tuple of other values.
882 /// These contain a list of the Constants that represent the metadata.
884 class MDNode : public Constant, public FoldingSetNode {
885 MDNode(const MDNode &); // DO NOT IMPLEMENT
887 explicit MDNode(Constant*const* Vals, unsigned NumVals);
889 /// get() - Static factory methods - Return objects of the specified value.
891 static MDNode *get(Constant*const* Vals, unsigned NumVals);
893 // Transparently provide more efficient getOperand methods.
894 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
896 /// getType() specialization - Type is always an empty struct.
898 inline const Type *getType() const {
899 return Type::EmptyStructTy;
902 /// isNullValue - Return true if this is the value that would be returned by
903 /// getNullValue. This always returns false because getNullValue will never
904 /// produce metadata.
905 virtual bool isNullValue() const {
909 /// Profile - calculate a unique identifier for this MDNode to collapse
911 void Profile(FoldingSetNodeID &ID);
913 virtual void destroyConstant();
914 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
916 /// Methods for support type inquiry through isa, cast, and dyn_cast:
917 static inline bool classof(const MDNode *) { return true; }
918 static bool classof(const Value *V) {
919 return V->getValueID() == MDNodeVal;
924 struct OperandTraits<MDNode> : VariadicOperandTraits<> {
927 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(MDNode, Constant)
929 } // End llvm namespace