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) = delete;
50 ConstantInt(const ConstantInt &) = delete;
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) = delete;
232 ConstantFP(const ConstantFP &) = delete;
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 *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
255 static Constant *getNegativeZero(Type *Ty);
256 static Constant *getInfinity(Type *Ty, bool Negative = false);
258 /// isValueValidForType - return true if Ty is big enough to represent V.
259 static bool isValueValidForType(Type *Ty, const APFloat &V);
260 inline const APFloat &getValueAPF() const { return Val; }
262 /// isZero - Return true if the value is positive or negative zero.
263 bool isZero() const { return Val.isZero(); }
265 /// isNegative - Return true if the sign bit is set.
266 bool isNegative() const { return Val.isNegative(); }
268 /// isInfinity - Return true if the value is infinity
269 bool isInfinity() const { return Val.isInfinity(); }
271 /// isNaN - Return true if the value is a NaN.
272 bool isNaN() const { return Val.isNaN(); }
274 /// isExactlyValue - We don't rely on operator== working on double values, as
275 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
276 /// As such, this method can be used to do an exact bit-for-bit comparison of
277 /// two floating point values. The version with a double operand is retained
278 /// because it's so convenient to write isExactlyValue(2.0), but please use
279 /// it only for simple constants.
280 bool isExactlyValue(const APFloat &V) const;
282 bool isExactlyValue(double V) const {
285 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
286 return isExactlyValue(FV);
288 /// Methods for support type inquiry through isa, cast, and dyn_cast:
289 static bool classof(const Value *V) {
290 return V->getValueID() == ConstantFPVal;
294 //===----------------------------------------------------------------------===//
295 /// ConstantAggregateZero - All zero aggregate value
297 class ConstantAggregateZero : public Constant {
298 void *operator new(size_t, unsigned) = delete;
299 ConstantAggregateZero(const ConstantAggregateZero &) = delete;
301 explicit ConstantAggregateZero(Type *ty)
302 : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
304 // allocate space for exactly zero operands
305 void *operator new(size_t s) {
306 return User::operator new(s, 0);
309 static ConstantAggregateZero *get(Type *Ty);
311 void destroyConstant() override;
313 /// getSequentialElement - If this CAZ has array or vector type, return a zero
314 /// with the right element type.
315 Constant *getSequentialElement() const;
317 /// getStructElement - If this CAZ has struct type, return a zero with the
318 /// right element type for the specified element.
319 Constant *getStructElement(unsigned Elt) const;
321 /// getElementValue - Return a zero of the right value for the specified GEP
323 Constant *getElementValue(Constant *C) const;
325 /// getElementValue - Return a zero of the right value for the specified GEP
327 Constant *getElementValue(unsigned Idx) const;
329 /// \brief Return the number of elements in the array, vector, or struct.
330 unsigned getNumElements() const;
332 /// Methods for support type inquiry through isa, cast, and dyn_cast:
334 static bool classof(const Value *V) {
335 return V->getValueID() == ConstantAggregateZeroVal;
340 //===----------------------------------------------------------------------===//
341 /// ConstantArray - Constant Array Declarations
343 class ConstantArray : public Constant {
344 friend struct ConstantAggrKeyType<ConstantArray>;
345 ConstantArray(const ConstantArray &) = delete;
347 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
349 // ConstantArray accessors
350 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
353 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
356 /// Transparently provide more efficient getOperand methods.
357 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
359 /// getType - Specialize the getType() method to always return an ArrayType,
360 /// which reduces the amount of casting needed in parts of the compiler.
362 inline ArrayType *getType() const {
363 return cast<ArrayType>(Value::getType());
366 void destroyConstant() override;
367 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
369 /// Methods for support type inquiry through isa, cast, and dyn_cast:
370 static bool classof(const Value *V) {
371 return V->getValueID() == ConstantArrayVal;
376 struct OperandTraits<ConstantArray> :
377 public VariadicOperandTraits<ConstantArray> {
380 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
382 //===----------------------------------------------------------------------===//
383 // ConstantStruct - Constant Struct Declarations
385 class ConstantStruct : public Constant {
386 friend struct ConstantAggrKeyType<ConstantStruct>;
387 ConstantStruct(const ConstantStruct &) = delete;
389 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
391 // ConstantStruct accessors
392 static Constant *get(StructType *T, ArrayRef<Constant*> V);
393 static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
395 /// getAnon - Return an anonymous struct that has the specified
396 /// elements. If the struct is possibly empty, then you must specify a
398 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
399 return get(getTypeForElements(V, Packed), V);
401 static Constant *getAnon(LLVMContext &Ctx,
402 ArrayRef<Constant*> V, bool Packed = false) {
403 return get(getTypeForElements(Ctx, V, Packed), V);
406 /// getTypeForElements - Return an anonymous struct type to use for a constant
407 /// with the specified set of elements. The list must not be empty.
408 static StructType *getTypeForElements(ArrayRef<Constant*> V,
409 bool Packed = false);
410 /// getTypeForElements - This version of the method allows an empty list.
411 static StructType *getTypeForElements(LLVMContext &Ctx,
412 ArrayRef<Constant*> V,
413 bool Packed = false);
415 /// Transparently provide more efficient getOperand methods.
416 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
418 /// getType() specialization - Reduce amount of casting...
420 inline StructType *getType() const {
421 return cast<StructType>(Value::getType());
424 void destroyConstant() override;
425 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
427 /// Methods for support type inquiry through isa, cast, and dyn_cast:
428 static bool classof(const Value *V) {
429 return V->getValueID() == ConstantStructVal;
434 struct OperandTraits<ConstantStruct> :
435 public VariadicOperandTraits<ConstantStruct> {
438 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
441 //===----------------------------------------------------------------------===//
442 /// ConstantVector - Constant Vector Declarations
444 class ConstantVector : public Constant {
445 friend struct ConstantAggrKeyType<ConstantVector>;
446 ConstantVector(const ConstantVector &) = delete;
448 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
450 // ConstantVector accessors
451 static Constant *get(ArrayRef<Constant*> V);
454 static Constant *getImpl(ArrayRef<Constant *> V);
457 /// getSplat - Return a ConstantVector with the specified constant in each
459 static Constant *getSplat(unsigned NumElts, Constant *Elt);
461 /// Transparently provide more efficient getOperand methods.
462 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
464 /// getType - Specialize the getType() method to always return a VectorType,
465 /// which reduces the amount of casting needed in parts of the compiler.
467 inline VectorType *getType() const {
468 return cast<VectorType>(Value::getType());
471 /// getSplatValue - If this is a splat constant, meaning that all of the
472 /// elements have the same value, return that value. Otherwise return NULL.
473 Constant *getSplatValue() const;
475 void destroyConstant() override;
476 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
478 /// Methods for support type inquiry through isa, cast, and dyn_cast:
479 static bool classof(const Value *V) {
480 return V->getValueID() == ConstantVectorVal;
485 struct OperandTraits<ConstantVector> :
486 public VariadicOperandTraits<ConstantVector> {
489 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
491 //===----------------------------------------------------------------------===//
492 /// ConstantPointerNull - a constant pointer value that points to null
494 class ConstantPointerNull : public Constant {
495 void *operator new(size_t, unsigned) = delete;
496 ConstantPointerNull(const ConstantPointerNull &) = delete;
498 explicit ConstantPointerNull(PointerType *T)
500 Value::ConstantPointerNullVal, nullptr, 0) {}
503 // allocate space for exactly zero operands
504 void *operator new(size_t s) {
505 return User::operator new(s, 0);
508 /// get() - Static factory methods - Return objects of the specified value
509 static ConstantPointerNull *get(PointerType *T);
511 void destroyConstant() override;
513 /// getType - Specialize the getType() method to always return an PointerType,
514 /// which reduces the amount of casting needed in parts of the compiler.
516 inline PointerType *getType() const {
517 return cast<PointerType>(Value::getType());
520 /// Methods for support type inquiry through isa, cast, and dyn_cast:
521 static bool classof(const Value *V) {
522 return V->getValueID() == ConstantPointerNullVal;
526 //===----------------------------------------------------------------------===//
527 /// ConstantDataSequential - A vector or array constant whose element type is a
528 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
529 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
530 /// operands because it stores all of the elements of the constant as densely
531 /// packed data, instead of as Value*'s.
533 /// This is the common base class of ConstantDataArray and ConstantDataVector.
535 class ConstantDataSequential : public Constant {
536 friend class LLVMContextImpl;
537 /// DataElements - A pointer to the bytes underlying this constant (which is
538 /// owned by the uniquing StringMap).
539 const char *DataElements;
541 /// Next - This forms a link list of ConstantDataSequential nodes that have
542 /// the same value but different type. For example, 0,0,0,1 could be a 4
543 /// element array of i8, or a 1-element array of i32. They'll both end up in
544 /// the same StringMap bucket, linked up.
545 ConstantDataSequential *Next;
546 void *operator new(size_t, unsigned) = delete;
547 ConstantDataSequential(const ConstantDataSequential &) = delete;
549 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
550 : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
551 ~ConstantDataSequential() override { delete Next; }
553 static Constant *getImpl(StringRef Bytes, Type *Ty);
556 // allocate space for exactly zero operands.
557 void *operator new(size_t s) {
558 return User::operator new(s, 0);
562 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
563 /// formed with a vector or array of the specified element type.
564 /// ConstantDataArray only works with normal float and int types that are
565 /// stored densely in memory, not with things like i42 or x86_f80.
566 static bool isElementTypeCompatible(const Type *Ty);
568 /// getElementAsInteger - If this is a sequential container of integers (of
569 /// any size), return the specified element in the low bits of a uint64_t.
570 uint64_t getElementAsInteger(unsigned i) const;
572 /// getElementAsAPFloat - If this is a sequential container of floating point
573 /// type, return the specified element as an APFloat.
574 APFloat getElementAsAPFloat(unsigned i) const;
576 /// getElementAsFloat - If this is an sequential container of floats, return
577 /// the specified element as a float.
578 float getElementAsFloat(unsigned i) const;
580 /// getElementAsDouble - If this is an sequential container of doubles, return
581 /// the specified element as a double.
582 double getElementAsDouble(unsigned i) const;
584 /// getElementAsConstant - Return a Constant for a specified index's element.
585 /// Note that this has to compute a new constant to return, so it isn't as
586 /// efficient as getElementAsInteger/Float/Double.
587 Constant *getElementAsConstant(unsigned i) const;
589 /// getType - Specialize the getType() method to always return a
590 /// SequentialType, which reduces the amount of casting needed in parts of the
592 inline SequentialType *getType() const {
593 return cast<SequentialType>(Value::getType());
596 /// getElementType - Return the element type of the array/vector.
597 Type *getElementType() const;
599 /// getNumElements - Return the number of elements in the array or vector.
600 unsigned getNumElements() const;
602 /// getElementByteSize - Return the size (in bytes) of each element in the
603 /// array/vector. The size of the elements is known to be a multiple of one
605 uint64_t getElementByteSize() const;
608 /// isString - This method returns true if this is an array of i8.
609 bool isString() const;
611 /// isCString - This method returns true if the array "isString", ends with a
612 /// nul byte, and does not contains any other nul bytes.
613 bool isCString() const;
615 /// getAsString - If this array is isString(), then this method returns the
616 /// array as a StringRef. Otherwise, it asserts out.
618 StringRef getAsString() const {
619 assert(isString() && "Not a string");
620 return getRawDataValues();
623 /// getAsCString - If this array is isCString(), then this method returns the
624 /// array (without the trailing null byte) as a StringRef. Otherwise, it
627 StringRef getAsCString() const {
628 assert(isCString() && "Isn't a C string");
629 StringRef Str = getAsString();
630 return Str.substr(0, Str.size()-1);
633 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
634 /// that this is an extremely tricky thing to work with, as it exposes the
635 /// host endianness of the data elements.
636 StringRef getRawDataValues() const;
638 void destroyConstant() override;
640 /// Methods for support type inquiry through isa, cast, and dyn_cast:
642 static bool classof(const Value *V) {
643 return V->getValueID() == ConstantDataArrayVal ||
644 V->getValueID() == ConstantDataVectorVal;
647 const char *getElementPointer(unsigned Elt) const;
650 //===----------------------------------------------------------------------===//
651 /// ConstantDataArray - An array constant whose element type is a simple
652 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
653 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
654 /// operands because it stores all of the elements of the constant as densely
655 /// packed data, instead of as Value*'s.
656 class ConstantDataArray : public ConstantDataSequential {
657 void *operator new(size_t, unsigned) = delete;
658 ConstantDataArray(const ConstantDataArray &) = delete;
659 void anchor() override;
660 friend class ConstantDataSequential;
661 explicit ConstantDataArray(Type *ty, const char *Data)
662 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
664 // allocate space for exactly zero operands.
665 void *operator new(size_t s) {
666 return User::operator new(s, 0);
670 /// get() constructors - Return a constant with array type with an element
671 /// count and element type matching the ArrayRef passed in. Note that this
672 /// can return a ConstantAggregateZero object.
673 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
674 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
675 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
676 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
677 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
678 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
680 /// getFP() constructors - Return a constant with array type with an element
681 /// count and element type of float with precision matching the number of
682 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
683 /// double for 64bits) Note that this can return a ConstantAggregateZero
685 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
686 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
687 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
689 /// getString - This method constructs a CDS and initializes it with a text
690 /// string. The default behavior (AddNull==true) causes a null terminator to
691 /// be placed at the end of the array (increasing the length of the string by
692 /// one more than the StringRef would normally indicate. Pass AddNull=false
693 /// to disable this behavior.
694 static Constant *getString(LLVMContext &Context, StringRef Initializer,
695 bool AddNull = true);
697 /// getType - Specialize the getType() method to always return an ArrayType,
698 /// which reduces the amount of casting needed in parts of the compiler.
700 inline ArrayType *getType() const {
701 return cast<ArrayType>(Value::getType());
704 /// Methods for support type inquiry through isa, cast, and dyn_cast:
706 static bool classof(const Value *V) {
707 return V->getValueID() == ConstantDataArrayVal;
711 //===----------------------------------------------------------------------===//
712 /// ConstantDataVector - A vector constant whose element type is a simple
713 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
714 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
715 /// operands because it stores all of the elements of the constant as densely
716 /// packed data, instead of as Value*'s.
717 class ConstantDataVector : public ConstantDataSequential {
718 void *operator new(size_t, unsigned) = delete;
719 ConstantDataVector(const ConstantDataVector &) = delete;
720 void anchor() override;
721 friend class ConstantDataSequential;
722 explicit ConstantDataVector(Type *ty, const char *Data)
723 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
725 // allocate space for exactly zero operands.
726 void *operator new(size_t s) {
727 return User::operator new(s, 0);
731 /// get() constructors - Return a constant with vector type with an element
732 /// count and element type matching the ArrayRef passed in. Note that this
733 /// can return a ConstantAggregateZero object.
734 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
735 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
736 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
737 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
738 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
739 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
741 /// getFP() constructors - Return a constant with vector type with an element
742 /// count and element type of float with the precision matching the number of
743 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
744 /// double for 64bits) Note that this can return a ConstantAggregateZero
746 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
747 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
748 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
750 /// getSplat - Return a ConstantVector with the specified constant in each
751 /// element. The specified constant has to be a of a compatible type (i8/i16/
752 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
753 static Constant *getSplat(unsigned NumElts, Constant *Elt);
755 /// getSplatValue - If this is a splat constant, meaning that all of the
756 /// elements have the same value, return that value. Otherwise return NULL.
757 Constant *getSplatValue() const;
759 /// getType - Specialize the getType() method to always return a VectorType,
760 /// which reduces the amount of casting needed in parts of the compiler.
762 inline VectorType *getType() const {
763 return cast<VectorType>(Value::getType());
766 /// Methods for support type inquiry through isa, cast, and dyn_cast:
768 static bool classof(const Value *V) {
769 return V->getValueID() == ConstantDataVectorVal;
775 /// BlockAddress - The address of a basic block.
777 class BlockAddress : public Constant {
778 void *operator new(size_t, unsigned) = delete;
779 void *operator new(size_t s) { return User::operator new(s, 2); }
780 BlockAddress(Function *F, BasicBlock *BB);
782 /// get - Return a BlockAddress for the specified function and basic block.
783 static BlockAddress *get(Function *F, BasicBlock *BB);
785 /// get - Return a BlockAddress for the specified basic block. The basic
786 /// block must be embedded into a function.
787 static BlockAddress *get(BasicBlock *BB);
789 /// \brief Lookup an existing \c BlockAddress constant for the given
792 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
793 static BlockAddress *lookup(const BasicBlock *BB);
795 /// Transparently provide more efficient getOperand methods.
796 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
798 Function *getFunction() const { return (Function*)Op<0>().get(); }
799 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
801 void destroyConstant() override;
802 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
804 /// Methods for support type inquiry through isa, cast, and dyn_cast:
805 static inline bool classof(const Value *V) {
806 return V->getValueID() == BlockAddressVal;
811 struct OperandTraits<BlockAddress> :
812 public FixedNumOperandTraits<BlockAddress, 2> {
815 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
818 //===----------------------------------------------------------------------===//
819 /// ConstantExpr - a constant value that is initialized with an expression using
820 /// other constant values.
822 /// This class uses the standard Instruction opcodes to define the various
823 /// constant expressions. The Opcode field for the ConstantExpr class is
824 /// maintained in the Value::SubclassData field.
825 class ConstantExpr : public Constant {
826 friend struct ConstantExprKeyType;
829 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
830 : Constant(ty, ConstantExprVal, Ops, NumOps) {
831 // Operation type (an Instruction opcode) is stored as the SubclassData.
832 setValueSubclassData(Opcode);
836 // Static methods to construct a ConstantExpr of different kinds. Note that
837 // these methods may return a object that is not an instance of the
838 // ConstantExpr class, because they will attempt to fold the constant
839 // expression into something simpler if possible.
841 /// getAlignOf constant expr - computes the alignment of a type in a target
842 /// independent way (Note: the return type is an i64).
843 static Constant *getAlignOf(Type *Ty);
845 /// getSizeOf constant expr - computes the (alloc) size of a type (in
846 /// address-units, not bits) in a target independent way (Note: the return
849 static Constant *getSizeOf(Type *Ty);
851 /// getOffsetOf constant expr - computes the offset of a struct field in a
852 /// target independent way (Note: the return type is an i64).
854 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
856 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
857 /// which supports any aggregate type, and any Constant index.
859 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
861 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
862 static Constant *getFNeg(Constant *C);
863 static Constant *getNot(Constant *C);
864 static Constant *getAdd(Constant *C1, Constant *C2,
865 bool HasNUW = false, bool HasNSW = false);
866 static Constant *getFAdd(Constant *C1, Constant *C2);
867 static Constant *getSub(Constant *C1, Constant *C2,
868 bool HasNUW = false, bool HasNSW = false);
869 static Constant *getFSub(Constant *C1, Constant *C2);
870 static Constant *getMul(Constant *C1, Constant *C2,
871 bool HasNUW = false, bool HasNSW = false);
872 static Constant *getFMul(Constant *C1, Constant *C2);
873 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
874 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
875 static Constant *getFDiv(Constant *C1, Constant *C2);
876 static Constant *getURem(Constant *C1, Constant *C2);
877 static Constant *getSRem(Constant *C1, Constant *C2);
878 static Constant *getFRem(Constant *C1, Constant *C2);
879 static Constant *getAnd(Constant *C1, Constant *C2);
880 static Constant *getOr(Constant *C1, Constant *C2);
881 static Constant *getXor(Constant *C1, Constant *C2);
882 static Constant *getShl(Constant *C1, Constant *C2,
883 bool HasNUW = false, bool HasNSW = false);
884 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
885 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
886 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
887 static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
888 static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
889 static Constant *getFPTrunc(Constant *C, Type *Ty,
890 bool OnlyIfReduced = false);
891 static Constant *getFPExtend(Constant *C, Type *Ty,
892 bool OnlyIfReduced = false);
893 static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
894 static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
895 static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
896 static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
897 static Constant *getPtrToInt(Constant *C, Type *Ty,
898 bool OnlyIfReduced = false);
899 static Constant *getIntToPtr(Constant *C, Type *Ty,
900 bool OnlyIfReduced = false);
901 static Constant *getBitCast(Constant *C, Type *Ty,
902 bool OnlyIfReduced = false);
903 static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
904 bool OnlyIfReduced = false);
906 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
907 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
908 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
909 return getAdd(C1, C2, false, true);
911 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
912 return getAdd(C1, C2, true, false);
914 static Constant *getNSWSub(Constant *C1, Constant *C2) {
915 return getSub(C1, C2, false, true);
917 static Constant *getNUWSub(Constant *C1, Constant *C2) {
918 return getSub(C1, C2, true, false);
920 static Constant *getNSWMul(Constant *C1, Constant *C2) {
921 return getMul(C1, C2, false, true);
923 static Constant *getNUWMul(Constant *C1, Constant *C2) {
924 return getMul(C1, C2, true, false);
926 static Constant *getNSWShl(Constant *C1, Constant *C2) {
927 return getShl(C1, C2, false, true);
929 static Constant *getNUWShl(Constant *C1, Constant *C2) {
930 return getShl(C1, C2, true, false);
932 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
933 return getSDiv(C1, C2, true);
935 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
936 return getUDiv(C1, C2, true);
938 static Constant *getExactAShr(Constant *C1, Constant *C2) {
939 return getAShr(C1, C2, true);
941 static Constant *getExactLShr(Constant *C1, Constant *C2) {
942 return getLShr(C1, C2, true);
945 /// getBinOpIdentity - Return the identity for the given binary operation,
946 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
947 /// returns null if the operator doesn't have an identity.
948 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
950 /// getBinOpAbsorber - Return the absorbing element for the given binary
951 /// operation, i.e. a constant C such that X op C = C and C op X = C for
952 /// every X. For example, this returns zero for integer multiplication.
953 /// It returns null if the operator doesn't have an absorbing element.
954 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
956 /// Transparently provide more efficient getOperand methods.
957 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
959 /// \brief Convenience function for getting a Cast operation.
961 /// \param ops The opcode for the conversion
962 /// \param C The constant to be converted
963 /// \param Ty The type to which the constant is converted
964 /// \param OnlyIfReduced see \a getWithOperands() docs.
965 static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
966 bool OnlyIfReduced = false);
968 // @brief Create a ZExt or BitCast cast constant expression
969 static Constant *getZExtOrBitCast(
970 Constant *C, ///< The constant to zext or bitcast
971 Type *Ty ///< The type to zext or bitcast C to
974 // @brief Create a SExt or BitCast cast constant expression
975 static Constant *getSExtOrBitCast(
976 Constant *C, ///< The constant to sext or bitcast
977 Type *Ty ///< The type to sext or bitcast C to
980 // @brief Create a Trunc or BitCast cast constant expression
981 static Constant *getTruncOrBitCast(
982 Constant *C, ///< The constant to trunc or bitcast
983 Type *Ty ///< The type to trunc or bitcast C to
986 /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
988 static Constant *getPointerCast(
989 Constant *C, ///< The pointer value to be casted (operand 0)
990 Type *Ty ///< The type to which cast should be made
993 /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
994 /// the address space.
995 static Constant *getPointerBitCastOrAddrSpaceCast(
996 Constant *C, ///< The constant to addrspacecast or bitcast
997 Type *Ty ///< The type to bitcast or addrspacecast C to
1000 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
1001 static Constant *getIntegerCast(
1002 Constant *C, ///< The integer constant to be casted
1003 Type *Ty, ///< The integer type to cast to
1004 bool isSigned ///< Whether C should be treated as signed or not
1007 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1008 static Constant *getFPCast(
1009 Constant *C, ///< The integer constant to be casted
1010 Type *Ty ///< The integer type to cast to
1013 /// @brief Return true if this is a convert constant expression
1014 bool isCast() const;
1016 /// @brief Return true if this is a compare constant expression
1017 bool isCompare() const;
1019 /// @brief Return true if this is an insertvalue or extractvalue expression,
1020 /// and the getIndices() method may be used.
1021 bool hasIndices() const;
1023 /// @brief Return true if this is a getelementptr expression and all
1024 /// the index operands are compile-time known integers within the
1025 /// corresponding notional static array extents. Note that this is
1026 /// not equivalant to, a subset of, or a superset of the "inbounds"
1028 bool isGEPWithNoNotionalOverIndexing() const;
1030 /// Select constant expr
1032 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1033 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1034 Type *OnlyIfReducedTy = nullptr);
1036 /// get - Return a binary or shift operator constant expression,
1037 /// folding if possible.
1039 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1040 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1041 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1043 /// \brief Return an ICmp or FCmp comparison operator constant expression.
1045 /// \param OnlyIfReduced see \a getWithOperands() docs.
1046 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1047 bool OnlyIfReduced = false);
1049 /// get* - Return some common constants without having to
1050 /// specify the full Instruction::OPCODE identifier.
1052 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1053 bool OnlyIfReduced = false);
1054 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1055 bool OnlyIfReduced = false);
1057 /// Getelementptr form. Value* is only accepted for convenience;
1058 /// all elements must be Constant's.
1060 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1061 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1062 ArrayRef<Constant *> IdxList,
1063 bool InBounds = false,
1064 Type *OnlyIfReducedTy = nullptr) {
1065 return getGetElementPtr(
1066 Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1067 InBounds, OnlyIfReducedTy);
1069 static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
1070 bool InBounds = false,
1071 Type *OnlyIfReducedTy = nullptr) {
1072 // This form of the function only exists to avoid ambiguous overload
1073 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1074 // ArrayRef<Value *>.
1075 return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
1077 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1078 ArrayRef<Value *> IdxList,
1079 bool InBounds = false,
1080 Type *OnlyIfReducedTy = nullptr);
1082 /// Create an "inbounds" getelementptr. See the documentation for the
1083 /// "inbounds" flag in LangRef.html for details.
1084 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1085 ArrayRef<Constant *> IdxList) {
1086 return getGetElementPtr(Ty, C, IdxList, true);
1088 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1090 // This form of the function only exists to avoid ambiguous overload
1091 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1092 // ArrayRef<Value *>.
1093 return getGetElementPtr(Ty, C, Idx, true);
1095 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1096 ArrayRef<Value *> IdxList) {
1097 return getGetElementPtr(Ty, C, IdxList, true);
1100 static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1101 Type *OnlyIfReducedTy = nullptr);
1102 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1103 Type *OnlyIfReducedTy = nullptr);
1104 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1105 Type *OnlyIfReducedTy = nullptr);
1106 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1107 Type *OnlyIfReducedTy = nullptr);
1108 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1109 ArrayRef<unsigned> Idxs,
1110 Type *OnlyIfReducedTy = nullptr);
1112 /// getOpcode - Return the opcode at the root of this constant expression
1113 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1115 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1116 /// not an ICMP or FCMP constant expression.
1117 unsigned getPredicate() const;
1119 /// getIndices - Assert that this is an insertvalue or exactvalue
1120 /// expression and return the list of indices.
1121 ArrayRef<unsigned> getIndices() const;
1123 /// getOpcodeName - Return a string representation for an opcode.
1124 const char *getOpcodeName() const;
1126 /// getWithOperandReplaced - Return a constant expression identical to this
1127 /// one, but with the specified operand set to the specified value.
1128 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1130 /// getWithOperands - This returns the current constant expression with the
1131 /// operands replaced with the specified values. The specified array must
1132 /// have the same number of operands as our current one.
1133 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1134 return getWithOperands(Ops, getType());
1137 /// \brief Get the current expression with the operands replaced.
1139 /// Return the current constant expression with the operands replaced with \c
1140 /// Ops and the type with \c Ty. The new operands must have the same number
1141 /// as the current ones.
1143 /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1144 /// gets constant-folded, the type changes, or the expression is otherwise
1145 /// canonicalized. This parameter should almost always be \c false.
1146 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1147 bool OnlyIfReduced = false) const;
1149 /// getAsInstruction - Returns an Instruction which implements the same operation
1150 /// as this ConstantExpr. The instruction is not linked to any basic block.
1152 /// A better approach to this could be to have a constructor for Instruction
1153 /// which would take a ConstantExpr parameter, but that would have spread
1154 /// implementation details of ConstantExpr outside of Constants.cpp, which
1155 /// would make it harder to remove ConstantExprs altogether.
1156 Instruction *getAsInstruction();
1158 void destroyConstant() override;
1159 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
1161 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1162 static inline bool classof(const Value *V) {
1163 return V->getValueID() == ConstantExprVal;
1167 // Shadow Value::setValueSubclassData with a private forwarding method so that
1168 // subclasses cannot accidentally use it.
1169 void setValueSubclassData(unsigned short D) {
1170 Value::setValueSubclassData(D);
1175 struct OperandTraits<ConstantExpr> :
1176 public VariadicOperandTraits<ConstantExpr, 1> {
1179 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1181 //===----------------------------------------------------------------------===//
1182 /// UndefValue - 'undef' values are things that do not have specified contents.
1183 /// These are used for a variety of purposes, including global variable
1184 /// initializers and operands to instructions. 'undef' values can occur with
1185 /// any first-class type.
1187 /// Undef values aren't exactly constants; if they have multiple uses, they
1188 /// can appear to have different bit patterns at each use. See
1189 /// LangRef.html#undefvalues for details.
1191 class UndefValue : public Constant {
1192 void *operator new(size_t, unsigned) = delete;
1193 UndefValue(const UndefValue &) = delete;
1195 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
1197 // allocate space for exactly zero operands
1198 void *operator new(size_t s) {
1199 return User::operator new(s, 0);
1202 /// get() - Static factory methods - Return an 'undef' object of the specified
1205 static UndefValue *get(Type *T);
1207 /// getSequentialElement - If this Undef has array or vector type, return a
1208 /// undef with the right element type.
1209 UndefValue *getSequentialElement() const;
1211 /// getStructElement - If this undef has struct type, return a undef with the
1212 /// right element type for the specified element.
1213 UndefValue *getStructElement(unsigned Elt) const;
1215 /// getElementValue - Return an undef of the right value for the specified GEP
1217 UndefValue *getElementValue(Constant *C) const;
1219 /// getElementValue - Return an undef of the right value for the specified GEP
1221 UndefValue *getElementValue(unsigned Idx) const;
1223 /// \brief Return the number of elements in the array, vector, or struct.
1224 unsigned getNumElements() const;
1226 void destroyConstant() override;
1228 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1229 static bool classof(const Value *V) {
1230 return V->getValueID() == UndefValueVal;
1234 } // End llvm namespace