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/DerivedTypes.h"
29 #include "llvm/IR/OperandTraits.h"
40 struct ConstantExprKeyType;
41 template <class ConstantClass> struct ConstantAggrKeyType;
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 anchor() override;
49 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
50 ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
51 ConstantInt(IntegerType *Ty, const APInt& V);
54 // allocate space for exactly zero operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 0);
59 static ConstantInt *getTrue(LLVMContext &Context);
60 static ConstantInt *getFalse(LLVMContext &Context);
61 static Constant *getTrue(Type *Ty);
62 static Constant *getFalse(Type *Ty);
64 /// If Ty is a vector type, return a Constant with a splat of the given
65 /// value. Otherwise return a ConstantInt for the given value.
66 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
68 /// Return a ConstantInt with the specified integer value for the specified
69 /// type. If the type is wider than 64 bits, the value will be zero-extended
70 /// to fit the type, unless isSigned is true, in which case the value will
71 /// be interpreted as a 64-bit signed integer and sign-extended to fit
73 /// @brief Get a ConstantInt for a specific value.
74 static ConstantInt *get(IntegerType *Ty, uint64_t V,
75 bool isSigned = false);
77 /// Return a ConstantInt with the specified value for the specified type. The
78 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
79 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
80 /// signed value for the type Ty.
81 /// @brief Get a ConstantInt for a specific signed value.
82 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
83 static Constant *getSigned(Type *Ty, int64_t V);
85 /// Return a ConstantInt with the specified value and an implied Type. The
86 /// type is the integer type that corresponds to the bit width of the value.
87 static ConstantInt *get(LLVMContext &Context, const APInt &V);
89 /// Return a ConstantInt constructed from the string strStart with the given
91 static ConstantInt *get(IntegerType *Ty, StringRef Str,
94 /// If Ty is a vector type, return a Constant with a splat of the given
95 /// value. Otherwise return a ConstantInt for the given value.
96 static Constant *get(Type* Ty, const APInt& V);
98 /// Return the constant as an APInt value reference. This allows clients to
99 /// obtain a copy of the value, with all its precision in tact.
100 /// @brief Return the constant's value.
101 inline const APInt &getValue() const {
105 /// getBitWidth - Return the bitwidth of this constant.
106 unsigned getBitWidth() const { return Val.getBitWidth(); }
108 /// Return the constant as a 64-bit unsigned integer value after it
109 /// has been zero extended as appropriate for the type of this constant. Note
110 /// that this method can assert if the value does not fit in 64 bits.
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.
119 /// @brief Return the sign extended value.
120 inline int64_t getSExtValue() const {
121 return Val.getSExtValue();
124 /// A helper method that can be used to determine if the constant contained
125 /// within is equal to a constant. This only works for very small values,
126 /// because this is all that can be represented with all types.
127 /// @brief Determine if this constant's value is same as an unsigned char.
128 bool equalsInt(uint64_t V) const {
132 /// getType - Specialize the getType() method to always return an IntegerType,
133 /// which reduces the amount of casting needed in parts of the compiler.
135 inline IntegerType *getType() const {
136 return cast<IntegerType>(Value::getType());
139 /// This static method returns true if the type Ty is big enough to
140 /// represent the value V. This can be used to avoid having the get method
141 /// assert when V is larger than Ty can represent. Note that there are two
142 /// versions of this method, one for unsigned and one for signed integers.
143 /// Although ConstantInt canonicalizes everything to an unsigned integer,
144 /// the signed version avoids callers having to convert a signed quantity
145 /// to the appropriate unsigned type before calling the method.
146 /// @returns true if V is a valid value for type Ty
147 /// @brief Determine if the value is in range for the given type.
148 static bool isValueValidForType(Type *Ty, uint64_t V);
149 static bool isValueValidForType(Type *Ty, int64_t V);
151 bool isNegative() const { return Val.isNegative(); }
153 /// This is just a convenience method to make client code smaller for a
154 /// common code. It also correctly performs the comparison without the
155 /// potential for an assertion from getZExtValue().
156 bool isZero() const {
160 /// This is just a convenience method to make client code smaller for a
161 /// common case. It also correctly performs the comparison without the
162 /// potential for an assertion from getZExtValue().
163 /// @brief Determine if the value is one.
168 /// This function will return true iff every bit in this constant is set
170 /// @returns true iff this constant's bits are all set to true.
171 /// @brief Determine if the value is all ones.
172 bool isMinusOne() const {
173 return Val.isAllOnesValue();
176 /// This function will return true iff this constant represents the largest
177 /// value that may be represented by the constant's type.
178 /// @returns true iff this is the largest value that may be represented
180 /// @brief Determine if the value is maximal.
181 bool isMaxValue(bool isSigned) const {
183 return Val.isMaxSignedValue();
185 return Val.isMaxValue();
188 /// This function will return true iff this constant represents the smallest
189 /// value that may be represented by this constant's type.
190 /// @returns true if this is the smallest value that may be represented by
192 /// @brief Determine if the value is minimal.
193 bool isMinValue(bool isSigned) const {
195 return Val.isMinSignedValue();
197 return Val.isMinValue();
200 /// This function will return true iff this constant represents a value with
201 /// active bits bigger than 64 bits or a value greater than the given uint64_t
203 /// @returns true iff this constant is greater or equal to the given number.
204 /// @brief Determine if the value is greater or equal to the given number.
205 bool uge(uint64_t Num) const {
206 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
209 /// getLimitedValue - If the value is smaller than the specified limit,
210 /// return it, otherwise return the limit value. This causes the value
211 /// to saturate to the limit.
212 /// @returns the min of the value of the constant and the specified value
213 /// @brief Get the constant's value with a saturation limit
214 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
215 return Val.getLimitedValue(Limit);
218 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
219 static bool classof(const Value *V) {
220 return V->getValueID() == ConstantIntVal;
225 //===----------------------------------------------------------------------===//
226 /// ConstantFP - Floating Point Values [float, double]
228 class ConstantFP : public Constant {
230 void anchor() override;
231 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
232 ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
233 friend class LLVMContextImpl;
235 ConstantFP(Type *Ty, const APFloat& V);
237 // allocate space for exactly zero operands
238 void *operator new(size_t s) {
239 return User::operator new(s, 0);
242 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
243 /// method returns the negative zero constant for floating point or vector
244 /// floating point types; for all other types, it returns the null value.
245 static Constant *getZeroValueForNegation(Type *Ty);
247 /// get() - This returns a ConstantFP, or a vector containing a splat of a
248 /// ConstantFP, for the specified value in the specified type. This should
249 /// only be used for simple constant values like 2.0/1.0 etc, that are
250 /// known-valid both as host double and as the target format.
251 static Constant *get(Type* Ty, double V);
252 static Constant *get(Type* Ty, StringRef Str);
253 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
254 static Constant *getNegativeZero(Type *Ty);
255 static Constant *getInfinity(Type *Ty, bool Negative = false);
257 /// isValueValidForType - return true if Ty is big enough to represent V.
258 static bool isValueValidForType(Type *Ty, const APFloat &V);
259 inline const APFloat &getValueAPF() const { return Val; }
261 /// isZero - Return true if the value is positive or negative zero.
262 bool isZero() const { return Val.isZero(); }
264 /// isNegative - Return true if the sign bit is set.
265 bool isNegative() const { return Val.isNegative(); }
267 /// isInfinity - Return true if the value is infinity
268 bool isInfinity() const { return Val.isInfinity(); }
270 /// isNaN - Return true if the value is a NaN.
271 bool isNaN() const { return Val.isNaN(); }
273 /// isExactlyValue - We don't rely on operator== working on double values, as
274 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
275 /// As such, this method can be used to do an exact bit-for-bit comparison of
276 /// two floating point values. The version with a double operand is retained
277 /// because it's so convenient to write isExactlyValue(2.0), but please use
278 /// it only for simple constants.
279 bool isExactlyValue(const APFloat &V) const;
281 bool isExactlyValue(double V) const {
284 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
285 return isExactlyValue(FV);
287 /// Methods for support type inquiry through isa, cast, and dyn_cast:
288 static bool classof(const Value *V) {
289 return V->getValueID() == ConstantFPVal;
293 //===----------------------------------------------------------------------===//
294 /// ConstantAggregateZero - All zero aggregate value
296 class ConstantAggregateZero : public Constant {
297 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
298 ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
300 explicit ConstantAggregateZero(Type *ty)
301 : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
303 // allocate space for exactly zero operands
304 void *operator new(size_t s) {
305 return User::operator new(s, 0);
308 static ConstantAggregateZero *get(Type *Ty);
310 void destroyConstant() override;
312 /// getSequentialElement - If this CAZ has array or vector type, return a zero
313 /// with the right element type.
314 Constant *getSequentialElement() const;
316 /// getStructElement - If this CAZ has struct type, return a zero with the
317 /// right element type for the specified element.
318 Constant *getStructElement(unsigned Elt) const;
320 /// getElementValue - Return a zero of the right value for the specified GEP
322 Constant *getElementValue(Constant *C) const;
324 /// getElementValue - Return a zero of the right value for the specified GEP
326 Constant *getElementValue(unsigned Idx) const;
328 /// Methods for support type inquiry through isa, cast, and dyn_cast:
330 static bool classof(const Value *V) {
331 return V->getValueID() == ConstantAggregateZeroVal;
336 //===----------------------------------------------------------------------===//
337 /// ConstantArray - Constant Array Declarations
339 class ConstantArray : public Constant {
340 friend struct ConstantAggrKeyType<ConstantArray>;
341 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
343 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
345 // ConstantArray accessors
346 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
349 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
352 /// Transparently provide more efficient getOperand methods.
353 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
355 /// getType - Specialize the getType() method to always return an ArrayType,
356 /// which reduces the amount of casting needed in parts of the compiler.
358 inline ArrayType *getType() const {
359 return cast<ArrayType>(Value::getType());
362 void destroyConstant() override;
363 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
365 /// Methods for support type inquiry through isa, cast, and dyn_cast:
366 static bool classof(const Value *V) {
367 return V->getValueID() == ConstantArrayVal;
372 struct OperandTraits<ConstantArray> :
373 public VariadicOperandTraits<ConstantArray> {
376 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
378 //===----------------------------------------------------------------------===//
379 // ConstantStruct - Constant Struct Declarations
381 class ConstantStruct : public Constant {
382 friend struct ConstantAggrKeyType<ConstantStruct>;
383 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
385 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
387 // ConstantStruct accessors
388 static Constant *get(StructType *T, ArrayRef<Constant*> V);
389 static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
391 /// getAnon - Return an anonymous struct that has the specified
392 /// elements. If the struct is possibly empty, then you must specify a
394 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
395 return get(getTypeForElements(V, Packed), V);
397 static Constant *getAnon(LLVMContext &Ctx,
398 ArrayRef<Constant*> V, bool Packed = false) {
399 return get(getTypeForElements(Ctx, V, Packed), V);
402 /// getTypeForElements - Return an anonymous struct type to use for a constant
403 /// with the specified set of elements. The list must not be empty.
404 static StructType *getTypeForElements(ArrayRef<Constant*> V,
405 bool Packed = false);
406 /// getTypeForElements - This version of the method allows an empty list.
407 static StructType *getTypeForElements(LLVMContext &Ctx,
408 ArrayRef<Constant*> V,
409 bool Packed = false);
411 /// Transparently provide more efficient getOperand methods.
412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
414 /// getType() specialization - Reduce amount of casting...
416 inline StructType *getType() const {
417 return cast<StructType>(Value::getType());
420 void destroyConstant() override;
421 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
423 /// Methods for support type inquiry through isa, cast, and dyn_cast:
424 static bool classof(const Value *V) {
425 return V->getValueID() == ConstantStructVal;
430 struct OperandTraits<ConstantStruct> :
431 public VariadicOperandTraits<ConstantStruct> {
434 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
437 //===----------------------------------------------------------------------===//
438 /// ConstantVector - Constant Vector Declarations
440 class ConstantVector : public Constant {
441 friend struct ConstantAggrKeyType<ConstantVector>;
442 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
444 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
446 // ConstantVector accessors
447 static Constant *get(ArrayRef<Constant*> V);
450 static Constant *getImpl(ArrayRef<Constant *> V);
453 /// getSplat - Return a ConstantVector with the specified constant in each
455 static Constant *getSplat(unsigned NumElts, Constant *Elt);
457 /// Transparently provide more efficient getOperand methods.
458 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
460 /// getType - Specialize the getType() method to always return a VectorType,
461 /// which reduces the amount of casting needed in parts of the compiler.
463 inline VectorType *getType() const {
464 return cast<VectorType>(Value::getType());
467 /// getSplatValue - If this is a splat constant, meaning that all of the
468 /// elements have the same value, return that value. Otherwise return NULL.
469 Constant *getSplatValue() const;
471 void destroyConstant() override;
472 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
474 /// Methods for support type inquiry through isa, cast, and dyn_cast:
475 static bool classof(const Value *V) {
476 return V->getValueID() == ConstantVectorVal;
481 struct OperandTraits<ConstantVector> :
482 public VariadicOperandTraits<ConstantVector> {
485 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
487 //===----------------------------------------------------------------------===//
488 /// ConstantPointerNull - a constant pointer value that points to null
490 class ConstantPointerNull : public Constant {
491 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
492 ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
494 explicit ConstantPointerNull(PointerType *T)
496 Value::ConstantPointerNullVal, nullptr, 0) {}
499 // allocate space for exactly zero operands
500 void *operator new(size_t s) {
501 return User::operator new(s, 0);
504 /// get() - Static factory methods - Return objects of the specified value
505 static ConstantPointerNull *get(PointerType *T);
507 void destroyConstant() override;
509 /// getType - Specialize the getType() method to always return an PointerType,
510 /// which reduces the amount of casting needed in parts of the compiler.
512 inline PointerType *getType() const {
513 return cast<PointerType>(Value::getType());
516 /// Methods for support type inquiry through isa, cast, and dyn_cast:
517 static bool classof(const Value *V) {
518 return V->getValueID() == ConstantPointerNullVal;
522 //===----------------------------------------------------------------------===//
523 /// ConstantDataSequential - A vector or array constant whose element type is a
524 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
525 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
526 /// operands because it stores all of the elements of the constant as densely
527 /// packed data, instead of as Value*'s.
529 /// This is the common base class of ConstantDataArray and ConstantDataVector.
531 class ConstantDataSequential : public Constant {
532 friend class LLVMContextImpl;
533 /// DataElements - A pointer to the bytes underlying this constant (which is
534 /// owned by the uniquing StringMap).
535 const char *DataElements;
537 /// Next - This forms a link list of ConstantDataSequential nodes that have
538 /// the same value but different type. For example, 0,0,0,1 could be a 4
539 /// element array of i8, or a 1-element array of i32. They'll both end up in
540 /// the same StringMap bucket, linked up.
541 ConstantDataSequential *Next;
542 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
543 ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
545 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
546 : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
547 ~ConstantDataSequential() { delete Next; }
549 static Constant *getImpl(StringRef Bytes, Type *Ty);
552 // allocate space for exactly zero operands.
553 void *operator new(size_t s) {
554 return User::operator new(s, 0);
558 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
559 /// formed with a vector or array of the specified element type.
560 /// ConstantDataArray only works with normal float and int types that are
561 /// stored densely in memory, not with things like i42 or x86_f80.
562 static bool isElementTypeCompatible(const Type *Ty);
564 /// getElementAsInteger - If this is a sequential container of integers (of
565 /// any size), return the specified element in the low bits of a uint64_t.
566 uint64_t getElementAsInteger(unsigned i) const;
568 /// getElementAsAPFloat - If this is a sequential container of floating point
569 /// type, return the specified element as an APFloat.
570 APFloat getElementAsAPFloat(unsigned i) const;
572 /// getElementAsFloat - If this is an sequential container of floats, return
573 /// the specified element as a float.
574 float getElementAsFloat(unsigned i) const;
576 /// getElementAsDouble - If this is an sequential container of doubles, return
577 /// the specified element as a double.
578 double getElementAsDouble(unsigned i) const;
580 /// getElementAsConstant - Return a Constant for a specified index's element.
581 /// Note that this has to compute a new constant to return, so it isn't as
582 /// efficient as getElementAsInteger/Float/Double.
583 Constant *getElementAsConstant(unsigned i) const;
585 /// getType - Specialize the getType() method to always return a
586 /// SequentialType, which reduces the amount of casting needed in parts of the
588 inline SequentialType *getType() const {
589 return cast<SequentialType>(Value::getType());
592 /// getElementType - Return the element type of the array/vector.
593 Type *getElementType() const;
595 /// getNumElements - Return the number of elements in the array or vector.
596 unsigned getNumElements() const;
598 /// getElementByteSize - Return the size (in bytes) of each element in the
599 /// array/vector. The size of the elements is known to be a multiple of one
601 uint64_t getElementByteSize() const;
604 /// isString - This method returns true if this is an array of i8.
605 bool isString() const;
607 /// isCString - This method returns true if the array "isString", ends with a
608 /// nul byte, and does not contains any other nul bytes.
609 bool isCString() const;
611 /// getAsString - If this array is isString(), then this method returns the
612 /// array as a StringRef. Otherwise, it asserts out.
614 StringRef getAsString() const {
615 assert(isString() && "Not a string");
616 return getRawDataValues();
619 /// getAsCString - If this array is isCString(), then this method returns the
620 /// array (without the trailing null byte) as a StringRef. Otherwise, it
623 StringRef getAsCString() const {
624 assert(isCString() && "Isn't a C string");
625 StringRef Str = getAsString();
626 return Str.substr(0, Str.size()-1);
629 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
630 /// that this is an extremely tricky thing to work with, as it exposes the
631 /// host endianness of the data elements.
632 StringRef getRawDataValues() const;
634 void destroyConstant() override;
636 /// Methods for support type inquiry through isa, cast, and dyn_cast:
638 static bool classof(const Value *V) {
639 return V->getValueID() == ConstantDataArrayVal ||
640 V->getValueID() == ConstantDataVectorVal;
643 const char *getElementPointer(unsigned Elt) const;
646 //===----------------------------------------------------------------------===//
647 /// ConstantDataArray - An array constant whose element type is a simple
648 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
649 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
650 /// operands because it stores all of the elements of the constant as densely
651 /// packed data, instead of as Value*'s.
652 class ConstantDataArray : public ConstantDataSequential {
653 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
654 ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
655 void anchor() override;
656 friend class ConstantDataSequential;
657 explicit ConstantDataArray(Type *ty, const char *Data)
658 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
660 // allocate space for exactly zero operands.
661 void *operator new(size_t s) {
662 return User::operator new(s, 0);
666 /// get() constructors - Return a constant with array type with an element
667 /// count and element type matching the ArrayRef passed in. Note that this
668 /// can return a ConstantAggregateZero object.
669 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
670 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
671 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
672 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
673 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
674 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
676 /// getString - This method constructs a CDS and initializes it with a text
677 /// string. The default behavior (AddNull==true) causes a null terminator to
678 /// be placed at the end of the array (increasing the length of the string by
679 /// one more than the StringRef would normally indicate. Pass AddNull=false
680 /// to disable this behavior.
681 static Constant *getString(LLVMContext &Context, StringRef Initializer,
682 bool AddNull = true);
684 /// getType - Specialize the getType() method to always return an ArrayType,
685 /// which reduces the amount of casting needed in parts of the compiler.
687 inline ArrayType *getType() const {
688 return cast<ArrayType>(Value::getType());
691 /// Methods for support type inquiry through isa, cast, and dyn_cast:
693 static bool classof(const Value *V) {
694 return V->getValueID() == ConstantDataArrayVal;
698 //===----------------------------------------------------------------------===//
699 /// ConstantDataVector - A vector constant whose element type is a simple
700 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
701 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
702 /// operands because it stores all of the elements of the constant as densely
703 /// packed data, instead of as Value*'s.
704 class ConstantDataVector : public ConstantDataSequential {
705 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
706 ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
707 void anchor() override;
708 friend class ConstantDataSequential;
709 explicit ConstantDataVector(Type *ty, const char *Data)
710 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
712 // allocate space for exactly zero operands.
713 void *operator new(size_t s) {
714 return User::operator new(s, 0);
718 /// get() constructors - Return a constant with vector type with an element
719 /// count and element type matching the ArrayRef passed in. Note that this
720 /// can return a ConstantAggregateZero object.
721 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
722 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
723 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
724 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
725 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
726 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
728 /// getSplat - Return a ConstantVector with the specified constant in each
729 /// element. The specified constant has to be a of a compatible type (i8/i16/
730 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
731 static Constant *getSplat(unsigned NumElts, Constant *Elt);
733 /// getSplatValue - If this is a splat constant, meaning that all of the
734 /// elements have the same value, return that value. Otherwise return NULL.
735 Constant *getSplatValue() const;
737 /// getType - Specialize the getType() method to always return a VectorType,
738 /// which reduces the amount of casting needed in parts of the compiler.
740 inline VectorType *getType() const {
741 return cast<VectorType>(Value::getType());
744 /// Methods for support type inquiry through isa, cast, and dyn_cast:
746 static bool classof(const Value *V) {
747 return V->getValueID() == ConstantDataVectorVal;
753 /// BlockAddress - The address of a basic block.
755 class BlockAddress : public Constant {
756 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
757 void *operator new(size_t s) { return User::operator new(s, 2); }
758 BlockAddress(Function *F, BasicBlock *BB);
760 /// get - Return a BlockAddress for the specified function and basic block.
761 static BlockAddress *get(Function *F, BasicBlock *BB);
763 /// get - Return a BlockAddress for the specified basic block. The basic
764 /// block must be embedded into a function.
765 static BlockAddress *get(BasicBlock *BB);
767 /// \brief Lookup an existing \c BlockAddress constant for the given
770 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
771 static BlockAddress *lookup(const BasicBlock *BB);
773 /// Transparently provide more efficient getOperand methods.
774 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
776 Function *getFunction() const { return (Function*)Op<0>().get(); }
777 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
779 void destroyConstant() override;
780 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
782 /// Methods for support type inquiry through isa, cast, and dyn_cast:
783 static inline bool classof(const Value *V) {
784 return V->getValueID() == BlockAddressVal;
789 struct OperandTraits<BlockAddress> :
790 public FixedNumOperandTraits<BlockAddress, 2> {
793 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
796 //===----------------------------------------------------------------------===//
797 /// ConstantExpr - a constant value that is initialized with an expression using
798 /// other constant values.
800 /// This class uses the standard Instruction opcodes to define the various
801 /// constant expressions. The Opcode field for the ConstantExpr class is
802 /// maintained in the Value::SubclassData field.
803 class ConstantExpr : public Constant {
804 friend struct ConstantExprKeyType;
807 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
808 : Constant(ty, ConstantExprVal, Ops, NumOps) {
809 // Operation type (an Instruction opcode) is stored as the SubclassData.
810 setValueSubclassData(Opcode);
814 // Static methods to construct a ConstantExpr of different kinds. Note that
815 // these methods may return a object that is not an instance of the
816 // ConstantExpr class, because they will attempt to fold the constant
817 // expression into something simpler if possible.
819 /// getAlignOf constant expr - computes the alignment of a type in a target
820 /// independent way (Note: the return type is an i64).
821 static Constant *getAlignOf(Type *Ty);
823 /// getSizeOf constant expr - computes the (alloc) size of a type (in
824 /// address-units, not bits) in a target independent way (Note: the return
827 static Constant *getSizeOf(Type *Ty);
829 /// getOffsetOf constant expr - computes the offset of a struct field in a
830 /// target independent way (Note: the return type is an i64).
832 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
834 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
835 /// which supports any aggregate type, and any Constant index.
837 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
839 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
840 static Constant *getFNeg(Constant *C);
841 static Constant *getNot(Constant *C);
842 static Constant *getAdd(Constant *C1, Constant *C2,
843 bool HasNUW = false, bool HasNSW = false);
844 static Constant *getFAdd(Constant *C1, Constant *C2);
845 static Constant *getSub(Constant *C1, Constant *C2,
846 bool HasNUW = false, bool HasNSW = false);
847 static Constant *getFSub(Constant *C1, Constant *C2);
848 static Constant *getMul(Constant *C1, Constant *C2,
849 bool HasNUW = false, bool HasNSW = false);
850 static Constant *getFMul(Constant *C1, Constant *C2);
851 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
852 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
853 static Constant *getFDiv(Constant *C1, Constant *C2);
854 static Constant *getURem(Constant *C1, Constant *C2);
855 static Constant *getSRem(Constant *C1, Constant *C2);
856 static Constant *getFRem(Constant *C1, Constant *C2);
857 static Constant *getAnd(Constant *C1, Constant *C2);
858 static Constant *getOr(Constant *C1, Constant *C2);
859 static Constant *getXor(Constant *C1, Constant *C2);
860 static Constant *getShl(Constant *C1, Constant *C2,
861 bool HasNUW = false, bool HasNSW = false);
862 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
863 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
864 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
865 static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
866 static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
867 static Constant *getFPTrunc(Constant *C, Type *Ty,
868 bool OnlyIfReduced = false);
869 static Constant *getFPExtend(Constant *C, Type *Ty,
870 bool OnlyIfReduced = false);
871 static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
872 static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
873 static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
874 static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
875 static Constant *getPtrToInt(Constant *C, Type *Ty,
876 bool OnlyIfReduced = false);
877 static Constant *getIntToPtr(Constant *C, Type *Ty,
878 bool OnlyIfReduced = false);
879 static Constant *getBitCast(Constant *C, Type *Ty,
880 bool OnlyIfReduced = false);
881 static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
882 bool OnlyIfReduced = false);
884 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
885 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
886 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
887 return getAdd(C1, C2, false, true);
889 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
890 return getAdd(C1, C2, true, false);
892 static Constant *getNSWSub(Constant *C1, Constant *C2) {
893 return getSub(C1, C2, false, true);
895 static Constant *getNUWSub(Constant *C1, Constant *C2) {
896 return getSub(C1, C2, true, false);
898 static Constant *getNSWMul(Constant *C1, Constant *C2) {
899 return getMul(C1, C2, false, true);
901 static Constant *getNUWMul(Constant *C1, Constant *C2) {
902 return getMul(C1, C2, true, false);
904 static Constant *getNSWShl(Constant *C1, Constant *C2) {
905 return getShl(C1, C2, false, true);
907 static Constant *getNUWShl(Constant *C1, Constant *C2) {
908 return getShl(C1, C2, true, false);
910 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
911 return getSDiv(C1, C2, true);
913 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
914 return getUDiv(C1, C2, true);
916 static Constant *getExactAShr(Constant *C1, Constant *C2) {
917 return getAShr(C1, C2, true);
919 static Constant *getExactLShr(Constant *C1, Constant *C2) {
920 return getLShr(C1, C2, true);
923 /// getBinOpIdentity - Return the identity for the given binary operation,
924 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
925 /// returns null if the operator doesn't have an identity.
926 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
928 /// getBinOpAbsorber - Return the absorbing element for the given binary
929 /// operation, i.e. a constant C such that X op C = C and C op X = C for
930 /// every X. For example, this returns zero for integer multiplication.
931 /// It returns null if the operator doesn't have an absorbing element.
932 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
934 /// Transparently provide more efficient getOperand methods.
935 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
937 /// \brief Convenience function for getting a Cast operation.
939 /// \param ops The opcode for the conversion
940 /// \param C The constant to be converted
941 /// \param Ty The type to which the constant is converted
942 /// \param OnlyIfReduced see \a getWithOperands() docs.
943 static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
944 bool OnlyIfReduced = false);
946 // @brief Create a ZExt or BitCast cast constant expression
947 static Constant *getZExtOrBitCast(
948 Constant *C, ///< The constant to zext or bitcast
949 Type *Ty ///< The type to zext or bitcast C to
952 // @brief Create a SExt or BitCast cast constant expression
953 static Constant *getSExtOrBitCast(
954 Constant *C, ///< The constant to sext or bitcast
955 Type *Ty ///< The type to sext or bitcast C to
958 // @brief Create a Trunc or BitCast cast constant expression
959 static Constant *getTruncOrBitCast(
960 Constant *C, ///< The constant to trunc or bitcast
961 Type *Ty ///< The type to trunc or bitcast C to
964 /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
966 static Constant *getPointerCast(
967 Constant *C, ///< The pointer value to be casted (operand 0)
968 Type *Ty ///< The type to which cast should be made
971 /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
972 /// the address space.
973 static Constant *getPointerBitCastOrAddrSpaceCast(
974 Constant *C, ///< The constant to addrspacecast or bitcast
975 Type *Ty ///< The type to bitcast or addrspacecast C to
978 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
979 static Constant *getIntegerCast(
980 Constant *C, ///< The integer constant to be casted
981 Type *Ty, ///< The integer type to cast to
982 bool isSigned ///< Whether C should be treated as signed or not
985 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
986 static Constant *getFPCast(
987 Constant *C, ///< The integer constant to be casted
988 Type *Ty ///< The integer type to cast to
991 /// @brief Return true if this is a convert constant expression
994 /// @brief Return true if this is a compare constant expression
995 bool isCompare() const;
997 /// @brief Return true if this is an insertvalue or extractvalue expression,
998 /// and the getIndices() method may be used.
999 bool hasIndices() const;
1001 /// @brief Return true if this is a getelementptr expression and all
1002 /// the index operands are compile-time known integers within the
1003 /// corresponding notional static array extents. Note that this is
1004 /// not equivalant to, a subset of, or a superset of the "inbounds"
1006 bool isGEPWithNoNotionalOverIndexing() const;
1008 /// Select constant expr
1010 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1011 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1012 Type *OnlyIfReducedTy = nullptr);
1014 /// get - Return a binary or shift operator constant expression,
1015 /// folding if possible.
1017 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1018 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1019 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1021 /// \brief Return an ICmp or FCmp comparison operator constant expression.
1023 /// \param OnlyIfReduced see \a getWithOperands() docs.
1024 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1025 bool OnlyIfReduced = false);
1027 /// get* - Return some common constants without having to
1028 /// specify the full Instruction::OPCODE identifier.
1030 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1031 bool OnlyIfReduced = false);
1032 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1033 bool OnlyIfReduced = false);
1035 /// Getelementptr form. Value* is only accepted for convenience;
1036 /// all elements must be Constant's.
1038 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1039 static Constant *getGetElementPtr(Constant *C, ArrayRef<Constant *> IdxList,
1040 bool InBounds = false,
1041 Type *OnlyIfReducedTy = nullptr) {
1042 return getGetElementPtr(
1043 C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1044 InBounds, OnlyIfReducedTy);
1046 static Constant *getGetElementPtr(Constant *C, Constant *Idx,
1047 bool InBounds = false,
1048 Type *OnlyIfReducedTy = nullptr) {
1049 // This form of the function only exists to avoid ambiguous overload
1050 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1051 // ArrayRef<Value *>.
1052 return getGetElementPtr(C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
1054 static Constant *getGetElementPtr(Constant *C, ArrayRef<Value *> IdxList,
1055 bool InBounds = false,
1056 Type *OnlyIfReducedTy = nullptr);
1058 /// Create an "inbounds" getelementptr. See the documentation for the
1059 /// "inbounds" flag in LangRef.html for details.
1060 static Constant *getInBoundsGetElementPtr(Constant *C,
1061 ArrayRef<Constant *> IdxList) {
1062 return getGetElementPtr(C, IdxList, true);
1064 static Constant *getInBoundsGetElementPtr(Constant *C,
1066 // This form of the function only exists to avoid ambiguous overload
1067 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1068 // ArrayRef<Value *>.
1069 return getGetElementPtr(C, Idx, true);
1071 static Constant *getInBoundsGetElementPtr(Constant *C,
1072 ArrayRef<Value *> IdxList) {
1073 return getGetElementPtr(C, IdxList, true);
1076 static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1077 Type *OnlyIfReducedTy = nullptr);
1078 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1079 Type *OnlyIfReducedTy = nullptr);
1080 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1081 Type *OnlyIfReducedTy = nullptr);
1082 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1083 Type *OnlyIfReducedTy = nullptr);
1084 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1085 ArrayRef<unsigned> Idxs,
1086 Type *OnlyIfReducedTy = nullptr);
1088 /// getOpcode - Return the opcode at the root of this constant expression
1089 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1091 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1092 /// not an ICMP or FCMP constant expression.
1093 unsigned getPredicate() const;
1095 /// getIndices - Assert that this is an insertvalue or exactvalue
1096 /// expression and return the list of indices.
1097 ArrayRef<unsigned> getIndices() const;
1099 /// getOpcodeName - Return a string representation for an opcode.
1100 const char *getOpcodeName() const;
1102 /// getWithOperandReplaced - Return a constant expression identical to this
1103 /// one, but with the specified operand set to the specified value.
1104 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1106 /// getWithOperands - This returns the current constant expression with the
1107 /// operands replaced with the specified values. The specified array must
1108 /// have the same number of operands as our current one.
1109 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1110 return getWithOperands(Ops, getType());
1113 /// \brief Get the current expression with the operands replaced.
1115 /// Return the current constant expression with the operands replaced with \c
1116 /// Ops and the type with \c Ty. The new operands must have the same number
1117 /// as the current ones.
1119 /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1120 /// gets constant-folded, the type changes, or the expression is otherwise
1121 /// canonicalized. This parameter should almost always be \c false.
1122 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1123 bool OnlyIfReduced = false) const;
1125 /// getAsInstruction - Returns an Instruction which implements the same operation
1126 /// as this ConstantExpr. The instruction is not linked to any basic block.
1128 /// A better approach to this could be to have a constructor for Instruction
1129 /// which would take a ConstantExpr parameter, but that would have spread
1130 /// implementation details of ConstantExpr outside of Constants.cpp, which
1131 /// would make it harder to remove ConstantExprs altogether.
1132 Instruction *getAsInstruction();
1134 void destroyConstant() override;
1135 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
1137 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1138 static inline bool classof(const Value *V) {
1139 return V->getValueID() == ConstantExprVal;
1143 // Shadow Value::setValueSubclassData with a private forwarding method so that
1144 // subclasses cannot accidentally use it.
1145 void setValueSubclassData(unsigned short D) {
1146 Value::setValueSubclassData(D);
1151 struct OperandTraits<ConstantExpr> :
1152 public VariadicOperandTraits<ConstantExpr, 1> {
1155 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1157 //===----------------------------------------------------------------------===//
1158 /// UndefValue - 'undef' values are things that do not have specified contents.
1159 /// These are used for a variety of purposes, including global variable
1160 /// initializers and operands to instructions. 'undef' values can occur with
1161 /// any first-class type.
1163 /// Undef values aren't exactly constants; if they have multiple uses, they
1164 /// can appear to have different bit patterns at each use. See
1165 /// LangRef.html#undefvalues for details.
1167 class UndefValue : public Constant {
1168 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1169 UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1171 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
1173 // allocate space for exactly zero operands
1174 void *operator new(size_t s) {
1175 return User::operator new(s, 0);
1178 /// get() - Static factory methods - Return an 'undef' object of the specified
1181 static UndefValue *get(Type *T);
1183 /// getSequentialElement - If this Undef has array or vector type, return a
1184 /// undef with the right element type.
1185 UndefValue *getSequentialElement() const;
1187 /// getStructElement - If this undef has struct type, return a undef with the
1188 /// right element type for the specified element.
1189 UndefValue *getStructElement(unsigned Elt) const;
1191 /// getElementValue - Return an undef of the right value for the specified GEP
1193 UndefValue *getElementValue(Constant *C) const;
1195 /// getElementValue - Return an undef of the right value for the specified GEP
1197 UndefValue *getElementValue(unsigned Idx) const;
1199 void destroyConstant() override;
1201 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1202 static bool classof(const Value *V) {
1203 return V->getValueID() == UndefValueVal;
1207 } // End llvm namespace