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. Constants 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) = delete;
50 ConstantInt(const ConstantInt &) = delete;
51 ConstantInt(IntegerType *Ty, const APInt& V);
54 friend class Constant;
55 void destroyConstantImpl();
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.
115 /// @brief Return the zero extended value.
116 inline uint64_t getZExtValue() const {
117 return Val.getZExtValue();
120 /// Return the constant as a 64-bit integer value after it has been sign
121 /// extended as appropriate for the type of this constant. Note that
122 /// this method can assert if the value does not fit in 64 bits.
123 /// @brief Return the sign extended value.
124 inline int64_t getSExtValue() const {
125 return Val.getSExtValue();
128 /// A helper method that can be used to determine if the constant contained
129 /// within is equal to a constant. This only works for very small values,
130 /// because this is all that can be represented with all types.
131 /// @brief Determine if this constant's value is same as an unsigned char.
132 bool equalsInt(uint64_t V) const {
136 /// getType - Specialize the getType() method to always return an IntegerType,
137 /// which reduces the amount of casting needed in parts of the compiler.
139 inline IntegerType *getType() const {
140 return cast<IntegerType>(Value::getType());
143 /// This static method returns true if the type Ty is big enough to
144 /// represent the value V. This can be used to avoid having the get method
145 /// assert when V is larger than Ty can represent. Note that there are two
146 /// versions of this method, one for unsigned and one for signed integers.
147 /// Although ConstantInt canonicalizes everything to an unsigned integer,
148 /// the signed version avoids callers having to convert a signed quantity
149 /// to the appropriate unsigned type before calling the method.
150 /// @returns true if V is a valid value for type Ty
151 /// @brief Determine if the value is in range for the given type.
152 static bool isValueValidForType(Type *Ty, uint64_t V);
153 static bool isValueValidForType(Type *Ty, int64_t V);
155 bool isNegative() const { return Val.isNegative(); }
157 /// This is just a convenience method to make client code smaller for a
158 /// common code. It also correctly performs the comparison without the
159 /// potential for an assertion from getZExtValue().
160 bool isZero() const {
164 /// This is just a convenience method to make client code smaller for a
165 /// common case. It also correctly performs the comparison without the
166 /// potential for an assertion from getZExtValue().
167 /// @brief Determine if the value is one.
172 /// This function will return true iff every bit in this constant is set
174 /// @returns true iff this constant's bits are all set to true.
175 /// @brief Determine if the value is all ones.
176 bool isMinusOne() const {
177 return Val.isAllOnesValue();
180 /// This function will return true iff this constant represents the largest
181 /// value that may be represented by the constant's type.
182 /// @returns true iff this is the largest value that may be represented
184 /// @brief Determine if the value is maximal.
185 bool isMaxValue(bool isSigned) const {
187 return Val.isMaxSignedValue();
189 return Val.isMaxValue();
192 /// This function will return true iff this constant represents the smallest
193 /// value that may be represented by this constant's type.
194 /// @returns true if this is the smallest value that may be represented by
196 /// @brief Determine if the value is minimal.
197 bool isMinValue(bool isSigned) const {
199 return Val.isMinSignedValue();
201 return Val.isMinValue();
204 /// This function will return true iff this constant represents a value with
205 /// active bits bigger than 64 bits or a value greater than the given uint64_t
207 /// @returns true iff this constant is greater or equal to the given number.
208 /// @brief Determine if the value is greater or equal to the given number.
209 bool uge(uint64_t Num) const {
210 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
213 /// getLimitedValue - If the value is smaller than the specified limit,
214 /// return it, otherwise return the limit value. This causes the value
215 /// to saturate to the limit.
216 /// @returns the min of the value of the constant and the specified value
217 /// @brief Get the constant's value with a saturation limit
218 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
219 return Val.getLimitedValue(Limit);
222 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
223 static bool classof(const Value *V) {
224 return V->getValueID() == ConstantIntVal;
229 //===----------------------------------------------------------------------===//
230 /// ConstantFP - Floating Point Values [float, double]
232 class ConstantFP : public Constant {
234 void anchor() override;
235 void *operator new(size_t, unsigned) = delete;
236 ConstantFP(const ConstantFP &) = delete;
237 friend class LLVMContextImpl;
239 friend class Constant;
240 void destroyConstantImpl();
243 ConstantFP(Type *Ty, const APFloat& V);
245 // allocate space for exactly zero operands
246 void *operator new(size_t s) {
247 return User::operator new(s, 0);
250 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
251 /// method returns the negative zero constant for floating point or vector
252 /// floating point types; for all other types, it returns the null value.
253 static Constant *getZeroValueForNegation(Type *Ty);
255 /// get() - This returns a ConstantFP, or a vector containing a splat of a
256 /// ConstantFP, for the specified value in the specified type. This should
257 /// only be used for simple constant values like 2.0/1.0 etc, that are
258 /// known-valid both as host double and as the target format.
259 static Constant *get(Type* Ty, double V);
260 static Constant *get(Type* Ty, StringRef Str);
261 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
262 static Constant *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
263 static Constant *getNegativeZero(Type *Ty);
264 static Constant *getInfinity(Type *Ty, bool Negative = false);
266 /// isValueValidForType - return true if Ty is big enough to represent V.
267 static bool isValueValidForType(Type *Ty, const APFloat &V);
268 inline const APFloat &getValueAPF() const { return Val; }
270 /// isZero - Return true if the value is positive or negative zero.
271 bool isZero() const { return Val.isZero(); }
273 /// isNegative - Return true if the sign bit is set.
274 bool isNegative() const { return Val.isNegative(); }
276 /// isInfinity - Return true if the value is infinity
277 bool isInfinity() const { return Val.isInfinity(); }
279 /// isNaN - Return true if the value is a NaN.
280 bool isNaN() const { return Val.isNaN(); }
282 /// isExactlyValue - We don't rely on operator== working on double values, as
283 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
284 /// As such, this method can be used to do an exact bit-for-bit comparison of
285 /// two floating point values. The version with a double operand is retained
286 /// because it's so convenient to write isExactlyValue(2.0), but please use
287 /// it only for simple constants.
288 bool isExactlyValue(const APFloat &V) const;
290 bool isExactlyValue(double V) const {
293 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
294 return isExactlyValue(FV);
296 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static bool classof(const Value *V) {
298 return V->getValueID() == ConstantFPVal;
302 //===----------------------------------------------------------------------===//
303 /// ConstantAggregateZero - All zero aggregate value
305 class ConstantAggregateZero : public Constant {
306 void *operator new(size_t, unsigned) = delete;
307 ConstantAggregateZero(const ConstantAggregateZero &) = delete;
309 friend class Constant;
310 void destroyConstantImpl();
313 explicit ConstantAggregateZero(Type *ty)
314 : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
316 // allocate space for exactly zero operands
317 void *operator new(size_t s) {
318 return User::operator new(s, 0);
321 static ConstantAggregateZero *get(Type *Ty);
323 /// getSequentialElement - If this CAZ has array or vector type, return a zero
324 /// with the right element type.
325 Constant *getSequentialElement() const;
327 /// getStructElement - If this CAZ has struct type, return a zero with the
328 /// right element type for the specified element.
329 Constant *getStructElement(unsigned Elt) const;
331 /// getElementValue - Return a zero of the right value for the specified GEP
333 Constant *getElementValue(Constant *C) const;
335 /// getElementValue - Return a zero of the right value for the specified GEP
337 Constant *getElementValue(unsigned Idx) const;
339 /// \brief Return the number of elements in the array, vector, or struct.
340 unsigned getNumElements() const;
342 /// Methods for support type inquiry through isa, cast, and dyn_cast:
344 static bool classof(const Value *V) {
345 return V->getValueID() == ConstantAggregateZeroVal;
350 //===----------------------------------------------------------------------===//
351 /// ConstantArray - Constant Array Declarations
353 class ConstantArray : public Constant {
354 friend struct ConstantAggrKeyType<ConstantArray>;
355 ConstantArray(const ConstantArray &) = delete;
357 friend class Constant;
358 void destroyConstantImpl();
361 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
363 // ConstantArray accessors
364 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
367 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
370 /// Transparently provide more efficient getOperand methods.
371 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
373 /// getType - Specialize the getType() method to always return an ArrayType,
374 /// which reduces the amount of casting needed in parts of the compiler.
376 inline ArrayType *getType() const {
377 return cast<ArrayType>(Value::getType());
380 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
382 /// Methods for support type inquiry through isa, cast, and dyn_cast:
383 static bool classof(const Value *V) {
384 return V->getValueID() == ConstantArrayVal;
389 struct OperandTraits<ConstantArray> :
390 public VariadicOperandTraits<ConstantArray> {
393 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
395 //===----------------------------------------------------------------------===//
396 // ConstantStruct - Constant Struct Declarations
398 class ConstantStruct : public Constant {
399 friend struct ConstantAggrKeyType<ConstantStruct>;
400 ConstantStruct(const ConstantStruct &) = delete;
402 friend class Constant;
403 void destroyConstantImpl();
406 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
408 // ConstantStruct accessors
409 static Constant *get(StructType *T, ArrayRef<Constant*> V);
410 static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
412 /// getAnon - Return an anonymous struct that has the specified
413 /// elements. If the struct is possibly empty, then you must specify a
415 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
416 return get(getTypeForElements(V, Packed), V);
418 static Constant *getAnon(LLVMContext &Ctx,
419 ArrayRef<Constant*> V, bool Packed = false) {
420 return get(getTypeForElements(Ctx, V, Packed), V);
423 /// getTypeForElements - Return an anonymous struct type to use for a constant
424 /// with the specified set of elements. The list must not be empty.
425 static StructType *getTypeForElements(ArrayRef<Constant*> V,
426 bool Packed = false);
427 /// getTypeForElements - This version of the method allows an empty list.
428 static StructType *getTypeForElements(LLVMContext &Ctx,
429 ArrayRef<Constant*> V,
430 bool Packed = false);
432 /// Transparently provide more efficient getOperand methods.
433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
435 /// getType() specialization - Reduce amount of casting...
437 inline StructType *getType() const {
438 return cast<StructType>(Value::getType());
441 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
443 /// Methods for support type inquiry through isa, cast, and dyn_cast:
444 static bool classof(const Value *V) {
445 return V->getValueID() == ConstantStructVal;
450 struct OperandTraits<ConstantStruct> :
451 public VariadicOperandTraits<ConstantStruct> {
454 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
457 //===----------------------------------------------------------------------===//
458 /// ConstantVector - Constant Vector Declarations
460 class ConstantVector : public Constant {
461 friend struct ConstantAggrKeyType<ConstantVector>;
462 ConstantVector(const ConstantVector &) = delete;
464 friend class Constant;
465 void destroyConstantImpl();
468 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
470 // ConstantVector accessors
471 static Constant *get(ArrayRef<Constant*> V);
474 static Constant *getImpl(ArrayRef<Constant *> V);
477 /// getSplat - Return a ConstantVector with the specified constant in each
479 static Constant *getSplat(unsigned NumElts, Constant *Elt);
481 /// Transparently provide more efficient getOperand methods.
482 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
484 /// getType - Specialize the getType() method to always return a VectorType,
485 /// which reduces the amount of casting needed in parts of the compiler.
487 inline VectorType *getType() const {
488 return cast<VectorType>(Value::getType());
491 /// getSplatValue - If this is a splat constant, meaning that all of the
492 /// elements have the same value, return that value. Otherwise return NULL.
493 Constant *getSplatValue() const;
495 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
497 /// Methods for support type inquiry through isa, cast, and dyn_cast:
498 static bool classof(const Value *V) {
499 return V->getValueID() == ConstantVectorVal;
504 struct OperandTraits<ConstantVector> :
505 public VariadicOperandTraits<ConstantVector> {
508 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
510 //===----------------------------------------------------------------------===//
511 /// ConstantPointerNull - a constant pointer value that points to null
513 class ConstantPointerNull : public Constant {
514 void *operator new(size_t, unsigned) = delete;
515 ConstantPointerNull(const ConstantPointerNull &) = delete;
517 friend class Constant;
518 void destroyConstantImpl();
521 explicit ConstantPointerNull(PointerType *T)
523 Value::ConstantPointerNullVal, nullptr, 0) {}
526 // allocate space for exactly zero operands
527 void *operator new(size_t s) {
528 return User::operator new(s, 0);
531 /// get() - Static factory methods - Return objects of the specified value
532 static ConstantPointerNull *get(PointerType *T);
534 /// getType - Specialize the getType() method to always return an PointerType,
535 /// which reduces the amount of casting needed in parts of the compiler.
537 inline PointerType *getType() const {
538 return cast<PointerType>(Value::getType());
541 /// Methods for support type inquiry through isa, cast, and dyn_cast:
542 static bool classof(const Value *V) {
543 return V->getValueID() == ConstantPointerNullVal;
547 //===----------------------------------------------------------------------===//
548 /// ConstantDataSequential - A vector or array constant whose element type is a
549 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
550 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
551 /// operands because it stores all of the elements of the constant as densely
552 /// packed data, instead of as Value*'s.
554 /// This is the common base class of ConstantDataArray and ConstantDataVector.
556 class ConstantDataSequential : public Constant {
557 friend class LLVMContextImpl;
558 /// DataElements - A pointer to the bytes underlying this constant (which is
559 /// owned by the uniquing StringMap).
560 const char *DataElements;
562 /// Next - This forms a link list of ConstantDataSequential nodes that have
563 /// the same value but different type. For example, 0,0,0,1 could be a 4
564 /// element array of i8, or a 1-element array of i32. They'll both end up in
565 /// the same StringMap bucket, linked up.
566 ConstantDataSequential *Next;
567 void *operator new(size_t, unsigned) = delete;
568 ConstantDataSequential(const ConstantDataSequential &) = delete;
570 friend class Constant;
571 void destroyConstantImpl();
574 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
575 : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
576 ~ConstantDataSequential() override { delete Next; }
578 static Constant *getImpl(StringRef Bytes, Type *Ty);
581 // allocate space for exactly zero operands.
582 void *operator new(size_t s) {
583 return User::operator new(s, 0);
587 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
588 /// formed with a vector or array of the specified element type.
589 /// ConstantDataArray only works with normal float and int types that are
590 /// stored densely in memory, not with things like i42 or x86_f80.
591 static bool isElementTypeCompatible(const Type *Ty);
593 /// getElementAsInteger - If this is a sequential container of integers (of
594 /// any size), return the specified element in the low bits of a uint64_t.
595 uint64_t getElementAsInteger(unsigned i) const;
597 /// getElementAsAPFloat - If this is a sequential container of floating point
598 /// type, return the specified element as an APFloat.
599 APFloat getElementAsAPFloat(unsigned i) const;
601 /// getElementAsFloat - If this is an sequential container of floats, return
602 /// the specified element as a float.
603 float getElementAsFloat(unsigned i) const;
605 /// getElementAsDouble - If this is an sequential container of doubles, return
606 /// the specified element as a double.
607 double getElementAsDouble(unsigned i) const;
609 /// getElementAsConstant - Return a Constant for a specified index's element.
610 /// Note that this has to compute a new constant to return, so it isn't as
611 /// efficient as getElementAsInteger/Float/Double.
612 Constant *getElementAsConstant(unsigned i) const;
614 /// getType - Specialize the getType() method to always return a
615 /// SequentialType, which reduces the amount of casting needed in parts of the
617 inline SequentialType *getType() const {
618 return cast<SequentialType>(Value::getType());
621 /// getElementType - Return the element type of the array/vector.
622 Type *getElementType() const;
624 /// getNumElements - Return the number of elements in the array or vector.
625 unsigned getNumElements() const;
627 /// getElementByteSize - Return the size (in bytes) of each element in the
628 /// array/vector. The size of the elements is known to be a multiple of one
630 uint64_t getElementByteSize() const;
633 /// isString - This method returns true if this is an array of i8.
634 bool isString() const;
636 /// isCString - This method returns true if the array "isString", ends with a
637 /// nul byte, and does not contains any other nul bytes.
638 bool isCString() const;
640 /// getAsString - If this array is isString(), then this method returns the
641 /// array as a StringRef. Otherwise, it asserts out.
643 StringRef getAsString() const {
644 assert(isString() && "Not a string");
645 return getRawDataValues();
648 /// getAsCString - If this array is isCString(), then this method returns the
649 /// array (without the trailing null byte) as a StringRef. Otherwise, it
652 StringRef getAsCString() const {
653 assert(isCString() && "Isn't a C string");
654 StringRef Str = getAsString();
655 return Str.substr(0, Str.size()-1);
658 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
659 /// that this is an extremely tricky thing to work with, as it exposes the
660 /// host endianness of the data elements.
661 StringRef getRawDataValues() const;
663 /// Methods for support type inquiry through isa, cast, and dyn_cast:
665 static bool classof(const Value *V) {
666 return V->getValueID() == ConstantDataArrayVal ||
667 V->getValueID() == ConstantDataVectorVal;
670 const char *getElementPointer(unsigned Elt) const;
673 //===----------------------------------------------------------------------===//
674 /// ConstantDataArray - An array constant whose element type is a simple
675 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
676 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
677 /// operands because it stores all of the elements of the constant as densely
678 /// packed data, instead of as Value*'s.
679 class ConstantDataArray : public ConstantDataSequential {
680 void *operator new(size_t, unsigned) = delete;
681 ConstantDataArray(const ConstantDataArray &) = delete;
682 void anchor() override;
683 friend class ConstantDataSequential;
684 explicit ConstantDataArray(Type *ty, const char *Data)
685 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
687 // allocate space for exactly zero operands.
688 void *operator new(size_t s) {
689 return User::operator new(s, 0);
693 /// get() constructors - Return a constant with array type with an element
694 /// count and element type matching the ArrayRef passed in. Note that this
695 /// can return a ConstantAggregateZero object.
696 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
697 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
698 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
699 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
700 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
701 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
703 /// getFP() constructors - Return a constant with array type with an element
704 /// count and element type of float with precision matching the number of
705 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
706 /// double for 64bits) Note that this can return a ConstantAggregateZero
708 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
709 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
710 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
712 /// getString - This method constructs a CDS and initializes it with a text
713 /// string. The default behavior (AddNull==true) causes a null terminator to
714 /// be placed at the end of the array (increasing the length of the string by
715 /// one more than the StringRef would normally indicate. Pass AddNull=false
716 /// to disable this behavior.
717 static Constant *getString(LLVMContext &Context, StringRef Initializer,
718 bool AddNull = true);
720 /// getType - Specialize the getType() method to always return an ArrayType,
721 /// which reduces the amount of casting needed in parts of the compiler.
723 inline ArrayType *getType() const {
724 return cast<ArrayType>(Value::getType());
727 /// Methods for support type inquiry through isa, cast, and dyn_cast:
729 static bool classof(const Value *V) {
730 return V->getValueID() == ConstantDataArrayVal;
734 //===----------------------------------------------------------------------===//
735 /// ConstantDataVector - A vector constant whose element type is a simple
736 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
737 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
738 /// operands because it stores all of the elements of the constant as densely
739 /// packed data, instead of as Value*'s.
740 class ConstantDataVector : public ConstantDataSequential {
741 void *operator new(size_t, unsigned) = delete;
742 ConstantDataVector(const ConstantDataVector &) = delete;
743 void anchor() override;
744 friend class ConstantDataSequential;
745 explicit ConstantDataVector(Type *ty, const char *Data)
746 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
748 // allocate space for exactly zero operands.
749 void *operator new(size_t s) {
750 return User::operator new(s, 0);
754 /// get() constructors - Return a constant with vector type with an element
755 /// count and element type matching the ArrayRef passed in. Note that this
756 /// can return a ConstantAggregateZero object.
757 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
758 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
759 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
760 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
761 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
762 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
764 /// getFP() constructors - Return a constant with vector type with an element
765 /// count and element type of float with the precision matching the number of
766 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
767 /// double for 64bits) Note that this can return a ConstantAggregateZero
769 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
770 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
771 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
773 /// getSplat - Return a ConstantVector with the specified constant in each
774 /// element. The specified constant has to be a of a compatible type (i8/i16/
775 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
776 static Constant *getSplat(unsigned NumElts, Constant *Elt);
778 /// getSplatValue - If this is a splat constant, meaning that all of the
779 /// elements have the same value, return that value. Otherwise return NULL.
780 Constant *getSplatValue() const;
782 /// getType - Specialize the getType() method to always return a VectorType,
783 /// which reduces the amount of casting needed in parts of the compiler.
785 inline VectorType *getType() const {
786 return cast<VectorType>(Value::getType());
789 /// Methods for support type inquiry through isa, cast, and dyn_cast:
791 static bool classof(const Value *V) {
792 return V->getValueID() == ConstantDataVectorVal;
798 /// BlockAddress - The address of a basic block.
800 class BlockAddress : public Constant {
801 void *operator new(size_t, unsigned) = delete;
802 void *operator new(size_t s) { return User::operator new(s, 2); }
803 BlockAddress(Function *F, BasicBlock *BB);
805 friend class Constant;
806 void destroyConstantImpl();
809 /// get - Return a BlockAddress for the specified function and basic block.
810 static BlockAddress *get(Function *F, BasicBlock *BB);
812 /// get - Return a BlockAddress for the specified basic block. The basic
813 /// block must be embedded into a function.
814 static BlockAddress *get(BasicBlock *BB);
816 /// \brief Lookup an existing \c BlockAddress constant for the given
819 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
820 static BlockAddress *lookup(const BasicBlock *BB);
822 /// Transparently provide more efficient getOperand methods.
823 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
825 Function *getFunction() const { return (Function*)Op<0>().get(); }
826 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
828 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
830 /// Methods for support type inquiry through isa, cast, and dyn_cast:
831 static inline bool classof(const Value *V) {
832 return V->getValueID() == BlockAddressVal;
837 struct OperandTraits<BlockAddress> :
838 public FixedNumOperandTraits<BlockAddress, 2> {
841 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
844 //===----------------------------------------------------------------------===//
845 /// ConstantExpr - a constant value that is initialized with an expression using
846 /// other constant values.
848 /// This class uses the standard Instruction opcodes to define the various
849 /// constant expressions. The Opcode field for the ConstantExpr class is
850 /// maintained in the Value::SubclassData field.
851 class ConstantExpr : public Constant {
852 friend struct ConstantExprKeyType;
854 friend class Constant;
855 void destroyConstantImpl();
858 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
859 : Constant(ty, ConstantExprVal, Ops, NumOps) {
860 // Operation type (an Instruction opcode) is stored as the SubclassData.
861 setValueSubclassData(Opcode);
865 // Static methods to construct a ConstantExpr of different kinds. Note that
866 // these methods may return a object that is not an instance of the
867 // ConstantExpr class, because they will attempt to fold the constant
868 // expression into something simpler if possible.
870 /// getAlignOf constant expr - computes the alignment of a type in a target
871 /// independent way (Note: the return type is an i64).
872 static Constant *getAlignOf(Type *Ty);
874 /// getSizeOf constant expr - computes the (alloc) size of a type (in
875 /// address-units, not bits) in a target independent way (Note: the return
878 static Constant *getSizeOf(Type *Ty);
880 /// getOffsetOf constant expr - computes the offset of a struct field in a
881 /// target independent way (Note: the return type is an i64).
883 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
885 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
886 /// which supports any aggregate type, and any Constant index.
888 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
890 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
891 static Constant *getFNeg(Constant *C);
892 static Constant *getNot(Constant *C);
893 static Constant *getAdd(Constant *C1, Constant *C2,
894 bool HasNUW = false, bool HasNSW = false);
895 static Constant *getFAdd(Constant *C1, Constant *C2);
896 static Constant *getSub(Constant *C1, Constant *C2,
897 bool HasNUW = false, bool HasNSW = false);
898 static Constant *getFSub(Constant *C1, Constant *C2);
899 static Constant *getMul(Constant *C1, Constant *C2,
900 bool HasNUW = false, bool HasNSW = false);
901 static Constant *getFMul(Constant *C1, Constant *C2);
902 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
903 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
904 static Constant *getFDiv(Constant *C1, Constant *C2);
905 static Constant *getURem(Constant *C1, Constant *C2);
906 static Constant *getSRem(Constant *C1, Constant *C2);
907 static Constant *getFRem(Constant *C1, Constant *C2);
908 static Constant *getAnd(Constant *C1, Constant *C2);
909 static Constant *getOr(Constant *C1, Constant *C2);
910 static Constant *getXor(Constant *C1, Constant *C2);
911 static Constant *getShl(Constant *C1, Constant *C2,
912 bool HasNUW = false, bool HasNSW = false);
913 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
914 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
915 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
916 static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
917 static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
918 static Constant *getFPTrunc(Constant *C, Type *Ty,
919 bool OnlyIfReduced = false);
920 static Constant *getFPExtend(Constant *C, Type *Ty,
921 bool OnlyIfReduced = false);
922 static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
923 static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
924 static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
925 static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
926 static Constant *getPtrToInt(Constant *C, Type *Ty,
927 bool OnlyIfReduced = false);
928 static Constant *getIntToPtr(Constant *C, Type *Ty,
929 bool OnlyIfReduced = false);
930 static Constant *getBitCast(Constant *C, Type *Ty,
931 bool OnlyIfReduced = false);
932 static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
933 bool OnlyIfReduced = false);
935 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
936 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
937 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
938 return getAdd(C1, C2, false, true);
940 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
941 return getAdd(C1, C2, true, false);
943 static Constant *getNSWSub(Constant *C1, Constant *C2) {
944 return getSub(C1, C2, false, true);
946 static Constant *getNUWSub(Constant *C1, Constant *C2) {
947 return getSub(C1, C2, true, false);
949 static Constant *getNSWMul(Constant *C1, Constant *C2) {
950 return getMul(C1, C2, false, true);
952 static Constant *getNUWMul(Constant *C1, Constant *C2) {
953 return getMul(C1, C2, true, false);
955 static Constant *getNSWShl(Constant *C1, Constant *C2) {
956 return getShl(C1, C2, false, true);
958 static Constant *getNUWShl(Constant *C1, Constant *C2) {
959 return getShl(C1, C2, true, false);
961 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
962 return getSDiv(C1, C2, true);
964 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
965 return getUDiv(C1, C2, true);
967 static Constant *getExactAShr(Constant *C1, Constant *C2) {
968 return getAShr(C1, C2, true);
970 static Constant *getExactLShr(Constant *C1, Constant *C2) {
971 return getLShr(C1, C2, true);
974 /// getBinOpIdentity - Return the identity for the given binary operation,
975 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
976 /// returns null if the operator doesn't have an identity.
977 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
979 /// getBinOpAbsorber - Return the absorbing element for the given binary
980 /// operation, i.e. a constant C such that X op C = C and C op X = C for
981 /// every X. For example, this returns zero for integer multiplication.
982 /// It returns null if the operator doesn't have an absorbing element.
983 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
985 /// Transparently provide more efficient getOperand methods.
986 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
988 /// \brief Convenience function for getting a Cast operation.
990 /// \param ops The opcode for the conversion
991 /// \param C The constant to be converted
992 /// \param Ty The type to which the constant is converted
993 /// \param OnlyIfReduced see \a getWithOperands() docs.
994 static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
995 bool OnlyIfReduced = false);
997 // @brief Create a ZExt or BitCast cast constant expression
998 static Constant *getZExtOrBitCast(
999 Constant *C, ///< The constant to zext or bitcast
1000 Type *Ty ///< The type to zext or bitcast C to
1003 // @brief Create a SExt or BitCast cast constant expression
1004 static Constant *getSExtOrBitCast(
1005 Constant *C, ///< The constant to sext or bitcast
1006 Type *Ty ///< The type to sext or bitcast C to
1009 // @brief Create a Trunc or BitCast cast constant expression
1010 static Constant *getTruncOrBitCast(
1011 Constant *C, ///< The constant to trunc or bitcast
1012 Type *Ty ///< The type to trunc or bitcast C to
1015 /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
1017 static Constant *getPointerCast(
1018 Constant *C, ///< The pointer value to be casted (operand 0)
1019 Type *Ty ///< The type to which cast should be made
1022 /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
1023 /// the address space.
1024 static Constant *getPointerBitCastOrAddrSpaceCast(
1025 Constant *C, ///< The constant to addrspacecast or bitcast
1026 Type *Ty ///< The type to bitcast or addrspacecast C to
1029 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
1030 static Constant *getIntegerCast(
1031 Constant *C, ///< The integer constant to be casted
1032 Type *Ty, ///< The integer type to cast to
1033 bool isSigned ///< Whether C should be treated as signed or not
1036 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1037 static Constant *getFPCast(
1038 Constant *C, ///< The integer constant to be casted
1039 Type *Ty ///< The integer type to cast to
1042 /// @brief Return true if this is a convert constant expression
1043 bool isCast() const;
1045 /// @brief Return true if this is a compare constant expression
1046 bool isCompare() const;
1048 /// @brief Return true if this is an insertvalue or extractvalue expression,
1049 /// and the getIndices() method may be used.
1050 bool hasIndices() const;
1052 /// @brief Return true if this is a getelementptr expression and all
1053 /// the index operands are compile-time known integers within the
1054 /// corresponding notional static array extents. Note that this is
1055 /// not equivalant to, a subset of, or a superset of the "inbounds"
1057 bool isGEPWithNoNotionalOverIndexing() const;
1059 /// Select constant expr
1061 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1062 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1063 Type *OnlyIfReducedTy = nullptr);
1065 /// get - Return a binary or shift operator constant expression,
1066 /// folding if possible.
1068 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1069 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1070 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1072 /// \brief Return an ICmp or FCmp comparison operator constant expression.
1074 /// \param OnlyIfReduced see \a getWithOperands() docs.
1075 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1076 bool OnlyIfReduced = false);
1078 /// get* - Return some common constants without having to
1079 /// specify the full Instruction::OPCODE identifier.
1081 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1082 bool OnlyIfReduced = false);
1083 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1084 bool OnlyIfReduced = false);
1086 /// Getelementptr form. Value* is only accepted for convenience;
1087 /// all elements must be Constants.
1089 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1090 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1091 ArrayRef<Constant *> IdxList,
1092 bool InBounds = false,
1093 Type *OnlyIfReducedTy = nullptr) {
1094 return getGetElementPtr(
1095 Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1096 InBounds, OnlyIfReducedTy);
1098 static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
1099 bool InBounds = false,
1100 Type *OnlyIfReducedTy = nullptr) {
1101 // This form of the function only exists to avoid ambiguous overload
1102 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1103 // ArrayRef<Value *>.
1104 return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
1106 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1107 ArrayRef<Value *> IdxList,
1108 bool InBounds = false,
1109 Type *OnlyIfReducedTy = nullptr);
1111 /// Create an "inbounds" getelementptr. See the documentation for the
1112 /// "inbounds" flag in LangRef.html for details.
1113 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1114 ArrayRef<Constant *> IdxList) {
1115 return getGetElementPtr(Ty, C, IdxList, true);
1117 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1119 // This form of the function only exists to avoid ambiguous overload
1120 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1121 // ArrayRef<Value *>.
1122 return getGetElementPtr(Ty, C, Idx, true);
1124 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1125 ArrayRef<Value *> IdxList) {
1126 return getGetElementPtr(Ty, C, IdxList, true);
1129 static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1130 Type *OnlyIfReducedTy = nullptr);
1131 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1132 Type *OnlyIfReducedTy = nullptr);
1133 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1134 Type *OnlyIfReducedTy = nullptr);
1135 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1136 Type *OnlyIfReducedTy = nullptr);
1137 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1138 ArrayRef<unsigned> Idxs,
1139 Type *OnlyIfReducedTy = nullptr);
1141 /// getOpcode - Return the opcode at the root of this constant expression
1142 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1144 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1145 /// not an ICMP or FCMP constant expression.
1146 unsigned getPredicate() const;
1148 /// getIndices - Assert that this is an insertvalue or exactvalue
1149 /// expression and return the list of indices.
1150 ArrayRef<unsigned> getIndices() const;
1152 /// getOpcodeName - Return a string representation for an opcode.
1153 const char *getOpcodeName() const;
1155 /// getWithOperandReplaced - Return a constant expression identical to this
1156 /// one, but with the specified operand set to the specified value.
1157 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1159 /// getWithOperands - This returns the current constant expression with the
1160 /// operands replaced with the specified values. The specified array must
1161 /// have the same number of operands as our current one.
1162 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1163 return getWithOperands(Ops, getType());
1166 /// \brief Get the current expression with the operands replaced.
1168 /// Return the current constant expression with the operands replaced with \c
1169 /// Ops and the type with \c Ty. The new operands must have the same number
1170 /// as the current ones.
1172 /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1173 /// gets constant-folded, the type changes, or the expression is otherwise
1174 /// canonicalized. This parameter should almost always be \c false.
1175 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1176 bool OnlyIfReduced = false) const;
1178 /// getAsInstruction - Returns an Instruction which implements the same
1179 /// operation as this ConstantExpr. The instruction is not linked to any basic
1182 /// A better approach to this could be to have a constructor for Instruction
1183 /// which would take a ConstantExpr parameter, but that would have spread
1184 /// implementation details of ConstantExpr outside of Constants.cpp, which
1185 /// would make it harder to remove ConstantExprs altogether.
1186 Instruction *getAsInstruction();
1188 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
1190 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1191 static inline bool classof(const Value *V) {
1192 return V->getValueID() == ConstantExprVal;
1196 // Shadow Value::setValueSubclassData with a private forwarding method so that
1197 // subclasses cannot accidentally use it.
1198 void setValueSubclassData(unsigned short D) {
1199 Value::setValueSubclassData(D);
1204 struct OperandTraits<ConstantExpr> :
1205 public VariadicOperandTraits<ConstantExpr, 1> {
1208 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1210 //===----------------------------------------------------------------------===//
1211 /// UndefValue - 'undef' values are things that do not have specified contents.
1212 /// These are used for a variety of purposes, including global variable
1213 /// initializers and operands to instructions. 'undef' values can occur with
1214 /// any first-class type.
1216 /// Undef values aren't exactly constants; if they have multiple uses, they
1217 /// can appear to have different bit patterns at each use. See
1218 /// LangRef.html#undefvalues for details.
1220 class UndefValue : public Constant {
1221 void *operator new(size_t, unsigned) = delete;
1222 UndefValue(const UndefValue &) = delete;
1224 friend class Constant;
1225 void destroyConstantImpl();
1228 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
1230 // allocate space for exactly zero operands
1231 void *operator new(size_t s) {
1232 return User::operator new(s, 0);
1235 /// get() - Static factory methods - Return an 'undef' object of the specified
1238 static UndefValue *get(Type *T);
1240 /// getSequentialElement - If this Undef has array or vector type, return a
1241 /// undef with the right element type.
1242 UndefValue *getSequentialElement() const;
1244 /// getStructElement - If this undef has struct type, return a undef with the
1245 /// right element type for the specified element.
1246 UndefValue *getStructElement(unsigned Elt) const;
1248 /// getElementValue - Return an undef of the right value for the specified GEP
1250 UndefValue *getElementValue(Constant *C) const;
1252 /// getElementValue - Return an undef of the right value for the specified GEP
1254 UndefValue *getElementValue(unsigned Idx) const;
1256 /// \brief Return the number of elements in the array, vector, or struct.
1257 unsigned getNumElements() const;
1259 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1260 static bool classof(const Value *V) {
1261 return V->getValueID() == UndefValueVal;
1265 } // End llvm namespace