Home | History | Annotate | Download | only in IR
      1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 /// @file
     11 /// This file contains the declarations for the subclasses of Constant,
     12 /// which represent the different flavors of constant values that live in LLVM.
     13 /// Note that Constants are immutable (once created they never change) and are
     14 /// fully shared by structural equivalence.  This means that two structurally
     15 /// equivalent constants will always have the same address.  Constants are
     16 /// created on demand as needed and never deleted: thus clients don't have to
     17 /// worry about the lifetime of the objects.
     18 //
     19 //===----------------------------------------------------------------------===//
     20 
     21 #ifndef LLVM_IR_CONSTANTS_H
     22 #define LLVM_IR_CONSTANTS_H
     23 
     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"
     30 
     31 namespace llvm {
     32 
     33 class ArrayType;
     34 class IntegerType;
     35 class StructType;
     36 class PointerType;
     37 class VectorType;
     38 class SequentialType;
     39 
     40 struct ConstantExprKeyType;
     41 template <class ConstantClass> struct ConstantAggrKeyType;
     42 
     43 //===----------------------------------------------------------------------===//
     44 /// This is the shared class of boolean and integer constants. This class
     45 /// represents both boolean and integral constants.
     46 /// @brief Class for constant integers.
     47 class ConstantInt : public Constant {
     48   void anchor() override;
     49   void *operator new(size_t, unsigned) = delete;
     50   ConstantInt(const ConstantInt &) = delete;
     51   ConstantInt(IntegerType *Ty, const APInt& V);
     52   APInt Val;
     53 
     54   friend class Constant;
     55   void destroyConstantImpl();
     56   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
     57 
     58 protected:
     59   // allocate space for exactly zero operands
     60   void *operator new(size_t s) {
     61     return User::operator new(s, 0);
     62   }
     63 public:
     64   static ConstantInt *getTrue(LLVMContext &Context);
     65   static ConstantInt *getFalse(LLVMContext &Context);
     66   static Constant *getTrue(Type *Ty);
     67   static Constant *getFalse(Type *Ty);
     68 
     69   /// If Ty is a vector type, return a Constant with a splat of the given
     70   /// value. Otherwise return a ConstantInt for the given value.
     71   static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
     72 
     73   /// Return a ConstantInt with the specified integer value for the specified
     74   /// type. If the type is wider than 64 bits, the value will be zero-extended
     75   /// to fit the type, unless isSigned is true, in which case the value will
     76   /// be interpreted as a 64-bit signed integer and sign-extended to fit
     77   /// the type.
     78   /// @brief Get a ConstantInt for a specific value.
     79   static ConstantInt *get(IntegerType *Ty, uint64_t V,
     80                           bool isSigned = false);
     81 
     82   /// Return a ConstantInt with the specified value for the specified type. The
     83   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
     84   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
     85   /// signed value for the type Ty.
     86   /// @brief Get a ConstantInt for a specific signed value.
     87   static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
     88   static Constant *getSigned(Type *Ty, int64_t V);
     89 
     90   /// Return a ConstantInt with the specified value and an implied Type. The
     91   /// type is the integer type that corresponds to the bit width of the value.
     92   static ConstantInt *get(LLVMContext &Context, const APInt &V);
     93 
     94   /// Return a ConstantInt constructed from the string strStart with the given
     95   /// radix.
     96   static ConstantInt *get(IntegerType *Ty, StringRef Str,
     97                           uint8_t radix);
     98 
     99   /// If Ty is a vector type, return a Constant with a splat of the given
    100   /// value. Otherwise return a ConstantInt for the given value.
    101   static Constant *get(Type* Ty, const APInt& V);
    102 
    103   /// Return the constant as an APInt value reference. This allows clients to
    104   /// obtain a copy of the value, with all its precision in tact.
    105   /// @brief Return the constant's value.
    106   inline const APInt &getValue() const {
    107     return Val;
    108   }
    109 
    110   /// getBitWidth - Return the bitwidth of this constant.
    111   unsigned getBitWidth() const { return Val.getBitWidth(); }
    112 
    113   /// Return the constant as a 64-bit unsigned integer value after it
    114   /// has been zero extended as appropriate for the type of this constant. Note
    115   /// that this method can assert if the value does not fit in 64 bits.
    116   /// @brief Return the zero extended value.
    117   inline uint64_t getZExtValue() const {
    118     return Val.getZExtValue();
    119   }
    120 
    121   /// Return the constant as a 64-bit integer value after it has been sign
    122   /// extended as appropriate for the type of this constant. Note that
    123   /// this method can assert if the value does not fit in 64 bits.
    124   /// @brief Return the sign extended value.
    125   inline int64_t getSExtValue() const {
    126     return Val.getSExtValue();
    127   }
    128 
    129   /// A helper method that can be used to determine if the constant contained
    130   /// within is equal to a constant.  This only works for very small values,
    131   /// because this is all that can be represented with all types.
    132   /// @brief Determine if this constant's value is same as an unsigned char.
    133   bool equalsInt(uint64_t V) const {
    134     return Val == V;
    135   }
    136 
    137   /// getType - Specialize the getType() method to always return an IntegerType,
    138   /// which reduces the amount of casting needed in parts of the compiler.
    139   ///
    140   inline IntegerType *getType() const {
    141     return cast<IntegerType>(Value::getType());
    142   }
    143 
    144   /// This static method returns true if the type Ty is big enough to
    145   /// represent the value V. This can be used to avoid having the get method
    146   /// assert when V is larger than Ty can represent. Note that there are two
    147   /// versions of this method, one for unsigned and one for signed integers.
    148   /// Although ConstantInt canonicalizes everything to an unsigned integer,
    149   /// the signed version avoids callers having to convert a signed quantity
    150   /// to the appropriate unsigned type before calling the method.
    151   /// @returns true if V is a valid value for type Ty
    152   /// @brief Determine if the value is in range for the given type.
    153   static bool isValueValidForType(Type *Ty, uint64_t V);
    154   static bool isValueValidForType(Type *Ty, int64_t V);
    155 
    156   bool isNegative() const { return Val.isNegative(); }
    157 
    158   /// This is just a convenience method to make client code smaller for a
    159   /// common code. It also correctly performs the comparison without the
    160   /// potential for an assertion from getZExtValue().
    161   bool isZero() const {
    162     return Val == 0;
    163   }
    164 
    165   /// This is just a convenience method to make client code smaller for a
    166   /// common case. It also correctly performs the comparison without the
    167   /// potential for an assertion from getZExtValue().
    168   /// @brief Determine if the value is one.
    169   bool isOne() const {
    170     return Val == 1;
    171   }
    172 
    173   /// This function will return true iff every bit in this constant is set
    174   /// to true.
    175   /// @returns true iff this constant's bits are all set to true.
    176   /// @brief Determine if the value is all ones.
    177   bool isMinusOne() const {
    178     return Val.isAllOnesValue();
    179   }
    180 
    181   /// This function will return true iff this constant represents the largest
    182   /// value that may be represented by the constant's type.
    183   /// @returns true iff this is the largest value that may be represented
    184   /// by this type.
    185   /// @brief Determine if the value is maximal.
    186   bool isMaxValue(bool isSigned) const {
    187     if (isSigned)
    188       return Val.isMaxSignedValue();
    189     else
    190       return Val.isMaxValue();
    191   }
    192 
    193   /// This function will return true iff this constant represents the smallest
    194   /// value that may be represented by this constant's type.
    195   /// @returns true if this is the smallest value that may be represented by
    196   /// this type.
    197   /// @brief Determine if the value is minimal.
    198   bool isMinValue(bool isSigned) const {
    199     if (isSigned)
    200       return Val.isMinSignedValue();
    201     else
    202       return Val.isMinValue();
    203   }
    204 
    205   /// This function will return true iff this constant represents a value with
    206   /// active bits bigger than 64 bits or a value greater than the given uint64_t
    207   /// value.
    208   /// @returns true iff this constant is greater or equal to the given number.
    209   /// @brief Determine if the value is greater or equal to the given number.
    210   bool uge(uint64_t Num) const {
    211     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
    212   }
    213 
    214   /// getLimitedValue - If the value is smaller than the specified limit,
    215   /// return it, otherwise return the limit value.  This causes the value
    216   /// to saturate to the limit.
    217   /// @returns the min of the value of the constant and the specified value
    218   /// @brief Get the constant's value with a saturation limit
    219   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
    220     return Val.getLimitedValue(Limit);
    221   }
    222 
    223   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
    224   static bool classof(const Value *V) {
    225     return V->getValueID() == ConstantIntVal;
    226   }
    227 };
    228 
    229 
    230 //===----------------------------------------------------------------------===//
    231 /// ConstantFP - Floating Point Values [float, double]
    232 ///
    233 class ConstantFP : public Constant {
    234   APFloat Val;
    235   void anchor() override;
    236   void *operator new(size_t, unsigned) = delete;
    237   ConstantFP(const ConstantFP &) = delete;
    238   friend class LLVMContextImpl;
    239 
    240   friend class Constant;
    241   void destroyConstantImpl();
    242   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    243 
    244 protected:
    245   ConstantFP(Type *Ty, const APFloat& V);
    246 protected:
    247   // allocate space for exactly zero operands
    248   void *operator new(size_t s) {
    249     return User::operator new(s, 0);
    250   }
    251 public:
    252   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
    253   /// method returns the negative zero constant for floating point or vector
    254   /// floating point types; for all other types, it returns the null value.
    255   static Constant *getZeroValueForNegation(Type *Ty);
    256 
    257   /// get() - This returns a ConstantFP, or a vector containing a splat of a
    258   /// ConstantFP, for the specified value in the specified type.  This should
    259   /// only be used for simple constant values like 2.0/1.0 etc, that are
    260   /// known-valid both as host double and as the target format.
    261   static Constant *get(Type* Ty, double V);
    262   static Constant *get(Type* Ty, StringRef Str);
    263   static ConstantFP *get(LLVMContext &Context, const APFloat &V);
    264   static Constant *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
    265   static Constant *getNegativeZero(Type *Ty);
    266   static Constant *getInfinity(Type *Ty, bool Negative = false);
    267 
    268   /// isValueValidForType - return true if Ty is big enough to represent V.
    269   static bool isValueValidForType(Type *Ty, const APFloat &V);
    270   inline const APFloat &getValueAPF() const { return Val; }
    271 
    272   /// isZero - Return true if the value is positive or negative zero.
    273   bool isZero() const { return Val.isZero(); }
    274 
    275   /// isNegative - Return true if the sign bit is set.
    276   bool isNegative() const { return Val.isNegative(); }
    277 
    278   /// isInfinity - Return true if the value is infinity
    279   bool isInfinity() const { return Val.isInfinity(); }
    280 
    281   /// isNaN - Return true if the value is a NaN.
    282   bool isNaN() const { return Val.isNaN(); }
    283 
    284   /// isExactlyValue - We don't rely on operator== working on double values, as
    285   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
    286   /// As such, this method can be used to do an exact bit-for-bit comparison of
    287   /// two floating point values.  The version with a double operand is retained
    288   /// because it's so convenient to write isExactlyValue(2.0), but please use
    289   /// it only for simple constants.
    290   bool isExactlyValue(const APFloat &V) const;
    291 
    292   bool isExactlyValue(double V) const {
    293     bool ignored;
    294     APFloat FV(V);
    295     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
    296     return isExactlyValue(FV);
    297   }
    298   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    299   static bool classof(const Value *V) {
    300     return V->getValueID() == ConstantFPVal;
    301   }
    302 };
    303 
    304 //===----------------------------------------------------------------------===//
    305 /// ConstantAggregateZero - All zero aggregate value
    306 ///
    307 class ConstantAggregateZero : public Constant {
    308   void *operator new(size_t, unsigned) = delete;
    309   ConstantAggregateZero(const ConstantAggregateZero &) = delete;
    310 
    311   friend class Constant;
    312   void destroyConstantImpl();
    313   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    314 
    315 protected:
    316   explicit ConstantAggregateZero(Type *ty)
    317     : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
    318 protected:
    319   // allocate space for exactly zero operands
    320   void *operator new(size_t s) {
    321     return User::operator new(s, 0);
    322   }
    323 public:
    324   static ConstantAggregateZero *get(Type *Ty);
    325 
    326   /// getSequentialElement - If this CAZ has array or vector type, return a zero
    327   /// with the right element type.
    328   Constant *getSequentialElement() const;
    329 
    330   /// getStructElement - If this CAZ has struct type, return a zero with the
    331   /// right element type for the specified element.
    332   Constant *getStructElement(unsigned Elt) const;
    333 
    334   /// getElementValue - Return a zero of the right value for the specified GEP
    335   /// index.
    336   Constant *getElementValue(Constant *C) const;
    337 
    338   /// getElementValue - Return a zero of the right value for the specified GEP
    339   /// index.
    340   Constant *getElementValue(unsigned Idx) const;
    341 
    342   /// \brief Return the number of elements in the array, vector, or struct.
    343   unsigned getNumElements() const;
    344 
    345   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    346   ///
    347   static bool classof(const Value *V) {
    348     return V->getValueID() == ConstantAggregateZeroVal;
    349   }
    350 };
    351 
    352 
    353 //===----------------------------------------------------------------------===//
    354 /// ConstantArray - Constant Array Declarations
    355 ///
    356 class ConstantArray : public Constant {
    357   friend struct ConstantAggrKeyType<ConstantArray>;
    358   ConstantArray(const ConstantArray &) = delete;
    359 
    360   friend class Constant;
    361   void destroyConstantImpl();
    362   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    363 
    364 protected:
    365   ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
    366 public:
    367   // ConstantArray accessors
    368   static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
    369 
    370 private:
    371   static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
    372 
    373 public:
    374   /// Transparently provide more efficient getOperand methods.
    375   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    376 
    377   /// getType - Specialize the getType() method to always return an ArrayType,
    378   /// which reduces the amount of casting needed in parts of the compiler.
    379   ///
    380   inline ArrayType *getType() const {
    381     return cast<ArrayType>(Value::getType());
    382   }
    383 
    384   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    385   static bool classof(const Value *V) {
    386     return V->getValueID() == ConstantArrayVal;
    387   }
    388 };
    389 
    390 template <>
    391 struct OperandTraits<ConstantArray> :
    392   public VariadicOperandTraits<ConstantArray> {
    393 };
    394 
    395 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
    396 
    397 //===----------------------------------------------------------------------===//
    398 // ConstantStruct - Constant Struct Declarations
    399 //
    400 class ConstantStruct : public Constant {
    401   friend struct ConstantAggrKeyType<ConstantStruct>;
    402   ConstantStruct(const ConstantStruct &) = delete;
    403 
    404   friend class Constant;
    405   void destroyConstantImpl();
    406   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    407 
    408 protected:
    409   ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
    410 public:
    411   // ConstantStruct accessors
    412   static Constant *get(StructType *T, ArrayRef<Constant*> V);
    413   static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
    414 
    415   /// getAnon - Return an anonymous struct that has the specified
    416   /// elements.  If the struct is possibly empty, then you must specify a
    417   /// context.
    418   static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
    419     return get(getTypeForElements(V, Packed), V);
    420   }
    421   static Constant *getAnon(LLVMContext &Ctx,
    422                            ArrayRef<Constant*> V, bool Packed = false) {
    423     return get(getTypeForElements(Ctx, V, Packed), V);
    424   }
    425 
    426   /// getTypeForElements - Return an anonymous struct type to use for a constant
    427   /// with the specified set of elements.  The list must not be empty.
    428   static StructType *getTypeForElements(ArrayRef<Constant*> V,
    429                                         bool Packed = false);
    430   /// getTypeForElements - This version of the method allows an empty list.
    431   static StructType *getTypeForElements(LLVMContext &Ctx,
    432                                         ArrayRef<Constant*> V,
    433                                         bool Packed = false);
    434 
    435   /// Transparently provide more efficient getOperand methods.
    436   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    437 
    438   /// getType() specialization - Reduce amount of casting...
    439   ///
    440   inline StructType *getType() const {
    441     return cast<StructType>(Value::getType());
    442   }
    443 
    444   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    445   static bool classof(const Value *V) {
    446     return V->getValueID() == ConstantStructVal;
    447   }
    448 };
    449 
    450 template <>
    451 struct OperandTraits<ConstantStruct> :
    452   public VariadicOperandTraits<ConstantStruct> {
    453 };
    454 
    455 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
    456 
    457 
    458 //===----------------------------------------------------------------------===//
    459 /// ConstantVector - Constant Vector Declarations
    460 ///
    461 class ConstantVector : public Constant {
    462   friend struct ConstantAggrKeyType<ConstantVector>;
    463   ConstantVector(const ConstantVector &) = delete;
    464 
    465   friend class Constant;
    466   void destroyConstantImpl();
    467   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    468 
    469 protected:
    470   ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
    471 public:
    472   // ConstantVector accessors
    473   static Constant *get(ArrayRef<Constant*> V);
    474 
    475 private:
    476   static Constant *getImpl(ArrayRef<Constant *> V);
    477 
    478 public:
    479   /// getSplat - Return a ConstantVector with the specified constant in each
    480   /// element.
    481   static Constant *getSplat(unsigned NumElts, Constant *Elt);
    482 
    483   /// Transparently provide more efficient getOperand methods.
    484   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    485 
    486   /// getType - Specialize the getType() method to always return a VectorType,
    487   /// which reduces the amount of casting needed in parts of the compiler.
    488   ///
    489   inline VectorType *getType() const {
    490     return cast<VectorType>(Value::getType());
    491   }
    492 
    493   /// getSplatValue - If this is a splat constant, meaning that all of the
    494   /// elements have the same value, return that value. Otherwise return NULL.
    495   Constant *getSplatValue() const;
    496 
    497   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    498   static bool classof(const Value *V) {
    499     return V->getValueID() == ConstantVectorVal;
    500   }
    501 };
    502 
    503 template <>
    504 struct OperandTraits<ConstantVector> :
    505   public VariadicOperandTraits<ConstantVector> {
    506 };
    507 
    508 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
    509 
    510 //===----------------------------------------------------------------------===//
    511 /// ConstantPointerNull - a constant pointer value that points to null
    512 ///
    513 class ConstantPointerNull : public Constant {
    514   void *operator new(size_t, unsigned) = delete;
    515   ConstantPointerNull(const ConstantPointerNull &) = delete;
    516 
    517   friend class Constant;
    518   void destroyConstantImpl();
    519   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    520 
    521 protected:
    522   explicit ConstantPointerNull(PointerType *T)
    523     : Constant(T,
    524                Value::ConstantPointerNullVal, nullptr, 0) {}
    525 
    526 protected:
    527   // allocate space for exactly zero operands
    528   void *operator new(size_t s) {
    529     return User::operator new(s, 0);
    530   }
    531 public:
    532   /// get() - Static factory methods - Return objects of the specified value
    533   static ConstantPointerNull *get(PointerType *T);
    534 
    535   /// getType - Specialize the getType() method to always return an PointerType,
    536   /// which reduces the amount of casting needed in parts of the compiler.
    537   ///
    538   inline PointerType *getType() const {
    539     return cast<PointerType>(Value::getType());
    540   }
    541 
    542   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    543   static bool classof(const Value *V) {
    544     return V->getValueID() == ConstantPointerNullVal;
    545   }
    546 };
    547 
    548 //===----------------------------------------------------------------------===//
    549 /// ConstantDataSequential - A vector or array constant whose element type is a
    550 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
    551 /// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
    552 /// operands because it stores all of the elements of the constant as densely
    553 /// packed data, instead of as Value*'s.
    554 ///
    555 /// This is the common base class of ConstantDataArray and ConstantDataVector.
    556 ///
    557 class ConstantDataSequential : public Constant {
    558   friend class LLVMContextImpl;
    559   /// DataElements - A pointer to the bytes underlying this constant (which is
    560   /// owned by the uniquing StringMap).
    561   const char *DataElements;
    562 
    563   /// Next - This forms a link list of ConstantDataSequential nodes that have
    564   /// the same value but different type.  For example, 0,0,0,1 could be a 4
    565   /// element array of i8, or a 1-element array of i32.  They'll both end up in
    566   /// the same StringMap bucket, linked up.
    567   ConstantDataSequential *Next;
    568   void *operator new(size_t, unsigned) = delete;
    569   ConstantDataSequential(const ConstantDataSequential &) = delete;
    570 
    571   friend class Constant;
    572   void destroyConstantImpl();
    573   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    574 
    575 protected:
    576   explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
    577     : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
    578   ~ConstantDataSequential() override { delete Next; }
    579 
    580   static Constant *getImpl(StringRef Bytes, Type *Ty);
    581 
    582 protected:
    583   // allocate space for exactly zero operands.
    584   void *operator new(size_t s) {
    585     return User::operator new(s, 0);
    586   }
    587 public:
    588 
    589   /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
    590   /// formed with a vector or array of the specified element type.
    591   /// ConstantDataArray only works with normal float and int types that are
    592   /// stored densely in memory, not with things like i42 or x86_f80.
    593   static bool isElementTypeCompatible(Type *Ty);
    594 
    595   /// getElementAsInteger - If this is a sequential container of integers (of
    596   /// any size), return the specified element in the low bits of a uint64_t.
    597   uint64_t getElementAsInteger(unsigned i) const;
    598 
    599   /// getElementAsAPFloat - If this is a sequential container of floating point
    600   /// type, return the specified element as an APFloat.
    601   APFloat getElementAsAPFloat(unsigned i) const;
    602 
    603   /// getElementAsFloat - If this is an sequential container of floats, return
    604   /// the specified element as a float.
    605   float getElementAsFloat(unsigned i) const;
    606 
    607   /// getElementAsDouble - If this is an sequential container of doubles, return
    608   /// the specified element as a double.
    609   double getElementAsDouble(unsigned i) const;
    610 
    611   /// getElementAsConstant - Return a Constant for a specified index's element.
    612   /// Note that this has to compute a new constant to return, so it isn't as
    613   /// efficient as getElementAsInteger/Float/Double.
    614   Constant *getElementAsConstant(unsigned i) const;
    615 
    616   /// getType - Specialize the getType() method to always return a
    617   /// SequentialType, which reduces the amount of casting needed in parts of the
    618   /// compiler.
    619   inline SequentialType *getType() const {
    620     return cast<SequentialType>(Value::getType());
    621   }
    622 
    623   /// getElementType - Return the element type of the array/vector.
    624   Type *getElementType() const;
    625 
    626   /// getNumElements - Return the number of elements in the array or vector.
    627   unsigned getNumElements() const;
    628 
    629   /// getElementByteSize - Return the size (in bytes) of each element in the
    630   /// array/vector.  The size of the elements is known to be a multiple of one
    631   /// byte.
    632   uint64_t getElementByteSize() const;
    633 
    634 
    635   /// isString - This method returns true if this is an array of i8.
    636   bool isString() const;
    637 
    638   /// isCString - This method returns true if the array "isString", ends with a
    639   /// nul byte, and does not contains any other nul bytes.
    640   bool isCString() const;
    641 
    642   /// getAsString - If this array is isString(), then this method returns the
    643   /// array as a StringRef.  Otherwise, it asserts out.
    644   ///
    645   StringRef getAsString() const {
    646     assert(isString() && "Not a string");
    647     return getRawDataValues();
    648   }
    649 
    650   /// getAsCString - If this array is isCString(), then this method returns the
    651   /// array (without the trailing null byte) as a StringRef. Otherwise, it
    652   /// asserts out.
    653   ///
    654   StringRef getAsCString() const {
    655     assert(isCString() && "Isn't a C string");
    656     StringRef Str = getAsString();
    657     return Str.substr(0, Str.size()-1);
    658   }
    659 
    660   /// getRawDataValues - Return the raw, underlying, bytes of this data.  Note
    661   /// that this is an extremely tricky thing to work with, as it exposes the
    662   /// host endianness of the data elements.
    663   StringRef getRawDataValues() const;
    664 
    665   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    666   ///
    667   static bool classof(const Value *V) {
    668     return V->getValueID() == ConstantDataArrayVal ||
    669            V->getValueID() == ConstantDataVectorVal;
    670   }
    671 private:
    672   const char *getElementPointer(unsigned Elt) const;
    673 };
    674 
    675 //===----------------------------------------------------------------------===//
    676 /// ConstantDataArray - An array constant whose element type is a simple
    677 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
    678 /// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
    679 /// operands because it stores all of the elements of the constant as densely
    680 /// packed data, instead of as Value*'s.
    681 class ConstantDataArray : public ConstantDataSequential {
    682   void *operator new(size_t, unsigned) = delete;
    683   ConstantDataArray(const ConstantDataArray &) = delete;
    684   void anchor() override;
    685   friend class ConstantDataSequential;
    686   explicit ConstantDataArray(Type *ty, const char *Data)
    687     : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
    688 protected:
    689   // allocate space for exactly zero operands.
    690   void *operator new(size_t s) {
    691     return User::operator new(s, 0);
    692   }
    693 public:
    694 
    695   /// get() constructors - Return a constant with array type with an element
    696   /// count and element type matching the ArrayRef passed in.  Note that this
    697   /// can return a ConstantAggregateZero object.
    698   static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
    699   static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
    700   static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
    701   static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
    702   static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
    703   static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
    704 
    705   /// getFP() constructors - Return a constant with array type with an element
    706   /// count and element type of float with precision matching the number of
    707   /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
    708   /// double for 64bits) Note that this can return a ConstantAggregateZero
    709   /// object.
    710   static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
    711   static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
    712   static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
    713 
    714   /// getString - This method constructs a CDS and initializes it with a text
    715   /// string. The default behavior (AddNull==true) causes a null terminator to
    716   /// be placed at the end of the array (increasing the length of the string by
    717   /// one more than the StringRef would normally indicate.  Pass AddNull=false
    718   /// to disable this behavior.
    719   static Constant *getString(LLVMContext &Context, StringRef Initializer,
    720                              bool AddNull = true);
    721 
    722   /// getType - Specialize the getType() method to always return an ArrayType,
    723   /// which reduces the amount of casting needed in parts of the compiler.
    724   ///
    725   inline ArrayType *getType() const {
    726     return cast<ArrayType>(Value::getType());
    727   }
    728 
    729   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    730   ///
    731   static bool classof(const Value *V) {
    732     return V->getValueID() == ConstantDataArrayVal;
    733   }
    734 };
    735 
    736 //===----------------------------------------------------------------------===//
    737 /// ConstantDataVector - A vector constant whose element type is a simple
    738 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
    739 /// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
    740 /// operands because it stores all of the elements of the constant as densely
    741 /// packed data, instead of as Value*'s.
    742 class ConstantDataVector : public ConstantDataSequential {
    743   void *operator new(size_t, unsigned) = delete;
    744   ConstantDataVector(const ConstantDataVector &) = delete;
    745   void anchor() override;
    746   friend class ConstantDataSequential;
    747   explicit ConstantDataVector(Type *ty, const char *Data)
    748   : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
    749 protected:
    750   // allocate space for exactly zero operands.
    751   void *operator new(size_t s) {
    752     return User::operator new(s, 0);
    753   }
    754 public:
    755 
    756   /// get() constructors - Return a constant with vector type with an element
    757   /// count and element type matching the ArrayRef passed in.  Note that this
    758   /// can return a ConstantAggregateZero object.
    759   static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
    760   static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
    761   static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
    762   static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
    763   static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
    764   static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
    765 
    766   /// getFP() constructors - Return a constant with vector type with an element
    767   /// count and element type of float with the precision matching the number of
    768   /// bits in the ArrayRef passed in.  (i.e. half for 16bits, float for 32bits,
    769   /// double for 64bits) Note that this can return a ConstantAggregateZero
    770   /// object.
    771   static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
    772   static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
    773   static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
    774 
    775   /// getSplat - Return a ConstantVector with the specified constant in each
    776   /// element.  The specified constant has to be a of a compatible type (i8/i16/
    777   /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
    778   static Constant *getSplat(unsigned NumElts, Constant *Elt);
    779 
    780   /// getSplatValue - If this is a splat constant, meaning that all of the
    781   /// elements have the same value, return that value. Otherwise return NULL.
    782   Constant *getSplatValue() const;
    783 
    784   /// getType - Specialize the getType() method to always return a VectorType,
    785   /// which reduces the amount of casting needed in parts of the compiler.
    786   ///
    787   inline VectorType *getType() const {
    788     return cast<VectorType>(Value::getType());
    789   }
    790 
    791   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    792   ///
    793   static bool classof(const Value *V) {
    794     return V->getValueID() == ConstantDataVectorVal;
    795   }
    796 };
    797 
    798 //===----------------------------------------------------------------------===//
    799 /// ConstantTokenNone - a constant token which is empty
    800 ///
    801 class ConstantTokenNone : public Constant {
    802   void *operator new(size_t, unsigned) = delete;
    803   ConstantTokenNone(const ConstantTokenNone &) = delete;
    804 
    805   friend class Constant;
    806   void destroyConstantImpl();
    807   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    808 
    809 protected:
    810   explicit ConstantTokenNone(LLVMContext &Context)
    811       : Constant(Type::getTokenTy(Context), ConstantTokenNoneVal, nullptr, 0) {}
    812   // allocate space for exactly zero operands
    813   void *operator new(size_t s) { return User::operator new(s, 0); }
    814 
    815 public:
    816   /// Return the ConstantTokenNone.
    817   static ConstantTokenNone *get(LLVMContext &Context);
    818 
    819   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
    820   static bool classof(const Value *V) {
    821     return V->getValueID() == ConstantTokenNoneVal;
    822   }
    823 };
    824 
    825 /// BlockAddress - The address of a basic block.
    826 ///
    827 class BlockAddress : public Constant {
    828   void *operator new(size_t, unsigned) = delete;
    829   void *operator new(size_t s) { return User::operator new(s, 2); }
    830   BlockAddress(Function *F, BasicBlock *BB);
    831 
    832   friend class Constant;
    833   void destroyConstantImpl();
    834   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    835 
    836 public:
    837   /// get - Return a BlockAddress for the specified function and basic block.
    838   static BlockAddress *get(Function *F, BasicBlock *BB);
    839 
    840   /// get - Return a BlockAddress for the specified basic block.  The basic
    841   /// block must be embedded into a function.
    842   static BlockAddress *get(BasicBlock *BB);
    843 
    844   /// \brief Lookup an existing \c BlockAddress constant for the given
    845   /// BasicBlock.
    846   ///
    847   /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
    848   static BlockAddress *lookup(const BasicBlock *BB);
    849 
    850   /// Transparently provide more efficient getOperand methods.
    851   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    852 
    853   Function *getFunction() const { return (Function*)Op<0>().get(); }
    854   BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
    855 
    856   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    857   static inline bool classof(const Value *V) {
    858     return V->getValueID() == BlockAddressVal;
    859   }
    860 };
    861 
    862 template <>
    863 struct OperandTraits<BlockAddress> :
    864   public FixedNumOperandTraits<BlockAddress, 2> {
    865 };
    866 
    867 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
    868 
    869 
    870 //===----------------------------------------------------------------------===//
    871 /// ConstantExpr - a constant value that is initialized with an expression using
    872 /// other constant values.
    873 ///
    874 /// This class uses the standard Instruction opcodes to define the various
    875 /// constant expressions.  The Opcode field for the ConstantExpr class is
    876 /// maintained in the Value::SubclassData field.
    877 class ConstantExpr : public Constant {
    878   friend struct ConstantExprKeyType;
    879 
    880   friend class Constant;
    881   void destroyConstantImpl();
    882   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
    883 
    884 protected:
    885   ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
    886     : Constant(ty, ConstantExprVal, Ops, NumOps) {
    887     // Operation type (an Instruction opcode) is stored as the SubclassData.
    888     setValueSubclassData(Opcode);
    889   }
    890 
    891 public:
    892   // Static methods to construct a ConstantExpr of different kinds.  Note that
    893   // these methods may return a object that is not an instance of the
    894   // ConstantExpr class, because they will attempt to fold the constant
    895   // expression into something simpler if possible.
    896 
    897   /// getAlignOf constant expr - computes the alignment of a type in a target
    898   /// independent way (Note: the return type is an i64).
    899   static Constant *getAlignOf(Type *Ty);
    900 
    901   /// getSizeOf constant expr - computes the (alloc) size of a type (in
    902   /// address-units, not bits) in a target independent way (Note: the return
    903   /// type is an i64).
    904   ///
    905   static Constant *getSizeOf(Type *Ty);
    906 
    907   /// getOffsetOf constant expr - computes the offset of a struct field in a
    908   /// target independent way (Note: the return type is an i64).
    909   ///
    910   static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
    911 
    912   /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
    913   /// which supports any aggregate type, and any Constant index.
    914   ///
    915   static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
    916 
    917   static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
    918   static Constant *getFNeg(Constant *C);
    919   static Constant *getNot(Constant *C);
    920   static Constant *getAdd(Constant *C1, Constant *C2,
    921                           bool HasNUW = false, bool HasNSW = false);
    922   static Constant *getFAdd(Constant *C1, Constant *C2);
    923   static Constant *getSub(Constant *C1, Constant *C2,
    924                           bool HasNUW = false, bool HasNSW = false);
    925   static Constant *getFSub(Constant *C1, Constant *C2);
    926   static Constant *getMul(Constant *C1, Constant *C2,
    927                           bool HasNUW = false, bool HasNSW = false);
    928   static Constant *getFMul(Constant *C1, Constant *C2);
    929   static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
    930   static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
    931   static Constant *getFDiv(Constant *C1, Constant *C2);
    932   static Constant *getURem(Constant *C1, Constant *C2);
    933   static Constant *getSRem(Constant *C1, Constant *C2);
    934   static Constant *getFRem(Constant *C1, Constant *C2);
    935   static Constant *getAnd(Constant *C1, Constant *C2);
    936   static Constant *getOr(Constant *C1, Constant *C2);
    937   static Constant *getXor(Constant *C1, Constant *C2);
    938   static Constant *getShl(Constant *C1, Constant *C2,
    939                           bool HasNUW = false, bool HasNSW = false);
    940   static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
    941   static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
    942   static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    943   static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    944   static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    945   static Constant *getFPTrunc(Constant *C, Type *Ty,
    946                               bool OnlyIfReduced = false);
    947   static Constant *getFPExtend(Constant *C, Type *Ty,
    948                                bool OnlyIfReduced = false);
    949   static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    950   static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    951   static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    952   static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
    953   static Constant *getPtrToInt(Constant *C, Type *Ty,
    954                                bool OnlyIfReduced = false);
    955   static Constant *getIntToPtr(Constant *C, Type *Ty,
    956                                bool OnlyIfReduced = false);
    957   static Constant *getBitCast(Constant *C, Type *Ty,
    958                               bool OnlyIfReduced = false);
    959   static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
    960                                     bool OnlyIfReduced = false);
    961 
    962   static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
    963   static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
    964   static Constant *getNSWAdd(Constant *C1, Constant *C2) {
    965     return getAdd(C1, C2, false, true);
    966   }
    967   static Constant *getNUWAdd(Constant *C1, Constant *C2) {
    968     return getAdd(C1, C2, true, false);
    969   }
    970   static Constant *getNSWSub(Constant *C1, Constant *C2) {
    971     return getSub(C1, C2, false, true);
    972   }
    973   static Constant *getNUWSub(Constant *C1, Constant *C2) {
    974     return getSub(C1, C2, true, false);
    975   }
    976   static Constant *getNSWMul(Constant *C1, Constant *C2) {
    977     return getMul(C1, C2, false, true);
    978   }
    979   static Constant *getNUWMul(Constant *C1, Constant *C2) {
    980     return getMul(C1, C2, true, false);
    981   }
    982   static Constant *getNSWShl(Constant *C1, Constant *C2) {
    983     return getShl(C1, C2, false, true);
    984   }
    985   static Constant *getNUWShl(Constant *C1, Constant *C2) {
    986     return getShl(C1, C2, true, false);
    987   }
    988   static Constant *getExactSDiv(Constant *C1, Constant *C2) {
    989     return getSDiv(C1, C2, true);
    990   }
    991   static Constant *getExactUDiv(Constant *C1, Constant *C2) {
    992     return getUDiv(C1, C2, true);
    993   }
    994   static Constant *getExactAShr(Constant *C1, Constant *C2) {
    995     return getAShr(C1, C2, true);
    996   }
    997   static Constant *getExactLShr(Constant *C1, Constant *C2) {
    998     return getLShr(C1, C2, true);
    999   }
   1000 
   1001   /// getBinOpIdentity - Return the identity for the given binary operation,
   1002   /// i.e. a constant C such that X op C = X and C op X = X for every X.  It
   1003   /// returns null if the operator doesn't have an identity.
   1004   static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
   1005 
   1006   /// getBinOpAbsorber - Return the absorbing element for the given binary
   1007   /// operation, i.e. a constant C such that X op C = C and C op X = C for
   1008   /// every X.  For example, this returns zero for integer multiplication.
   1009   /// It returns null if the operator doesn't have an absorbing element.
   1010   static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
   1011 
   1012   /// Transparently provide more efficient getOperand methods.
   1013   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
   1014 
   1015   /// \brief Convenience function for getting a Cast operation.
   1016   ///
   1017   /// \param ops The opcode for the conversion
   1018   /// \param C  The constant to be converted
   1019   /// \param Ty The type to which the constant is converted
   1020   /// \param OnlyIfReduced see \a getWithOperands() docs.
   1021   static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
   1022                            bool OnlyIfReduced = false);
   1023 
   1024   // @brief Create a ZExt or BitCast cast constant expression
   1025   static Constant *getZExtOrBitCast(
   1026     Constant *C,   ///< The constant to zext or bitcast
   1027     Type *Ty ///< The type to zext or bitcast C to
   1028   );
   1029 
   1030   // @brief Create a SExt or BitCast cast constant expression
   1031   static Constant *getSExtOrBitCast(
   1032     Constant *C,   ///< The constant to sext or bitcast
   1033     Type *Ty ///< The type to sext or bitcast C to
   1034   );
   1035 
   1036   // @brief Create a Trunc or BitCast cast constant expression
   1037   static Constant *getTruncOrBitCast(
   1038     Constant *C,   ///< The constant to trunc or bitcast
   1039     Type *Ty ///< The type to trunc or bitcast C to
   1040   );
   1041 
   1042   /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
   1043   /// expression.
   1044   static Constant *getPointerCast(
   1045     Constant *C,   ///< The pointer value to be casted (operand 0)
   1046     Type *Ty ///< The type to which cast should be made
   1047   );
   1048 
   1049   /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
   1050   /// the address space.
   1051   static Constant *getPointerBitCastOrAddrSpaceCast(
   1052     Constant *C,   ///< The constant to addrspacecast or bitcast
   1053     Type *Ty ///< The type to bitcast or addrspacecast C to
   1054   );
   1055 
   1056   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
   1057   static Constant *getIntegerCast(
   1058     Constant *C,    ///< The integer constant to be casted
   1059     Type *Ty, ///< The integer type to cast to
   1060     bool isSigned   ///< Whether C should be treated as signed or not
   1061   );
   1062 
   1063   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
   1064   static Constant *getFPCast(
   1065     Constant *C,    ///< The integer constant to be casted
   1066     Type *Ty ///< The integer type to cast to
   1067   );
   1068 
   1069   /// @brief Return true if this is a convert constant expression
   1070   bool isCast() const;
   1071 
   1072   /// @brief Return true if this is a compare constant expression
   1073   bool isCompare() const;
   1074 
   1075   /// @brief Return true if this is an insertvalue or extractvalue expression,
   1076   /// and the getIndices() method may be used.
   1077   bool hasIndices() const;
   1078 
   1079   /// @brief Return true if this is a getelementptr expression and all
   1080   /// the index operands are compile-time known integers within the
   1081   /// corresponding notional static array extents. Note that this is
   1082   /// not equivalant to, a subset of, or a superset of the "inbounds"
   1083   /// property.
   1084   bool isGEPWithNoNotionalOverIndexing() const;
   1085 
   1086   /// Select constant expr
   1087   ///
   1088   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
   1089   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
   1090                              Type *OnlyIfReducedTy = nullptr);
   1091 
   1092   /// get - Return a binary or shift operator constant expression,
   1093   /// folding if possible.
   1094   ///
   1095   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
   1096   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
   1097                        unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
   1098 
   1099   /// \brief Return an ICmp or FCmp comparison operator constant expression.
   1100   ///
   1101   /// \param OnlyIfReduced see \a getWithOperands() docs.
   1102   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
   1103                               bool OnlyIfReduced = false);
   1104 
   1105   /// get* - Return some common constants without having to
   1106   /// specify the full Instruction::OPCODE identifier.
   1107   ///
   1108   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
   1109                            bool OnlyIfReduced = false);
   1110   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
   1111                            bool OnlyIfReduced = false);
   1112 
   1113   /// Getelementptr form.  Value* is only accepted for convenience;
   1114   /// all elements must be Constants.
   1115   ///
   1116   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
   1117   static Constant *getGetElementPtr(Type *Ty, Constant *C,
   1118                                     ArrayRef<Constant *> IdxList,
   1119                                     bool InBounds = false,
   1120                                     Type *OnlyIfReducedTy = nullptr) {
   1121     return getGetElementPtr(
   1122         Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
   1123         InBounds, OnlyIfReducedTy);
   1124   }
   1125   static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
   1126                                     bool InBounds = false,
   1127                                     Type *OnlyIfReducedTy = nullptr) {
   1128     // This form of the function only exists to avoid ambiguous overload
   1129     // warnings about whether to convert Idx to ArrayRef<Constant *> or
   1130     // ArrayRef<Value *>.
   1131     return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
   1132   }
   1133   static Constant *getGetElementPtr(Type *Ty, Constant *C,
   1134                                     ArrayRef<Value *> IdxList,
   1135                                     bool InBounds = false,
   1136                                     Type *OnlyIfReducedTy = nullptr);
   1137 
   1138   /// Create an "inbounds" getelementptr. See the documentation for the
   1139   /// "inbounds" flag in LangRef.html for details.
   1140   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
   1141                                             ArrayRef<Constant *> IdxList) {
   1142     return getGetElementPtr(Ty, C, IdxList, true);
   1143   }
   1144   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
   1145                                             Constant *Idx) {
   1146     // This form of the function only exists to avoid ambiguous overload
   1147     // warnings about whether to convert Idx to ArrayRef<Constant *> or
   1148     // ArrayRef<Value *>.
   1149     return getGetElementPtr(Ty, C, Idx, true);
   1150   }
   1151   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
   1152                                             ArrayRef<Value *> IdxList) {
   1153     return getGetElementPtr(Ty, C, IdxList, true);
   1154   }
   1155 
   1156   static Constant *getExtractElement(Constant *Vec, Constant *Idx,
   1157                                      Type *OnlyIfReducedTy = nullptr);
   1158   static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
   1159                                     Type *OnlyIfReducedTy = nullptr);
   1160   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
   1161                                     Type *OnlyIfReducedTy = nullptr);
   1162   static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
   1163                                    Type *OnlyIfReducedTy = nullptr);
   1164   static Constant *getInsertValue(Constant *Agg, Constant *Val,
   1165                                   ArrayRef<unsigned> Idxs,
   1166                                   Type *OnlyIfReducedTy = nullptr);
   1167 
   1168   /// getOpcode - Return the opcode at the root of this constant expression
   1169   unsigned getOpcode() const { return getSubclassDataFromValue(); }
   1170 
   1171   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
   1172   /// not an ICMP or FCMP constant expression.
   1173   unsigned getPredicate() const;
   1174 
   1175   /// getIndices - Assert that this is an insertvalue or exactvalue
   1176   /// expression and return the list of indices.
   1177   ArrayRef<unsigned> getIndices() const;
   1178 
   1179   /// getOpcodeName - Return a string representation for an opcode.
   1180   const char *getOpcodeName() const;
   1181 
   1182   /// getWithOperandReplaced - Return a constant expression identical to this
   1183   /// one, but with the specified operand set to the specified value.
   1184   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
   1185 
   1186   /// getWithOperands - This returns the current constant expression with the
   1187   /// operands replaced with the specified values.  The specified array must
   1188   /// have the same number of operands as our current one.
   1189   Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
   1190     return getWithOperands(Ops, getType());
   1191   }
   1192 
   1193   /// \brief Get the current expression with the operands replaced.
   1194   ///
   1195   /// Return the current constant expression with the operands replaced with \c
   1196   /// Ops and the type with \c Ty.  The new operands must have the same number
   1197   /// as the current ones.
   1198   ///
   1199   /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
   1200   /// gets constant-folded, the type changes, or the expression is otherwise
   1201   /// canonicalized.  This parameter should almost always be \c false.
   1202   Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
   1203                             bool OnlyIfReduced = false,
   1204                             Type *SrcTy = nullptr) const;
   1205 
   1206   /// getAsInstruction - Returns an Instruction which implements the same
   1207   /// operation as this ConstantExpr. The instruction is not linked to any basic
   1208   /// block.
   1209   ///
   1210   /// A better approach to this could be to have a constructor for Instruction
   1211   /// which would take a ConstantExpr parameter, but that would have spread
   1212   /// implementation details of ConstantExpr outside of Constants.cpp, which
   1213   /// would make it harder to remove ConstantExprs altogether.
   1214   Instruction *getAsInstruction();
   1215 
   1216   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   1217   static inline bool classof(const Value *V) {
   1218     return V->getValueID() == ConstantExprVal;
   1219   }
   1220 
   1221 private:
   1222   // Shadow Value::setValueSubclassData with a private forwarding method so that
   1223   // subclasses cannot accidentally use it.
   1224   void setValueSubclassData(unsigned short D) {
   1225     Value::setValueSubclassData(D);
   1226   }
   1227 };
   1228 
   1229 template <>
   1230 struct OperandTraits<ConstantExpr> :
   1231   public VariadicOperandTraits<ConstantExpr, 1> {
   1232 };
   1233 
   1234 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
   1235 
   1236 //===----------------------------------------------------------------------===//
   1237 /// UndefValue - 'undef' values are things that do not have specified contents.
   1238 /// These are used for a variety of purposes, including global variable
   1239 /// initializers and operands to instructions.  'undef' values can occur with
   1240 /// any first-class type.
   1241 ///
   1242 /// Undef values aren't exactly constants; if they have multiple uses, they
   1243 /// can appear to have different bit patterns at each use. See
   1244 /// LangRef.html#undefvalues for details.
   1245 ///
   1246 class UndefValue : public Constant {
   1247   void *operator new(size_t, unsigned) = delete;
   1248   UndefValue(const UndefValue &) = delete;
   1249 
   1250   friend class Constant;
   1251   void destroyConstantImpl();
   1252   Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
   1253 
   1254 protected:
   1255   explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
   1256 protected:
   1257   // allocate space for exactly zero operands
   1258   void *operator new(size_t s) {
   1259     return User::operator new(s, 0);
   1260   }
   1261 public:
   1262   /// get() - Static factory methods - Return an 'undef' object of the specified
   1263   /// type.
   1264   ///
   1265   static UndefValue *get(Type *T);
   1266 
   1267   /// getSequentialElement - If this Undef has array or vector type, return a
   1268   /// undef with the right element type.
   1269   UndefValue *getSequentialElement() const;
   1270 
   1271   /// getStructElement - If this undef has struct type, return a undef with the
   1272   /// right element type for the specified element.
   1273   UndefValue *getStructElement(unsigned Elt) const;
   1274 
   1275   /// getElementValue - Return an undef of the right value for the specified GEP
   1276   /// index.
   1277   UndefValue *getElementValue(Constant *C) const;
   1278 
   1279   /// getElementValue - Return an undef of the right value for the specified GEP
   1280   /// index.
   1281   UndefValue *getElementValue(unsigned Idx) const;
   1282 
   1283   /// \brief Return the number of elements in the array, vector, or struct.
   1284   unsigned getNumElements() const;
   1285 
   1286   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   1287   static bool classof(const Value *V) {
   1288     return V->getValueID() == UndefValueVal;
   1289   }
   1290 };
   1291 
   1292 } // End llvm namespace
   1293 
   1294 #endif
   1295