1 //===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- 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 // This file declares the SDNode class and derived classes, which are used to 10 // represent the nodes and operations present in a SelectionDAG. These nodes 11 // and operations are machine code level operations, with some similarities to 12 // the GCC RTL representation. 13 // 14 // Clients should include the SelectionDAG.h file instead of this file directly. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H 19 #define LLVM_CODEGEN_SELECTIONDAGNODES_H 20 21 #include "llvm/ADT/APFloat.h" 22 #include "llvm/ADT/ArrayRef.h" 23 #include "llvm/ADT/BitVector.h" 24 #include "llvm/ADT/FoldingSet.h" 25 #include "llvm/ADT/GraphTraits.h" 26 #include "llvm/ADT/SmallPtrSet.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/ADT/ilist_node.h" 29 #include "llvm/ADT/iterator.h" 30 #include "llvm/ADT/iterator_range.h" 31 #include "llvm/CodeGen/ISDOpcodes.h" 32 #include "llvm/CodeGen/MachineMemOperand.h" 33 #include "llvm/CodeGen/Register.h" 34 #include "llvm/CodeGen/ValueTypes.h" 35 #include "llvm/CodeGenTypes/MachineValueType.h" 36 #include "llvm/IR/Constants.h" 37 #include "llvm/IR/DebugLoc.h" 38 #include "llvm/IR/Instruction.h" 39 #include "llvm/IR/Instructions.h" 40 #include "llvm/IR/Metadata.h" 41 #include "llvm/IR/Operator.h" 42 #include "llvm/Support/AlignOf.h" 43 #include "llvm/Support/AtomicOrdering.h" 44 #include "llvm/Support/Casting.h" 45 #include "llvm/Support/Compiler.h" 46 #include "llvm/Support/ErrorHandling.h" 47 #include "llvm/Support/TypeSize.h" 48 #include <algorithm> 49 #include <cassert> 50 #include <climits> 51 #include <cstddef> 52 #include <cstdint> 53 #include <cstring> 54 #include <iterator> 55 #include <string> 56 #include <tuple> 57 #include <utility> 58 59 namespace llvm { 60 61 class APInt; 62 class Constant; 63 class GlobalValue; 64 class MachineBasicBlock; 65 class MachineConstantPoolValue; 66 class MCSymbol; 67 class raw_ostream; 68 class SDNode; 69 class SelectionDAG; 70 class Type; 71 class Value; 72 73 LLVM_ABI void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr, 74 bool force = false); 75 76 /// This represents a list of ValueType's that has been intern'd by 77 /// a SelectionDAG. Instances of this simple value class are returned by 78 /// SelectionDAG::getVTList(...). 79 /// 80 struct SDVTList { 81 const EVT *VTs; 82 unsigned int NumVTs; 83 }; 84 85 namespace ISD { 86 87 /// Node predicates 88 89 /// If N is a BUILD_VECTOR or SPLAT_VECTOR node whose elements are all the 90 /// same constant or undefined, return true and return the constant value in 91 /// \p SplatValue. 92 LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue); 93 94 /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where 95 /// all of the elements are ~0 or undef. If \p BuildVectorOnly is set to 96 /// true, it only checks BUILD_VECTOR. 97 LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, 98 bool BuildVectorOnly = false); 99 100 /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where 101 /// all of the elements are 0 or undef. If \p BuildVectorOnly is set to true, it 102 /// only checks BUILD_VECTOR. 103 LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, 104 bool BuildVectorOnly = false); 105 106 /// Return true if the specified node is a BUILD_VECTOR where all of the 107 /// elements are ~0 or undef. 108 LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N); 109 110 /// Return true if the specified node is a BUILD_VECTOR where all of the 111 /// elements are 0 or undef. 112 LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N); 113 114 /// Return true if the specified node is a BUILD_VECTOR node of all 115 /// ConstantSDNode or undef. 116 LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N); 117 118 /// Return true if the specified node is a BUILD_VECTOR node of all 119 /// ConstantFPSDNode or undef. 120 LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N); 121 122 /// Returns true if the specified node is a vector where all elements can 123 /// be truncated to the specified element size without a loss in meaning. 124 LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, 125 bool Signed); 126 127 /// Return true if the node has at least one operand and all operands of the 128 /// specified node are ISD::UNDEF. 129 LLVM_ABI bool allOperandsUndef(const SDNode *N); 130 131 /// Return true if the specified node is FREEZE(UNDEF). 132 LLVM_ABI bool isFreezeUndef(const SDNode *N); 133 134 } // end namespace ISD 135 136 //===----------------------------------------------------------------------===// 137 /// Unlike LLVM values, Selection DAG nodes may return multiple 138 /// values as the result of a computation. Many nodes return multiple values, 139 /// from loads (which define a token and a return value) to ADDC (which returns 140 /// a result and a carry value), to calls (which may return an arbitrary number 141 /// of values). 142 /// 143 /// As such, each use of a SelectionDAG computation must indicate the node that 144 /// computes it as well as which return value to use from that node. This pair 145 /// of information is represented with the SDValue value type. 146 /// 147 class SDValue { 148 friend struct DenseMapInfo<SDValue>; 149 150 SDNode *Node = nullptr; // The node defining the value we are using. 151 unsigned ResNo = 0; // Which return value of the node we are using. 152 153 public: 154 SDValue() = default; 155 SDValue(SDNode *node, unsigned resno); 156 157 /// get the index which selects a specific result in the SDNode 158 unsigned getResNo() const { return ResNo; } 159 160 /// get the SDNode which holds the desired result 161 SDNode *getNode() const { return Node; } 162 163 /// set the SDNode 164 void setNode(SDNode *N) { Node = N; } 165 166 inline SDNode *operator->() const { return Node; } 167 168 bool operator==(const SDValue &O) const { 169 return Node == O.Node && ResNo == O.ResNo; 170 } 171 bool operator!=(const SDValue &O) const { 172 return !operator==(O); 173 } 174 bool operator<(const SDValue &O) const { 175 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo); 176 } 177 explicit operator bool() const { 178 return Node != nullptr; 179 } 180 181 SDValue getValue(unsigned R) const { 182 return SDValue(Node, R); 183 } 184 185 /// Return true if this node is an operand of N. 186 LLVM_ABI bool isOperandOf(const SDNode *N) const; 187 188 /// Return the ValueType of the referenced return value. 189 inline EVT getValueType() const; 190 191 /// Return the simple ValueType of the referenced return value. 192 MVT getSimpleValueType() const { 193 return getValueType().getSimpleVT(); 194 } 195 196 /// Returns the size of the value in bits. 197 /// 198 /// If the value type is a scalable vector type, the scalable property will 199 /// be set and the runtime size will be a positive integer multiple of the 200 /// base size. 201 TypeSize getValueSizeInBits() const { 202 return getValueType().getSizeInBits(); 203 } 204 205 uint64_t getScalarValueSizeInBits() const { 206 return getValueType().getScalarType().getFixedSizeInBits(); 207 } 208 209 // Forwarding methods - These forward to the corresponding methods in SDNode. 210 inline unsigned getOpcode() const; 211 inline unsigned getNumOperands() const; 212 inline const SDValue &getOperand(unsigned i) const; 213 inline uint64_t getConstantOperandVal(unsigned i) const; 214 inline const APInt &getConstantOperandAPInt(unsigned i) const; 215 inline bool isTargetOpcode() const; 216 inline bool isMachineOpcode() const; 217 inline bool isUndef() const; 218 inline bool isAnyAdd() const; 219 inline unsigned getMachineOpcode() const; 220 inline const DebugLoc &getDebugLoc() const; 221 inline void dump() const; 222 inline void dump(const SelectionDAG *G) const; 223 inline void dumpr() const; 224 inline void dumpr(const SelectionDAG *G) const; 225 226 /// Return true if this operand (which must be a chain) reaches the 227 /// specified operand without crossing any side-effecting instructions. 228 /// In practice, this looks through token factors and non-volatile loads. 229 /// In order to remain efficient, this only 230 /// looks a couple of nodes in, it does not do an exhaustive search. 231 LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, 232 unsigned Depth = 2) const; 233 234 /// Return true if there are no nodes using value ResNo of Node. 235 inline bool use_empty() const; 236 237 /// Return true if there is exactly one node using value ResNo of Node. 238 inline bool hasOneUse() const; 239 }; 240 241 template<> struct DenseMapInfo<SDValue> { 242 static inline SDValue getEmptyKey() { 243 SDValue V; 244 V.ResNo = -1U; 245 return V; 246 } 247 248 static inline SDValue getTombstoneKey() { 249 SDValue V; 250 V.ResNo = -2U; 251 return V; 252 } 253 254 static unsigned getHashValue(const SDValue &Val) { 255 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^ 256 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo(); 257 } 258 259 static bool isEqual(const SDValue &LHS, const SDValue &RHS) { 260 return LHS == RHS; 261 } 262 }; 263 264 /// Allow casting operators to work directly on 265 /// SDValues as if they were SDNode*'s. 266 template<> struct simplify_type<SDValue> { 267 using SimpleType = SDNode *; 268 269 static SimpleType getSimplifiedValue(SDValue &Val) { 270 return Val.getNode(); 271 } 272 }; 273 template<> struct simplify_type<const SDValue> { 274 using SimpleType = /*const*/ SDNode *; 275 276 static SimpleType getSimplifiedValue(const SDValue &Val) { 277 return Val.getNode(); 278 } 279 }; 280 281 /// Represents a use of a SDNode. This class holds an SDValue, 282 /// which records the SDNode being used and the result number, a 283 /// pointer to the SDNode using the value, and Next and Prev pointers, 284 /// which link together all the uses of an SDNode. 285 /// 286 class SDUse { 287 /// Val - The value being used. 288 SDValue Val; 289 /// User - The user of this value. 290 SDNode *User = nullptr; 291 /// Prev, Next - Pointers to the uses list of the SDNode referred by 292 /// this operand. 293 SDUse **Prev = nullptr; 294 SDUse *Next = nullptr; 295 296 public: 297 SDUse() = default; 298 SDUse(const SDUse &U) = delete; 299 SDUse &operator=(const SDUse &) = delete; 300 301 /// Normally SDUse will just implicitly convert to an SDValue that it holds. 302 operator const SDValue&() const { return Val; } 303 304 /// If implicit conversion to SDValue doesn't work, the get() method returns 305 /// the SDValue. 306 const SDValue &get() const { return Val; } 307 308 /// This returns the SDNode that contains this Use. 309 SDNode *getUser() { return User; } 310 const SDNode *getUser() const { return User; } 311 312 /// Get the next SDUse in the use list. 313 SDUse *getNext() const { return Next; } 314 315 /// Return the operand # of this use in its user. 316 inline unsigned getOperandNo() const; 317 318 /// Convenience function for get().getNode(). 319 SDNode *getNode() const { return Val.getNode(); } 320 /// Convenience function for get().getResNo(). 321 unsigned getResNo() const { return Val.getResNo(); } 322 /// Convenience function for get().getValueType(). 323 EVT getValueType() const { return Val.getValueType(); } 324 325 /// Convenience function for get().operator== 326 bool operator==(const SDValue &V) const { 327 return Val == V; 328 } 329 330 /// Convenience function for get().operator!= 331 bool operator!=(const SDValue &V) const { 332 return Val != V; 333 } 334 335 /// Convenience function for get().operator< 336 bool operator<(const SDValue &V) const { 337 return Val < V; 338 } 339 340 private: 341 friend class SelectionDAG; 342 friend class SDNode; 343 // TODO: unfriend HandleSDNode once we fix its operand handling. 344 friend class HandleSDNode; 345 346 void setUser(SDNode *p) { User = p; } 347 348 /// Remove this use from its existing use list, assign it the 349 /// given value, and add it to the new value's node's use list. 350 inline void set(const SDValue &V); 351 /// Like set, but only supports initializing a newly-allocated 352 /// SDUse with a non-null value. 353 inline void setInitial(const SDValue &V); 354 /// Like set, but only sets the Node portion of the value, 355 /// leaving the ResNo portion unmodified. 356 inline void setNode(SDNode *N); 357 358 void addToList(SDUse **List) { 359 Next = *List; 360 if (Next) Next->Prev = &Next; 361 Prev = List; 362 *List = this; 363 } 364 365 void removeFromList() { 366 *Prev = Next; 367 if (Next) Next->Prev = Prev; 368 } 369 }; 370 371 /// simplify_type specializations - Allow casting operators to work directly on 372 /// SDValues as if they were SDNode*'s. 373 template<> struct simplify_type<SDUse> { 374 using SimpleType = SDNode *; 375 376 static SimpleType getSimplifiedValue(SDUse &Val) { 377 return Val.getNode(); 378 } 379 }; 380 381 /// These are IR-level optimization flags that may be propagated to SDNodes. 382 /// TODO: This data structure should be shared by the IR optimizer and the 383 /// the backend. 384 struct SDNodeFlags { 385 private: 386 friend class SDNode; 387 388 unsigned Flags = 0; 389 390 template <unsigned Flag> void setFlag(bool B) { 391 Flags = (Flags & ~Flag) | (B ? Flag : 0); 392 } 393 394 public: 395 enum : unsigned { 396 None = 0, 397 NoUnsignedWrap = 1 << 0, 398 NoSignedWrap = 1 << 1, 399 NoWrap = NoUnsignedWrap | NoSignedWrap, 400 Exact = 1 << 2, 401 Disjoint = 1 << 3, 402 NonNeg = 1 << 4, 403 NoNaNs = 1 << 5, 404 NoInfs = 1 << 6, 405 NoSignedZeros = 1 << 7, 406 AllowReciprocal = 1 << 8, 407 AllowContract = 1 << 9, 408 ApproximateFuncs = 1 << 10, 409 AllowReassociation = 1 << 11, 410 411 // We assume instructions do not raise floating-point exceptions by default, 412 // and only those marked explicitly may do so. We could choose to represent 413 // this via a positive "FPExcept" flags like on the MI level, but having a 414 // negative "NoFPExcept" flag here makes the flag intersection logic more 415 // straightforward. 416 NoFPExcept = 1 << 12, 417 // Instructions with attached 'unpredictable' metadata on IR level. 418 Unpredictable = 1 << 13, 419 // Compare instructions which may carry the samesign flag. 420 SameSign = 1 << 14, 421 422 // NOTE: Please update LargestValue in LLVM_DECLARE_ENUM_AS_BITMASK below 423 // the class definition when adding new flags. 424 425 PoisonGeneratingFlags = NoUnsignedWrap | NoSignedWrap | Exact | Disjoint | 426 NonNeg | NoNaNs | NoInfs | SameSign, 427 FastMathFlags = NoNaNs | NoInfs | NoSignedZeros | AllowReciprocal | 428 AllowContract | ApproximateFuncs | AllowReassociation, 429 }; 430 431 /// Default constructor turns off all optimization flags. 432 SDNodeFlags(unsigned Flags = SDNodeFlags::None) : Flags(Flags) {} 433 434 /// Propagate the fast-math-flags from an IR FPMathOperator. 435 void copyFMF(const FPMathOperator &FPMO) { 436 setNoNaNs(FPMO.hasNoNaNs()); 437 setNoInfs(FPMO.hasNoInfs()); 438 setNoSignedZeros(FPMO.hasNoSignedZeros()); 439 setAllowReciprocal(FPMO.hasAllowReciprocal()); 440 setAllowContract(FPMO.hasAllowContract()); 441 setApproximateFuncs(FPMO.hasApproxFunc()); 442 setAllowReassociation(FPMO.hasAllowReassoc()); 443 } 444 445 // These are mutators for each flag. 446 void setNoUnsignedWrap(bool b) { setFlag<NoUnsignedWrap>(b); } 447 void setNoSignedWrap(bool b) { setFlag<NoSignedWrap>(b); } 448 void setExact(bool b) { setFlag<Exact>(b); } 449 void setDisjoint(bool b) { setFlag<Disjoint>(b); } 450 void setSameSign(bool b) { setFlag<SameSign>(b); } 451 void setNonNeg(bool b) { setFlag<NonNeg>(b); } 452 void setNoNaNs(bool b) { setFlag<NoNaNs>(b); } 453 void setNoInfs(bool b) { setFlag<NoInfs>(b); } 454 void setNoSignedZeros(bool b) { setFlag<NoSignedZeros>(b); } 455 void setAllowReciprocal(bool b) { setFlag<AllowReciprocal>(b); } 456 void setAllowContract(bool b) { setFlag<AllowContract>(b); } 457 void setApproximateFuncs(bool b) { setFlag<ApproximateFuncs>(b); } 458 void setAllowReassociation(bool b) { setFlag<AllowReassociation>(b); } 459 void setNoFPExcept(bool b) { setFlag<NoFPExcept>(b); } 460 void setUnpredictable(bool b) { setFlag<Unpredictable>(b); } 461 462 // These are accessors for each flag. 463 bool hasNoUnsignedWrap() const { return Flags & NoUnsignedWrap; } 464 bool hasNoSignedWrap() const { return Flags & NoSignedWrap; } 465 bool hasExact() const { return Flags & Exact; } 466 bool hasDisjoint() const { return Flags & Disjoint; } 467 bool hasSameSign() const { return Flags & SameSign; } 468 bool hasNonNeg() const { return Flags & NonNeg; } 469 bool hasNoNaNs() const { return Flags & NoNaNs; } 470 bool hasNoInfs() const { return Flags & NoInfs; } 471 bool hasNoSignedZeros() const { return Flags & NoSignedZeros; } 472 bool hasAllowReciprocal() const { return Flags & AllowReciprocal; } 473 bool hasAllowContract() const { return Flags & AllowContract; } 474 bool hasApproximateFuncs() const { return Flags & ApproximateFuncs; } 475 bool hasAllowReassociation() const { return Flags & AllowReassociation; } 476 bool hasNoFPExcept() const { return Flags & NoFPExcept; } 477 bool hasUnpredictable() const { return Flags & Unpredictable; } 478 479 bool operator==(const SDNodeFlags &Other) const { 480 return Flags == Other.Flags; 481 } 482 void operator&=(const SDNodeFlags &OtherFlags) { Flags &= OtherFlags.Flags; } 483 void operator|=(const SDNodeFlags &OtherFlags) { Flags |= OtherFlags.Flags; } 484 }; 485 486 LLVM_DECLARE_ENUM_AS_BITMASK(decltype(SDNodeFlags::None), 487 SDNodeFlags::SameSign); 488 489 inline SDNodeFlags operator|(SDNodeFlags LHS, SDNodeFlags RHS) { 490 LHS |= RHS; 491 return LHS; 492 } 493 494 inline SDNodeFlags operator&(SDNodeFlags LHS, SDNodeFlags RHS) { 495 LHS &= RHS; 496 return LHS; 497 } 498 499 /// Represents one node in the SelectionDAG. 500 /// 501 class SDNode : public FoldingSetNode, public ilist_node<SDNode> { 502 private: 503 /// The operation that this node performs. 504 int32_t NodeType; 505 506 SDNodeFlags Flags; 507 508 protected: 509 // We define a set of mini-helper classes to help us interpret the bits in our 510 // SubclassData. These are designed to fit within a uint16_t so they pack 511 // with SDNodeFlags. 512 513 #if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__)) 514 // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words 515 // and give the `pack` pragma push semantics. 516 #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)") 517 #define END_TWO_BYTE_PACK() _Pragma("pack(pop)") 518 #else 519 #define BEGIN_TWO_BYTE_PACK() 520 #define END_TWO_BYTE_PACK() 521 #endif 522 523 BEGIN_TWO_BYTE_PACK() 524 class SDNodeBitfields { 525 friend class SDNode; 526 friend class MemIntrinsicSDNode; 527 friend class MemSDNode; 528 friend class SelectionDAG; 529 530 uint16_t HasDebugValue : 1; 531 uint16_t IsMemIntrinsic : 1; 532 uint16_t IsDivergent : 1; 533 }; 534 enum { NumSDNodeBits = 3 }; 535 536 class ConstantSDNodeBitfields { 537 friend class ConstantSDNode; 538 539 uint16_t : NumSDNodeBits; 540 541 uint16_t IsOpaque : 1; 542 }; 543 544 class MemSDNodeBitfields { 545 friend class MemSDNode; 546 friend class MemIntrinsicSDNode; 547 friend class AtomicSDNode; 548 549 uint16_t : NumSDNodeBits; 550 551 uint16_t IsVolatile : 1; 552 uint16_t IsNonTemporal : 1; 553 uint16_t IsDereferenceable : 1; 554 uint16_t IsInvariant : 1; 555 }; 556 enum { NumMemSDNodeBits = NumSDNodeBits + 4 }; 557 558 class LSBaseSDNodeBitfields { 559 friend class LSBaseSDNode; 560 friend class VPBaseLoadStoreSDNode; 561 friend class MaskedLoadStoreSDNode; 562 friend class MaskedGatherScatterSDNode; 563 friend class VPGatherScatterSDNode; 564 friend class MaskedHistogramSDNode; 565 566 uint16_t : NumMemSDNodeBits; 567 568 // This storage is shared between disparate class hierarchies to hold an 569 // enumeration specific to the class hierarchy in use. 570 // LSBaseSDNode => enum ISD::MemIndexedMode 571 // VPLoadStoreBaseSDNode => enum ISD::MemIndexedMode 572 // MaskedLoadStoreBaseSDNode => enum ISD::MemIndexedMode 573 // VPGatherScatterSDNode => enum ISD::MemIndexType 574 // MaskedGatherScatterSDNode => enum ISD::MemIndexType 575 // MaskedHistogramSDNode => enum ISD::MemIndexType 576 uint16_t AddressingMode : 3; 577 }; 578 enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 }; 579 580 class LoadSDNodeBitfields { 581 friend class LoadSDNode; 582 friend class AtomicSDNode; 583 friend class VPLoadSDNode; 584 friend class VPStridedLoadSDNode; 585 friend class MaskedLoadSDNode; 586 friend class MaskedGatherSDNode; 587 friend class VPGatherSDNode; 588 friend class MaskedHistogramSDNode; 589 590 uint16_t : NumLSBaseSDNodeBits; 591 592 uint16_t ExtTy : 2; // enum ISD::LoadExtType 593 uint16_t IsExpanding : 1; 594 }; 595 596 class StoreSDNodeBitfields { 597 friend class StoreSDNode; 598 friend class VPStoreSDNode; 599 friend class VPStridedStoreSDNode; 600 friend class MaskedStoreSDNode; 601 friend class MaskedScatterSDNode; 602 friend class VPScatterSDNode; 603 604 uint16_t : NumLSBaseSDNodeBits; 605 606 uint16_t IsTruncating : 1; 607 uint16_t IsCompressing : 1; 608 }; 609 610 union { 611 char RawSDNodeBits[sizeof(uint16_t)]; 612 SDNodeBitfields SDNodeBits; 613 ConstantSDNodeBitfields ConstantSDNodeBits; 614 MemSDNodeBitfields MemSDNodeBits; 615 LSBaseSDNodeBitfields LSBaseSDNodeBits; 616 LoadSDNodeBitfields LoadSDNodeBits; 617 StoreSDNodeBitfields StoreSDNodeBits; 618 }; 619 END_TWO_BYTE_PACK() 620 #undef BEGIN_TWO_BYTE_PACK 621 #undef END_TWO_BYTE_PACK 622 623 // RawSDNodeBits must cover the entirety of the union. This means that all of 624 // the union's members must have size <= RawSDNodeBits. We write the RHS as 625 // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter. 626 static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide"); 627 static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide"); 628 static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide"); 629 static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide"); 630 static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide"); 631 static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide"); 632 633 public: 634 /// Unique and persistent id per SDNode in the DAG. Used for debug printing. 635 /// We do not place that under `#if LLVM_ENABLE_ABI_BREAKING_CHECKS` 636 /// intentionally because it adds unneeded complexity without noticeable 637 /// benefits (see discussion with @thakis in D120714). Currently, there are 638 /// two padding bytes after this field. 639 uint16_t PersistentId = 0xffff; 640 641 private: 642 friend class SelectionDAG; 643 // TODO: unfriend HandleSDNode once we fix its operand handling. 644 friend class HandleSDNode; 645 646 /// Unique id per SDNode in the DAG. 647 int NodeId = -1; 648 649 /// The values that are used by this operation. 650 SDUse *OperandList = nullptr; 651 652 /// The types of the values this node defines. SDNode's may 653 /// define multiple values simultaneously. 654 const EVT *ValueList; 655 656 /// List of uses for this SDNode. 657 SDUse *UseList = nullptr; 658 659 /// The number of entries in the Operand/Value list. 660 unsigned short NumOperands = 0; 661 unsigned short NumValues; 662 663 // The ordering of the SDNodes. It roughly corresponds to the ordering of the 664 // original LLVM instructions. 665 // This is used for turning off scheduling, because we'll forgo 666 // the normal scheduling algorithms and output the instructions according to 667 // this ordering. 668 unsigned IROrder; 669 670 /// Source line information. 671 DebugLoc debugLoc; 672 673 /// Return a pointer to the specified value type. 674 LLVM_ABI static const EVT *getValueTypeList(MVT VT); 675 676 /// Index in worklist of DAGCombiner, or negative if the node is not in the 677 /// worklist. -1 = not in worklist; -2 = not in worklist, but has already been 678 /// combined at least once. 679 int CombinerWorklistIndex = -1; 680 681 uint32_t CFIType = 0; 682 683 public: 684 //===--------------------------------------------------------------------===// 685 // Accessors 686 // 687 688 /// Return the SelectionDAG opcode value for this node. For 689 /// pre-isel nodes (those for which isMachineOpcode returns false), these 690 /// are the opcode values in the ISD and <target>ISD namespaces. For 691 /// post-isel opcodes, see getMachineOpcode. 692 unsigned getOpcode() const { return (unsigned)NodeType; } 693 694 /// Test if this node has a target-specific opcode (in the 695 /// \<target\>ISD namespace). 696 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; } 697 698 /// Returns true if the node type is UNDEF or POISON. 699 bool isUndef() const { 700 return NodeType == ISD::UNDEF || NodeType == ISD::POISON; 701 } 702 703 /// Returns true if the node type is ADD or PTRADD. 704 bool isAnyAdd() const { 705 return NodeType == ISD::ADD || NodeType == ISD::PTRADD; 706 } 707 708 /// Test if this node is a memory intrinsic (with valid pointer information). 709 bool isMemIntrinsic() const { return SDNodeBits.IsMemIntrinsic; } 710 711 /// Test if this node is a strict floating point pseudo-op. 712 bool isStrictFPOpcode() { 713 switch (NodeType) { 714 default: 715 return false; 716 case ISD::STRICT_FP16_TO_FP: 717 case ISD::STRICT_FP_TO_FP16: 718 case ISD::STRICT_BF16_TO_FP: 719 case ISD::STRICT_FP_TO_BF16: 720 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 721 case ISD::STRICT_##DAGN: 722 #include "llvm/IR/ConstrainedOps.def" 723 return true; 724 } 725 } 726 727 /// Test if this node is an assert operation. 728 bool isAssert() const { 729 switch (NodeType) { 730 default: 731 return false; 732 case ISD::AssertAlign: 733 case ISD::AssertNoFPClass: 734 case ISD::AssertSext: 735 case ISD::AssertZext: 736 return true; 737 } 738 } 739 740 /// Test if this node is a vector predication operation. 741 bool isVPOpcode() const { return ISD::isVPOpcode(getOpcode()); } 742 743 /// Test if this node has a post-isel opcode, directly 744 /// corresponding to a MachineInstr opcode. 745 bool isMachineOpcode() const { return NodeType < 0; } 746 747 /// This may only be called if isMachineOpcode returns 748 /// true. It returns the MachineInstr opcode value that the node's opcode 749 /// corresponds to. 750 unsigned getMachineOpcode() const { 751 assert(isMachineOpcode() && "Not a MachineInstr opcode!"); 752 return ~NodeType; 753 } 754 755 bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; } 756 void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; } 757 758 bool isDivergent() const { return SDNodeBits.IsDivergent; } 759 760 /// Return true if there are no uses of this node. 761 bool use_empty() const { return UseList == nullptr; } 762 763 /// Return true if there is exactly one use of this node. 764 bool hasOneUse() const { return hasSingleElement(uses()); } 765 766 /// Return the number of uses of this node. This method takes 767 /// time proportional to the number of uses. 768 size_t use_size() const { return std::distance(use_begin(), use_end()); } 769 770 /// Return the unique node id. 771 int getNodeId() const { return NodeId; } 772 773 /// Set unique node id. 774 void setNodeId(int Id) { NodeId = Id; } 775 776 /// Get worklist index for DAGCombiner 777 int getCombinerWorklistIndex() const { return CombinerWorklistIndex; } 778 779 /// Set worklist index for DAGCombiner 780 void setCombinerWorklistIndex(int Index) { CombinerWorklistIndex = Index; } 781 782 /// Return the node ordering. 783 unsigned getIROrder() const { return IROrder; } 784 785 /// Set the node ordering. 786 void setIROrder(unsigned Order) { IROrder = Order; } 787 788 /// Return the source location info. 789 const DebugLoc &getDebugLoc() const { return debugLoc; } 790 791 /// Set source location info. Try to avoid this, putting 792 /// it in the constructor is preferable. 793 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); } 794 795 /// This class provides iterator support for SDUse 796 /// operands that use a specific SDNode. 797 class use_iterator { 798 friend class SDNode; 799 800 SDUse *Op = nullptr; 801 802 explicit use_iterator(SDUse *op) : Op(op) {} 803 804 public: 805 using iterator_category = std::forward_iterator_tag; 806 using value_type = SDUse; 807 using difference_type = std::ptrdiff_t; 808 using pointer = value_type *; 809 using reference = value_type &; 810 811 use_iterator() = default; 812 use_iterator(const use_iterator &I) = default; 813 use_iterator &operator=(const use_iterator &) = default; 814 815 bool operator==(const use_iterator &x) const { return Op == x.Op; } 816 bool operator!=(const use_iterator &x) const { 817 return !operator==(x); 818 } 819 820 // Iterator traversal: forward iteration only. 821 use_iterator &operator++() { // Preincrement 822 assert(Op && "Cannot increment end iterator!"); 823 Op = Op->getNext(); 824 return *this; 825 } 826 827 use_iterator operator++(int) { // Postincrement 828 use_iterator tmp = *this; ++*this; return tmp; 829 } 830 831 /// Retrieve a pointer to the current user node. 832 SDUse &operator*() const { 833 assert(Op && "Cannot dereference end iterator!"); 834 return *Op; 835 } 836 837 SDUse *operator->() const { return &operator*(); } 838 }; 839 840 class user_iterator { 841 friend class SDNode; 842 use_iterator UI; 843 844 explicit user_iterator(SDUse *op) : UI(op) {}; 845 846 public: 847 using iterator_category = std::forward_iterator_tag; 848 using value_type = SDNode *; 849 using difference_type = std::ptrdiff_t; 850 using pointer = value_type *; 851 using reference = value_type &; 852 853 user_iterator() = default; 854 855 bool operator==(const user_iterator &x) const { return UI == x.UI; } 856 bool operator!=(const user_iterator &x) const { return !operator==(x); } 857 858 user_iterator &operator++() { // Preincrement 859 ++UI; 860 return *this; 861 } 862 863 user_iterator operator++(int) { // Postincrement 864 auto tmp = *this; 865 ++*this; 866 return tmp; 867 } 868 869 // Retrieve a pointer to the current User. 870 SDNode *operator*() const { return UI->getUser(); } 871 872 SDNode *operator->() const { return operator*(); } 873 874 SDUse &getUse() const { return *UI; } 875 }; 876 877 /// Provide iteration support to walk over all uses of an SDNode. 878 use_iterator use_begin() const { 879 return use_iterator(UseList); 880 } 881 882 static use_iterator use_end() { return use_iterator(nullptr); } 883 884 inline iterator_range<use_iterator> uses() { 885 return make_range(use_begin(), use_end()); 886 } 887 inline iterator_range<use_iterator> uses() const { 888 return make_range(use_begin(), use_end()); 889 } 890 891 /// Provide iteration support to walk over all users of an SDNode. 892 user_iterator user_begin() const { return user_iterator(UseList); } 893 894 static user_iterator user_end() { return user_iterator(nullptr); } 895 896 inline iterator_range<user_iterator> users() { 897 return make_range(user_begin(), user_end()); 898 } 899 inline iterator_range<user_iterator> users() const { 900 return make_range(user_begin(), user_end()); 901 } 902 903 /// Return true if there are exactly NUSES uses of the indicated value. 904 /// This method ignores uses of other values defined by this operation. 905 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const { 906 assert(Value < getNumValues() && "Bad value!"); 907 908 // TODO: Only iterate over uses of a given value of the node 909 for (SDUse &U : uses()) { 910 if (U.getResNo() == Value) { 911 if (NUses == 0) 912 return false; 913 --NUses; 914 } 915 } 916 917 // Found exactly the right number of uses? 918 return NUses == 0; 919 } 920 921 /// Return true if there are any use of the indicated value. 922 /// This method ignores uses of other values defined by this operation. 923 LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const; 924 925 /// Return true if this node is the only use of N. 926 LLVM_ABI bool isOnlyUserOf(const SDNode *N) const; 927 928 /// Return true if this node is an operand of N. 929 LLVM_ABI bool isOperandOf(const SDNode *N) const; 930 931 /// Return true if this node is a predecessor of N. 932 /// NOTE: Implemented on top of hasPredecessor and every bit as 933 /// expensive. Use carefully. 934 bool isPredecessorOf(const SDNode *N) const { 935 return N->hasPredecessor(this); 936 } 937 938 /// Return true if N is a predecessor of this node. 939 /// N is either an operand of this node, or can be reached by recursively 940 /// traversing up the operands. 941 /// NOTE: This is an expensive method. Use it carefully. 942 LLVM_ABI bool hasPredecessor(const SDNode *N) const; 943 944 /// Returns true if N is a predecessor of any node in Worklist. This 945 /// helper keeps Visited and Worklist sets externally to allow unions 946 /// searches to be performed in parallel, caching of results across 947 /// queries and incremental addition to Worklist. Stops early if N is 948 /// found but will resume. Remember to clear Visited and Worklists 949 /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before 950 /// giving up. The TopologicalPrune flag signals that positive NodeIds are 951 /// topologically ordered (Operands have strictly smaller node id) and search 952 /// can be pruned leveraging this. 953 static bool hasPredecessorHelper(const SDNode *N, 954 SmallPtrSetImpl<const SDNode *> &Visited, 955 SmallVectorImpl<const SDNode *> &Worklist, 956 unsigned int MaxSteps = 0, 957 bool TopologicalPrune = false) { 958 if (Visited.count(N)) 959 return true; 960 961 SmallVector<const SDNode *, 8> DeferredNodes; 962 // Node Id's are assigned in three places: As a topological 963 // ordering (> 0), during legalization (results in values set to 964 // 0), new nodes (set to -1). If N has a topolgical id then we 965 // know that all nodes with ids smaller than it cannot be 966 // successors and we need not check them. Filter out all node 967 // that can't be matches. We add them to the worklist before exit 968 // in case of multiple calls. Note that during selection the topological id 969 // may be violated if a node's predecessor is selected before it. We mark 970 // this at selection negating the id of unselected successors and 971 // restricting topological pruning to positive ids. 972 973 int NId = N->getNodeId(); 974 // If we Invalidated the Id, reconstruct original NId. 975 if (NId < -1) 976 NId = -(NId + 1); 977 978 bool Found = false; 979 while (!Worklist.empty()) { 980 const SDNode *M = Worklist.pop_back_val(); 981 int MId = M->getNodeId(); 982 if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) && 983 (MId > 0) && (MId < NId)) { 984 DeferredNodes.push_back(M); 985 continue; 986 } 987 for (const SDValue &OpV : M->op_values()) { 988 SDNode *Op = OpV.getNode(); 989 if (Visited.insert(Op).second) 990 Worklist.push_back(Op); 991 if (Op == N) 992 Found = true; 993 } 994 if (Found) 995 break; 996 if (MaxSteps != 0 && Visited.size() >= MaxSteps) 997 break; 998 } 999 // Push deferred nodes back on worklist. 1000 Worklist.append(DeferredNodes.begin(), DeferredNodes.end()); 1001 // If we bailed early, conservatively return found. 1002 if (MaxSteps != 0 && Visited.size() >= MaxSteps) 1003 return true; 1004 return Found; 1005 } 1006 1007 /// Return true if all the users of N are contained in Nodes. 1008 /// NOTE: Requires at least one match, but doesn't require them all. 1009 LLVM_ABI static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, 1010 const SDNode *N); 1011 1012 /// Return the number of values used by this operation. 1013 unsigned getNumOperands() const { return NumOperands; } 1014 1015 /// Return the maximum number of operands that a SDNode can hold. 1016 static constexpr size_t getMaxNumOperands() { 1017 return std::numeric_limits<decltype(SDNode::NumOperands)>::max(); 1018 } 1019 1020 /// Helper method returns the integer value of a ConstantSDNode operand. 1021 inline uint64_t getConstantOperandVal(unsigned Num) const; 1022 1023 /// Helper method returns the zero-extended integer value of a ConstantSDNode. 1024 inline uint64_t getAsZExtVal() const; 1025 1026 /// Helper method returns the APInt of a ConstantSDNode operand. 1027 inline const APInt &getConstantOperandAPInt(unsigned Num) const; 1028 1029 /// Helper method returns the APInt value of a ConstantSDNode. 1030 inline const APInt &getAsAPIntVal() const; 1031 1032 inline std::optional<APInt> bitcastToAPInt() const; 1033 1034 const SDValue &getOperand(unsigned Num) const { 1035 assert(Num < NumOperands && "Invalid child # of SDNode!"); 1036 return OperandList[Num]; 1037 } 1038 1039 using op_iterator = SDUse *; 1040 1041 op_iterator op_begin() const { return OperandList; } 1042 op_iterator op_end() const { return OperandList+NumOperands; } 1043 ArrayRef<SDUse> ops() const { return ArrayRef(op_begin(), op_end()); } 1044 1045 /// Iterator for directly iterating over the operand SDValue's. 1046 struct value_op_iterator 1047 : iterator_adaptor_base<value_op_iterator, op_iterator, 1048 std::random_access_iterator_tag, SDValue, 1049 ptrdiff_t, value_op_iterator *, 1050 value_op_iterator *> { 1051 explicit value_op_iterator(SDUse *U = nullptr) 1052 : iterator_adaptor_base(U) {} 1053 1054 const SDValue &operator*() const { return I->get(); } 1055 }; 1056 1057 iterator_range<value_op_iterator> op_values() const { 1058 return make_range(value_op_iterator(op_begin()), 1059 value_op_iterator(op_end())); 1060 } 1061 1062 SDVTList getVTList() const { 1063 SDVTList X = { ValueList, NumValues }; 1064 return X; 1065 } 1066 1067 /// If this node has a glue operand, return the node 1068 /// to which the glue operand points. Otherwise return NULL. 1069 SDNode *getGluedNode() const { 1070 if (getNumOperands() != 0 && 1071 getOperand(getNumOperands()-1).getValueType() == MVT::Glue) 1072 return getOperand(getNumOperands()-1).getNode(); 1073 return nullptr; 1074 } 1075 1076 /// If this node has a glue value with a user, return 1077 /// the user (there is at most one). Otherwise return NULL. 1078 SDNode *getGluedUser() const { 1079 for (SDUse &U : uses()) 1080 if (U.getValueType() == MVT::Glue) 1081 return U.getUser(); 1082 return nullptr; 1083 } 1084 1085 SDNodeFlags getFlags() const { return Flags; } 1086 void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; } 1087 void dropFlags(unsigned Mask) { Flags &= ~Mask; } 1088 1089 /// Clear any flags in this node that aren't also set in Flags. 1090 /// If Flags is not in a defined state then this has no effect. 1091 LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags); 1092 1093 bool hasPoisonGeneratingFlags() const { 1094 return Flags.Flags & SDNodeFlags::PoisonGeneratingFlags; 1095 } 1096 1097 void setCFIType(uint32_t Type) { CFIType = Type; } 1098 uint32_t getCFIType() const { return CFIType; } 1099 1100 /// Return the number of values defined/returned by this operator. 1101 unsigned getNumValues() const { return NumValues; } 1102 1103 /// Return the type of a specified result. 1104 EVT getValueType(unsigned ResNo) const { 1105 assert(ResNo < NumValues && "Illegal result number!"); 1106 return ValueList[ResNo]; 1107 } 1108 1109 /// Return the type of a specified result as a simple type. 1110 MVT getSimpleValueType(unsigned ResNo) const { 1111 return getValueType(ResNo).getSimpleVT(); 1112 } 1113 1114 /// Returns MVT::getSizeInBits(getValueType(ResNo)). 1115 /// 1116 /// If the value type is a scalable vector type, the scalable property will 1117 /// be set and the runtime size will be a positive integer multiple of the 1118 /// base size. 1119 TypeSize getValueSizeInBits(unsigned ResNo) const { 1120 return getValueType(ResNo).getSizeInBits(); 1121 } 1122 1123 using value_iterator = const EVT *; 1124 1125 value_iterator value_begin() const { return ValueList; } 1126 value_iterator value_end() const { return ValueList+NumValues; } 1127 iterator_range<value_iterator> values() const { 1128 return llvm::make_range(value_begin(), value_end()); 1129 } 1130 1131 /// Return the opcode of this operation for printing. 1132 LLVM_ABI std::string getOperationName(const SelectionDAG *G = nullptr) const; 1133 LLVM_ABI static const char *getIndexedModeName(ISD::MemIndexedMode AM); 1134 LLVM_ABI void print_types(raw_ostream &OS, const SelectionDAG *G) const; 1135 LLVM_ABI void print_details(raw_ostream &OS, const SelectionDAG *G) const; 1136 LLVM_ABI void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const; 1137 LLVM_ABI void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const; 1138 1139 /// Print a SelectionDAG node and all children down to 1140 /// the leaves. The given SelectionDAG allows target-specific nodes 1141 /// to be printed in human-readable form. Unlike printr, this will 1142 /// print the whole DAG, including children that appear multiple 1143 /// times. 1144 /// 1145 LLVM_ABI void printrFull(raw_ostream &O, 1146 const SelectionDAG *G = nullptr) const; 1147 1148 /// Print a SelectionDAG node and children up to 1149 /// depth "depth." The given SelectionDAG allows target-specific 1150 /// nodes to be printed in human-readable form. Unlike printr, this 1151 /// will print children that appear multiple times wherever they are 1152 /// used. 1153 /// 1154 LLVM_ABI void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr, 1155 unsigned depth = 100) const; 1156 1157 /// Dump this node, for debugging. 1158 LLVM_ABI void dump() const; 1159 1160 /// Dump (recursively) this node and its use-def subgraph. 1161 LLVM_ABI void dumpr() const; 1162 1163 /// Dump this node, for debugging. 1164 /// The given SelectionDAG allows target-specific nodes to be printed 1165 /// in human-readable form. 1166 LLVM_ABI void dump(const SelectionDAG *G) const; 1167 1168 /// Dump (recursively) this node and its use-def subgraph. 1169 /// The given SelectionDAG allows target-specific nodes to be printed 1170 /// in human-readable form. 1171 LLVM_ABI void dumpr(const SelectionDAG *G) const; 1172 1173 /// printrFull to dbgs(). The given SelectionDAG allows 1174 /// target-specific nodes to be printed in human-readable form. 1175 /// Unlike dumpr, this will print the whole DAG, including children 1176 /// that appear multiple times. 1177 LLVM_ABI void dumprFull(const SelectionDAG *G = nullptr) const; 1178 1179 /// printrWithDepth to dbgs(). The given 1180 /// SelectionDAG allows target-specific nodes to be printed in 1181 /// human-readable form. Unlike dumpr, this will print children 1182 /// that appear multiple times wherever they are used. 1183 /// 1184 LLVM_ABI void dumprWithDepth(const SelectionDAG *G = nullptr, 1185 unsigned depth = 100) const; 1186 1187 /// Gather unique data for the node. 1188 LLVM_ABI void Profile(FoldingSetNodeID &ID) const; 1189 1190 /// This method should only be used by the SDUse class. 1191 void addUse(SDUse &U) { U.addToList(&UseList); } 1192 1193 protected: 1194 static SDVTList getSDVTList(MVT VT) { 1195 SDVTList Ret = { getValueTypeList(VT), 1 }; 1196 return Ret; 1197 } 1198 1199 /// Create an SDNode. 1200 /// 1201 /// SDNodes are created without any operands, and never own the operand 1202 /// storage. To add operands, see SelectionDAG::createOperands. 1203 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs) 1204 : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs), 1205 IROrder(Order), debugLoc(std::move(dl)) { 1206 memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits)); 1207 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); 1208 assert(NumValues == VTs.NumVTs && 1209 "NumValues wasn't wide enough for its operands!"); 1210 } 1211 1212 /// Release the operands and set this node to have zero operands. 1213 LLVM_ABI void DropOperands(); 1214 }; 1215 1216 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed 1217 /// into SDNode creation functions. 1218 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted 1219 /// from the original Instruction, and IROrder is the ordinal position of 1220 /// the instruction. 1221 /// When an SDNode is created after the DAG is being built, both DebugLoc and 1222 /// the IROrder are propagated from the original SDNode. 1223 /// So SDLoc class provides two constructors besides the default one, one to 1224 /// be used by the DAGBuilder, the other to be used by others. 1225 class SDLoc { 1226 private: 1227 DebugLoc DL; 1228 int IROrder = 0; 1229 1230 public: 1231 SDLoc() = default; 1232 SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {} 1233 SDLoc(const SDValue V) : SDLoc(V.getNode()) {} 1234 SDLoc(const Instruction *I, int Order) : IROrder(Order) { 1235 assert(Order >= 0 && "bad IROrder"); 1236 if (I) 1237 DL = I->getDebugLoc(); 1238 } 1239 1240 unsigned getIROrder() const { return IROrder; } 1241 const DebugLoc &getDebugLoc() const { return DL; } 1242 }; 1243 1244 // Define inline functions from the SDValue class. 1245 1246 inline SDValue::SDValue(SDNode *node, unsigned resno) 1247 : Node(node), ResNo(resno) { 1248 // Explicitly check for !ResNo to avoid use-after-free, because there are 1249 // callers that use SDValue(N, 0) with a deleted N to indicate successful 1250 // combines. 1251 assert((!Node || !ResNo || ResNo < Node->getNumValues()) && 1252 "Invalid result number for the given node!"); 1253 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps."); 1254 } 1255 1256 inline unsigned SDValue::getOpcode() const { 1257 return Node->getOpcode(); 1258 } 1259 1260 inline EVT SDValue::getValueType() const { 1261 return Node->getValueType(ResNo); 1262 } 1263 1264 inline unsigned SDValue::getNumOperands() const { 1265 return Node->getNumOperands(); 1266 } 1267 1268 inline const SDValue &SDValue::getOperand(unsigned i) const { 1269 return Node->getOperand(i); 1270 } 1271 1272 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const { 1273 return Node->getConstantOperandVal(i); 1274 } 1275 1276 inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const { 1277 return Node->getConstantOperandAPInt(i); 1278 } 1279 1280 inline bool SDValue::isTargetOpcode() const { 1281 return Node->isTargetOpcode(); 1282 } 1283 1284 inline bool SDValue::isMachineOpcode() const { 1285 return Node->isMachineOpcode(); 1286 } 1287 1288 inline unsigned SDValue::getMachineOpcode() const { 1289 return Node->getMachineOpcode(); 1290 } 1291 1292 inline bool SDValue::isUndef() const { 1293 return Node->isUndef(); 1294 } 1295 1296 inline bool SDValue::isAnyAdd() const { return Node->isAnyAdd(); } 1297 1298 inline bool SDValue::use_empty() const { 1299 return !Node->hasAnyUseOfValue(ResNo); 1300 } 1301 1302 inline bool SDValue::hasOneUse() const { 1303 return Node->hasNUsesOfValue(1, ResNo); 1304 } 1305 1306 inline const DebugLoc &SDValue::getDebugLoc() const { 1307 return Node->getDebugLoc(); 1308 } 1309 1310 inline void SDValue::dump() const { 1311 return Node->dump(); 1312 } 1313 1314 inline void SDValue::dump(const SelectionDAG *G) const { 1315 return Node->dump(G); 1316 } 1317 1318 inline void SDValue::dumpr() const { 1319 return Node->dumpr(); 1320 } 1321 1322 inline void SDValue::dumpr(const SelectionDAG *G) const { 1323 return Node->dumpr(G); 1324 } 1325 1326 // Define inline functions from the SDUse class. 1327 inline unsigned SDUse::getOperandNo() const { 1328 return this - getUser()->op_begin(); 1329 } 1330 1331 inline void SDUse::set(const SDValue &V) { 1332 if (Val.getNode()) removeFromList(); 1333 Val = V; 1334 if (V.getNode()) 1335 V->addUse(*this); 1336 } 1337 1338 inline void SDUse::setInitial(const SDValue &V) { 1339 Val = V; 1340 V->addUse(*this); 1341 } 1342 1343 inline void SDUse::setNode(SDNode *N) { 1344 if (Val.getNode()) removeFromList(); 1345 Val.setNode(N); 1346 if (N) N->addUse(*this); 1347 } 1348 1349 /// This class is used to form a handle around another node that 1350 /// is persistent and is updated across invocations of replaceAllUsesWith on its 1351 /// operand. This node should be directly created by end-users and not added to 1352 /// the AllNodes list. 1353 class HandleSDNode : public SDNode { 1354 SDUse Op; 1355 1356 public: 1357 explicit HandleSDNode(SDValue X) 1358 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) { 1359 // HandleSDNodes are never inserted into the DAG, so they won't be 1360 // auto-numbered. Use ID 65535 as a sentinel. 1361 PersistentId = 0xffff; 1362 1363 // Manually set up the operand list. This node type is special in that it's 1364 // always stack allocated and SelectionDAG does not manage its operands. 1365 // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not 1366 // be so special. 1367 Op.setUser(this); 1368 Op.setInitial(X); 1369 NumOperands = 1; 1370 OperandList = &Op; 1371 } 1372 LLVM_ABI ~HandleSDNode(); 1373 1374 const SDValue &getValue() const { return Op; } 1375 }; 1376 1377 class AddrSpaceCastSDNode : public SDNode { 1378 private: 1379 unsigned SrcAddrSpace; 1380 unsigned DestAddrSpace; 1381 1382 public: 1383 AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 1384 unsigned SrcAS, unsigned DestAS) 1385 : SDNode(ISD::ADDRSPACECAST, Order, dl, VTs), SrcAddrSpace(SrcAS), 1386 DestAddrSpace(DestAS) {} 1387 1388 unsigned getSrcAddressSpace() const { return SrcAddrSpace; } 1389 unsigned getDestAddressSpace() const { return DestAddrSpace; } 1390 1391 static bool classof(const SDNode *N) { 1392 return N->getOpcode() == ISD::ADDRSPACECAST; 1393 } 1394 }; 1395 1396 /// This is an abstract virtual class for memory operations. 1397 class MemSDNode : public SDNode { 1398 private: 1399 // VT of in-memory value. 1400 EVT MemoryVT; 1401 1402 protected: 1403 /// Memory reference information. 1404 MachineMemOperand *MMO; 1405 1406 public: 1407 LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, 1408 SDVTList VTs, EVT memvt, MachineMemOperand *MMO); 1409 1410 bool readMem() const { return MMO->isLoad(); } 1411 bool writeMem() const { return MMO->isStore(); } 1412 1413 /// Returns alignment and volatility of the memory access 1414 Align getBaseAlign() const { return MMO->getBaseAlign(); } 1415 Align getAlign() const { return MMO->getAlign(); } 1416 1417 /// Return the SubclassData value, without HasDebugValue. This contains an 1418 /// encoding of the volatile flag, as well as bits used by subclasses. This 1419 /// function should only be used to compute a FoldingSetNodeID value. 1420 /// The HasDebugValue bit is masked out because CSE map needs to match 1421 /// nodes with debug info with nodes without debug info. Same is about 1422 /// isDivergent bit. 1423 unsigned getRawSubclassData() const { 1424 uint16_t Data; 1425 union { 1426 char RawSDNodeBits[sizeof(uint16_t)]; 1427 SDNodeBitfields SDNodeBits; 1428 }; 1429 memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits)); 1430 SDNodeBits.HasDebugValue = 0; 1431 SDNodeBits.IsDivergent = false; 1432 memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits)); 1433 return Data; 1434 } 1435 1436 bool isVolatile() const { return MemSDNodeBits.IsVolatile; } 1437 bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; } 1438 bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; } 1439 bool isInvariant() const { return MemSDNodeBits.IsInvariant; } 1440 1441 // Returns the offset from the location of the access. 1442 int64_t getSrcValueOffset() const { return MMO->getOffset(); } 1443 1444 /// Returns the AA info that describes the dereference. 1445 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); } 1446 1447 /// Returns the Ranges that describes the dereference. 1448 const MDNode *getRanges() const { return MMO->getRanges(); } 1449 1450 /// Returns the synchronization scope ID for this memory operation. 1451 SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); } 1452 1453 /// Return the atomic ordering requirements for this memory operation. For 1454 /// cmpxchg atomic operations, return the atomic ordering requirements when 1455 /// store occurs. 1456 AtomicOrdering getSuccessOrdering() const { 1457 return MMO->getSuccessOrdering(); 1458 } 1459 1460 /// Return a single atomic ordering that is at least as strong as both the 1461 /// success and failure orderings for an atomic operation. (For operations 1462 /// other than cmpxchg, this is equivalent to getSuccessOrdering().) 1463 AtomicOrdering getMergedOrdering() const { return MMO->getMergedOrdering(); } 1464 1465 /// Return true if the memory operation ordering is Unordered or higher. 1466 bool isAtomic() const { return MMO->isAtomic(); } 1467 1468 /// Returns true if the memory operation doesn't imply any ordering 1469 /// constraints on surrounding memory operations beyond the normal memory 1470 /// aliasing rules. 1471 bool isUnordered() const { return MMO->isUnordered(); } 1472 1473 /// Returns true if the memory operation is neither atomic or volatile. 1474 bool isSimple() const { return !isAtomic() && !isVolatile(); } 1475 1476 /// Return the type of the in-memory value. 1477 EVT getMemoryVT() const { return MemoryVT; } 1478 1479 /// Return a MachineMemOperand object describing the memory 1480 /// reference performed by operation. 1481 MachineMemOperand *getMemOperand() const { return MMO; } 1482 1483 const MachinePointerInfo &getPointerInfo() const { 1484 return MMO->getPointerInfo(); 1485 } 1486 1487 /// Return the address space for the associated pointer 1488 unsigned getAddressSpace() const { 1489 return getPointerInfo().getAddrSpace(); 1490 } 1491 1492 /// Update this MemSDNode's MachineMemOperand information 1493 /// to reflect the alignment of NewMMO, if it has a greater alignment. 1494 /// This must only be used when the new alignment applies to all users of 1495 /// this MachineMemOperand. 1496 void refineAlignment(const MachineMemOperand *NewMMO) { 1497 MMO->refineAlignment(NewMMO); 1498 } 1499 1500 void refineRanges(const MachineMemOperand *NewMMO) { 1501 // If this node has range metadata that is different than NewMMO, clear the 1502 // range metadata. 1503 // FIXME: Union the ranges instead? 1504 if (getRanges() && getRanges() != NewMMO->getRanges()) 1505 MMO->clearRanges(); 1506 } 1507 1508 const SDValue &getChain() const { return getOperand(0); } 1509 1510 const SDValue &getBasePtr() const { 1511 switch (getOpcode()) { 1512 case ISD::STORE: 1513 case ISD::ATOMIC_STORE: 1514 case ISD::VP_STORE: 1515 case ISD::MSTORE: 1516 case ISD::VP_SCATTER: 1517 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 1518 return getOperand(2); 1519 case ISD::MGATHER: 1520 case ISD::MSCATTER: 1521 case ISD::EXPERIMENTAL_VECTOR_HISTOGRAM: 1522 return getOperand(3); 1523 default: 1524 return getOperand(1); 1525 } 1526 } 1527 1528 // Methods to support isa and dyn_cast 1529 static bool classof(const SDNode *N) { 1530 // For some targets, we lower some target intrinsics to a MemIntrinsicNode 1531 // with either an intrinsic or a target opcode. 1532 switch (N->getOpcode()) { 1533 case ISD::LOAD: 1534 case ISD::STORE: 1535 case ISD::ATOMIC_CMP_SWAP: 1536 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 1537 case ISD::ATOMIC_SWAP: 1538 case ISD::ATOMIC_LOAD_ADD: 1539 case ISD::ATOMIC_LOAD_SUB: 1540 case ISD::ATOMIC_LOAD_AND: 1541 case ISD::ATOMIC_LOAD_CLR: 1542 case ISD::ATOMIC_LOAD_OR: 1543 case ISD::ATOMIC_LOAD_XOR: 1544 case ISD::ATOMIC_LOAD_NAND: 1545 case ISD::ATOMIC_LOAD_MIN: 1546 case ISD::ATOMIC_LOAD_MAX: 1547 case ISD::ATOMIC_LOAD_UMIN: 1548 case ISD::ATOMIC_LOAD_UMAX: 1549 case ISD::ATOMIC_LOAD_FADD: 1550 case ISD::ATOMIC_LOAD_FSUB: 1551 case ISD::ATOMIC_LOAD_FMAX: 1552 case ISD::ATOMIC_LOAD_FMIN: 1553 case ISD::ATOMIC_LOAD_FMAXIMUM: 1554 case ISD::ATOMIC_LOAD_FMINIMUM: 1555 case ISD::ATOMIC_LOAD_UINC_WRAP: 1556 case ISD::ATOMIC_LOAD_UDEC_WRAP: 1557 case ISD::ATOMIC_LOAD_USUB_COND: 1558 case ISD::ATOMIC_LOAD_USUB_SAT: 1559 case ISD::ATOMIC_LOAD: 1560 case ISD::ATOMIC_STORE: 1561 case ISD::MLOAD: 1562 case ISD::MSTORE: 1563 case ISD::MGATHER: 1564 case ISD::MSCATTER: 1565 case ISD::VP_LOAD: 1566 case ISD::VP_STORE: 1567 case ISD::VP_GATHER: 1568 case ISD::VP_SCATTER: 1569 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: 1570 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 1571 case ISD::GET_FPENV_MEM: 1572 case ISD::SET_FPENV_MEM: 1573 case ISD::EXPERIMENTAL_VECTOR_HISTOGRAM: 1574 return true; 1575 default: 1576 return N->isMemIntrinsic(); 1577 } 1578 } 1579 }; 1580 1581 /// This is an SDNode representing atomic operations. 1582 class AtomicSDNode : public MemSDNode { 1583 public: 1584 AtomicSDNode(unsigned Order, const DebugLoc &dl, unsigned Opc, SDVTList VTL, 1585 EVT MemVT, MachineMemOperand *MMO, ISD::LoadExtType ETy) 1586 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) { 1587 assert(((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) || 1588 MMO->isAtomic()) && "then why are we using an AtomicSDNode?"); 1589 assert((Opc == ISD::ATOMIC_LOAD || ETy == ISD::NON_EXTLOAD) && 1590 "Only atomic load uses ExtTy"); 1591 LoadSDNodeBits.ExtTy = ETy; 1592 } 1593 1594 ISD::LoadExtType getExtensionType() const { 1595 assert(getOpcode() == ISD::ATOMIC_LOAD && "Only used for atomic loads."); 1596 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy); 1597 } 1598 1599 const SDValue &getBasePtr() const { 1600 return getOpcode() == ISD::ATOMIC_STORE ? getOperand(2) : getOperand(1); 1601 } 1602 const SDValue &getVal() const { 1603 return getOpcode() == ISD::ATOMIC_STORE ? getOperand(1) : getOperand(2); 1604 } 1605 1606 /// Returns true if this SDNode represents cmpxchg atomic operation, false 1607 /// otherwise. 1608 bool isCompareAndSwap() const { 1609 unsigned Op = getOpcode(); 1610 return Op == ISD::ATOMIC_CMP_SWAP || 1611 Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS; 1612 } 1613 1614 /// For cmpxchg atomic operations, return the atomic ordering requirements 1615 /// when store does not occur. 1616 AtomicOrdering getFailureOrdering() const { 1617 assert(isCompareAndSwap() && "Must be cmpxchg operation"); 1618 return MMO->getFailureOrdering(); 1619 } 1620 1621 // Methods to support isa and dyn_cast 1622 static bool classof(const SDNode *N) { 1623 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP || 1624 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS || 1625 N->getOpcode() == ISD::ATOMIC_SWAP || 1626 N->getOpcode() == ISD::ATOMIC_LOAD_ADD || 1627 N->getOpcode() == ISD::ATOMIC_LOAD_SUB || 1628 N->getOpcode() == ISD::ATOMIC_LOAD_AND || 1629 N->getOpcode() == ISD::ATOMIC_LOAD_CLR || 1630 N->getOpcode() == ISD::ATOMIC_LOAD_OR || 1631 N->getOpcode() == ISD::ATOMIC_LOAD_XOR || 1632 N->getOpcode() == ISD::ATOMIC_LOAD_NAND || 1633 N->getOpcode() == ISD::ATOMIC_LOAD_MIN || 1634 N->getOpcode() == ISD::ATOMIC_LOAD_MAX || 1635 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN || 1636 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX || 1637 N->getOpcode() == ISD::ATOMIC_LOAD_FADD || 1638 N->getOpcode() == ISD::ATOMIC_LOAD_FSUB || 1639 N->getOpcode() == ISD::ATOMIC_LOAD_FMAX || 1640 N->getOpcode() == ISD::ATOMIC_LOAD_FMIN || 1641 N->getOpcode() == ISD::ATOMIC_LOAD_FMAXIMUM || 1642 N->getOpcode() == ISD::ATOMIC_LOAD_FMINIMUM || 1643 N->getOpcode() == ISD::ATOMIC_LOAD_UINC_WRAP || 1644 N->getOpcode() == ISD::ATOMIC_LOAD_UDEC_WRAP || 1645 N->getOpcode() == ISD::ATOMIC_LOAD_USUB_COND || 1646 N->getOpcode() == ISD::ATOMIC_LOAD_USUB_SAT || 1647 N->getOpcode() == ISD::ATOMIC_LOAD || 1648 N->getOpcode() == ISD::ATOMIC_STORE; 1649 } 1650 }; 1651 1652 /// This SDNode is used for target intrinsics that touch memory and need 1653 /// an associated MachineMemOperand. Its opcode may be INTRINSIC_VOID, 1654 /// INTRINSIC_W_CHAIN, PREFETCH, or a target-specific memory-referencing 1655 /// opcode (see `SelectionDAGTargetInfo::isTargetMemoryOpcode`). 1656 class MemIntrinsicSDNode : public MemSDNode { 1657 public: 1658 MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, 1659 SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO) 1660 : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) { 1661 SDNodeBits.IsMemIntrinsic = true; 1662 } 1663 1664 // Methods to support isa and dyn_cast 1665 static bool classof(const SDNode *N) { 1666 // We lower some target intrinsics to their target opcode 1667 // early a node with a target opcode can be of this class 1668 return N->isMemIntrinsic(); 1669 } 1670 }; 1671 1672 /// This SDNode is used to implement the code generator 1673 /// support for the llvm IR shufflevector instruction. It combines elements 1674 /// from two input vectors into a new input vector, with the selection and 1675 /// ordering of elements determined by an array of integers, referred to as 1676 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1 1677 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS. 1678 /// An index of -1 is treated as undef, such that the code generator may put 1679 /// any value in the corresponding element of the result. 1680 class ShuffleVectorSDNode : public SDNode { 1681 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and 1682 // is freed when the SelectionDAG object is destroyed. 1683 const int *Mask; 1684 1685 protected: 1686 friend class SelectionDAG; 1687 1688 ShuffleVectorSDNode(SDVTList VTs, unsigned Order, const DebugLoc &dl, 1689 const int *M) 1690 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, VTs), Mask(M) {} 1691 1692 public: 1693 ArrayRef<int> getMask() const { 1694 EVT VT = getValueType(0); 1695 return ArrayRef(Mask, VT.getVectorNumElements()); 1696 } 1697 1698 int getMaskElt(unsigned Idx) const { 1699 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!"); 1700 return Mask[Idx]; 1701 } 1702 1703 bool isSplat() const { return isSplatMask(getMask()); } 1704 1705 int getSplatIndex() const { return getSplatMaskIndex(getMask()); } 1706 1707 LLVM_ABI static bool isSplatMask(ArrayRef<int> Mask); 1708 1709 static int getSplatMaskIndex(ArrayRef<int> Mask) { 1710 assert(isSplatMask(Mask) && "Cannot get splat index for non-splat!"); 1711 for (int Elem : Mask) 1712 if (Elem >= 0) 1713 return Elem; 1714 1715 // We can choose any index value here and be correct because all elements 1716 // are undefined. Return 0 for better potential for callers to simplify. 1717 return 0; 1718 } 1719 1720 /// Change values in a shuffle permute mask assuming 1721 /// the two vector operands have swapped position. 1722 static void commuteMask(MutableArrayRef<int> Mask) { 1723 unsigned NumElems = Mask.size(); 1724 for (unsigned i = 0; i != NumElems; ++i) { 1725 int idx = Mask[i]; 1726 if (idx < 0) 1727 continue; 1728 else if (idx < (int)NumElems) 1729 Mask[i] = idx + NumElems; 1730 else 1731 Mask[i] = idx - NumElems; 1732 } 1733 } 1734 1735 static bool classof(const SDNode *N) { 1736 return N->getOpcode() == ISD::VECTOR_SHUFFLE; 1737 } 1738 }; 1739 1740 class ConstantSDNode : public SDNode { 1741 friend class SelectionDAG; 1742 1743 const ConstantInt *Value; 1744 1745 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, 1746 SDVTList VTs) 1747 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(), 1748 VTs), 1749 Value(val) { 1750 assert(!isa<VectorType>(val->getType()) && "Unexpected vector type!"); 1751 ConstantSDNodeBits.IsOpaque = isOpaque; 1752 } 1753 1754 public: 1755 const ConstantInt *getConstantIntValue() const { return Value; } 1756 const APInt &getAPIntValue() const { return Value->getValue(); } 1757 uint64_t getZExtValue() const { return Value->getZExtValue(); } 1758 int64_t getSExtValue() const { return Value->getSExtValue(); } 1759 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) { 1760 return Value->getLimitedValue(Limit); 1761 } 1762 MaybeAlign getMaybeAlignValue() const { return Value->getMaybeAlignValue(); } 1763 Align getAlignValue() const { return Value->getAlignValue(); } 1764 1765 bool isOne() const { return Value->isOne(); } 1766 bool isZero() const { return Value->isZero(); } 1767 bool isAllOnes() const { return Value->isMinusOne(); } 1768 bool isMaxSignedValue() const { return Value->isMaxValue(true); } 1769 bool isMinSignedValue() const { return Value->isMinValue(true); } 1770 1771 bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; } 1772 1773 static bool classof(const SDNode *N) { 1774 return N->getOpcode() == ISD::Constant || 1775 N->getOpcode() == ISD::TargetConstant; 1776 } 1777 }; 1778 1779 uint64_t SDNode::getConstantOperandVal(unsigned Num) const { 1780 return cast<ConstantSDNode>(getOperand(Num))->getZExtValue(); 1781 } 1782 1783 uint64_t SDNode::getAsZExtVal() const { 1784 return cast<ConstantSDNode>(this)->getZExtValue(); 1785 } 1786 1787 const APInt &SDNode::getConstantOperandAPInt(unsigned Num) const { 1788 return cast<ConstantSDNode>(getOperand(Num))->getAPIntValue(); 1789 } 1790 1791 const APInt &SDNode::getAsAPIntVal() const { 1792 return cast<ConstantSDNode>(this)->getAPIntValue(); 1793 } 1794 1795 class ConstantFPSDNode : public SDNode { 1796 friend class SelectionDAG; 1797 1798 const ConstantFP *Value; 1799 1800 ConstantFPSDNode(bool isTarget, const ConstantFP *val, SDVTList VTs) 1801 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0, 1802 DebugLoc(), VTs), 1803 Value(val) { 1804 assert(!isa<VectorType>(val->getType()) && "Unexpected vector type!"); 1805 } 1806 1807 public: 1808 const APFloat& getValueAPF() const { return Value->getValueAPF(); } 1809 const ConstantFP *getConstantFPValue() const { return Value; } 1810 1811 /// Return true if the value is positive or negative zero. 1812 bool isZero() const { return Value->isZero(); } 1813 1814 /// Return true if the value is a NaN. 1815 bool isNaN() const { return Value->isNaN(); } 1816 1817 /// Return true if the value is an infinity 1818 bool isInfinity() const { return Value->isInfinity(); } 1819 1820 /// Return true if the value is negative. 1821 bool isNegative() const { return Value->isNegative(); } 1822 1823 /// We don't rely on operator== working on double values, as 1824 /// it returns true for things that are clearly not equal, like -0.0 and 0.0. 1825 /// As such, this method can be used to do an exact bit-for-bit comparison of 1826 /// two floating point values. 1827 1828 /// We leave the version with the double argument here because it's just so 1829 /// convenient to write "2.0" and the like. Without this function we'd 1830 /// have to duplicate its logic everywhere it's called. 1831 bool isExactlyValue(double V) const { 1832 return Value->getValueAPF().isExactlyValue(V); 1833 } 1834 LLVM_ABI bool isExactlyValue(const APFloat &V) const; 1835 1836 LLVM_ABI static bool isValueValidForType(EVT VT, const APFloat &Val); 1837 1838 static bool classof(const SDNode *N) { 1839 return N->getOpcode() == ISD::ConstantFP || 1840 N->getOpcode() == ISD::TargetConstantFP; 1841 } 1842 }; 1843 1844 std::optional<APInt> SDNode::bitcastToAPInt() const { 1845 if (auto *CN = dyn_cast<ConstantSDNode>(this)) 1846 return CN->getAPIntValue(); 1847 if (auto *CFPN = dyn_cast<ConstantFPSDNode>(this)) 1848 return CFPN->getValueAPF().bitcastToAPInt(); 1849 return std::nullopt; 1850 } 1851 1852 /// Returns true if \p V is a constant integer zero. 1853 LLVM_ABI bool isNullConstant(SDValue V); 1854 1855 /// Returns true if \p V is a constant integer zero or an UNDEF node. 1856 LLVM_ABI bool isNullConstantOrUndef(SDValue V); 1857 1858 /// Returns true if \p V is an FP constant with a value of positive zero. 1859 LLVM_ABI bool isNullFPConstant(SDValue V); 1860 1861 /// Returns true if \p V is an integer constant with all bits set. 1862 LLVM_ABI bool isAllOnesConstant(SDValue V); 1863 1864 /// Returns true if \p V is a constant integer one. 1865 LLVM_ABI bool isOneConstant(SDValue V); 1866 1867 /// Returns true if \p V is a constant min signed integer value. 1868 LLVM_ABI bool isMinSignedConstant(SDValue V); 1869 1870 /// Returns true if \p V is a neutral element of Opc with Flags. 1871 /// When OperandNo is 0, it checks that V is a left identity. Otherwise, it 1872 /// checks that V is a right identity. 1873 LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, 1874 unsigned OperandNo); 1875 1876 /// Return the non-bitcasted source operand of \p V if it exists. 1877 /// If \p V is not a bitcasted value, it is returned as-is. 1878 LLVM_ABI SDValue peekThroughBitcasts(SDValue V); 1879 1880 /// Return the non-bitcasted and one-use source operand of \p V if it exists. 1881 /// If \p V is not a bitcasted one-use value, it is returned as-is. 1882 LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V); 1883 1884 /// Return the non-extracted vector source operand of \p V if it exists. 1885 /// If \p V is not an extracted subvector, it is returned as-is. 1886 LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V); 1887 1888 /// Return the non-truncated source operand of \p V if it exists. 1889 /// If \p V is not a truncation, it is returned as-is. 1890 LLVM_ABI SDValue peekThroughTruncates(SDValue V); 1891 1892 /// Returns true if \p V is a bitwise not operation. Assumes that an all ones 1893 /// constant is canonicalized to be operand 1. 1894 LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs = false); 1895 1896 /// If \p V is a bitwise not, returns the inverted operand. Otherwise returns 1897 /// an empty SDValue. Only bits set in \p Mask are required to be inverted, 1898 /// other bits may be arbitrary. 1899 LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, 1900 bool AllowUndefs); 1901 1902 /// Returns the SDNode if it is a constant splat BuildVector or constant int. 1903 LLVM_ABI ConstantSDNode *isConstOrConstSplat(SDValue N, 1904 bool AllowUndefs = false, 1905 bool AllowTruncation = false); 1906 1907 /// Returns the SDNode if it is a demanded constant splat BuildVector or 1908 /// constant int. 1909 LLVM_ABI ConstantSDNode *isConstOrConstSplat(SDValue N, 1910 const APInt &DemandedElts, 1911 bool AllowUndefs = false, 1912 bool AllowTruncation = false); 1913 1914 /// Returns the SDNode if it is a constant splat BuildVector or constant float. 1915 LLVM_ABI ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, 1916 bool AllowUndefs = false); 1917 1918 /// Returns the SDNode if it is a demanded constant splat BuildVector or 1919 /// constant float. 1920 LLVM_ABI ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, 1921 const APInt &DemandedElts, 1922 bool AllowUndefs = false); 1923 1924 /// Return true if the value is a constant 0 integer or a splatted vector of 1925 /// a constant 0 integer (with no undefs by default). 1926 /// Build vector implicit truncation is not an issue for null values. 1927 LLVM_ABI bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false); 1928 1929 /// Return true if the value is a constant 1 integer or a splatted vector of a 1930 /// constant 1 integer (with no undefs). 1931 /// Build vector implicit truncation is allowed, but the truncated bits need to 1932 /// be zero. 1933 LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs = false); 1934 1935 /// Return true if the value is a constant -1 integer or a splatted vector of a 1936 /// constant -1 integer (with no undefs). 1937 /// Does not permit build vector implicit truncation. 1938 LLVM_ABI bool isAllOnesOrAllOnesSplat(SDValue V, bool AllowUndefs = false); 1939 1940 /// Return true if the value is a constant 1 integer or a splatted vector of a 1941 /// constant 1 integer (with no undefs). 1942 /// Does not permit build vector implicit truncation. 1943 LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs = false); 1944 1945 /// Return true if the value is a constant 0 integer or a splatted vector of a 1946 /// constant 0 integer (with no undefs). 1947 /// Does not permit build vector implicit truncation. 1948 LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs = false); 1949 1950 /// Return true if \p V is either a integer or FP constant. 1951 inline bool isIntOrFPConstant(SDValue V) { 1952 return isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V); 1953 } 1954 1955 class GlobalAddressSDNode : public SDNode { 1956 friend class SelectionDAG; 1957 1958 const GlobalValue *TheGlobal; 1959 int64_t Offset; 1960 unsigned TargetFlags; 1961 1962 GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, 1963 const GlobalValue *GA, SDVTList VTs, int64_t o, 1964 unsigned TF) 1965 : SDNode(Opc, Order, DL, VTs), TheGlobal(GA), Offset(o), TargetFlags(TF) { 1966 } 1967 1968 public: 1969 const GlobalValue *getGlobal() const { return TheGlobal; } 1970 int64_t getOffset() const { return Offset; } 1971 unsigned getTargetFlags() const { return TargetFlags; } 1972 // Return the address space this GlobalAddress belongs to. 1973 LLVM_ABI unsigned getAddressSpace() const; 1974 1975 static bool classof(const SDNode *N) { 1976 return N->getOpcode() == ISD::GlobalAddress || 1977 N->getOpcode() == ISD::TargetGlobalAddress || 1978 N->getOpcode() == ISD::GlobalTLSAddress || 1979 N->getOpcode() == ISD::TargetGlobalTLSAddress; 1980 } 1981 }; 1982 1983 class FrameIndexSDNode : public SDNode { 1984 friend class SelectionDAG; 1985 1986 int FI; 1987 1988 FrameIndexSDNode(int fi, SDVTList VTs, bool isTarg) 1989 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex, 0, DebugLoc(), 1990 VTs), 1991 FI(fi) {} 1992 1993 public: 1994 int getIndex() const { return FI; } 1995 1996 static bool classof(const SDNode *N) { 1997 return N->getOpcode() == ISD::FrameIndex || 1998 N->getOpcode() == ISD::TargetFrameIndex; 1999 } 2000 }; 2001 2002 /// This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate 2003 /// the offet and size that are started/ended in the underlying FrameIndex. 2004 class LifetimeSDNode : public SDNode { 2005 friend class SelectionDAG; 2006 int64_t Size; 2007 int64_t Offset; // -1 if offset is unknown. 2008 2009 LifetimeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, 2010 SDVTList VTs, int64_t Size, int64_t Offset) 2011 : SDNode(Opcode, Order, dl, VTs), Size(Size), Offset(Offset) {} 2012 public: 2013 int64_t getFrameIndex() const { 2014 return cast<FrameIndexSDNode>(getOperand(1))->getIndex(); 2015 } 2016 2017 bool hasOffset() const { return Offset >= 0; } 2018 int64_t getOffset() const { 2019 assert(hasOffset() && "offset is unknown"); 2020 return Offset; 2021 } 2022 int64_t getSize() const { 2023 assert(hasOffset() && "offset is unknown"); 2024 return Size; 2025 } 2026 2027 // Methods to support isa and dyn_cast 2028 static bool classof(const SDNode *N) { 2029 return N->getOpcode() == ISD::LIFETIME_START || 2030 N->getOpcode() == ISD::LIFETIME_END; 2031 } 2032 }; 2033 2034 /// This SDNode is used for PSEUDO_PROBE values, which are the function guid and 2035 /// the index of the basic block being probed. A pseudo probe serves as a place 2036 /// holder and will be removed at the end of compilation. It does not have any 2037 /// operand because we do not want the instruction selection to deal with any. 2038 class PseudoProbeSDNode : public SDNode { 2039 friend class SelectionDAG; 2040 uint64_t Guid; 2041 uint64_t Index; 2042 uint32_t Attributes; 2043 2044 PseudoProbeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &Dl, 2045 SDVTList VTs, uint64_t Guid, uint64_t Index, uint32_t Attr) 2046 : SDNode(Opcode, Order, Dl, VTs), Guid(Guid), Index(Index), 2047 Attributes(Attr) {} 2048 2049 public: 2050 uint64_t getGuid() const { return Guid; } 2051 uint64_t getIndex() const { return Index; } 2052 uint32_t getAttributes() const { return Attributes; } 2053 2054 // Methods to support isa and dyn_cast 2055 static bool classof(const SDNode *N) { 2056 return N->getOpcode() == ISD::PSEUDO_PROBE; 2057 } 2058 }; 2059 2060 class JumpTableSDNode : public SDNode { 2061 friend class SelectionDAG; 2062 2063 int JTI; 2064 unsigned TargetFlags; 2065 2066 JumpTableSDNode(int jti, SDVTList VTs, bool isTarg, unsigned TF) 2067 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, 0, DebugLoc(), 2068 VTs), 2069 JTI(jti), TargetFlags(TF) {} 2070 2071 public: 2072 int getIndex() const { return JTI; } 2073 unsigned getTargetFlags() const { return TargetFlags; } 2074 2075 static bool classof(const SDNode *N) { 2076 return N->getOpcode() == ISD::JumpTable || 2077 N->getOpcode() == ISD::TargetJumpTable; 2078 } 2079 }; 2080 2081 class ConstantPoolSDNode : public SDNode { 2082 friend class SelectionDAG; 2083 2084 union { 2085 const Constant *ConstVal; 2086 MachineConstantPoolValue *MachineCPVal; 2087 } Val; 2088 int Offset; // It's a MachineConstantPoolValue if top bit is set. 2089 Align Alignment; // Minimum alignment requirement of CP. 2090 unsigned TargetFlags; 2091 2092 ConstantPoolSDNode(bool isTarget, const Constant *c, SDVTList VTs, int o, 2093 Align Alignment, unsigned TF) 2094 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0, 2095 DebugLoc(), VTs), 2096 Offset(o), Alignment(Alignment), TargetFlags(TF) { 2097 assert(Offset >= 0 && "Offset is too large"); 2098 Val.ConstVal = c; 2099 } 2100 2101 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v, SDVTList VTs, 2102 int o, Align Alignment, unsigned TF) 2103 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0, 2104 DebugLoc(), VTs), 2105 Offset(o), Alignment(Alignment), TargetFlags(TF) { 2106 assert(Offset >= 0 && "Offset is too large"); 2107 Val.MachineCPVal = v; 2108 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1); 2109 } 2110 2111 public: 2112 bool isMachineConstantPoolEntry() const { 2113 return Offset < 0; 2114 } 2115 2116 const Constant *getConstVal() const { 2117 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type"); 2118 return Val.ConstVal; 2119 } 2120 2121 MachineConstantPoolValue *getMachineCPVal() const { 2122 assert(isMachineConstantPoolEntry() && "Wrong constantpool type"); 2123 return Val.MachineCPVal; 2124 } 2125 2126 int getOffset() const { 2127 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1)); 2128 } 2129 2130 // Return the alignment of this constant pool object, which is either 0 (for 2131 // default alignment) or the desired value. 2132 Align getAlign() const { return Alignment; } 2133 unsigned getTargetFlags() const { return TargetFlags; } 2134 2135 LLVM_ABI Type *getType() const; 2136 2137 static bool classof(const SDNode *N) { 2138 return N->getOpcode() == ISD::ConstantPool || 2139 N->getOpcode() == ISD::TargetConstantPool; 2140 } 2141 }; 2142 2143 /// Completely target-dependent object reference. 2144 class TargetIndexSDNode : public SDNode { 2145 friend class SelectionDAG; 2146 2147 unsigned TargetFlags; 2148 int Index; 2149 int64_t Offset; 2150 2151 public: 2152 TargetIndexSDNode(int Idx, SDVTList VTs, int64_t Ofs, unsigned TF) 2153 : SDNode(ISD::TargetIndex, 0, DebugLoc(), VTs), TargetFlags(TF), 2154 Index(Idx), Offset(Ofs) {} 2155 2156 unsigned getTargetFlags() const { return TargetFlags; } 2157 int getIndex() const { return Index; } 2158 int64_t getOffset() const { return Offset; } 2159 2160 static bool classof(const SDNode *N) { 2161 return N->getOpcode() == ISD::TargetIndex; 2162 } 2163 }; 2164 2165 class BasicBlockSDNode : public SDNode { 2166 friend class SelectionDAG; 2167 2168 MachineBasicBlock *MBB; 2169 2170 /// Debug info is meaningful and potentially useful here, but we create 2171 /// blocks out of order when they're jumped to, which makes it a bit 2172 /// harder. Let's see if we need it first. 2173 explicit BasicBlockSDNode(MachineBasicBlock *mbb) 2174 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) 2175 {} 2176 2177 public: 2178 MachineBasicBlock *getBasicBlock() const { return MBB; } 2179 2180 static bool classof(const SDNode *N) { 2181 return N->getOpcode() == ISD::BasicBlock; 2182 } 2183 }; 2184 2185 /// A "pseudo-class" with methods for operating on BUILD_VECTORs. 2186 class BuildVectorSDNode : public SDNode { 2187 public: 2188 // These are constructed as SDNodes and then cast to BuildVectorSDNodes. 2189 explicit BuildVectorSDNode() = delete; 2190 2191 /// Check if this is a constant splat, and if so, find the 2192 /// smallest element size that splats the vector. If MinSplatBits is 2193 /// nonzero, the element size must be at least that large. Note that the 2194 /// splat element may be the entire vector (i.e., a one element vector). 2195 /// Returns the splat element value in SplatValue. Any undefined bits in 2196 /// that value are zero, and the corresponding bits in the SplatUndef mask 2197 /// are set. The SplatBitSize value is set to the splat element size in 2198 /// bits. HasAnyUndefs is set to true if any bits in the vector are 2199 /// undefined. isBigEndian describes the endianness of the target. 2200 LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, 2201 unsigned &SplatBitSize, bool &HasAnyUndefs, 2202 unsigned MinSplatBits = 0, 2203 bool isBigEndian = false) const; 2204 2205 /// Returns the demanded splatted value or a null value if this is not a 2206 /// splat. 2207 /// 2208 /// The DemandedElts mask indicates the elements that must be in the splat. 2209 /// If passed a non-null UndefElements bitvector, it will resize it to match 2210 /// the vector width and set the bits where elements are undef. 2211 LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, 2212 BitVector *UndefElements = nullptr) const; 2213 2214 /// Returns the splatted value or a null value if this is not a splat. 2215 /// 2216 /// If passed a non-null UndefElements bitvector, it will resize it to match 2217 /// the vector width and set the bits where elements are undef. 2218 LLVM_ABI SDValue getSplatValue(BitVector *UndefElements = nullptr) const; 2219 2220 /// Find the shortest repeating sequence of values in the build vector. 2221 /// 2222 /// e.g. { u, X, u, X, u, u, X, u } -> { X } 2223 /// { X, Y, u, Y, u, u, X, u } -> { X, Y } 2224 /// 2225 /// Currently this must be a power-of-2 build vector. 2226 /// The DemandedElts mask indicates the elements that must be present, 2227 /// undemanded elements in Sequence may be null (SDValue()). If passed a 2228 /// non-null UndefElements bitvector, it will resize it to match the original 2229 /// vector width and set the bits where elements are undef. If result is 2230 /// false, Sequence will be empty. 2231 LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, 2232 SmallVectorImpl<SDValue> &Sequence, 2233 BitVector *UndefElements = nullptr) const; 2234 2235 /// Find the shortest repeating sequence of values in the build vector. 2236 /// 2237 /// e.g. { u, X, u, X, u, u, X, u } -> { X } 2238 /// { X, Y, u, Y, u, u, X, u } -> { X, Y } 2239 /// 2240 /// Currently this must be a power-of-2 build vector. 2241 /// If passed a non-null UndefElements bitvector, it will resize it to match 2242 /// the original vector width and set the bits where elements are undef. 2243 /// If result is false, Sequence will be empty. 2244 LLVM_ABI bool getRepeatedSequence(SmallVectorImpl<SDValue> &Sequence, 2245 BitVector *UndefElements = nullptr) const; 2246 2247 /// Returns the demanded splatted constant or null if this is not a constant 2248 /// splat. 2249 /// 2250 /// The DemandedElts mask indicates the elements that must be in the splat. 2251 /// If passed a non-null UndefElements bitvector, it will resize it to match 2252 /// the vector width and set the bits where elements are undef. 2253 LLVM_ABI ConstantSDNode * 2254 getConstantSplatNode(const APInt &DemandedElts, 2255 BitVector *UndefElements = nullptr) const; 2256 2257 /// Returns the splatted constant or null if this is not a constant 2258 /// splat. 2259 /// 2260 /// If passed a non-null UndefElements bitvector, it will resize it to match 2261 /// the vector width and set the bits where elements are undef. 2262 LLVM_ABI ConstantSDNode * 2263 getConstantSplatNode(BitVector *UndefElements = nullptr) const; 2264 2265 /// Returns the demanded splatted constant FP or null if this is not a 2266 /// constant FP splat. 2267 /// 2268 /// The DemandedElts mask indicates the elements that must be in the splat. 2269 /// If passed a non-null UndefElements bitvector, it will resize it to match 2270 /// the vector width and set the bits where elements are undef. 2271 LLVM_ABI ConstantFPSDNode * 2272 getConstantFPSplatNode(const APInt &DemandedElts, 2273 BitVector *UndefElements = nullptr) const; 2274 2275 /// Returns the splatted constant FP or null if this is not a constant 2276 /// FP splat. 2277 /// 2278 /// If passed a non-null UndefElements bitvector, it will resize it to match 2279 /// the vector width and set the bits where elements are undef. 2280 LLVM_ABI ConstantFPSDNode * 2281 getConstantFPSplatNode(BitVector *UndefElements = nullptr) const; 2282 2283 /// If this is a constant FP splat and the splatted constant FP is an 2284 /// exact power or 2, return the log base 2 integer value. Otherwise, 2285 /// return -1. 2286 /// 2287 /// The BitWidth specifies the necessary bit precision. 2288 LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, 2289 uint32_t BitWidth) const; 2290 2291 /// Extract the raw bit data from a build vector of Undef, Constant or 2292 /// ConstantFP node elements. Each raw bit element will be \p 2293 /// DstEltSizeInBits wide, undef elements are treated as zero, and entirely 2294 /// undefined elements are flagged in \p UndefElements. 2295 LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, 2296 unsigned DstEltSizeInBits, 2297 SmallVectorImpl<APInt> &RawBitElements, 2298 BitVector &UndefElements) const; 2299 2300 LLVM_ABI bool isConstant() const; 2301 2302 /// If this BuildVector is constant and represents the numerical series 2303 /// "<a, a+n, a+2n, a+3n, ...>" where a is integer and n is a non-zero integer, 2304 /// the value "<a,n>" is returned. 2305 LLVM_ABI std::optional<std::pair<APInt, APInt>> isConstantSequence() const; 2306 2307 /// Recast bit data \p SrcBitElements to \p DstEltSizeInBits wide elements. 2308 /// Undef elements are treated as zero, and entirely undefined elements are 2309 /// flagged in \p DstUndefElements. 2310 LLVM_ABI static void recastRawBits(bool IsLittleEndian, 2311 unsigned DstEltSizeInBits, 2312 SmallVectorImpl<APInt> &DstBitElements, 2313 ArrayRef<APInt> SrcBitElements, 2314 BitVector &DstUndefElements, 2315 const BitVector &SrcUndefElements); 2316 2317 static bool classof(const SDNode *N) { 2318 return N->getOpcode() == ISD::BUILD_VECTOR; 2319 } 2320 }; 2321 2322 /// An SDNode that holds an arbitrary LLVM IR Value. This is 2323 /// used when the SelectionDAG needs to make a simple reference to something 2324 /// in the LLVM IR representation. 2325 /// 2326 class SrcValueSDNode : public SDNode { 2327 friend class SelectionDAG; 2328 2329 const Value *V; 2330 2331 /// Create a SrcValue for a general value. 2332 explicit SrcValueSDNode(const Value *v) 2333 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {} 2334 2335 public: 2336 /// Return the contained Value. 2337 const Value *getValue() const { return V; } 2338 2339 static bool classof(const SDNode *N) { 2340 return N->getOpcode() == ISD::SRCVALUE; 2341 } 2342 }; 2343 2344 class MDNodeSDNode : public SDNode { 2345 friend class SelectionDAG; 2346 2347 const MDNode *MD; 2348 2349 explicit MDNodeSDNode(const MDNode *md) 2350 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md) 2351 {} 2352 2353 public: 2354 const MDNode *getMD() const { return MD; } 2355 2356 static bool classof(const SDNode *N) { 2357 return N->getOpcode() == ISD::MDNODE_SDNODE; 2358 } 2359 }; 2360 2361 class RegisterSDNode : public SDNode { 2362 friend class SelectionDAG; 2363 2364 Register Reg; 2365 2366 RegisterSDNode(Register reg, SDVTList VTs) 2367 : SDNode(ISD::Register, 0, DebugLoc(), VTs), Reg(reg) {} 2368 2369 public: 2370 Register getReg() const { return Reg; } 2371 2372 static bool classof(const SDNode *N) { 2373 return N->getOpcode() == ISD::Register; 2374 } 2375 }; 2376 2377 class RegisterMaskSDNode : public SDNode { 2378 friend class SelectionDAG; 2379 2380 // The memory for RegMask is not owned by the node. 2381 const uint32_t *RegMask; 2382 2383 RegisterMaskSDNode(const uint32_t *mask) 2384 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)), 2385 RegMask(mask) {} 2386 2387 public: 2388 const uint32_t *getRegMask() const { return RegMask; } 2389 2390 static bool classof(const SDNode *N) { 2391 return N->getOpcode() == ISD::RegisterMask; 2392 } 2393 }; 2394 2395 class BlockAddressSDNode : public SDNode { 2396 friend class SelectionDAG; 2397 2398 const BlockAddress *BA; 2399 int64_t Offset; 2400 unsigned TargetFlags; 2401 2402 BlockAddressSDNode(unsigned NodeTy, SDVTList VTs, const BlockAddress *ba, 2403 int64_t o, unsigned Flags) 2404 : SDNode(NodeTy, 0, DebugLoc(), VTs), BA(ba), Offset(o), 2405 TargetFlags(Flags) {} 2406 2407 public: 2408 const BlockAddress *getBlockAddress() const { return BA; } 2409 int64_t getOffset() const { return Offset; } 2410 unsigned getTargetFlags() const { return TargetFlags; } 2411 2412 static bool classof(const SDNode *N) { 2413 return N->getOpcode() == ISD::BlockAddress || 2414 N->getOpcode() == ISD::TargetBlockAddress; 2415 } 2416 }; 2417 2418 class LabelSDNode : public SDNode { 2419 friend class SelectionDAG; 2420 2421 MCSymbol *Label; 2422 2423 LabelSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, MCSymbol *L) 2424 : SDNode(Opcode, Order, dl, getSDVTList(MVT::Other)), Label(L) { 2425 assert(LabelSDNode::classof(this) && "not a label opcode"); 2426 } 2427 2428 public: 2429 MCSymbol *getLabel() const { return Label; } 2430 2431 static bool classof(const SDNode *N) { 2432 return N->getOpcode() == ISD::EH_LABEL || 2433 N->getOpcode() == ISD::ANNOTATION_LABEL; 2434 } 2435 }; 2436 2437 class ExternalSymbolSDNode : public SDNode { 2438 friend class SelectionDAG; 2439 2440 const char *Symbol; 2441 unsigned TargetFlags; 2442 2443 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned TF, 2444 SDVTList VTs) 2445 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, 0, 2446 DebugLoc(), VTs), 2447 Symbol(Sym), TargetFlags(TF) {} 2448 2449 public: 2450 const char *getSymbol() const { return Symbol; } 2451 unsigned getTargetFlags() const { return TargetFlags; } 2452 2453 static bool classof(const SDNode *N) { 2454 return N->getOpcode() == ISD::ExternalSymbol || 2455 N->getOpcode() == ISD::TargetExternalSymbol; 2456 } 2457 }; 2458 2459 class MCSymbolSDNode : public SDNode { 2460 friend class SelectionDAG; 2461 2462 MCSymbol *Symbol; 2463 2464 MCSymbolSDNode(MCSymbol *Symbol, SDVTList VTs) 2465 : SDNode(ISD::MCSymbol, 0, DebugLoc(), VTs), Symbol(Symbol) {} 2466 2467 public: 2468 MCSymbol *getMCSymbol() const { return Symbol; } 2469 2470 static bool classof(const SDNode *N) { 2471 return N->getOpcode() == ISD::MCSymbol; 2472 } 2473 }; 2474 2475 class CondCodeSDNode : public SDNode { 2476 friend class SelectionDAG; 2477 2478 ISD::CondCode Condition; 2479 2480 explicit CondCodeSDNode(ISD::CondCode Cond) 2481 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)), 2482 Condition(Cond) {} 2483 2484 public: 2485 ISD::CondCode get() const { return Condition; } 2486 2487 static bool classof(const SDNode *N) { 2488 return N->getOpcode() == ISD::CONDCODE; 2489 } 2490 }; 2491 2492 /// This class is used to represent EVT's, which are used 2493 /// to parameterize some operations. 2494 class VTSDNode : public SDNode { 2495 friend class SelectionDAG; 2496 2497 EVT ValueType; 2498 2499 explicit VTSDNode(EVT VT) 2500 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)), 2501 ValueType(VT) {} 2502 2503 public: 2504 EVT getVT() const { return ValueType; } 2505 2506 static bool classof(const SDNode *N) { 2507 return N->getOpcode() == ISD::VALUETYPE; 2508 } 2509 }; 2510 2511 /// Base class for LoadSDNode and StoreSDNode 2512 class LSBaseSDNode : public MemSDNode { 2513 public: 2514 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl, 2515 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT, 2516 MachineMemOperand *MMO) 2517 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) { 2518 LSBaseSDNodeBits.AddressingMode = AM; 2519 assert(getAddressingMode() == AM && "Value truncated"); 2520 } 2521 2522 const SDValue &getOffset() const { 2523 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3); 2524 } 2525 2526 /// Return the addressing mode for this load or store: 2527 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec. 2528 ISD::MemIndexedMode getAddressingMode() const { 2529 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode); 2530 } 2531 2532 /// Return true if this is a pre/post inc/dec load/store. 2533 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; } 2534 2535 /// Return true if this is NOT a pre/post inc/dec load/store. 2536 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; } 2537 2538 static bool classof(const SDNode *N) { 2539 return N->getOpcode() == ISD::LOAD || 2540 N->getOpcode() == ISD::STORE; 2541 } 2542 }; 2543 2544 /// This class is used to represent ISD::LOAD nodes. 2545 class LoadSDNode : public LSBaseSDNode { 2546 friend class SelectionDAG; 2547 2548 LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2549 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT, 2550 MachineMemOperand *MMO) 2551 : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) { 2552 LoadSDNodeBits.ExtTy = ETy; 2553 assert(readMem() && "Load MachineMemOperand is not a load!"); 2554 assert(!writeMem() && "Load MachineMemOperand is a store!"); 2555 } 2556 2557 public: 2558 /// Return whether this is a plain node, 2559 /// or one of the varieties of value-extending loads. 2560 ISD::LoadExtType getExtensionType() const { 2561 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy); 2562 } 2563 2564 const SDValue &getBasePtr() const { return getOperand(1); } 2565 const SDValue &getOffset() const { return getOperand(2); } 2566 2567 static bool classof(const SDNode *N) { 2568 return N->getOpcode() == ISD::LOAD; 2569 } 2570 }; 2571 2572 /// This class is used to represent ISD::STORE nodes. 2573 class StoreSDNode : public LSBaseSDNode { 2574 friend class SelectionDAG; 2575 2576 StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2577 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT, 2578 MachineMemOperand *MMO) 2579 : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) { 2580 StoreSDNodeBits.IsTruncating = isTrunc; 2581 assert(!readMem() && "Store MachineMemOperand is a load!"); 2582 assert(writeMem() && "Store MachineMemOperand is not a store!"); 2583 } 2584 2585 public: 2586 /// Return true if the op does a truncation before store. 2587 /// For integers this is the same as doing a TRUNCATE and storing the result. 2588 /// For floats, it is the same as doing an FP_ROUND and storing the result. 2589 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; } 2590 2591 const SDValue &getValue() const { return getOperand(1); } 2592 const SDValue &getBasePtr() const { return getOperand(2); } 2593 const SDValue &getOffset() const { return getOperand(3); } 2594 2595 static bool classof(const SDNode *N) { 2596 return N->getOpcode() == ISD::STORE; 2597 } 2598 }; 2599 2600 /// This base class is used to represent VP_LOAD, VP_STORE, 2601 /// EXPERIMENTAL_VP_STRIDED_LOAD and EXPERIMENTAL_VP_STRIDED_STORE nodes 2602 class VPBaseLoadStoreSDNode : public MemSDNode { 2603 public: 2604 friend class SelectionDAG; 2605 2606 VPBaseLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, 2607 const DebugLoc &DL, SDVTList VTs, 2608 ISD::MemIndexedMode AM, EVT MemVT, 2609 MachineMemOperand *MMO) 2610 : MemSDNode(NodeTy, Order, DL, VTs, MemVT, MMO) { 2611 LSBaseSDNodeBits.AddressingMode = AM; 2612 assert(getAddressingMode() == AM && "Value truncated"); 2613 } 2614 2615 // VPStridedStoreSDNode (Chain, Data, Ptr, Offset, Stride, Mask, EVL) 2616 // VPStoreSDNode (Chain, Data, Ptr, Offset, Mask, EVL) 2617 // VPStridedLoadSDNode (Chain, Ptr, Offset, Stride, Mask, EVL) 2618 // VPLoadSDNode (Chain, Ptr, Offset, Mask, EVL) 2619 // Mask is a vector of i1 elements; 2620 // the type of EVL is TLI.getVPExplicitVectorLengthTy(). 2621 const SDValue &getOffset() const { 2622 return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD || 2623 getOpcode() == ISD::VP_LOAD) 2624 ? 2 2625 : 3); 2626 } 2627 const SDValue &getBasePtr() const { 2628 return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD || 2629 getOpcode() == ISD::VP_LOAD) 2630 ? 1 2631 : 2); 2632 } 2633 const SDValue &getMask() const { 2634 switch (getOpcode()) { 2635 default: 2636 llvm_unreachable("Invalid opcode"); 2637 case ISD::VP_LOAD: 2638 return getOperand(3); 2639 case ISD::VP_STORE: 2640 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: 2641 return getOperand(4); 2642 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 2643 return getOperand(5); 2644 } 2645 } 2646 const SDValue &getVectorLength() const { 2647 switch (getOpcode()) { 2648 default: 2649 llvm_unreachable("Invalid opcode"); 2650 case ISD::VP_LOAD: 2651 return getOperand(4); 2652 case ISD::VP_STORE: 2653 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: 2654 return getOperand(5); 2655 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 2656 return getOperand(6); 2657 } 2658 } 2659 2660 /// Return the addressing mode for this load or store: 2661 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec. 2662 ISD::MemIndexedMode getAddressingMode() const { 2663 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode); 2664 } 2665 2666 /// Return true if this is a pre/post inc/dec load/store. 2667 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; } 2668 2669 /// Return true if this is NOT a pre/post inc/dec load/store. 2670 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; } 2671 2672 static bool classof(const SDNode *N) { 2673 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD || 2674 N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE || 2675 N->getOpcode() == ISD::VP_LOAD || N->getOpcode() == ISD::VP_STORE; 2676 } 2677 }; 2678 2679 /// This class is used to represent a VP_LOAD node 2680 class VPLoadSDNode : public VPBaseLoadStoreSDNode { 2681 public: 2682 friend class SelectionDAG; 2683 2684 VPLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2685 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, bool isExpanding, 2686 EVT MemVT, MachineMemOperand *MMO) 2687 : VPBaseLoadStoreSDNode(ISD::VP_LOAD, Order, dl, VTs, AM, MemVT, MMO) { 2688 LoadSDNodeBits.ExtTy = ETy; 2689 LoadSDNodeBits.IsExpanding = isExpanding; 2690 } 2691 2692 ISD::LoadExtType getExtensionType() const { 2693 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy); 2694 } 2695 2696 const SDValue &getBasePtr() const { return getOperand(1); } 2697 const SDValue &getOffset() const { return getOperand(2); } 2698 const SDValue &getMask() const { return getOperand(3); } 2699 const SDValue &getVectorLength() const { return getOperand(4); } 2700 2701 static bool classof(const SDNode *N) { 2702 return N->getOpcode() == ISD::VP_LOAD; 2703 } 2704 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; } 2705 }; 2706 2707 /// This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node. 2708 class VPStridedLoadSDNode : public VPBaseLoadStoreSDNode { 2709 public: 2710 friend class SelectionDAG; 2711 2712 VPStridedLoadSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs, 2713 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, 2714 bool IsExpanding, EVT MemVT, MachineMemOperand *MMO) 2715 : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_LOAD, Order, DL, VTs, 2716 AM, MemVT, MMO) { 2717 LoadSDNodeBits.ExtTy = ETy; 2718 LoadSDNodeBits.IsExpanding = IsExpanding; 2719 } 2720 2721 ISD::LoadExtType getExtensionType() const { 2722 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy); 2723 } 2724 2725 const SDValue &getBasePtr() const { return getOperand(1); } 2726 const SDValue &getOffset() const { return getOperand(2); } 2727 const SDValue &getStride() const { return getOperand(3); } 2728 const SDValue &getMask() const { return getOperand(4); } 2729 const SDValue &getVectorLength() const { return getOperand(5); } 2730 2731 static bool classof(const SDNode *N) { 2732 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD; 2733 } 2734 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; } 2735 }; 2736 2737 /// This class is used to represent a VP_STORE node 2738 class VPStoreSDNode : public VPBaseLoadStoreSDNode { 2739 public: 2740 friend class SelectionDAG; 2741 2742 VPStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2743 ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing, 2744 EVT MemVT, MachineMemOperand *MMO) 2745 : VPBaseLoadStoreSDNode(ISD::VP_STORE, Order, dl, VTs, AM, MemVT, MMO) { 2746 StoreSDNodeBits.IsTruncating = isTrunc; 2747 StoreSDNodeBits.IsCompressing = isCompressing; 2748 } 2749 2750 /// Return true if this is a truncating store. 2751 /// For integers this is the same as doing a TRUNCATE and storing the result. 2752 /// For floats, it is the same as doing an FP_ROUND and storing the result. 2753 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; } 2754 2755 /// Returns true if the op does a compression to the vector before storing. 2756 /// The node contiguously stores the active elements (integers or floats) 2757 /// in src (those with their respective bit set in writemask k) to unaligned 2758 /// memory at base_addr. 2759 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; } 2760 2761 const SDValue &getValue() const { return getOperand(1); } 2762 const SDValue &getBasePtr() const { return getOperand(2); } 2763 const SDValue &getOffset() const { return getOperand(3); } 2764 const SDValue &getMask() const { return getOperand(4); } 2765 const SDValue &getVectorLength() const { return getOperand(5); } 2766 2767 static bool classof(const SDNode *N) { 2768 return N->getOpcode() == ISD::VP_STORE; 2769 } 2770 }; 2771 2772 /// This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node. 2773 class VPStridedStoreSDNode : public VPBaseLoadStoreSDNode { 2774 public: 2775 friend class SelectionDAG; 2776 2777 VPStridedStoreSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs, 2778 ISD::MemIndexedMode AM, bool IsTrunc, bool IsCompressing, 2779 EVT MemVT, MachineMemOperand *MMO) 2780 : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_STORE, Order, DL, 2781 VTs, AM, MemVT, MMO) { 2782 StoreSDNodeBits.IsTruncating = IsTrunc; 2783 StoreSDNodeBits.IsCompressing = IsCompressing; 2784 } 2785 2786 /// Return true if this is a truncating store. 2787 /// For integers this is the same as doing a TRUNCATE and storing the result. 2788 /// For floats, it is the same as doing an FP_ROUND and storing the result. 2789 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; } 2790 2791 /// Returns true if the op does a compression to the vector before storing. 2792 /// The node contiguously stores the active elements (integers or floats) 2793 /// in src (those with their respective bit set in writemask k) to unaligned 2794 /// memory at base_addr. 2795 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; } 2796 2797 const SDValue &getValue() const { return getOperand(1); } 2798 const SDValue &getBasePtr() const { return getOperand(2); } 2799 const SDValue &getOffset() const { return getOperand(3); } 2800 const SDValue &getStride() const { return getOperand(4); } 2801 const SDValue &getMask() const { return getOperand(5); } 2802 const SDValue &getVectorLength() const { return getOperand(6); } 2803 2804 static bool classof(const SDNode *N) { 2805 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE; 2806 } 2807 }; 2808 2809 /// This base class is used to represent MLOAD and MSTORE nodes 2810 class MaskedLoadStoreSDNode : public MemSDNode { 2811 public: 2812 friend class SelectionDAG; 2813 2814 MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, 2815 const DebugLoc &dl, SDVTList VTs, 2816 ISD::MemIndexedMode AM, EVT MemVT, 2817 MachineMemOperand *MMO) 2818 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) { 2819 LSBaseSDNodeBits.AddressingMode = AM; 2820 assert(getAddressingMode() == AM && "Value truncated"); 2821 } 2822 2823 // MaskedLoadSDNode (Chain, ptr, offset, mask, passthru) 2824 // MaskedStoreSDNode (Chain, data, ptr, offset, mask) 2825 // Mask is a vector of i1 elements 2826 const SDValue &getOffset() const { 2827 return getOperand(getOpcode() == ISD::MLOAD ? 2 : 3); 2828 } 2829 const SDValue &getMask() const { 2830 return getOperand(getOpcode() == ISD::MLOAD ? 3 : 4); 2831 } 2832 2833 /// Return the addressing mode for this load or store: 2834 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec. 2835 ISD::MemIndexedMode getAddressingMode() const { 2836 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode); 2837 } 2838 2839 /// Return true if this is a pre/post inc/dec load/store. 2840 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; } 2841 2842 /// Return true if this is NOT a pre/post inc/dec load/store. 2843 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; } 2844 2845 static bool classof(const SDNode *N) { 2846 return N->getOpcode() == ISD::MLOAD || 2847 N->getOpcode() == ISD::MSTORE; 2848 } 2849 }; 2850 2851 /// This class is used to represent an MLOAD node 2852 class MaskedLoadSDNode : public MaskedLoadStoreSDNode { 2853 public: 2854 friend class SelectionDAG; 2855 2856 MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2857 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, 2858 bool IsExpanding, EVT MemVT, MachineMemOperand *MMO) 2859 : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, AM, MemVT, MMO) { 2860 LoadSDNodeBits.ExtTy = ETy; 2861 LoadSDNodeBits.IsExpanding = IsExpanding; 2862 } 2863 2864 ISD::LoadExtType getExtensionType() const { 2865 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy); 2866 } 2867 2868 const SDValue &getBasePtr() const { return getOperand(1); } 2869 const SDValue &getOffset() const { return getOperand(2); } 2870 const SDValue &getMask() const { return getOperand(3); } 2871 const SDValue &getPassThru() const { return getOperand(4); } 2872 2873 static bool classof(const SDNode *N) { 2874 return N->getOpcode() == ISD::MLOAD; 2875 } 2876 2877 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; } 2878 }; 2879 2880 /// This class is used to represent an MSTORE node 2881 class MaskedStoreSDNode : public MaskedLoadStoreSDNode { 2882 public: 2883 friend class SelectionDAG; 2884 2885 MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 2886 ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing, 2887 EVT MemVT, MachineMemOperand *MMO) 2888 : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, AM, MemVT, MMO) { 2889 StoreSDNodeBits.IsTruncating = isTrunc; 2890 StoreSDNodeBits.IsCompressing = isCompressing; 2891 } 2892 2893 /// Return true if the op does a truncation before store. 2894 /// For integers this is the same as doing a TRUNCATE and storing the result. 2895 /// For floats, it is the same as doing an FP_ROUND and storing the result. 2896 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; } 2897 2898 /// Returns true if the op does a compression to the vector before storing. 2899 /// The node contiguously stores the active elements (integers or floats) 2900 /// in src (those with their respective bit set in writemask k) to unaligned 2901 /// memory at base_addr. 2902 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; } 2903 2904 const SDValue &getValue() const { return getOperand(1); } 2905 const SDValue &getBasePtr() const { return getOperand(2); } 2906 const SDValue &getOffset() const { return getOperand(3); } 2907 const SDValue &getMask() const { return getOperand(4); } 2908 2909 static bool classof(const SDNode *N) { 2910 return N->getOpcode() == ISD::MSTORE; 2911 } 2912 }; 2913 2914 /// This is a base class used to represent 2915 /// VP_GATHER and VP_SCATTER nodes 2916 /// 2917 class VPGatherScatterSDNode : public MemSDNode { 2918 public: 2919 friend class SelectionDAG; 2920 2921 VPGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order, 2922 const DebugLoc &dl, SDVTList VTs, EVT MemVT, 2923 MachineMemOperand *MMO, ISD::MemIndexType IndexType) 2924 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) { 2925 LSBaseSDNodeBits.AddressingMode = IndexType; 2926 assert(getIndexType() == IndexType && "Value truncated"); 2927 } 2928 2929 /// How is Index applied to BasePtr when computing addresses. 2930 ISD::MemIndexType getIndexType() const { 2931 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode); 2932 } 2933 bool isIndexScaled() const { 2934 return !cast<ConstantSDNode>(getScale())->isOne(); 2935 } 2936 bool isIndexSigned() const { return isIndexTypeSigned(getIndexType()); } 2937 2938 // In the both nodes address is Op1, mask is Op2: 2939 // VPGatherSDNode (Chain, base, index, scale, mask, vlen) 2940 // VPScatterSDNode (Chain, value, base, index, scale, mask, vlen) 2941 // Mask is a vector of i1 elements 2942 const SDValue &getBasePtr() const { 2943 return getOperand((getOpcode() == ISD::VP_GATHER) ? 1 : 2); 2944 } 2945 const SDValue &getIndex() const { 2946 return getOperand((getOpcode() == ISD::VP_GATHER) ? 2 : 3); 2947 } 2948 const SDValue &getScale() const { 2949 return getOperand((getOpcode() == ISD::VP_GATHER) ? 3 : 4); 2950 } 2951 const SDValue &getMask() const { 2952 return getOperand((getOpcode() == ISD::VP_GATHER) ? 4 : 5); 2953 } 2954 const SDValue &getVectorLength() const { 2955 return getOperand((getOpcode() == ISD::VP_GATHER) ? 5 : 6); 2956 } 2957 2958 static bool classof(const SDNode *N) { 2959 return N->getOpcode() == ISD::VP_GATHER || 2960 N->getOpcode() == ISD::VP_SCATTER; 2961 } 2962 }; 2963 2964 /// This class is used to represent an VP_GATHER node 2965 /// 2966 class VPGatherSDNode : public VPGatherScatterSDNode { 2967 public: 2968 friend class SelectionDAG; 2969 2970 VPGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT, 2971 MachineMemOperand *MMO, ISD::MemIndexType IndexType) 2972 : VPGatherScatterSDNode(ISD::VP_GATHER, Order, dl, VTs, MemVT, MMO, 2973 IndexType) {} 2974 2975 static bool classof(const SDNode *N) { 2976 return N->getOpcode() == ISD::VP_GATHER; 2977 } 2978 }; 2979 2980 /// This class is used to represent an VP_SCATTER node 2981 /// 2982 class VPScatterSDNode : public VPGatherScatterSDNode { 2983 public: 2984 friend class SelectionDAG; 2985 2986 VPScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT, 2987 MachineMemOperand *MMO, ISD::MemIndexType IndexType) 2988 : VPGatherScatterSDNode(ISD::VP_SCATTER, Order, dl, VTs, MemVT, MMO, 2989 IndexType) {} 2990 2991 const SDValue &getValue() const { return getOperand(1); } 2992 2993 static bool classof(const SDNode *N) { 2994 return N->getOpcode() == ISD::VP_SCATTER; 2995 } 2996 }; 2997 2998 /// This is a base class used to represent 2999 /// MGATHER and MSCATTER nodes 3000 /// 3001 class MaskedGatherScatterSDNode : public MemSDNode { 3002 public: 3003 friend class SelectionDAG; 3004 3005 MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order, 3006 const DebugLoc &dl, SDVTList VTs, EVT MemVT, 3007 MachineMemOperand *MMO, ISD::MemIndexType IndexType) 3008 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) { 3009 LSBaseSDNodeBits.AddressingMode = IndexType; 3010 assert(getIndexType() == IndexType && "Value truncated"); 3011 } 3012 3013 /// How is Index applied to BasePtr when computing addresses. 3014 ISD::MemIndexType getIndexType() const { 3015 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode); 3016 } 3017 bool isIndexScaled() const { 3018 return !cast<ConstantSDNode>(getScale())->isOne(); 3019 } 3020 bool isIndexSigned() const { return isIndexTypeSigned(getIndexType()); } 3021 3022 // In the both nodes address is Op1, mask is Op2: 3023 // MaskedGatherSDNode (Chain, passthru, mask, base, index, scale) 3024 // MaskedScatterSDNode (Chain, value, mask, base, index, scale) 3025 // Mask is a vector of i1 elements 3026 const SDValue &getBasePtr() const { return getOperand(3); } 3027 const SDValue &getIndex() const { return getOperand(4); } 3028 const SDValue &getMask() const { return getOperand(2); } 3029 const SDValue &getScale() const { return getOperand(5); } 3030 3031 static bool classof(const SDNode *N) { 3032 return N->getOpcode() == ISD::MGATHER || N->getOpcode() == ISD::MSCATTER || 3033 N->getOpcode() == ISD::EXPERIMENTAL_VECTOR_HISTOGRAM; 3034 } 3035 }; 3036 3037 /// This class is used to represent an MGATHER node 3038 /// 3039 class MaskedGatherSDNode : public MaskedGatherScatterSDNode { 3040 public: 3041 friend class SelectionDAG; 3042 3043 MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 3044 EVT MemVT, MachineMemOperand *MMO, 3045 ISD::MemIndexType IndexType, ISD::LoadExtType ETy) 3046 : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO, 3047 IndexType) { 3048 LoadSDNodeBits.ExtTy = ETy; 3049 } 3050 3051 const SDValue &getPassThru() const { return getOperand(1); } 3052 3053 ISD::LoadExtType getExtensionType() const { 3054 return ISD::LoadExtType(LoadSDNodeBits.ExtTy); 3055 } 3056 3057 static bool classof(const SDNode *N) { 3058 return N->getOpcode() == ISD::MGATHER; 3059 } 3060 }; 3061 3062 /// This class is used to represent an MSCATTER node 3063 /// 3064 class MaskedScatterSDNode : public MaskedGatherScatterSDNode { 3065 public: 3066 friend class SelectionDAG; 3067 3068 MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, 3069 EVT MemVT, MachineMemOperand *MMO, 3070 ISD::MemIndexType IndexType, bool IsTrunc) 3071 : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO, 3072 IndexType) { 3073 StoreSDNodeBits.IsTruncating = IsTrunc; 3074 } 3075 3076 /// Return true if the op does a truncation before store. 3077 /// For integers this is the same as doing a TRUNCATE and storing the result. 3078 /// For floats, it is the same as doing an FP_ROUND and storing the result. 3079 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; } 3080 3081 const SDValue &getValue() const { return getOperand(1); } 3082 3083 static bool classof(const SDNode *N) { 3084 return N->getOpcode() == ISD::MSCATTER; 3085 } 3086 }; 3087 3088 class MaskedHistogramSDNode : public MaskedGatherScatterSDNode { 3089 public: 3090 friend class SelectionDAG; 3091 3092 MaskedHistogramSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs, 3093 EVT MemVT, MachineMemOperand *MMO, 3094 ISD::MemIndexType IndexType) 3095 : MaskedGatherScatterSDNode(ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, Order, DL, 3096 VTs, MemVT, MMO, IndexType) {} 3097 3098 ISD::MemIndexType getIndexType() const { 3099 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode); 3100 } 3101 3102 const SDValue &getBasePtr() const { return getOperand(3); } 3103 const SDValue &getIndex() const { return getOperand(4); } 3104 const SDValue &getMask() const { return getOperand(2); } 3105 const SDValue &getScale() const { return getOperand(5); } 3106 const SDValue &getInc() const { return getOperand(1); } 3107 const SDValue &getIntID() const { return getOperand(6); } 3108 3109 static bool classof(const SDNode *N) { 3110 return N->getOpcode() == ISD::EXPERIMENTAL_VECTOR_HISTOGRAM; 3111 } 3112 }; 3113 3114 class FPStateAccessSDNode : public MemSDNode { 3115 public: 3116 friend class SelectionDAG; 3117 3118 FPStateAccessSDNode(unsigned NodeTy, unsigned Order, const DebugLoc &dl, 3119 SDVTList VTs, EVT MemVT, MachineMemOperand *MMO) 3120 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) { 3121 assert((NodeTy == ISD::GET_FPENV_MEM || NodeTy == ISD::SET_FPENV_MEM) && 3122 "Expected FP state access node"); 3123 } 3124 3125 static bool classof(const SDNode *N) { 3126 return N->getOpcode() == ISD::GET_FPENV_MEM || 3127 N->getOpcode() == ISD::SET_FPENV_MEM; 3128 } 3129 }; 3130 3131 /// An SDNode that represents everything that will be needed 3132 /// to construct a MachineInstr. These nodes are created during the 3133 /// instruction selection proper phase. 3134 /// 3135 /// Note that the only supported way to set the `memoperands` is by calling the 3136 /// `SelectionDAG::setNodeMemRefs` function as the memory management happens 3137 /// inside the DAG rather than in the node. 3138 class MachineSDNode : public SDNode { 3139 private: 3140 friend class SelectionDAG; 3141 3142 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs) 3143 : SDNode(Opc, Order, DL, VTs) {} 3144 3145 // We use a pointer union between a single `MachineMemOperand` pointer and 3146 // a pointer to an array of `MachineMemOperand` pointers. This is null when 3147 // the number of these is zero, the single pointer variant used when the 3148 // number is one, and the array is used for larger numbers. 3149 // 3150 // The array is allocated via the `SelectionDAG`'s allocator and so will 3151 // always live until the DAG is cleaned up and doesn't require ownership here. 3152 // 3153 // We can't use something simpler like `TinyPtrVector` here because `SDNode` 3154 // subclasses aren't managed in a conforming C++ manner. See the comments on 3155 // `SelectionDAG::MorphNodeTo` which details what all goes on, but the 3156 // constraint here is that these don't manage memory with their constructor or 3157 // destructor and can be initialized to a good state even if they start off 3158 // uninitialized. 3159 PointerUnion<MachineMemOperand *, MachineMemOperand **> MemRefs = {}; 3160 3161 // Note that this could be folded into the above `MemRefs` member if doing so 3162 // is advantageous at some point. We don't need to store this in most cases. 3163 // However, at the moment this doesn't appear to make the allocation any 3164 // smaller and makes the code somewhat simpler to read. 3165 int NumMemRefs = 0; 3166 3167 public: 3168 using mmo_iterator = ArrayRef<MachineMemOperand *>::const_iterator; 3169 3170 ArrayRef<MachineMemOperand *> memoperands() const { 3171 // Special case the common cases. 3172 if (NumMemRefs == 0) 3173 return {}; 3174 if (NumMemRefs == 1) 3175 return ArrayRef(MemRefs.getAddrOfPtr1(), 1); 3176 3177 // Otherwise we have an actual array. 3178 return ArrayRef(cast<MachineMemOperand **>(MemRefs), NumMemRefs); 3179 } 3180 mmo_iterator memoperands_begin() const { return memoperands().begin(); } 3181 mmo_iterator memoperands_end() const { return memoperands().end(); } 3182 bool memoperands_empty() const { return memoperands().empty(); } 3183 3184 /// Clear out the memory reference descriptor list. 3185 void clearMemRefs() { 3186 MemRefs = nullptr; 3187 NumMemRefs = 0; 3188 } 3189 3190 static bool classof(const SDNode *N) { 3191 return N->isMachineOpcode(); 3192 } 3193 }; 3194 3195 /// An SDNode that records if a register contains a value that is guaranteed to 3196 /// be aligned accordingly. 3197 class AssertAlignSDNode : public SDNode { 3198 Align Alignment; 3199 3200 public: 3201 AssertAlignSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs, Align A) 3202 : SDNode(ISD::AssertAlign, Order, DL, VTs), Alignment(A) {} 3203 3204 Align getAlign() const { return Alignment; } 3205 3206 static bool classof(const SDNode *N) { 3207 return N->getOpcode() == ISD::AssertAlign; 3208 } 3209 }; 3210 3211 class SDNodeIterator { 3212 const SDNode *Node; 3213 unsigned Operand; 3214 3215 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {} 3216 3217 public: 3218 using iterator_category = std::forward_iterator_tag; 3219 using value_type = SDNode; 3220 using difference_type = std::ptrdiff_t; 3221 using pointer = value_type *; 3222 using reference = value_type &; 3223 3224 bool operator==(const SDNodeIterator& x) const { 3225 return Operand == x.Operand; 3226 } 3227 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); } 3228 3229 pointer operator*() const { 3230 return Node->getOperand(Operand).getNode(); 3231 } 3232 pointer operator->() const { return operator*(); } 3233 3234 SDNodeIterator& operator++() { // Preincrement 3235 ++Operand; 3236 return *this; 3237 } 3238 SDNodeIterator operator++(int) { // Postincrement 3239 SDNodeIterator tmp = *this; ++*this; return tmp; 3240 } 3241 size_t operator-(SDNodeIterator Other) const { 3242 assert(Node == Other.Node && 3243 "Cannot compare iterators of two different nodes!"); 3244 return Operand - Other.Operand; 3245 } 3246 3247 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); } 3248 static SDNodeIterator end (const SDNode *N) { 3249 return SDNodeIterator(N, N->getNumOperands()); 3250 } 3251 3252 unsigned getOperand() const { return Operand; } 3253 const SDNode *getNode() const { return Node; } 3254 }; 3255 3256 template <> struct GraphTraits<SDNode*> { 3257 using NodeRef = SDNode *; 3258 using ChildIteratorType = SDNodeIterator; 3259 3260 static NodeRef getEntryNode(SDNode *N) { return N; } 3261 3262 static ChildIteratorType child_begin(NodeRef N) { 3263 return SDNodeIterator::begin(N); 3264 } 3265 3266 static ChildIteratorType child_end(NodeRef N) { 3267 return SDNodeIterator::end(N); 3268 } 3269 }; 3270 3271 /// A representation of the largest SDNode, for use in sizeof(). 3272 /// 3273 /// This needs to be a union because the largest node differs on 32 bit systems 3274 /// with 4 and 8 byte pointer alignment, respectively. 3275 using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode, 3276 BlockAddressSDNode, 3277 GlobalAddressSDNode, 3278 PseudoProbeSDNode>; 3279 3280 /// The SDNode class with the greatest alignment requirement. 3281 using MostAlignedSDNode = GlobalAddressSDNode; 3282 3283 namespace ISD { 3284 3285 /// Returns true if the specified node is a non-extending and unindexed load. 3286 inline bool isNormalLoad(const SDNode *N) { 3287 auto *Ld = dyn_cast<LoadSDNode>(N); 3288 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD && 3289 Ld->getAddressingMode() == ISD::UNINDEXED; 3290 } 3291 3292 /// Returns true if the specified node is a non-extending load. 3293 inline bool isNON_EXTLoad(const SDNode *N) { 3294 auto *Ld = dyn_cast<LoadSDNode>(N); 3295 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD; 3296 } 3297 3298 /// Returns true if the specified node is a EXTLOAD. 3299 inline bool isEXTLoad(const SDNode *N) { 3300 auto *Ld = dyn_cast<LoadSDNode>(N); 3301 return Ld && Ld->getExtensionType() == ISD::EXTLOAD; 3302 } 3303 3304 /// Returns true if the specified node is a SEXTLOAD. 3305 inline bool isSEXTLoad(const SDNode *N) { 3306 auto *Ld = dyn_cast<LoadSDNode>(N); 3307 return Ld && Ld->getExtensionType() == ISD::SEXTLOAD; 3308 } 3309 3310 /// Returns true if the specified node is a ZEXTLOAD. 3311 inline bool isZEXTLoad(const SDNode *N) { 3312 auto *Ld = dyn_cast<LoadSDNode>(N); 3313 return Ld && Ld->getExtensionType() == ISD::ZEXTLOAD; 3314 } 3315 3316 /// Returns true if the specified node is an unindexed load. 3317 inline bool isUNINDEXEDLoad(const SDNode *N) { 3318 auto *Ld = dyn_cast<LoadSDNode>(N); 3319 return Ld && Ld->getAddressingMode() == ISD::UNINDEXED; 3320 } 3321 3322 /// Returns true if the specified node is a non-truncating 3323 /// and unindexed store. 3324 inline bool isNormalStore(const SDNode *N) { 3325 auto *St = dyn_cast<StoreSDNode>(N); 3326 return St && !St->isTruncatingStore() && 3327 St->getAddressingMode() == ISD::UNINDEXED; 3328 } 3329 3330 /// Returns true if the specified node is an unindexed store. 3331 inline bool isUNINDEXEDStore(const SDNode *N) { 3332 auto *St = dyn_cast<StoreSDNode>(N); 3333 return St && St->getAddressingMode() == ISD::UNINDEXED; 3334 } 3335 3336 /// Attempt to match a unary predicate against a scalar/splat constant or 3337 /// every element of a constant BUILD_VECTOR. 3338 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match. 3339 template <typename ConstNodeType> 3340 bool matchUnaryPredicateImpl(SDValue Op, 3341 std::function<bool(ConstNodeType *)> Match, 3342 bool AllowUndefs = false, 3343 bool AllowTruncation = false); 3344 3345 /// Hook for matching ConstantSDNode predicate 3346 inline bool matchUnaryPredicate(SDValue Op, 3347 std::function<bool(ConstantSDNode *)> Match, 3348 bool AllowUndefs = false, 3349 bool AllowTruncation = false) { 3350 return matchUnaryPredicateImpl<ConstantSDNode>(Op, Match, AllowUndefs, 3351 AllowTruncation); 3352 } 3353 3354 /// Hook for matching ConstantFPSDNode predicate 3355 inline bool 3356 matchUnaryFpPredicate(SDValue Op, 3357 std::function<bool(ConstantFPSDNode *)> Match, 3358 bool AllowUndefs = false) { 3359 return matchUnaryPredicateImpl<ConstantFPSDNode>(Op, Match, AllowUndefs); 3360 } 3361 3362 /// Attempt to match a binary predicate against a pair of scalar/splat 3363 /// constants or every element of a pair of constant BUILD_VECTORs. 3364 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match. 3365 /// If AllowTypeMismatch is true then RetType + ArgTypes don't need to match. 3366 LLVM_ABI bool matchBinaryPredicate( 3367 SDValue LHS, SDValue RHS, 3368 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match, 3369 bool AllowUndefs = false, bool AllowTypeMismatch = false); 3370 3371 /// Returns true if the specified value is the overflow result from one 3372 /// of the overflow intrinsic nodes. 3373 inline bool isOverflowIntrOpRes(SDValue Op) { 3374 unsigned Opc = Op.getOpcode(); 3375 return (Op.getResNo() == 1 && 3376 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 3377 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO)); 3378 } 3379 3380 } // end namespace ISD 3381 3382 } // end namespace llvm 3383 3384 #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H 3385