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