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_IR_CONSTANTS_H
22 #define LLVM_IR_CONSTANTS_H
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/IR/Constant.h"
28 #include "llvm/IR/OperandTraits.h"
29 #include "llvm/IR/DerivedTypes.h"
40 template<class ConstantClass, class TypeClass, class ValType>
41 struct ConstantCreator;
42 template<class ConstantClass, class TypeClass>
43 struct ConstantArrayCreator;
44 template<class ConstantClass, class TypeClass>
45 struct ConvertConstantType;
47 //===----------------------------------------------------------------------===//
48 /// This is the shared class of boolean and integer constants. This class
49 /// represents both boolean and integral constants.
50 /// @brief Class for constant integers.
51 class ConstantInt : public Constant {
52 virtual void anchor();
53 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
54 ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
55 ConstantInt(IntegerType *Ty, const APInt& V);
58 // allocate space for exactly zero operands
59 void *operator new(size_t s) {
60 return User::operator new(s, 0);
63 static ConstantInt *getTrue(LLVMContext &Context);
64 static ConstantInt *getFalse(LLVMContext &Context);
65 static Constant *getTrue(Type *Ty);
66 static Constant *getFalse(Type *Ty);
68 /// If Ty is a vector type, return a Constant with a splat of the given
69 /// value. Otherwise return a ConstantInt for the given value.
70 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
72 /// Return a ConstantInt with the specified integer value for the specified
73 /// type. If the type is wider than 64 bits, the value will be zero-extended
74 /// to fit the type, unless isSigned is true, in which case the value will
75 /// be interpreted as a 64-bit signed integer and sign-extended to fit
77 /// @brief Get a ConstantInt for a specific value.
78 static ConstantInt *get(IntegerType *Ty, uint64_t V,
79 bool isSigned = false);
81 /// Return a ConstantInt with the specified value for the specified type. The
82 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
83 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
84 /// signed value for the type Ty.
85 /// @brief Get a ConstantInt for a specific signed value.
86 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
87 static Constant *getSigned(Type *Ty, int64_t V);
89 /// Return a ConstantInt with the specified value and an implied Type. The
90 /// type is the integer type that corresponds to the bit width of the value.
91 static ConstantInt *get(LLVMContext &Context, const APInt &V);
93 /// Return a ConstantInt constructed from the string strStart with the given
95 static ConstantInt *get(IntegerType *Ty, StringRef Str,
98 /// If Ty is a vector type, return a Constant with a splat of the given
99 /// value. Otherwise return a ConstantInt for the given value.
100 static Constant *get(Type* Ty, const APInt& V);
102 /// Return the constant as an APInt value reference. This allows clients to
103 /// obtain a copy of the value, with all its precision in tact.
104 /// @brief Return the constant's value.
105 inline const APInt &getValue() const {
109 /// getBitWidth - Return the bitwidth of this constant.
110 unsigned getBitWidth() const { return Val.getBitWidth(); }
112 /// Return the constant as a 64-bit unsigned integer value after it
113 /// has been zero extended as appropriate for the type of this constant. Note
114 /// that this method can assert if the value does not fit in 64 bits.
116 /// @brief Return the zero extended value.
117 inline uint64_t getZExtValue() const {
118 return Val.getZExtValue();
121 /// Return the constant as a 64-bit integer value after it has been sign
122 /// extended as appropriate for the type of this constant. Note that
123 /// this method can assert if the value does not fit in 64 bits.
125 /// @brief Return the sign extended value.
126 inline int64_t getSExtValue() const {
127 return Val.getSExtValue();
130 /// A helper method that can be used to determine if the constant contained
131 /// within is equal to a constant. This only works for very small values,
132 /// because this is all that can be represented with all types.
133 /// @brief Determine if this constant's value is same as an unsigned char.
134 bool equalsInt(uint64_t V) const {
138 /// getType - Specialize the getType() method to always return an IntegerType,
139 /// which reduces the amount of casting needed in parts of the compiler.
141 inline IntegerType *getType() const {
142 return cast<IntegerType>(Value::getType());
145 /// This static method returns true if the type Ty is big enough to
146 /// represent the value V. This can be used to avoid having the get method
147 /// assert when V is larger than Ty can represent. Note that there are two
148 /// versions of this method, one for unsigned and one for signed integers.
149 /// Although ConstantInt canonicalizes everything to an unsigned integer,
150 /// the signed version avoids callers having to convert a signed quantity
151 /// to the appropriate unsigned type before calling the method.
152 /// @returns true if V is a valid value for type Ty
153 /// @brief Determine if the value is in range for the given type.
154 static bool isValueValidForType(Type *Ty, uint64_t V);
155 static bool isValueValidForType(Type *Ty, int64_t V);
157 bool isNegative() const { return Val.isNegative(); }
159 /// This is just a convenience method to make client code smaller for a
160 /// common code. It also correctly performs the comparison without the
161 /// potential for an assertion from getZExtValue().
162 bool isZero() const {
166 /// This is just a convenience method to make client code smaller for a
167 /// common case. It also correctly performs the comparison without the
168 /// potential for an assertion from getZExtValue().
169 /// @brief Determine if the value is one.
174 /// This function will return true iff every bit in this constant is set
176 /// @returns true iff this constant's bits are all set to true.
177 /// @brief Determine if the value is all ones.
178 bool isMinusOne() const {
179 return Val.isAllOnesValue();
182 /// This function will return true iff this constant represents the largest
183 /// value that may be represented by the constant's type.
184 /// @returns true iff this is the largest value that may be represented
186 /// @brief Determine if the value is maximal.
187 bool isMaxValue(bool isSigned) const {
189 return Val.isMaxSignedValue();
191 return Val.isMaxValue();
194 /// This function will return true iff this constant represents the smallest
195 /// value that may be represented by this constant's type.
196 /// @returns true if this is the smallest value that may be represented by
198 /// @brief Determine if the value is minimal.
199 bool isMinValue(bool isSigned) const {
201 return Val.isMinSignedValue();
203 return Val.isMinValue();
206 /// This function will return true iff this constant represents a value with
207 /// active bits bigger than 64 bits or a value greater than the given uint64_t
209 /// @returns true iff this constant is greater or equal to the given number.
210 /// @brief Determine if the value is greater or equal to the given number.
211 bool uge(uint64_t Num) const {
212 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
215 /// getLimitedValue - If the value is smaller than the specified limit,
216 /// return it, otherwise return the limit value. This causes the value
217 /// to saturate to the limit.
218 /// @returns the min of the value of the constant and the specified value
219 /// @brief Get the constant's value with a saturation limit
220 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
221 return Val.getLimitedValue(Limit);
224 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
225 static bool classof(const Value *V) {
226 return V->getValueID() == ConstantIntVal;
231 //===----------------------------------------------------------------------===//
232 /// ConstantFP - Floating Point Values [float, double]
234 class ConstantFP : public Constant {
236 virtual void anchor();
237 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
238 ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
239 friend class LLVMContextImpl;
241 ConstantFP(Type *Ty, const APFloat& V);
243 // allocate space for exactly zero operands
244 void *operator new(size_t s) {
245 return User::operator new(s, 0);
248 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
249 /// method returns the negative zero constant for floating point or vector
250 /// floating point types; for all other types, it returns the null value.
251 static Constant *getZeroValueForNegation(Type *Ty);
253 /// get() - This returns a ConstantFP, or a vector containing a splat of a
254 /// ConstantFP, for the specified value in the specified type. This should
255 /// only be used for simple constant values like 2.0/1.0 etc, that are
256 /// known-valid both as host double and as the target format.
257 static Constant *get(Type* Ty, double V);
258 static Constant *get(Type* Ty, StringRef Str);
259 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
260 static ConstantFP *getNegativeZero(Type* Ty);
261 static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
263 /// isValueValidForType - return true if Ty is big enough to represent V.
264 static bool isValueValidForType(Type *Ty, const APFloat &V);
265 inline const APFloat &getValueAPF() const { return Val; }
267 /// isZero - Return true if the value is positive or negative zero.
268 bool isZero() const { return Val.isZero(); }
270 /// isNegative - Return true if the sign bit is set.
271 bool isNegative() const { return Val.isNegative(); }
273 /// isNaN - Return true if the value is a NaN.
274 bool isNaN() const { return Val.isNaN(); }
276 /// isExactlyValue - We don't rely on operator== working on double values, as
277 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
278 /// As such, this method can be used to do an exact bit-for-bit comparison of
279 /// two floating point values. The version with a double operand is retained
280 /// because it's so convenient to write isExactlyValue(2.0), but please use
281 /// it only for simple constants.
282 bool isExactlyValue(const APFloat &V) const;
284 bool isExactlyValue(double V) const {
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 bool classof(const Value *V) {
292 return V->getValueID() == ConstantFPVal;
296 //===----------------------------------------------------------------------===//
297 /// ConstantAggregateZero - All zero aggregate value
299 class ConstantAggregateZero : public Constant {
300 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
301 ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
303 explicit ConstantAggregateZero(Type *ty)
304 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
306 // allocate space for exactly zero operands
307 void *operator new(size_t s) {
308 return User::operator new(s, 0);
311 static ConstantAggregateZero *get(Type *Ty);
313 virtual void destroyConstant();
315 /// getSequentialElement - If this CAZ has array or vector type, return a zero
316 /// with the right element type.
317 Constant *getSequentialElement() const;
319 /// getStructElement - If this CAZ has struct type, return a zero with the
320 /// right element type for the specified element.
321 Constant *getStructElement(unsigned Elt) const;
323 /// getElementValue - Return a zero of the right value for the specified GEP
325 Constant *getElementValue(Constant *C) const;
327 /// getElementValue - Return a zero of the right value for the specified GEP
329 Constant *getElementValue(unsigned Idx) const;
331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
333 static bool classof(const Value *V) {
334 return V->getValueID() == ConstantAggregateZeroVal;
339 //===----------------------------------------------------------------------===//
340 /// ConstantArray - Constant Array Declarations
342 class ConstantArray : public Constant {
343 friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
344 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
346 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
348 // ConstantArray accessors
349 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
354 /// getType - Specialize the getType() method to always return an ArrayType,
355 /// which reduces the amount of casting needed in parts of the compiler.
357 inline ArrayType *getType() const {
358 return cast<ArrayType>(Value::getType());
361 virtual void destroyConstant();
362 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
364 /// Methods for support type inquiry through isa, cast, and dyn_cast:
365 static bool classof(const Value *V) {
366 return V->getValueID() == ConstantArrayVal;
371 struct OperandTraits<ConstantArray> :
372 public VariadicOperandTraits<ConstantArray> {
375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
377 //===----------------------------------------------------------------------===//
378 // ConstantStruct - Constant Struct Declarations
380 class ConstantStruct : public Constant {
381 friend struct ConstantArrayCreator<ConstantStruct, StructType>;
382 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
384 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
386 // ConstantStruct accessors
387 static Constant *get(StructType *T, ArrayRef<Constant*> V);
388 static Constant *get(StructType *T, ...) END_WITH_NULL;
390 /// getAnon - Return an anonymous struct that has the specified
391 /// elements. If the struct is possibly empty, then you must specify a
393 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
394 return get(getTypeForElements(V, Packed), V);
396 static Constant *getAnon(LLVMContext &Ctx,
397 ArrayRef<Constant*> V, bool Packed = false) {
398 return get(getTypeForElements(Ctx, V, Packed), V);
401 /// getTypeForElements - Return an anonymous struct type to use for a constant
402 /// with the specified set of elements. The list must not be empty.
403 static StructType *getTypeForElements(ArrayRef<Constant*> V,
404 bool Packed = false);
405 /// getTypeForElements - This version of the method allows an empty list.
406 static StructType *getTypeForElements(LLVMContext &Ctx,
407 ArrayRef<Constant*> V,
408 bool Packed = false);
410 /// Transparently provide more efficient getOperand methods.
411 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
413 /// getType() specialization - Reduce amount of casting...
415 inline StructType *getType() const {
416 return cast<StructType>(Value::getType());
419 virtual void destroyConstant();
420 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
422 /// Methods for support type inquiry through isa, cast, and dyn_cast:
423 static bool classof(const Value *V) {
424 return V->getValueID() == ConstantStructVal;
429 struct OperandTraits<ConstantStruct> :
430 public VariadicOperandTraits<ConstantStruct> {
433 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
436 //===----------------------------------------------------------------------===//
437 /// ConstantVector - Constant Vector Declarations
439 class ConstantVector : public Constant {
440 friend struct ConstantArrayCreator<ConstantVector, VectorType>;
441 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
443 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
445 // ConstantVector accessors
446 static Constant *get(ArrayRef<Constant*> V);
448 /// getSplat - Return a ConstantVector with the specified constant in each
450 static Constant *getSplat(unsigned NumElts, Constant *Elt);
452 /// Transparently provide more efficient getOperand methods.
453 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
455 /// getType - Specialize the getType() method to always return a VectorType,
456 /// which reduces the amount of casting needed in parts of the compiler.
458 inline VectorType *getType() const {
459 return cast<VectorType>(Value::getType());
462 /// getSplatValue - If this is a splat constant, meaning that all of the
463 /// elements have the same value, return that value. Otherwise return NULL.
464 Constant *getSplatValue() const;
466 virtual void destroyConstant();
467 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
469 /// Methods for support type inquiry through isa, cast, and dyn_cast:
470 static bool classof(const Value *V) {
471 return V->getValueID() == ConstantVectorVal;
476 struct OperandTraits<ConstantVector> :
477 public VariadicOperandTraits<ConstantVector> {
480 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
482 //===----------------------------------------------------------------------===//
483 /// ConstantPointerNull - a constant pointer value that points to null
485 class ConstantPointerNull : public Constant {
486 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
487 ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
489 explicit ConstantPointerNull(PointerType *T)
491 Value::ConstantPointerNullVal, 0, 0) {}
494 // allocate space for exactly zero operands
495 void *operator new(size_t s) {
496 return User::operator new(s, 0);
499 /// get() - Static factory methods - Return objects of the specified value
500 static ConstantPointerNull *get(PointerType *T);
502 virtual void destroyConstant();
504 /// getType - Specialize the getType() method to always return an PointerType,
505 /// which reduces the amount of casting needed in parts of the compiler.
507 inline PointerType *getType() const {
508 return cast<PointerType>(Value::getType());
511 /// Methods for support type inquiry through isa, cast, and dyn_cast:
512 static bool classof(const Value *V) {
513 return V->getValueID() == ConstantPointerNullVal;
517 //===----------------------------------------------------------------------===//
518 /// ConstantDataSequential - A vector or array constant whose element type is a
519 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
520 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
521 /// operands because it stores all of the elements of the constant as densely
522 /// packed data, instead of as Value*'s.
524 /// This is the common base class of ConstantDataArray and ConstantDataVector.
526 class ConstantDataSequential : public Constant {
527 friend class LLVMContextImpl;
528 /// DataElements - A pointer to the bytes underlying this constant (which is
529 /// owned by the uniquing StringMap).
530 const char *DataElements;
532 /// Next - This forms a link list of ConstantDataSequential nodes that have
533 /// the same value but different type. For example, 0,0,0,1 could be a 4
534 /// element array of i8, or a 1-element array of i32. They'll both end up in
535 /// the same StringMap bucket, linked up.
536 ConstantDataSequential *Next;
537 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
538 ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
540 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
541 : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
542 ~ConstantDataSequential() { delete Next; }
544 static Constant *getImpl(StringRef Bytes, Type *Ty);
547 // allocate space for exactly zero operands.
548 void *operator new(size_t s) {
549 return User::operator new(s, 0);
553 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
554 /// formed with a vector or array of the specified element type.
555 /// ConstantDataArray only works with normal float and int types that are
556 /// stored densely in memory, not with things like i42 or x86_f80.
557 static bool isElementTypeCompatible(const Type *Ty);
559 /// getElementAsInteger - If this is a sequential container of integers (of
560 /// any size), return the specified element in the low bits of a uint64_t.
561 uint64_t getElementAsInteger(unsigned i) const;
563 /// getElementAsAPFloat - If this is a sequential container of floating point
564 /// type, return the specified element as an APFloat.
565 APFloat getElementAsAPFloat(unsigned i) const;
567 /// getElementAsFloat - If this is an sequential container of floats, return
568 /// the specified element as a float.
569 float getElementAsFloat(unsigned i) const;
571 /// getElementAsDouble - If this is an sequential container of doubles, return
572 /// the specified element as a double.
573 double getElementAsDouble(unsigned i) const;
575 /// getElementAsConstant - Return a Constant for a specified index's element.
576 /// Note that this has to compute a new constant to return, so it isn't as
577 /// efficient as getElementAsInteger/Float/Double.
578 Constant *getElementAsConstant(unsigned i) const;
580 /// getType - Specialize the getType() method to always return a
581 /// SequentialType, which reduces the amount of casting needed in parts of the
583 inline SequentialType *getType() const {
584 return cast<SequentialType>(Value::getType());
587 /// getElementType - Return the element type of the array/vector.
588 Type *getElementType() const;
590 /// getNumElements - Return the number of elements in the array or vector.
591 unsigned getNumElements() const;
593 /// getElementByteSize - Return the size (in bytes) of each element in the
594 /// array/vector. The size of the elements is known to be a multiple of one
596 uint64_t getElementByteSize() const;
599 /// isString - This method returns true if this is an array of i8.
600 bool isString() const;
602 /// isCString - This method returns true if the array "isString", ends with a
603 /// nul byte, and does not contains any other nul bytes.
604 bool isCString() const;
606 /// getAsString - If this array is isString(), then this method returns the
607 /// array as a StringRef. Otherwise, it asserts out.
609 StringRef getAsString() const {
610 assert(isString() && "Not a string");
611 return getRawDataValues();
614 /// getAsCString - If this array is isCString(), then this method returns the
615 /// array (without the trailing null byte) as a StringRef. Otherwise, it
618 StringRef getAsCString() const {
619 assert(isCString() && "Isn't a C string");
620 StringRef Str = getAsString();
621 return Str.substr(0, Str.size()-1);
624 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
625 /// that this is an extremely tricky thing to work with, as it exposes the
626 /// host endianness of the data elements.
627 StringRef getRawDataValues() const;
629 virtual void destroyConstant();
631 /// Methods for support type inquiry through isa, cast, and dyn_cast:
633 static bool classof(const Value *V) {
634 return V->getValueID() == ConstantDataArrayVal ||
635 V->getValueID() == ConstantDataVectorVal;
638 const char *getElementPointer(unsigned Elt) const;
641 //===----------------------------------------------------------------------===//
642 /// ConstantDataArray - An array constant whose element type is a simple
643 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
644 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
645 /// operands because it stores all of the elements of the constant as densely
646 /// packed data, instead of as Value*'s.
647 class ConstantDataArray : public ConstantDataSequential {
648 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
649 ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
650 virtual void anchor();
651 friend class ConstantDataSequential;
652 explicit ConstantDataArray(Type *ty, const char *Data)
653 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
655 // allocate space for exactly zero operands.
656 void *operator new(size_t s) {
657 return User::operator new(s, 0);
661 /// get() constructors - Return a constant with array type with an element
662 /// count and element type matching the ArrayRef passed in. Note that this
663 /// can return a ConstantAggregateZero object.
664 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
665 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
666 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
667 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
668 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
669 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
671 /// getString - This method constructs a CDS and initializes it with a text
672 /// string. The default behavior (AddNull==true) causes a null terminator to
673 /// be placed at the end of the array (increasing the length of the string by
674 /// one more than the StringRef would normally indicate. Pass AddNull=false
675 /// to disable this behavior.
676 static Constant *getString(LLVMContext &Context, StringRef Initializer,
677 bool AddNull = true);
679 /// getType - Specialize the getType() method to always return an ArrayType,
680 /// which reduces the amount of casting needed in parts of the compiler.
682 inline ArrayType *getType() const {
683 return cast<ArrayType>(Value::getType());
686 /// Methods for support type inquiry through isa, cast, and dyn_cast:
688 static bool classof(const Value *V) {
689 return V->getValueID() == ConstantDataArrayVal;
693 //===----------------------------------------------------------------------===//
694 /// ConstantDataVector - A vector constant whose element type is a simple
695 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
696 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
697 /// operands because it stores all of the elements of the constant as densely
698 /// packed data, instead of as Value*'s.
699 class ConstantDataVector : public ConstantDataSequential {
700 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
701 ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
702 virtual void anchor();
703 friend class ConstantDataSequential;
704 explicit ConstantDataVector(Type *ty, const char *Data)
705 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
707 // allocate space for exactly zero operands.
708 void *operator new(size_t s) {
709 return User::operator new(s, 0);
713 /// get() constructors - Return a constant with vector type with an element
714 /// count and element type matching the ArrayRef passed in. Note that this
715 /// can return a ConstantAggregateZero object.
716 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
717 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
718 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
719 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
720 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
721 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
723 /// getSplat - Return a ConstantVector with the specified constant in each
724 /// element. The specified constant has to be a of a compatible type (i8/i16/
725 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
726 static Constant *getSplat(unsigned NumElts, Constant *Elt);
728 /// getSplatValue - If this is a splat constant, meaning that all of the
729 /// elements have the same value, return that value. Otherwise return NULL.
730 Constant *getSplatValue() const;
732 /// getType - Specialize the getType() method to always return a VectorType,
733 /// which reduces the amount of casting needed in parts of the compiler.
735 inline VectorType *getType() const {
736 return cast<VectorType>(Value::getType());
739 /// Methods for support type inquiry through isa, cast, and dyn_cast:
741 static bool classof(const Value *V) {
742 return V->getValueID() == ConstantDataVectorVal;
748 /// BlockAddress - The address of a basic block.
750 class BlockAddress : public Constant {
751 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
752 void *operator new(size_t s) { return User::operator new(s, 2); }
753 BlockAddress(Function *F, BasicBlock *BB);
755 /// get - Return a BlockAddress for the specified function and basic block.
756 static BlockAddress *get(Function *F, BasicBlock *BB);
758 /// get - Return a BlockAddress for the specified basic block. The basic
759 /// block must be embedded into a function.
760 static BlockAddress *get(BasicBlock *BB);
762 /// Transparently provide more efficient getOperand methods.
763 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
765 Function *getFunction() const { return (Function*)Op<0>().get(); }
766 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
768 virtual void destroyConstant();
769 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
771 /// Methods for support type inquiry through isa, cast, and dyn_cast:
772 static inline bool classof(const Value *V) {
773 return V->getValueID() == BlockAddressVal;
778 struct OperandTraits<BlockAddress> :
779 public FixedNumOperandTraits<BlockAddress, 2> {
782 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
785 //===----------------------------------------------------------------------===//
786 /// ConstantExpr - a constant value that is initialized with an expression using
787 /// other constant values.
789 /// This class uses the standard Instruction opcodes to define the various
790 /// constant expressions. The Opcode field for the ConstantExpr class is
791 /// maintained in the Value::SubclassData field.
792 class ConstantExpr : public Constant {
793 friend struct ConstantCreator<ConstantExpr,Type,
794 std::pair<unsigned, std::vector<Constant*> > >;
795 friend struct ConvertConstantType<ConstantExpr, Type>;
798 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
799 : Constant(ty, ConstantExprVal, Ops, NumOps) {
800 // Operation type (an Instruction opcode) is stored as the SubclassData.
801 setValueSubclassData(Opcode);
805 // Static methods to construct a ConstantExpr of different kinds. Note that
806 // these methods may return a object that is not an instance of the
807 // ConstantExpr class, because they will attempt to fold the constant
808 // expression into something simpler if possible.
810 /// getAlignOf constant expr - computes the alignment of a type in a target
811 /// independent way (Note: the return type is an i64).
812 static Constant *getAlignOf(Type *Ty);
814 /// getSizeOf constant expr - computes the (alloc) size of a type (in
815 /// address-units, not bits) in a target independent way (Note: the return
818 static Constant *getSizeOf(Type *Ty);
820 /// getOffsetOf constant expr - computes the offset of a struct field in a
821 /// target independent way (Note: the return type is an i64).
823 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
825 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
826 /// which supports any aggregate type, and any Constant index.
828 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
830 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
831 static Constant *getFNeg(Constant *C);
832 static Constant *getNot(Constant *C);
833 static Constant *getAdd(Constant *C1, Constant *C2,
834 bool HasNUW = false, bool HasNSW = false);
835 static Constant *getFAdd(Constant *C1, Constant *C2);
836 static Constant *getSub(Constant *C1, Constant *C2,
837 bool HasNUW = false, bool HasNSW = false);
838 static Constant *getFSub(Constant *C1, Constant *C2);
839 static Constant *getMul(Constant *C1, Constant *C2,
840 bool HasNUW = false, bool HasNSW = false);
841 static Constant *getFMul(Constant *C1, Constant *C2);
842 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
843 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
844 static Constant *getFDiv(Constant *C1, Constant *C2);
845 static Constant *getURem(Constant *C1, Constant *C2);
846 static Constant *getSRem(Constant *C1, Constant *C2);
847 static Constant *getFRem(Constant *C1, Constant *C2);
848 static Constant *getAnd(Constant *C1, Constant *C2);
849 static Constant *getOr(Constant *C1, Constant *C2);
850 static Constant *getXor(Constant *C1, Constant *C2);
851 static Constant *getShl(Constant *C1, Constant *C2,
852 bool HasNUW = false, bool HasNSW = false);
853 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
854 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
855 static Constant *getTrunc (Constant *C, Type *Ty);
856 static Constant *getSExt (Constant *C, Type *Ty);
857 static Constant *getZExt (Constant *C, Type *Ty);
858 static Constant *getFPTrunc (Constant *C, Type *Ty);
859 static Constant *getFPExtend(Constant *C, Type *Ty);
860 static Constant *getUIToFP (Constant *C, Type *Ty);
861 static Constant *getSIToFP (Constant *C, Type *Ty);
862 static Constant *getFPToUI (Constant *C, Type *Ty);
863 static Constant *getFPToSI (Constant *C, Type *Ty);
864 static Constant *getPtrToInt(Constant *C, Type *Ty);
865 static Constant *getIntToPtr(Constant *C, Type *Ty);
866 static Constant *getBitCast (Constant *C, Type *Ty);
868 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
869 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
870 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
871 return getAdd(C1, C2, false, true);
873 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
874 return getAdd(C1, C2, true, false);
876 static Constant *getNSWSub(Constant *C1, Constant *C2) {
877 return getSub(C1, C2, false, true);
879 static Constant *getNUWSub(Constant *C1, Constant *C2) {
880 return getSub(C1, C2, true, false);
882 static Constant *getNSWMul(Constant *C1, Constant *C2) {
883 return getMul(C1, C2, false, true);
885 static Constant *getNUWMul(Constant *C1, Constant *C2) {
886 return getMul(C1, C2, true, false);
888 static Constant *getNSWShl(Constant *C1, Constant *C2) {
889 return getShl(C1, C2, false, true);
891 static Constant *getNUWShl(Constant *C1, Constant *C2) {
892 return getShl(C1, C2, true, false);
894 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
895 return getSDiv(C1, C2, true);
897 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
898 return getUDiv(C1, C2, true);
900 static Constant *getExactAShr(Constant *C1, Constant *C2) {
901 return getAShr(C1, C2, true);
903 static Constant *getExactLShr(Constant *C1, Constant *C2) {
904 return getLShr(C1, C2, true);
907 /// getBinOpIdentity - Return the identity for the given binary operation,
908 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
909 /// returns null if the operator doesn't have an identity.
910 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
912 /// getBinOpAbsorber - Return the absorbing element for the given binary
913 /// operation, i.e. a constant C such that X op C = C and C op X = C for
914 /// every X. For example, this returns zero for integer multiplication.
915 /// It returns null if the operator doesn't have an absorbing element.
916 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
918 /// Transparently provide more efficient getOperand methods.
919 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
921 // @brief Convenience function for getting one of the casting operations
922 // using a CastOps opcode.
923 static Constant *getCast(
924 unsigned ops, ///< The opcode for the conversion
925 Constant *C, ///< The constant to be converted
926 Type *Ty ///< The type to which the constant is converted
929 // @brief Create a ZExt or BitCast cast constant expression
930 static Constant *getZExtOrBitCast(
931 Constant *C, ///< The constant to zext or bitcast
932 Type *Ty ///< The type to zext or bitcast C to
935 // @brief Create a SExt or BitCast cast constant expression
936 static Constant *getSExtOrBitCast(
937 Constant *C, ///< The constant to sext or bitcast
938 Type *Ty ///< The type to sext or bitcast C to
941 // @brief Create a Trunc or BitCast cast constant expression
942 static Constant *getTruncOrBitCast(
943 Constant *C, ///< The constant to trunc or bitcast
944 Type *Ty ///< The type to trunc or bitcast C to
947 /// @brief Create a BitCast or a PtrToInt cast constant expression
948 static Constant *getPointerCast(
949 Constant *C, ///< The pointer value to be casted (operand 0)
950 Type *Ty ///< The type to which cast should be made
953 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
954 static Constant *getIntegerCast(
955 Constant *C, ///< The integer constant to be casted
956 Type *Ty, ///< The integer type to cast to
957 bool isSigned ///< Whether C should be treated as signed or not
960 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
961 static Constant *getFPCast(
962 Constant *C, ///< The integer constant to be casted
963 Type *Ty ///< The integer type to cast to
966 /// @brief Return true if this is a convert constant expression
969 /// @brief Return true if this is a compare constant expression
970 bool isCompare() const;
972 /// @brief Return true if this is an insertvalue or extractvalue expression,
973 /// and the getIndices() method may be used.
974 bool hasIndices() const;
976 /// @brief Return true if this is a getelementptr expression and all
977 /// the index operands are compile-time known integers within the
978 /// corresponding notional static array extents. Note that this is
979 /// not equivalant to, a subset of, or a superset of the "inbounds"
981 bool isGEPWithNoNotionalOverIndexing() const;
983 /// Select constant expr
985 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
987 /// get - Return a binary or shift operator constant expression,
988 /// folding if possible.
990 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
993 /// @brief Return an ICmp or FCmp comparison operator constant expression.
994 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
996 /// get* - Return some common constants without having to
997 /// specify the full Instruction::OPCODE identifier.
999 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1000 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1002 /// Getelementptr form. Value* is only accepted for convenience;
1003 /// all elements must be Constant's.
1005 static Constant *getGetElementPtr(Constant *C,
1006 ArrayRef<Constant *> IdxList,
1007 bool InBounds = false) {
1008 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1012 static Constant *getGetElementPtr(Constant *C,
1014 bool InBounds = false) {
1015 // This form of the function only exists to avoid ambiguous overload
1016 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1017 // ArrayRef<Value *>.
1018 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1020 static Constant *getGetElementPtr(Constant *C,
1021 ArrayRef<Value *> IdxList,
1022 bool InBounds = false);
1024 /// Create an "inbounds" getelementptr. See the documentation for the
1025 /// "inbounds" flag in LangRef.html for details.
1026 static Constant *getInBoundsGetElementPtr(Constant *C,
1027 ArrayRef<Constant *> IdxList) {
1028 return getGetElementPtr(C, IdxList, true);
1030 static Constant *getInBoundsGetElementPtr(Constant *C,
1032 // This form of the function only exists to avoid ambiguous overload
1033 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1034 // ArrayRef<Value *>.
1035 return getGetElementPtr(C, Idx, true);
1037 static Constant *getInBoundsGetElementPtr(Constant *C,
1038 ArrayRef<Value *> IdxList) {
1039 return getGetElementPtr(C, IdxList, true);
1042 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1043 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1044 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1045 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1046 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1047 ArrayRef<unsigned> Idxs);
1049 /// getOpcode - Return the opcode at the root of this constant expression
1050 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1052 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1053 /// not an ICMP or FCMP constant expression.
1054 unsigned getPredicate() const;
1056 /// getIndices - Assert that this is an insertvalue or exactvalue
1057 /// expression and return the list of indices.
1058 ArrayRef<unsigned> getIndices() const;
1060 /// getOpcodeName - Return a string representation for an opcode.
1061 const char *getOpcodeName() const;
1063 /// getWithOperandReplaced - Return a constant expression identical to this
1064 /// one, but with the specified operand set to the specified value.
1065 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1067 /// getWithOperands - This returns the current constant expression with the
1068 /// operands replaced with the specified values. The specified array must
1069 /// have the same number of operands as our current one.
1070 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1071 return getWithOperands(Ops, getType());
1074 /// getWithOperands - This returns the current constant expression with the
1075 /// operands replaced with the specified values and with the specified result
1076 /// type. The specified array must have the same number of operands as our
1078 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1080 /// getAsInstruction - Returns an Instruction which implements the same operation
1081 /// as this ConstantExpr. The instruction is not linked to any basic block.
1083 /// A better approach to this could be to have a constructor for Instruction
1084 /// which would take a ConstantExpr parameter, but that would have spread
1085 /// implementation details of ConstantExpr outside of Constants.cpp, which
1086 /// would make it harder to remove ConstantExprs altogether.
1087 Instruction *getAsInstruction();
1089 virtual void destroyConstant();
1090 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1092 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1093 static inline bool classof(const Value *V) {
1094 return V->getValueID() == ConstantExprVal;
1098 // Shadow Value::setValueSubclassData with a private forwarding method so that
1099 // subclasses cannot accidentally use it.
1100 void setValueSubclassData(unsigned short D) {
1101 Value::setValueSubclassData(D);
1106 struct OperandTraits<ConstantExpr> :
1107 public VariadicOperandTraits<ConstantExpr, 1> {
1110 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1112 //===----------------------------------------------------------------------===//
1113 /// UndefValue - 'undef' values are things that do not have specified contents.
1114 /// These are used for a variety of purposes, including global variable
1115 /// initializers and operands to instructions. 'undef' values can occur with
1116 /// any first-class type.
1118 /// Undef values aren't exactly constants; if they have multiple uses, they
1119 /// can appear to have different bit patterns at each use. See
1120 /// LangRef.html#undefvalues for details.
1122 class UndefValue : public Constant {
1123 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1124 UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1126 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1128 // allocate space for exactly zero operands
1129 void *operator new(size_t s) {
1130 return User::operator new(s, 0);
1133 /// get() - Static factory methods - Return an 'undef' object of the specified
1136 static UndefValue *get(Type *T);
1138 /// getSequentialElement - If this Undef has array or vector type, return a
1139 /// undef with the right element type.
1140 UndefValue *getSequentialElement() const;
1142 /// getStructElement - If this undef has struct type, return a undef with the
1143 /// right element type for the specified element.
1144 UndefValue *getStructElement(unsigned Elt) const;
1146 /// getElementValue - Return an undef of the right value for the specified GEP
1148 UndefValue *getElementValue(Constant *C) const;
1150 /// getElementValue - Return an undef of the right value for the specified GEP
1152 UndefValue *getElementValue(unsigned Idx) const;
1154 virtual void destroyConstant();
1156 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1157 static bool classof(const Value *V) {
1158 return V->getValueID() == UndefValueVal;
1162 } // End llvm namespace