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);
60 static Constant *getTrue(const Type *Ty);
61 static Constant *getFalse(const Type *Ty);
63 /// If Ty is a vector type, return a Constant with a splat of the given
64 /// value. Otherwise return a ConstantInt for the given value.
65 static Constant *get(const Type *Ty, uint64_t V, bool isSigned = false);
67 /// Return a ConstantInt with the specified integer value for the specified
68 /// type. If the type is wider than 64 bits, the value will be zero-extended
69 /// to fit the type, unless isSigned is true, in which case the value will
70 /// be interpreted as a 64-bit signed integer and sign-extended to fit
72 /// @brief Get a ConstantInt for a specific value.
73 static ConstantInt *get(const IntegerType *Ty, uint64_t V,
74 bool isSigned = false);
76 /// Return a ConstantInt with the specified value for the specified type. The
77 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
78 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
79 /// signed value for the type Ty.
80 /// @brief Get a ConstantInt for a specific signed value.
81 static ConstantInt *getSigned(const IntegerType *Ty, int64_t V);
82 static Constant *getSigned(const Type *Ty, int64_t V);
84 /// Return a ConstantInt with the specified value and an implied Type. The
85 /// type is the integer type that corresponds to the bit width of the value.
86 static ConstantInt *get(LLVMContext &Context, const APInt &V);
88 /// Return a ConstantInt constructed from the string strStart with the given
90 static ConstantInt *get(const IntegerType *Ty, StringRef Str,
93 /// If Ty is a vector type, return a Constant with a splat of the given
94 /// value. Otherwise return a ConstantInt for the given value.
95 static Constant *get(const Type* Ty, const APInt& V);
97 /// Return the constant as an APInt value reference. This allows clients to
98 /// obtain a copy of the value, with all its precision in tact.
99 /// @brief Return the constant's value.
100 inline const APInt &getValue() const {
104 /// getBitWidth - Return the bitwidth of this constant.
105 unsigned getBitWidth() const { return Val.getBitWidth(); }
107 /// Return the constant as a 64-bit unsigned integer value after it
108 /// has been zero extended as appropriate for the type of this constant. Note
109 /// that this method can assert if the value does not fit in 64 bits.
111 /// @brief Return the zero extended value.
112 inline uint64_t getZExtValue() const {
113 return Val.getZExtValue();
116 /// Return the constant as a 64-bit integer value after it has been sign
117 /// extended as appropriate for the type of this constant. Note that
118 /// this method can assert if the value does not fit in 64 bits.
120 /// @brief Return the sign extended value.
121 inline int64_t getSExtValue() const {
122 return Val.getSExtValue();
125 /// A helper method that can be used to determine if the constant contained
126 /// within is equal to a constant. This only works for very small values,
127 /// because this is all that can be represented with all types.
128 /// @brief Determine if this constant's value is same as an unsigned char.
129 bool equalsInt(uint64_t V) const {
133 /// getType - Specialize the getType() method to always return an IntegerType,
134 /// which reduces the amount of casting needed in parts of the compiler.
136 inline const IntegerType *getType() const {
137 return reinterpret_cast<const IntegerType*>(Value::getType());
140 /// This static method returns true if the type Ty is big enough to
141 /// represent the value V. This can be used to avoid having the get method
142 /// assert when V is larger than Ty can represent. Note that there are two
143 /// versions of this method, one for unsigned and one for signed integers.
144 /// Although ConstantInt canonicalizes everything to an unsigned integer,
145 /// the signed version avoids callers having to convert a signed quantity
146 /// to the appropriate unsigned type before calling the method.
147 /// @returns true if V is a valid value for type Ty
148 /// @brief Determine if the value is in range for the given type.
149 static bool isValueValidForType(const Type *Ty, uint64_t V);
150 static bool isValueValidForType(const Type *Ty, int64_t V);
152 /// This function will return true iff this constant represents the "null"
153 /// value that would be returned by the getNullValue method.
154 /// @returns true if this is the null integer value.
155 /// @brief Determine if the value is null.
156 virtual bool isNullValue() const {
160 /// This is just a convenience method to make client code smaller for a
161 /// common code. It also correctly performs the comparison without the
162 /// potential for an assertion from getZExtValue().
163 bool isZero() const {
167 /// This is just a convenience method to make client code smaller for a
168 /// common case. It also correctly performs the comparison without the
169 /// potential for an assertion from getZExtValue().
170 /// @brief Determine if the value is one.
175 /// This function will return true iff every bit in this constant is set
177 /// @returns true iff this constant's bits are all set to true.
178 /// @brief Determine if the value is all ones.
179 bool isAllOnesValue() const {
180 return Val.isAllOnesValue();
183 /// This function will return true iff this constant represents the largest
184 /// value that may be represented by the constant's type.
185 /// @returns true iff this is the largest value that may be represented
187 /// @brief Determine if the value is maximal.
188 bool isMaxValue(bool isSigned) const {
190 return Val.isMaxSignedValue();
192 return Val.isMaxValue();
195 /// This function will return true iff this constant represents the smallest
196 /// value that may be represented by this constant's type.
197 /// @returns true if this is the smallest value that may be represented by
199 /// @brief Determine if the value is minimal.
200 bool isMinValue(bool isSigned) const {
202 return Val.isMinSignedValue();
204 return Val.isMinValue();
207 /// This function will return true iff this constant represents a value with
208 /// active bits bigger than 64 bits or a value greater than the given uint64_t
210 /// @returns true iff this constant is greater or equal to the given number.
211 /// @brief Determine if the value is greater or equal to the given number.
212 bool uge(uint64_t Num) {
213 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
216 /// getLimitedValue - If the value is smaller than the specified limit,
217 /// return it, otherwise return the limit value. This causes the value
218 /// to saturate to the limit.
219 /// @returns the min of the value of the constant and the specified value
220 /// @brief Get the constant's value with a saturation limit
221 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
222 return Val.getLimitedValue(Limit);
225 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
226 static inline bool classof(const ConstantInt *) { return true; }
227 static bool classof(const Value *V) {
228 return V->getValueID() == ConstantIntVal;
233 //===----------------------------------------------------------------------===//
234 /// ConstantFP - Floating Point Values [float, double]
236 class ConstantFP : public Constant {
238 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
239 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
240 friend class LLVMContextImpl;
242 ConstantFP(const Type *Ty, const APFloat& V);
244 // allocate space for exactly zero operands
245 void *operator new(size_t s) {
246 return User::operator new(s, 0);
249 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
250 /// method returns the negative zero constant for floating point or vector
251 /// floating point types; for all other types, it returns the null value.
252 static Constant *getZeroValueForNegation(const Type *Ty);
254 /// get() - This returns a ConstantFP, or a vector containing a splat of a
255 /// ConstantFP, for the specified value in the specified type. This should
256 /// only be used for simple constant values like 2.0/1.0 etc, that are
257 /// known-valid both as host double and as the target format.
258 static Constant *get(const Type* Ty, double V);
259 static Constant *get(const Type* Ty, StringRef Str);
260 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
261 static ConstantFP *getNegativeZero(const Type* Ty);
262 static ConstantFP *getInfinity(const Type *Ty, bool Negative = false);
264 /// isValueValidForType - return true if Ty is big enough to represent V.
265 static bool isValueValidForType(const Type *Ty, const APFloat &V);
266 inline const APFloat& getValueAPF() const { return Val; }
268 /// isNullValue - Return true if this is the value that would be returned by
269 /// getNullValue. For ConstantFP, this is +0.0, but not -0.0. To handle the
270 /// two the same, use isZero().
271 virtual bool isNullValue() const;
273 /// isNegativeZeroValue - Return true if the value is what would be returned
274 /// by getZeroValueForNegation.
275 virtual bool isNegativeZeroValue() const {
276 return Val.isZero() && Val.isNegative();
279 /// isZero - Return true if the value is positive or negative zero.
280 bool isZero() const { return Val.isZero(); }
282 /// isNaN - Return true if the value is a NaN.
283 bool isNaN() const { return Val.isNaN(); }
285 /// isExactlyValue - We don't rely on operator== working on double values, as
286 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
287 /// As such, this method can be used to do an exact bit-for-bit comparison of
288 /// two floating point values. The version with a double operand is retained
289 /// because it's so convenient to write isExactlyValue(2.0), but please use
290 /// it only for simple constants.
291 bool isExactlyValue(const APFloat &V) const;
293 bool isExactlyValue(double V) const {
295 // convert is not supported on this type
296 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
299 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
300 return isExactlyValue(FV);
302 /// Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const ConstantFP *) { return true; }
304 static bool classof(const Value *V) {
305 return V->getValueID() == ConstantFPVal;
309 //===----------------------------------------------------------------------===//
310 /// ConstantAggregateZero - All zero aggregate value
312 class ConstantAggregateZero : public Constant {
313 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
314 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
315 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
317 explicit ConstantAggregateZero(const Type *ty)
318 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
320 // allocate space for exactly zero operands
321 void *operator new(size_t s) {
322 return User::operator new(s, 0);
325 static ConstantAggregateZero* get(const Type *Ty);
327 /// isNullValue - Return true if this is the value that would be returned by
329 virtual bool isNullValue() const { return true; }
331 virtual void destroyConstant();
333 /// Methods for support type inquiry through isa, cast, and dyn_cast:
335 static bool classof(const ConstantAggregateZero *) { return true; }
336 static bool classof(const Value *V) {
337 return V->getValueID() == ConstantAggregateZeroVal;
342 //===----------------------------------------------------------------------===//
343 /// ConstantArray - Constant Array Declarations
345 class ConstantArray : public Constant {
346 friend struct ConstantCreator<ConstantArray, ArrayType,
347 std::vector<Constant*> >;
348 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
350 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
352 // ConstantArray accessors
353 static Constant *get(const ArrayType *T, ArrayRef<Constant*> V);
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 /// getAsCString - If this array is isCString(), then this method converts the
391 /// array (without the trailing null byte) to an std::string and returns it.
392 /// Otherwise, it asserts out.
394 std::string getAsCString() const;
396 /// isNullValue - Return true if this is the value that would be returned by
397 /// getNullValue. This always returns false because zero arrays are always
398 /// created as ConstantAggregateZero objects.
399 virtual bool isNullValue() const { return false; }
401 virtual void destroyConstant();
402 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
404 /// Methods for support type inquiry through isa, cast, and dyn_cast:
405 static inline bool classof(const ConstantArray *) { return true; }
406 static bool classof(const Value *V) {
407 return V->getValueID() == ConstantArrayVal;
412 struct OperandTraits<ConstantArray> :
413 public VariadicOperandTraits<ConstantArray> {
416 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
418 //===----------------------------------------------------------------------===//
419 // ConstantStruct - Constant Struct Declarations
421 class ConstantStruct : public Constant {
422 friend struct ConstantCreator<ConstantStruct, StructType,
423 std::vector<Constant*> >;
424 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
426 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
428 // ConstantStruct accessors
429 static Constant *get(const StructType *T, ArrayRef<Constant*> V);
430 static Constant *get(const StructType *T, ...) END_WITH_NULL;
432 /// getAnon - Return an anonymous struct that has the specified
433 /// elements. If the struct is possibly empty, then you must specify a
435 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
436 return get(getTypeForElements(V, Packed), V);
438 static Constant *getAnon(LLVMContext &Ctx,
439 ArrayRef<Constant*> V, bool Packed = false) {
440 return get(getTypeForElements(Ctx, V, Packed), V);
443 /// getTypeForElements - Return an anonymous struct type to use for a constant
444 /// with the specified set of elements. The list must not be empty.
445 static StructType *getTypeForElements(ArrayRef<Constant*> V,
446 bool Packed = false);
447 /// getTypeForElements - This version of the method allows an empty list.
448 static StructType *getTypeForElements(LLVMContext &Ctx,
449 ArrayRef<Constant*> V,
450 bool Packed = false);
452 /// Transparently provide more efficient getOperand methods.
453 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
455 /// getType() specialization - Reduce amount of casting...
457 inline const StructType *getType() const {
458 return reinterpret_cast<const StructType*>(Value::getType());
461 /// isNullValue - Return true if this is the value that would be returned by
462 /// getNullValue. This always returns false because zero structs are always
463 /// created as ConstantAggregateZero objects.
464 virtual bool isNullValue() const {
468 virtual void destroyConstant();
469 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
471 /// Methods for support type inquiry through isa, cast, and dyn_cast:
472 static inline bool classof(const ConstantStruct *) { return true; }
473 static bool classof(const Value *V) {
474 return V->getValueID() == ConstantStructVal;
479 struct OperandTraits<ConstantStruct> :
480 public VariadicOperandTraits<ConstantStruct> {
483 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
486 //===----------------------------------------------------------------------===//
487 /// ConstantVector - Constant Vector Declarations
489 class ConstantVector : public Constant {
490 friend struct ConstantCreator<ConstantVector, VectorType,
491 std::vector<Constant*> >;
492 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
494 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
496 // ConstantVector accessors
497 static Constant *get(ArrayRef<Constant*> V);
499 /// Transparently provide more efficient getOperand methods.
500 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
502 /// getType - Specialize the getType() method to always return a VectorType,
503 /// which reduces the amount of casting needed in parts of the compiler.
505 inline const VectorType *getType() const {
506 return reinterpret_cast<const VectorType*>(Value::getType());
509 /// isNullValue - Return true if this is the value that would be returned by
510 /// getNullValue. This always returns false because zero vectors are always
511 /// created as ConstantAggregateZero objects.
512 virtual bool isNullValue() const { return false; }
514 /// This function will return true iff every element in this vector constant
515 /// is set to all ones.
516 /// @returns true iff this constant's emements are all set to all ones.
517 /// @brief Determine if the value is all ones.
518 bool isAllOnesValue() const;
520 /// getSplatValue - If this is a splat constant, meaning that all of the
521 /// elements have the same value, return that value. Otherwise return NULL.
522 Constant *getSplatValue() const;
524 virtual void destroyConstant();
525 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
527 /// Methods for support type inquiry through isa, cast, and dyn_cast:
528 static inline bool classof(const ConstantVector *) { return true; }
529 static bool classof(const Value *V) {
530 return V->getValueID() == ConstantVectorVal;
535 struct OperandTraits<ConstantVector> :
536 public VariadicOperandTraits<ConstantVector> {
539 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
541 //===----------------------------------------------------------------------===//
542 /// ConstantPointerNull - a constant pointer value that points to null
544 class ConstantPointerNull : public Constant {
545 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
546 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
547 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
549 explicit ConstantPointerNull(const PointerType *T)
550 : Constant(reinterpret_cast<const Type*>(T),
551 Value::ConstantPointerNullVal, 0, 0) {}
554 // allocate space for exactly zero operands
555 void *operator new(size_t s) {
556 return User::operator new(s, 0);
559 /// get() - Static factory methods - Return objects of the specified value
560 static ConstantPointerNull *get(const PointerType *T);
562 /// isNullValue - Return true if this is the value that would be returned by
564 virtual bool isNullValue() const { return true; }
566 virtual void destroyConstant();
568 /// getType - Specialize the getType() method to always return an PointerType,
569 /// which reduces the amount of casting needed in parts of the compiler.
571 inline const PointerType *getType() const {
572 return reinterpret_cast<const PointerType*>(Value::getType());
575 /// Methods for support type inquiry through isa, cast, and dyn_cast:
576 static inline bool classof(const ConstantPointerNull *) { return true; }
577 static bool classof(const Value *V) {
578 return V->getValueID() == ConstantPointerNullVal;
582 /// BlockAddress - The address of a basic block.
584 class BlockAddress : public Constant {
585 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
586 void *operator new(size_t s) { return User::operator new(s, 2); }
587 BlockAddress(Function *F, BasicBlock *BB);
589 /// get - Return a BlockAddress for the specified function and basic block.
590 static BlockAddress *get(Function *F, BasicBlock *BB);
592 /// get - Return a BlockAddress for the specified basic block. The basic
593 /// block must be embedded into a function.
594 static BlockAddress *get(BasicBlock *BB);
596 /// Transparently provide more efficient getOperand methods.
597 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
599 Function *getFunction() const { return (Function*)Op<0>().get(); }
600 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
602 /// isNullValue - Return true if this is the value that would be returned by
604 virtual bool isNullValue() const { return false; }
606 virtual void destroyConstant();
607 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
609 /// Methods for support type inquiry through isa, cast, and dyn_cast:
610 static inline bool classof(const BlockAddress *) { return true; }
611 static inline bool classof(const Value *V) {
612 return V->getValueID() == BlockAddressVal;
617 struct OperandTraits<BlockAddress> :
618 public FixedNumOperandTraits<BlockAddress, 2> {
621 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value)
624 //===----------------------------------------------------------------------===//
625 /// ConstantExpr - a constant value that is initialized with an expression using
626 /// other constant values.
628 /// This class uses the standard Instruction opcodes to define the various
629 /// constant expressions. The Opcode field for the ConstantExpr class is
630 /// maintained in the Value::SubclassData field.
631 class ConstantExpr : public Constant {
632 friend struct ConstantCreator<ConstantExpr,Type,
633 std::pair<unsigned, std::vector<Constant*> > >;
634 friend struct ConvertConstantType<ConstantExpr, Type>;
637 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
638 : Constant(ty, ConstantExprVal, Ops, NumOps) {
639 // Operation type (an Instruction opcode) is stored as the SubclassData.
640 setValueSubclassData(Opcode);
644 // Static methods to construct a ConstantExpr of different kinds. Note that
645 // these methods may return a object that is not an instance of the
646 // ConstantExpr class, because they will attempt to fold the constant
647 // expression into something simpler if possible.
649 /// getAlignOf constant expr - computes the alignment of a type in a target
650 /// independent way (Note: the return type is an i64).
651 static Constant *getAlignOf(const Type *Ty);
653 /// getSizeOf constant expr - computes the (alloc) size of a type (in
654 /// address-units, not bits) in a target independent way (Note: the return
657 static Constant *getSizeOf(const Type *Ty);
659 /// getOffsetOf constant expr - computes the offset of a struct field in a
660 /// target independent way (Note: the return type is an i64).
662 static Constant *getOffsetOf(const StructType *STy, unsigned FieldNo);
664 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
665 /// which supports any aggregate type, and any Constant index.
667 static Constant *getOffsetOf(const Type *Ty, Constant *FieldNo);
669 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
670 static Constant *getFNeg(Constant *C);
671 static Constant *getNot(Constant *C);
672 static Constant *getAdd(Constant *C1, Constant *C2,
673 bool HasNUW = false, bool HasNSW = false);
674 static Constant *getFAdd(Constant *C1, Constant *C2);
675 static Constant *getSub(Constant *C1, Constant *C2,
676 bool HasNUW = false, bool HasNSW = false);
677 static Constant *getFSub(Constant *C1, Constant *C2);
678 static Constant *getMul(Constant *C1, Constant *C2,
679 bool HasNUW = false, bool HasNSW = false);
680 static Constant *getFMul(Constant *C1, Constant *C2);
681 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
682 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
683 static Constant *getFDiv(Constant *C1, Constant *C2);
684 static Constant *getURem(Constant *C1, Constant *C2);
685 static Constant *getSRem(Constant *C1, Constant *C2);
686 static Constant *getFRem(Constant *C1, Constant *C2);
687 static Constant *getAnd(Constant *C1, Constant *C2);
688 static Constant *getOr(Constant *C1, Constant *C2);
689 static Constant *getXor(Constant *C1, Constant *C2);
690 static Constant *getShl(Constant *C1, Constant *C2,
691 bool HasNUW = false, bool HasNSW = false);
692 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
693 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
694 static Constant *getTrunc (Constant *C, const Type *Ty);
695 static Constant *getSExt (Constant *C, const Type *Ty);
696 static Constant *getZExt (Constant *C, const Type *Ty);
697 static Constant *getFPTrunc (Constant *C, const Type *Ty);
698 static Constant *getFPExtend(Constant *C, const Type *Ty);
699 static Constant *getUIToFP (Constant *C, const Type *Ty);
700 static Constant *getSIToFP (Constant *C, const Type *Ty);
701 static Constant *getFPToUI (Constant *C, const Type *Ty);
702 static Constant *getFPToSI (Constant *C, const Type *Ty);
703 static Constant *getPtrToInt(Constant *C, const Type *Ty);
704 static Constant *getIntToPtr(Constant *C, const Type *Ty);
705 static Constant *getBitCast (Constant *C, const Type *Ty);
707 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
708 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
709 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
710 return getAdd(C1, C2, false, true);
712 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
713 return getAdd(C1, C2, true, false);
715 static Constant *getNSWSub(Constant *C1, Constant *C2) {
716 return getSub(C1, C2, false, true);
718 static Constant *getNUWSub(Constant *C1, Constant *C2) {
719 return getSub(C1, C2, true, false);
721 static Constant *getNSWMul(Constant *C1, Constant *C2) {
722 return getMul(C1, C2, false, true);
724 static Constant *getNUWMul(Constant *C1, Constant *C2) {
725 return getMul(C1, C2, true, false);
727 static Constant *getNSWShl(Constant *C1, Constant *C2) {
728 return getShl(C1, C2, false, true);
730 static Constant *getNUWShl(Constant *C1, Constant *C2) {
731 return getShl(C1, C2, true, false);
733 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
734 return getSDiv(C1, C2, true);
736 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
737 return getUDiv(C1, C2, true);
739 static Constant *getExactAShr(Constant *C1, Constant *C2) {
740 return getAShr(C1, C2, true);
742 static Constant *getExactLShr(Constant *C1, Constant *C2) {
743 return getLShr(C1, C2, true);
746 /// Transparently provide more efficient getOperand methods.
747 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
749 // @brief Convenience function for getting one of the casting operations
750 // using a CastOps opcode.
751 static Constant *getCast(
752 unsigned ops, ///< The opcode for the conversion
753 Constant *C, ///< The constant to be converted
754 const Type *Ty ///< The type to which the constant is converted
757 // @brief Create a ZExt or BitCast cast constant expression
758 static Constant *getZExtOrBitCast(
759 Constant *C, ///< The constant to zext or bitcast
760 const Type *Ty ///< The type to zext or bitcast C to
763 // @brief Create a SExt or BitCast cast constant expression
764 static Constant *getSExtOrBitCast(
765 Constant *C, ///< The constant to sext or bitcast
766 const Type *Ty ///< The type to sext or bitcast C to
769 // @brief Create a Trunc or BitCast cast constant expression
770 static Constant *getTruncOrBitCast(
771 Constant *C, ///< The constant to trunc or bitcast
772 const Type *Ty ///< The type to trunc or bitcast C to
775 /// @brief Create a BitCast or a PtrToInt cast constant expression
776 static Constant *getPointerCast(
777 Constant *C, ///< The pointer value to be casted (operand 0)
778 const Type *Ty ///< The type to which cast should be made
781 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
782 static Constant *getIntegerCast(
783 Constant *C, ///< The integer constant to be casted
784 const Type *Ty, ///< The integer type to cast to
785 bool isSigned ///< Whether C should be treated as signed or not
788 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
789 static Constant *getFPCast(
790 Constant *C, ///< The integer constant to be casted
791 const Type *Ty ///< The integer type to cast to
794 /// @brief Return true if this is a convert constant expression
797 /// @brief Return true if this is a compare constant expression
798 bool isCompare() const;
800 /// @brief Return true if this is an insertvalue or extractvalue expression,
801 /// and the getIndices() method may be used.
802 bool hasIndices() const;
804 /// @brief Return true if this is a getelementptr expression and all
805 /// the index operands are compile-time known integers within the
806 /// corresponding notional static array extents. Note that this is
807 /// not equivalant to, a subset of, or a superset of the "inbounds"
809 bool isGEPWithNoNotionalOverIndexing() const;
811 /// Select constant expr
813 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
815 /// get - Return a binary or shift operator constant expression,
816 /// folding if possible.
818 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
821 /// @brief Return an ICmp or FCmp comparison operator constant expression.
822 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
824 /// get* - Return some common constants without having to
825 /// specify the full Instruction::OPCODE identifier.
827 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
828 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
830 /// Getelementptr form. Value* is only accepted for convenience;
831 /// all elements must be Constant's.
833 static Constant *getGetElementPtr(Constant *C,
834 Constant *const *IdxList, unsigned NumIdx,
835 bool InBounds = false) {
836 return getGetElementPtr(C, (Value**)IdxList, NumIdx, InBounds);
838 static Constant *getGetElementPtr(Constant *C,
839 Value *const *IdxList, unsigned NumIdx,
840 bool InBounds = false);
842 /// Create an "inbounds" getelementptr. See the documentation for the
843 /// "inbounds" flag in LangRef.html for details.
844 static Constant *getInBoundsGetElementPtr(Constant *C,
845 Constant *const *IdxList,
847 return getGetElementPtr(C, IdxList, NumIdx, true);
849 static Constant *getInBoundsGetElementPtr(Constant *C,
850 Value* const *IdxList,
852 return getGetElementPtr(C, IdxList, NumIdx, true);
855 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
856 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
857 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
858 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
859 static Constant *getInsertValue(Constant *Agg, Constant *Val,
860 ArrayRef<unsigned> Idxs);
862 /// isNullValue - Return true if this is the value that would be returned by
864 virtual bool isNullValue() const { return false; }
866 /// getOpcode - Return the opcode at the root of this constant expression
867 unsigned getOpcode() const { return getSubclassDataFromValue(); }
869 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
870 /// not an ICMP or FCMP constant expression.
871 unsigned getPredicate() const;
873 /// getIndices - Assert that this is an insertvalue or exactvalue
874 /// expression and return the list of indices.
875 ArrayRef<unsigned> getIndices() const;
877 /// getOpcodeName - Return a string representation for an opcode.
878 const char *getOpcodeName() const;
880 /// getWithOperandReplaced - Return a constant expression identical to this
881 /// one, but with the specified operand set to the specified value.
882 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
884 /// getWithOperands - This returns the current constant expression with the
885 /// operands replaced with the specified values. The specified array must
886 /// have the same number of operands as our current one.
887 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
888 return getWithOperands(Ops, getType());
891 /// getWithOperands - This returns the current constant expression with the
892 /// operands replaced with the specified values and with the specified result
893 /// type. The specified array must have the same number of operands as our
895 Constant *getWithOperands(ArrayRef<Constant*> Ops, const Type *Ty) const;
897 virtual void destroyConstant();
898 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
900 /// Methods for support type inquiry through isa, cast, and dyn_cast:
901 static inline bool classof(const ConstantExpr *) { return true; }
902 static inline bool classof(const Value *V) {
903 return V->getValueID() == ConstantExprVal;
907 // Shadow Value::setValueSubclassData with a private forwarding method so that
908 // subclasses cannot accidentally use it.
909 void setValueSubclassData(unsigned short D) {
910 Value::setValueSubclassData(D);
915 struct OperandTraits<ConstantExpr> :
916 public VariadicOperandTraits<ConstantExpr, 1> {
919 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
921 //===----------------------------------------------------------------------===//
922 /// UndefValue - 'undef' values are things that do not have specified contents.
923 /// These are used for a variety of purposes, including global variable
924 /// initializers and operands to instructions. 'undef' values can occur with
925 /// any first-class type.
927 /// Undef values aren't exactly constants; if they have multiple uses, they
928 /// can appear to have different bit patterns at each use. See
929 /// LangRef.html#undefvalues for details.
931 class UndefValue : public Constant {
932 friend struct ConstantCreator<UndefValue, Type, char>;
933 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
934 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
936 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
938 // allocate space for exactly zero operands
939 void *operator new(size_t s) {
940 return User::operator new(s, 0);
943 /// get() - Static factory methods - Return an 'undef' object of the specified
946 static UndefValue *get(const Type *T);
948 /// isNullValue - Return true if this is the value that would be returned by
950 virtual bool isNullValue() const { return false; }
952 virtual void destroyConstant();
954 /// Methods for support type inquiry through isa, cast, and dyn_cast:
955 static inline bool classof(const UndefValue *) { return true; }
956 static bool classof(const Value *V) {
957 return V->getValueID() == UndefValueVal;
961 } // End llvm namespace