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/OperandTraits.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/ArrayRef.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 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
49 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
50 ConstantInt(const IntegerType *Ty, const APInt& V);
53 // allocate space for exactly zero operands
54 void *operator new(size_t s) {
55 return User::operator new(s, 0);
58 static ConstantInt *getTrue(LLVMContext &Context);
59 static ConstantInt *getFalse(LLVMContext &Context);
61 /// If Ty is a vector type, return a Constant with a splat of the given
62 /// value. Otherwise return a ConstantInt for the given value.
63 static Constant *get(const Type *Ty, uint64_t V, bool isSigned = false);
65 /// Return a ConstantInt with the specified integer value for the specified
66 /// type. If the type is wider than 64 bits, the value will be zero-extended
67 /// to fit the type, unless isSigned is true, in which case the value will
68 /// be interpreted as a 64-bit signed integer and sign-extended to fit
70 /// @brief Get a ConstantInt for a specific value.
71 static ConstantInt *get(const IntegerType *Ty, uint64_t V,
72 bool isSigned = false);
74 /// Return a ConstantInt with the specified value for the specified type. The
75 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
76 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
77 /// signed value for the type Ty.
78 /// @brief Get a ConstantInt for a specific signed value.
79 static ConstantInt *getSigned(const IntegerType *Ty, int64_t V);
80 static Constant *getSigned(const Type *Ty, int64_t V);
82 /// Return a ConstantInt with the specified value and an implied Type. The
83 /// type is the integer type that corresponds to the bit width of the value.
84 static ConstantInt *get(LLVMContext &Context, const APInt &V);
86 /// Return a ConstantInt constructed from the string strStart with the given
88 static ConstantInt *get(const IntegerType *Ty, StringRef Str,
91 /// If Ty is a vector type, return a Constant with a splat of the given
92 /// value. Otherwise return a ConstantInt for the given value.
93 static Constant *get(const Type* Ty, const APInt& V);
95 /// Return the constant as an APInt value reference. This allows clients to
96 /// obtain a copy of the value, with all its precision in tact.
97 /// @brief Return the constant's value.
98 inline const APInt &getValue() const {
102 /// getBitWidth - Return the bitwidth of this constant.
103 unsigned getBitWidth() const { return Val.getBitWidth(); }
105 /// Return the constant as a 64-bit unsigned integer value after it
106 /// has been zero extended as appropriate for the type of this constant. Note
107 /// that this method can assert if the value does not fit in 64 bits.
109 /// @brief Return the zero extended value.
110 inline uint64_t getZExtValue() const {
111 return Val.getZExtValue();
114 /// Return the constant as a 64-bit integer value after it has been sign
115 /// extended as appropriate for the type of this constant. Note that
116 /// this method can assert if the value does not fit in 64 bits.
118 /// @brief Return the sign extended value.
119 inline int64_t getSExtValue() const {
120 return Val.getSExtValue();
123 /// A helper method that can be used to determine if the constant contained
124 /// within is equal to a constant. This only works for very small values,
125 /// because this is all that can be represented with all types.
126 /// @brief Determine if this constant's value is same as an unsigned char.
127 bool equalsInt(uint64_t V) const {
131 /// getType - Specialize the getType() method to always return an IntegerType,
132 /// which reduces the amount of casting needed in parts of the compiler.
134 inline const IntegerType *getType() const {
135 return reinterpret_cast<const IntegerType*>(Value::getType());
138 /// This static method returns true if the type Ty is big enough to
139 /// represent the value V. This can be used to avoid having the get method
140 /// assert when V is larger than Ty can represent. Note that there are two
141 /// versions of this method, one for unsigned and one for signed integers.
142 /// Although ConstantInt canonicalizes everything to an unsigned integer,
143 /// the signed version avoids callers having to convert a signed quantity
144 /// to the appropriate unsigned type before calling the method.
145 /// @returns true if V is a valid value for type Ty
146 /// @brief Determine if the value is in range for the given type.
147 static bool isValueValidForType(const Type *Ty, uint64_t V);
148 static bool isValueValidForType(const Type *Ty, int64_t V);
150 /// This function will return true iff this constant represents the "null"
151 /// value that would be returned by the getNullValue method.
152 /// @returns true if this is the null integer value.
153 /// @brief Determine if the value is null.
154 virtual bool isNullValue() const {
158 /// This is just a convenience method to make client code smaller for a
159 /// common code. It also correctly performs the comparison without the
160 /// potential for an assertion from getZExtValue().
161 bool isZero() const {
165 /// This is just a convenience method to make client code smaller for a
166 /// common case. It also correctly performs the comparison without the
167 /// potential for an assertion from getZExtValue().
168 /// @brief Determine if the value is one.
173 /// This function will return true iff every bit in this constant is set
175 /// @returns true iff this constant's bits are all set to true.
176 /// @brief Determine if the value is all ones.
177 bool isAllOnesValue() const {
178 return Val.isAllOnesValue();
181 /// This function will return true iff this constant represents the largest
182 /// value that may be represented by the constant's type.
183 /// @returns true iff this is the largest value that may be represented
185 /// @brief Determine if the value is maximal.
186 bool isMaxValue(bool isSigned) const {
188 return Val.isMaxSignedValue();
190 return Val.isMaxValue();
193 /// This function will return true iff this constant represents the smallest
194 /// value that may be represented by this constant's type.
195 /// @returns true if this is the smallest value that may be represented by
197 /// @brief Determine if the value is minimal.
198 bool isMinValue(bool isSigned) const {
200 return Val.isMinSignedValue();
202 return Val.isMinValue();
205 /// This function will return true iff this constant represents a value with
206 /// active bits bigger than 64 bits or a value greater than the given uint64_t
208 /// @returns true iff this constant is greater or equal to the given number.
209 /// @brief Determine if the value is greater or equal to the given number.
210 bool uge(uint64_t Num) {
211 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
214 /// getLimitedValue - If the value is smaller than the specified limit,
215 /// return it, otherwise return the limit value. This causes the value
216 /// to saturate to the limit.
217 /// @returns the min of the value of the constant and the specified value
218 /// @brief Get the constant's value with a saturation limit
219 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
220 return Val.getLimitedValue(Limit);
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;
231 //===----------------------------------------------------------------------===//
232 /// ConstantFP - Floating Point Values [float, double]
234 class ConstantFP : public Constant {
236 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
237 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
238 friend class LLVMContextImpl;
240 ConstantFP(const Type *Ty, const APFloat& V);
242 // allocate space for exactly zero operands
243 void *operator new(size_t s) {
244 return User::operator new(s, 0);
247 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
248 /// method returns the negative zero constant for floating point or vector
249 /// floating point types; for all other types, it returns the null value.
250 static Constant *getZeroValueForNegation(const Type *Ty);
252 /// get() - This returns a ConstantFP, or a vector containing a splat of a
253 /// ConstantFP, for the specified value in the specified type. This should
254 /// only be used for simple constant values like 2.0/1.0 etc, that are
255 /// known-valid both as host double and as the target format.
256 static Constant *get(const Type* Ty, double V);
257 static Constant *get(const Type* Ty, StringRef Str);
258 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
259 static ConstantFP *getNegativeZero(const Type* Ty);
260 static ConstantFP *getInfinity(const Type *Ty, bool Negative = false);
262 /// isValueValidForType - return true if Ty is big enough to represent V.
263 static bool isValueValidForType(const Type *Ty, const APFloat &V);
264 inline const APFloat& getValueAPF() const { return Val; }
266 /// isNullValue - Return true if this is the value that would be returned by
267 /// getNullValue. For ConstantFP, this is +0.0, but not -0.0. To handle the
268 /// two the same, use isZero().
269 virtual bool isNullValue() const;
271 /// isNegativeZeroValue - Return true if the value is what would be returned
272 /// by getZeroValueForNegation.
273 virtual bool isNegativeZeroValue() const {
274 return Val.isZero() && Val.isNegative();
277 /// isZero - Return true if the value is positive or negative zero.
278 bool isZero() const { return Val.isZero(); }
280 /// isNaN - Return true if the value is a NaN.
281 bool isNaN() const { return Val.isNaN(); }
283 /// isExactlyValue - We don't rely on operator== working on double values, as
284 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
285 /// As such, this method can be used to do an exact bit-for-bit comparison of
286 /// two floating point values. The version with a double operand is retained
287 /// because it's so convenient to write isExactlyValue(2.0), but please use
288 /// it only for simple constants.
289 bool isExactlyValue(const APFloat &V) const;
291 bool isExactlyValue(double V) const {
293 // convert is not supported on this type
294 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
297 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
298 return isExactlyValue(FV);
300 /// Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool classof(const ConstantFP *) { return true; }
302 static bool classof(const Value *V) {
303 return V->getValueID() == ConstantFPVal;
307 //===----------------------------------------------------------------------===//
308 /// ConstantAggregateZero - All zero aggregate value
310 class ConstantAggregateZero : public Constant {
311 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
312 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
313 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
315 explicit ConstantAggregateZero(const Type *ty)
316 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
318 // allocate space for exactly zero operands
319 void *operator new(size_t s) {
320 return User::operator new(s, 0);
323 static ConstantAggregateZero* get(const Type *Ty);
325 /// isNullValue - Return true if this is the value that would be returned by
327 virtual bool isNullValue() const { return true; }
329 virtual void destroyConstant();
331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
333 static bool classof(const ConstantAggregateZero *) { return true; }
334 static bool classof(const Value *V) {
335 return V->getValueID() == ConstantAggregateZeroVal;
340 //===----------------------------------------------------------------------===//
341 /// ConstantArray - Constant Array Declarations
343 class ConstantArray : public Constant {
344 friend struct ConstantCreator<ConstantArray, ArrayType,
345 std::vector<Constant*> >;
346 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
348 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
350 // ConstantArray accessors
351 static Constant *get(const ArrayType *T, const std::vector<Constant*> &V);
352 static Constant *get(const ArrayType *T, Constant *const *Vals,
355 /// This method constructs a ConstantArray and initializes it with a text
356 /// string. The default behavior (AddNull==true) causes a null terminator to
357 /// be placed at the end of the array. This effectively increases the length
358 /// of the array by one (you've been warned). However, in some situations
359 /// this is not desired so if AddNull==false then the string is copied without
360 /// null termination.
361 static Constant *get(LLVMContext &Context, StringRef Initializer,
362 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() 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> :
407 public VariadicOperandTraits<ConstantArray> {
410 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
412 //===----------------------------------------------------------------------===//
413 // ConstantStruct - Constant Struct Declarations
415 class ConstantStruct : public Constant {
416 friend struct ConstantCreator<ConstantStruct, StructType,
417 std::vector<Constant*> >;
418 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
420 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
422 // ConstantStruct accessors
423 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
424 static Constant *get(LLVMContext &Context,
425 const std::vector<Constant*> &V, bool Packed);
426 static Constant *get(LLVMContext &Context,
427 Constant *const *Vals, unsigned NumVals, bool Packed);
428 static Constant *get(LLVMContext &Context, bool Packed,
429 Constant * Val, ...) END_WITH_NULL;
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> :
459 public VariadicOperandTraits<ConstantStruct> {
462 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
465 //===----------------------------------------------------------------------===//
466 /// ConstantVector - Constant Vector Declarations
468 class ConstantVector : public Constant {
469 friend struct ConstantCreator<ConstantVector, VectorType,
470 std::vector<Constant*> >;
471 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
473 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
475 // ConstantVector accessors
476 static Constant *get(ArrayRef<Constant*> V);
477 // FIXME: Eliminate this constructor form.
478 static Constant *get(const VectorType *T, const std::vector<Constant*> &V);
480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
483 /// getType - Specialize the getType() method to always return a VectorType,
484 /// which reduces the amount of casting needed in parts of the compiler.
486 inline const VectorType *getType() const {
487 return reinterpret_cast<const VectorType*>(Value::getType());
490 /// isNullValue - Return true if this is the value that would be returned by
491 /// getNullValue. This always returns false because zero vectors are always
492 /// created as ConstantAggregateZero objects.
493 virtual bool isNullValue() const { return false; }
495 /// This function will return true iff every element in this vector constant
496 /// is set to all ones.
497 /// @returns true iff this constant's emements are all set to all ones.
498 /// @brief Determine if the value is all ones.
499 bool isAllOnesValue() const;
501 /// getSplatValue - If this is a splat constant, meaning that all of the
502 /// elements have the same value, return that value. Otherwise return NULL.
503 Constant *getSplatValue() const;
505 virtual void destroyConstant();
506 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
508 /// Methods for support type inquiry through isa, cast, and dyn_cast:
509 static inline bool classof(const ConstantVector *) { return true; }
510 static bool classof(const Value *V) {
511 return V->getValueID() == ConstantVectorVal;
516 struct OperandTraits<ConstantVector> :
517 public VariadicOperandTraits<ConstantVector> {
520 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
522 //===----------------------------------------------------------------------===//
523 /// ConstantPointerNull - a constant pointer value that points to null
525 class ConstantPointerNull : public Constant {
526 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
527 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
528 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
530 explicit ConstantPointerNull(const PointerType *T)
531 : Constant(reinterpret_cast<const Type*>(T),
532 Value::ConstantPointerNullVal, 0, 0) {}
535 // allocate space for exactly zero operands
536 void *operator new(size_t s) {
537 return User::operator new(s, 0);
540 /// get() - Static factory methods - Return objects of the specified value
541 static ConstantPointerNull *get(const PointerType *T);
543 /// isNullValue - Return true if this is the value that would be returned by
545 virtual bool isNullValue() const { return true; }
547 virtual void destroyConstant();
549 /// getType - Specialize the getType() method to always return an PointerType,
550 /// which reduces the amount of casting needed in parts of the compiler.
552 inline const PointerType *getType() const {
553 return reinterpret_cast<const PointerType*>(Value::getType());
556 /// Methods for support type inquiry through isa, cast, and dyn_cast:
557 static inline bool classof(const ConstantPointerNull *) { return true; }
558 static bool classof(const Value *V) {
559 return V->getValueID() == ConstantPointerNullVal;
563 /// BlockAddress - The address of a basic block.
565 class BlockAddress : public Constant {
566 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
567 void *operator new(size_t s) { return User::operator new(s, 2); }
568 BlockAddress(Function *F, BasicBlock *BB);
570 /// get - Return a BlockAddress for the specified function and basic block.
571 static BlockAddress *get(Function *F, BasicBlock *BB);
573 /// get - Return a BlockAddress for the specified basic block. The basic
574 /// block must be embedded into a function.
575 static BlockAddress *get(BasicBlock *BB);
577 /// Transparently provide more efficient getOperand methods.
578 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
580 Function *getFunction() const { return (Function*)Op<0>().get(); }
581 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
583 /// isNullValue - Return true if this is the value that would be returned by
585 virtual bool isNullValue() const { return false; }
587 virtual void destroyConstant();
588 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
590 /// Methods for support type inquiry through isa, cast, and dyn_cast:
591 static inline bool classof(const BlockAddress *) { return true; }
592 static inline bool classof(const Value *V) {
593 return V->getValueID() == BlockAddressVal;
598 struct OperandTraits<BlockAddress> :
599 public FixedNumOperandTraits<BlockAddress, 2> {
602 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value)
605 //===----------------------------------------------------------------------===//
606 /// ConstantExpr - a constant value that is initialized with an expression using
607 /// other constant values.
609 /// This class uses the standard Instruction opcodes to define the various
610 /// constant expressions. The Opcode field for the ConstantExpr class is
611 /// maintained in the Value::SubclassData field.
612 class ConstantExpr : public Constant {
613 friend struct ConstantCreator<ConstantExpr,Type,
614 std::pair<unsigned, std::vector<Constant*> > >;
615 friend struct ConvertConstantType<ConstantExpr, Type>;
618 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
619 : Constant(ty, ConstantExprVal, Ops, NumOps) {
620 // Operation type (an Instruction opcode) is stored as the SubclassData.
621 setValueSubclassData(Opcode);
624 // These private methods are used by the type resolution code to create
625 // ConstantExprs in intermediate forms.
626 static Constant *getTy(const Type *Ty, unsigned Opcode,
627 Constant *C1, Constant *C2,
629 static Constant *getCompareTy(unsigned short pred, Constant *C1,
631 static Constant *getSelectTy(const Type *Ty,
632 Constant *C1, Constant *C2, Constant *C3);
633 template<typename IndexTy>
634 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
635 IndexTy const *Idxs, unsigned NumIdxs,
637 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
639 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
640 Constant *Elt, Constant *Idx);
641 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
642 Constant *V2, Constant *Mask);
643 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
644 const unsigned *Idxs, unsigned NumIdxs);
645 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
647 const unsigned *Idxs, unsigned NumIdxs);
648 template<typename IndexTy>
649 static Constant *getGetElementPtrImpl(Constant *C,
650 IndexTy const *IdxList,
651 unsigned NumIdx, bool InBounds);
654 // Static methods to construct a ConstantExpr of different kinds. Note that
655 // these methods may return a object that is not an instance of the
656 // ConstantExpr class, because they will attempt to fold the constant
657 // expression into something simpler if possible.
659 /// getAlignOf constant expr - computes the alignment of a type in a target
660 /// independent way (Note: the return type is an i64).
661 static Constant *getAlignOf(const Type *Ty);
663 /// getSizeOf constant expr - computes the (alloc) size of a type (in
664 /// address-units, not bits) in a target independent way (Note: the return
667 static Constant *getSizeOf(const Type *Ty);
669 /// getOffsetOf constant expr - computes the offset of a struct field in a
670 /// target independent way (Note: the return type is an i64).
672 static Constant *getOffsetOf(const StructType *STy, unsigned FieldNo);
674 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
675 /// which supports any aggregate type, and any Constant index.
677 static Constant *getOffsetOf(const Type *Ty, Constant *FieldNo);
679 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
680 static Constant *getFNeg(Constant *C);
681 static Constant *getNot(Constant *C);
682 static Constant *getAdd(Constant *C1, Constant *C2,
683 bool HasNUW = false, bool HasNSW = false);
684 static Constant *getFAdd(Constant *C1, Constant *C2);
685 static Constant *getSub(Constant *C1, Constant *C2,
686 bool HasNUW = false, bool HasNSW = false);
687 static Constant *getFSub(Constant *C1, Constant *C2);
688 static Constant *getMul(Constant *C1, Constant *C2,
689 bool HasNUW = false, bool HasNSW = false);
690 static Constant *getFMul(Constant *C1, Constant *C2);
691 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
692 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
693 static Constant *getFDiv(Constant *C1, Constant *C2);
694 static Constant *getURem(Constant *C1, Constant *C2);
695 static Constant *getSRem(Constant *C1, Constant *C2);
696 static Constant *getFRem(Constant *C1, Constant *C2);
697 static Constant *getAnd(Constant *C1, Constant *C2);
698 static Constant *getOr(Constant *C1, Constant *C2);
699 static Constant *getXor(Constant *C1, Constant *C2);
700 static Constant *getShl(Constant *C1, Constant *C2,
701 bool HasNUW = false, bool HasNSW = false);
702 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
703 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
704 static Constant *getTrunc (Constant *C, const Type *Ty);
705 static Constant *getSExt (Constant *C, const Type *Ty);
706 static Constant *getZExt (Constant *C, const Type *Ty);
707 static Constant *getFPTrunc (Constant *C, const Type *Ty);
708 static Constant *getFPExtend(Constant *C, const Type *Ty);
709 static Constant *getUIToFP (Constant *C, const Type *Ty);
710 static Constant *getSIToFP (Constant *C, const Type *Ty);
711 static Constant *getFPToUI (Constant *C, const Type *Ty);
712 static Constant *getFPToSI (Constant *C, const Type *Ty);
713 static Constant *getPtrToInt(Constant *C, const Type *Ty);
714 static Constant *getIntToPtr(Constant *C, const Type *Ty);
715 static Constant *getBitCast (Constant *C, const Type *Ty);
717 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
718 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
719 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
720 return getAdd(C1, C2, false, true);
722 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
723 return getAdd(C1, C2, true, false);
725 static Constant *getNSWSub(Constant *C1, Constant *C2) {
726 return getSub(C1, C2, false, true);
728 static Constant *getNUWSub(Constant *C1, Constant *C2) {
729 return getSub(C1, C2, true, false);
731 static Constant *getNSWMul(Constant *C1, Constant *C2) {
732 return getMul(C1, C2, false, true);
734 static Constant *getNUWMul(Constant *C1, Constant *C2) {
735 return getMul(C1, C2, true, false);
737 static Constant *getNSWShl(Constant *C1, Constant *C2) {
738 return getShl(C1, C2, false, true);
740 static Constant *getNUWShl(Constant *C1, Constant *C2) {
741 return getShl(C1, C2, true, false);
743 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
744 return getSDiv(C1, C2, true);
746 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
747 return getUDiv(C1, C2, true);
749 static Constant *getExactAShr(Constant *C1, Constant *C2) {
750 return getAShr(C1, C2, true);
752 static Constant *getExactLShr(Constant *C1, Constant *C2) {
753 return getLShr(C1, C2, true);
756 /// Transparently provide more efficient getOperand methods.
757 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
759 // @brief Convenience function for getting one of the casting operations
760 // using a CastOps opcode.
761 static Constant *getCast(
762 unsigned ops, ///< The opcode for the conversion
763 Constant *C, ///< The constant to be converted
764 const Type *Ty ///< The type to which the constant is converted
767 // @brief Create a ZExt or BitCast cast constant expression
768 static Constant *getZExtOrBitCast(
769 Constant *C, ///< The constant to zext or bitcast
770 const Type *Ty ///< The type to zext or bitcast C to
773 // @brief Create a SExt or BitCast cast constant expression
774 static Constant *getSExtOrBitCast(
775 Constant *C, ///< The constant to sext or bitcast
776 const Type *Ty ///< The type to sext or bitcast C to
779 // @brief Create a Trunc or BitCast cast constant expression
780 static Constant *getTruncOrBitCast(
781 Constant *C, ///< The constant to trunc or bitcast
782 const Type *Ty ///< The type to trunc or bitcast C to
785 /// @brief Create a BitCast or a PtrToInt cast constant expression
786 static Constant *getPointerCast(
787 Constant *C, ///< The pointer value to be casted (operand 0)
788 const Type *Ty ///< The type to which cast should be made
791 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
792 static Constant *getIntegerCast(
793 Constant *C, ///< The integer constant to be casted
794 const Type *Ty, ///< The integer type to cast to
795 bool isSigned ///< Whether C should be treated as signed or not
798 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
799 static Constant *getFPCast(
800 Constant *C, ///< The integer constant to be casted
801 const Type *Ty ///< The integer type to cast to
804 /// @brief Return true if this is a convert constant expression
807 /// @brief Return true if this is a compare constant expression
808 bool isCompare() const;
810 /// @brief Return true if this is an insertvalue or extractvalue expression,
811 /// and the getIndices() method may be used.
812 bool hasIndices() const;
814 /// @brief Return true if this is a getelementptr expression and all
815 /// the index operands are compile-time known integers within the
816 /// corresponding notional static array extents. Note that this is
817 /// not equivalant to, a subset of, or a superset of the "inbounds"
819 bool isGEPWithNoNotionalOverIndexing() const;
821 /// Select constant expr
823 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
824 return getSelectTy(V1->getType(), C, V1, V2);
827 /// get - Return a binary or shift operator constant expression,
828 /// folding if possible.
830 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
833 /// @brief Return an ICmp or FCmp comparison operator constant expression.
834 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
836 /// get* - Return some common constants without having to
837 /// specify the full Instruction::OPCODE identifier.
839 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
840 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
842 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
843 /// all elements must be Constant's.
845 static Constant *getGetElementPtr(Constant *C,
846 Constant *const *IdxList, unsigned NumIdx,
847 bool InBounds = false);
848 static Constant *getGetElementPtr(Constant *C,
849 Value *const *IdxList, unsigned NumIdx,
850 bool InBounds = false);
852 /// Create an "inbounds" getelementptr. See the documentation for the
853 /// "inbounds" flag in LangRef.html for details.
854 static Constant *getInBoundsGetElementPtr(Constant *C,
855 Constant *const *IdxList,
857 return getGetElementPtr(C, IdxList, NumIdx, true);
859 static Constant *getInBoundsGetElementPtr(Constant *C,
860 Value* const *IdxList,
862 return getGetElementPtr(C, IdxList, NumIdx, true);
865 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
866 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
867 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
868 static Constant *getExtractValue(Constant *Agg,
869 const unsigned *IdxList, unsigned NumIdx);
870 static Constant *getInsertValue(Constant *Agg, Constant *Val,
871 const unsigned *IdxList, unsigned NumIdx);
873 /// isNullValue - Return true if this is the value that would be returned by
875 virtual bool isNullValue() const { return false; }
877 /// getOpcode - Return the opcode at the root of this constant expression
878 unsigned getOpcode() const { return getSubclassDataFromValue(); }
880 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
881 /// not an ICMP or FCMP constant expression.
882 unsigned getPredicate() const;
884 /// getIndices - Assert that this is an insertvalue or exactvalue
885 /// expression and return the list of indices.
886 const SmallVector<unsigned, 4> &getIndices() const;
888 /// getOpcodeName - Return a string representation for an opcode.
889 const char *getOpcodeName() const;
891 /// getWithOperandReplaced - Return a constant expression identical to this
892 /// one, but with the specified operand set to the specified value.
893 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
895 /// getWithOperands - This returns the current constant expression with the
896 /// operands replaced with the specified values. The specified operands must
897 /// match count and type with the existing ones.
898 Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
899 return getWithOperands(&Ops[0], (unsigned)Ops.size());
901 Constant *getWithOperands(Constant *const *Ops, unsigned NumOps) const;
903 virtual void destroyConstant();
904 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
906 /// Methods for support type inquiry through isa, cast, and dyn_cast:
907 static inline bool classof(const ConstantExpr *) { return true; }
908 static inline bool classof(const Value *V) {
909 return V->getValueID() == ConstantExprVal;
913 // Shadow Value::setValueSubclassData with a private forwarding method so that
914 // subclasses cannot accidentally use it.
915 void setValueSubclassData(unsigned short D) {
916 Value::setValueSubclassData(D);
921 struct OperandTraits<ConstantExpr> :
922 public VariadicOperandTraits<ConstantExpr, 1> {
925 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
927 //===----------------------------------------------------------------------===//
928 /// UndefValue - 'undef' values are things that do not have specified contents.
929 /// These are used for a variety of purposes, including global variable
930 /// initializers and operands to instructions. 'undef' values can occur with
931 /// any first-class type.
933 /// Undef values aren't exactly constants; if they have multiple uses, they
934 /// can appear to have different bit patterns at each use. See
935 /// LangRef.html#undefvalues for details.
937 class UndefValue : public Constant {
938 friend struct ConstantCreator<UndefValue, Type, char>;
939 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
940 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
942 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
944 // allocate space for exactly zero operands
945 void *operator new(size_t s) {
946 return User::operator new(s, 0);
949 /// get() - Static factory methods - Return an 'undef' object of the specified
952 static UndefValue *get(const Type *T);
954 /// isNullValue - Return true if this is the value that would be returned by
956 virtual bool isNullValue() const { return false; }
958 virtual void destroyConstant();
960 /// Methods for support type inquiry through isa, cast, and dyn_cast:
961 static inline bool classof(const UndefValue *) { return true; }
962 static bool classof(const Value *V) {
963 return V->getValueID() == UndefValueVal;
967 } // End llvm namespace