LLVM  mainline
Constants.h
Go to the documentation of this file.
00001 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 /// @file
00011 /// This file contains the declarations for the subclasses of Constant,
00012 /// which represent the different flavors of constant values that live in LLVM.
00013 /// Note that Constants are immutable (once created they never change) and are
00014 /// fully shared by structural equivalence.  This means that two structurally
00015 /// equivalent constants will always have the same address.  Constants are
00016 /// created on demand as needed and never deleted: thus clients don't have to
00017 /// worry about the lifetime of the objects.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #ifndef LLVM_IR_CONSTANTS_H
00022 #define LLVM_IR_CONSTANTS_H
00023 
00024 #include "llvm/ADT/APFloat.h"
00025 #include "llvm/ADT/APInt.h"
00026 #include "llvm/ADT/ArrayRef.h"
00027 #include "llvm/IR/Constant.h"
00028 #include "llvm/IR/DerivedTypes.h"
00029 #include "llvm/IR/OperandTraits.h"
00030 
00031 namespace llvm {
00032 
00033 class ArrayType;
00034 class IntegerType;
00035 class StructType;
00036 class PointerType;
00037 class VectorType;
00038 class SequentialType;
00039 
00040 struct ConstantExprKeyType;
00041 template <class ConstantClass> struct ConstantAggrKeyType;
00042 
00043 //===----------------------------------------------------------------------===//
00044 /// This is the shared class of boolean and integer constants. This class
00045 /// represents both boolean and integral constants.
00046 /// @brief Class for constant integers.
00047 class ConstantInt : public Constant {
00048   void anchor() override;
00049   void *operator new(size_t, unsigned) = delete;
00050   ConstantInt(const ConstantInt &) = delete;
00051   ConstantInt(IntegerType *Ty, const APInt& V);
00052   APInt Val;
00053 
00054   friend class Constant;
00055   void destroyConstantImpl();
00056 
00057 protected:
00058   // allocate space for exactly zero operands
00059   void *operator new(size_t s) {
00060     return User::operator new(s, 0);
00061   }
00062 public:
00063   static ConstantInt *getTrue(LLVMContext &Context);
00064   static ConstantInt *getFalse(LLVMContext &Context);
00065   static Constant *getTrue(Type *Ty);
00066   static Constant *getFalse(Type *Ty);
00067 
00068   /// If Ty is a vector type, return a Constant with a splat of the given
00069   /// value. Otherwise return a ConstantInt for the given value.
00070   static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
00071 
00072   /// Return a ConstantInt with the specified integer value for the specified
00073   /// type. If the type is wider than 64 bits, the value will be zero-extended
00074   /// to fit the type, unless isSigned is true, in which case the value will
00075   /// be interpreted as a 64-bit signed integer and sign-extended to fit
00076   /// the type.
00077   /// @brief Get a ConstantInt for a specific value.
00078   static ConstantInt *get(IntegerType *Ty, uint64_t V,
00079                           bool isSigned = false);
00080 
00081   /// Return a ConstantInt with the specified value for the specified type. The
00082   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
00083   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
00084   /// signed value for the type Ty.
00085   /// @brief Get a ConstantInt for a specific signed value.
00086   static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
00087   static Constant *getSigned(Type *Ty, int64_t V);
00088 
00089   /// Return a ConstantInt with the specified value and an implied Type. The
00090   /// type is the integer type that corresponds to the bit width of the value.
00091   static ConstantInt *get(LLVMContext &Context, const APInt &V);
00092 
00093   /// Return a ConstantInt constructed from the string strStart with the given
00094   /// radix.
00095   static ConstantInt *get(IntegerType *Ty, StringRef Str,
00096                           uint8_t radix);
00097 
00098   /// If Ty is a vector type, return a Constant with a splat of the given
00099   /// value. Otherwise return a ConstantInt for the given value.
00100   static Constant *get(Type* Ty, const APInt& V);
00101 
00102   /// Return the constant as an APInt value reference. This allows clients to
00103   /// obtain a copy of the value, with all its precision in tact.
00104   /// @brief Return the constant's value.
00105   inline const APInt &getValue() const {
00106     return Val;
00107   }
00108 
00109   /// getBitWidth - Return the bitwidth of this constant.
00110   unsigned getBitWidth() const { return Val.getBitWidth(); }
00111 
00112   /// Return the constant as a 64-bit unsigned integer value after it
00113   /// has been zero extended as appropriate for the type of this constant. Note
00114   /// that this method can assert if the value does not fit in 64 bits.
00115   /// @brief Return the zero extended value.
00116   inline uint64_t getZExtValue() const {
00117     return Val.getZExtValue();
00118   }
00119 
00120   /// Return the constant as a 64-bit integer value after it has been sign
00121   /// extended as appropriate for the type of this constant. Note that
00122   /// this method can assert if the value does not fit in 64 bits.
00123   /// @brief Return the sign extended value.
00124   inline int64_t getSExtValue() const {
00125     return Val.getSExtValue();
00126   }
00127 
00128   /// A helper method that can be used to determine if the constant contained
00129   /// within is equal to a constant.  This only works for very small values,
00130   /// because this is all that can be represented with all types.
00131   /// @brief Determine if this constant's value is same as an unsigned char.
00132   bool equalsInt(uint64_t V) const {
00133     return Val == V;
00134   }
00135 
00136   /// getType - Specialize the getType() method to always return an IntegerType,
00137   /// which reduces the amount of casting needed in parts of the compiler.
00138   ///
00139   inline IntegerType *getType() const {
00140     return cast<IntegerType>(Value::getType());
00141   }
00142 
00143   /// This static method returns true if the type Ty is big enough to
00144   /// represent the value V. This can be used to avoid having the get method
00145   /// assert when V is larger than Ty can represent. Note that there are two
00146   /// versions of this method, one for unsigned and one for signed integers.
00147   /// Although ConstantInt canonicalizes everything to an unsigned integer,
00148   /// the signed version avoids callers having to convert a signed quantity
00149   /// to the appropriate unsigned type before calling the method.
00150   /// @returns true if V is a valid value for type Ty
00151   /// @brief Determine if the value is in range for the given type.
00152   static bool isValueValidForType(Type *Ty, uint64_t V);
00153   static bool isValueValidForType(Type *Ty, int64_t V);
00154 
00155   bool isNegative() const { return Val.isNegative(); }
00156 
00157   /// This is just a convenience method to make client code smaller for a
00158   /// common code. It also correctly performs the comparison without the
00159   /// potential for an assertion from getZExtValue().
00160   bool isZero() const {
00161     return Val == 0;
00162   }
00163 
00164   /// This is just a convenience method to make client code smaller for a
00165   /// common case. It also correctly performs the comparison without the
00166   /// potential for an assertion from getZExtValue().
00167   /// @brief Determine if the value is one.
00168   bool isOne() const {
00169     return Val == 1;
00170   }
00171 
00172   /// This function will return true iff every bit in this constant is set
00173   /// to true.
00174   /// @returns true iff this constant's bits are all set to true.
00175   /// @brief Determine if the value is all ones.
00176   bool isMinusOne() const {
00177     return Val.isAllOnesValue();
00178   }
00179 
00180   /// This function will return true iff this constant represents the largest
00181   /// value that may be represented by the constant's type.
00182   /// @returns true iff this is the largest value that may be represented
00183   /// by this type.
00184   /// @brief Determine if the value is maximal.
00185   bool isMaxValue(bool isSigned) const {
00186     if (isSigned)
00187       return Val.isMaxSignedValue();
00188     else
00189       return Val.isMaxValue();
00190   }
00191 
00192   /// This function will return true iff this constant represents the smallest
00193   /// value that may be represented by this constant's type.
00194   /// @returns true if this is the smallest value that may be represented by
00195   /// this type.
00196   /// @brief Determine if the value is minimal.
00197   bool isMinValue(bool isSigned) const {
00198     if (isSigned)
00199       return Val.isMinSignedValue();
00200     else
00201       return Val.isMinValue();
00202   }
00203 
00204   /// This function will return true iff this constant represents a value with
00205   /// active bits bigger than 64 bits or a value greater than the given uint64_t
00206   /// value.
00207   /// @returns true iff this constant is greater or equal to the given number.
00208   /// @brief Determine if the value is greater or equal to the given number.
00209   bool uge(uint64_t Num) const {
00210     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
00211   }
00212 
00213   /// getLimitedValue - If the value is smaller than the specified limit,
00214   /// return it, otherwise return the limit value.  This causes the value
00215   /// to saturate to the limit.
00216   /// @returns the min of the value of the constant and the specified value
00217   /// @brief Get the constant's value with a saturation limit
00218   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
00219     return Val.getLimitedValue(Limit);
00220   }
00221 
00222   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
00223   static bool classof(const Value *V) {
00224     return V->getValueID() == ConstantIntVal;
00225   }
00226 };
00227 
00228 
00229 //===----------------------------------------------------------------------===//
00230 /// ConstantFP - Floating Point Values [float, double]
00231 ///
00232 class ConstantFP : public Constant {
00233   APFloat Val;
00234   void anchor() override;
00235   void *operator new(size_t, unsigned) = delete;
00236   ConstantFP(const ConstantFP &) = delete;
00237   friend class LLVMContextImpl;
00238 
00239   friend class Constant;
00240   void destroyConstantImpl();
00241 
00242 protected:
00243   ConstantFP(Type *Ty, const APFloat& V);
00244 protected:
00245   // allocate space for exactly zero operands
00246   void *operator new(size_t s) {
00247     return User::operator new(s, 0);
00248   }
00249 public:
00250   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
00251   /// method returns the negative zero constant for floating point or vector
00252   /// floating point types; for all other types, it returns the null value.
00253   static Constant *getZeroValueForNegation(Type *Ty);
00254 
00255   /// get() - This returns a ConstantFP, or a vector containing a splat of a
00256   /// ConstantFP, for the specified value in the specified type.  This should
00257   /// only be used for simple constant values like 2.0/1.0 etc, that are
00258   /// known-valid both as host double and as the target format.
00259   static Constant *get(Type* Ty, double V);
00260   static Constant *get(Type* Ty, StringRef Str);
00261   static ConstantFP *get(LLVMContext &Context, const APFloat &V);
00262   static Constant *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
00263   static Constant *getNegativeZero(Type *Ty);
00264   static Constant *getInfinity(Type *Ty, bool Negative = false);
00265 
00266   /// isValueValidForType - return true if Ty is big enough to represent V.
00267   static bool isValueValidForType(Type *Ty, const APFloat &V);
00268   inline const APFloat &getValueAPF() const { return Val; }
00269 
00270   /// isZero - Return true if the value is positive or negative zero.
00271   bool isZero() const { return Val.isZero(); }
00272 
00273   /// isNegative - Return true if the sign bit is set.
00274   bool isNegative() const { return Val.isNegative(); }
00275 
00276   /// isInfinity - Return true if the value is infinity
00277   bool isInfinity() const { return Val.isInfinity(); }
00278 
00279   /// isNaN - Return true if the value is a NaN.
00280   bool isNaN() const { return Val.isNaN(); }
00281 
00282   /// isExactlyValue - We don't rely on operator== working on double values, as
00283   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
00284   /// As such, this method can be used to do an exact bit-for-bit comparison of
00285   /// two floating point values.  The version with a double operand is retained
00286   /// because it's so convenient to write isExactlyValue(2.0), but please use
00287   /// it only for simple constants.
00288   bool isExactlyValue(const APFloat &V) const;
00289 
00290   bool isExactlyValue(double V) const {
00291     bool ignored;
00292     APFloat FV(V);
00293     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
00294     return isExactlyValue(FV);
00295   }
00296   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00297   static bool classof(const Value *V) {
00298     return V->getValueID() == ConstantFPVal;
00299   }
00300 };
00301 
00302 //===----------------------------------------------------------------------===//
00303 /// ConstantAggregateZero - All zero aggregate value
00304 ///
00305 class ConstantAggregateZero : public Constant {
00306   void *operator new(size_t, unsigned) = delete;
00307   ConstantAggregateZero(const ConstantAggregateZero &) = delete;
00308 
00309   friend class Constant;
00310   void destroyConstantImpl();
00311 
00312 protected:
00313   explicit ConstantAggregateZero(Type *ty)
00314     : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
00315 protected:
00316   // allocate space for exactly zero operands
00317   void *operator new(size_t s) {
00318     return User::operator new(s, 0);
00319   }
00320 public:
00321   static ConstantAggregateZero *get(Type *Ty);
00322 
00323   /// getSequentialElement - If this CAZ has array or vector type, return a zero
00324   /// with the right element type.
00325   Constant *getSequentialElement() const;
00326 
00327   /// getStructElement - If this CAZ has struct type, return a zero with the
00328   /// right element type for the specified element.
00329   Constant *getStructElement(unsigned Elt) const;
00330 
00331   /// getElementValue - Return a zero of the right value for the specified GEP
00332   /// index.
00333   Constant *getElementValue(Constant *C) const;
00334 
00335   /// getElementValue - Return a zero of the right value for the specified GEP
00336   /// index.
00337   Constant *getElementValue(unsigned Idx) const;
00338 
00339   /// \brief Return the number of elements in the array, vector, or struct.
00340   unsigned getNumElements() const;
00341 
00342   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00343   ///
00344   static bool classof(const Value *V) {
00345     return V->getValueID() == ConstantAggregateZeroVal;
00346   }
00347 };
00348 
00349 
00350 //===----------------------------------------------------------------------===//
00351 /// ConstantArray - Constant Array Declarations
00352 ///
00353 class ConstantArray : public Constant {
00354   friend struct ConstantAggrKeyType<ConstantArray>;
00355   ConstantArray(const ConstantArray &) = delete;
00356 
00357   friend class Constant;
00358   void destroyConstantImpl();
00359 
00360 protected:
00361   ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
00362 public:
00363   // ConstantArray accessors
00364   static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
00365 
00366 private:
00367   static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
00368 
00369 public:
00370   /// Transparently provide more efficient getOperand methods.
00371   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00372 
00373   /// getType - Specialize the getType() method to always return an ArrayType,
00374   /// which reduces the amount of casting needed in parts of the compiler.
00375   ///
00376   inline ArrayType *getType() const {
00377     return cast<ArrayType>(Value::getType());
00378   }
00379 
00380   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
00381 
00382   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00383   static bool classof(const Value *V) {
00384     return V->getValueID() == ConstantArrayVal;
00385   }
00386 };
00387 
00388 template <>
00389 struct OperandTraits<ConstantArray> :
00390   public VariadicOperandTraits<ConstantArray> {
00391 };
00392 
00393 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
00394 
00395 //===----------------------------------------------------------------------===//
00396 // ConstantStruct - Constant Struct Declarations
00397 //
00398 class ConstantStruct : public Constant {
00399   friend struct ConstantAggrKeyType<ConstantStruct>;
00400   ConstantStruct(const ConstantStruct &) = delete;
00401 
00402   friend class Constant;
00403   void destroyConstantImpl();
00404 
00405 protected:
00406   ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
00407 public:
00408   // ConstantStruct accessors
00409   static Constant *get(StructType *T, ArrayRef<Constant*> V);
00410   static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
00411 
00412   /// getAnon - Return an anonymous struct that has the specified
00413   /// elements.  If the struct is possibly empty, then you must specify a
00414   /// context.
00415   static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
00416     return get(getTypeForElements(V, Packed), V);
00417   }
00418   static Constant *getAnon(LLVMContext &Ctx,
00419                            ArrayRef<Constant*> V, bool Packed = false) {
00420     return get(getTypeForElements(Ctx, V, Packed), V);
00421   }
00422 
00423   /// getTypeForElements - Return an anonymous struct type to use for a constant
00424   /// with the specified set of elements.  The list must not be empty.
00425   static StructType *getTypeForElements(ArrayRef<Constant*> V,
00426                                         bool Packed = false);
00427   /// getTypeForElements - This version of the method allows an empty list.
00428   static StructType *getTypeForElements(LLVMContext &Ctx,
00429                                         ArrayRef<Constant*> V,
00430                                         bool Packed = false);
00431 
00432   /// Transparently provide more efficient getOperand methods.
00433   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00434 
00435   /// getType() specialization - Reduce amount of casting...
00436   ///
00437   inline StructType *getType() const {
00438     return cast<StructType>(Value::getType());
00439   }
00440 
00441   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
00442 
00443   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00444   static bool classof(const Value *V) {
00445     return V->getValueID() == ConstantStructVal;
00446   }
00447 };
00448 
00449 template <>
00450 struct OperandTraits<ConstantStruct> :
00451   public VariadicOperandTraits<ConstantStruct> {
00452 };
00453 
00454 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
00455 
00456 
00457 //===----------------------------------------------------------------------===//
00458 /// ConstantVector - Constant Vector Declarations
00459 ///
00460 class ConstantVector : public Constant {
00461   friend struct ConstantAggrKeyType<ConstantVector>;
00462   ConstantVector(const ConstantVector &) = delete;
00463 
00464   friend class Constant;
00465   void destroyConstantImpl();
00466 
00467 protected:
00468   ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
00469 public:
00470   // ConstantVector accessors
00471   static Constant *get(ArrayRef<Constant*> V);
00472 
00473 private:
00474   static Constant *getImpl(ArrayRef<Constant *> V);
00475 
00476 public:
00477   /// getSplat - Return a ConstantVector with the specified constant in each
00478   /// element.
00479   static Constant *getSplat(unsigned NumElts, Constant *Elt);
00480 
00481   /// Transparently provide more efficient getOperand methods.
00482   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00483 
00484   /// getType - Specialize the getType() method to always return a VectorType,
00485   /// which reduces the amount of casting needed in parts of the compiler.
00486   ///
00487   inline VectorType *getType() const {
00488     return cast<VectorType>(Value::getType());
00489   }
00490 
00491   /// getSplatValue - If this is a splat constant, meaning that all of the
00492   /// elements have the same value, return that value. Otherwise return NULL.
00493   Constant *getSplatValue() const;
00494 
00495   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
00496 
00497   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00498   static bool classof(const Value *V) {
00499     return V->getValueID() == ConstantVectorVal;
00500   }
00501 };
00502 
00503 template <>
00504 struct OperandTraits<ConstantVector> :
00505   public VariadicOperandTraits<ConstantVector> {
00506 };
00507 
00508 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
00509 
00510 //===----------------------------------------------------------------------===//
00511 /// ConstantPointerNull - a constant pointer value that points to null
00512 ///
00513 class ConstantPointerNull : public Constant {
00514   void *operator new(size_t, unsigned) = delete;
00515   ConstantPointerNull(const ConstantPointerNull &) = delete;
00516 
00517   friend class Constant;
00518   void destroyConstantImpl();
00519 
00520 protected:
00521   explicit ConstantPointerNull(PointerType *T)
00522     : Constant(T,
00523                Value::ConstantPointerNullVal, nullptr, 0) {}
00524 
00525 protected:
00526   // allocate space for exactly zero operands
00527   void *operator new(size_t s) {
00528     return User::operator new(s, 0);
00529   }
00530 public:
00531   /// get() - Static factory methods - Return objects of the specified value
00532   static ConstantPointerNull *get(PointerType *T);
00533 
00534   /// getType - Specialize the getType() method to always return an PointerType,
00535   /// which reduces the amount of casting needed in parts of the compiler.
00536   ///
00537   inline PointerType *getType() const {
00538     return cast<PointerType>(Value::getType());
00539   }
00540 
00541   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00542   static bool classof(const Value *V) {
00543     return V->getValueID() == ConstantPointerNullVal;
00544   }
00545 };
00546 
00547 //===----------------------------------------------------------------------===//
00548 /// ConstantDataSequential - A vector or array constant whose element type is a
00549 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
00550 /// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
00551 /// operands because it stores all of the elements of the constant as densely
00552 /// packed data, instead of as Value*'s.
00553 ///
00554 /// This is the common base class of ConstantDataArray and ConstantDataVector.
00555 ///
00556 class ConstantDataSequential : public Constant {
00557   friend class LLVMContextImpl;
00558   /// DataElements - A pointer to the bytes underlying this constant (which is
00559   /// owned by the uniquing StringMap).
00560   const char *DataElements;
00561 
00562   /// Next - This forms a link list of ConstantDataSequential nodes that have
00563   /// the same value but different type.  For example, 0,0,0,1 could be a 4
00564   /// element array of i8, or a 1-element array of i32.  They'll both end up in
00565   /// the same StringMap bucket, linked up.
00566   ConstantDataSequential *Next;
00567   void *operator new(size_t, unsigned) = delete;
00568   ConstantDataSequential(const ConstantDataSequential &) = delete;
00569 
00570   friend class Constant;
00571   void destroyConstantImpl();
00572 
00573 protected:
00574   explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
00575     : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
00576   ~ConstantDataSequential() override { delete Next; }
00577 
00578   static Constant *getImpl(StringRef Bytes, Type *Ty);
00579 
00580 protected:
00581   // allocate space for exactly zero operands.
00582   void *operator new(size_t s) {
00583     return User::operator new(s, 0);
00584   }
00585 public:
00586 
00587   /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
00588   /// formed with a vector or array of the specified element type.
00589   /// ConstantDataArray only works with normal float and int types that are
00590   /// stored densely in memory, not with things like i42 or x86_f80.
00591   static bool isElementTypeCompatible(const Type *Ty);
00592 
00593   /// getElementAsInteger - If this is a sequential container of integers (of
00594   /// any size), return the specified element in the low bits of a uint64_t.
00595   uint64_t getElementAsInteger(unsigned i) const;
00596 
00597   /// getElementAsAPFloat - If this is a sequential container of floating point
00598   /// type, return the specified element as an APFloat.
00599   APFloat getElementAsAPFloat(unsigned i) const;
00600 
00601   /// getElementAsFloat - If this is an sequential container of floats, return
00602   /// the specified element as a float.
00603   float getElementAsFloat(unsigned i) const;
00604 
00605   /// getElementAsDouble - If this is an sequential container of doubles, return
00606   /// the specified element as a double.
00607   double getElementAsDouble(unsigned i) const;
00608 
00609   /// getElementAsConstant - Return a Constant for a specified index's element.
00610   /// Note that this has to compute a new constant to return, so it isn't as
00611   /// efficient as getElementAsInteger/Float/Double.
00612   Constant *getElementAsConstant(unsigned i) const;
00613 
00614   /// getType - Specialize the getType() method to always return a
00615   /// SequentialType, which reduces the amount of casting needed in parts of the
00616   /// compiler.
00617   inline SequentialType *getType() const {
00618     return cast<SequentialType>(Value::getType());
00619   }
00620 
00621   /// getElementType - Return the element type of the array/vector.
00622   Type *getElementType() const;
00623 
00624   /// getNumElements - Return the number of elements in the array or vector.
00625   unsigned getNumElements() const;
00626 
00627   /// getElementByteSize - Return the size (in bytes) of each element in the
00628   /// array/vector.  The size of the elements is known to be a multiple of one
00629   /// byte.
00630   uint64_t getElementByteSize() const;
00631 
00632 
00633   /// isString - This method returns true if this is an array of i8.
00634   bool isString() const;
00635 
00636   /// isCString - This method returns true if the array "isString", ends with a
00637   /// nul byte, and does not contains any other nul bytes.
00638   bool isCString() const;
00639 
00640   /// getAsString - If this array is isString(), then this method returns the
00641   /// array as a StringRef.  Otherwise, it asserts out.
00642   ///
00643   StringRef getAsString() const {
00644     assert(isString() && "Not a string");
00645     return getRawDataValues();
00646   }
00647 
00648   /// getAsCString - If this array is isCString(), then this method returns the
00649   /// array (without the trailing null byte) as a StringRef. Otherwise, it
00650   /// asserts out.
00651   ///
00652   StringRef getAsCString() const {
00653     assert(isCString() && "Isn't a C string");
00654     StringRef Str = getAsString();
00655     return Str.substr(0, Str.size()-1);
00656   }
00657 
00658   /// getRawDataValues - Return the raw, underlying, bytes of this data.  Note
00659   /// that this is an extremely tricky thing to work with, as it exposes the
00660   /// host endianness of the data elements.
00661   StringRef getRawDataValues() const;
00662 
00663   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00664   ///
00665   static bool classof(const Value *V) {
00666     return V->getValueID() == ConstantDataArrayVal ||
00667            V->getValueID() == ConstantDataVectorVal;
00668   }
00669 private:
00670   const char *getElementPointer(unsigned Elt) const;
00671 };
00672 
00673 //===----------------------------------------------------------------------===//
00674 /// ConstantDataArray - An array constant whose element type is a simple
00675 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
00676 /// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
00677 /// operands because it stores all of the elements of the constant as densely
00678 /// packed data, instead of as Value*'s.
00679 class ConstantDataArray : public ConstantDataSequential {
00680   void *operator new(size_t, unsigned) = delete;
00681   ConstantDataArray(const ConstantDataArray &) = delete;
00682   void anchor() override;
00683   friend class ConstantDataSequential;
00684   explicit ConstantDataArray(Type *ty, const char *Data)
00685     : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
00686 protected:
00687   // allocate space for exactly zero operands.
00688   void *operator new(size_t s) {
00689     return User::operator new(s, 0);
00690   }
00691 public:
00692 
00693   /// get() constructors - Return a constant with array type with an element
00694   /// count and element type matching the ArrayRef passed in.  Note that this
00695   /// can return a ConstantAggregateZero object.
00696   static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
00697   static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
00698   static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
00699   static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
00700   static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
00701   static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
00702 
00703   /// getFP() constructors - Return a constant with array type with an element
00704   /// count and element type of float with precision matching the number of
00705   /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
00706   /// double for 64bits) Note that this can return a ConstantAggregateZero
00707   /// object.
00708   static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
00709   static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
00710   static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
00711 
00712   /// getString - This method constructs a CDS and initializes it with a text
00713   /// string. The default behavior (AddNull==true) causes a null terminator to
00714   /// be placed at the end of the array (increasing the length of the string by
00715   /// one more than the StringRef would normally indicate.  Pass AddNull=false
00716   /// to disable this behavior.
00717   static Constant *getString(LLVMContext &Context, StringRef Initializer,
00718                              bool AddNull = true);
00719 
00720   /// getType - Specialize the getType() method to always return an ArrayType,
00721   /// which reduces the amount of casting needed in parts of the compiler.
00722   ///
00723   inline ArrayType *getType() const {
00724     return cast<ArrayType>(Value::getType());
00725   }
00726 
00727   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00728   ///
00729   static bool classof(const Value *V) {
00730     return V->getValueID() == ConstantDataArrayVal;
00731   }
00732 };
00733 
00734 //===----------------------------------------------------------------------===//
00735 /// ConstantDataVector - A vector constant whose element type is a simple
00736 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
00737 /// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
00738 /// operands because it stores all of the elements of the constant as densely
00739 /// packed data, instead of as Value*'s.
00740 class ConstantDataVector : public ConstantDataSequential {
00741   void *operator new(size_t, unsigned) = delete;
00742   ConstantDataVector(const ConstantDataVector &) = delete;
00743   void anchor() override;
00744   friend class ConstantDataSequential;
00745   explicit ConstantDataVector(Type *ty, const char *Data)
00746   : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
00747 protected:
00748   // allocate space for exactly zero operands.
00749   void *operator new(size_t s) {
00750     return User::operator new(s, 0);
00751   }
00752 public:
00753 
00754   /// get() constructors - Return a constant with vector type with an element
00755   /// count and element type matching the ArrayRef passed in.  Note that this
00756   /// can return a ConstantAggregateZero object.
00757   static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
00758   static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
00759   static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
00760   static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
00761   static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
00762   static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
00763 
00764   /// getFP() constructors - Return a constant with vector type with an element
00765   /// count and element type of float with the precision matching the number of
00766   /// bits in the ArrayRef passed in.  (i.e. half for 16bits, float for 32bits,
00767   /// double for 64bits) Note that this can return a ConstantAggregateZero
00768   /// object.
00769   static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
00770   static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
00771   static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
00772 
00773   /// getSplat - Return a ConstantVector with the specified constant in each
00774   /// element.  The specified constant has to be a of a compatible type (i8/i16/
00775   /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
00776   static Constant *getSplat(unsigned NumElts, Constant *Elt);
00777 
00778   /// getSplatValue - If this is a splat constant, meaning that all of the
00779   /// elements have the same value, return that value. Otherwise return NULL.
00780   Constant *getSplatValue() const;
00781 
00782   /// getType - Specialize the getType() method to always return a VectorType,
00783   /// which reduces the amount of casting needed in parts of the compiler.
00784   ///
00785   inline VectorType *getType() const {
00786     return cast<VectorType>(Value::getType());
00787   }
00788 
00789   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00790   ///
00791   static bool classof(const Value *V) {
00792     return V->getValueID() == ConstantDataVectorVal;
00793   }
00794 };
00795 
00796 
00797 
00798 /// BlockAddress - The address of a basic block.
00799 ///
00800 class BlockAddress : public Constant {
00801   void *operator new(size_t, unsigned) = delete;
00802   void *operator new(size_t s) { return User::operator new(s, 2); }
00803   BlockAddress(Function *F, BasicBlock *BB);
00804 
00805   friend class Constant;
00806   void destroyConstantImpl();
00807 
00808 public:
00809   /// get - Return a BlockAddress for the specified function and basic block.
00810   static BlockAddress *get(Function *F, BasicBlock *BB);
00811 
00812   /// get - Return a BlockAddress for the specified basic block.  The basic
00813   /// block must be embedded into a function.
00814   static BlockAddress *get(BasicBlock *BB);
00815 
00816   /// \brief Lookup an existing \c BlockAddress constant for the given
00817   /// BasicBlock.
00818   ///
00819   /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
00820   static BlockAddress *lookup(const BasicBlock *BB);
00821 
00822   /// Transparently provide more efficient getOperand methods.
00823   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00824 
00825   Function *getFunction() const { return (Function*)Op<0>().get(); }
00826   BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
00827 
00828   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
00829 
00830   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00831   static inline bool classof(const Value *V) {
00832     return V->getValueID() == BlockAddressVal;
00833   }
00834 };
00835 
00836 template <>
00837 struct OperandTraits<BlockAddress> :
00838   public FixedNumOperandTraits<BlockAddress, 2> {
00839 };
00840 
00841 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
00842 
00843 
00844 //===----------------------------------------------------------------------===//
00845 /// ConstantExpr - a constant value that is initialized with an expression using
00846 /// other constant values.
00847 ///
00848 /// This class uses the standard Instruction opcodes to define the various
00849 /// constant expressions.  The Opcode field for the ConstantExpr class is
00850 /// maintained in the Value::SubclassData field.
00851 class ConstantExpr : public Constant {
00852   friend struct ConstantExprKeyType;
00853 
00854   friend class Constant;
00855   void destroyConstantImpl();
00856 
00857 protected:
00858   ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
00859     : Constant(ty, ConstantExprVal, Ops, NumOps) {
00860     // Operation type (an Instruction opcode) is stored as the SubclassData.
00861     setValueSubclassData(Opcode);
00862   }
00863 
00864 public:
00865   // Static methods to construct a ConstantExpr of different kinds.  Note that
00866   // these methods may return a object that is not an instance of the
00867   // ConstantExpr class, because they will attempt to fold the constant
00868   // expression into something simpler if possible.
00869 
00870   /// getAlignOf constant expr - computes the alignment of a type in a target
00871   /// independent way (Note: the return type is an i64).
00872   static Constant *getAlignOf(Type *Ty);
00873 
00874   /// getSizeOf constant expr - computes the (alloc) size of a type (in
00875   /// address-units, not bits) in a target independent way (Note: the return
00876   /// type is an i64).
00877   ///
00878   static Constant *getSizeOf(Type *Ty);
00879 
00880   /// getOffsetOf constant expr - computes the offset of a struct field in a
00881   /// target independent way (Note: the return type is an i64).
00882   ///
00883   static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
00884 
00885   /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
00886   /// which supports any aggregate type, and any Constant index.
00887   ///
00888   static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
00889 
00890   static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
00891   static Constant *getFNeg(Constant *C);
00892   static Constant *getNot(Constant *C);
00893   static Constant *getAdd(Constant *C1, Constant *C2,
00894                           bool HasNUW = false, bool HasNSW = false);
00895   static Constant *getFAdd(Constant *C1, Constant *C2);
00896   static Constant *getSub(Constant *C1, Constant *C2,
00897                           bool HasNUW = false, bool HasNSW = false);
00898   static Constant *getFSub(Constant *C1, Constant *C2);
00899   static Constant *getMul(Constant *C1, Constant *C2,
00900                           bool HasNUW = false, bool HasNSW = false);
00901   static Constant *getFMul(Constant *C1, Constant *C2);
00902   static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
00903   static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
00904   static Constant *getFDiv(Constant *C1, Constant *C2);
00905   static Constant *getURem(Constant *C1, Constant *C2);
00906   static Constant *getSRem(Constant *C1, Constant *C2);
00907   static Constant *getFRem(Constant *C1, Constant *C2);
00908   static Constant *getAnd(Constant *C1, Constant *C2);
00909   static Constant *getOr(Constant *C1, Constant *C2);
00910   static Constant *getXor(Constant *C1, Constant *C2);
00911   static Constant *getShl(Constant *C1, Constant *C2,
00912                           bool HasNUW = false, bool HasNSW = false);
00913   static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
00914   static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
00915   static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00916   static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00917   static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00918   static Constant *getFPTrunc(Constant *C, Type *Ty,
00919                               bool OnlyIfReduced = false);
00920   static Constant *getFPExtend(Constant *C, Type *Ty,
00921                                bool OnlyIfReduced = false);
00922   static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00923   static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00924   static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00925   static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
00926   static Constant *getPtrToInt(Constant *C, Type *Ty,
00927                                bool OnlyIfReduced = false);
00928   static Constant *getIntToPtr(Constant *C, Type *Ty,
00929                                bool OnlyIfReduced = false);
00930   static Constant *getBitCast(Constant *C, Type *Ty,
00931                               bool OnlyIfReduced = false);
00932   static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
00933                                     bool OnlyIfReduced = false);
00934 
00935   static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
00936   static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
00937   static Constant *getNSWAdd(Constant *C1, Constant *C2) {
00938     return getAdd(C1, C2, false, true);
00939   }
00940   static Constant *getNUWAdd(Constant *C1, Constant *C2) {
00941     return getAdd(C1, C2, true, false);
00942   }
00943   static Constant *getNSWSub(Constant *C1, Constant *C2) {
00944     return getSub(C1, C2, false, true);
00945   }
00946   static Constant *getNUWSub(Constant *C1, Constant *C2) {
00947     return getSub(C1, C2, true, false);
00948   }
00949   static Constant *getNSWMul(Constant *C1, Constant *C2) {
00950     return getMul(C1, C2, false, true);
00951   }
00952   static Constant *getNUWMul(Constant *C1, Constant *C2) {
00953     return getMul(C1, C2, true, false);
00954   }
00955   static Constant *getNSWShl(Constant *C1, Constant *C2) {
00956     return getShl(C1, C2, false, true);
00957   }
00958   static Constant *getNUWShl(Constant *C1, Constant *C2) {
00959     return getShl(C1, C2, true, false);
00960   }
00961   static Constant *getExactSDiv(Constant *C1, Constant *C2) {
00962     return getSDiv(C1, C2, true);
00963   }
00964   static Constant *getExactUDiv(Constant *C1, Constant *C2) {
00965     return getUDiv(C1, C2, true);
00966   }
00967   static Constant *getExactAShr(Constant *C1, Constant *C2) {
00968     return getAShr(C1, C2, true);
00969   }
00970   static Constant *getExactLShr(Constant *C1, Constant *C2) {
00971     return getLShr(C1, C2, true);
00972   }
00973 
00974   /// getBinOpIdentity - Return the identity for the given binary operation,
00975   /// i.e. a constant C such that X op C = X and C op X = X for every X.  It
00976   /// returns null if the operator doesn't have an identity.
00977   static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
00978 
00979   /// getBinOpAbsorber - Return the absorbing element for the given binary
00980   /// operation, i.e. a constant C such that X op C = C and C op X = C for
00981   /// every X.  For example, this returns zero for integer multiplication.
00982   /// It returns null if the operator doesn't have an absorbing element.
00983   static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
00984 
00985   /// Transparently provide more efficient getOperand methods.
00986   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00987 
00988   /// \brief Convenience function for getting a Cast operation.
00989   ///
00990   /// \param ops The opcode for the conversion
00991   /// \param C  The constant to be converted
00992   /// \param Ty The type to which the constant is converted
00993   /// \param OnlyIfReduced see \a getWithOperands() docs.
00994   static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
00995                            bool OnlyIfReduced = false);
00996 
00997   // @brief Create a ZExt or BitCast cast constant expression
00998   static Constant *getZExtOrBitCast(
00999     Constant *C,   ///< The constant to zext or bitcast
01000     Type *Ty ///< The type to zext or bitcast C to
01001   );
01002 
01003   // @brief Create a SExt or BitCast cast constant expression
01004   static Constant *getSExtOrBitCast(
01005     Constant *C,   ///< The constant to sext or bitcast
01006     Type *Ty ///< The type to sext or bitcast C to
01007   );
01008 
01009   // @brief Create a Trunc or BitCast cast constant expression
01010   static Constant *getTruncOrBitCast(
01011     Constant *C,   ///< The constant to trunc or bitcast
01012     Type *Ty ///< The type to trunc or bitcast C to
01013   );
01014 
01015   /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
01016   /// expression.
01017   static Constant *getPointerCast(
01018     Constant *C,   ///< The pointer value to be casted (operand 0)
01019     Type *Ty ///< The type to which cast should be made
01020   );
01021 
01022   /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
01023   /// the address space.
01024   static Constant *getPointerBitCastOrAddrSpaceCast(
01025     Constant *C,   ///< The constant to addrspacecast or bitcast
01026     Type *Ty ///< The type to bitcast or addrspacecast C to
01027   );
01028 
01029   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
01030   static Constant *getIntegerCast(
01031     Constant *C,    ///< The integer constant to be casted
01032     Type *Ty, ///< The integer type to cast to
01033     bool isSigned   ///< Whether C should be treated as signed or not
01034   );
01035 
01036   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
01037   static Constant *getFPCast(
01038     Constant *C,    ///< The integer constant to be casted
01039     Type *Ty ///< The integer type to cast to
01040   );
01041 
01042   /// @brief Return true if this is a convert constant expression
01043   bool isCast() const;
01044 
01045   /// @brief Return true if this is a compare constant expression
01046   bool isCompare() const;
01047 
01048   /// @brief Return true if this is an insertvalue or extractvalue expression,
01049   /// and the getIndices() method may be used.
01050   bool hasIndices() const;
01051 
01052   /// @brief Return true if this is a getelementptr expression and all
01053   /// the index operands are compile-time known integers within the
01054   /// corresponding notional static array extents. Note that this is
01055   /// not equivalant to, a subset of, or a superset of the "inbounds"
01056   /// property.
01057   bool isGEPWithNoNotionalOverIndexing() const;
01058 
01059   /// Select constant expr
01060   ///
01061   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
01062   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
01063                              Type *OnlyIfReducedTy = nullptr);
01064 
01065   /// get - Return a binary or shift operator constant expression,
01066   /// folding if possible.
01067   ///
01068   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
01069   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
01070                        unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
01071 
01072   /// \brief Return an ICmp or FCmp comparison operator constant expression.
01073   ///
01074   /// \param OnlyIfReduced see \a getWithOperands() docs.
01075   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
01076                               bool OnlyIfReduced = false);
01077 
01078   /// get* - Return some common constants without having to
01079   /// specify the full Instruction::OPCODE identifier.
01080   ///
01081   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
01082                            bool OnlyIfReduced = false);
01083   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
01084                            bool OnlyIfReduced = false);
01085 
01086   /// Getelementptr form.  Value* is only accepted for convenience;
01087   /// all elements must be Constants.
01088   ///
01089   /// \param OnlyIfReducedTy see \a getWithOperands() docs.
01090   static Constant *getGetElementPtr(Type *Ty, Constant *C,
01091                                     ArrayRef<Constant *> IdxList,
01092                                     bool InBounds = false,
01093                                     Type *OnlyIfReducedTy = nullptr) {
01094     return getGetElementPtr(
01095         Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
01096         InBounds, OnlyIfReducedTy);
01097   }
01098   static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
01099                                     bool InBounds = false,
01100                                     Type *OnlyIfReducedTy = nullptr) {
01101     // This form of the function only exists to avoid ambiguous overload
01102     // warnings about whether to convert Idx to ArrayRef<Constant *> or
01103     // ArrayRef<Value *>.
01104     return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
01105   }
01106   static Constant *getGetElementPtr(Type *Ty, Constant *C,
01107                                     ArrayRef<Value *> IdxList,
01108                                     bool InBounds = false,
01109                                     Type *OnlyIfReducedTy = nullptr);
01110 
01111   /// Create an "inbounds" getelementptr. See the documentation for the
01112   /// "inbounds" flag in LangRef.html for details.
01113   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
01114                                             ArrayRef<Constant *> IdxList) {
01115     return getGetElementPtr(Ty, C, IdxList, true);
01116   }
01117   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
01118                                             Constant *Idx) {
01119     // This form of the function only exists to avoid ambiguous overload
01120     // warnings about whether to convert Idx to ArrayRef<Constant *> or
01121     // ArrayRef<Value *>.
01122     return getGetElementPtr(Ty, C, Idx, true);
01123   }
01124   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
01125                                             ArrayRef<Value *> IdxList) {
01126     return getGetElementPtr(Ty, C, IdxList, true);
01127   }
01128 
01129   static Constant *getExtractElement(Constant *Vec, Constant *Idx,
01130                                      Type *OnlyIfReducedTy = nullptr);
01131   static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
01132                                     Type *OnlyIfReducedTy = nullptr);
01133   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
01134                                     Type *OnlyIfReducedTy = nullptr);
01135   static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
01136                                    Type *OnlyIfReducedTy = nullptr);
01137   static Constant *getInsertValue(Constant *Agg, Constant *Val,
01138                                   ArrayRef<unsigned> Idxs,
01139                                   Type *OnlyIfReducedTy = nullptr);
01140 
01141   /// getOpcode - Return the opcode at the root of this constant expression
01142   unsigned getOpcode() const { return getSubclassDataFromValue(); }
01143 
01144   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
01145   /// not an ICMP or FCMP constant expression.
01146   unsigned getPredicate() const;
01147 
01148   /// getIndices - Assert that this is an insertvalue or exactvalue
01149   /// expression and return the list of indices.
01150   ArrayRef<unsigned> getIndices() const;
01151 
01152   /// getOpcodeName - Return a string representation for an opcode.
01153   const char *getOpcodeName() const;
01154 
01155   /// getWithOperandReplaced - Return a constant expression identical to this
01156   /// one, but with the specified operand set to the specified value.
01157   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
01158 
01159   /// getWithOperands - This returns the current constant expression with the
01160   /// operands replaced with the specified values.  The specified array must
01161   /// have the same number of operands as our current one.
01162   Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
01163     return getWithOperands(Ops, getType());
01164   }
01165 
01166   /// \brief Get the current expression with the operands replaced.
01167   ///
01168   /// Return the current constant expression with the operands replaced with \c
01169   /// Ops and the type with \c Ty.  The new operands must have the same number
01170   /// as the current ones.
01171   ///
01172   /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
01173   /// gets constant-folded, the type changes, or the expression is otherwise
01174   /// canonicalized.  This parameter should almost always be \c false.
01175   Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
01176                             bool OnlyIfReduced = false) const;
01177 
01178   /// getAsInstruction - Returns an Instruction which implements the same
01179   /// operation as this ConstantExpr. The instruction is not linked to any basic
01180   /// block.
01181   ///
01182   /// A better approach to this could be to have a constructor for Instruction
01183   /// which would take a ConstantExpr parameter, but that would have spread
01184   /// implementation details of ConstantExpr outside of Constants.cpp, which
01185   /// would make it harder to remove ConstantExprs altogether.
01186   Instruction *getAsInstruction();
01187 
01188   void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
01189 
01190   /// Methods for support type inquiry through isa, cast, and dyn_cast:
01191   static inline bool classof(const Value *V) {
01192     return V->getValueID() == ConstantExprVal;
01193   }
01194 
01195 private:
01196   // Shadow Value::setValueSubclassData with a private forwarding method so that
01197   // subclasses cannot accidentally use it.
01198   void setValueSubclassData(unsigned short D) {
01199     Value::setValueSubclassData(D);
01200   }
01201 };
01202 
01203 template <>
01204 struct OperandTraits<ConstantExpr> :
01205   public VariadicOperandTraits<ConstantExpr, 1> {
01206 };
01207 
01208 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
01209 
01210 //===----------------------------------------------------------------------===//
01211 /// UndefValue - 'undef' values are things that do not have specified contents.
01212 /// These are used for a variety of purposes, including global variable
01213 /// initializers and operands to instructions.  'undef' values can occur with
01214 /// any first-class type.
01215 ///
01216 /// Undef values aren't exactly constants; if they have multiple uses, they
01217 /// can appear to have different bit patterns at each use. See
01218 /// LangRef.html#undefvalues for details.
01219 ///
01220 class UndefValue : public Constant {
01221   void *operator new(size_t, unsigned) = delete;
01222   UndefValue(const UndefValue &) = delete;
01223 
01224   friend class Constant;
01225   void destroyConstantImpl();
01226 
01227 protected:
01228   explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
01229 protected:
01230   // allocate space for exactly zero operands
01231   void *operator new(size_t s) {
01232     return User::operator new(s, 0);
01233   }
01234 public:
01235   /// get() - Static factory methods - Return an 'undef' object of the specified
01236   /// type.
01237   ///
01238   static UndefValue *get(Type *T);
01239 
01240   /// getSequentialElement - If this Undef has array or vector type, return a
01241   /// undef with the right element type.
01242   UndefValue *getSequentialElement() const;
01243 
01244   /// getStructElement - If this undef has struct type, return a undef with the
01245   /// right element type for the specified element.
01246   UndefValue *getStructElement(unsigned Elt) const;
01247 
01248   /// getElementValue - Return an undef of the right value for the specified GEP
01249   /// index.
01250   UndefValue *getElementValue(Constant *C) const;
01251 
01252   /// getElementValue - Return an undef of the right value for the specified GEP
01253   /// index.
01254   UndefValue *getElementValue(unsigned Idx) const;
01255 
01256   /// \brief Return the number of elements in the array, vector, or struct.
01257   unsigned getNumElements() const;
01258 
01259   /// Methods for support type inquiry through isa, cast, and dyn_cast:
01260   static bool classof(const Value *V) {
01261     return V->getValueID() == UndefValueVal;
01262   }
01263 };
01264 
01265 } // End llvm namespace
01266 
01267 #endif