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