[Alignment][NFC] Remove dependency on GlobalObject::setAlignment(unsigned)
[llvm-core.git] / include / llvm / IR / Constants.h
blobca56e8b9328ca5d6ee3b0a083a9054c67bcb961e
1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// This file contains the declarations for the subclasses of Constant,
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are
13 /// fully shared by structural equivalence. This means that two structurally
14 /// equivalent constants will always have the same address. Constants are
15 /// created on demand as needed and never deleted: thus clients don't have to
16 /// worry about the lifetime of the objects.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_IR_CONSTANTS_H
21 #define LLVM_IR_CONSTANTS_H
23 #include "llvm/ADT/APFloat.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/None.h"
27 #include "llvm/ADT/Optional.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/IR/Constant.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/OperandTraits.h"
33 #include "llvm/IR/User.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include <cassert>
39 #include <cstddef>
40 #include <cstdint>
42 namespace llvm {
44 class ArrayType;
45 class IntegerType;
46 class PointerType;
47 class SequentialType;
48 class StructType;
49 class VectorType;
50 template <class ConstantClass> struct ConstantAggrKeyType;
52 /// Base class for constants with no operands.
53 ///
54 /// These constants have no operands; they represent their data directly.
55 /// Since they can be in use by unrelated modules (and are never based on
56 /// GlobalValues), it never makes sense to RAUW them.
57 class ConstantData : public Constant {
58 friend class Constant;
60 Value *handleOperandChangeImpl(Value *From, Value *To) {
61 llvm_unreachable("Constant data does not have operands!");
64 protected:
65 explicit ConstantData(Type *Ty, ValueTy VT) : Constant(Ty, VT, nullptr, 0) {}
67 void *operator new(size_t s) { return User::operator new(s, 0); }
69 public:
70 ConstantData(const ConstantData &) = delete;
72 /// Methods to support type inquiry through isa, cast, and dyn_cast.
73 static bool classof(const Value *V) {
74 return V->getValueID() >= ConstantDataFirstVal &&
75 V->getValueID() <= ConstantDataLastVal;
79 //===----------------------------------------------------------------------===//
80 /// This is the shared class of boolean and integer constants. This class
81 /// represents both boolean and integral constants.
82 /// Class for constant integers.
83 class ConstantInt final : public ConstantData {
84 friend class Constant;
86 APInt Val;
88 ConstantInt(IntegerType *Ty, const APInt& V);
90 void destroyConstantImpl();
92 public:
93 ConstantInt(const ConstantInt &) = delete;
95 static ConstantInt *getTrue(LLVMContext &Context);
96 static ConstantInt *getFalse(LLVMContext &Context);
97 static Constant *getTrue(Type *Ty);
98 static Constant *getFalse(Type *Ty);
100 /// If Ty is a vector type, return a Constant with a splat of the given
101 /// value. Otherwise return a ConstantInt for the given value.
102 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
104 /// Return a ConstantInt with the specified integer value for the specified
105 /// type. If the type is wider than 64 bits, the value will be zero-extended
106 /// to fit the type, unless isSigned is true, in which case the value will
107 /// be interpreted as a 64-bit signed integer and sign-extended to fit
108 /// the type.
109 /// Get a ConstantInt for a specific value.
110 static ConstantInt *get(IntegerType *Ty, uint64_t V,
111 bool isSigned = false);
113 /// Return a ConstantInt with the specified value for the specified type. The
114 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
115 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
116 /// signed value for the type Ty.
117 /// Get a ConstantInt for a specific signed value.
118 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
119 static Constant *getSigned(Type *Ty, int64_t V);
121 /// Return a ConstantInt with the specified value and an implied Type. The
122 /// type is the integer type that corresponds to the bit width of the value.
123 static ConstantInt *get(LLVMContext &Context, const APInt &V);
125 /// Return a ConstantInt constructed from the string strStart with the given
126 /// radix.
127 static ConstantInt *get(IntegerType *Ty, StringRef Str,
128 uint8_t radix);
130 /// If Ty is a vector type, return a Constant with a splat of the given
131 /// value. Otherwise return a ConstantInt for the given value.
132 static Constant *get(Type* Ty, const APInt& V);
134 /// Return the constant as an APInt value reference. This allows clients to
135 /// obtain a full-precision copy of the value.
136 /// Return the constant's value.
137 inline const APInt &getValue() const {
138 return Val;
141 /// getBitWidth - Return the bitwidth of this constant.
142 unsigned getBitWidth() const { return Val.getBitWidth(); }
144 /// Return the constant as a 64-bit unsigned integer value after it
145 /// has been zero extended as appropriate for the type of this constant. Note
146 /// that this method can assert if the value does not fit in 64 bits.
147 /// Return the zero extended value.
148 inline uint64_t getZExtValue() const {
149 return Val.getZExtValue();
152 /// Return the constant as a 64-bit integer value after it has been sign
153 /// extended as appropriate for the type of this constant. Note that
154 /// this method can assert if the value does not fit in 64 bits.
155 /// Return the sign extended value.
156 inline int64_t getSExtValue() const {
157 return Val.getSExtValue();
160 /// A helper method that can be used to determine if the constant contained
161 /// within is equal to a constant. This only works for very small values,
162 /// because this is all that can be represented with all types.
163 /// Determine if this constant's value is same as an unsigned char.
164 bool equalsInt(uint64_t V) const {
165 return Val == V;
168 /// getType - Specialize the getType() method to always return an IntegerType,
169 /// which reduces the amount of casting needed in parts of the compiler.
171 inline IntegerType *getType() const {
172 return cast<IntegerType>(Value::getType());
175 /// This static method returns true if the type Ty is big enough to
176 /// represent the value V. This can be used to avoid having the get method
177 /// assert when V is larger than Ty can represent. Note that there are two
178 /// versions of this method, one for unsigned and one for signed integers.
179 /// Although ConstantInt canonicalizes everything to an unsigned integer,
180 /// the signed version avoids callers having to convert a signed quantity
181 /// to the appropriate unsigned type before calling the method.
182 /// @returns true if V is a valid value for type Ty
183 /// Determine if the value is in range for the given type.
184 static bool isValueValidForType(Type *Ty, uint64_t V);
185 static bool isValueValidForType(Type *Ty, int64_t V);
187 bool isNegative() const { return Val.isNegative(); }
189 /// This is just a convenience method to make client code smaller for a
190 /// common code. It also correctly performs the comparison without the
191 /// potential for an assertion from getZExtValue().
192 bool isZero() const {
193 return Val.isNullValue();
196 /// This is just a convenience method to make client code smaller for a
197 /// common case. It also correctly performs the comparison without the
198 /// potential for an assertion from getZExtValue().
199 /// Determine if the value is one.
200 bool isOne() const {
201 return Val.isOneValue();
204 /// This function will return true iff every bit in this constant is set
205 /// to true.
206 /// @returns true iff this constant's bits are all set to true.
207 /// Determine if the value is all ones.
208 bool isMinusOne() const {
209 return Val.isAllOnesValue();
212 /// This function will return true iff this constant represents the largest
213 /// value that may be represented by the constant's type.
214 /// @returns true iff this is the largest value that may be represented
215 /// by this type.
216 /// Determine if the value is maximal.
217 bool isMaxValue(bool isSigned) const {
218 if (isSigned)
219 return Val.isMaxSignedValue();
220 else
221 return Val.isMaxValue();
224 /// This function will return true iff this constant represents the smallest
225 /// value that may be represented by this constant's type.
226 /// @returns true if this is the smallest value that may be represented by
227 /// this type.
228 /// Determine if the value is minimal.
229 bool isMinValue(bool isSigned) const {
230 if (isSigned)
231 return Val.isMinSignedValue();
232 else
233 return Val.isMinValue();
236 /// This function will return true iff this constant represents a value with
237 /// active bits bigger than 64 bits or a value greater than the given uint64_t
238 /// value.
239 /// @returns true iff this constant is greater or equal to the given number.
240 /// Determine if the value is greater or equal to the given number.
241 bool uge(uint64_t Num) const {
242 return Val.uge(Num);
245 /// getLimitedValue - If the value is smaller than the specified limit,
246 /// return it, otherwise return the limit value. This causes the value
247 /// to saturate to the limit.
248 /// @returns the min of the value of the constant and the specified value
249 /// Get the constant's value with a saturation limit
250 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
251 return Val.getLimitedValue(Limit);
254 /// Methods to support type inquiry through isa, cast, and dyn_cast.
255 static bool classof(const Value *V) {
256 return V->getValueID() == ConstantIntVal;
260 //===----------------------------------------------------------------------===//
261 /// ConstantFP - Floating Point Values [float, double]
263 class ConstantFP final : public ConstantData {
264 friend class Constant;
266 APFloat Val;
268 ConstantFP(Type *Ty, const APFloat& V);
270 void destroyConstantImpl();
272 public:
273 ConstantFP(const ConstantFP &) = delete;
275 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
276 /// method returns the negative zero constant for floating point or vector
277 /// floating point types; for all other types, it returns the null value.
278 static Constant *getZeroValueForNegation(Type *Ty);
280 /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
281 /// for the specified value in the specified type. This should only be used
282 /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
283 /// host double and as the target format.
284 static Constant *get(Type* Ty, double V);
286 /// If Ty is a vector type, return a Constant with a splat of the given
287 /// value. Otherwise return a ConstantFP for the given value.
288 static Constant *get(Type *Ty, const APFloat &V);
290 static Constant *get(Type* Ty, StringRef Str);
291 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
292 static Constant *getNaN(Type *Ty, bool Negative = false, uint64_t Payload = 0);
293 static Constant *getQNaN(Type *Ty, bool Negative = false,
294 APInt *Payload = nullptr);
295 static Constant *getSNaN(Type *Ty, bool Negative = false,
296 APInt *Payload = nullptr);
297 static Constant *getNegativeZero(Type *Ty);
298 static Constant *getInfinity(Type *Ty, bool Negative = false);
300 /// Return true if Ty is big enough to represent V.
301 static bool isValueValidForType(Type *Ty, const APFloat &V);
302 inline const APFloat &getValueAPF() const { return Val; }
304 /// Return true if the value is positive or negative zero.
305 bool isZero() const { return Val.isZero(); }
307 /// Return true if the sign bit is set.
308 bool isNegative() const { return Val.isNegative(); }
310 /// Return true if the value is infinity
311 bool isInfinity() const { return Val.isInfinity(); }
313 /// Return true if the value is a NaN.
314 bool isNaN() const { return Val.isNaN(); }
316 /// We don't rely on operator== working on double values, as it returns true
317 /// for things that are clearly not equal, like -0.0 and 0.0.
318 /// As such, this method can be used to do an exact bit-for-bit comparison of
319 /// two floating point values. The version with a double operand is retained
320 /// because it's so convenient to write isExactlyValue(2.0), but please use
321 /// it only for simple constants.
322 bool isExactlyValue(const APFloat &V) const;
324 bool isExactlyValue(double V) const {
325 bool ignored;
326 APFloat FV(V);
327 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
328 return isExactlyValue(FV);
331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
332 static bool classof(const Value *V) {
333 return V->getValueID() == ConstantFPVal;
337 //===----------------------------------------------------------------------===//
338 /// All zero aggregate value
340 class ConstantAggregateZero final : public ConstantData {
341 friend class Constant;
343 explicit ConstantAggregateZero(Type *Ty)
344 : ConstantData(Ty, ConstantAggregateZeroVal) {}
346 void destroyConstantImpl();
348 public:
349 ConstantAggregateZero(const ConstantAggregateZero &) = delete;
351 static ConstantAggregateZero *get(Type *Ty);
353 /// If this CAZ has array or vector type, return a zero with the right element
354 /// type.
355 Constant *getSequentialElement() const;
357 /// If this CAZ has struct type, return a zero with the right element type for
358 /// the specified element.
359 Constant *getStructElement(unsigned Elt) const;
361 /// Return a zero of the right value for the specified GEP index if we can,
362 /// otherwise return null (e.g. if C is a ConstantExpr).
363 Constant *getElementValue(Constant *C) const;
365 /// Return a zero of the right value for the specified GEP index.
366 Constant *getElementValue(unsigned Idx) const;
368 /// Return the number of elements in the array, vector, or struct.
369 unsigned getNumElements() const;
371 /// Methods for support type inquiry through isa, cast, and dyn_cast:
373 static bool classof(const Value *V) {
374 return V->getValueID() == ConstantAggregateZeroVal;
378 /// Base class for aggregate constants (with operands).
380 /// These constants are aggregates of other constants, which are stored as
381 /// operands.
383 /// Subclasses are \a ConstantStruct, \a ConstantArray, and \a
384 /// ConstantVector.
386 /// \note Some subclasses of \a ConstantData are semantically aggregates --
387 /// such as \a ConstantDataArray -- but are not subclasses of this because they
388 /// use operands.
389 class ConstantAggregate : public Constant {
390 protected:
391 ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef<Constant *> V);
393 public:
394 /// Transparently provide more efficient getOperand methods.
395 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
397 /// Methods for support type inquiry through isa, cast, and dyn_cast:
398 static bool classof(const Value *V) {
399 return V->getValueID() >= ConstantAggregateFirstVal &&
400 V->getValueID() <= ConstantAggregateLastVal;
404 template <>
405 struct OperandTraits<ConstantAggregate>
406 : public VariadicOperandTraits<ConstantAggregate> {};
408 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantAggregate, Constant)
410 //===----------------------------------------------------------------------===//
411 /// ConstantArray - Constant Array Declarations
413 class ConstantArray final : public ConstantAggregate {
414 friend struct ConstantAggrKeyType<ConstantArray>;
415 friend class Constant;
417 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
419 void destroyConstantImpl();
420 Value *handleOperandChangeImpl(Value *From, Value *To);
422 public:
423 // ConstantArray accessors
424 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
426 private:
427 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
429 public:
430 /// Specialize the getType() method to always return an ArrayType,
431 /// which reduces the amount of casting needed in parts of the compiler.
432 inline ArrayType *getType() const {
433 return cast<ArrayType>(Value::getType());
436 /// Methods for support type inquiry through isa, cast, and dyn_cast:
437 static bool classof(const Value *V) {
438 return V->getValueID() == ConstantArrayVal;
442 //===----------------------------------------------------------------------===//
443 // Constant Struct Declarations
445 class ConstantStruct final : public ConstantAggregate {
446 friend struct ConstantAggrKeyType<ConstantStruct>;
447 friend class Constant;
449 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
451 void destroyConstantImpl();
452 Value *handleOperandChangeImpl(Value *From, Value *To);
454 public:
455 // ConstantStruct accessors
456 static Constant *get(StructType *T, ArrayRef<Constant*> V);
458 template <typename... Csts>
459 static typename std::enable_if<are_base_of<Constant, Csts...>::value,
460 Constant *>::type
461 get(StructType *T, Csts *... Vs) {
462 SmallVector<Constant *, 8> Values({Vs...});
463 return get(T, Values);
466 /// Return an anonymous struct that has the specified elements.
467 /// If the struct is possibly empty, then you must specify a context.
468 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
469 return get(getTypeForElements(V, Packed), V);
471 static Constant *getAnon(LLVMContext &Ctx,
472 ArrayRef<Constant*> V, bool Packed = false) {
473 return get(getTypeForElements(Ctx, V, Packed), V);
476 /// Return an anonymous struct type to use for a constant with the specified
477 /// set of elements. The list must not be empty.
478 static StructType *getTypeForElements(ArrayRef<Constant*> V,
479 bool Packed = false);
480 /// This version of the method allows an empty list.
481 static StructType *getTypeForElements(LLVMContext &Ctx,
482 ArrayRef<Constant*> V,
483 bool Packed = false);
485 /// Specialization - reduce amount of casting.
486 inline StructType *getType() const {
487 return cast<StructType>(Value::getType());
490 /// Methods for support type inquiry through isa, cast, and dyn_cast:
491 static bool classof(const Value *V) {
492 return V->getValueID() == ConstantStructVal;
496 //===----------------------------------------------------------------------===//
497 /// Constant Vector Declarations
499 class ConstantVector final : public ConstantAggregate {
500 friend struct ConstantAggrKeyType<ConstantVector>;
501 friend class Constant;
503 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
505 void destroyConstantImpl();
506 Value *handleOperandChangeImpl(Value *From, Value *To);
508 public:
509 // ConstantVector accessors
510 static Constant *get(ArrayRef<Constant*> V);
512 private:
513 static Constant *getImpl(ArrayRef<Constant *> V);
515 public:
516 /// Return a ConstantVector with the specified constant in each element.
517 static Constant *getSplat(unsigned NumElts, Constant *Elt);
519 /// Specialize the getType() method to always return a VectorType,
520 /// which reduces the amount of casting needed in parts of the compiler.
521 inline VectorType *getType() const {
522 return cast<VectorType>(Value::getType());
525 /// If this is a splat constant, meaning that all of the elements have the
526 /// same value, return that value. Otherwise return NULL.
527 Constant *getSplatValue() const;
529 /// Methods for support type inquiry through isa, cast, and dyn_cast:
530 static bool classof(const Value *V) {
531 return V->getValueID() == ConstantVectorVal;
535 //===----------------------------------------------------------------------===//
536 /// A constant pointer value that points to null
538 class ConstantPointerNull final : public ConstantData {
539 friend class Constant;
541 explicit ConstantPointerNull(PointerType *T)
542 : ConstantData(T, Value::ConstantPointerNullVal) {}
544 void destroyConstantImpl();
546 public:
547 ConstantPointerNull(const ConstantPointerNull &) = delete;
549 /// Static factory methods - Return objects of the specified value
550 static ConstantPointerNull *get(PointerType *T);
552 /// Specialize the getType() method to always return an PointerType,
553 /// which reduces the amount of casting needed in parts of the compiler.
554 inline PointerType *getType() const {
555 return cast<PointerType>(Value::getType());
558 /// Methods for support type inquiry through isa, cast, and dyn_cast:
559 static bool classof(const Value *V) {
560 return V->getValueID() == ConstantPointerNullVal;
564 //===----------------------------------------------------------------------===//
565 /// ConstantDataSequential - A vector or array constant whose element type is a
566 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
567 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
568 /// operands because it stores all of the elements of the constant as densely
569 /// packed data, instead of as Value*'s.
571 /// This is the common base class of ConstantDataArray and ConstantDataVector.
573 class ConstantDataSequential : public ConstantData {
574 friend class LLVMContextImpl;
575 friend class Constant;
577 /// A pointer to the bytes underlying this constant (which is owned by the
578 /// uniquing StringMap).
579 const char *DataElements;
581 /// This forms a link list of ConstantDataSequential nodes that have
582 /// the same value but different type. For example, 0,0,0,1 could be a 4
583 /// element array of i8, or a 1-element array of i32. They'll both end up in
584 /// the same StringMap bucket, linked up.
585 ConstantDataSequential *Next;
587 void destroyConstantImpl();
589 protected:
590 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
591 : ConstantData(ty, VT), DataElements(Data), Next(nullptr) {}
592 ~ConstantDataSequential() { delete Next; }
594 static Constant *getImpl(StringRef Bytes, Type *Ty);
596 public:
597 ConstantDataSequential(const ConstantDataSequential &) = delete;
599 /// Return true if a ConstantDataSequential can be formed with a vector or
600 /// array of the specified element type.
601 /// ConstantDataArray only works with normal float and int types that are
602 /// stored densely in memory, not with things like i42 or x86_f80.
603 static bool isElementTypeCompatible(Type *Ty);
605 /// If this is a sequential container of integers (of any size), return the
606 /// specified element in the low bits of a uint64_t.
607 uint64_t getElementAsInteger(unsigned i) const;
609 /// If this is a sequential container of integers (of any size), return the
610 /// specified element as an APInt.
611 APInt getElementAsAPInt(unsigned i) const;
613 /// If this is a sequential container of floating point type, return the
614 /// specified element as an APFloat.
615 APFloat getElementAsAPFloat(unsigned i) const;
617 /// If this is an sequential container of floats, return the specified element
618 /// as a float.
619 float getElementAsFloat(unsigned i) const;
621 /// If this is an sequential container of doubles, return the specified
622 /// element as a double.
623 double getElementAsDouble(unsigned i) const;
625 /// Return a Constant for a specified index's element.
626 /// Note that this has to compute a new constant to return, so it isn't as
627 /// efficient as getElementAsInteger/Float/Double.
628 Constant *getElementAsConstant(unsigned i) const;
630 /// Specialize the getType() method to always return a SequentialType, which
631 /// reduces the amount of casting needed in parts of the compiler.
632 inline SequentialType *getType() const {
633 return cast<SequentialType>(Value::getType());
636 /// Return the element type of the array/vector.
637 Type *getElementType() const;
639 /// Return the number of elements in the array or vector.
640 unsigned getNumElements() const;
642 /// Return the size (in bytes) of each element in the array/vector.
643 /// The size of the elements is known to be a multiple of one byte.
644 uint64_t getElementByteSize() const;
646 /// This method returns true if this is an array of \p CharSize integers.
647 bool isString(unsigned CharSize = 8) const;
649 /// This method returns true if the array "isString", ends with a null byte,
650 /// and does not contains any other null bytes.
651 bool isCString() const;
653 /// If this array is isString(), then this method returns the array as a
654 /// StringRef. Otherwise, it asserts out.
655 StringRef getAsString() const {
656 assert(isString() && "Not a string");
657 return getRawDataValues();
660 /// If this array is isCString(), then this method returns the array (without
661 /// the trailing null byte) as a StringRef. Otherwise, it asserts out.
662 StringRef getAsCString() const {
663 assert(isCString() && "Isn't a C string");
664 StringRef Str = getAsString();
665 return Str.substr(0, Str.size()-1);
668 /// Return the raw, underlying, bytes of this data. Note that this is an
669 /// extremely tricky thing to work with, as it exposes the host endianness of
670 /// the data elements.
671 StringRef getRawDataValues() const;
673 /// Methods for support type inquiry through isa, cast, and dyn_cast:
674 static bool classof(const Value *V) {
675 return V->getValueID() == ConstantDataArrayVal ||
676 V->getValueID() == ConstantDataVectorVal;
679 private:
680 const char *getElementPointer(unsigned Elt) const;
683 //===----------------------------------------------------------------------===//
684 /// An array constant whose element type is a simple 1/2/4/8-byte integer or
685 /// float/double, and whose elements are just simple data values
686 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
687 /// stores all of the elements of the constant as densely packed data, instead
688 /// of as Value*'s.
689 class ConstantDataArray final : public ConstantDataSequential {
690 friend class ConstantDataSequential;
692 explicit ConstantDataArray(Type *ty, const char *Data)
693 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
695 public:
696 ConstantDataArray(const ConstantDataArray &) = delete;
698 /// get() constructor - Return a constant with array type with an element
699 /// count and element type matching the ArrayRef passed in. Note that this
700 /// can return a ConstantAggregateZero object.
701 template <typename ElementTy>
702 static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
703 const char *Data = reinterpret_cast<const char *>(Elts.data());
704 return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
705 Type::getScalarTy<ElementTy>(Context));
708 /// get() constructor - ArrayTy needs to be compatible with
709 /// ArrayRef<ElementTy>. Calls get(LLVMContext, ArrayRef<ElementTy>).
710 template <typename ArrayTy>
711 static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
712 return ConstantDataArray::get(Context, makeArrayRef(Elts));
715 /// get() constructor - Return a constant with array type with an element
716 /// count and element type matching the NumElements and ElementTy parameters
717 /// passed in. Note that this can return a ConstantAggregateZero object.
718 /// ElementTy needs to be one of i8/i16/i32/i64/float/double. Data is the
719 /// buffer containing the elements. Be careful to make sure Data uses the
720 /// right endianness, the buffer will be used as-is.
721 static Constant *getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy) {
722 Type *Ty = ArrayType::get(ElementTy, NumElements);
723 return getImpl(Data, Ty);
726 /// getFP() constructors - Return a constant with array type with an element
727 /// count and element type of float with precision matching the number of
728 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
729 /// double for 64bits) Note that this can return a ConstantAggregateZero
730 /// object.
731 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
732 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
733 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
735 /// This method constructs a CDS and initializes it with a text string.
736 /// The default behavior (AddNull==true) causes a null terminator to
737 /// be placed at the end of the array (increasing the length of the string by
738 /// one more than the StringRef would normally indicate. Pass AddNull=false
739 /// to disable this behavior.
740 static Constant *getString(LLVMContext &Context, StringRef Initializer,
741 bool AddNull = true);
743 /// Specialize the getType() method to always return an ArrayType,
744 /// which reduces the amount of casting needed in parts of the compiler.
745 inline ArrayType *getType() const {
746 return cast<ArrayType>(Value::getType());
749 /// Methods for support type inquiry through isa, cast, and dyn_cast:
750 static bool classof(const Value *V) {
751 return V->getValueID() == ConstantDataArrayVal;
755 //===----------------------------------------------------------------------===//
756 /// A vector constant whose element type is a simple 1/2/4/8-byte integer or
757 /// float/double, and whose elements are just simple data values
758 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
759 /// stores all of the elements of the constant as densely packed data, instead
760 /// of as Value*'s.
761 class ConstantDataVector final : public ConstantDataSequential {
762 friend class ConstantDataSequential;
764 explicit ConstantDataVector(Type *ty, const char *Data)
765 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
767 public:
768 ConstantDataVector(const ConstantDataVector &) = delete;
770 /// get() constructors - Return a constant with vector type with an element
771 /// count and element type matching the ArrayRef passed in. Note that this
772 /// can return a ConstantAggregateZero object.
773 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
774 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
775 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
776 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
777 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
778 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
780 /// getFP() constructors - Return a constant with vector type with an element
781 /// count and element type of float with the precision matching the number of
782 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
783 /// double for 64bits) Note that this can return a ConstantAggregateZero
784 /// object.
785 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
786 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
787 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
789 /// Return a ConstantVector with the specified constant in each element.
790 /// The specified constant has to be a of a compatible type (i8/i16/
791 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
792 static Constant *getSplat(unsigned NumElts, Constant *Elt);
794 /// Returns true if this is a splat constant, meaning that all elements have
795 /// the same value.
796 bool isSplat() const;
798 /// If this is a splat constant, meaning that all of the elements have the
799 /// same value, return that value. Otherwise return NULL.
800 Constant *getSplatValue() const;
802 /// Specialize the getType() method to always return a VectorType,
803 /// which reduces the amount of casting needed in parts of the compiler.
804 inline VectorType *getType() const {
805 return cast<VectorType>(Value::getType());
808 /// Methods for support type inquiry through isa, cast, and dyn_cast:
809 static bool classof(const Value *V) {
810 return V->getValueID() == ConstantDataVectorVal;
814 //===----------------------------------------------------------------------===//
815 /// A constant token which is empty
817 class ConstantTokenNone final : public ConstantData {
818 friend class Constant;
820 explicit ConstantTokenNone(LLVMContext &Context)
821 : ConstantData(Type::getTokenTy(Context), ConstantTokenNoneVal) {}
823 void destroyConstantImpl();
825 public:
826 ConstantTokenNone(const ConstantTokenNone &) = delete;
828 /// Return the ConstantTokenNone.
829 static ConstantTokenNone *get(LLVMContext &Context);
831 /// Methods to support type inquiry through isa, cast, and dyn_cast.
832 static bool classof(const Value *V) {
833 return V->getValueID() == ConstantTokenNoneVal;
837 /// The address of a basic block.
839 class BlockAddress final : public Constant {
840 friend class Constant;
842 BlockAddress(Function *F, BasicBlock *BB);
844 void *operator new(size_t s) { return User::operator new(s, 2); }
846 void destroyConstantImpl();
847 Value *handleOperandChangeImpl(Value *From, Value *To);
849 public:
850 /// Return a BlockAddress for the specified function and basic block.
851 static BlockAddress *get(Function *F, BasicBlock *BB);
853 /// Return a BlockAddress for the specified basic block. The basic
854 /// block must be embedded into a function.
855 static BlockAddress *get(BasicBlock *BB);
857 /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
859 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
860 static BlockAddress *lookup(const BasicBlock *BB);
862 /// Transparently provide more efficient getOperand methods.
863 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
865 Function *getFunction() const { return (Function*)Op<0>().get(); }
866 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
868 /// Methods for support type inquiry through isa, cast, and dyn_cast:
869 static bool classof(const Value *V) {
870 return V->getValueID() == BlockAddressVal;
874 template <>
875 struct OperandTraits<BlockAddress> :
876 public FixedNumOperandTraits<BlockAddress, 2> {
879 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
881 //===----------------------------------------------------------------------===//
882 /// A constant value that is initialized with an expression using
883 /// other constant values.
885 /// This class uses the standard Instruction opcodes to define the various
886 /// constant expressions. The Opcode field for the ConstantExpr class is
887 /// maintained in the Value::SubclassData field.
888 class ConstantExpr : public Constant {
889 friend struct ConstantExprKeyType;
890 friend class Constant;
892 void destroyConstantImpl();
893 Value *handleOperandChangeImpl(Value *From, Value *To);
895 protected:
896 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
897 : Constant(ty, ConstantExprVal, Ops, NumOps) {
898 // Operation type (an Instruction opcode) is stored as the SubclassData.
899 setValueSubclassData(Opcode);
902 public:
903 // Static methods to construct a ConstantExpr of different kinds. Note that
904 // these methods may return a object that is not an instance of the
905 // ConstantExpr class, because they will attempt to fold the constant
906 // expression into something simpler if possible.
908 /// getAlignOf constant expr - computes the alignment of a type in a target
909 /// independent way (Note: the return type is an i64).
910 static Constant *getAlignOf(Type *Ty);
912 /// getSizeOf constant expr - computes the (alloc) size of a type (in
913 /// address-units, not bits) in a target independent way (Note: the return
914 /// type is an i64).
916 static Constant *getSizeOf(Type *Ty);
918 /// getOffsetOf constant expr - computes the offset of a struct field in a
919 /// target independent way (Note: the return type is an i64).
921 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
923 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
924 /// which supports any aggregate type, and any Constant index.
926 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
928 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
929 static Constant *getFNeg(Constant *C);
930 static Constant *getNot(Constant *C);
931 static Constant *getAdd(Constant *C1, Constant *C2,
932 bool HasNUW = false, bool HasNSW = false);
933 static Constant *getFAdd(Constant *C1, Constant *C2);
934 static Constant *getSub(Constant *C1, Constant *C2,
935 bool HasNUW = false, bool HasNSW = false);
936 static Constant *getFSub(Constant *C1, Constant *C2);
937 static Constant *getMul(Constant *C1, Constant *C2,
938 bool HasNUW = false, bool HasNSW = false);
939 static Constant *getFMul(Constant *C1, Constant *C2);
940 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
941 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
942 static Constant *getFDiv(Constant *C1, Constant *C2);
943 static Constant *getURem(Constant *C1, Constant *C2);
944 static Constant *getSRem(Constant *C1, Constant *C2);
945 static Constant *getFRem(Constant *C1, Constant *C2);
946 static Constant *getAnd(Constant *C1, Constant *C2);
947 static Constant *getOr(Constant *C1, Constant *C2);
948 static Constant *getXor(Constant *C1, Constant *C2);
949 static Constant *getShl(Constant *C1, Constant *C2,
950 bool HasNUW = false, bool HasNSW = false);
951 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
952 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
953 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
954 static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
955 static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
956 static Constant *getFPTrunc(Constant *C, Type *Ty,
957 bool OnlyIfReduced = false);
958 static Constant *getFPExtend(Constant *C, Type *Ty,
959 bool OnlyIfReduced = false);
960 static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
961 static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
962 static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
963 static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
964 static Constant *getPtrToInt(Constant *C, Type *Ty,
965 bool OnlyIfReduced = false);
966 static Constant *getIntToPtr(Constant *C, Type *Ty,
967 bool OnlyIfReduced = false);
968 static Constant *getBitCast(Constant *C, Type *Ty,
969 bool OnlyIfReduced = false);
970 static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
971 bool OnlyIfReduced = false);
973 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
974 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
976 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
977 return getAdd(C1, C2, false, true);
980 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
981 return getAdd(C1, C2, true, false);
984 static Constant *getNSWSub(Constant *C1, Constant *C2) {
985 return getSub(C1, C2, false, true);
988 static Constant *getNUWSub(Constant *C1, Constant *C2) {
989 return getSub(C1, C2, true, false);
992 static Constant *getNSWMul(Constant *C1, Constant *C2) {
993 return getMul(C1, C2, false, true);
996 static Constant *getNUWMul(Constant *C1, Constant *C2) {
997 return getMul(C1, C2, true, false);
1000 static Constant *getNSWShl(Constant *C1, Constant *C2) {
1001 return getShl(C1, C2, false, true);
1004 static Constant *getNUWShl(Constant *C1, Constant *C2) {
1005 return getShl(C1, C2, true, false);
1008 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
1009 return getSDiv(C1, C2, true);
1012 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
1013 return getUDiv(C1, C2, true);
1016 static Constant *getExactAShr(Constant *C1, Constant *C2) {
1017 return getAShr(C1, C2, true);
1020 static Constant *getExactLShr(Constant *C1, Constant *C2) {
1021 return getLShr(C1, C2, true);
1024 /// Return the identity constant for a binary opcode.
1025 /// The identity constant C is defined as X op C = X and C op X = X for every
1026 /// X when the binary operation is commutative. If the binop is not
1027 /// commutative, callers can acquire the operand 1 identity constant by
1028 /// setting AllowRHSConstant to true. For example, any shift has a zero
1029 /// identity constant for operand 1: X shift 0 = X.
1030 /// Return nullptr if the operator does not have an identity constant.
1031 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty,
1032 bool AllowRHSConstant = false);
1034 /// Return the absorbing element for the given binary
1035 /// operation, i.e. a constant C such that X op C = C and C op X = C for
1036 /// every X. For example, this returns zero for integer multiplication.
1037 /// It returns null if the operator doesn't have an absorbing element.
1038 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
1040 /// Transparently provide more efficient getOperand methods.
1041 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
1043 /// Convenience function for getting a Cast operation.
1045 /// \param ops The opcode for the conversion
1046 /// \param C The constant to be converted
1047 /// \param Ty The type to which the constant is converted
1048 /// \param OnlyIfReduced see \a getWithOperands() docs.
1049 static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
1050 bool OnlyIfReduced = false);
1052 // Create a ZExt or BitCast cast constant expression
1053 static Constant *getZExtOrBitCast(
1054 Constant *C, ///< The constant to zext or bitcast
1055 Type *Ty ///< The type to zext or bitcast C to
1058 // Create a SExt or BitCast cast constant expression
1059 static Constant *getSExtOrBitCast(
1060 Constant *C, ///< The constant to sext or bitcast
1061 Type *Ty ///< The type to sext or bitcast C to
1064 // Create a Trunc or BitCast cast constant expression
1065 static Constant *getTruncOrBitCast(
1066 Constant *C, ///< The constant to trunc or bitcast
1067 Type *Ty ///< The type to trunc or bitcast C to
1070 /// Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
1071 /// expression.
1072 static Constant *getPointerCast(
1073 Constant *C, ///< The pointer value to be casted (operand 0)
1074 Type *Ty ///< The type to which cast should be made
1077 /// Create a BitCast or AddrSpaceCast for a pointer type depending on
1078 /// the address space.
1079 static Constant *getPointerBitCastOrAddrSpaceCast(
1080 Constant *C, ///< The constant to addrspacecast or bitcast
1081 Type *Ty ///< The type to bitcast or addrspacecast C to
1084 /// Create a ZExt, Bitcast or Trunc for integer -> integer casts
1085 static Constant *getIntegerCast(
1086 Constant *C, ///< The integer constant to be casted
1087 Type *Ty, ///< The integer type to cast to
1088 bool isSigned ///< Whether C should be treated as signed or not
1091 /// Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1092 static Constant *getFPCast(
1093 Constant *C, ///< The integer constant to be casted
1094 Type *Ty ///< The integer type to cast to
1097 /// Return true if this is a convert constant expression
1098 bool isCast() const;
1100 /// Return true if this is a compare constant expression
1101 bool isCompare() const;
1103 /// Return true if this is an insertvalue or extractvalue expression,
1104 /// and the getIndices() method may be used.
1105 bool hasIndices() const;
1107 /// Return true if this is a getelementptr expression and all
1108 /// the index operands are compile-time known integers within the
1109 /// corresponding notional static array extents. Note that this is
1110 /// not equivalant to, a subset of, or a superset of the "inbounds"
1111 /// property.
1112 bool isGEPWithNoNotionalOverIndexing() const;
1114 /// Select constant expr
1116 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1117 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1118 Type *OnlyIfReducedTy = nullptr);
1120 /// get - Return a unary operator constant expression,
1121 /// folding if possible.
1123 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1124 static Constant *get(unsigned Opcode, Constant *C1, unsigned Flags = 0,
1125 Type *OnlyIfReducedTy = nullptr);
1127 /// get - Return a binary or shift operator constant expression,
1128 /// folding if possible.
1130 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1131 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1132 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1134 /// Return an ICmp or FCmp comparison operator constant expression.
1136 /// \param OnlyIfReduced see \a getWithOperands() docs.
1137 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1138 bool OnlyIfReduced = false);
1140 /// get* - Return some common constants without having to
1141 /// specify the full Instruction::OPCODE identifier.
1143 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1144 bool OnlyIfReduced = false);
1145 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1146 bool OnlyIfReduced = false);
1148 /// Getelementptr form. Value* is only accepted for convenience;
1149 /// all elements must be Constants.
1151 /// \param InRangeIndex the inrange index if present or None.
1152 /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1153 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1154 ArrayRef<Constant *> IdxList,
1155 bool InBounds = false,
1156 Optional<unsigned> InRangeIndex = None,
1157 Type *OnlyIfReducedTy = nullptr) {
1158 return getGetElementPtr(
1159 Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1160 InBounds, InRangeIndex, OnlyIfReducedTy);
1162 static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
1163 bool InBounds = false,
1164 Optional<unsigned> InRangeIndex = None,
1165 Type *OnlyIfReducedTy = nullptr) {
1166 // This form of the function only exists to avoid ambiguous overload
1167 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1168 // ArrayRef<Value *>.
1169 return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, InRangeIndex,
1170 OnlyIfReducedTy);
1172 static Constant *getGetElementPtr(Type *Ty, Constant *C,
1173 ArrayRef<Value *> IdxList,
1174 bool InBounds = false,
1175 Optional<unsigned> InRangeIndex = None,
1176 Type *OnlyIfReducedTy = nullptr);
1178 /// Create an "inbounds" getelementptr. See the documentation for the
1179 /// "inbounds" flag in LangRef.html for details.
1180 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1181 ArrayRef<Constant *> IdxList) {
1182 return getGetElementPtr(Ty, C, IdxList, true);
1184 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1185 Constant *Idx) {
1186 // This form of the function only exists to avoid ambiguous overload
1187 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1188 // ArrayRef<Value *>.
1189 return getGetElementPtr(Ty, C, Idx, true);
1191 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1192 ArrayRef<Value *> IdxList) {
1193 return getGetElementPtr(Ty, C, IdxList, true);
1196 static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1197 Type *OnlyIfReducedTy = nullptr);
1198 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1199 Type *OnlyIfReducedTy = nullptr);
1200 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1201 Type *OnlyIfReducedTy = nullptr);
1202 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1203 Type *OnlyIfReducedTy = nullptr);
1204 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1205 ArrayRef<unsigned> Idxs,
1206 Type *OnlyIfReducedTy = nullptr);
1208 /// Return the opcode at the root of this constant expression
1209 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1211 /// Return the ICMP or FCMP predicate value. Assert if this is not an ICMP or
1212 /// FCMP constant expression.
1213 unsigned getPredicate() const;
1215 /// Assert that this is an insertvalue or exactvalue
1216 /// expression and return the list of indices.
1217 ArrayRef<unsigned> getIndices() const;
1219 /// Return a string representation for an opcode.
1220 const char *getOpcodeName() const;
1222 /// Return a constant expression identical to this one, but with the specified
1223 /// operand set to the specified value.
1224 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1226 /// This returns the current constant expression with the operands replaced
1227 /// with the specified values. The specified array must have the same number
1228 /// of operands as our current one.
1229 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1230 return getWithOperands(Ops, getType());
1233 /// Get the current expression with the operands replaced.
1235 /// Return the current constant expression with the operands replaced with \c
1236 /// Ops and the type with \c Ty. The new operands must have the same number
1237 /// as the current ones.
1239 /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1240 /// gets constant-folded, the type changes, or the expression is otherwise
1241 /// canonicalized. This parameter should almost always be \c false.
1242 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1243 bool OnlyIfReduced = false,
1244 Type *SrcTy = nullptr) const;
1246 /// Returns an Instruction which implements the same operation as this
1247 /// ConstantExpr. The instruction is not linked to any basic block.
1249 /// A better approach to this could be to have a constructor for Instruction
1250 /// which would take a ConstantExpr parameter, but that would have spread
1251 /// implementation details of ConstantExpr outside of Constants.cpp, which
1252 /// would make it harder to remove ConstantExprs altogether.
1253 Instruction *getAsInstruction();
1255 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1256 static bool classof(const Value *V) {
1257 return V->getValueID() == ConstantExprVal;
1260 private:
1261 // Shadow Value::setValueSubclassData with a private forwarding method so that
1262 // subclasses cannot accidentally use it.
1263 void setValueSubclassData(unsigned short D) {
1264 Value::setValueSubclassData(D);
1268 template <>
1269 struct OperandTraits<ConstantExpr> :
1270 public VariadicOperandTraits<ConstantExpr, 1> {
1273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1275 //===----------------------------------------------------------------------===//
1276 /// 'undef' values are things that do not have specified contents.
1277 /// These are used for a variety of purposes, including global variable
1278 /// initializers and operands to instructions. 'undef' values can occur with
1279 /// any first-class type.
1281 /// Undef values aren't exactly constants; if they have multiple uses, they
1282 /// can appear to have different bit patterns at each use. See
1283 /// LangRef.html#undefvalues for details.
1285 class UndefValue final : public ConstantData {
1286 friend class Constant;
1288 explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {}
1290 void destroyConstantImpl();
1292 public:
1293 UndefValue(const UndefValue &) = delete;
1295 /// Static factory methods - Return an 'undef' object of the specified type.
1296 static UndefValue *get(Type *T);
1298 /// If this Undef has array or vector type, return a undef with the right
1299 /// element type.
1300 UndefValue *getSequentialElement() const;
1302 /// If this undef has struct type, return a undef with the right element type
1303 /// for the specified element.
1304 UndefValue *getStructElement(unsigned Elt) const;
1306 /// Return an undef of the right value for the specified GEP index if we can,
1307 /// otherwise return null (e.g. if C is a ConstantExpr).
1308 UndefValue *getElementValue(Constant *C) const;
1310 /// Return an undef of the right value for the specified GEP index.
1311 UndefValue *getElementValue(unsigned Idx) const;
1313 /// Return the number of elements in the array, vector, or struct.
1314 unsigned getNumElements() const;
1316 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1317 static bool classof(const Value *V) {
1318 return V->getValueID() == UndefValueVal;
1322 } // end namespace llvm
1324 #endif // LLVM_IR_CONSTANTS_H