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"
39 template<class ConstantClass, class TypeClass, class ValType>
40 struct ConstantCreator;
41 template<class ConstantClass, class TypeClass>
42 struct ConstantArrayCreator;
43 template<class ConstantClass, class TypeClass>
44 struct ConvertConstantType;
46 //===----------------------------------------------------------------------===//
47 /// This is the shared class of boolean and integer constants. This class
48 /// represents both boolean and integral constants.
49 /// @brief Class for constant integers.
50 class ConstantInt : public Constant {
51 virtual void anchor();
52 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
53 ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
54 ConstantInt(IntegerType *Ty, const APInt& V);
57 // allocate space for exactly zero operands
58 void *operator new(size_t s) {
59 return User::operator new(s, 0);
62 static ConstantInt *getTrue(LLVMContext &Context);
63 static ConstantInt *getFalse(LLVMContext &Context);
64 static Constant *getTrue(Type *Ty);
65 static Constant *getFalse(Type *Ty);
67 /// If Ty is a vector type, return a Constant with a splat of the given
68 /// value. Otherwise return a ConstantInt for the given value.
69 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
71 /// Return a ConstantInt with the specified integer value for the specified
72 /// type. If the type is wider than 64 bits, the value will be zero-extended
73 /// to fit the type, unless isSigned is true, in which case the value will
74 /// be interpreted as a 64-bit signed integer and sign-extended to fit
76 /// @brief Get a ConstantInt for a specific value.
77 static ConstantInt *get(IntegerType *Ty, uint64_t V,
78 bool isSigned = false);
80 /// Return a ConstantInt with the specified value for the specified type. The
81 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
82 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
83 /// signed value for the type Ty.
84 /// @brief Get a ConstantInt for a specific signed value.
85 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
86 static Constant *getSigned(Type *Ty, int64_t V);
88 /// Return a ConstantInt with the specified value and an implied Type. The
89 /// type is the integer type that corresponds to the bit width of the value.
90 static ConstantInt *get(LLVMContext &Context, const APInt &V);
92 /// Return a ConstantInt constructed from the string strStart with the given
94 static ConstantInt *get(IntegerType *Ty, StringRef Str,
97 /// If Ty is a vector type, return a Constant with a splat of the given
98 /// value. Otherwise return a ConstantInt for the given value.
99 static Constant *get(Type* Ty, const APInt& V);
101 /// Return the constant as an APInt value reference. This allows clients to
102 /// obtain a copy of the value, with all its precision in tact.
103 /// @brief Return the constant's value.
104 inline const APInt &getValue() const {
108 /// getBitWidth - Return the bitwidth of this constant.
109 unsigned getBitWidth() const { return Val.getBitWidth(); }
111 /// Return the constant as a 64-bit unsigned integer value after it
112 /// has been zero extended as appropriate for the type of this constant. Note
113 /// that this method can assert if the value does not fit in 64 bits.
115 /// @brief Return the zero extended value.
116 inline uint64_t getZExtValue() const {
117 return Val.getZExtValue();
120 /// Return the constant as a 64-bit integer value after it has been sign
121 /// extended as appropriate for the type of this constant. Note that
122 /// this method can assert if the value does not fit in 64 bits.
124 /// @brief Return the sign extended value.
125 inline int64_t getSExtValue() const {
126 return Val.getSExtValue();
129 /// A helper method that can be used to determine if the constant contained
130 /// within is equal to a constant. This only works for very small values,
131 /// because this is all that can be represented with all types.
132 /// @brief Determine if this constant's value is same as an unsigned char.
133 bool equalsInt(uint64_t V) const {
137 /// getType - Specialize the getType() method to always return an IntegerType,
138 /// which reduces the amount of casting needed in parts of the compiler.
140 inline IntegerType *getType() const {
141 return reinterpret_cast<IntegerType*>(Value::getType());
144 /// This static method returns true if the type Ty is big enough to
145 /// represent the value V. This can be used to avoid having the get method
146 /// assert when V is larger than Ty can represent. Note that there are two
147 /// versions of this method, one for unsigned and one for signed integers.
148 /// Although ConstantInt canonicalizes everything to an unsigned integer,
149 /// the signed version avoids callers having to convert a signed quantity
150 /// to the appropriate unsigned type before calling the method.
151 /// @returns true if V is a valid value for type Ty
152 /// @brief Determine if the value is in range for the given type.
153 static bool isValueValidForType(Type *Ty, uint64_t V);
154 static bool isValueValidForType(Type *Ty, int64_t V);
156 bool isNegative() const { return Val.isNegative(); }
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 isMinusOne() 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) const {
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 bool classof(const Value *V) {
225 return V->getValueID() == ConstantIntVal;
230 //===----------------------------------------------------------------------===//
231 /// ConstantFP - Floating Point Values [float, double]
233 class ConstantFP : public Constant {
235 virtual void anchor();
236 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
237 ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
238 friend class LLVMContextImpl;
240 ConstantFP(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(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(Type* Ty, double V);
257 static Constant *get(Type* Ty, StringRef Str);
258 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
259 static ConstantFP *getNegativeZero(Type* Ty);
260 static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
262 /// isValueValidForType - return true if Ty is big enough to represent V.
263 static bool isValueValidForType(Type *Ty, const APFloat &V);
264 inline const APFloat &getValueAPF() const { return Val; }
266 /// isZero - Return true if the value is positive or negative zero.
267 bool isZero() const { return Val.isZero(); }
269 /// isNegative - Return true if the sign bit is set.
270 bool isNegative() const { return Val.isNegative(); }
272 /// isNaN - Return true if the value is a NaN.
273 bool isNaN() const { return Val.isNaN(); }
275 /// isExactlyValue - We don't rely on operator== working on double values, as
276 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
277 /// As such, this method can be used to do an exact bit-for-bit comparison of
278 /// two floating point values. The version with a double operand is retained
279 /// because it's so convenient to write isExactlyValue(2.0), but please use
280 /// it only for simple constants.
281 bool isExactlyValue(const APFloat &V) const;
283 bool isExactlyValue(double V) const {
285 // convert is not supported on this type
286 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
289 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
290 return isExactlyValue(FV);
292 /// Methods for support type inquiry through isa, cast, and dyn_cast:
293 static bool classof(const Value *V) {
294 return V->getValueID() == ConstantFPVal;
298 //===----------------------------------------------------------------------===//
299 /// ConstantAggregateZero - All zero aggregate value
301 class ConstantAggregateZero : public Constant {
302 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
303 ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
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 /// getSequentialElement - If this CAZ has array or vector type, return a zero
318 /// with the right element type.
319 Constant *getSequentialElement() const;
321 /// getStructElement - If this CAZ has struct type, return a zero with the
322 /// right element type for the specified element.
323 Constant *getStructElement(unsigned Elt) const;
325 /// getElementValue - Return a zero of the right value for the specified GEP
327 Constant *getElementValue(Constant *C) const;
329 /// getElementValue - Return a zero of the right value for the specified GEP
331 Constant *getElementValue(unsigned Idx) const;
333 /// Methods for support type inquiry through isa, cast, and dyn_cast:
335 static bool classof(const Value *V) {
336 return V->getValueID() == ConstantAggregateZeroVal;
341 //===----------------------------------------------------------------------===//
342 /// ConstantArray - Constant Array Declarations
344 class ConstantArray : public Constant {
345 friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
346 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
348 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
350 // ConstantArray accessors
351 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
353 /// Transparently provide more efficient getOperand methods.
354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
356 /// getType - Specialize the getType() method to always return an ArrayType,
357 /// which reduces the amount of casting needed in parts of the compiler.
359 inline ArrayType *getType() const {
360 return reinterpret_cast<ArrayType*>(Value::getType());
363 virtual void destroyConstant();
364 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
366 /// Methods for support type inquiry through isa, cast, and dyn_cast:
367 static bool classof(const Value *V) {
368 return V->getValueID() == ConstantArrayVal;
373 struct OperandTraits<ConstantArray> :
374 public VariadicOperandTraits<ConstantArray> {
377 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
379 //===----------------------------------------------------------------------===//
380 // ConstantStruct - Constant Struct Declarations
382 class ConstantStruct : public Constant {
383 friend struct ConstantArrayCreator<ConstantStruct, StructType>;
384 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
386 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
388 // ConstantStruct accessors
389 static Constant *get(StructType *T, ArrayRef<Constant*> V);
390 static Constant *get(StructType *T, ...) END_WITH_NULL;
392 /// getAnon - Return an anonymous struct that has the specified
393 /// elements. If the struct is possibly empty, then you must specify a
395 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
396 return get(getTypeForElements(V, Packed), V);
398 static Constant *getAnon(LLVMContext &Ctx,
399 ArrayRef<Constant*> V, bool Packed = false) {
400 return get(getTypeForElements(Ctx, V, Packed), V);
403 /// getTypeForElements - Return an anonymous struct type to use for a constant
404 /// with the specified set of elements. The list must not be empty.
405 static StructType *getTypeForElements(ArrayRef<Constant*> V,
406 bool Packed = false);
407 /// getTypeForElements - This version of the method allows an empty list.
408 static StructType *getTypeForElements(LLVMContext &Ctx,
409 ArrayRef<Constant*> V,
410 bool Packed = false);
412 /// Transparently provide more efficient getOperand methods.
413 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
415 /// getType() specialization - Reduce amount of casting...
417 inline StructType *getType() const {
418 return reinterpret_cast<StructType*>(Value::getType());
421 virtual void destroyConstant();
422 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
424 /// Methods for support type inquiry through isa, cast, and dyn_cast:
425 static bool classof(const Value *V) {
426 return V->getValueID() == ConstantStructVal;
431 struct OperandTraits<ConstantStruct> :
432 public VariadicOperandTraits<ConstantStruct> {
435 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
438 //===----------------------------------------------------------------------===//
439 /// ConstantVector - Constant Vector Declarations
441 class ConstantVector : public Constant {
442 friend struct ConstantArrayCreator<ConstantVector, VectorType>;
443 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
445 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
447 // ConstantVector accessors
448 static Constant *get(ArrayRef<Constant*> V);
450 /// getSplat - Return a ConstantVector with the specified constant in each
452 static Constant *getSplat(unsigned NumElts, Constant *Elt);
454 /// Transparently provide more efficient getOperand methods.
455 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
457 /// getType - Specialize the getType() method to always return a VectorType,
458 /// which reduces the amount of casting needed in parts of the compiler.
460 inline VectorType *getType() const {
461 return reinterpret_cast<VectorType*>(Value::getType());
464 /// getSplatValue - If this is a splat constant, meaning that all of the
465 /// elements have the same value, return that value. Otherwise return NULL.
466 Constant *getSplatValue() 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 bool classof(const Value *V) {
473 return V->getValueID() == ConstantVectorVal;
478 struct OperandTraits<ConstantVector> :
479 public VariadicOperandTraits<ConstantVector> {
482 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
484 //===----------------------------------------------------------------------===//
485 /// ConstantPointerNull - a constant pointer value that points to null
487 class ConstantPointerNull : public Constant {
488 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
489 ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
491 explicit ConstantPointerNull(PointerType *T)
492 : Constant(reinterpret_cast<Type*>(T),
493 Value::ConstantPointerNullVal, 0, 0) {}
496 // allocate space for exactly zero operands
497 void *operator new(size_t s) {
498 return User::operator new(s, 0);
501 /// get() - Static factory methods - Return objects of the specified value
502 static ConstantPointerNull *get(PointerType *T);
504 virtual void destroyConstant();
506 /// getType - Specialize the getType() method to always return an PointerType,
507 /// which reduces the amount of casting needed in parts of the compiler.
509 inline PointerType *getType() const {
510 return reinterpret_cast<PointerType*>(Value::getType());
513 /// Methods for support type inquiry through isa, cast, and dyn_cast:
514 static bool classof(const Value *V) {
515 return V->getValueID() == ConstantPointerNullVal;
519 //===----------------------------------------------------------------------===//
520 /// ConstantDataSequential - A vector or array constant whose element type is a
521 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
522 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
523 /// operands because it stores all of the elements of the constant as densely
524 /// packed data, instead of as Value*'s.
526 /// This is the common base class of ConstantDataArray and ConstantDataVector.
528 class ConstantDataSequential : public Constant {
529 friend class LLVMContextImpl;
530 /// DataElements - A pointer to the bytes underlying this constant (which is
531 /// owned by the uniquing StringMap).
532 const char *DataElements;
534 /// Next - This forms a link list of ConstantDataSequential nodes that have
535 /// the same value but different type. For example, 0,0,0,1 could be a 4
536 /// element array of i8, or a 1-element array of i32. They'll both end up in
537 /// the same StringMap bucket, linked up.
538 ConstantDataSequential *Next;
539 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
540 ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
542 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
543 : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
544 ~ConstantDataSequential() { delete Next; }
546 static Constant *getImpl(StringRef Bytes, Type *Ty);
549 // allocate space for exactly zero operands.
550 void *operator new(size_t s) {
551 return User::operator new(s, 0);
555 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
556 /// formed with a vector or array of the specified element type.
557 /// ConstantDataArray only works with normal float and int types that are
558 /// stored densely in memory, not with things like i42 or x86_f80.
559 static bool isElementTypeCompatible(const Type *Ty);
561 /// getElementAsInteger - If this is a sequential container of integers (of
562 /// any size), return the specified element in the low bits of a uint64_t.
563 uint64_t getElementAsInteger(unsigned i) const;
565 /// getElementAsAPFloat - If this is a sequential container of floating point
566 /// type, return the specified element as an APFloat.
567 APFloat getElementAsAPFloat(unsigned i) const;
569 /// getElementAsFloat - If this is an sequential container of floats, return
570 /// the specified element as a float.
571 float getElementAsFloat(unsigned i) const;
573 /// getElementAsDouble - If this is an sequential container of doubles, return
574 /// the specified element as a double.
575 double getElementAsDouble(unsigned i) const;
577 /// getElementAsConstant - Return a Constant for a specified index's element.
578 /// Note that this has to compute a new constant to return, so it isn't as
579 /// efficient as getElementAsInteger/Float/Double.
580 Constant *getElementAsConstant(unsigned i) const;
582 /// getType - Specialize the getType() method to always return a
583 /// SequentialType, which reduces the amount of casting needed in parts of the
585 inline SequentialType *getType() const {
586 return reinterpret_cast<SequentialType*>(Value::getType());
589 /// getElementType - Return the element type of the array/vector.
590 Type *getElementType() const;
592 /// getNumElements - Return the number of elements in the array or vector.
593 unsigned getNumElements() const;
595 /// getElementByteSize - Return the size (in bytes) of each element in the
596 /// array/vector. The size of the elements is known to be a multiple of one
598 uint64_t getElementByteSize() const;
601 /// isString - This method returns true if this is an array of i8.
602 bool isString() const;
604 /// isCString - This method returns true if the array "isString", ends with a
605 /// nul byte, and does not contains any other nul bytes.
606 bool isCString() const;
608 /// getAsString - If this array is isString(), then this method returns the
609 /// array as a StringRef. Otherwise, it asserts out.
611 StringRef getAsString() const {
612 assert(isString() && "Not a string");
613 return getRawDataValues();
616 /// getAsCString - If this array is isCString(), then this method returns the
617 /// array (without the trailing null byte) as a StringRef. Otherwise, it
620 StringRef getAsCString() const {
621 assert(isCString() && "Isn't a C string");
622 StringRef Str = getAsString();
623 return Str.substr(0, Str.size()-1);
626 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
627 /// that this is an extremely tricky thing to work with, as it exposes the
628 /// host endianness of the data elements.
629 StringRef getRawDataValues() const;
631 virtual void destroyConstant();
633 /// Methods for support type inquiry through isa, cast, and dyn_cast:
635 static bool classof(const Value *V) {
636 return V->getValueID() == ConstantDataArrayVal ||
637 V->getValueID() == ConstantDataVectorVal;
640 const char *getElementPointer(unsigned Elt) const;
643 //===----------------------------------------------------------------------===//
644 /// ConstantDataArray - An array constant whose element type is a simple
645 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
646 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
647 /// operands because it stores all of the elements of the constant as densely
648 /// packed data, instead of as Value*'s.
649 class ConstantDataArray : public ConstantDataSequential {
650 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
651 ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
652 virtual void anchor();
653 friend class ConstantDataSequential;
654 explicit ConstantDataArray(Type *ty, const char *Data)
655 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
657 // allocate space for exactly zero operands.
658 void *operator new(size_t s) {
659 return User::operator new(s, 0);
663 /// get() constructors - Return a constant with array type with an element
664 /// count and element type matching the ArrayRef passed in. Note that this
665 /// can return a ConstantAggregateZero object.
666 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
667 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
668 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
669 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
670 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
671 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
673 /// getString - This method constructs a CDS and initializes it with a text
674 /// string. The default behavior (AddNull==true) causes a null terminator to
675 /// be placed at the end of the array (increasing the length of the string by
676 /// one more than the StringRef would normally indicate. Pass AddNull=false
677 /// to disable this behavior.
678 static Constant *getString(LLVMContext &Context, StringRef Initializer,
679 bool AddNull = true);
681 /// getType - Specialize the getType() method to always return an ArrayType,
682 /// which reduces the amount of casting needed in parts of the compiler.
684 inline ArrayType *getType() const {
685 return reinterpret_cast<ArrayType*>(Value::getType());
688 /// Methods for support type inquiry through isa, cast, and dyn_cast:
690 static bool classof(const Value *V) {
691 return V->getValueID() == ConstantDataArrayVal;
695 //===----------------------------------------------------------------------===//
696 /// ConstantDataVector - A vector constant whose element type is a simple
697 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
698 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
699 /// operands because it stores all of the elements of the constant as densely
700 /// packed data, instead of as Value*'s.
701 class ConstantDataVector : public ConstantDataSequential {
702 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
703 ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
704 virtual void anchor();
705 friend class ConstantDataSequential;
706 explicit ConstantDataVector(Type *ty, const char *Data)
707 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
709 // allocate space for exactly zero operands.
710 void *operator new(size_t s) {
711 return User::operator new(s, 0);
715 /// get() constructors - Return a constant with vector type with an element
716 /// count and element type matching the ArrayRef passed in. Note that this
717 /// can return a ConstantAggregateZero object.
718 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
719 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
720 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
721 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
722 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
723 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
725 /// getSplat - Return a ConstantVector with the specified constant in each
726 /// element. The specified constant has to be a of a compatible type (i8/i16/
727 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
728 static Constant *getSplat(unsigned NumElts, Constant *Elt);
730 /// getSplatValue - If this is a splat constant, meaning that all of the
731 /// elements have the same value, return that value. Otherwise return NULL.
732 Constant *getSplatValue() const;
734 /// getType - Specialize the getType() method to always return a VectorType,
735 /// which reduces the amount of casting needed in parts of the compiler.
737 inline VectorType *getType() const {
738 return reinterpret_cast<VectorType*>(Value::getType());
741 /// Methods for support type inquiry through isa, cast, and dyn_cast:
743 static bool classof(const Value *V) {
744 return V->getValueID() == ConstantDataVectorVal;
750 /// BlockAddress - The address of a basic block.
752 class BlockAddress : public Constant {
753 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
754 void *operator new(size_t s) { return User::operator new(s, 2); }
755 BlockAddress(Function *F, BasicBlock *BB);
757 /// get - Return a BlockAddress for the specified function and basic block.
758 static BlockAddress *get(Function *F, BasicBlock *BB);
760 /// get - Return a BlockAddress for the specified basic block. The basic
761 /// block must be embedded into a function.
762 static BlockAddress *get(BasicBlock *BB);
764 /// Transparently provide more efficient getOperand methods.
765 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
767 Function *getFunction() const { return (Function*)Op<0>().get(); }
768 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
770 virtual void destroyConstant();
771 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
773 /// Methods for support type inquiry through isa, cast, and dyn_cast:
774 static inline bool classof(const Value *V) {
775 return V->getValueID() == BlockAddressVal;
780 struct OperandTraits<BlockAddress> :
781 public FixedNumOperandTraits<BlockAddress, 2> {
784 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
787 //===----------------------------------------------------------------------===//
788 /// ConstantExpr - a constant value that is initialized with an expression using
789 /// other constant values.
791 /// This class uses the standard Instruction opcodes to define the various
792 /// constant expressions. The Opcode field for the ConstantExpr class is
793 /// maintained in the Value::SubclassData field.
794 class ConstantExpr : public Constant {
795 friend struct ConstantCreator<ConstantExpr,Type,
796 std::pair<unsigned, std::vector<Constant*> > >;
797 friend struct ConvertConstantType<ConstantExpr, Type>;
800 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
801 : Constant(ty, ConstantExprVal, Ops, NumOps) {
802 // Operation type (an Instruction opcode) is stored as the SubclassData.
803 setValueSubclassData(Opcode);
807 // Static methods to construct a ConstantExpr of different kinds. Note that
808 // these methods may return a object that is not an instance of the
809 // ConstantExpr class, because they will attempt to fold the constant
810 // expression into something simpler if possible.
812 /// getAlignOf constant expr - computes the alignment of a type in a target
813 /// independent way (Note: the return type is an i64).
814 static Constant *getAlignOf(Type *Ty);
816 /// getSizeOf constant expr - computes the (alloc) size of a type (in
817 /// address-units, not bits) in a target independent way (Note: the return
820 static Constant *getSizeOf(Type *Ty);
822 /// getOffsetOf constant expr - computes the offset of a struct field in a
823 /// target independent way (Note: the return type is an i64).
825 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
827 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
828 /// which supports any aggregate type, and any Constant index.
830 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
832 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
833 static Constant *getFNeg(Constant *C);
834 static Constant *getNot(Constant *C);
835 static Constant *getAdd(Constant *C1, Constant *C2,
836 bool HasNUW = false, bool HasNSW = false);
837 static Constant *getFAdd(Constant *C1, Constant *C2);
838 static Constant *getSub(Constant *C1, Constant *C2,
839 bool HasNUW = false, bool HasNSW = false);
840 static Constant *getFSub(Constant *C1, Constant *C2);
841 static Constant *getMul(Constant *C1, Constant *C2,
842 bool HasNUW = false, bool HasNSW = false);
843 static Constant *getFMul(Constant *C1, Constant *C2);
844 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
845 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
846 static Constant *getFDiv(Constant *C1, Constant *C2);
847 static Constant *getURem(Constant *C1, Constant *C2);
848 static Constant *getSRem(Constant *C1, Constant *C2);
849 static Constant *getFRem(Constant *C1, Constant *C2);
850 static Constant *getAnd(Constant *C1, Constant *C2);
851 static Constant *getOr(Constant *C1, Constant *C2);
852 static Constant *getXor(Constant *C1, Constant *C2);
853 static Constant *getShl(Constant *C1, Constant *C2,
854 bool HasNUW = false, bool HasNSW = false);
855 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
856 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
857 static Constant *getTrunc (Constant *C, Type *Ty);
858 static Constant *getSExt (Constant *C, Type *Ty);
859 static Constant *getZExt (Constant *C, Type *Ty);
860 static Constant *getFPTrunc (Constant *C, Type *Ty);
861 static Constant *getFPExtend(Constant *C, Type *Ty);
862 static Constant *getUIToFP (Constant *C, Type *Ty);
863 static Constant *getSIToFP (Constant *C, Type *Ty);
864 static Constant *getFPToUI (Constant *C, Type *Ty);
865 static Constant *getFPToSI (Constant *C, Type *Ty);
866 static Constant *getPtrToInt(Constant *C, Type *Ty);
867 static Constant *getIntToPtr(Constant *C, Type *Ty);
868 static Constant *getBitCast (Constant *C, Type *Ty);
870 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
871 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
872 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
873 return getAdd(C1, C2, false, true);
875 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
876 return getAdd(C1, C2, true, false);
878 static Constant *getNSWSub(Constant *C1, Constant *C2) {
879 return getSub(C1, C2, false, true);
881 static Constant *getNUWSub(Constant *C1, Constant *C2) {
882 return getSub(C1, C2, true, false);
884 static Constant *getNSWMul(Constant *C1, Constant *C2) {
885 return getMul(C1, C2, false, true);
887 static Constant *getNUWMul(Constant *C1, Constant *C2) {
888 return getMul(C1, C2, true, false);
890 static Constant *getNSWShl(Constant *C1, Constant *C2) {
891 return getShl(C1, C2, false, true);
893 static Constant *getNUWShl(Constant *C1, Constant *C2) {
894 return getShl(C1, C2, true, false);
896 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
897 return getSDiv(C1, C2, true);
899 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
900 return getUDiv(C1, C2, true);
902 static Constant *getExactAShr(Constant *C1, Constant *C2) {
903 return getAShr(C1, C2, true);
905 static Constant *getExactLShr(Constant *C1, Constant *C2) {
906 return getLShr(C1, C2, true);
909 /// getBinOpIdentity - Return the identity for the given binary operation,
910 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
911 /// returns null if the operator doesn't have an identity.
912 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
914 /// getBinOpAbsorber - Return the absorbing element for the given binary
915 /// operation, i.e. a constant C such that X op C = C and C op X = C for
916 /// every X. For example, this returns zero for integer multiplication.
917 /// It returns null if the operator doesn't have an absorbing element.
918 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
920 /// Transparently provide more efficient getOperand methods.
921 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
923 // @brief Convenience function for getting one of the casting operations
924 // using a CastOps opcode.
925 static Constant *getCast(
926 unsigned ops, ///< The opcode for the conversion
927 Constant *C, ///< The constant to be converted
928 Type *Ty ///< The type to which the constant is converted
931 // @brief Create a ZExt or BitCast cast constant expression
932 static Constant *getZExtOrBitCast(
933 Constant *C, ///< The constant to zext or bitcast
934 Type *Ty ///< The type to zext or bitcast C to
937 // @brief Create a SExt or BitCast cast constant expression
938 static Constant *getSExtOrBitCast(
939 Constant *C, ///< The constant to sext or bitcast
940 Type *Ty ///< The type to sext or bitcast C to
943 // @brief Create a Trunc or BitCast cast constant expression
944 static Constant *getTruncOrBitCast(
945 Constant *C, ///< The constant to trunc or bitcast
946 Type *Ty ///< The type to trunc or bitcast C to
949 /// @brief Create a BitCast or a PtrToInt cast constant expression
950 static Constant *getPointerCast(
951 Constant *C, ///< The pointer value to be casted (operand 0)
952 Type *Ty ///< The type to which cast should be made
955 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
956 static Constant *getIntegerCast(
957 Constant *C, ///< The integer constant to be casted
958 Type *Ty, ///< The integer type to cast to
959 bool isSigned ///< Whether C should be treated as signed or not
962 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
963 static Constant *getFPCast(
964 Constant *C, ///< The integer constant to be casted
965 Type *Ty ///< The integer type to cast to
968 /// @brief Return true if this is a convert constant expression
971 /// @brief Return true if this is a compare constant expression
972 bool isCompare() const;
974 /// @brief Return true if this is an insertvalue or extractvalue expression,
975 /// and the getIndices() method may be used.
976 bool hasIndices() const;
978 /// @brief Return true if this is a getelementptr expression and all
979 /// the index operands are compile-time known integers within the
980 /// corresponding notional static array extents. Note that this is
981 /// not equivalant to, a subset of, or a superset of the "inbounds"
983 bool isGEPWithNoNotionalOverIndexing() const;
985 /// Select constant expr
987 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
989 /// get - Return a binary or shift operator constant expression,
990 /// folding if possible.
992 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
995 /// @brief Return an ICmp or FCmp comparison operator constant expression.
996 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
998 /// get* - Return some common constants without having to
999 /// specify the full Instruction::OPCODE identifier.
1001 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1002 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1004 /// Getelementptr form. Value* is only accepted for convenience;
1005 /// all elements must be Constant's.
1007 static Constant *getGetElementPtr(Constant *C,
1008 ArrayRef<Constant *> IdxList,
1009 bool InBounds = false) {
1010 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1014 static Constant *getGetElementPtr(Constant *C,
1016 bool InBounds = false) {
1017 // This form of the function only exists to avoid ambiguous overload
1018 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1019 // ArrayRef<Value *>.
1020 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1022 static Constant *getGetElementPtr(Constant *C,
1023 ArrayRef<Value *> IdxList,
1024 bool InBounds = false);
1026 /// Create an "inbounds" getelementptr. See the documentation for the
1027 /// "inbounds" flag in LangRef.html for details.
1028 static Constant *getInBoundsGetElementPtr(Constant *C,
1029 ArrayRef<Constant *> IdxList) {
1030 return getGetElementPtr(C, IdxList, true);
1032 static Constant *getInBoundsGetElementPtr(Constant *C,
1034 // This form of the function only exists to avoid ambiguous overload
1035 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1036 // ArrayRef<Value *>.
1037 return getGetElementPtr(C, Idx, true);
1039 static Constant *getInBoundsGetElementPtr(Constant *C,
1040 ArrayRef<Value *> IdxList) {
1041 return getGetElementPtr(C, IdxList, true);
1044 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1045 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1046 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1047 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1048 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1049 ArrayRef<unsigned> Idxs);
1051 /// getOpcode - Return the opcode at the root of this constant expression
1052 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1054 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1055 /// not an ICMP or FCMP constant expression.
1056 unsigned getPredicate() const;
1058 /// getIndices - Assert that this is an insertvalue or exactvalue
1059 /// expression and return the list of indices.
1060 ArrayRef<unsigned> getIndices() const;
1062 /// getOpcodeName - Return a string representation for an opcode.
1063 const char *getOpcodeName() const;
1065 /// getWithOperandReplaced - Return a constant expression identical to this
1066 /// one, but with the specified operand set to the specified value.
1067 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1069 /// getWithOperands - This returns the current constant expression with the
1070 /// operands replaced with the specified values. The specified array must
1071 /// have the same number of operands as our current one.
1072 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1073 return getWithOperands(Ops, getType());
1076 /// getWithOperands - This returns the current constant expression with the
1077 /// operands replaced with the specified values and with the specified result
1078 /// type. The specified array must have the same number of operands as our
1080 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1082 virtual void destroyConstant();
1083 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1085 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1086 static inline bool classof(const Value *V) {
1087 return V->getValueID() == ConstantExprVal;
1091 // Shadow Value::setValueSubclassData with a private forwarding method so that
1092 // subclasses cannot accidentally use it.
1093 void setValueSubclassData(unsigned short D) {
1094 Value::setValueSubclassData(D);
1099 struct OperandTraits<ConstantExpr> :
1100 public VariadicOperandTraits<ConstantExpr, 1> {
1103 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1105 //===----------------------------------------------------------------------===//
1106 /// UndefValue - 'undef' values are things that do not have specified contents.
1107 /// These are used for a variety of purposes, including global variable
1108 /// initializers and operands to instructions. 'undef' values can occur with
1109 /// any first-class type.
1111 /// Undef values aren't exactly constants; if they have multiple uses, they
1112 /// can appear to have different bit patterns at each use. See
1113 /// LangRef.html#undefvalues for details.
1115 class UndefValue : public Constant {
1116 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1117 UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1119 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1121 // allocate space for exactly zero operands
1122 void *operator new(size_t s) {
1123 return User::operator new(s, 0);
1126 /// get() - Static factory methods - Return an 'undef' object of the specified
1129 static UndefValue *get(Type *T);
1131 /// getSequentialElement - If this Undef has array or vector type, return a
1132 /// undef with the right element type.
1133 UndefValue *getSequentialElement() const;
1135 /// getStructElement - If this undef has struct type, return a undef with the
1136 /// right element type for the specified element.
1137 UndefValue *getStructElement(unsigned Elt) const;
1139 /// getElementValue - Return an undef of the right value for the specified GEP
1141 UndefValue *getElementValue(Constant *C) const;
1143 /// getElementValue - Return an undef of the right value for the specified GEP
1145 UndefValue *getElementValue(unsigned Idx) const;
1147 virtual void destroyConstant();
1149 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1150 static bool classof(const Value *V) {
1151 return V->getValueID() == UndefValueVal;
1155 } // End llvm namespace