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