LLVM API Documentation

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.  Constant's 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_CONSTANTS_H
00022 #define LLVM_CONSTANTS_H
00023 
00024 #include "llvm/Constant.h"
00025 #include "llvm/Type.h"
00026 #include "llvm/OperandTraits.h"
00027 #include "llvm/ADT/APInt.h"
00028 #include "llvm/ADT/APFloat.h"
00029 #include "llvm/ADT/SmallVector.h"
00030 
00031 namespace llvm {
00032 
00033 class ArrayType;
00034 class StructType;
00035 class PointerType;
00036 class VectorType;
00037 
00038 template<class ConstantClass, class TypeClass, class ValType>
00039 struct ConstantCreator;
00040 template<class ConstantClass, class TypeClass>
00041 struct ConvertConstant;
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   static ConstantInt *TheTrueVal, *TheFalseVal;
00049   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
00050   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
00051   ConstantInt(const IntegerType *Ty, const APInt& V);
00052   APInt Val;
00053 protected:
00054   // allocate space for exactly zero operands
00055   void *operator new(size_t s) {
00056     return User::operator new(s, 0);
00057   }
00058 public:
00059   static ConstantInt* getTrue(LLVMContext &Context);
00060   static ConstantInt* getFalse(LLVMContext &Context);
00061   
00062   /// If Ty is a vector type, return a Constant with a splat of the given
00063   /// value. Otherwise return a ConstantInt for the given value.
00064   static Constant* get(const Type* Ty, uint64_t V, bool isSigned = false);
00065                               
00066   /// Return a ConstantInt with the specified integer value for the specified
00067   /// type. If the type is wider than 64 bits, the value will be zero-extended
00068   /// to fit the type, unless isSigned is true, in which case the value will
00069   /// be interpreted as a 64-bit signed integer and sign-extended to fit
00070   /// the type.
00071   /// @brief Get a ConstantInt for a specific value.
00072   static ConstantInt* get(const IntegerType* Ty, uint64_t V,
00073                           bool isSigned = false);
00074 
00075   /// Return a ConstantInt with the specified value for the specified type. The
00076   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
00077   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
00078   /// signed value for the type Ty.
00079   /// @brief Get a ConstantInt for a specific signed value.
00080   static ConstantInt* getSigned(const IntegerType* Ty, int64_t V);
00081   static Constant *getSigned(const Type *Ty, int64_t V);
00082   
00083   /// Return a ConstantInt with the specified value and an implied Type. The
00084   /// type is the integer type that corresponds to the bit width of the value.
00085   static ConstantInt* get(LLVMContext &Context, const APInt& V);
00086 
00087   /// Return a ConstantInt constructed from the string strStart with the given
00088   /// radix. 
00089   static ConstantInt* get(const IntegerType* Ty, const StringRef& Str,
00090                           uint8_t radix);
00091   
00092   /// If Ty is a vector type, return a Constant with a splat of the given
00093   /// value. Otherwise return a ConstantInt for the given value.
00094   static Constant* get(const Type* Ty, const APInt& V);
00095   
00096   /// Return the constant as an APInt value reference. This allows clients to
00097   /// obtain a copy of the value, with all its precision in tact.
00098   /// @brief Return the constant's value.
00099   inline const APInt& getValue() const {
00100     return Val;
00101   }
00102   
00103   /// getBitWidth - Return the bitwidth of this constant.
00104   unsigned getBitWidth() const { return Val.getBitWidth(); }
00105 
00106   /// Return the constant as a 64-bit unsigned integer value after it
00107   /// has been zero extended as appropriate for the type of this constant. Note
00108   /// that this method can assert if the value does not fit in 64 bits.
00109   /// @deprecated
00110   /// @brief Return the zero extended value.
00111   inline uint64_t getZExtValue() const {
00112     return Val.getZExtValue();
00113   }
00114 
00115   /// Return the constant as a 64-bit integer value after it has been sign
00116   /// extended as appropriate for the type of this constant. Note that
00117   /// this method can assert if the value does not fit in 64 bits.
00118   /// @deprecated
00119   /// @brief Return the sign extended value.
00120   inline int64_t getSExtValue() const {
00121     return Val.getSExtValue();
00122   }
00123 
00124   /// A helper method that can be used to determine if the constant contained 
00125   /// within is equal to a constant.  This only works for very small values, 
00126   /// because this is all that can be represented with all types.
00127   /// @brief Determine if this constant's value is same as an unsigned char.
00128   bool equalsInt(uint64_t V) const {
00129     return Val == V;
00130   }
00131 
00132   /// getType - Specialize the getType() method to always return an IntegerType,
00133   /// which reduces the amount of casting needed in parts of the compiler.
00134   ///
00135   inline const IntegerType *getType() const {
00136     return reinterpret_cast<const IntegerType*>(Value::getType());
00137   }
00138 
00139   /// This static method returns true if the type Ty is big enough to 
00140   /// represent the value V. This can be used to avoid having the get method 
00141   /// assert when V is larger than Ty can represent. Note that there are two
00142   /// versions of this method, one for unsigned and one for signed integers.
00143   /// Although ConstantInt canonicalizes everything to an unsigned integer, 
00144   /// the signed version avoids callers having to convert a signed quantity
00145   /// to the appropriate unsigned type before calling the method.
00146   /// @returns true if V is a valid value for type Ty
00147   /// @brief Determine if the value is in range for the given type.
00148   static bool isValueValidForType(const Type *Ty, uint64_t V);
00149   static bool isValueValidForType(const Type *Ty, int64_t V);
00150 
00151   /// This function will return true iff this constant represents the "null"
00152   /// value that would be returned by the getNullValue method.
00153   /// @returns true if this is the null integer value.
00154   /// @brief Determine if the value is null.
00155   virtual bool isNullValue() const { 
00156     return Val == 0; 
00157   }
00158 
00159   /// This is just a convenience method to make client code smaller for a
00160   /// common code. It also correctly performs the comparison without the
00161   /// potential for an assertion from getZExtValue().
00162   bool isZero() const {
00163     return Val == 0;
00164   }
00165 
00166   /// This is just a convenience method to make client code smaller for a 
00167   /// common case. It also correctly performs the comparison without the
00168   /// potential for an assertion from getZExtValue().
00169   /// @brief Determine if the value is one.
00170   bool isOne() const {
00171     return Val == 1;
00172   }
00173 
00174   /// This function will return true iff every bit in this constant is set
00175   /// to true.
00176   /// @returns true iff this constant's bits are all set to true.
00177   /// @brief Determine if the value is all ones.
00178   bool isAllOnesValue() const { 
00179     return Val.isAllOnesValue();
00180   }
00181 
00182   /// This function will return true iff this constant represents the largest
00183   /// value that may be represented by the constant's type.
00184   /// @returns true iff this is the largest value that may be represented 
00185   /// by this type.
00186   /// @brief Determine if the value is maximal.
00187   bool isMaxValue(bool isSigned) const {
00188     if (isSigned) 
00189       return Val.isMaxSignedValue();
00190     else
00191       return Val.isMaxValue();
00192   }
00193 
00194   /// This function will return true iff this constant represents the smallest
00195   /// value that may be represented by this constant's type.
00196   /// @returns true if this is the smallest value that may be represented by 
00197   /// this type.
00198   /// @brief Determine if the value is minimal.
00199   bool isMinValue(bool isSigned) const {
00200     if (isSigned) 
00201       return Val.isMinSignedValue();
00202     else
00203       return Val.isMinValue();
00204   }
00205 
00206   /// This function will return true iff this constant represents a value with
00207   /// active bits bigger than 64 bits or a value greater than the given uint64_t
00208   /// value.
00209   /// @returns true iff this constant is greater or equal to the given number.
00210   /// @brief Determine if the value is greater or equal to the given number.
00211   bool uge(uint64_t Num) {
00212     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
00213   }
00214 
00215   /// getLimitedValue - If the value is smaller than the specified limit,
00216   /// return it, otherwise return the limit value.  This causes the value
00217   /// to saturate to the limit.
00218   /// @returns the min of the value of the constant and the specified value
00219   /// @brief Get the constant's value with a saturation limit
00220   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
00221     return Val.getLimitedValue(Limit);
00222   }
00223 
00224   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
00225   static inline bool classof(const ConstantInt *) { return true; }
00226   static bool classof(const Value *V) {
00227     return V->getValueID() == ConstantIntVal;
00228   }
00229 };
00230 
00231 
00232 //===----------------------------------------------------------------------===//
00233 /// ConstantFP - Floating Point Values [float, double]
00234 ///
00235 class ConstantFP : public Constant {
00236   APFloat Val;
00237   void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
00238   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
00239   friend class LLVMContextImpl;
00240 protected:
00241   ConstantFP(const Type *Ty, const APFloat& V);
00242 protected:
00243   // allocate space for exactly zero operands
00244   void *operator new(size_t s) {
00245     return User::operator new(s, 0);
00246   }
00247 public:
00248   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
00249   /// method returns the negative zero constant for floating point or vector
00250   /// floating point types; for all other types, it returns the null value.
00251   static Constant* getZeroValueForNegation(const Type* Ty);
00252   
00253   /// get() - This returns a ConstantFP, or a vector containing a splat of a
00254   /// ConstantFP, for the specified value in the specified type.  This should
00255   /// only be used for simple constant values like 2.0/1.0 etc, that are
00256   /// known-valid both as host double and as the target format.
00257   static Constant* get(const Type* Ty, double V);
00258   static Constant* get(const Type* Ty, const StringRef& Str);
00259   static ConstantFP* get(LLVMContext &Context, const APFloat& V);
00260   static ConstantFP* getNegativeZero(const Type* Ty);
00261   
00262   /// isValueValidForType - return true if Ty is big enough to represent V.
00263   static bool isValueValidForType(const Type *Ty, const APFloat& V);
00264   inline const APFloat& getValueAPF() const { return Val; }
00265 
00266   /// isNullValue - Return true if this is the value that would be returned by
00267   /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
00268   /// considers -0.0 to be null as well as 0.0.  :(
00269   virtual bool isNullValue() const;
00270   
00271   /// isNegativeZeroValue - Return true if the value is what would be returned 
00272   /// by getZeroValueForNegation.
00273   virtual bool isNegativeZeroValue() const {
00274     return Val.isZero() && Val.isNegative();
00275   }
00276 
00277   /// isExactlyValue - We don't rely on operator== working on double values, as
00278   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
00279   /// As such, this method can be used to do an exact bit-for-bit comparison of
00280   /// two floating point values.  The version with a double operand is retained
00281   /// because it's so convenient to write isExactlyValue(2.0), but please use
00282   /// it only for simple constants.
00283   bool isExactlyValue(const APFloat& V) const;
00284 
00285   bool isExactlyValue(double V) const {
00286     bool ignored;
00287     // convert is not supported on this type
00288     if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
00289       return false;
00290     APFloat FV(V);
00291     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
00292     return isExactlyValue(FV);
00293   }
00294   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00295   static inline bool classof(const ConstantFP *) { return true; }
00296   static bool classof(const Value *V) {
00297     return V->getValueID() == ConstantFPVal;
00298   }
00299 };
00300 
00301 //===----------------------------------------------------------------------===//
00302 /// ConstantAggregateZero - All zero aggregate value
00303 ///
00304 class ConstantAggregateZero : public Constant {
00305   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
00306   void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
00307   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
00308 protected:
00309   explicit ConstantAggregateZero(const Type *ty)
00310     : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
00311 protected:
00312   // allocate space for exactly zero operands
00313   void *operator new(size_t s) {
00314     return User::operator new(s, 0);
00315   }
00316 public:
00317   static ConstantAggregateZero* get(const Type* Ty);
00318   
00319   /// isNullValue - Return true if this is the value that would be returned by
00320   /// getNullValue.
00321   virtual bool isNullValue() const { return true; }
00322 
00323   virtual void destroyConstant();
00324 
00325   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00326   ///
00327   static bool classof(const ConstantAggregateZero *) { return true; }
00328   static bool classof(const Value *V) {
00329     return V->getValueID() == ConstantAggregateZeroVal;
00330   }
00331 };
00332 
00333 
00334 //===----------------------------------------------------------------------===//
00335 /// ConstantArray - Constant Array Declarations
00336 ///
00337 class ConstantArray : public Constant {
00338   friend struct ConstantCreator<ConstantArray, ArrayType,
00339                                     std::vector<Constant*> >;
00340   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
00341 protected:
00342   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
00343 public:
00344   // ConstantArray accessors
00345   static Constant* get(const ArrayType* T, const std::vector<Constant*>& V);
00346   static Constant* get(const ArrayType* T, Constant* const* Vals, 
00347                        unsigned NumVals);
00348                              
00349   /// This method constructs a ConstantArray and initializes it with a text
00350   /// string. The default behavior (AddNull==true) causes a null terminator to
00351   /// be placed at the end of the array. This effectively increases the length
00352   /// of the array by one (you've been warned).  However, in some situations 
00353   /// this is not desired so if AddNull==false then the string is copied without
00354   /// null termination.
00355   static Constant* get(LLVMContext &Context, const StringRef &Initializer,
00356                        bool AddNull = true);
00357   
00358   /// Transparently provide more efficient getOperand methods.
00359   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00360 
00361   /// getType - Specialize the getType() method to always return an ArrayType,
00362   /// which reduces the amount of casting needed in parts of the compiler.
00363   ///
00364   inline const ArrayType *getType() const {
00365     return reinterpret_cast<const ArrayType*>(Value::getType());
00366   }
00367 
00368   /// isString - This method returns true if the array is an array of i8 and
00369   /// the elements of the array are all ConstantInt's.
00370   bool isString() const;
00371 
00372   /// isCString - This method returns true if the array is a string (see
00373   /// @verbatim
00374   /// isString) and it ends in a null byte \0 and does not contains any other
00375   /// @endverbatim
00376   /// null bytes except its terminator.
00377   bool isCString() const;
00378 
00379   /// getAsString - If this array is isString(), then this method converts the
00380   /// array to an std::string and returns it.  Otherwise, it asserts out.
00381   ///
00382   std::string getAsString() const;
00383 
00384   /// isNullValue - Return true if this is the value that would be returned by
00385   /// getNullValue.  This always returns false because zero arrays are always
00386   /// created as ConstantAggregateZero objects.
00387   virtual bool isNullValue() const { return false; }
00388 
00389   virtual void destroyConstant();
00390   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00391 
00392   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00393   static inline bool classof(const ConstantArray *) { return true; }
00394   static bool classof(const Value *V) {
00395     return V->getValueID() == ConstantArrayVal;
00396   }
00397 };
00398 
00399 template <>
00400 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
00401 };
00402 
00403 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
00404 
00405 //===----------------------------------------------------------------------===//
00406 // ConstantStruct - Constant Struct Declarations
00407 //
00408 class ConstantStruct : public Constant {
00409   friend struct ConstantCreator<ConstantStruct, StructType,
00410                                     std::vector<Constant*> >;
00411   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
00412 protected:
00413   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
00414 public:
00415   // ConstantStruct accessors
00416   static Constant* get(const StructType* T, const std::vector<Constant*>& V);
00417   static Constant* get(LLVMContext &Context, 
00418                        const std::vector<Constant*>& V, bool Packed = false);
00419   static Constant* get(LLVMContext &Context,
00420                        Constant* const *Vals, unsigned NumVals,
00421                        bool Packed = false);
00422   
00423   /// Transparently provide more efficient getOperand methods.
00424   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00425 
00426   /// getType() specialization - Reduce amount of casting...
00427   ///
00428   inline const StructType *getType() const {
00429     return reinterpret_cast<const StructType*>(Value::getType());
00430   }
00431 
00432   /// isNullValue - Return true if this is the value that would be returned by
00433   /// getNullValue.  This always returns false because zero structs are always
00434   /// created as ConstantAggregateZero objects.
00435   virtual bool isNullValue() const {
00436     return false;
00437   }
00438 
00439   virtual void destroyConstant();
00440   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00441 
00442   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00443   static inline bool classof(const ConstantStruct *) { return true; }
00444   static bool classof(const Value *V) {
00445     return V->getValueID() == ConstantStructVal;
00446   }
00447 };
00448 
00449 template <>
00450 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
00451 };
00452 
00453 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
00454 
00455 //===----------------------------------------------------------------------===//
00456 /// ConstantVector - Constant Vector Declarations
00457 ///
00458 class ConstantVector : public Constant {
00459   friend struct ConstantCreator<ConstantVector, VectorType,
00460                                     std::vector<Constant*> >;
00461   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
00462 protected:
00463   ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
00464 public:
00465   // ConstantVector accessors
00466   static Constant* get(const VectorType* T, const std::vector<Constant*>& V);
00467   static Constant* get(const std::vector<Constant*>& V);
00468   static Constant* get(Constant* const* Vals, unsigned NumVals);
00469   
00470   /// Transparently provide more efficient getOperand methods.
00471   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00472 
00473   /// getType - Specialize the getType() method to always return a VectorType,
00474   /// which reduces the amount of casting needed in parts of the compiler.
00475   ///
00476   inline const VectorType *getType() const {
00477     return reinterpret_cast<const VectorType*>(Value::getType());
00478   }
00479   
00480   /// isNullValue - Return true if this is the value that would be returned by
00481   /// getNullValue.  This always returns false because zero vectors are always
00482   /// created as ConstantAggregateZero objects.
00483   virtual bool isNullValue() const { return false; }
00484 
00485   /// This function will return true iff every element in this vector constant
00486   /// is set to all ones.
00487   /// @returns true iff this constant's emements are all set to all ones.
00488   /// @brief Determine if the value is all ones.
00489   bool isAllOnesValue() const;
00490 
00491   /// getSplatValue - If this is a splat constant, meaning that all of the
00492   /// elements have the same value, return that value. Otherwise return NULL.
00493   Constant *getSplatValue();
00494 
00495   virtual void destroyConstant();
00496   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00497 
00498   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00499   static inline bool classof(const ConstantVector *) { return true; }
00500   static bool classof(const Value *V) {
00501     return V->getValueID() == ConstantVectorVal;
00502   }
00503 };
00504 
00505 template <>
00506 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
00507 };
00508 
00509 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
00510 
00511 //===----------------------------------------------------------------------===//
00512 /// ConstantPointerNull - a constant pointer value that points to null
00513 ///
00514 class ConstantPointerNull : public Constant {
00515   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
00516   void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
00517   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
00518 protected:
00519   explicit ConstantPointerNull(const PointerType *T)
00520     : Constant(reinterpret_cast<const Type*>(T),
00521                Value::ConstantPointerNullVal, 0, 0) {}
00522 
00523 protected:
00524   // allocate space for exactly zero operands
00525   void *operator new(size_t s) {
00526     return User::operator new(s, 0);
00527   }
00528 public:
00529   /// get() - Static factory methods - Return objects of the specified value
00530   static ConstantPointerNull *get(const PointerType *T);
00531 
00532   /// isNullValue - Return true if this is the value that would be returned by
00533   /// getNullValue.
00534   virtual bool isNullValue() const { return true; }
00535 
00536   virtual void destroyConstant();
00537 
00538   /// getType - Specialize the getType() method to always return an PointerType,
00539   /// which reduces the amount of casting needed in parts of the compiler.
00540   ///
00541   inline const PointerType *getType() const {
00542     return reinterpret_cast<const PointerType*>(Value::getType());
00543   }
00544 
00545   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00546   static inline bool classof(const ConstantPointerNull *) { return true; }
00547   static bool classof(const Value *V) {
00548     return V->getValueID() == ConstantPointerNullVal;
00549   }
00550 };
00551 
00552 
00553 /// ConstantExpr - a constant value that is initialized with an expression using
00554 /// other constant values.
00555 ///
00556 /// This class uses the standard Instruction opcodes to define the various
00557 /// constant expressions.  The Opcode field for the ConstantExpr class is
00558 /// maintained in the Value::SubclassData field.
00559 class ConstantExpr : public Constant {
00560   friend struct ConstantCreator<ConstantExpr,Type,
00561                             std::pair<unsigned, std::vector<Constant*> > >;
00562   friend struct ConvertConstant<ConstantExpr, Type>;
00563 
00564 protected:
00565   ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
00566     : Constant(ty, ConstantExprVal, Ops, NumOps) {
00567     // Operation type (an Instruction opcode) is stored as the SubclassData.
00568     SubclassData = Opcode;
00569   }
00570 
00571   // These private methods are used by the type resolution code to create
00572   // ConstantExprs in intermediate forms.
00573   static Constant *getTy(const Type *Ty, unsigned Opcode,
00574                          Constant *C1, Constant *C2);
00575   static Constant *getCompareTy(unsigned short pred, Constant *C1,
00576                                 Constant *C2);
00577   static Constant *getSelectTy(const Type *Ty,
00578                                Constant *C1, Constant *C2, Constant *C3);
00579   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
00580                                       Value* const *Idxs, unsigned NumIdxs);
00581   static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
00582                                        Constant *Idx);
00583   static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
00584                                       Constant *Elt, Constant *Idx);
00585   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
00586                                       Constant *V2, Constant *Mask);
00587   static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
00588                                      const unsigned *Idxs, unsigned NumIdxs);
00589   static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
00590                                     Constant *Val,
00591                                     const unsigned *Idxs, unsigned NumIdxs);
00592 
00593 public:
00594   // Static methods to construct a ConstantExpr of different kinds.  Note that
00595   // these methods may return a object that is not an instance of the
00596   // ConstantExpr class, because they will attempt to fold the constant
00597   // expression into something simpler if possible.
00598 
00599   /// Cast constant expr
00600   ///
00601 
00602   /// getAlignOf constant expr - computes the alignment of a type in a target
00603   /// independent way (Note: the return type is an i32; Note: assumes that i8
00604   /// is byte aligned).
00605   static Constant* getAlignOf(const Type* Ty);
00606   
00607   /// getSizeOf constant expr - computes the size of a type in a target
00608   /// independent way (Note: the return type is an i64).
00609   ///
00610   static Constant* getSizeOf(const Type* Ty);
00611 
00612   /// getOffsetOf constant expr - computes the offset of a field in a target
00613   /// independent way (Note: the return type is an i64).
00614   ///
00615   static Constant* getOffsetOf(const StructType* Ty, unsigned FieldNo);
00616   
00617   static Constant* getNeg(Constant* C);
00618   static Constant* getFNeg(Constant* C);
00619   static Constant* getNot(Constant* C);
00620   static Constant* getAdd(Constant* C1, Constant* C2);
00621   static Constant* getFAdd(Constant* C1, Constant* C2);
00622   static Constant* getSub(Constant* C1, Constant* C2);
00623   static Constant* getFSub(Constant* C1, Constant* C2);
00624   static Constant* getMul(Constant* C1, Constant* C2);
00625   static Constant* getFMul(Constant* C1, Constant* C2);
00626   static Constant* getUDiv(Constant* C1, Constant* C2);
00627   static Constant* getSDiv(Constant* C1, Constant* C2);
00628   static Constant* getFDiv(Constant* C1, Constant* C2);
00629   static Constant* getURem(Constant* C1, Constant* C2);
00630   static Constant* getSRem(Constant* C1, Constant* C2);
00631   static Constant* getFRem(Constant* C1, Constant* C2);
00632   static Constant* getAnd(Constant* C1, Constant* C2);
00633   static Constant* getOr(Constant* C1, Constant* C2);
00634   static Constant* getXor(Constant* C1, Constant* C2);
00635   static Constant* getShl(Constant* C1, Constant* C2);
00636   static Constant* getLShr(Constant* C1, Constant* C2);
00637   static Constant* getAShr(Constant* C1, Constant* C2);
00638   static Constant *getTrunc   (Constant *C, const Type *Ty);
00639   static Constant *getSExt    (Constant *C, const Type *Ty);
00640   static Constant *getZExt    (Constant *C, const Type *Ty);
00641   static Constant *getFPTrunc (Constant *C, const Type *Ty);
00642   static Constant *getFPExtend(Constant *C, const Type *Ty);
00643   static Constant *getUIToFP  (Constant *C, const Type *Ty);
00644   static Constant *getSIToFP  (Constant *C, const Type *Ty);
00645   static Constant *getFPToUI  (Constant *C, const Type *Ty);
00646   static Constant *getFPToSI  (Constant *C, const Type *Ty);
00647   static Constant *getPtrToInt(Constant *C, const Type *Ty);
00648   static Constant *getIntToPtr(Constant *C, const Type *Ty);
00649   static Constant *getBitCast (Constant *C, const Type *Ty);
00650 
00651   static Constant* getNSWAdd(Constant* C1, Constant* C2);
00652   static Constant* getExactSDiv(Constant* C1, Constant* C2);
00653 
00654   /// Transparently provide more efficient getOperand methods.
00655   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
00656 
00657   // @brief Convenience function for getting one of the casting operations
00658   // using a CastOps opcode.
00659   static Constant *getCast(
00660     unsigned ops,  ///< The opcode for the conversion
00661     Constant *C,   ///< The constant to be converted
00662     const Type *Ty ///< The type to which the constant is converted
00663   );
00664 
00665   // @brief Create a ZExt or BitCast cast constant expression
00666   static Constant *getZExtOrBitCast(
00667     Constant *C,   ///< The constant to zext or bitcast
00668     const Type *Ty ///< The type to zext or bitcast C to
00669   );
00670 
00671   // @brief Create a SExt or BitCast cast constant expression 
00672   static Constant *getSExtOrBitCast(
00673     Constant *C,   ///< The constant to sext or bitcast
00674     const Type *Ty ///< The type to sext or bitcast C to
00675   );
00676 
00677   // @brief Create a Trunc or BitCast cast constant expression
00678   static Constant *getTruncOrBitCast(
00679     Constant *C,   ///< The constant to trunc or bitcast
00680     const Type *Ty ///< The type to trunc or bitcast C to
00681   );
00682 
00683   /// @brief Create a BitCast or a PtrToInt cast constant expression
00684   static Constant *getPointerCast(
00685     Constant *C,   ///< The pointer value to be casted (operand 0)
00686     const Type *Ty ///< The type to which cast should be made
00687   );
00688 
00689   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
00690   static Constant *getIntegerCast(
00691     Constant *C,    ///< The integer constant to be casted 
00692     const Type *Ty, ///< The integer type to cast to
00693     bool isSigned   ///< Whether C should be treated as signed or not
00694   );
00695 
00696   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
00697   static Constant *getFPCast(
00698     Constant *C,    ///< The integer constant to be casted 
00699     const Type *Ty ///< The integer type to cast to
00700   );
00701 
00702   /// @brief Return true if this is a convert constant expression
00703   bool isCast() const;
00704 
00705   /// @brief Return true if this is a compare constant expression
00706   bool isCompare() const;
00707 
00708   /// @brief Return true if this is an insertvalue or extractvalue expression,
00709   /// and the getIndices() method may be used.
00710   bool hasIndices() const;
00711 
00712   /// Select constant expr
00713   ///
00714   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
00715     return getSelectTy(V1->getType(), C, V1, V2);
00716   }
00717 
00718   /// get - Return a binary or shift operator constant expression,
00719   /// folding if possible.
00720   ///
00721   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
00722 
00723   /// @brief Return an ICmp or FCmp comparison operator constant expression.
00724   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
00725 
00726   /// get* - Return some common constants without having to
00727   /// specify the full Instruction::OPCODE identifier.
00728   ///
00729   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
00730   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
00731 
00732   /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
00733   /// all elements must be Constant's.
00734   ///
00735   static Constant *getGetElementPtr(Constant *C,
00736                                     Constant* const *IdxList, unsigned NumIdx);
00737   static Constant *getGetElementPtr(Constant *C,
00738                                     Value* const *IdxList, unsigned NumIdx);
00739 
00740   /// Create an "inbounds" getelementptr. See the documentation for the
00741   /// "inbounds" flag in LangRef.html for details.
00742   static Constant *getInBoundsGetElementPtr(Constant *C,
00743                                             Constant* const *IdxList,
00744                                             unsigned NumIdx);
00745   static Constant *getInBoundsGetElementPtr(Constant *C,
00746                                             Value* const *IdxList,
00747                                             unsigned NumIdx);
00748 
00749   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
00750   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
00751   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
00752   static Constant *getExtractValue(Constant *Agg,
00753                                    const unsigned *IdxList, unsigned NumIdx);
00754   static Constant *getInsertValue(Constant *Agg, Constant *Val,
00755                                   const unsigned *IdxList, unsigned NumIdx);
00756 
00757   /// isNullValue - Return true if this is the value that would be returned by
00758   /// getNullValue.
00759   virtual bool isNullValue() const { return false; }
00760 
00761   /// getOpcode - Return the opcode at the root of this constant expression
00762   unsigned getOpcode() const { return SubclassData; }
00763 
00764   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
00765   /// not an ICMP or FCMP constant expression.
00766   unsigned getPredicate() const;
00767 
00768   /// getIndices - Assert that this is an insertvalue or exactvalue
00769   /// expression and return the list of indices.
00770   const SmallVector<unsigned, 4> &getIndices() const;
00771 
00772   /// getOpcodeName - Return a string representation for an opcode.
00773   const char *getOpcodeName() const;
00774 
00775   /// getWithOperandReplaced - Return a constant expression identical to this
00776   /// one, but with the specified operand set to the specified value.
00777   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
00778   
00779   /// getWithOperands - This returns the current constant expression with the
00780   /// operands replaced with the specified values.  The specified operands must
00781   /// match count and type with the existing ones.
00782   Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
00783     return getWithOperands(&Ops[0], (unsigned)Ops.size());
00784   }
00785   Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
00786   
00787   virtual void destroyConstant();
00788   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
00789 
00790   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00791   static inline bool classof(const ConstantExpr *) { return true; }
00792   static inline bool classof(const Value *V) {
00793     return V->getValueID() == ConstantExprVal;
00794   }
00795 };
00796 
00797 template <>
00798 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
00799 };
00800 
00801 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
00802 
00803 //===----------------------------------------------------------------------===//
00804 /// UndefValue - 'undef' values are things that do not have specified contents.
00805 /// These are used for a variety of purposes, including global variable
00806 /// initializers and operands to instructions.  'undef' values can occur with
00807 /// any type.
00808 ///
00809 class UndefValue : public Constant {
00810   friend struct ConstantCreator<UndefValue, Type, char>;
00811   void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
00812   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
00813 protected:
00814   explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
00815 protected:
00816   // allocate space for exactly zero operands
00817   void *operator new(size_t s) {
00818     return User::operator new(s, 0);
00819   }
00820 public:
00821   /// get() - Static factory methods - Return an 'undef' object of the specified
00822   /// type.
00823   ///
00824   static UndefValue *get(const Type *T);
00825 
00826   /// isNullValue - Return true if this is the value that would be returned by
00827   /// getNullValue.
00828   virtual bool isNullValue() const { return false; }
00829 
00830   virtual void destroyConstant();
00831 
00832   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00833   static inline bool classof(const UndefValue *) { return true; }
00834   static bool classof(const Value *V) {
00835     return V->getValueID() == UndefValueVal;
00836   }
00837 };
00838 } // End llvm namespace
00839 
00840 #endif