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 virtual void anchor();
49 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
50 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
51 ConstantInt(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 static ConstantInt *getTrue(LLVMContext &Context);
60 static ConstantInt *getFalse(LLVMContext &Context);
61 static Constant *getTrue(Type *Ty);
62 static Constant *getFalse(Type *Ty);
64 /// If Ty is a vector type, return a Constant with a splat of the given
65 /// value. Otherwise return a ConstantInt for the given value.
66 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
68 /// Return a ConstantInt with the specified integer value for the specified
69 /// type. If the type is wider than 64 bits, the value will be zero-extended
70 /// to fit the type, unless isSigned is true, in which case the value will
71 /// be interpreted as a 64-bit signed integer and sign-extended to fit
73 /// @brief Get a ConstantInt for a specific value.
74 static ConstantInt *get(IntegerType *Ty, uint64_t V,
75 bool isSigned = false);
77 /// Return a ConstantInt with the specified value for the specified type. The
78 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
79 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
80 /// signed value for the type Ty.
81 /// @brief Get a ConstantInt for a specific signed value.
82 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
83 static Constant *getSigned(Type *Ty, int64_t V);
85 /// Return a ConstantInt with the specified value and an implied Type. The
86 /// type is the integer type that corresponds to the bit width of the value.
87 static ConstantInt *get(LLVMContext &Context, const APInt &V);
89 /// Return a ConstantInt constructed from the string strStart with the given
91 static ConstantInt *get(IntegerType *Ty, StringRef Str,
94 /// If Ty is a vector type, return a Constant with a splat of the given
95 /// value. Otherwise return a ConstantInt for the given value.
96 static Constant *get(Type* Ty, const APInt& V);
98 /// Return the constant as an APInt value reference. This allows clients to
99 /// obtain a copy of the value, with all its precision in tact.
100 /// @brief Return the constant's value.
101 inline const APInt &getValue() const {
105 /// getBitWidth - Return the bitwidth of this constant.
106 unsigned getBitWidth() const { return Val.getBitWidth(); }
108 /// Return the constant as a 64-bit unsigned integer value after it
109 /// has been zero extended as appropriate for the type of this constant. Note
110 /// that this method can assert if the value does not fit in 64 bits.
112 /// @brief Return the zero extended value.
113 inline uint64_t getZExtValue() const {
114 return Val.getZExtValue();
117 /// Return the constant as a 64-bit integer value after it has been sign
118 /// extended as appropriate for the type of this constant. Note that
119 /// this method can assert if the value does not fit in 64 bits.
121 /// @brief Return the sign extended value.
122 inline int64_t getSExtValue() const {
123 return Val.getSExtValue();
126 /// A helper method that can be used to determine if the constant contained
127 /// within is equal to a constant. This only works for very small values,
128 /// because this is all that can be represented with all types.
129 /// @brief Determine if this constant's value is same as an unsigned char.
130 bool equalsInt(uint64_t V) const {
134 /// getType - Specialize the getType() method to always return an IntegerType,
135 /// which reduces the amount of casting needed in parts of the compiler.
137 inline IntegerType *getType() const {
138 return reinterpret_cast<IntegerType*>(Value::getType());
141 /// This static method returns true if the type Ty is big enough to
142 /// represent the value V. This can be used to avoid having the get method
143 /// assert when V is larger than Ty can represent. Note that there are two
144 /// versions of this method, one for unsigned and one for signed integers.
145 /// Although ConstantInt canonicalizes everything to an unsigned integer,
146 /// the signed version avoids callers having to convert a signed quantity
147 /// to the appropriate unsigned type before calling the method.
148 /// @returns true if V is a valid value for type Ty
149 /// @brief Determine if the value is in range for the given type.
150 static bool isValueValidForType(Type *Ty, uint64_t V);
151 static bool isValueValidForType(Type *Ty, int64_t V);
153 bool isNegative() const { return Val.isNegative(); }
155 /// This is just a convenience method to make client code smaller for a
156 /// common code. It also correctly performs the comparison without the
157 /// potential for an assertion from getZExtValue().
158 bool isZero() const {
162 /// This is just a convenience method to make client code smaller for a
163 /// common case. It also correctly performs the comparison without the
164 /// potential for an assertion from getZExtValue().
165 /// @brief Determine if the value is one.
170 /// This function will return true iff every bit in this constant is set
172 /// @returns true iff this constant's bits are all set to true.
173 /// @brief Determine if the value is all ones.
174 bool isMinusOne() const {
175 return Val.isAllOnesValue();
178 /// This function will return true iff this constant represents the largest
179 /// value that may be represented by the constant's type.
180 /// @returns true iff this is the largest value that may be represented
182 /// @brief Determine if the value is maximal.
183 bool isMaxValue(bool isSigned) const {
185 return Val.isMaxSignedValue();
187 return Val.isMaxValue();
190 /// This function will return true iff this constant represents the smallest
191 /// value that may be represented by this constant's type.
192 /// @returns true if this is the smallest value that may be represented by
194 /// @brief Determine if the value is minimal.
195 bool isMinValue(bool isSigned) const {
197 return Val.isMinSignedValue();
199 return Val.isMinValue();
202 /// This function will return true iff this constant represents a value with
203 /// active bits bigger than 64 bits or a value greater than the given uint64_t
205 /// @returns true iff this constant is greater or equal to the given number.
206 /// @brief Determine if the value is greater or equal to the given number.
207 bool uge(uint64_t Num) const {
208 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
211 /// getLimitedValue - If the value is smaller than the specified limit,
212 /// return it, otherwise return the limit value. This causes the value
213 /// to saturate to the limit.
214 /// @returns the min of the value of the constant and the specified value
215 /// @brief Get the constant's value with a saturation limit
216 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
217 return Val.getLimitedValue(Limit);
220 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
221 static inline bool classof(const ConstantInt *) { return true; }
222 static bool classof(const Value *V) {
223 return V->getValueID() == ConstantIntVal;
228 //===----------------------------------------------------------------------===//
229 /// ConstantFP - Floating Point Values [float, double]
231 class ConstantFP : public Constant {
233 virtual void anchor();
234 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
235 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
236 friend class LLVMContextImpl;
238 ConstantFP(Type *Ty, const APFloat& V);
240 // allocate space for exactly zero operands
241 void *operator new(size_t s) {
242 return User::operator new(s, 0);
245 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
246 /// method returns the negative zero constant for floating point or vector
247 /// floating point types; for all other types, it returns the null value.
248 static Constant *getZeroValueForNegation(Type *Ty);
250 /// get() - This returns a ConstantFP, or a vector containing a splat of a
251 /// ConstantFP, for the specified value in the specified type. This should
252 /// only be used for simple constant values like 2.0/1.0 etc, that are
253 /// known-valid both as host double and as the target format.
254 static Constant *get(Type* Ty, double V);
255 static Constant *get(Type* Ty, StringRef Str);
256 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
257 static ConstantFP *getNegativeZero(Type* Ty);
258 static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
260 /// isValueValidForType - return true if Ty is big enough to represent V.
261 static bool isValueValidForType(Type *Ty, const APFloat &V);
262 inline const APFloat &getValueAPF() const { return Val; }
264 /// isZero - Return true if the value is positive or negative zero.
265 bool isZero() const { return Val.isZero(); }
267 /// isNegative - Return true if the sign bit is set.
268 bool isNegative() const { return Val.isNegative(); }
270 /// isNaN - Return true if the value is a NaN.
271 bool isNaN() const { return Val.isNaN(); }
273 /// isExactlyValue - We don't rely on operator== working on double values, as
274 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
275 /// As such, this method can be used to do an exact bit-for-bit comparison of
276 /// two floating point values. The version with a double operand is retained
277 /// because it's so convenient to write isExactlyValue(2.0), but please use
278 /// it only for simple constants.
279 bool isExactlyValue(const APFloat &V) const;
281 bool isExactlyValue(double V) const {
283 // convert is not supported on this type
284 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
287 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
288 return isExactlyValue(FV);
290 /// Methods for support type inquiry through isa, cast, and dyn_cast:
291 static inline bool classof(const ConstantFP *) { return true; }
292 static bool classof(const Value *V) {
293 return V->getValueID() == ConstantFPVal;
297 //===----------------------------------------------------------------------===//
298 /// ConstantAggregateZero - All zero aggregate value
300 class ConstantAggregateZero : public Constant {
301 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
302 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
303 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
305 explicit ConstantAggregateZero(Type *ty)
306 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
308 // allocate space for exactly zero operands
309 void *operator new(size_t s) {
310 return User::operator new(s, 0);
313 static ConstantAggregateZero* get(Type *Ty);
315 virtual void destroyConstant();
317 /// Methods for support type inquiry through isa, cast, and dyn_cast:
319 static bool classof(const ConstantAggregateZero *) { return true; }
320 static bool classof(const Value *V) {
321 return V->getValueID() == ConstantAggregateZeroVal;
326 //===----------------------------------------------------------------------===//
327 /// ConstantArray - Constant Array Declarations
329 class ConstantArray : public Constant {
330 friend struct ConstantCreator<ConstantArray, ArrayType,
331 std::vector<Constant*> >;
332 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
334 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
336 // ConstantArray accessors
337 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
339 /// This method constructs a ConstantArray and initializes it with a text
340 /// string. The default behavior (AddNull==true) causes a null terminator to
341 /// be placed at the end of the array. This effectively increases the length
342 /// of the array by one (you've been warned). However, in some situations
343 /// this is not desired so if AddNull==false then the string is copied without
344 /// null termination.
345 static Constant *get(LLVMContext &Context, StringRef Initializer,
346 bool AddNull = true);
348 /// Transparently provide more efficient getOperand methods.
349 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
351 /// getType - Specialize the getType() method to always return an ArrayType,
352 /// which reduces the amount of casting needed in parts of the compiler.
354 inline ArrayType *getType() const {
355 return reinterpret_cast<ArrayType*>(Value::getType());
358 /// isString - This method returns true if the array is an array of i8 and
359 /// the elements of the array are all ConstantInt's.
360 bool isString() const;
362 /// isCString - This method returns true if the array is a string (see
364 /// isString) and it ends in a null byte \0 and does not contains any other
366 /// null bytes except its terminator.
367 bool isCString() const;
369 /// getAsString - If this array is isString(), then this method converts the
370 /// array to an std::string and returns it. Otherwise, it asserts out.
372 std::string getAsString() const;
374 /// getAsCString - If this array is isCString(), then this method converts the
375 /// array (without the trailing null byte) to an std::string and returns it.
376 /// Otherwise, it asserts out.
378 std::string getAsCString() const;
380 virtual void destroyConstant();
381 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
383 /// Methods for support type inquiry through isa, cast, and dyn_cast:
384 static inline bool classof(const ConstantArray *) { return true; }
385 static bool classof(const Value *V) {
386 return V->getValueID() == ConstantArrayVal;
391 struct OperandTraits<ConstantArray> :
392 public VariadicOperandTraits<ConstantArray> {
395 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
397 //===----------------------------------------------------------------------===//
398 // ConstantStruct - Constant Struct Declarations
400 class ConstantStruct : public Constant {
401 friend struct ConstantCreator<ConstantStruct, StructType,
402 std::vector<Constant*> >;
403 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
405 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
407 // ConstantStruct accessors
408 static Constant *get(StructType *T, ArrayRef<Constant*> V);
409 static Constant *get(StructType *T, ...) END_WITH_NULL;
411 /// getAnon - Return an anonymous struct that has the specified
412 /// elements. If the struct is possibly empty, then you must specify a
414 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
415 return get(getTypeForElements(V, Packed), V);
417 static Constant *getAnon(LLVMContext &Ctx,
418 ArrayRef<Constant*> V, bool Packed = false) {
419 return get(getTypeForElements(Ctx, V, Packed), V);
422 /// getTypeForElements - Return an anonymous struct type to use for a constant
423 /// with the specified set of elements. The list must not be empty.
424 static StructType *getTypeForElements(ArrayRef<Constant*> V,
425 bool Packed = false);
426 /// getTypeForElements - This version of the method allows an empty list.
427 static StructType *getTypeForElements(LLVMContext &Ctx,
428 ArrayRef<Constant*> V,
429 bool Packed = false);
431 /// Transparently provide more efficient getOperand methods.
432 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
434 /// getType() specialization - Reduce amount of casting...
436 inline StructType *getType() const {
437 return reinterpret_cast<StructType*>(Value::getType());
440 virtual void destroyConstant();
441 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
443 /// Methods for support type inquiry through isa, cast, and dyn_cast:
444 static inline bool classof(const ConstantStruct *) { return true; }
445 static bool classof(const Value *V) {
446 return V->getValueID() == ConstantStructVal;
451 struct OperandTraits<ConstantStruct> :
452 public VariadicOperandTraits<ConstantStruct> {
455 DEFINE_TRANSPARENT_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(VectorType *T, ArrayRef<Constant *> Val);
468 // ConstantVector accessors
469 static Constant *get(ArrayRef<Constant*> V);
471 /// Transparently provide more efficient getOperand methods.
472 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
474 /// getType - Specialize the getType() method to always return a VectorType,
475 /// which reduces the amount of casting needed in parts of the compiler.
477 inline VectorType *getType() const {
478 return reinterpret_cast<VectorType*>(Value::getType());
481 /// getSplatValue - If this is a splat constant, meaning that all of the
482 /// elements have the same value, return that value. Otherwise return NULL.
483 Constant *getSplatValue() const;
485 virtual void destroyConstant();
486 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
488 /// Methods for support type inquiry through isa, cast, and dyn_cast:
489 static inline bool classof(const ConstantVector *) { return true; }
490 static bool classof(const Value *V) {
491 return V->getValueID() == ConstantVectorVal;
496 struct OperandTraits<ConstantVector> :
497 public VariadicOperandTraits<ConstantVector> {
500 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
502 //===----------------------------------------------------------------------===//
503 /// ConstantPointerNull - a constant pointer value that points to null
505 class ConstantPointerNull : public Constant {
506 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
507 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
508 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
510 explicit ConstantPointerNull(PointerType *T)
511 : Constant(reinterpret_cast<Type*>(T),
512 Value::ConstantPointerNullVal, 0, 0) {}
515 // allocate space for exactly zero operands
516 void *operator new(size_t s) {
517 return User::operator new(s, 0);
520 /// get() - Static factory methods - Return objects of the specified value
521 static ConstantPointerNull *get(PointerType *T);
523 virtual void destroyConstant();
525 /// getType - Specialize the getType() method to always return an PointerType,
526 /// which reduces the amount of casting needed in parts of the compiler.
528 inline PointerType *getType() const {
529 return reinterpret_cast<PointerType*>(Value::getType());
532 /// Methods for support type inquiry through isa, cast, and dyn_cast:
533 static inline bool classof(const ConstantPointerNull *) { return true; }
534 static bool classof(const Value *V) {
535 return V->getValueID() == ConstantPointerNullVal;
539 /// BlockAddress - The address of a basic block.
541 class BlockAddress : public Constant {
542 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
543 void *operator new(size_t s) { return User::operator new(s, 2); }
544 BlockAddress(Function *F, BasicBlock *BB);
546 /// get - Return a BlockAddress for the specified function and basic block.
547 static BlockAddress *get(Function *F, BasicBlock *BB);
549 /// get - Return a BlockAddress for the specified basic block. The basic
550 /// block must be embedded into a function.
551 static BlockAddress *get(BasicBlock *BB);
553 /// Transparently provide more efficient getOperand methods.
554 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
556 Function *getFunction() const { return (Function*)Op<0>().get(); }
557 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
559 virtual void destroyConstant();
560 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
562 /// Methods for support type inquiry through isa, cast, and dyn_cast:
563 static inline bool classof(const BlockAddress *) { return true; }
564 static inline bool classof(const Value *V) {
565 return V->getValueID() == BlockAddressVal;
570 struct OperandTraits<BlockAddress> :
571 public FixedNumOperandTraits<BlockAddress, 2> {
574 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
577 //===----------------------------------------------------------------------===//
578 /// ConstantExpr - a constant value that is initialized with an expression using
579 /// other constant values.
581 /// This class uses the standard Instruction opcodes to define the various
582 /// constant expressions. The Opcode field for the ConstantExpr class is
583 /// maintained in the Value::SubclassData field.
584 class ConstantExpr : public Constant {
585 friend struct ConstantCreator<ConstantExpr,Type,
586 std::pair<unsigned, std::vector<Constant*> > >;
587 friend struct ConvertConstantType<ConstantExpr, Type>;
590 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
591 : Constant(ty, ConstantExprVal, Ops, NumOps) {
592 // Operation type (an Instruction opcode) is stored as the SubclassData.
593 setValueSubclassData(Opcode);
597 // Static methods to construct a ConstantExpr of different kinds. Note that
598 // these methods may return a object that is not an instance of the
599 // ConstantExpr class, because they will attempt to fold the constant
600 // expression into something simpler if possible.
602 /// getAlignOf constant expr - computes the alignment of a type in a target
603 /// independent way (Note: the return type is an i64).
604 static Constant *getAlignOf(Type *Ty);
606 /// getSizeOf constant expr - computes the (alloc) size of a type (in
607 /// address-units, not bits) in a target independent way (Note: the return
610 static Constant *getSizeOf(Type *Ty);
612 /// getOffsetOf constant expr - computes the offset of a struct field in a
613 /// target independent way (Note: the return type is an i64).
615 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
617 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
618 /// which supports any aggregate type, and any Constant index.
620 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
622 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
623 static Constant *getFNeg(Constant *C);
624 static Constant *getNot(Constant *C);
625 static Constant *getAdd(Constant *C1, Constant *C2,
626 bool HasNUW = false, bool HasNSW = false);
627 static Constant *getFAdd(Constant *C1, Constant *C2);
628 static Constant *getSub(Constant *C1, Constant *C2,
629 bool HasNUW = false, bool HasNSW = false);
630 static Constant *getFSub(Constant *C1, Constant *C2);
631 static Constant *getMul(Constant *C1, Constant *C2,
632 bool HasNUW = false, bool HasNSW = false);
633 static Constant *getFMul(Constant *C1, Constant *C2);
634 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
635 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
636 static Constant *getFDiv(Constant *C1, Constant *C2);
637 static Constant *getURem(Constant *C1, Constant *C2);
638 static Constant *getSRem(Constant *C1, Constant *C2);
639 static Constant *getFRem(Constant *C1, Constant *C2);
640 static Constant *getAnd(Constant *C1, Constant *C2);
641 static Constant *getOr(Constant *C1, Constant *C2);
642 static Constant *getXor(Constant *C1, Constant *C2);
643 static Constant *getShl(Constant *C1, Constant *C2,
644 bool HasNUW = false, bool HasNSW = false);
645 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
646 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
647 static Constant *getTrunc (Constant *C, Type *Ty);
648 static Constant *getSExt (Constant *C, Type *Ty);
649 static Constant *getZExt (Constant *C, Type *Ty);
650 static Constant *getFPTrunc (Constant *C, Type *Ty);
651 static Constant *getFPExtend(Constant *C, Type *Ty);
652 static Constant *getUIToFP (Constant *C, Type *Ty);
653 static Constant *getSIToFP (Constant *C, Type *Ty);
654 static Constant *getFPToUI (Constant *C, Type *Ty);
655 static Constant *getFPToSI (Constant *C, Type *Ty);
656 static Constant *getPtrToInt(Constant *C, Type *Ty);
657 static Constant *getIntToPtr(Constant *C, Type *Ty);
658 static Constant *getBitCast (Constant *C, Type *Ty);
660 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
661 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
662 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
663 return getAdd(C1, C2, false, true);
665 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
666 return getAdd(C1, C2, true, false);
668 static Constant *getNSWSub(Constant *C1, Constant *C2) {
669 return getSub(C1, C2, false, true);
671 static Constant *getNUWSub(Constant *C1, Constant *C2) {
672 return getSub(C1, C2, true, false);
674 static Constant *getNSWMul(Constant *C1, Constant *C2) {
675 return getMul(C1, C2, false, true);
677 static Constant *getNUWMul(Constant *C1, Constant *C2) {
678 return getMul(C1, C2, true, false);
680 static Constant *getNSWShl(Constant *C1, Constant *C2) {
681 return getShl(C1, C2, false, true);
683 static Constant *getNUWShl(Constant *C1, Constant *C2) {
684 return getShl(C1, C2, true, false);
686 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
687 return getSDiv(C1, C2, true);
689 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
690 return getUDiv(C1, C2, true);
692 static Constant *getExactAShr(Constant *C1, Constant *C2) {
693 return getAShr(C1, C2, true);
695 static Constant *getExactLShr(Constant *C1, Constant *C2) {
696 return getLShr(C1, C2, true);
699 /// Transparently provide more efficient getOperand methods.
700 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
702 // @brief Convenience function for getting one of the casting operations
703 // using a CastOps opcode.
704 static Constant *getCast(
705 unsigned ops, ///< The opcode for the conversion
706 Constant *C, ///< The constant to be converted
707 Type *Ty ///< The type to which the constant is converted
710 // @brief Create a ZExt or BitCast cast constant expression
711 static Constant *getZExtOrBitCast(
712 Constant *C, ///< The constant to zext or bitcast
713 Type *Ty ///< The type to zext or bitcast C to
716 // @brief Create a SExt or BitCast cast constant expression
717 static Constant *getSExtOrBitCast(
718 Constant *C, ///< The constant to sext or bitcast
719 Type *Ty ///< The type to sext or bitcast C to
722 // @brief Create a Trunc or BitCast cast constant expression
723 static Constant *getTruncOrBitCast(
724 Constant *C, ///< The constant to trunc or bitcast
725 Type *Ty ///< The type to trunc or bitcast C to
728 /// @brief Create a BitCast or a PtrToInt cast constant expression
729 static Constant *getPointerCast(
730 Constant *C, ///< The pointer value to be casted (operand 0)
731 Type *Ty ///< The type to which cast should be made
734 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
735 static Constant *getIntegerCast(
736 Constant *C, ///< The integer constant to be casted
737 Type *Ty, ///< The integer type to cast to
738 bool isSigned ///< Whether C should be treated as signed or not
741 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
742 static Constant *getFPCast(
743 Constant *C, ///< The integer constant to be casted
744 Type *Ty ///< The integer type to cast to
747 /// @brief Return true if this is a convert constant expression
750 /// @brief Return true if this is a compare constant expression
751 bool isCompare() const;
753 /// @brief Return true if this is an insertvalue or extractvalue expression,
754 /// and the getIndices() method may be used.
755 bool hasIndices() const;
757 /// @brief Return true if this is a getelementptr expression and all
758 /// the index operands are compile-time known integers within the
759 /// corresponding notional static array extents. Note that this is
760 /// not equivalant to, a subset of, or a superset of the "inbounds"
762 bool isGEPWithNoNotionalOverIndexing() const;
764 /// Select constant expr
766 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
768 /// get - Return a binary or shift operator constant expression,
769 /// folding if possible.
771 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
774 /// @brief Return an ICmp or FCmp comparison operator constant expression.
775 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
777 /// get* - Return some common constants without having to
778 /// specify the full Instruction::OPCODE identifier.
780 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
781 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
783 /// Getelementptr form. Value* is only accepted for convenience;
784 /// all elements must be Constant's.
786 static Constant *getGetElementPtr(Constant *C,
787 ArrayRef<Constant *> IdxList,
788 bool InBounds = false) {
789 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
793 static Constant *getGetElementPtr(Constant *C,
795 bool InBounds = false) {
796 // This form of the function only exists to avoid ambiguous overload
797 // warnings about whether to convert Idx to ArrayRef<Constant *> or
798 // ArrayRef<Value *>.
799 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
801 static Constant *getGetElementPtr(Constant *C,
802 ArrayRef<Value *> IdxList,
803 bool InBounds = false);
805 /// Create an "inbounds" getelementptr. See the documentation for the
806 /// "inbounds" flag in LangRef.html for details.
807 static Constant *getInBoundsGetElementPtr(Constant *C,
808 ArrayRef<Constant *> IdxList) {
809 return getGetElementPtr(C, IdxList, true);
811 static Constant *getInBoundsGetElementPtr(Constant *C,
813 // This form of the function only exists to avoid ambiguous overload
814 // warnings about whether to convert Idx to ArrayRef<Constant *> or
815 // ArrayRef<Value *>.
816 return getGetElementPtr(C, Idx, true);
818 static Constant *getInBoundsGetElementPtr(Constant *C,
819 ArrayRef<Value *> IdxList) {
820 return getGetElementPtr(C, IdxList, true);
823 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
824 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
825 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
826 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
827 static Constant *getInsertValue(Constant *Agg, Constant *Val,
828 ArrayRef<unsigned> Idxs);
830 /// getOpcode - Return the opcode at the root of this constant expression
831 unsigned getOpcode() const { return getSubclassDataFromValue(); }
833 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
834 /// not an ICMP or FCMP constant expression.
835 unsigned getPredicate() const;
837 /// getIndices - Assert that this is an insertvalue or exactvalue
838 /// expression and return the list of indices.
839 ArrayRef<unsigned> getIndices() const;
841 /// getOpcodeName - Return a string representation for an opcode.
842 const char *getOpcodeName() const;
844 /// getWithOperandReplaced - Return a constant expression identical to this
845 /// one, but with the specified operand set to the specified value.
846 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
848 /// getWithOperands - This returns the current constant expression with the
849 /// operands replaced with the specified values. The specified array must
850 /// have the same number of operands as our current one.
851 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
852 return getWithOperands(Ops, getType());
855 /// getWithOperands - This returns the current constant expression with the
856 /// operands replaced with the specified values and with the specified result
857 /// type. The specified array must have the same number of operands as our
859 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
861 virtual void destroyConstant();
862 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
864 /// Methods for support type inquiry through isa, cast, and dyn_cast:
865 static inline bool classof(const ConstantExpr *) { return true; }
866 static inline bool classof(const Value *V) {
867 return V->getValueID() == ConstantExprVal;
871 // Shadow Value::setValueSubclassData with a private forwarding method so that
872 // subclasses cannot accidentally use it.
873 void setValueSubclassData(unsigned short D) {
874 Value::setValueSubclassData(D);
879 struct OperandTraits<ConstantExpr> :
880 public VariadicOperandTraits<ConstantExpr, 1> {
883 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
885 //===----------------------------------------------------------------------===//
886 /// UndefValue - 'undef' values are things that do not have specified contents.
887 /// These are used for a variety of purposes, including global variable
888 /// initializers and operands to instructions. 'undef' values can occur with
889 /// any first-class type.
891 /// Undef values aren't exactly constants; if they have multiple uses, they
892 /// can appear to have different bit patterns at each use. See
893 /// LangRef.html#undefvalues for details.
895 class UndefValue : public Constant {
896 friend struct ConstantCreator<UndefValue, Type, char>;
897 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
898 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
900 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
902 // allocate space for exactly zero operands
903 void *operator new(size_t s) {
904 return User::operator new(s, 0);
907 /// get() - Static factory methods - Return an 'undef' object of the specified
910 static UndefValue *get(Type *T);
912 virtual void destroyConstant();
914 /// Methods for support type inquiry through isa, cast, and dyn_cast:
915 static inline bool classof(const UndefValue *) { return true; }
916 static bool classof(const Value *V) {
917 return V->getValueID() == UndefValueVal;
921 } // End llvm namespace