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 /// isExactlyValue - We don't rely on operator== working on double values, as
280 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
281 /// As such, this method can be used to do an exact bit-for-bit comparison of
282 /// two floating point values. The version with a double operand is retained
283 /// because it's so convenient to write isExactlyValue(2.0), but please use
284 /// it only for simple constants.
285 bool isExactlyValue(const APFloat& V) const;
287 bool isExactlyValue(double V) const {
289 // convert is not supported on this type
290 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
293 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
294 return isExactlyValue(FV);
296 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static inline bool classof(const ConstantFP *) { return true; }
298 static bool classof(const Value *V) {
299 return V->getValueID() == ConstantFPVal;
303 //===----------------------------------------------------------------------===//
304 /// ConstantAggregateZero - All zero aggregate value
306 class ConstantAggregateZero : public Constant {
307 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
308 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
309 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
311 explicit ConstantAggregateZero(const Type *ty)
312 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
314 // allocate space for exactly zero operands
315 void *operator new(size_t s) {
316 return User::operator new(s, 0);
319 /// get() - static factory method for creating a null aggregate. It is
320 /// illegal to call this method with a non-aggregate type.
321 static ConstantAggregateZero *get(const Type *Ty);
323 /// isNullValue - Return true if this is the value that would be returned by
325 virtual bool isNullValue() const { return true; }
327 virtual void destroyConstant();
329 /// Methods for support type inquiry through isa, cast, and dyn_cast:
331 static bool classof(const ConstantAggregateZero *) { return true; }
332 static bool classof(const Value *V) {
333 return V->getValueID() == ConstantAggregateZeroVal;
338 //===----------------------------------------------------------------------===//
339 /// ConstantArray - Constant Array Declarations
341 class ConstantArray : public Constant {
342 friend struct ConstantCreator<ConstantArray, ArrayType,
343 std::vector<Constant*> >;
344 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
346 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
348 /// get() - Static factory methods - Return objects of the specified value
349 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
350 static Constant *get(const ArrayType *T,
351 Constant*const*Vals, unsigned NumVals) {
352 // FIXME: make this the primary ctor method.
353 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
356 /// This method constructs a ConstantArray and initializes it with a text
357 /// string. The default behavior (AddNull==true) causes a null terminator to
358 /// be placed at the end of the array. This effectively increases the length
359 /// of the array by one (you've been warned). However, in some situations
360 /// this is not desired so if AddNull==false then the string is copied without
361 /// null termination.
362 static Constant *get(const std::string &Initializer, bool AddNull = true);
364 /// Transparently provide more efficient getOperand methods.
365 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
367 /// getType - Specialize the getType() method to always return an ArrayType,
368 /// which reduces the amount of casting needed in parts of the compiler.
370 inline const ArrayType *getType() const {
371 return reinterpret_cast<const ArrayType*>(Value::getType());
374 /// isString - This method returns true if the array is an array of i8 and
375 /// the elements of the array are all ConstantInt's.
376 bool isString() const;
378 /// isCString - This method returns true if the array is a string (see
380 /// isString) and it ends in a null byte \0 and does not contains any other
382 /// null bytes except its terminator.
383 bool isCString(LLVMContext &Context) const;
385 /// getAsString - If this array is isString(), then this method converts the
386 /// array to an std::string and returns it. Otherwise, it asserts out.
388 std::string getAsString() const;
390 /// isNullValue - Return true if this is the value that would be returned by
391 /// getNullValue. This always returns false because zero arrays are always
392 /// created as ConstantAggregateZero objects.
393 virtual bool isNullValue() const { return false; }
395 virtual void destroyConstant();
396 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
398 /// Methods for support type inquiry through isa, cast, and dyn_cast:
399 static inline bool classof(const ConstantArray *) { return true; }
400 static bool classof(const Value *V) {
401 return V->getValueID() == ConstantArrayVal;
406 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
409 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
411 //===----------------------------------------------------------------------===//
412 // ConstantStruct - Constant Struct Declarations
414 class ConstantStruct : public Constant {
415 friend struct ConstantCreator<ConstantStruct, StructType,
416 std::vector<Constant*> >;
417 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
419 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
421 /// get() - Static factory methods - Return objects of the specified value
423 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
424 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
425 static Constant *get(Constant*const* Vals, unsigned NumVals,
426 bool Packed = false) {
427 // FIXME: make this the primary ctor method.
428 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
431 /// Transparently provide more efficient getOperand methods.
432 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
434 /// getType() specialization - Reduce amount of casting...
436 inline const StructType *getType() const {
437 return reinterpret_cast<const StructType*>(Value::getType());
440 /// isNullValue - Return true if this is the value that would be returned by
441 /// getNullValue. This always returns false because zero structs are always
442 /// created as ConstantAggregateZero objects.
443 virtual bool isNullValue() const {
447 virtual void destroyConstant();
448 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
450 /// Methods for support type inquiry through isa, cast, and dyn_cast:
451 static inline bool classof(const ConstantStruct *) { return true; }
452 static bool classof(const Value *V) {
453 return V->getValueID() == ConstantStructVal;
458 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
461 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
463 //===----------------------------------------------------------------------===//
464 /// ConstantVector - Constant Vector Declarations
466 class ConstantVector : public Constant {
467 friend struct ConstantCreator<ConstantVector, VectorType,
468 std::vector<Constant*> >;
469 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
471 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
473 /// get() - Static factory methods - Return objects of the specified value
474 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
475 static Constant *get(const std::vector<Constant*> &V);
476 static Constant *get(Constant*const* Vals, unsigned NumVals) {
477 // FIXME: make this the primary ctor method.
478 return get(std::vector<Constant*>(Vals, Vals+NumVals));
481 /// Transparently provide more efficient getOperand methods.
482 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
484 /// getType - Specialize the getType() method to always return a VectorType,
485 /// which reduces the amount of casting needed in parts of the compiler.
487 inline const VectorType *getType() const {
488 return reinterpret_cast<const VectorType*>(Value::getType());
491 /// @returns the value for a vector integer constant of the given type that
492 /// has all its bits set to true.
493 /// @brief Get the all ones value
494 static ConstantVector *getAllOnesValue(const VectorType *Ty);
496 /// isNullValue - Return true if this is the value that would be returned by
497 /// getNullValue. This always returns false because zero vectors are always
498 /// created as ConstantAggregateZero objects.
499 virtual bool isNullValue() const { return false; }
501 /// This function will return true iff every element in this vector constant
502 /// is set to all ones.
503 /// @returns true iff this constant's emements are all set to all ones.
504 /// @brief Determine if the value is all ones.
505 bool isAllOnesValue() const;
507 /// getSplatValue - If this is a splat constant, meaning that all of the
508 /// elements have the same value, return that value. Otherwise return NULL.
509 Constant *getSplatValue();
511 virtual void destroyConstant();
512 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
514 /// Methods for support type inquiry through isa, cast, and dyn_cast:
515 static inline bool classof(const ConstantVector *) { return true; }
516 static bool classof(const Value *V) {
517 return V->getValueID() == ConstantVectorVal;
522 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
525 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
527 //===----------------------------------------------------------------------===//
528 /// ConstantPointerNull - a constant pointer value that points to null
530 class ConstantPointerNull : public Constant {
531 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
532 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
533 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
535 explicit ConstantPointerNull(const PointerType *T)
536 : Constant(reinterpret_cast<const Type*>(T),
537 Value::ConstantPointerNullVal, 0, 0) {}
540 // allocate space for exactly zero operands
541 void *operator new(size_t s) {
542 return User::operator new(s, 0);
545 /// get() - Static factory methods - Return objects of the specified value
546 static ConstantPointerNull *get(const PointerType *T);
548 /// isNullValue - Return true if this is the value that would be returned by
550 virtual bool isNullValue() const { return true; }
552 virtual void destroyConstant();
554 /// getType - Specialize the getType() method to always return an PointerType,
555 /// which reduces the amount of casting needed in parts of the compiler.
557 inline const PointerType *getType() const {
558 return reinterpret_cast<const PointerType*>(Value::getType());
561 /// Methods for support type inquiry through isa, cast, and dyn_cast:
562 static inline bool classof(const ConstantPointerNull *) { return true; }
563 static bool classof(const Value *V) {
564 return V->getValueID() == ConstantPointerNullVal;
569 /// ConstantExpr - a constant value that is initialized with an expression using
570 /// other constant values.
572 /// This class uses the standard Instruction opcodes to define the various
573 /// constant expressions. The Opcode field for the ConstantExpr class is
574 /// maintained in the Value::SubclassData field.
575 class ConstantExpr : public Constant {
576 friend struct ConstantCreator<ConstantExpr,Type,
577 std::pair<unsigned, std::vector<Constant*> > >;
578 friend struct ConvertConstantType<ConstantExpr, Type>;
581 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
582 : Constant(ty, ConstantExprVal, Ops, NumOps) {
583 // Operation type (an Instruction opcode) is stored as the SubclassData.
584 SubclassData = Opcode;
587 // These private methods are used by the type resolution code to create
588 // ConstantExprs in intermediate forms.
589 static Constant *getTy(const Type *Ty, unsigned Opcode,
590 Constant *C1, Constant *C2);
591 static Constant *getCompareTy(unsigned short pred, Constant *C1,
593 static Constant *getSelectTy(const Type *Ty,
594 Constant *C1, Constant *C2, Constant *C3);
595 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
596 Value* const *Idxs, unsigned NumIdxs);
597 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
599 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
600 Constant *Elt, Constant *Idx);
601 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
602 Constant *V2, Constant *Mask);
603 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
604 const unsigned *Idxs, unsigned NumIdxs);
605 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
607 const unsigned *Idxs, unsigned NumIdxs);
610 // Static methods to construct a ConstantExpr of different kinds. Note that
611 // these methods may return a object that is not an instance of the
612 // ConstantExpr class, because they will attempt to fold the constant
613 // expression into something simpler if possible.
615 /// Cast constant expr
617 static Constant *getTrunc (Constant *C, const Type *Ty);
618 static Constant *getSExt (Constant *C, const Type *Ty);
619 static Constant *getZExt (Constant *C, const Type *Ty);
620 static Constant *getFPTrunc (Constant *C, const Type *Ty);
621 static Constant *getFPExtend(Constant *C, const Type *Ty);
622 static Constant *getUIToFP (Constant *C, const Type *Ty);
623 static Constant *getSIToFP (Constant *C, const Type *Ty);
624 static Constant *getFPToUI (Constant *C, const Type *Ty);
625 static Constant *getFPToSI (Constant *C, const Type *Ty);
626 static Constant *getPtrToInt(Constant *C, const Type *Ty);
627 static Constant *getIntToPtr(Constant *C, const Type *Ty);
628 static Constant *getBitCast (Constant *C, const Type *Ty);
630 /// Transparently provide more efficient getOperand methods.
631 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
633 // @brief Convenience function for getting one of the casting operations
634 // using a CastOps opcode.
635 static Constant *getCast(
636 unsigned ops, ///< The opcode for the conversion
637 Constant *C, ///< The constant to be converted
638 const Type *Ty ///< The type to which the constant is converted
641 // @brief Create a ZExt or BitCast cast constant expression
642 static Constant *getZExtOrBitCast(
643 Constant *C, ///< The constant to zext or bitcast
644 const Type *Ty ///< The type to zext or bitcast C to
647 // @brief Create a SExt or BitCast cast constant expression
648 static Constant *getSExtOrBitCast(
649 Constant *C, ///< The constant to sext or bitcast
650 const Type *Ty ///< The type to sext or bitcast C to
653 // @brief Create a Trunc or BitCast cast constant expression
654 static Constant *getTruncOrBitCast(
655 Constant *C, ///< The constant to trunc or bitcast
656 const Type *Ty ///< The type to trunc or bitcast C to
659 /// @brief Create a BitCast or a PtrToInt cast constant expression
660 static Constant *getPointerCast(
661 Constant *C, ///< The pointer value to be casted (operand 0)
662 const Type *Ty ///< The type to which cast should be made
665 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
666 static Constant *getIntegerCast(
667 Constant *C, ///< The integer constant to be casted
668 const Type *Ty, ///< The integer type to cast to
669 bool isSigned ///< Whether C should be treated as signed or not
672 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
673 static Constant *getFPCast(
674 Constant *C, ///< The integer constant to be casted
675 const Type *Ty ///< The integer type to cast to
678 /// @brief Return true if this is a convert constant expression
681 /// @brief Return true if this is a compare constant expression
682 bool isCompare() const;
684 /// @brief Return true if this is an insertvalue or extractvalue expression,
685 /// and the getIndices() method may be used.
686 bool hasIndices() const;
688 /// Select constant expr
690 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
691 return getSelectTy(V1->getType(), C, V1, V2);
694 /// ConstantExpr::get - Return a binary or shift operator constant expression,
695 /// folding if possible.
697 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
699 /// @brief Return an ICmp or FCmp comparison operator constant expression.
700 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
702 /// ConstantExpr::get* - Return some common constants without having to
703 /// specify the full Instruction::OPCODE identifier.
705 static Constant *getNot(Constant *C);
706 static Constant *getAdd(Constant *C1, Constant *C2);
707 static Constant *getFAdd(Constant *C1, Constant *C2);
708 static Constant *getSub(Constant *C1, Constant *C2);
709 static Constant *getFSub(Constant *C1, Constant *C2);
710 static Constant *getMul(Constant *C1, Constant *C2);
711 static Constant *getFMul(Constant *C1, Constant *C2);
712 static Constant *getUDiv(Constant *C1, Constant *C2);
713 static Constant *getSDiv(Constant *C1, Constant *C2);
714 static Constant *getFDiv(Constant *C1, Constant *C2);
715 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
716 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
717 static Constant *getFRem(Constant *C1, Constant *C2);
718 static Constant *getAnd(Constant *C1, Constant *C2);
719 static Constant *getOr(Constant *C1, Constant *C2);
720 static Constant *getXor(Constant *C1, Constant *C2);
721 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
722 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
723 static Constant *getShl(Constant *C1, Constant *C2);
724 static Constant *getLShr(Constant *C1, Constant *C2);
725 static Constant *getAShr(Constant *C1, Constant *C2);
727 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
728 /// all elements must be Constant's.
730 static Constant *getGetElementPtr(Constant *C,
731 Constant* const *IdxList, unsigned NumIdx);
732 static Constant *getGetElementPtr(Constant *C,
733 Value* const *IdxList, unsigned NumIdx);
735 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
736 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
737 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
738 static Constant *getExtractValue(Constant *Agg,
739 const unsigned *IdxList, unsigned NumIdx);
740 static Constant *getInsertValue(Constant *Agg, Constant *Val,
741 const unsigned *IdxList, unsigned NumIdx);
743 /// isNullValue - Return true if this is the value that would be returned by
745 virtual bool isNullValue() const { return false; }
747 /// getOpcode - Return the opcode at the root of this constant expression
748 unsigned getOpcode() const { return SubclassData; }
750 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
751 /// not an ICMP or FCMP constant expression.
752 unsigned getPredicate() const;
754 /// getIndices - Assert that this is an insertvalue or exactvalue
755 /// expression and return the list of indices.
756 const SmallVector<unsigned, 4> &getIndices() const;
758 /// getOpcodeName - Return a string representation for an opcode.
759 const char *getOpcodeName() const;
761 /// getWithOperandReplaced - Return a constant expression identical to this
762 /// one, but with the specified operand set to the specified value.
763 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
765 /// getWithOperands - This returns the current constant expression with the
766 /// operands replaced with the specified values. The specified operands must
767 /// match count and type with the existing ones.
768 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
769 return getWithOperands(&Ops[0], (unsigned)Ops.size());
771 Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
773 virtual void destroyConstant();
774 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
776 /// Methods for support type inquiry through isa, cast, and dyn_cast:
777 static inline bool classof(const ConstantExpr *) { return true; }
778 static inline bool classof(const Value *V) {
779 return V->getValueID() == ConstantExprVal;
784 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
787 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
789 //===----------------------------------------------------------------------===//
790 /// UndefValue - 'undef' values are things that do not have specified contents.
791 /// These are used for a variety of purposes, including global variable
792 /// initializers and operands to instructions. 'undef' values can occur with
795 class UndefValue : public Constant {
796 friend struct ConstantCreator<UndefValue, Type, char>;
797 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
798 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
800 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
802 // allocate space for exactly zero operands
803 void *operator new(size_t s) {
804 return User::operator new(s, 0);
807 /// get() - Static factory methods - Return an 'undef' object of the specified
810 static UndefValue *get(const Type *T);
812 /// isNullValue - Return true if this is the value that would be returned by
814 virtual bool isNullValue() const { return false; }
816 virtual void destroyConstant();
818 /// Methods for support type inquiry through isa, cast, and dyn_cast:
819 static inline bool classof(const UndefValue *) { return true; }
820 static bool classof(const Value *V) {
821 return V->getValueID() == UndefValueVal;
825 //===----------------------------------------------------------------------===//
826 /// MDString - a single uniqued string.
827 /// These are used to efficiently contain a byte sequence for metadata.
829 class MDString : public Constant {
830 MDString(const MDString &); // DO NOT IMPLEMENT
831 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
832 MDString(const char *begin, const char *end);
834 const char *StrBegin, *StrEnd;
836 // allocate space for exactly zero operands
837 void *operator new(size_t s) {
838 return User::operator new(s, 0);
841 /// get() - Static factory methods - Return objects of the specified value.
843 static MDString *get(const char *StrBegin, const char *StrEnd);
844 static MDString *get(const std::string &Str);
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 MetadataTy.
860 inline const Type *getType() const {
861 return Type::MetadataTy;
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 } // End llvm namespace