1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for the subclasses of Constant,
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are
14 /// fully shared by structural equivalence. This means that two structurally
15 /// equivalent constants will always have the same address. Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_IR_CONSTANTS_H
22 #define LLVM_IR_CONSTANTS_H
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/IR/Constant.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/OperandTraits.h"
40 struct ConstantExprKeyType;
41 template <class ConstantClass> struct ConstantAggrKeyType;
43 //===----------------------------------------------------------------------===//
44 /// This is the shared class of boolean and integer constants. This class
45 /// represents both boolean and integral constants.
46 /// @brief Class for constant integers.
47 class ConstantInt : public Constant {
48 void anchor() override;
49 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
50 ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
51 ConstantInt(IntegerType *Ty, const APInt& V);
54 // allocate space for exactly zero operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 0);
59 static ConstantInt *getTrue(LLVMContext &Context);
60 static ConstantInt *getFalse(LLVMContext &Context);
61 static Constant *getTrue(Type *Ty);
62 static Constant *getFalse(Type *Ty);
64 /// If Ty is a vector type, return a Constant with a splat of the given
65 /// value. Otherwise return a ConstantInt for the given value.
66 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
68 /// Return a ConstantInt with the specified integer value for the specified
69 /// type. If the type is wider than 64 bits, the value will be zero-extended
70 /// to fit the type, unless isSigned is true, in which case the value will
71 /// be interpreted as a 64-bit signed integer and sign-extended to fit
73 /// @brief Get a ConstantInt for a specific value.
74 static ConstantInt *get(IntegerType *Ty, uint64_t V,
75 bool isSigned = false);
77 /// Return a ConstantInt with the specified value for the specified type. The
78 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
79 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
80 /// signed value for the type Ty.
81 /// @brief Get a ConstantInt for a specific signed value.
82 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
83 static Constant *getSigned(Type *Ty, int64_t V);
85 /// Return a ConstantInt with the specified value and an implied Type. The
86 /// type is the integer type that corresponds to the bit width of the value.
87 static ConstantInt *get(LLVMContext &Context, const APInt &V);
89 /// Return a ConstantInt constructed from the string strStart with the given
91 static ConstantInt *get(IntegerType *Ty, StringRef Str,
94 /// If Ty is a vector type, return a Constant with a splat of the given
95 /// value. Otherwise return a ConstantInt for the given value.
96 static Constant *get(Type* Ty, const APInt& V);
98 /// Return the constant as an APInt value reference. This allows clients to
99 /// obtain a copy of the value, with all its precision in tact.
100 /// @brief Return the constant's value.
101 inline const APInt &getValue() const {
105 /// getBitWidth - Return the bitwidth of this constant.
106 unsigned getBitWidth() const { return Val.getBitWidth(); }
108 /// Return the constant as a 64-bit unsigned integer value after it
109 /// has been zero extended as appropriate for the type of this constant. Note
110 /// that this method can assert if the value does not fit in 64 bits.
111 /// @brief Return the zero extended value.
112 inline uint64_t getZExtValue() const {
113 return Val.getZExtValue();
116 /// Return the constant as a 64-bit integer value after it has been sign
117 /// extended as appropriate for the type of this constant. Note that
118 /// this method can assert if the value does not fit in 64 bits.
119 /// @brief Return the sign extended value.
120 inline int64_t getSExtValue() const {
121 return Val.getSExtValue();
124 /// A helper method that can be used to determine if the constant contained
125 /// within is equal to a constant. This only works for very small values,
126 /// because this is all that can be represented with all types.
127 /// @brief Determine if this constant's value is same as an unsigned char.
128 bool equalsInt(uint64_t V) const {
132 /// getType - Specialize the getType() method to always return an IntegerType,
133 /// which reduces the amount of casting needed in parts of the compiler.
135 inline IntegerType *getType() const {
136 return cast<IntegerType>(Value::getType());
139 /// This static method returns true if the type Ty is big enough to
140 /// represent the value V. This can be used to avoid having the get method
141 /// assert when V is larger than Ty can represent. Note that there are two
142 /// versions of this method, one for unsigned and one for signed integers.
143 /// Although ConstantInt canonicalizes everything to an unsigned integer,
144 /// the signed version avoids callers having to convert a signed quantity
145 /// to the appropriate unsigned type before calling the method.
146 /// @returns true if V is a valid value for type Ty
147 /// @brief Determine if the value is in range for the given type.
148 static bool isValueValidForType(Type *Ty, uint64_t V);
149 static bool isValueValidForType(Type *Ty, int64_t V);
151 bool isNegative() const { return Val.isNegative(); }
153 /// This is just a convenience method to make client code smaller for a
154 /// common code. It also correctly performs the comparison without the
155 /// potential for an assertion from getZExtValue().
156 bool isZero() const {
160 /// This is just a convenience method to make client code smaller for a
161 /// common case. It also correctly performs the comparison without the
162 /// potential for an assertion from getZExtValue().
163 /// @brief Determine if the value is one.
168 /// This function will return true iff every bit in this constant is set
170 /// @returns true iff this constant's bits are all set to true.
171 /// @brief Determine if the value is all ones.
172 bool isMinusOne() const {
173 return Val.isAllOnesValue();
176 /// This function will return true iff this constant represents the largest
177 /// value that may be represented by the constant's type.
178 /// @returns true iff this is the largest value that may be represented
180 /// @brief Determine if the value is maximal.
181 bool isMaxValue(bool isSigned) const {
183 return Val.isMaxSignedValue();
185 return Val.isMaxValue();
188 /// This function will return true iff this constant represents the smallest
189 /// value that may be represented by this constant's type.
190 /// @returns true if this is the smallest value that may be represented by
192 /// @brief Determine if the value is minimal.
193 bool isMinValue(bool isSigned) const {
195 return Val.isMinSignedValue();
197 return Val.isMinValue();
200 /// This function will return true iff this constant represents a value with
201 /// active bits bigger than 64 bits or a value greater than the given uint64_t
203 /// @returns true iff this constant is greater or equal to the given number.
204 /// @brief Determine if the value is greater or equal to the given number.
205 bool uge(uint64_t Num) const {
206 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
209 /// getLimitedValue - If the value is smaller than the specified limit,
210 /// return it, otherwise return the limit value. This causes the value
211 /// to saturate to the limit.
212 /// @returns the min of the value of the constant and the specified value
213 /// @brief Get the constant's value with a saturation limit
214 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
215 return Val.getLimitedValue(Limit);
218 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
219 static bool classof(const Value *V) {
220 return V->getValueID() == ConstantIntVal;
225 //===----------------------------------------------------------------------===//
226 /// ConstantFP - Floating Point Values [float, double]
228 class ConstantFP : public Constant {
230 void anchor() override;
231 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
232 ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
233 friend class LLVMContextImpl;
235 ConstantFP(Type *Ty, const APFloat& V);
237 // allocate space for exactly zero operands
238 void *operator new(size_t s) {
239 return User::operator new(s, 0);
242 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
243 /// method returns the negative zero constant for floating point or vector
244 /// floating point types; for all other types, it returns the null value.
245 static Constant *getZeroValueForNegation(Type *Ty);
247 /// get() - This returns a ConstantFP, or a vector containing a splat of a
248 /// ConstantFP, for the specified value in the specified type. This should
249 /// only be used for simple constant values like 2.0/1.0 etc, that are
250 /// known-valid both as host double and as the target format.
251 static Constant *get(Type* Ty, double V);
252 static Constant *get(Type* Ty, StringRef Str);
253 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
254 static Constant *getNegativeZero(Type *Ty);
255 static Constant *getInfinity(Type *Ty, bool Negative = false);
257 /// isValueValidForType - return true if Ty is big enough to represent V.
258 static bool isValueValidForType(Type *Ty, const APFloat &V);
259 inline const APFloat &getValueAPF() const { return Val; }
261 /// isZero - Return true if the value is positive or negative zero.
262 bool isZero() const { return Val.isZero(); }
264 /// isNegative - Return true if the sign bit is set.
265 bool isNegative() const { return Val.isNegative(); }
267 /// isNaN - Return true if the value is a NaN.
268 bool isNaN() const { return Val.isNaN(); }
270 /// isExactlyValue - We don't rely on operator== working on double values, as
271 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
272 /// As such, this method can be used to do an exact bit-for-bit comparison of
273 /// two floating point values. The version with a double operand is retained
274 /// because it's so convenient to write isExactlyValue(2.0), but please use
275 /// it only for simple constants.
276 bool isExactlyValue(const APFloat &V) const;
278 bool isExactlyValue(double V) const {
281 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
282 return isExactlyValue(FV);
284 /// Methods for support type inquiry through isa, cast, and dyn_cast:
285 static bool classof(const Value *V) {
286 return V->getValueID() == ConstantFPVal;
290 //===----------------------------------------------------------------------===//
291 /// ConstantAggregateZero - All zero aggregate value
293 class ConstantAggregateZero : public Constant {
294 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
295 ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
297 explicit ConstantAggregateZero(Type *ty)
298 : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
300 // allocate space for exactly zero operands
301 void *operator new(size_t s) {
302 return User::operator new(s, 0);
305 static ConstantAggregateZero *get(Type *Ty);
307 void destroyConstant() override;
309 /// getSequentialElement - If this CAZ has array or vector type, return a zero
310 /// with the right element type.
311 Constant *getSequentialElement() const;
313 /// getStructElement - If this CAZ has struct type, return a zero with the
314 /// right element type for the specified element.
315 Constant *getStructElement(unsigned Elt) const;
317 /// getElementValue - Return a zero of the right value for the specified GEP
319 Constant *getElementValue(Constant *C) const;
321 /// getElementValue - Return a zero of the right value for the specified GEP
323 Constant *getElementValue(unsigned Idx) const;
325 /// Methods for support type inquiry through isa, cast, and dyn_cast:
327 static bool classof(const Value *V) {
328 return V->getValueID() == ConstantAggregateZeroVal;
333 //===----------------------------------------------------------------------===//
334 /// ConstantArray - Constant Array Declarations
336 class ConstantArray : public Constant {
337 friend struct ConstantAggrKeyType<ConstantArray>;
338 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
340 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
342 // ConstantArray accessors
343 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
345 /// Transparently provide more efficient getOperand methods.
346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
348 /// getType - Specialize the getType() method to always return an ArrayType,
349 /// which reduces the amount of casting needed in parts of the compiler.
351 inline ArrayType *getType() const {
352 return cast<ArrayType>(Value::getType());
355 void destroyConstant() override;
356 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
358 /// Methods for support type inquiry through isa, cast, and dyn_cast:
359 static bool classof(const Value *V) {
360 return V->getValueID() == ConstantArrayVal;
365 struct OperandTraits<ConstantArray> :
366 public VariadicOperandTraits<ConstantArray> {
369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
371 //===----------------------------------------------------------------------===//
372 // ConstantStruct - Constant Struct Declarations
374 class ConstantStruct : public Constant {
375 friend struct ConstantAggrKeyType<ConstantStruct>;
376 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
378 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
380 // ConstantStruct accessors
381 static Constant *get(StructType *T, ArrayRef<Constant*> V);
382 static Constant *get(StructType *T, ...) END_WITH_NULL;
384 /// getAnon - Return an anonymous struct that has the specified
385 /// elements. If the struct is possibly empty, then you must specify a
387 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
388 return get(getTypeForElements(V, Packed), V);
390 static Constant *getAnon(LLVMContext &Ctx,
391 ArrayRef<Constant*> V, bool Packed = false) {
392 return get(getTypeForElements(Ctx, V, Packed), V);
395 /// getTypeForElements - Return an anonymous struct type to use for a constant
396 /// with the specified set of elements. The list must not be empty.
397 static StructType *getTypeForElements(ArrayRef<Constant*> V,
398 bool Packed = false);
399 /// getTypeForElements - This version of the method allows an empty list.
400 static StructType *getTypeForElements(LLVMContext &Ctx,
401 ArrayRef<Constant*> V,
402 bool Packed = false);
404 /// Transparently provide more efficient getOperand methods.
405 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
407 /// getType() specialization - Reduce amount of casting...
409 inline StructType *getType() const {
410 return cast<StructType>(Value::getType());
413 void destroyConstant() override;
414 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
416 /// Methods for support type inquiry through isa, cast, and dyn_cast:
417 static bool classof(const Value *V) {
418 return V->getValueID() == ConstantStructVal;
423 struct OperandTraits<ConstantStruct> :
424 public VariadicOperandTraits<ConstantStruct> {
427 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
430 //===----------------------------------------------------------------------===//
431 /// ConstantVector - Constant Vector Declarations
433 class ConstantVector : public Constant {
434 friend struct ConstantAggrKeyType<ConstantVector>;
435 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
437 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
439 // ConstantVector accessors
440 static Constant *get(ArrayRef<Constant*> V);
442 /// getSplat - Return a ConstantVector with the specified constant in each
444 static Constant *getSplat(unsigned NumElts, Constant *Elt);
446 /// Transparently provide more efficient getOperand methods.
447 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
449 /// getType - Specialize the getType() method to always return a VectorType,
450 /// which reduces the amount of casting needed in parts of the compiler.
452 inline VectorType *getType() const {
453 return cast<VectorType>(Value::getType());
456 /// getSplatValue - If this is a splat constant, meaning that all of the
457 /// elements have the same value, return that value. Otherwise return NULL.
458 Constant *getSplatValue() const;
460 void destroyConstant() override;
461 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
463 /// Methods for support type inquiry through isa, cast, and dyn_cast:
464 static bool classof(const Value *V) {
465 return V->getValueID() == ConstantVectorVal;
470 struct OperandTraits<ConstantVector> :
471 public VariadicOperandTraits<ConstantVector> {
474 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
476 //===----------------------------------------------------------------------===//
477 /// ConstantPointerNull - a constant pointer value that points to null
479 class ConstantPointerNull : public Constant {
480 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
481 ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
483 explicit ConstantPointerNull(PointerType *T)
485 Value::ConstantPointerNullVal, nullptr, 0) {}
488 // allocate space for exactly zero operands
489 void *operator new(size_t s) {
490 return User::operator new(s, 0);
493 /// get() - Static factory methods - Return objects of the specified value
494 static ConstantPointerNull *get(PointerType *T);
496 void destroyConstant() override;
498 /// getType - Specialize the getType() method to always return an PointerType,
499 /// which reduces the amount of casting needed in parts of the compiler.
501 inline PointerType *getType() const {
502 return cast<PointerType>(Value::getType());
505 /// Methods for support type inquiry through isa, cast, and dyn_cast:
506 static bool classof(const Value *V) {
507 return V->getValueID() == ConstantPointerNullVal;
511 //===----------------------------------------------------------------------===//
512 /// ConstantDataSequential - A vector or array constant whose element type is a
513 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
514 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
515 /// operands because it stores all of the elements of the constant as densely
516 /// packed data, instead of as Value*'s.
518 /// This is the common base class of ConstantDataArray and ConstantDataVector.
520 class ConstantDataSequential : public Constant {
521 friend class LLVMContextImpl;
522 /// DataElements - A pointer to the bytes underlying this constant (which is
523 /// owned by the uniquing StringMap).
524 const char *DataElements;
526 /// Next - This forms a link list of ConstantDataSequential nodes that have
527 /// the same value but different type. For example, 0,0,0,1 could be a 4
528 /// element array of i8, or a 1-element array of i32. They'll both end up in
529 /// the same StringMap bucket, linked up.
530 ConstantDataSequential *Next;
531 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
532 ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
534 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
535 : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
536 ~ConstantDataSequential() { delete Next; }
538 static Constant *getImpl(StringRef Bytes, Type *Ty);
541 // allocate space for exactly zero operands.
542 void *operator new(size_t s) {
543 return User::operator new(s, 0);
547 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
548 /// formed with a vector or array of the specified element type.
549 /// ConstantDataArray only works with normal float and int types that are
550 /// stored densely in memory, not with things like i42 or x86_f80.
551 static bool isElementTypeCompatible(const Type *Ty);
553 /// getElementAsInteger - If this is a sequential container of integers (of
554 /// any size), return the specified element in the low bits of a uint64_t.
555 uint64_t getElementAsInteger(unsigned i) const;
557 /// getElementAsAPFloat - If this is a sequential container of floating point
558 /// type, return the specified element as an APFloat.
559 APFloat getElementAsAPFloat(unsigned i) const;
561 /// getElementAsFloat - If this is an sequential container of floats, return
562 /// the specified element as a float.
563 float getElementAsFloat(unsigned i) const;
565 /// getElementAsDouble - If this is an sequential container of doubles, return
566 /// the specified element as a double.
567 double getElementAsDouble(unsigned i) const;
569 /// getElementAsConstant - Return a Constant for a specified index's element.
570 /// Note that this has to compute a new constant to return, so it isn't as
571 /// efficient as getElementAsInteger/Float/Double.
572 Constant *getElementAsConstant(unsigned i) const;
574 /// getType - Specialize the getType() method to always return a
575 /// SequentialType, which reduces the amount of casting needed in parts of the
577 inline SequentialType *getType() const {
578 return cast<SequentialType>(Value::getType());
581 /// getElementType - Return the element type of the array/vector.
582 Type *getElementType() const;
584 /// getNumElements - Return the number of elements in the array or vector.
585 unsigned getNumElements() const;
587 /// getElementByteSize - Return the size (in bytes) of each element in the
588 /// array/vector. The size of the elements is known to be a multiple of one
590 uint64_t getElementByteSize() const;
593 /// isString - This method returns true if this is an array of i8.
594 bool isString() const;
596 /// isCString - This method returns true if the array "isString", ends with a
597 /// nul byte, and does not contains any other nul bytes.
598 bool isCString() const;
600 /// getAsString - If this array is isString(), then this method returns the
601 /// array as a StringRef. Otherwise, it asserts out.
603 StringRef getAsString() const {
604 assert(isString() && "Not a string");
605 return getRawDataValues();
608 /// getAsCString - If this array is isCString(), then this method returns the
609 /// array (without the trailing null byte) as a StringRef. Otherwise, it
612 StringRef getAsCString() const {
613 assert(isCString() && "Isn't a C string");
614 StringRef Str = getAsString();
615 return Str.substr(0, Str.size()-1);
618 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
619 /// that this is an extremely tricky thing to work with, as it exposes the
620 /// host endianness of the data elements.
621 StringRef getRawDataValues() const;
623 void destroyConstant() override;
625 /// Methods for support type inquiry through isa, cast, and dyn_cast:
627 static bool classof(const Value *V) {
628 return V->getValueID() == ConstantDataArrayVal ||
629 V->getValueID() == ConstantDataVectorVal;
632 const char *getElementPointer(unsigned Elt) const;
635 //===----------------------------------------------------------------------===//
636 /// ConstantDataArray - An array constant whose element type is a simple
637 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
638 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
639 /// operands because it stores all of the elements of the constant as densely
640 /// packed data, instead of as Value*'s.
641 class ConstantDataArray : public ConstantDataSequential {
642 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
643 ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
644 void anchor() override;
645 friend class ConstantDataSequential;
646 explicit ConstantDataArray(Type *ty, const char *Data)
647 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
649 // allocate space for exactly zero operands.
650 void *operator new(size_t s) {
651 return User::operator new(s, 0);
655 /// get() constructors - Return a constant with array type with an element
656 /// count and element type matching the ArrayRef passed in. Note that this
657 /// can return a ConstantAggregateZero object.
658 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
659 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
660 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
661 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
662 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
663 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
665 /// getString - This method constructs a CDS and initializes it with a text
666 /// string. The default behavior (AddNull==true) causes a null terminator to
667 /// be placed at the end of the array (increasing the length of the string by
668 /// one more than the StringRef would normally indicate. Pass AddNull=false
669 /// to disable this behavior.
670 static Constant *getString(LLVMContext &Context, StringRef Initializer,
671 bool AddNull = true);
673 /// getType - Specialize the getType() method to always return an ArrayType,
674 /// which reduces the amount of casting needed in parts of the compiler.
676 inline ArrayType *getType() const {
677 return cast<ArrayType>(Value::getType());
680 /// Methods for support type inquiry through isa, cast, and dyn_cast:
682 static bool classof(const Value *V) {
683 return V->getValueID() == ConstantDataArrayVal;
687 //===----------------------------------------------------------------------===//
688 /// ConstantDataVector - A vector constant whose element type is a simple
689 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
690 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
691 /// operands because it stores all of the elements of the constant as densely
692 /// packed data, instead of as Value*'s.
693 class ConstantDataVector : public ConstantDataSequential {
694 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
695 ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
696 void anchor() override;
697 friend class ConstantDataSequential;
698 explicit ConstantDataVector(Type *ty, const char *Data)
699 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
701 // allocate space for exactly zero operands.
702 void *operator new(size_t s) {
703 return User::operator new(s, 0);
707 /// get() constructors - Return a constant with vector type with an element
708 /// count and element type matching the ArrayRef passed in. Note that this
709 /// can return a ConstantAggregateZero object.
710 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
711 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
712 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
713 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
714 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
715 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
717 /// getSplat - Return a ConstantVector with the specified constant in each
718 /// element. The specified constant has to be a of a compatible type (i8/i16/
719 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
720 static Constant *getSplat(unsigned NumElts, Constant *Elt);
722 /// getSplatValue - If this is a splat constant, meaning that all of the
723 /// elements have the same value, return that value. Otherwise return NULL.
724 Constant *getSplatValue() const;
726 /// getType - Specialize the getType() method to always return a VectorType,
727 /// which reduces the amount of casting needed in parts of the compiler.
729 inline VectorType *getType() const {
730 return cast<VectorType>(Value::getType());
733 /// Methods for support type inquiry through isa, cast, and dyn_cast:
735 static bool classof(const Value *V) {
736 return V->getValueID() == ConstantDataVectorVal;
742 /// BlockAddress - The address of a basic block.
744 class BlockAddress : public Constant {
745 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
746 void *operator new(size_t s) { return User::operator new(s, 2); }
747 BlockAddress(Function *F, BasicBlock *BB);
749 /// get - Return a BlockAddress for the specified function and basic block.
750 static BlockAddress *get(Function *F, BasicBlock *BB);
752 /// get - Return a BlockAddress for the specified basic block. The basic
753 /// block must be embedded into a function.
754 static BlockAddress *get(BasicBlock *BB);
756 /// \brief Lookup an existing \c BlockAddress constant for the given
759 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
760 static BlockAddress *lookup(const BasicBlock *BB);
762 /// Transparently provide more efficient getOperand methods.
763 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
765 Function *getFunction() const { return (Function*)Op<0>().get(); }
766 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
768 void destroyConstant() override;
769 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
771 /// Methods for support type inquiry through isa, cast, and dyn_cast:
772 static inline bool classof(const Value *V) {
773 return V->getValueID() == BlockAddressVal;
778 struct OperandTraits<BlockAddress> :
779 public FixedNumOperandTraits<BlockAddress, 2> {
782 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
785 //===----------------------------------------------------------------------===//
786 /// ConstantExpr - a constant value that is initialized with an expression using
787 /// other constant values.
789 /// This class uses the standard Instruction opcodes to define the various
790 /// constant expressions. The Opcode field for the ConstantExpr class is
791 /// maintained in the Value::SubclassData field.
792 class ConstantExpr : public Constant {
793 friend struct ConstantExprKeyType;
796 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
797 : Constant(ty, ConstantExprVal, Ops, NumOps) {
798 // Operation type (an Instruction opcode) is stored as the SubclassData.
799 setValueSubclassData(Opcode);
803 // Static methods to construct a ConstantExpr of different kinds. Note that
804 // these methods may return a object that is not an instance of the
805 // ConstantExpr class, because they will attempt to fold the constant
806 // expression into something simpler if possible.
808 /// getAlignOf constant expr - computes the alignment of a type in a target
809 /// independent way (Note: the return type is an i64).
810 static Constant *getAlignOf(Type *Ty);
812 /// getSizeOf constant expr - computes the (alloc) size of a type (in
813 /// address-units, not bits) in a target independent way (Note: the return
816 static Constant *getSizeOf(Type *Ty);
818 /// getOffsetOf constant expr - computes the offset of a struct field in a
819 /// target independent way (Note: the return type is an i64).
821 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
823 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
824 /// which supports any aggregate type, and any Constant index.
826 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
828 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
829 static Constant *getFNeg(Constant *C);
830 static Constant *getNot(Constant *C);
831 static Constant *getAdd(Constant *C1, Constant *C2,
832 bool HasNUW = false, bool HasNSW = false);
833 static Constant *getFAdd(Constant *C1, Constant *C2);
834 static Constant *getSub(Constant *C1, Constant *C2,
835 bool HasNUW = false, bool HasNSW = false);
836 static Constant *getFSub(Constant *C1, Constant *C2);
837 static Constant *getMul(Constant *C1, Constant *C2,
838 bool HasNUW = false, bool HasNSW = false);
839 static Constant *getFMul(Constant *C1, Constant *C2);
840 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
841 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
842 static Constant *getFDiv(Constant *C1, Constant *C2);
843 static Constant *getURem(Constant *C1, Constant *C2);
844 static Constant *getSRem(Constant *C1, Constant *C2);
845 static Constant *getFRem(Constant *C1, Constant *C2);
846 static Constant *getAnd(Constant *C1, Constant *C2);
847 static Constant *getOr(Constant *C1, Constant *C2);
848 static Constant *getXor(Constant *C1, Constant *C2);
849 static Constant *getShl(Constant *C1, Constant *C2,
850 bool HasNUW = false, bool HasNSW = false);
851 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
852 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
853 static Constant *getTrunc (Constant *C, Type *Ty);
854 static Constant *getSExt (Constant *C, Type *Ty);
855 static Constant *getZExt (Constant *C, Type *Ty);
856 static Constant *getFPTrunc (Constant *C, Type *Ty);
857 static Constant *getFPExtend(Constant *C, Type *Ty);
858 static Constant *getUIToFP (Constant *C, Type *Ty);
859 static Constant *getSIToFP (Constant *C, Type *Ty);
860 static Constant *getFPToUI (Constant *C, Type *Ty);
861 static Constant *getFPToSI (Constant *C, Type *Ty);
862 static Constant *getPtrToInt(Constant *C, Type *Ty);
863 static Constant *getIntToPtr(Constant *C, Type *Ty);
864 static Constant *getBitCast (Constant *C, Type *Ty);
865 static Constant *getAddrSpaceCast(Constant *C, Type *Ty);
867 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
868 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
869 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
870 return getAdd(C1, C2, false, true);
872 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
873 return getAdd(C1, C2, true, false);
875 static Constant *getNSWSub(Constant *C1, Constant *C2) {
876 return getSub(C1, C2, false, true);
878 static Constant *getNUWSub(Constant *C1, Constant *C2) {
879 return getSub(C1, C2, true, false);
881 static Constant *getNSWMul(Constant *C1, Constant *C2) {
882 return getMul(C1, C2, false, true);
884 static Constant *getNUWMul(Constant *C1, Constant *C2) {
885 return getMul(C1, C2, true, false);
887 static Constant *getNSWShl(Constant *C1, Constant *C2) {
888 return getShl(C1, C2, false, true);
890 static Constant *getNUWShl(Constant *C1, Constant *C2) {
891 return getShl(C1, C2, true, false);
893 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
894 return getSDiv(C1, C2, true);
896 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
897 return getUDiv(C1, C2, true);
899 static Constant *getExactAShr(Constant *C1, Constant *C2) {
900 return getAShr(C1, C2, true);
902 static Constant *getExactLShr(Constant *C1, Constant *C2) {
903 return getLShr(C1, C2, true);
906 /// getBinOpIdentity - Return the identity for the given binary operation,
907 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
908 /// returns null if the operator doesn't have an identity.
909 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
911 /// getBinOpAbsorber - Return the absorbing element for the given binary
912 /// operation, i.e. a constant C such that X op C = C and C op X = C for
913 /// every X. For example, this returns zero for integer multiplication.
914 /// It returns null if the operator doesn't have an absorbing element.
915 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
917 /// Transparently provide more efficient getOperand methods.
918 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
920 // @brief Convenience function for getting one of the casting operations
921 // using a CastOps opcode.
922 static Constant *getCast(
923 unsigned ops, ///< The opcode for the conversion
924 Constant *C, ///< The constant to be converted
925 Type *Ty ///< The type to which the constant is converted
928 // @brief Create a ZExt or BitCast cast constant expression
929 static Constant *getZExtOrBitCast(
930 Constant *C, ///< The constant to zext or bitcast
931 Type *Ty ///< The type to zext or bitcast C to
934 // @brief Create a SExt or BitCast cast constant expression
935 static Constant *getSExtOrBitCast(
936 Constant *C, ///< The constant to sext or bitcast
937 Type *Ty ///< The type to sext or bitcast C to
940 // @brief Create a Trunc or BitCast cast constant expression
941 static Constant *getTruncOrBitCast(
942 Constant *C, ///< The constant to trunc or bitcast
943 Type *Ty ///< The type to trunc or bitcast C to
946 /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
948 static Constant *getPointerCast(
949 Constant *C, ///< The pointer value to be casted (operand 0)
950 Type *Ty ///< The type to which cast should be made
953 /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
954 /// the address space.
955 static Constant *getPointerBitCastOrAddrSpaceCast(
956 Constant *C, ///< The constant to addrspacecast or bitcast
957 Type *Ty ///< The type to bitcast or addrspacecast C to
960 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
961 static Constant *getIntegerCast(
962 Constant *C, ///< The integer constant to be casted
963 Type *Ty, ///< The integer type to cast to
964 bool isSigned ///< Whether C should be treated as signed or not
967 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
968 static Constant *getFPCast(
969 Constant *C, ///< The integer constant to be casted
970 Type *Ty ///< The integer type to cast to
973 /// @brief Return true if this is a convert constant expression
976 /// @brief Return true if this is a compare constant expression
977 bool isCompare() const;
979 /// @brief Return true if this is an insertvalue or extractvalue expression,
980 /// and the getIndices() method may be used.
981 bool hasIndices() const;
983 /// @brief Return true if this is a getelementptr expression and all
984 /// the index operands are compile-time known integers within the
985 /// corresponding notional static array extents. Note that this is
986 /// not equivalant to, a subset of, or a superset of the "inbounds"
988 bool isGEPWithNoNotionalOverIndexing() const;
990 /// Select constant expr
992 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
994 /// get - Return a binary or shift operator constant expression,
995 /// folding if possible.
997 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1000 /// @brief Return an ICmp or FCmp comparison operator constant expression.
1001 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
1003 /// get* - Return some common constants without having to
1004 /// specify the full Instruction::OPCODE identifier.
1006 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1007 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1009 /// Getelementptr form. Value* is only accepted for convenience;
1010 /// all elements must be Constant's.
1012 static Constant *getGetElementPtr(Constant *C,
1013 ArrayRef<Constant *> IdxList,
1014 bool InBounds = false) {
1015 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1019 static Constant *getGetElementPtr(Constant *C,
1021 bool InBounds = false) {
1022 // This form of the function only exists to avoid ambiguous overload
1023 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1024 // ArrayRef<Value *>.
1025 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1027 static Constant *getGetElementPtr(Constant *C,
1028 ArrayRef<Value *> IdxList,
1029 bool InBounds = false);
1031 /// Create an "inbounds" getelementptr. See the documentation for the
1032 /// "inbounds" flag in LangRef.html for details.
1033 static Constant *getInBoundsGetElementPtr(Constant *C,
1034 ArrayRef<Constant *> IdxList) {
1035 return getGetElementPtr(C, IdxList, true);
1037 static Constant *getInBoundsGetElementPtr(Constant *C,
1039 // This form of the function only exists to avoid ambiguous overload
1040 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1041 // ArrayRef<Value *>.
1042 return getGetElementPtr(C, Idx, true);
1044 static Constant *getInBoundsGetElementPtr(Constant *C,
1045 ArrayRef<Value *> IdxList) {
1046 return getGetElementPtr(C, IdxList, true);
1049 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1050 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1051 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1052 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1053 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1054 ArrayRef<unsigned> Idxs);
1056 /// getOpcode - Return the opcode at the root of this constant expression
1057 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1059 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1060 /// not an ICMP or FCMP constant expression.
1061 unsigned getPredicate() const;
1063 /// getIndices - Assert that this is an insertvalue or exactvalue
1064 /// expression and return the list of indices.
1065 ArrayRef<unsigned> getIndices() const;
1067 /// getOpcodeName - Return a string representation for an opcode.
1068 const char *getOpcodeName() const;
1070 /// getWithOperandReplaced - Return a constant expression identical to this
1071 /// one, but with the specified operand set to the specified value.
1072 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1074 /// getWithOperands - This returns the current constant expression with the
1075 /// operands replaced with the specified values. The specified array must
1076 /// have the same number of operands as our current one.
1077 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1078 return getWithOperands(Ops, getType());
1081 /// getWithOperands - This returns the current constant expression with the
1082 /// operands replaced with the specified values and with the specified result
1083 /// type. The specified array must have the same number of operands as our
1085 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1087 /// getAsInstruction - Returns an Instruction which implements the same operation
1088 /// as this ConstantExpr. The instruction is not linked to any basic block.
1090 /// A better approach to this could be to have a constructor for Instruction
1091 /// which would take a ConstantExpr parameter, but that would have spread
1092 /// implementation details of ConstantExpr outside of Constants.cpp, which
1093 /// would make it harder to remove ConstantExprs altogether.
1094 Instruction *getAsInstruction();
1096 void destroyConstant() override;
1097 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
1099 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1100 static inline bool classof(const Value *V) {
1101 return V->getValueID() == ConstantExprVal;
1105 // Shadow Value::setValueSubclassData with a private forwarding method so that
1106 // subclasses cannot accidentally use it.
1107 void setValueSubclassData(unsigned short D) {
1108 Value::setValueSubclassData(D);
1113 struct OperandTraits<ConstantExpr> :
1114 public VariadicOperandTraits<ConstantExpr, 1> {
1117 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1119 //===----------------------------------------------------------------------===//
1120 /// UndefValue - 'undef' values are things that do not have specified contents.
1121 /// These are used for a variety of purposes, including global variable
1122 /// initializers and operands to instructions. 'undef' values can occur with
1123 /// any first-class type.
1125 /// Undef values aren't exactly constants; if they have multiple uses, they
1126 /// can appear to have different bit patterns at each use. See
1127 /// LangRef.html#undefvalues for details.
1129 class UndefValue : public Constant {
1130 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1131 UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1133 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
1135 // allocate space for exactly zero operands
1136 void *operator new(size_t s) {
1137 return User::operator new(s, 0);
1140 /// get() - Static factory methods - Return an 'undef' object of the specified
1143 static UndefValue *get(Type *T);
1145 /// getSequentialElement - If this Undef has array or vector type, return a
1146 /// undef with the right element type.
1147 UndefValue *getSequentialElement() const;
1149 /// getStructElement - If this undef has struct type, return a undef with the
1150 /// right element type for the specified element.
1151 UndefValue *getStructElement(unsigned Elt) const;
1153 /// getElementValue - Return an undef of the right value for the specified GEP
1155 UndefValue *getElementValue(Constant *C) const;
1157 /// getElementValue - Return an undef of the right value for the specified GEP
1159 UndefValue *getElementValue(unsigned Idx) const;
1161 void destroyConstant() override;
1163 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1164 static bool classof(const Value *V) {
1165 return V->getValueID() == UndefValueVal;
1169 } // End llvm namespace