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