1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// @file 11 /// This file contains the declarations for the subclasses of Constant, 12 /// which represent the different flavors of constant values that live in LLVM. 13 /// Note that Constants are immutable (once created they never change) and are 14 /// fully shared by structural equivalence. This means that two structurally 15 /// equivalent constants will always have the same address. Constant's are 16 /// created on demand as needed and never deleted: thus clients don't have to 17 /// worry about the lifetime of the objects. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #ifndef LLVM_IR_CONSTANTS_H 22 #define LLVM_IR_CONSTANTS_H 23 24 #include "llvm/ADT/APFloat.h" 25 #include "llvm/ADT/APInt.h" 26 #include "llvm/ADT/ArrayRef.h" 27 #include "llvm/IR/Constant.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/OperandTraits.h" 30 31 namespace llvm { 32 33 class ArrayType; 34 class IntegerType; 35 class StructType; 36 class PointerType; 37 class VectorType; 38 class SequentialType; 39 40 struct ConstantExprKeyType; 41 template <class ConstantClass> struct ConstantAggrKeyType; 42 43 //===----------------------------------------------------------------------===// 44 /// This is the shared class of boolean and integer constants. This class 45 /// represents both boolean and integral constants. 46 /// @brief Class for constant integers. 47 class ConstantInt : public Constant { 48 void anchor() override; 49 void *operator new(size_t, unsigned) = delete; 50 ConstantInt(const ConstantInt &) = delete; 51 ConstantInt(IntegerType *Ty, const APInt& V); 52 APInt Val; 53 protected: 54 // allocate space for exactly zero operands new(size_t s)55 void *operator new(size_t s) { 56 return User::operator new(s, 0); 57 } 58 public: 59 static ConstantInt *getTrue(LLVMContext &Context); 60 static ConstantInt *getFalse(LLVMContext &Context); 61 static Constant *getTrue(Type *Ty); 62 static Constant *getFalse(Type *Ty); 63 64 /// If Ty is a vector type, return a Constant with a splat of the given 65 /// value. Otherwise return a ConstantInt for the given value. 66 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false); 67 68 /// Return a ConstantInt with the specified integer value for the specified 69 /// type. If the type is wider than 64 bits, the value will be zero-extended 70 /// to fit the type, unless isSigned is true, in which case the value will 71 /// be interpreted as a 64-bit signed integer and sign-extended to fit 72 /// the type. 73 /// @brief Get a ConstantInt for a specific value. 74 static ConstantInt *get(IntegerType *Ty, uint64_t V, 75 bool isSigned = false); 76 77 /// Return a ConstantInt with the specified value for the specified type. The 78 /// value V will be canonicalized to a an unsigned APInt. Accessing it with 79 /// either getSExtValue() or getZExtValue() will yield a correctly sized and 80 /// signed value for the type Ty. 81 /// @brief Get a ConstantInt for a specific signed value. 82 static ConstantInt *getSigned(IntegerType *Ty, int64_t V); 83 static Constant *getSigned(Type *Ty, int64_t V); 84 85 /// Return a ConstantInt with the specified value and an implied Type. The 86 /// type is the integer type that corresponds to the bit width of the value. 87 static ConstantInt *get(LLVMContext &Context, const APInt &V); 88 89 /// Return a ConstantInt constructed from the string strStart with the given 90 /// radix. 91 static ConstantInt *get(IntegerType *Ty, StringRef Str, 92 uint8_t radix); 93 94 /// If Ty is a vector type, return a Constant with a splat of the given 95 /// value. Otherwise return a ConstantInt for the given value. 96 static Constant *get(Type* Ty, const APInt& V); 97 98 /// Return the constant as an APInt value reference. This allows clients to 99 /// obtain a copy of the value, with all its precision in tact. 100 /// @brief Return the constant's value. getValue()101 inline const APInt &getValue() const { 102 return Val; 103 } 104 105 /// getBitWidth - Return the bitwidth of this constant. getBitWidth()106 unsigned getBitWidth() const { return Val.getBitWidth(); } 107 108 /// Return the constant as a 64-bit unsigned integer value after it 109 /// has been zero extended as appropriate for the type of this constant. Note 110 /// that this method can assert if the value does not fit in 64 bits. 111 /// @brief Return the zero extended value. getZExtValue()112 inline uint64_t getZExtValue() const { 113 return Val.getZExtValue(); 114 } 115 116 /// Return the constant as a 64-bit integer value after it has been sign 117 /// extended as appropriate for the type of this constant. Note that 118 /// this method can assert if the value does not fit in 64 bits. 119 /// @brief Return the sign extended value. getSExtValue()120 inline int64_t getSExtValue() const { 121 return Val.getSExtValue(); 122 } 123 124 /// A helper method that can be used to determine if the constant contained 125 /// within is equal to a constant. This only works for very small values, 126 /// because this is all that can be represented with all types. 127 /// @brief Determine if this constant's value is same as an unsigned char. equalsInt(uint64_t V)128 bool equalsInt(uint64_t V) const { 129 return Val == V; 130 } 131 132 /// getType - Specialize the getType() method to always return an IntegerType, 133 /// which reduces the amount of casting needed in parts of the compiler. 134 /// getType()135 inline IntegerType *getType() const { 136 return cast<IntegerType>(Value::getType()); 137 } 138 139 /// This static method returns true if the type Ty is big enough to 140 /// represent the value V. This can be used to avoid having the get method 141 /// assert when V is larger than Ty can represent. Note that there are two 142 /// versions of this method, one for unsigned and one for signed integers. 143 /// Although ConstantInt canonicalizes everything to an unsigned integer, 144 /// the signed version avoids callers having to convert a signed quantity 145 /// to the appropriate unsigned type before calling the method. 146 /// @returns true if V is a valid value for type Ty 147 /// @brief Determine if the value is in range for the given type. 148 static bool isValueValidForType(Type *Ty, uint64_t V); 149 static bool isValueValidForType(Type *Ty, int64_t V); 150 isNegative()151 bool isNegative() const { return Val.isNegative(); } 152 153 /// This is just a convenience method to make client code smaller for a 154 /// common code. It also correctly performs the comparison without the 155 /// potential for an assertion from getZExtValue(). isZero()156 bool isZero() const { 157 return Val == 0; 158 } 159 160 /// This is just a convenience method to make client code smaller for a 161 /// common case. It also correctly performs the comparison without the 162 /// potential for an assertion from getZExtValue(). 163 /// @brief Determine if the value is one. isOne()164 bool isOne() const { 165 return Val == 1; 166 } 167 168 /// This function will return true iff every bit in this constant is set 169 /// to true. 170 /// @returns true iff this constant's bits are all set to true. 171 /// @brief Determine if the value is all ones. isMinusOne()172 bool isMinusOne() const { 173 return Val.isAllOnesValue(); 174 } 175 176 /// This function will return true iff this constant represents the largest 177 /// value that may be represented by the constant's type. 178 /// @returns true iff this is the largest value that may be represented 179 /// by this type. 180 /// @brief Determine if the value is maximal. isMaxValue(bool isSigned)181 bool isMaxValue(bool isSigned) const { 182 if (isSigned) 183 return Val.isMaxSignedValue(); 184 else 185 return Val.isMaxValue(); 186 } 187 188 /// This function will return true iff this constant represents the smallest 189 /// value that may be represented by this constant's type. 190 /// @returns true if this is the smallest value that may be represented by 191 /// this type. 192 /// @brief Determine if the value is minimal. isMinValue(bool isSigned)193 bool isMinValue(bool isSigned) const { 194 if (isSigned) 195 return Val.isMinSignedValue(); 196 else 197 return Val.isMinValue(); 198 } 199 200 /// This function will return true iff this constant represents a value with 201 /// active bits bigger than 64 bits or a value greater than the given uint64_t 202 /// value. 203 /// @returns true iff this constant is greater or equal to the given number. 204 /// @brief Determine if the value is greater or equal to the given number. uge(uint64_t Num)205 bool uge(uint64_t Num) const { 206 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num; 207 } 208 209 /// getLimitedValue - If the value is smaller than the specified limit, 210 /// return it, otherwise return the limit value. This causes the value 211 /// to saturate to the limit. 212 /// @returns the min of the value of the constant and the specified value 213 /// @brief Get the constant's value with a saturation limit 214 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const { 215 return Val.getLimitedValue(Limit); 216 } 217 218 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast. classof(const Value * V)219 static bool classof(const Value *V) { 220 return V->getValueID() == ConstantIntVal; 221 } 222 }; 223 224 225 //===----------------------------------------------------------------------===// 226 /// ConstantFP - Floating Point Values [float, double] 227 /// 228 class ConstantFP : public Constant { 229 APFloat Val; 230 void anchor() override; 231 void *operator new(size_t, unsigned) = delete; 232 ConstantFP(const ConstantFP &) = delete; 233 friend class LLVMContextImpl; 234 protected: 235 ConstantFP(Type *Ty, const APFloat& V); 236 protected: 237 // allocate space for exactly zero operands new(size_t s)238 void *operator new(size_t s) { 239 return User::operator new(s, 0); 240 } 241 public: 242 /// Floating point negation must be implemented with f(x) = -0.0 - x. This 243 /// method returns the negative zero constant for floating point or vector 244 /// floating point types; for all other types, it returns the null value. 245 static Constant *getZeroValueForNegation(Type *Ty); 246 247 /// get() - This returns a ConstantFP, or a vector containing a splat of a 248 /// ConstantFP, for the specified value in the specified type. This should 249 /// only be used for simple constant values like 2.0/1.0 etc, that are 250 /// known-valid both as host double and as the target format. 251 static Constant *get(Type* Ty, double V); 252 static Constant *get(Type* Ty, StringRef Str); 253 static ConstantFP *get(LLVMContext &Context, const APFloat &V); 254 static Constant *getNegativeZero(Type *Ty); 255 static Constant *getInfinity(Type *Ty, bool Negative = false); 256 257 /// isValueValidForType - return true if Ty is big enough to represent V. 258 static bool isValueValidForType(Type *Ty, const APFloat &V); getValueAPF()259 inline const APFloat &getValueAPF() const { return Val; } 260 261 /// isZero - Return true if the value is positive or negative zero. isZero()262 bool isZero() const { return Val.isZero(); } 263 264 /// isNegative - Return true if the sign bit is set. isNegative()265 bool isNegative() const { return Val.isNegative(); } 266 267 /// isInfinity - Return true if the value is infinity isInfinity()268 bool isInfinity() const { return Val.isInfinity(); } 269 270 /// isNaN - Return true if the value is a NaN. isNaN()271 bool isNaN() const { return Val.isNaN(); } 272 273 /// isExactlyValue - We don't rely on operator== working on double values, as 274 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 275 /// As such, this method can be used to do an exact bit-for-bit comparison of 276 /// two floating point values. The version with a double operand is retained 277 /// because it's so convenient to write isExactlyValue(2.0), but please use 278 /// it only for simple constants. 279 bool isExactlyValue(const APFloat &V) const; 280 isExactlyValue(double V)281 bool isExactlyValue(double V) const { 282 bool ignored; 283 APFloat FV(V); 284 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored); 285 return isExactlyValue(FV); 286 } 287 /// Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)288 static bool classof(const Value *V) { 289 return V->getValueID() == ConstantFPVal; 290 } 291 }; 292 293 //===----------------------------------------------------------------------===// 294 /// ConstantAggregateZero - All zero aggregate value 295 /// 296 class ConstantAggregateZero : public Constant { 297 void *operator new(size_t, unsigned) = delete; 298 ConstantAggregateZero(const ConstantAggregateZero &) = delete; 299 protected: ConstantAggregateZero(Type * ty)300 explicit ConstantAggregateZero(Type *ty) 301 : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {} 302 protected: 303 // allocate space for exactly zero operands new(size_t s)304 void *operator new(size_t s) { 305 return User::operator new(s, 0); 306 } 307 public: 308 static ConstantAggregateZero *get(Type *Ty); 309 310 void destroyConstant() override; 311 312 /// getSequentialElement - If this CAZ has array or vector type, return a zero 313 /// with the right element type. 314 Constant *getSequentialElement() const; 315 316 /// getStructElement - If this CAZ has struct type, return a zero with the 317 /// right element type for the specified element. 318 Constant *getStructElement(unsigned Elt) const; 319 320 /// getElementValue - Return a zero of the right value for the specified GEP 321 /// index. 322 Constant *getElementValue(Constant *C) const; 323 324 /// getElementValue - Return a zero of the right value for the specified GEP 325 /// index. 326 Constant *getElementValue(unsigned Idx) const; 327 328 /// \brief Return the number of elements in the array, vector, or struct. 329 unsigned getNumElements() const; 330 331 /// Methods for support type inquiry through isa, cast, and dyn_cast: 332 /// classof(const Value * V)333 static bool classof(const Value *V) { 334 return V->getValueID() == ConstantAggregateZeroVal; 335 } 336 }; 337 338 339 //===----------------------------------------------------------------------===// 340 /// ConstantArray - Constant Array Declarations 341 /// 342 class ConstantArray : public Constant { 343 friend struct ConstantAggrKeyType<ConstantArray>; 344 ConstantArray(const ConstantArray &) = delete; 345 protected: 346 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val); 347 public: 348 // ConstantArray accessors 349 static Constant *get(ArrayType *T, ArrayRef<Constant*> V); 350 351 private: 352 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V); 353 354 public: 355 /// Transparently provide more efficient getOperand methods. 356 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 357 358 /// getType - Specialize the getType() method to always return an ArrayType, 359 /// which reduces the amount of casting needed in parts of the compiler. 360 /// 361 inline ArrayType *getType() const { 362 return cast<ArrayType>(Value::getType()); 363 } 364 365 void destroyConstant() override; 366 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 367 368 /// Methods for support type inquiry through isa, cast, and dyn_cast: 369 static bool classof(const Value *V) { 370 return V->getValueID() == ConstantArrayVal; 371 } 372 }; 373 374 template <> 375 struct OperandTraits<ConstantArray> : 376 public VariadicOperandTraits<ConstantArray> { 377 }; 378 379 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant) 380 381 //===----------------------------------------------------------------------===// 382 // ConstantStruct - Constant Struct Declarations 383 // 384 class ConstantStruct : public Constant { 385 friend struct ConstantAggrKeyType<ConstantStruct>; 386 ConstantStruct(const ConstantStruct &) = delete; 387 protected: 388 ConstantStruct(StructType *T, ArrayRef<Constant *> Val); 389 public: 390 // ConstantStruct accessors 391 static Constant *get(StructType *T, ArrayRef<Constant*> V); 392 static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL; 393 394 /// getAnon - Return an anonymous struct that has the specified 395 /// elements. If the struct is possibly empty, then you must specify a 396 /// context. 397 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) { 398 return get(getTypeForElements(V, Packed), V); 399 } 400 static Constant *getAnon(LLVMContext &Ctx, 401 ArrayRef<Constant*> V, bool Packed = false) { 402 return get(getTypeForElements(Ctx, V, Packed), V); 403 } 404 405 /// getTypeForElements - Return an anonymous struct type to use for a constant 406 /// with the specified set of elements. The list must not be empty. 407 static StructType *getTypeForElements(ArrayRef<Constant*> V, 408 bool Packed = false); 409 /// getTypeForElements - This version of the method allows an empty list. 410 static StructType *getTypeForElements(LLVMContext &Ctx, 411 ArrayRef<Constant*> V, 412 bool Packed = false); 413 414 /// Transparently provide more efficient getOperand methods. 415 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 416 417 /// getType() specialization - Reduce amount of casting... 418 /// 419 inline StructType *getType() const { 420 return cast<StructType>(Value::getType()); 421 } 422 423 void destroyConstant() override; 424 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 425 426 /// Methods for support type inquiry through isa, cast, and dyn_cast: 427 static bool classof(const Value *V) { 428 return V->getValueID() == ConstantStructVal; 429 } 430 }; 431 432 template <> 433 struct OperandTraits<ConstantStruct> : 434 public VariadicOperandTraits<ConstantStruct> { 435 }; 436 437 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant) 438 439 440 //===----------------------------------------------------------------------===// 441 /// ConstantVector - Constant Vector Declarations 442 /// 443 class ConstantVector : public Constant { 444 friend struct ConstantAggrKeyType<ConstantVector>; 445 ConstantVector(const ConstantVector &) = delete; 446 protected: 447 ConstantVector(VectorType *T, ArrayRef<Constant *> Val); 448 public: 449 // ConstantVector accessors 450 static Constant *get(ArrayRef<Constant*> V); 451 452 private: 453 static Constant *getImpl(ArrayRef<Constant *> V); 454 455 public: 456 /// getSplat - Return a ConstantVector with the specified constant in each 457 /// element. 458 static Constant *getSplat(unsigned NumElts, Constant *Elt); 459 460 /// Transparently provide more efficient getOperand methods. 461 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 462 463 /// getType - Specialize the getType() method to always return a VectorType, 464 /// which reduces the amount of casting needed in parts of the compiler. 465 /// 466 inline VectorType *getType() const { 467 return cast<VectorType>(Value::getType()); 468 } 469 470 /// getSplatValue - If this is a splat constant, meaning that all of the 471 /// elements have the same value, return that value. Otherwise return NULL. 472 Constant *getSplatValue() const; 473 474 void destroyConstant() override; 475 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 476 477 /// Methods for support type inquiry through isa, cast, and dyn_cast: 478 static bool classof(const Value *V) { 479 return V->getValueID() == ConstantVectorVal; 480 } 481 }; 482 483 template <> 484 struct OperandTraits<ConstantVector> : 485 public VariadicOperandTraits<ConstantVector> { 486 }; 487 488 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant) 489 490 //===----------------------------------------------------------------------===// 491 /// ConstantPointerNull - a constant pointer value that points to null 492 /// 493 class ConstantPointerNull : public Constant { 494 void *operator new(size_t, unsigned) = delete; 495 ConstantPointerNull(const ConstantPointerNull &) = delete; 496 protected: 497 explicit ConstantPointerNull(PointerType *T) 498 : Constant(T, 499 Value::ConstantPointerNullVal, nullptr, 0) {} 500 501 protected: 502 // allocate space for exactly zero operands 503 void *operator new(size_t s) { 504 return User::operator new(s, 0); 505 } 506 public: 507 /// get() - Static factory methods - Return objects of the specified value 508 static ConstantPointerNull *get(PointerType *T); 509 510 void destroyConstant() override; 511 512 /// getType - Specialize the getType() method to always return an PointerType, 513 /// which reduces the amount of casting needed in parts of the compiler. 514 /// 515 inline PointerType *getType() const { 516 return cast<PointerType>(Value::getType()); 517 } 518 519 /// Methods for support type inquiry through isa, cast, and dyn_cast: 520 static bool classof(const Value *V) { 521 return V->getValueID() == ConstantPointerNullVal; 522 } 523 }; 524 525 //===----------------------------------------------------------------------===// 526 /// ConstantDataSequential - A vector or array constant whose element type is a 527 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just 528 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no 529 /// operands because it stores all of the elements of the constant as densely 530 /// packed data, instead of as Value*'s. 531 /// 532 /// This is the common base class of ConstantDataArray and ConstantDataVector. 533 /// 534 class ConstantDataSequential : public Constant { 535 friend class LLVMContextImpl; 536 /// DataElements - A pointer to the bytes underlying this constant (which is 537 /// owned by the uniquing StringMap). 538 const char *DataElements; 539 540 /// Next - This forms a link list of ConstantDataSequential nodes that have 541 /// the same value but different type. For example, 0,0,0,1 could be a 4 542 /// element array of i8, or a 1-element array of i32. They'll both end up in 543 /// the same StringMap bucket, linked up. 544 ConstantDataSequential *Next; 545 void *operator new(size_t, unsigned) = delete; 546 ConstantDataSequential(const ConstantDataSequential &) = delete; 547 protected: 548 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data) 549 : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {} 550 ~ConstantDataSequential() override { delete Next; } 551 552 static Constant *getImpl(StringRef Bytes, Type *Ty); 553 554 protected: 555 // allocate space for exactly zero operands. 556 void *operator new(size_t s) { 557 return User::operator new(s, 0); 558 } 559 public: 560 561 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be 562 /// formed with a vector or array of the specified element type. 563 /// ConstantDataArray only works with normal float and int types that are 564 /// stored densely in memory, not with things like i42 or x86_f80. 565 static bool isElementTypeCompatible(const Type *Ty); 566 567 /// getElementAsInteger - If this is a sequential container of integers (of 568 /// any size), return the specified element in the low bits of a uint64_t. 569 uint64_t getElementAsInteger(unsigned i) const; 570 571 /// getElementAsAPFloat - If this is a sequential container of floating point 572 /// type, return the specified element as an APFloat. 573 APFloat getElementAsAPFloat(unsigned i) const; 574 575 /// getElementAsFloat - If this is an sequential container of floats, return 576 /// the specified element as a float. 577 float getElementAsFloat(unsigned i) const; 578 579 /// getElementAsDouble - If this is an sequential container of doubles, return 580 /// the specified element as a double. 581 double getElementAsDouble(unsigned i) const; 582 583 /// getElementAsConstant - Return a Constant for a specified index's element. 584 /// Note that this has to compute a new constant to return, so it isn't as 585 /// efficient as getElementAsInteger/Float/Double. 586 Constant *getElementAsConstant(unsigned i) const; 587 588 /// getType - Specialize the getType() method to always return a 589 /// SequentialType, which reduces the amount of casting needed in parts of the 590 /// compiler. 591 inline SequentialType *getType() const { 592 return cast<SequentialType>(Value::getType()); 593 } 594 595 /// getElementType - Return the element type of the array/vector. 596 Type *getElementType() const; 597 598 /// getNumElements - Return the number of elements in the array or vector. 599 unsigned getNumElements() const; 600 601 /// getElementByteSize - Return the size (in bytes) of each element in the 602 /// array/vector. The size of the elements is known to be a multiple of one 603 /// byte. 604 uint64_t getElementByteSize() const; 605 606 607 /// isString - This method returns true if this is an array of i8. 608 bool isString() const; 609 610 /// isCString - This method returns true if the array "isString", ends with a 611 /// nul byte, and does not contains any other nul bytes. 612 bool isCString() const; 613 614 /// getAsString - If this array is isString(), then this method returns the 615 /// array as a StringRef. Otherwise, it asserts out. 616 /// 617 StringRef getAsString() const { 618 assert(isString() && "Not a string"); 619 return getRawDataValues(); 620 } 621 622 /// getAsCString - If this array is isCString(), then this method returns the 623 /// array (without the trailing null byte) as a StringRef. Otherwise, it 624 /// asserts out. 625 /// 626 StringRef getAsCString() const { 627 assert(isCString() && "Isn't a C string"); 628 StringRef Str = getAsString(); 629 return Str.substr(0, Str.size()-1); 630 } 631 632 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note 633 /// that this is an extremely tricky thing to work with, as it exposes the 634 /// host endianness of the data elements. 635 StringRef getRawDataValues() const; 636 637 void destroyConstant() override; 638 639 /// Methods for support type inquiry through isa, cast, and dyn_cast: 640 /// 641 static bool classof(const Value *V) { 642 return V->getValueID() == ConstantDataArrayVal || 643 V->getValueID() == ConstantDataVectorVal; 644 } 645 private: 646 const char *getElementPointer(unsigned Elt) const; 647 }; 648 649 //===----------------------------------------------------------------------===// 650 /// ConstantDataArray - An array constant whose element type is a simple 651 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple 652 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no 653 /// operands because it stores all of the elements of the constant as densely 654 /// packed data, instead of as Value*'s. 655 class ConstantDataArray : public ConstantDataSequential { 656 void *operator new(size_t, unsigned) = delete; 657 ConstantDataArray(const ConstantDataArray &) = delete; 658 void anchor() override; 659 friend class ConstantDataSequential; 660 explicit ConstantDataArray(Type *ty, const char *Data) 661 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {} 662 protected: 663 // allocate space for exactly zero operands. 664 void *operator new(size_t s) { 665 return User::operator new(s, 0); 666 } 667 public: 668 669 /// get() constructors - Return a constant with array type with an element 670 /// count and element type matching the ArrayRef passed in. Note that this 671 /// can return a ConstantAggregateZero object. 672 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts); 673 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts); 674 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts); 675 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts); 676 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts); 677 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts); 678 679 /// getFP() constructors - Return a constant with array type with an element 680 /// count and element type of float with precision matching the number of 681 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, 682 /// double for 64bits) Note that this can return a ConstantAggregateZero 683 /// object. 684 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts); 685 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts); 686 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts); 687 688 /// getString - This method constructs a CDS and initializes it with a text 689 /// string. The default behavior (AddNull==true) causes a null terminator to 690 /// be placed at the end of the array (increasing the length of the string by 691 /// one more than the StringRef would normally indicate. Pass AddNull=false 692 /// to disable this behavior. 693 static Constant *getString(LLVMContext &Context, StringRef Initializer, 694 bool AddNull = true); 695 696 /// getType - Specialize the getType() method to always return an ArrayType, 697 /// which reduces the amount of casting needed in parts of the compiler. 698 /// 699 inline ArrayType *getType() const { 700 return cast<ArrayType>(Value::getType()); 701 } 702 703 /// Methods for support type inquiry through isa, cast, and dyn_cast: 704 /// 705 static bool classof(const Value *V) { 706 return V->getValueID() == ConstantDataArrayVal; 707 } 708 }; 709 710 //===----------------------------------------------------------------------===// 711 /// ConstantDataVector - A vector constant whose element type is a simple 712 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple 713 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no 714 /// operands because it stores all of the elements of the constant as densely 715 /// packed data, instead of as Value*'s. 716 class ConstantDataVector : public ConstantDataSequential { 717 void *operator new(size_t, unsigned) = delete; 718 ConstantDataVector(const ConstantDataVector &) = delete; 719 void anchor() override; 720 friend class ConstantDataSequential; 721 explicit ConstantDataVector(Type *ty, const char *Data) 722 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {} 723 protected: 724 // allocate space for exactly zero operands. 725 void *operator new(size_t s) { 726 return User::operator new(s, 0); 727 } 728 public: 729 730 /// get() constructors - Return a constant with vector type with an element 731 /// count and element type matching the ArrayRef passed in. Note that this 732 /// can return a ConstantAggregateZero object. 733 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts); 734 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts); 735 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts); 736 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts); 737 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts); 738 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts); 739 740 /// getFP() constructors - Return a constant with vector type with an element 741 /// count and element type of float with the precision matching the number of 742 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, 743 /// double for 64bits) Note that this can return a ConstantAggregateZero 744 /// object. 745 static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts); 746 static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts); 747 static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts); 748 749 /// getSplat - Return a ConstantVector with the specified constant in each 750 /// element. The specified constant has to be a of a compatible type (i8/i16/ 751 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt. 752 static Constant *getSplat(unsigned NumElts, Constant *Elt); 753 754 /// getSplatValue - If this is a splat constant, meaning that all of the 755 /// elements have the same value, return that value. Otherwise return NULL. 756 Constant *getSplatValue() const; 757 758 /// getType - Specialize the getType() method to always return a VectorType, 759 /// which reduces the amount of casting needed in parts of the compiler. 760 /// 761 inline VectorType *getType() const { 762 return cast<VectorType>(Value::getType()); 763 } 764 765 /// Methods for support type inquiry through isa, cast, and dyn_cast: 766 /// 767 static bool classof(const Value *V) { 768 return V->getValueID() == ConstantDataVectorVal; 769 } 770 }; 771 772 773 774 /// BlockAddress - The address of a basic block. 775 /// 776 class BlockAddress : public Constant { 777 void *operator new(size_t, unsigned) = delete; 778 void *operator new(size_t s) { return User::operator new(s, 2); } 779 BlockAddress(Function *F, BasicBlock *BB); 780 public: 781 /// get - Return a BlockAddress for the specified function and basic block. 782 static BlockAddress *get(Function *F, BasicBlock *BB); 783 784 /// get - Return a BlockAddress for the specified basic block. The basic 785 /// block must be embedded into a function. 786 static BlockAddress *get(BasicBlock *BB); 787 788 /// \brief Lookup an existing \c BlockAddress constant for the given 789 /// BasicBlock. 790 /// 791 /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress. 792 static BlockAddress *lookup(const BasicBlock *BB); 793 794 /// Transparently provide more efficient getOperand methods. 795 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 796 797 Function *getFunction() const { return (Function*)Op<0>().get(); } 798 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); } 799 800 void destroyConstant() override; 801 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 802 803 /// Methods for support type inquiry through isa, cast, and dyn_cast: 804 static inline bool classof(const Value *V) { 805 return V->getValueID() == BlockAddressVal; 806 } 807 }; 808 809 template <> 810 struct OperandTraits<BlockAddress> : 811 public FixedNumOperandTraits<BlockAddress, 2> { 812 }; 813 814 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value) 815 816 817 //===----------------------------------------------------------------------===// 818 /// ConstantExpr - a constant value that is initialized with an expression using 819 /// other constant values. 820 /// 821 /// This class uses the standard Instruction opcodes to define the various 822 /// constant expressions. The Opcode field for the ConstantExpr class is 823 /// maintained in the Value::SubclassData field. 824 class ConstantExpr : public Constant { 825 friend struct ConstantExprKeyType; 826 827 protected: 828 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps) 829 : Constant(ty, ConstantExprVal, Ops, NumOps) { 830 // Operation type (an Instruction opcode) is stored as the SubclassData. 831 setValueSubclassData(Opcode); 832 } 833 834 public: 835 // Static methods to construct a ConstantExpr of different kinds. Note that 836 // these methods may return a object that is not an instance of the 837 // ConstantExpr class, because they will attempt to fold the constant 838 // expression into something simpler if possible. 839 840 /// getAlignOf constant expr - computes the alignment of a type in a target 841 /// independent way (Note: the return type is an i64). 842 static Constant *getAlignOf(Type *Ty); 843 844 /// getSizeOf constant expr - computes the (alloc) size of a type (in 845 /// address-units, not bits) in a target independent way (Note: the return 846 /// type is an i64). 847 /// 848 static Constant *getSizeOf(Type *Ty); 849 850 /// getOffsetOf constant expr - computes the offset of a struct field in a 851 /// target independent way (Note: the return type is an i64). 852 /// 853 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo); 854 855 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf, 856 /// which supports any aggregate type, and any Constant index. 857 /// 858 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo); 859 860 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false); 861 static Constant *getFNeg(Constant *C); 862 static Constant *getNot(Constant *C); 863 static Constant *getAdd(Constant *C1, Constant *C2, 864 bool HasNUW = false, bool HasNSW = false); 865 static Constant *getFAdd(Constant *C1, Constant *C2); 866 static Constant *getSub(Constant *C1, Constant *C2, 867 bool HasNUW = false, bool HasNSW = false); 868 static Constant *getFSub(Constant *C1, Constant *C2); 869 static Constant *getMul(Constant *C1, Constant *C2, 870 bool HasNUW = false, bool HasNSW = false); 871 static Constant *getFMul(Constant *C1, Constant *C2); 872 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false); 873 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false); 874 static Constant *getFDiv(Constant *C1, Constant *C2); 875 static Constant *getURem(Constant *C1, Constant *C2); 876 static Constant *getSRem(Constant *C1, Constant *C2); 877 static Constant *getFRem(Constant *C1, Constant *C2); 878 static Constant *getAnd(Constant *C1, Constant *C2); 879 static Constant *getOr(Constant *C1, Constant *C2); 880 static Constant *getXor(Constant *C1, Constant *C2); 881 static Constant *getShl(Constant *C1, Constant *C2, 882 bool HasNUW = false, bool HasNSW = false); 883 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false); 884 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false); 885 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false); 886 static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false); 887 static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false); 888 static Constant *getFPTrunc(Constant *C, Type *Ty, 889 bool OnlyIfReduced = false); 890 static Constant *getFPExtend(Constant *C, Type *Ty, 891 bool OnlyIfReduced = false); 892 static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false); 893 static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false); 894 static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false); 895 static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false); 896 static Constant *getPtrToInt(Constant *C, Type *Ty, 897 bool OnlyIfReduced = false); 898 static Constant *getIntToPtr(Constant *C, Type *Ty, 899 bool OnlyIfReduced = false); 900 static Constant *getBitCast(Constant *C, Type *Ty, 901 bool OnlyIfReduced = false); 902 static Constant *getAddrSpaceCast(Constant *C, Type *Ty, 903 bool OnlyIfReduced = false); 904 905 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); } 906 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); } 907 static Constant *getNSWAdd(Constant *C1, Constant *C2) { 908 return getAdd(C1, C2, false, true); 909 } 910 static Constant *getNUWAdd(Constant *C1, Constant *C2) { 911 return getAdd(C1, C2, true, false); 912 } 913 static Constant *getNSWSub(Constant *C1, Constant *C2) { 914 return getSub(C1, C2, false, true); 915 } 916 static Constant *getNUWSub(Constant *C1, Constant *C2) { 917 return getSub(C1, C2, true, false); 918 } 919 static Constant *getNSWMul(Constant *C1, Constant *C2) { 920 return getMul(C1, C2, false, true); 921 } 922 static Constant *getNUWMul(Constant *C1, Constant *C2) { 923 return getMul(C1, C2, true, false); 924 } 925 static Constant *getNSWShl(Constant *C1, Constant *C2) { 926 return getShl(C1, C2, false, true); 927 } 928 static Constant *getNUWShl(Constant *C1, Constant *C2) { 929 return getShl(C1, C2, true, false); 930 } 931 static Constant *getExactSDiv(Constant *C1, Constant *C2) { 932 return getSDiv(C1, C2, true); 933 } 934 static Constant *getExactUDiv(Constant *C1, Constant *C2) { 935 return getUDiv(C1, C2, true); 936 } 937 static Constant *getExactAShr(Constant *C1, Constant *C2) { 938 return getAShr(C1, C2, true); 939 } 940 static Constant *getExactLShr(Constant *C1, Constant *C2) { 941 return getLShr(C1, C2, true); 942 } 943 944 /// getBinOpIdentity - Return the identity for the given binary operation, 945 /// i.e. a constant C such that X op C = X and C op X = X for every X. It 946 /// returns null if the operator doesn't have an identity. 947 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty); 948 949 /// getBinOpAbsorber - Return the absorbing element for the given binary 950 /// operation, i.e. a constant C such that X op C = C and C op X = C for 951 /// every X. For example, this returns zero for integer multiplication. 952 /// It returns null if the operator doesn't have an absorbing element. 953 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty); 954 955 /// Transparently provide more efficient getOperand methods. 956 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); 957 958 /// \brief Convenience function for getting a Cast operation. 959 /// 960 /// \param ops The opcode for the conversion 961 /// \param C The constant to be converted 962 /// \param Ty The type to which the constant is converted 963 /// \param OnlyIfReduced see \a getWithOperands() docs. 964 static Constant *getCast(unsigned ops, Constant *C, Type *Ty, 965 bool OnlyIfReduced = false); 966 967 // @brief Create a ZExt or BitCast cast constant expression 968 static Constant *getZExtOrBitCast( 969 Constant *C, ///< The constant to zext or bitcast 970 Type *Ty ///< The type to zext or bitcast C to 971 ); 972 973 // @brief Create a SExt or BitCast cast constant expression 974 static Constant *getSExtOrBitCast( 975 Constant *C, ///< The constant to sext or bitcast 976 Type *Ty ///< The type to sext or bitcast C to 977 ); 978 979 // @brief Create a Trunc or BitCast cast constant expression 980 static Constant *getTruncOrBitCast( 981 Constant *C, ///< The constant to trunc or bitcast 982 Type *Ty ///< The type to trunc or bitcast C to 983 ); 984 985 /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant 986 /// expression. 987 static Constant *getPointerCast( 988 Constant *C, ///< The pointer value to be casted (operand 0) 989 Type *Ty ///< The type to which cast should be made 990 ); 991 992 /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on 993 /// the address space. 994 static Constant *getPointerBitCastOrAddrSpaceCast( 995 Constant *C, ///< The constant to addrspacecast or bitcast 996 Type *Ty ///< The type to bitcast or addrspacecast C to 997 ); 998 999 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts 1000 static Constant *getIntegerCast( 1001 Constant *C, ///< The integer constant to be casted 1002 Type *Ty, ///< The integer type to cast to 1003 bool isSigned ///< Whether C should be treated as signed or not 1004 ); 1005 1006 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts 1007 static Constant *getFPCast( 1008 Constant *C, ///< The integer constant to be casted 1009 Type *Ty ///< The integer type to cast to 1010 ); 1011 1012 /// @brief Return true if this is a convert constant expression 1013 bool isCast() const; 1014 1015 /// @brief Return true if this is a compare constant expression 1016 bool isCompare() const; 1017 1018 /// @brief Return true if this is an insertvalue or extractvalue expression, 1019 /// and the getIndices() method may be used. 1020 bool hasIndices() const; 1021 1022 /// @brief Return true if this is a getelementptr expression and all 1023 /// the index operands are compile-time known integers within the 1024 /// corresponding notional static array extents. Note that this is 1025 /// not equivalant to, a subset of, or a superset of the "inbounds" 1026 /// property. 1027 bool isGEPWithNoNotionalOverIndexing() const; 1028 1029 /// Select constant expr 1030 /// 1031 /// \param OnlyIfReducedTy see \a getWithOperands() docs. 1032 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2, 1033 Type *OnlyIfReducedTy = nullptr); 1034 1035 /// get - Return a binary or shift operator constant expression, 1036 /// folding if possible. 1037 /// 1038 /// \param OnlyIfReducedTy see \a getWithOperands() docs. 1039 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2, 1040 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr); 1041 1042 /// \brief Return an ICmp or FCmp comparison operator constant expression. 1043 /// 1044 /// \param OnlyIfReduced see \a getWithOperands() docs. 1045 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2, 1046 bool OnlyIfReduced = false); 1047 1048 /// get* - Return some common constants without having to 1049 /// specify the full Instruction::OPCODE identifier. 1050 /// 1051 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS, 1052 bool OnlyIfReduced = false); 1053 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS, 1054 bool OnlyIfReduced = false); 1055 1056 /// Getelementptr form. Value* is only accepted for convenience; 1057 /// all elements must be Constant's. 1058 /// 1059 /// \param OnlyIfReducedTy see \a getWithOperands() docs. 1060 static Constant *getGetElementPtr(Type *Ty, Constant *C, 1061 ArrayRef<Constant *> IdxList, 1062 bool InBounds = false, 1063 Type *OnlyIfReducedTy = nullptr) { 1064 return getGetElementPtr( 1065 Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()), 1066 InBounds, OnlyIfReducedTy); 1067 } 1068 static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx, 1069 bool InBounds = false, 1070 Type *OnlyIfReducedTy = nullptr) { 1071 // This form of the function only exists to avoid ambiguous overload 1072 // warnings about whether to convert Idx to ArrayRef<Constant *> or 1073 // ArrayRef<Value *>. 1074 return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy); 1075 } 1076 static Constant *getGetElementPtr(Type *Ty, Constant *C, 1077 ArrayRef<Value *> IdxList, 1078 bool InBounds = false, 1079 Type *OnlyIfReducedTy = nullptr); 1080 1081 /// Create an "inbounds" getelementptr. See the documentation for the 1082 /// "inbounds" flag in LangRef.html for details. 1083 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C, 1084 ArrayRef<Constant *> IdxList) { 1085 return getGetElementPtr(Ty, C, IdxList, true); 1086 } 1087 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C, 1088 Constant *Idx) { 1089 // This form of the function only exists to avoid ambiguous overload 1090 // warnings about whether to convert Idx to ArrayRef<Constant *> or 1091 // ArrayRef<Value *>. 1092 return getGetElementPtr(Ty, C, Idx, true); 1093 } 1094 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C, 1095 ArrayRef<Value *> IdxList) { 1096 return getGetElementPtr(Ty, C, IdxList, true); 1097 } 1098 1099 static Constant *getExtractElement(Constant *Vec, Constant *Idx, 1100 Type *OnlyIfReducedTy = nullptr); 1101 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, 1102 Type *OnlyIfReducedTy = nullptr); 1103 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, 1104 Type *OnlyIfReducedTy = nullptr); 1105 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs, 1106 Type *OnlyIfReducedTy = nullptr); 1107 static Constant *getInsertValue(Constant *Agg, Constant *Val, 1108 ArrayRef<unsigned> Idxs, 1109 Type *OnlyIfReducedTy = nullptr); 1110 1111 /// getOpcode - Return the opcode at the root of this constant expression 1112 unsigned getOpcode() const { return getSubclassDataFromValue(); } 1113 1114 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is 1115 /// not an ICMP or FCMP constant expression. 1116 unsigned getPredicate() const; 1117 1118 /// getIndices - Assert that this is an insertvalue or exactvalue 1119 /// expression and return the list of indices. 1120 ArrayRef<unsigned> getIndices() const; 1121 1122 /// getOpcodeName - Return a string representation for an opcode. 1123 const char *getOpcodeName() const; 1124 1125 /// getWithOperandReplaced - Return a constant expression identical to this 1126 /// one, but with the specified operand set to the specified value. 1127 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const; 1128 1129 /// getWithOperands - This returns the current constant expression with the 1130 /// operands replaced with the specified values. The specified array must 1131 /// have the same number of operands as our current one. 1132 Constant *getWithOperands(ArrayRef<Constant*> Ops) const { 1133 return getWithOperands(Ops, getType()); 1134 } 1135 1136 /// \brief Get the current expression with the operands replaced. 1137 /// 1138 /// Return the current constant expression with the operands replaced with \c 1139 /// Ops and the type with \c Ty. The new operands must have the same number 1140 /// as the current ones. 1141 /// 1142 /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something 1143 /// gets constant-folded, the type changes, or the expression is otherwise 1144 /// canonicalized. This parameter should almost always be \c false. 1145 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty, 1146 bool OnlyIfReduced = false) const; 1147 1148 /// getAsInstruction - Returns an Instruction which implements the same operation 1149 /// as this ConstantExpr. The instruction is not linked to any basic block. 1150 /// 1151 /// A better approach to this could be to have a constructor for Instruction 1152 /// which would take a ConstantExpr parameter, but that would have spread 1153 /// implementation details of ConstantExpr outside of Constants.cpp, which 1154 /// would make it harder to remove ConstantExprs altogether. 1155 Instruction *getAsInstruction(); 1156 1157 void destroyConstant() override; 1158 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override; 1159 1160 /// Methods for support type inquiry through isa, cast, and dyn_cast: 1161 static inline bool classof(const Value *V) { 1162 return V->getValueID() == ConstantExprVal; 1163 } 1164 1165 private: 1166 // Shadow Value::setValueSubclassData with a private forwarding method so that 1167 // subclasses cannot accidentally use it. 1168 void setValueSubclassData(unsigned short D) { 1169 Value::setValueSubclassData(D); 1170 } 1171 }; 1172 1173 template <> 1174 struct OperandTraits<ConstantExpr> : 1175 public VariadicOperandTraits<ConstantExpr, 1> { 1176 }; 1177 1178 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant) 1179 1180 //===----------------------------------------------------------------------===// 1181 /// UndefValue - 'undef' values are things that do not have specified contents. 1182 /// These are used for a variety of purposes, including global variable 1183 /// initializers and operands to instructions. 'undef' values can occur with 1184 /// any first-class type. 1185 /// 1186 /// Undef values aren't exactly constants; if they have multiple uses, they 1187 /// can appear to have different bit patterns at each use. See 1188 /// LangRef.html#undefvalues for details. 1189 /// 1190 class UndefValue : public Constant { 1191 void *operator new(size_t, unsigned) = delete; 1192 UndefValue(const UndefValue &) = delete; 1193 protected: 1194 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {} 1195 protected: 1196 // allocate space for exactly zero operands 1197 void *operator new(size_t s) { 1198 return User::operator new(s, 0); 1199 } 1200 public: 1201 /// get() - Static factory methods - Return an 'undef' object of the specified 1202 /// type. 1203 /// 1204 static UndefValue *get(Type *T); 1205 1206 /// getSequentialElement - If this Undef has array or vector type, return a 1207 /// undef with the right element type. 1208 UndefValue *getSequentialElement() const; 1209 1210 /// getStructElement - If this undef has struct type, return a undef with the 1211 /// right element type for the specified element. 1212 UndefValue *getStructElement(unsigned Elt) const; 1213 1214 /// getElementValue - Return an undef of the right value for the specified GEP 1215 /// index. 1216 UndefValue *getElementValue(Constant *C) const; 1217 1218 /// getElementValue - Return an undef of the right value for the specified GEP 1219 /// index. 1220 UndefValue *getElementValue(unsigned Idx) const; 1221 1222 /// \brief Return the number of elements in the array, vector, or struct. 1223 unsigned getNumElements() const; 1224 1225 void destroyConstant() override; 1226 1227 /// Methods for support type inquiry through isa, cast, and dyn_cast: 1228 static bool classof(const Value *V) { 1229 return V->getValueID() == UndefValueVal; 1230 } 1231 }; 1232 1233 } // End llvm namespace 1234 1235 #endif 1236