Home | History | Annotate | Download | only in llvm
      1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 /// @file
     11 /// This file contains the declarations for the subclasses of Constant,
     12 /// which represent the different flavors of constant values that live in LLVM.
     13 /// Note that Constants are immutable (once created they never change) and are
     14 /// fully shared by structural equivalence.  This means that two structurally
     15 /// equivalent constants will always have the same address.  Constant's are
     16 /// created on demand as needed and never deleted: thus clients don't have to
     17 /// worry about the lifetime of the objects.
     18 //
     19 //===----------------------------------------------------------------------===//
     20 
     21 #ifndef LLVM_CONSTANTS_H
     22 #define LLVM_CONSTANTS_H
     23 
     24 #include "llvm/Constant.h"
     25 #include "llvm/OperandTraits.h"
     26 #include "llvm/ADT/APInt.h"
     27 #include "llvm/ADT/APFloat.h"
     28 #include "llvm/ADT/ArrayRef.h"
     29 
     30 namespace llvm {
     31 
     32 class ArrayType;
     33 class IntegerType;
     34 class StructType;
     35 class PointerType;
     36 class VectorType;
     37 
     38 template<class ConstantClass, class TypeClass, class ValType>
     39 struct ConstantCreator;
     40 template<class ConstantClass, class TypeClass>
     41 struct ConvertConstantType;
     42 
     43 //===----------------------------------------------------------------------===//
     44 /// This is the shared class of boolean and integer constants. This class
     45 /// represents both boolean and integral constants.
     46 /// @brief Class for constant integers.
     47 class ConstantInt : public Constant {
     48   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
     49   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
     50   ConstantInt(IntegerType *Ty, const APInt& V);
     51   APInt Val;
     52 protected:
     53   // allocate space for exactly zero operands
     54   void *operator new(size_t s) {
     55     return User::operator new(s, 0);
     56   }
     57 public:
     58   static ConstantInt *getTrue(LLVMContext &Context);
     59   static ConstantInt *getFalse(LLVMContext &Context);
     60   static Constant *getTrue(Type *Ty);
     61   static Constant *getFalse(Type *Ty);
     62 
     63   /// If Ty is a vector type, return a Constant with a splat of the given
     64   /// value. Otherwise return a ConstantInt for the given value.
     65   static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
     66 
     67   /// Return a ConstantInt with the specified integer value for the specified
     68   /// type. If the type is wider than 64 bits, the value will be zero-extended
     69   /// to fit the type, unless isSigned is true, in which case the value will
     70   /// be interpreted as a 64-bit signed integer and sign-extended to fit
     71   /// the type.
     72   /// @brief Get a ConstantInt for a specific value.
     73   static ConstantInt *get(IntegerType *Ty, uint64_t V,
     74                           bool isSigned = false);
     75 
     76   /// Return a ConstantInt with the specified value for the specified type. The
     77   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
     78   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
     79   /// signed value for the type Ty.
     80   /// @brief Get a ConstantInt for a specific signed value.
     81   static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
     82   static Constant *getSigned(Type *Ty, int64_t V);
     83 
     84   /// Return a ConstantInt with the specified value and an implied Type. The
     85   /// type is the integer type that corresponds to the bit width of the value.
     86   static ConstantInt *get(LLVMContext &Context, const APInt &V);
     87 
     88   /// Return a ConstantInt constructed from the string strStart with the given
     89   /// radix.
     90   static ConstantInt *get(IntegerType *Ty, StringRef Str,
     91                           uint8_t radix);
     92 
     93   /// If Ty is a vector type, return a Constant with a splat of the given
     94   /// value. Otherwise return a ConstantInt for the given value.
     95   static Constant *get(Type* Ty, const APInt& V);
     96 
     97   /// Return the constant as an APInt value reference. This allows clients to
     98   /// obtain a copy of the value, with all its precision in tact.
     99   /// @brief Return the constant's value.
    100   inline const APInt &getValue() const {
    101     return Val;
    102   }
    103 
    104   /// getBitWidth - Return the bitwidth of this constant.
    105   unsigned getBitWidth() const { return Val.getBitWidth(); }
    106 
    107   /// Return the constant as a 64-bit unsigned integer value after it
    108   /// has been zero extended as appropriate for the type of this constant. Note
    109   /// that this method can assert if the value does not fit in 64 bits.
    110   /// @deprecated
    111   /// @brief Return the zero extended value.
    112   inline uint64_t getZExtValue() const {
    113     return Val.getZExtValue();
    114   }
    115 
    116   /// Return the constant as a 64-bit integer value after it has been sign
    117   /// extended as appropriate for the type of this constant. Note that
    118   /// this method can assert if the value does not fit in 64 bits.
    119   /// @deprecated
    120   /// @brief Return the sign extended value.
    121   inline int64_t getSExtValue() const {
    122     return Val.getSExtValue();
    123   }
    124 
    125   /// A helper method that can be used to determine if the constant contained
    126   /// within is equal to a constant.  This only works for very small values,
    127   /// because this is all that can be represented with all types.
    128   /// @brief Determine if this constant's value is same as an unsigned char.
    129   bool equalsInt(uint64_t V) const {
    130     return Val == V;
    131   }
    132 
    133   /// getType - Specialize the getType() method to always return an IntegerType,
    134   /// which reduces the amount of casting needed in parts of the compiler.
    135   ///
    136   inline IntegerType *getType() const {
    137     return reinterpret_cast<IntegerType*>(Value::getType());
    138   }
    139 
    140   /// This static method returns true if the type Ty is big enough to
    141   /// represent the value V. This can be used to avoid having the get method
    142   /// assert when V is larger than Ty can represent. Note that there are two
    143   /// versions of this method, one for unsigned and one for signed integers.
    144   /// Although ConstantInt canonicalizes everything to an unsigned integer,
    145   /// the signed version avoids callers having to convert a signed quantity
    146   /// to the appropriate unsigned type before calling the method.
    147   /// @returns true if V is a valid value for type Ty
    148   /// @brief Determine if the value is in range for the given type.
    149   static bool isValueValidForType(Type *Ty, uint64_t V);
    150   static bool isValueValidForType(Type *Ty, int64_t V);
    151 
    152   bool isNegative() const { return Val.isNegative(); }
    153 
    154   /// This is just a convenience method to make client code smaller for a
    155   /// common code. It also correctly performs the comparison without the
    156   /// potential for an assertion from getZExtValue().
    157   bool isZero() const {
    158     return Val == 0;
    159   }
    160 
    161   /// This is just a convenience method to make client code smaller for a
    162   /// common case. It also correctly performs the comparison without the
    163   /// potential for an assertion from getZExtValue().
    164   /// @brief Determine if the value is one.
    165   bool isOne() const {
    166     return Val == 1;
    167   }
    168 
    169   /// This function will return true iff every bit in this constant is set
    170   /// to true.
    171   /// @returns true iff this constant's bits are all set to true.
    172   /// @brief Determine if the value is all ones.
    173   bool isAllOnesValue() const {
    174     return Val.isAllOnesValue();
    175   }
    176 
    177   /// This function will return true iff this constant represents the largest
    178   /// value that may be represented by the constant's type.
    179   /// @returns true iff this is the largest value that may be represented
    180   /// by this type.
    181   /// @brief Determine if the value is maximal.
    182   bool isMaxValue(bool isSigned) const {
    183     if (isSigned)
    184       return Val.isMaxSignedValue();
    185     else
    186       return Val.isMaxValue();
    187   }
    188 
    189   /// This function will return true iff this constant represents the smallest
    190   /// value that may be represented by this constant's type.
    191   /// @returns true if this is the smallest value that may be represented by
    192   /// this type.
    193   /// @brief Determine if the value is minimal.
    194   bool isMinValue(bool isSigned) const {
    195     if (isSigned)
    196       return Val.isMinSignedValue();
    197     else
    198       return Val.isMinValue();
    199   }
    200 
    201   /// This function will return true iff this constant represents a value with
    202   /// active bits bigger than 64 bits or a value greater than the given uint64_t
    203   /// value.
    204   /// @returns true iff this constant is greater or equal to the given number.
    205   /// @brief Determine if the value is greater or equal to the given number.
    206   bool uge(uint64_t Num) {
    207     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
    208   }
    209 
    210   /// getLimitedValue - If the value is smaller than the specified limit,
    211   /// return it, otherwise return the limit value.  This causes the value
    212   /// to saturate to the limit.
    213   /// @returns the min of the value of the constant and the specified value
    214   /// @brief Get the constant's value with a saturation limit
    215   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
    216     return Val.getLimitedValue(Limit);
    217   }
    218 
    219   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
    220   static inline bool classof(const ConstantInt *) { return true; }
    221   static bool classof(const Value *V) {
    222     return V->getValueID() == ConstantIntVal;
    223   }
    224 };
    225 
    226 
    227 //===----------------------------------------------------------------------===//
    228 /// ConstantFP - Floating Point Values [float, double]
    229 ///
    230 class ConstantFP : public Constant {
    231   APFloat Val;
    232   void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
    233   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
    234   friend class LLVMContextImpl;
    235 protected:
    236   ConstantFP(Type *Ty, const APFloat& V);
    237 protected:
    238   // allocate space for exactly zero operands
    239   void *operator new(size_t s) {
    240     return User::operator new(s, 0);
    241   }
    242 public:
    243   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
    244   /// method returns the negative zero constant for floating point or vector
    245   /// floating point types; for all other types, it returns the null value.
    246   static Constant *getZeroValueForNegation(Type *Ty);
    247 
    248   /// get() - This returns a ConstantFP, or a vector containing a splat of a
    249   /// ConstantFP, for the specified value in the specified type.  This should
    250   /// only be used for simple constant values like 2.0/1.0 etc, that are
    251   /// known-valid both as host double and as the target format.
    252   static Constant *get(Type* Ty, double V);
    253   static Constant *get(Type* Ty, StringRef Str);
    254   static ConstantFP *get(LLVMContext &Context, const APFloat &V);
    255   static ConstantFP *getNegativeZero(Type* Ty);
    256   static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
    257 
    258   /// isValueValidForType - return true if Ty is big enough to represent V.
    259   static bool isValueValidForType(Type *Ty, const APFloat &V);
    260   inline const APFloat &getValueAPF() const { return Val; }
    261 
    262   /// isZero - Return true if the value is positive or negative zero.
    263   bool isZero() const { return Val.isZero(); }
    264 
    265   /// isNegative - Return true if the sign bit is set.
    266   bool isNegative() const { return Val.isNegative(); }
    267 
    268   /// isNaN - Return true if the value is a NaN.
    269   bool isNaN() const { return Val.isNaN(); }
    270 
    271   /// isExactlyValue - We don't rely on operator== working on double values, as
    272   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
    273   /// As such, this method can be used to do an exact bit-for-bit comparison of
    274   /// two floating point values.  The version with a double operand is retained
    275   /// because it's so convenient to write isExactlyValue(2.0), but please use
    276   /// it only for simple constants.
    277   bool isExactlyValue(const APFloat &V) const;
    278 
    279   bool isExactlyValue(double V) const {
    280     bool ignored;
    281     // convert is not supported on this type
    282     if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
    283       return false;
    284     APFloat FV(V);
    285     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
    286     return isExactlyValue(FV);
    287   }
    288   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    289   static inline bool classof(const ConstantFP *) { return true; }
    290   static bool classof(const Value *V) {
    291     return V->getValueID() == ConstantFPVal;
    292   }
    293 };
    294 
    295 //===----------------------------------------------------------------------===//
    296 /// ConstantAggregateZero - All zero aggregate value
    297 ///
    298 class ConstantAggregateZero : public Constant {
    299   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
    300   void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
    301   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
    302 protected:
    303   explicit ConstantAggregateZero(Type *ty)
    304     : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
    305 protected:
    306   // allocate space for exactly zero operands
    307   void *operator new(size_t s) {
    308     return User::operator new(s, 0);
    309   }
    310 public:
    311   static ConstantAggregateZero* get(Type *Ty);
    312 
    313   virtual void destroyConstant();
    314 
    315   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    316   ///
    317   static bool classof(const ConstantAggregateZero *) { return true; }
    318   static bool classof(const Value *V) {
    319     return V->getValueID() == ConstantAggregateZeroVal;
    320   }
    321 };
    322 
    323 
    324 //===----------------------------------------------------------------------===//
    325 /// ConstantArray - Constant Array Declarations
    326 ///
    327 class ConstantArray : public Constant {
    328   friend struct ConstantCreator<ConstantArray, ArrayType,
    329                                     std::vector<Constant*> >;
    330   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
    331 protected:
    332   ConstantArray(ArrayType *T, const std::vector<Constant*> &Val);
    333 public:
    334   // ConstantArray accessors
    335   static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
    336 
    337   /// This method constructs a ConstantArray and initializes it with a text
    338   /// string. The default behavior (AddNull==true) causes a null terminator to
    339   /// be placed at the end of the array. This effectively increases the length
    340   /// of the array by one (you've been warned).  However, in some situations
    341   /// this is not desired so if AddNull==false then the string is copied without
    342   /// null termination.
    343   static Constant *get(LLVMContext &Context, StringRef Initializer,
    344                        bool AddNull = true);
    345 
    346   /// Transparently provide more efficient getOperand methods.
    347   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    348 
    349   /// getType - Specialize the getType() method to always return an ArrayType,
    350   /// which reduces the amount of casting needed in parts of the compiler.
    351   ///
    352   inline ArrayType *getType() const {
    353     return reinterpret_cast<ArrayType*>(Value::getType());
    354   }
    355 
    356   /// isString - This method returns true if the array is an array of i8 and
    357   /// the elements of the array are all ConstantInt's.
    358   bool isString() const;
    359 
    360   /// isCString - This method returns true if the array is a string (see
    361   /// @verbatim
    362   /// isString) and it ends in a null byte \0 and does not contains any other
    363   /// @endverbatim
    364   /// null bytes except its terminator.
    365   bool isCString() const;
    366 
    367   /// getAsString - If this array is isString(), then this method converts the
    368   /// array to an std::string and returns it.  Otherwise, it asserts out.
    369   ///
    370   std::string getAsString() const;
    371 
    372   /// getAsCString - If this array is isCString(), then this method converts the
    373   /// array (without the trailing null byte) to an std::string and returns it.
    374   /// Otherwise, it asserts out.
    375   ///
    376   std::string getAsCString() const;
    377 
    378   virtual void destroyConstant();
    379   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    380 
    381   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    382   static inline bool classof(const ConstantArray *) { return true; }
    383   static bool classof(const Value *V) {
    384     return V->getValueID() == ConstantArrayVal;
    385   }
    386 };
    387 
    388 template <>
    389 struct OperandTraits<ConstantArray> :
    390   public VariadicOperandTraits<ConstantArray> {
    391 };
    392 
    393 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
    394 
    395 //===----------------------------------------------------------------------===//
    396 // ConstantStruct - Constant Struct Declarations
    397 //
    398 class ConstantStruct : public Constant {
    399   friend struct ConstantCreator<ConstantStruct, StructType,
    400                                     std::vector<Constant*> >;
    401   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
    402 protected:
    403   ConstantStruct(StructType *T, const std::vector<Constant*> &Val);
    404 public:
    405   // ConstantStruct accessors
    406   static Constant *get(StructType *T, ArrayRef<Constant*> V);
    407   static Constant *get(StructType *T, ...) END_WITH_NULL;
    408 
    409   /// getAnon - Return an anonymous struct that has the specified
    410   /// elements.  If the struct is possibly empty, then you must specify a
    411   /// context.
    412   static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
    413     return get(getTypeForElements(V, Packed), V);
    414   }
    415   static Constant *getAnon(LLVMContext &Ctx,
    416                            ArrayRef<Constant*> V, bool Packed = false) {
    417     return get(getTypeForElements(Ctx, V, Packed), V);
    418   }
    419 
    420   /// getTypeForElements - Return an anonymous struct type to use for a constant
    421   /// with the specified set of elements.  The list must not be empty.
    422   static StructType *getTypeForElements(ArrayRef<Constant*> V,
    423                                         bool Packed = false);
    424   /// getTypeForElements - This version of the method allows an empty list.
    425   static StructType *getTypeForElements(LLVMContext &Ctx,
    426                                         ArrayRef<Constant*> V,
    427                                         bool Packed = false);
    428 
    429   /// Transparently provide more efficient getOperand methods.
    430   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    431 
    432   /// getType() specialization - Reduce amount of casting...
    433   ///
    434   inline StructType *getType() const {
    435     return reinterpret_cast<StructType*>(Value::getType());
    436   }
    437 
    438   virtual void destroyConstant();
    439   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    440 
    441   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    442   static inline bool classof(const ConstantStruct *) { return true; }
    443   static bool classof(const Value *V) {
    444     return V->getValueID() == ConstantStructVal;
    445   }
    446 };
    447 
    448 template <>
    449 struct OperandTraits<ConstantStruct> :
    450   public VariadicOperandTraits<ConstantStruct> {
    451 };
    452 
    453 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
    454 
    455 
    456 //===----------------------------------------------------------------------===//
    457 /// ConstantVector - Constant Vector Declarations
    458 ///
    459 class ConstantVector : public Constant {
    460   friend struct ConstantCreator<ConstantVector, VectorType,
    461                                     std::vector<Constant*> >;
    462   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
    463 protected:
    464   ConstantVector(VectorType *T, const std::vector<Constant*> &Val);
    465 public:
    466   // ConstantVector accessors
    467   static Constant *get(ArrayRef<Constant*> V);
    468 
    469   /// Transparently provide more efficient getOperand methods.
    470   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    471 
    472   /// getType - Specialize the getType() method to always return a VectorType,
    473   /// which reduces the amount of casting needed in parts of the compiler.
    474   ///
    475   inline VectorType *getType() const {
    476     return reinterpret_cast<VectorType*>(Value::getType());
    477   }
    478 
    479   /// This function will return true iff every element in this vector constant
    480   /// is set to all ones.
    481   /// @returns true iff this constant's emements are all set to all ones.
    482   /// @brief Determine if the value is all ones.
    483   bool isAllOnesValue() const;
    484 
    485   /// getSplatValue - If this is a splat constant, meaning that all of the
    486   /// elements have the same value, return that value. Otherwise return NULL.
    487   Constant *getSplatValue() const;
    488 
    489   virtual void destroyConstant();
    490   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    491 
    492   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    493   static inline bool classof(const ConstantVector *) { return true; }
    494   static bool classof(const Value *V) {
    495     return V->getValueID() == ConstantVectorVal;
    496   }
    497 };
    498 
    499 template <>
    500 struct OperandTraits<ConstantVector> :
    501   public VariadicOperandTraits<ConstantVector> {
    502 };
    503 
    504 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
    505 
    506 //===----------------------------------------------------------------------===//
    507 /// ConstantPointerNull - a constant pointer value that points to null
    508 ///
    509 class ConstantPointerNull : public Constant {
    510   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
    511   void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
    512   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
    513 protected:
    514   explicit ConstantPointerNull(PointerType *T)
    515     : Constant(reinterpret_cast<Type*>(T),
    516                Value::ConstantPointerNullVal, 0, 0) {}
    517 
    518 protected:
    519   // allocate space for exactly zero operands
    520   void *operator new(size_t s) {
    521     return User::operator new(s, 0);
    522   }
    523 public:
    524   /// get() - Static factory methods - Return objects of the specified value
    525   static ConstantPointerNull *get(PointerType *T);
    526 
    527   virtual void destroyConstant();
    528 
    529   /// getType - Specialize the getType() method to always return an PointerType,
    530   /// which reduces the amount of casting needed in parts of the compiler.
    531   ///
    532   inline PointerType *getType() const {
    533     return reinterpret_cast<PointerType*>(Value::getType());
    534   }
    535 
    536   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    537   static inline bool classof(const ConstantPointerNull *) { return true; }
    538   static bool classof(const Value *V) {
    539     return V->getValueID() == ConstantPointerNullVal;
    540   }
    541 };
    542 
    543 /// BlockAddress - The address of a basic block.
    544 ///
    545 class BlockAddress : public Constant {
    546   void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
    547   void *operator new(size_t s) { return User::operator new(s, 2); }
    548   BlockAddress(Function *F, BasicBlock *BB);
    549 public:
    550   /// get - Return a BlockAddress for the specified function and basic block.
    551   static BlockAddress *get(Function *F, BasicBlock *BB);
    552 
    553   /// get - Return a BlockAddress for the specified basic block.  The basic
    554   /// block must be embedded into a function.
    555   static BlockAddress *get(BasicBlock *BB);
    556 
    557   /// Transparently provide more efficient getOperand methods.
    558   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    559 
    560   Function *getFunction() const { return (Function*)Op<0>().get(); }
    561   BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
    562 
    563   virtual void destroyConstant();
    564   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    565 
    566   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    567   static inline bool classof(const BlockAddress *) { return true; }
    568   static inline bool classof(const Value *V) {
    569     return V->getValueID() == BlockAddressVal;
    570   }
    571 };
    572 
    573 template <>
    574 struct OperandTraits<BlockAddress> :
    575   public FixedNumOperandTraits<BlockAddress, 2> {
    576 };
    577 
    578 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value)
    579 
    580 
    581 //===----------------------------------------------------------------------===//
    582 /// ConstantExpr - a constant value that is initialized with an expression using
    583 /// other constant values.
    584 ///
    585 /// This class uses the standard Instruction opcodes to define the various
    586 /// constant expressions.  The Opcode field for the ConstantExpr class is
    587 /// maintained in the Value::SubclassData field.
    588 class ConstantExpr : public Constant {
    589   friend struct ConstantCreator<ConstantExpr,Type,
    590                             std::pair<unsigned, std::vector<Constant*> > >;
    591   friend struct ConvertConstantType<ConstantExpr, Type>;
    592 
    593 protected:
    594   ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
    595     : Constant(ty, ConstantExprVal, Ops, NumOps) {
    596     // Operation type (an Instruction opcode) is stored as the SubclassData.
    597     setValueSubclassData(Opcode);
    598   }
    599 
    600 public:
    601   // Static methods to construct a ConstantExpr of different kinds.  Note that
    602   // these methods may return a object that is not an instance of the
    603   // ConstantExpr class, because they will attempt to fold the constant
    604   // expression into something simpler if possible.
    605 
    606   /// getAlignOf constant expr - computes the alignment of a type in a target
    607   /// independent way (Note: the return type is an i64).
    608   static Constant *getAlignOf(Type *Ty);
    609 
    610   /// getSizeOf constant expr - computes the (alloc) size of a type (in
    611   /// address-units, not bits) in a target independent way (Note: the return
    612   /// type is an i64).
    613   ///
    614   static Constant *getSizeOf(Type *Ty);
    615 
    616   /// getOffsetOf constant expr - computes the offset of a struct field in a
    617   /// target independent way (Note: the return type is an i64).
    618   ///
    619   static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
    620 
    621   /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
    622   /// which supports any aggregate type, and any Constant index.
    623   ///
    624   static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
    625 
    626   static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
    627   static Constant *getFNeg(Constant *C);
    628   static Constant *getNot(Constant *C);
    629   static Constant *getAdd(Constant *C1, Constant *C2,
    630                           bool HasNUW = false, bool HasNSW = false);
    631   static Constant *getFAdd(Constant *C1, Constant *C2);
    632   static Constant *getSub(Constant *C1, Constant *C2,
    633                           bool HasNUW = false, bool HasNSW = false);
    634   static Constant *getFSub(Constant *C1, Constant *C2);
    635   static Constant *getMul(Constant *C1, Constant *C2,
    636                           bool HasNUW = false, bool HasNSW = false);
    637   static Constant *getFMul(Constant *C1, Constant *C2);
    638   static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
    639   static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
    640   static Constant *getFDiv(Constant *C1, Constant *C2);
    641   static Constant *getURem(Constant *C1, Constant *C2);
    642   static Constant *getSRem(Constant *C1, Constant *C2);
    643   static Constant *getFRem(Constant *C1, Constant *C2);
    644   static Constant *getAnd(Constant *C1, Constant *C2);
    645   static Constant *getOr(Constant *C1, Constant *C2);
    646   static Constant *getXor(Constant *C1, Constant *C2);
    647   static Constant *getShl(Constant *C1, Constant *C2,
    648                           bool HasNUW = false, bool HasNSW = false);
    649   static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
    650   static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
    651   static Constant *getTrunc   (Constant *C, Type *Ty);
    652   static Constant *getSExt    (Constant *C, Type *Ty);
    653   static Constant *getZExt    (Constant *C, Type *Ty);
    654   static Constant *getFPTrunc (Constant *C, Type *Ty);
    655   static Constant *getFPExtend(Constant *C, Type *Ty);
    656   static Constant *getUIToFP  (Constant *C, Type *Ty);
    657   static Constant *getSIToFP  (Constant *C, Type *Ty);
    658   static Constant *getFPToUI  (Constant *C, Type *Ty);
    659   static Constant *getFPToSI  (Constant *C, Type *Ty);
    660   static Constant *getPtrToInt(Constant *C, Type *Ty);
    661   static Constant *getIntToPtr(Constant *C, Type *Ty);
    662   static Constant *getBitCast (Constant *C, Type *Ty);
    663 
    664   static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
    665   static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
    666   static Constant *getNSWAdd(Constant *C1, Constant *C2) {
    667     return getAdd(C1, C2, false, true);
    668   }
    669   static Constant *getNUWAdd(Constant *C1, Constant *C2) {
    670     return getAdd(C1, C2, true, false);
    671   }
    672   static Constant *getNSWSub(Constant *C1, Constant *C2) {
    673     return getSub(C1, C2, false, true);
    674   }
    675   static Constant *getNUWSub(Constant *C1, Constant *C2) {
    676     return getSub(C1, C2, true, false);
    677   }
    678   static Constant *getNSWMul(Constant *C1, Constant *C2) {
    679     return getMul(C1, C2, false, true);
    680   }
    681   static Constant *getNUWMul(Constant *C1, Constant *C2) {
    682     return getMul(C1, C2, true, false);
    683   }
    684   static Constant *getNSWShl(Constant *C1, Constant *C2) {
    685     return getShl(C1, C2, false, true);
    686   }
    687   static Constant *getNUWShl(Constant *C1, Constant *C2) {
    688     return getShl(C1, C2, true, false);
    689   }
    690   static Constant *getExactSDiv(Constant *C1, Constant *C2) {
    691     return getSDiv(C1, C2, true);
    692   }
    693   static Constant *getExactUDiv(Constant *C1, Constant *C2) {
    694     return getUDiv(C1, C2, true);
    695   }
    696   static Constant *getExactAShr(Constant *C1, Constant *C2) {
    697     return getAShr(C1, C2, true);
    698   }
    699   static Constant *getExactLShr(Constant *C1, Constant *C2) {
    700     return getLShr(C1, C2, true);
    701   }
    702 
    703   /// Transparently provide more efficient getOperand methods.
    704   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    705 
    706   // @brief Convenience function for getting one of the casting operations
    707   // using a CastOps opcode.
    708   static Constant *getCast(
    709     unsigned ops,  ///< The opcode for the conversion
    710     Constant *C,   ///< The constant to be converted
    711     Type *Ty ///< The type to which the constant is converted
    712   );
    713 
    714   // @brief Create a ZExt or BitCast cast constant expression
    715   static Constant *getZExtOrBitCast(
    716     Constant *C,   ///< The constant to zext or bitcast
    717     Type *Ty ///< The type to zext or bitcast C to
    718   );
    719 
    720   // @brief Create a SExt or BitCast cast constant expression
    721   static Constant *getSExtOrBitCast(
    722     Constant *C,   ///< The constant to sext or bitcast
    723     Type *Ty ///< The type to sext or bitcast C to
    724   );
    725 
    726   // @brief Create a Trunc or BitCast cast constant expression
    727   static Constant *getTruncOrBitCast(
    728     Constant *C,   ///< The constant to trunc or bitcast
    729     Type *Ty ///< The type to trunc or bitcast C to
    730   );
    731 
    732   /// @brief Create a BitCast or a PtrToInt cast constant expression
    733   static Constant *getPointerCast(
    734     Constant *C,   ///< The pointer value to be casted (operand 0)
    735     Type *Ty ///< The type to which cast should be made
    736   );
    737 
    738   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
    739   static Constant *getIntegerCast(
    740     Constant *C,    ///< The integer constant to be casted
    741     Type *Ty, ///< The integer type to cast to
    742     bool isSigned   ///< Whether C should be treated as signed or not
    743   );
    744 
    745   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
    746   static Constant *getFPCast(
    747     Constant *C,    ///< The integer constant to be casted
    748     Type *Ty ///< The integer type to cast to
    749   );
    750 
    751   /// @brief Return true if this is a convert constant expression
    752   bool isCast() const;
    753 
    754   /// @brief Return true if this is a compare constant expression
    755   bool isCompare() const;
    756 
    757   /// @brief Return true if this is an insertvalue or extractvalue expression,
    758   /// and the getIndices() method may be used.
    759   bool hasIndices() const;
    760 
    761   /// @brief Return true if this is a getelementptr expression and all
    762   /// the index operands are compile-time known integers within the
    763   /// corresponding notional static array extents. Note that this is
    764   /// not equivalant to, a subset of, or a superset of the "inbounds"
    765   /// property.
    766   bool isGEPWithNoNotionalOverIndexing() const;
    767 
    768   /// Select constant expr
    769   ///
    770   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
    771 
    772   /// get - Return a binary or shift operator constant expression,
    773   /// folding if possible.
    774   ///
    775   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
    776                        unsigned Flags = 0);
    777 
    778   /// @brief Return an ICmp or FCmp comparison operator constant expression.
    779   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
    780 
    781   /// get* - Return some common constants without having to
    782   /// specify the full Instruction::OPCODE identifier.
    783   ///
    784   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
    785   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
    786 
    787   /// Getelementptr form.  Value* is only accepted for convenience;
    788   /// all elements must be Constant's.
    789   ///
    790   static Constant *getGetElementPtr(Constant *C,
    791                                     Constant *const *IdxList, unsigned NumIdx,
    792                                     bool InBounds = false) {
    793     return getGetElementPtr(C, (Value**)IdxList, NumIdx, InBounds);
    794   }
    795   static Constant *getGetElementPtr(Constant *C,
    796                                     Value *const *IdxList, unsigned NumIdx,
    797                                     bool InBounds = false);
    798 
    799   /// Create an "inbounds" getelementptr. See the documentation for the
    800   /// "inbounds" flag in LangRef.html for details.
    801   static Constant *getInBoundsGetElementPtr(Constant *C,
    802                                             Constant *const *IdxList,
    803                                             unsigned NumIdx) {
    804     return getGetElementPtr(C, IdxList, NumIdx, true);
    805   }
    806   static Constant *getInBoundsGetElementPtr(Constant *C,
    807                                             Value* const *IdxList,
    808                                             unsigned NumIdx) {
    809     return getGetElementPtr(C, IdxList, NumIdx, true);
    810   }
    811 
    812   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
    813   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
    814   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
    815   static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
    816   static Constant *getInsertValue(Constant *Agg, Constant *Val,
    817                                   ArrayRef<unsigned> Idxs);
    818 
    819   /// getOpcode - Return the opcode at the root of this constant expression
    820   unsigned getOpcode() const { return getSubclassDataFromValue(); }
    821 
    822   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
    823   /// not an ICMP or FCMP constant expression.
    824   unsigned getPredicate() const;
    825 
    826   /// getIndices - Assert that this is an insertvalue or exactvalue
    827   /// expression and return the list of indices.
    828   ArrayRef<unsigned> getIndices() const;
    829 
    830   /// getOpcodeName - Return a string representation for an opcode.
    831   const char *getOpcodeName() const;
    832 
    833   /// getWithOperandReplaced - Return a constant expression identical to this
    834   /// one, but with the specified operand set to the specified value.
    835   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
    836 
    837   /// getWithOperands - This returns the current constant expression with the
    838   /// operands replaced with the specified values.  The specified array must
    839   /// have the same number of operands as our current one.
    840   Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
    841     return getWithOperands(Ops, getType());
    842   }
    843 
    844   /// getWithOperands - This returns the current constant expression with the
    845   /// operands replaced with the specified values and with the specified result
    846   /// type.  The specified array must have the same number of operands as our
    847   /// current one.
    848   Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
    849 
    850   virtual void destroyConstant();
    851   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    852 
    853   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    854   static inline bool classof(const ConstantExpr *) { return true; }
    855   static inline bool classof(const Value *V) {
    856     return V->getValueID() == ConstantExprVal;
    857   }
    858 
    859 private:
    860   // Shadow Value::setValueSubclassData with a private forwarding method so that
    861   // subclasses cannot accidentally use it.
    862   void setValueSubclassData(unsigned short D) {
    863     Value::setValueSubclassData(D);
    864   }
    865 };
    866 
    867 template <>
    868 struct OperandTraits<ConstantExpr> :
    869   public VariadicOperandTraits<ConstantExpr, 1> {
    870 };
    871 
    872 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
    873 
    874 //===----------------------------------------------------------------------===//
    875 /// UndefValue - 'undef' values are things that do not have specified contents.
    876 /// These are used for a variety of purposes, including global variable
    877 /// initializers and operands to instructions.  'undef' values can occur with
    878 /// any first-class type.
    879 ///
    880 /// Undef values aren't exactly constants; if they have multiple uses, they
    881 /// can appear to have different bit patterns at each use. See
    882 /// LangRef.html#undefvalues for details.
    883 ///
    884 class UndefValue : public Constant {
    885   friend struct ConstantCreator<UndefValue, Type, char>;
    886   void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
    887   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
    888 protected:
    889   explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
    890 protected:
    891   // allocate space for exactly zero operands
    892   void *operator new(size_t s) {
    893     return User::operator new(s, 0);
    894   }
    895 public:
    896   /// get() - Static factory methods - Return an 'undef' object of the specified
    897   /// type.
    898   ///
    899   static UndefValue *get(Type *T);
    900 
    901   virtual void destroyConstant();
    902 
    903   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    904   static inline bool classof(const UndefValue *) { return true; }
    905   static bool classof(const Value *V) {
    906     return V->getValueID() == UndefValueVal;
    907   }
    908 };
    909 
    910 } // End llvm namespace
    911 
    912 #endif
    913