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