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 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
231 static inline bool classof(const ConstantInt *) { return true; }
232 static bool classof(const Value *V) {
233 return V->getValueID() == ConstantIntVal;
235 static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
237 static ConstantInt *CreateTrueFalseVals(bool WhichOne);
241 //===----------------------------------------------------------------------===//
242 /// ConstantFP - Floating Point Values [float, double]
244 class ConstantFP : public Constant {
246 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
247 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
249 ConstantFP(const Type *Ty, const APFloat& V);
251 // allocate space for exactly zero operands
252 void *operator new(size_t s) {
253 return User::operator new(s, 0);
256 /// get() - Static factory methods - Return objects of the specified value
257 static ConstantFP *get(const APFloat &V);
259 /// isValueValidForType - return true if Ty is big enough to represent V.
260 static bool isValueValidForType(const Type *Ty, const APFloat& V);
261 inline const APFloat& getValueAPF() const { return Val; }
263 /// isNullValue - Return true if this is the value that would be returned by
264 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
265 /// considers -0.0 to be null as well as 0.0. :(
266 virtual bool isNullValue() const;
268 /// isNegativeZeroValue - Return true if the value is what would be returned
269 /// by getZeroValueForNegation.
270 virtual bool isNegativeZeroValue() const {
271 return Val.isZero() && Val.isNegative();
274 /// isExactlyValue - We don't rely on operator== working on double values, as
275 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
276 /// As such, this method can be used to do an exact bit-for-bit comparison of
277 /// two floating point values. The version with a double operand is retained
278 /// because it's so convenient to write isExactlyValue(2.0), but please use
279 /// it only for simple constants.
280 bool isExactlyValue(const APFloat& V) const;
282 bool isExactlyValue(double V) const {
284 // convert is not supported on this type
285 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
288 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
289 return isExactlyValue(FV);
291 /// Methods for support type inquiry through isa, cast, and dyn_cast:
292 static inline bool classof(const ConstantFP *) { return true; }
293 static bool classof(const Value *V) {
294 return V->getValueID() == ConstantFPVal;
298 //===----------------------------------------------------------------------===//
299 /// ConstantAggregateZero - All zero aggregate value
301 class ConstantAggregateZero : public Constant {
302 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
303 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
304 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
306 explicit ConstantAggregateZero(const Type *ty)
307 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
309 // allocate space for exactly zero operands
310 void *operator new(size_t s) {
311 return User::operator new(s, 0);
314 /// get() - static factory method for creating a null aggregate. It is
315 /// illegal to call this method with a non-aggregate type.
316 static ConstantAggregateZero *get(const Type *Ty);
318 /// isNullValue - Return true if this is the value that would be returned by
320 virtual bool isNullValue() const { return true; }
322 virtual void destroyConstant();
324 /// Methods for support type inquiry through isa, cast, and dyn_cast:
326 static bool classof(const ConstantAggregateZero *) { return true; }
327 static bool classof(const Value *V) {
328 return V->getValueID() == ConstantAggregateZeroVal;
333 //===----------------------------------------------------------------------===//
334 /// ConstantArray - Constant Array Declarations
336 class ConstantArray : public Constant {
337 friend struct ConstantCreator<ConstantArray, ArrayType,
338 std::vector<Constant*> >;
339 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
341 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
343 /// get() - Static factory methods - Return objects of the specified value
344 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
345 static Constant *get(const ArrayType *T,
346 Constant*const*Vals, unsigned NumVals) {
347 // FIXME: make this the primary ctor method.
348 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
351 /// This method constructs a ConstantArray and initializes it with a text
352 /// string. The default behavior (AddNull==true) causes a null terminator to
353 /// be placed at the end of the array. This effectively increases the length
354 /// of the array by one (you've been warned). However, in some situations
355 /// this is not desired so if AddNull==false then the string is copied without
356 /// null termination.
357 static Constant *get(const std::string &Initializer, bool AddNull = true);
359 /// Transparently provide more efficient getOperand methods.
360 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
362 /// getType - Specialize the getType() method to always return an ArrayType,
363 /// which reduces the amount of casting needed in parts of the compiler.
365 inline const ArrayType *getType() const {
366 return reinterpret_cast<const ArrayType*>(Value::getType());
369 /// isString - This method returns true if the array is an array of i8 and
370 /// the elements of the array are all ConstantInt's.
371 bool isString() const;
373 /// isCString - This method returns true if the array is a string (see
375 /// isString) and it ends in a null byte \0 and does not contains any other
377 /// null bytes except its terminator.
378 bool isCString() const;
380 /// getAsString - If this array is isString(), then this method converts the
381 /// array to an std::string and returns it. Otherwise, it asserts out.
383 std::string getAsString() const;
385 /// isNullValue - Return true if this is the value that would be returned by
386 /// getNullValue. This always returns false because zero arrays are always
387 /// created as ConstantAggregateZero objects.
388 virtual bool isNullValue() const { return false; }
390 virtual void destroyConstant();
391 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
393 /// Methods for support type inquiry through isa, cast, and dyn_cast:
394 static inline bool classof(const ConstantArray *) { return true; }
395 static bool classof(const Value *V) {
396 return V->getValueID() == ConstantArrayVal;
401 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
404 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
406 //===----------------------------------------------------------------------===//
407 // ConstantStruct - Constant Struct Declarations
409 class ConstantStruct : public Constant {
410 friend struct ConstantCreator<ConstantStruct, StructType,
411 std::vector<Constant*> >;
412 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
414 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
416 /// get() - Static factory methods - Return objects of the specified value
418 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
419 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
420 static Constant *get(Constant*const* Vals, unsigned NumVals,
421 bool Packed = false) {
422 // FIXME: make this the primary ctor method.
423 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
426 /// Transparently provide more efficient getOperand methods.
427 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
429 /// getType() specialization - Reduce amount of casting...
431 inline const StructType *getType() const {
432 return reinterpret_cast<const StructType*>(Value::getType());
435 /// isNullValue - Return true if this is the value that would be returned by
436 /// getNullValue. This always returns false because zero structs are always
437 /// created as ConstantAggregateZero objects.
438 virtual bool isNullValue() const {
442 virtual void destroyConstant();
443 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
445 /// Methods for support type inquiry through isa, cast, and dyn_cast:
446 static inline bool classof(const ConstantStruct *) { return true; }
447 static bool classof(const Value *V) {
448 return V->getValueID() == ConstantStructVal;
453 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
456 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
458 //===----------------------------------------------------------------------===//
459 /// ConstantVector - Constant Vector Declarations
461 class ConstantVector : public Constant {
462 friend struct ConstantCreator<ConstantVector, VectorType,
463 std::vector<Constant*> >;
464 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
466 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
468 /// get() - Static factory methods - Return objects of the specified value
469 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
470 static Constant *get(const std::vector<Constant*> &V);
471 static Constant *get(Constant*const* Vals, unsigned NumVals) {
472 // FIXME: make this the primary ctor method.
473 return get(std::vector<Constant*>(Vals, Vals+NumVals));
476 /// Transparently provide more efficient getOperand methods.
477 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
479 /// getType - Specialize the getType() method to always return a VectorType,
480 /// which reduces the amount of casting needed in parts of the compiler.
482 inline const VectorType *getType() const {
483 return reinterpret_cast<const VectorType*>(Value::getType());
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 /// ConstantExpr::get - Return a binary or shift operator constant expression,
685 /// folding if possible.
687 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
689 /// @brief Return an ICmp or FCmp comparison operator constant expression.
690 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
692 /// ConstantExpr::get* - Return some common constants without having to
693 /// specify the full Instruction::OPCODE identifier.
695 static Constant *getAdd(Constant *C1, Constant *C2);
696 static Constant *getFAdd(Constant *C1, Constant *C2);
697 static Constant *getSub(Constant *C1, Constant *C2);
698 static Constant *getFSub(Constant *C1, Constant *C2);
699 static Constant *getMul(Constant *C1, Constant *C2);
700 static Constant *getFMul(Constant *C1, Constant *C2);
701 static Constant *getUDiv(Constant *C1, Constant *C2);
702 static Constant *getSDiv(Constant *C1, Constant *C2);
703 static Constant *getFDiv(Constant *C1, Constant *C2);
704 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
705 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
706 static Constant *getFRem(Constant *C1, Constant *C2);
707 static Constant *getAnd(Constant *C1, Constant *C2);
708 static Constant *getOr(Constant *C1, Constant *C2);
709 static Constant *getXor(Constant *C1, Constant *C2);
710 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
711 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
712 static Constant *getShl(Constant *C1, Constant *C2);
713 static Constant *getLShr(Constant *C1, Constant *C2);
714 static Constant *getAShr(Constant *C1, Constant *C2);
716 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
717 /// all elements must be Constant's.
719 static Constant *getGetElementPtr(Constant *C,
720 Constant* const *IdxList, unsigned NumIdx);
721 static Constant *getGetElementPtr(Constant *C,
722 Value* const *IdxList, unsigned NumIdx);
724 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
725 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
726 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
727 static Constant *getExtractValue(Constant *Agg,
728 const unsigned *IdxList, unsigned NumIdx);
729 static Constant *getInsertValue(Constant *Agg, Constant *Val,
730 const unsigned *IdxList, unsigned NumIdx);
732 /// isNullValue - Return true if this is the value that would be returned by
734 virtual bool isNullValue() const { return false; }
736 /// getOpcode - Return the opcode at the root of this constant expression
737 unsigned getOpcode() const { return SubclassData; }
739 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
740 /// not an ICMP or FCMP constant expression.
741 unsigned getPredicate() const;
743 /// getIndices - Assert that this is an insertvalue or exactvalue
744 /// expression and return the list of indices.
745 const SmallVector<unsigned, 4> &getIndices() const;
747 /// getOpcodeName - Return a string representation for an opcode.
748 const char *getOpcodeName() const;
750 /// getWithOperandReplaced - Return a constant expression identical to this
751 /// one, but with the specified operand set to the specified value.
752 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
754 /// getWithOperands - This returns the current constant expression with the
755 /// operands replaced with the specified values. The specified operands must
756 /// match count and type with the existing ones.
757 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
758 return getWithOperands(&Ops[0], (unsigned)Ops.size());
760 Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
762 virtual void destroyConstant();
763 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
765 /// Methods for support type inquiry through isa, cast, and dyn_cast:
766 static inline bool classof(const ConstantExpr *) { return true; }
767 static inline bool classof(const Value *V) {
768 return V->getValueID() == ConstantExprVal;
773 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
776 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
778 //===----------------------------------------------------------------------===//
779 /// UndefValue - 'undef' values are things that do not have specified contents.
780 /// These are used for a variety of purposes, including global variable
781 /// initializers and operands to instructions. 'undef' values can occur with
784 class UndefValue : public Constant {
785 friend struct ConstantCreator<UndefValue, Type, char>;
786 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
787 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
789 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
791 // allocate space for exactly zero operands
792 void *operator new(size_t s) {
793 return User::operator new(s, 0);
796 /// get() - Static factory methods - Return an 'undef' object of the specified
799 static UndefValue *get(const Type *T);
801 /// isNullValue - Return true if this is the value that would be returned by
803 virtual bool isNullValue() const { return false; }
805 virtual void destroyConstant();
807 /// Methods for support type inquiry through isa, cast, and dyn_cast:
808 static inline bool classof(const UndefValue *) { return true; }
809 static bool classof(const Value *V) {
810 return V->getValueID() == UndefValueVal;
814 //===----------------------------------------------------------------------===//
815 /// MDString - a single uniqued string.
816 /// These are used to efficiently contain a byte sequence for metadata.
818 class MDString : public Constant {
819 MDString(const MDString &); // DO NOT IMPLEMENT
820 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
821 MDString(const char *begin, const char *end);
823 const char *StrBegin, *StrEnd;
825 // allocate space for exactly zero operands
826 void *operator new(size_t s) {
827 return User::operator new(s, 0);
830 /// get() - Static factory methods - Return objects of the specified value.
832 static MDString *get(const char *StrBegin, const char *StrEnd);
833 static MDString *get(const std::string &Str);
835 /// size() - The length of this string.
837 intptr_t size() const { return StrEnd - StrBegin; }
839 /// begin() - Pointer to the first byte of the string.
841 const char *begin() const { return StrBegin; }
843 /// end() - Pointer to one byte past the end of the string.
845 const char *end() const { return StrEnd; }
847 /// getType() specialization - Type is always MetadataTy.
849 inline const Type *getType() const {
850 return Type::MetadataTy;
853 /// isNullValue - Return true if this is the value that would be returned by
854 /// getNullValue. This always returns false because getNullValue will never
855 /// produce metadata.
856 virtual bool isNullValue() const {
860 virtual void destroyConstant();
862 /// Methods for support type inquiry through isa, cast, and dyn_cast:
863 static inline bool classof(const MDString *) { return true; }
864 static bool classof(const Value *V) {
865 return V->getValueID() == MDStringVal;
869 } // End llvm namespace