1 //===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SDNode class and derived classes, which are used to
11 // represent the nodes and operations present in a SelectionDAG. These nodes
12 // and operations are machine code level operations, with some similarities to
13 // the GCC RTL representation.
15 // Clients should include the SelectionDAG.h file instead of this file directly.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
22 #include "llvm/ADT/APFloat.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/BitVector.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/GraphTraits.h"
27 #include "llvm/ADT/ilist_node.h"
28 #include "llvm/ADT/iterator.h"
29 #include "llvm/ADT/iterator_range.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/CodeGen/ISDOpcodes.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/MachineValueType.h"
35 #include "llvm/CodeGen/ValueTypes.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/Support/AlignOf.h"
41 #include "llvm/Support/AtomicOrdering.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/ErrorHandling.h"
58 class MachineBasicBlock;
59 class MachineConstantPoolValue;
63 template <typename T> struct DenseMapInfo;
65 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
68 /// This represents a list of ValueType's that has been intern'd by
69 /// a SelectionDAG. Instances of this simple value class are returned by
70 /// SelectionDAG::getVTList(...).
81 /// If N is a BUILD_VECTOR node whose elements are all the same constant or
82 /// undefined, return true and return the constant value in \p SplatValue.
83 bool isConstantSplatVector(const SDNode *N, APInt &SplatValue);
85 /// Return true if the specified node is a BUILD_VECTOR where all of the
86 /// elements are ~0 or undef.
87 bool isBuildVectorAllOnes(const SDNode *N);
89 /// Return true if the specified node is a BUILD_VECTOR where all of the
90 /// elements are 0 or undef.
91 bool isBuildVectorAllZeros(const SDNode *N);
93 /// Return true if the specified node is a BUILD_VECTOR node of all
94 /// ConstantSDNode or undef.
95 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
97 /// Return true if the specified node is a BUILD_VECTOR node of all
98 /// ConstantFPSDNode or undef.
99 bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
101 /// Return true if the node has at least one operand and all operands of the
102 /// specified node are ISD::UNDEF.
103 bool allOperandsUndef(const SDNode *N);
105 } // end namespace ISD
107 //===----------------------------------------------------------------------===//
108 /// Unlike LLVM values, Selection DAG nodes may return multiple
109 /// values as the result of a computation. Many nodes return multiple values,
110 /// from loads (which define a token and a return value) to ADDC (which returns
111 /// a result and a carry value), to calls (which may return an arbitrary number
114 /// As such, each use of a SelectionDAG computation must indicate the node that
115 /// computes it as well as which return value to use from that node. This pair
116 /// of information is represented with the SDValue value type.
119 friend struct DenseMapInfo<SDValue>;
121 SDNode *Node = nullptr; // The node defining the value we are using.
122 unsigned ResNo = 0; // Which return value of the node we are using.
126 SDValue(SDNode *node, unsigned resno);
128 /// get the index which selects a specific result in the SDNode
129 unsigned getResNo() const { return ResNo; }
131 /// get the SDNode which holds the desired result
132 SDNode *getNode() const { return Node; }
135 void setNode(SDNode *N) { Node = N; }
137 inline SDNode *operator->() const { return Node; }
139 bool operator==(const SDValue &O) const {
140 return Node == O.Node && ResNo == O.ResNo;
142 bool operator!=(const SDValue &O) const {
143 return !operator==(O);
145 bool operator<(const SDValue &O) const {
146 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
148 explicit operator bool() const {
149 return Node != nullptr;
152 SDValue getValue(unsigned R) const {
153 return SDValue(Node, R);
156 /// Return true if this node is an operand of N.
157 bool isOperandOf(const SDNode *N) const;
159 /// Return the ValueType of the referenced return value.
160 inline EVT getValueType() const;
162 /// Return the simple ValueType of the referenced return value.
163 MVT getSimpleValueType() const {
164 return getValueType().getSimpleVT();
167 /// Returns the size of the value in bits.
168 unsigned getValueSizeInBits() const {
169 return getValueType().getSizeInBits();
172 unsigned getScalarValueSizeInBits() const {
173 return getValueType().getScalarType().getSizeInBits();
176 // Forwarding methods - These forward to the corresponding methods in SDNode.
177 inline unsigned getOpcode() const;
178 inline unsigned getNumOperands() const;
179 inline const SDValue &getOperand(unsigned i) const;
180 inline uint64_t getConstantOperandVal(unsigned i) const;
181 inline bool isTargetMemoryOpcode() const;
182 inline bool isTargetOpcode() const;
183 inline bool isMachineOpcode() const;
184 inline bool isUndef() const;
185 inline unsigned getMachineOpcode() const;
186 inline const DebugLoc &getDebugLoc() const;
187 inline void dump() const;
188 inline void dumpr() const;
190 /// Return true if this operand (which must be a chain) reaches the
191 /// specified operand without crossing any side-effecting instructions.
192 /// In practice, this looks through token factors and non-volatile loads.
193 /// In order to remain efficient, this only
194 /// looks a couple of nodes in, it does not do an exhaustive search.
195 bool reachesChainWithoutSideEffects(SDValue Dest,
196 unsigned Depth = 2) const;
198 /// Return true if there are no nodes using value ResNo of Node.
199 inline bool use_empty() const;
201 /// Return true if there is exactly one node using value ResNo of Node.
202 inline bool hasOneUse() const;
205 template<> struct DenseMapInfo<SDValue> {
206 static inline SDValue getEmptyKey() {
212 static inline SDValue getTombstoneKey() {
218 static unsigned getHashValue(const SDValue &Val) {
219 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
220 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
223 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
227 template <> struct isPodLike<SDValue> { static const bool value = true; };
229 /// Allow casting operators to work directly on
230 /// SDValues as if they were SDNode*'s.
231 template<> struct simplify_type<SDValue> {
232 typedef SDNode* SimpleType;
233 static SimpleType getSimplifiedValue(SDValue &Val) {
234 return Val.getNode();
237 template<> struct simplify_type<const SDValue> {
238 typedef /*const*/ SDNode* SimpleType;
239 static SimpleType getSimplifiedValue(const SDValue &Val) {
240 return Val.getNode();
244 /// Represents a use of a SDNode. This class holds an SDValue,
245 /// which records the SDNode being used and the result number, a
246 /// pointer to the SDNode using the value, and Next and Prev pointers,
247 /// which link together all the uses of an SDNode.
250 /// Val - The value being used.
252 /// User - The user of this value.
253 SDNode *User = nullptr;
254 /// Prev, Next - Pointers to the uses list of the SDNode referred by
256 SDUse **Prev = nullptr;
257 SDUse *Next = nullptr;
261 SDUse(const SDUse &U) = delete;
262 SDUse &operator=(const SDUse &) = delete;
264 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
265 operator const SDValue&() const { return Val; }
267 /// If implicit conversion to SDValue doesn't work, the get() method returns
269 const SDValue &get() const { return Val; }
271 /// This returns the SDNode that contains this Use.
272 SDNode *getUser() { return User; }
274 /// Get the next SDUse in the use list.
275 SDUse *getNext() const { return Next; }
277 /// Convenience function for get().getNode().
278 SDNode *getNode() const { return Val.getNode(); }
279 /// Convenience function for get().getResNo().
280 unsigned getResNo() const { return Val.getResNo(); }
281 /// Convenience function for get().getValueType().
282 EVT getValueType() const { return Val.getValueType(); }
284 /// Convenience function for get().operator==
285 bool operator==(const SDValue &V) const {
289 /// Convenience function for get().operator!=
290 bool operator!=(const SDValue &V) const {
294 /// Convenience function for get().operator<
295 bool operator<(const SDValue &V) const {
300 friend class SelectionDAG;
302 // TODO: unfriend HandleSDNode once we fix its operand handling.
303 friend class HandleSDNode;
305 void setUser(SDNode *p) { User = p; }
307 /// Remove this use from its existing use list, assign it the
308 /// given value, and add it to the new value's node's use list.
309 inline void set(const SDValue &V);
310 /// Like set, but only supports initializing a newly-allocated
311 /// SDUse with a non-null value.
312 inline void setInitial(const SDValue &V);
313 /// Like set, but only sets the Node portion of the value,
314 /// leaving the ResNo portion unmodified.
315 inline void setNode(SDNode *N);
317 void addToList(SDUse **List) {
319 if (Next) Next->Prev = &Next;
324 void removeFromList() {
326 if (Next) Next->Prev = Prev;
330 /// simplify_type specializations - Allow casting operators to work directly on
331 /// SDValues as if they were SDNode*'s.
332 template<> struct simplify_type<SDUse> {
333 typedef SDNode* SimpleType;
334 static SimpleType getSimplifiedValue(SDUse &Val) {
335 return Val.getNode();
339 /// These are IR-level optimization flags that may be propagated to SDNodes.
340 /// TODO: This data structure should be shared by the IR optimizer and the
344 // This bit is used to determine if the flags are in a defined state.
345 // Flag bits can only be masked out during intersection if the masking flags
349 bool NoUnsignedWrap : 1;
350 bool NoSignedWrap : 1;
352 bool UnsafeAlgebra : 1;
355 bool NoSignedZeros : 1;
356 bool AllowReciprocal : 1;
357 bool VectorReduction : 1;
358 bool AllowContract : 1;
361 /// Default constructor turns off all optimization flags.
363 : AnyDefined(false), NoUnsignedWrap(false), NoSignedWrap(false),
364 Exact(false), UnsafeAlgebra(false), NoNaNs(false), NoInfs(false),
365 NoSignedZeros(false), AllowReciprocal(false), VectorReduction(false),
366 AllowContract(false) {}
368 /// Sets the state of the flags to the defined state.
369 void setDefined() { AnyDefined = true; }
370 /// Returns true if the flags are in a defined state.
371 bool isDefined() const { return AnyDefined; }
373 // These are mutators for each flag.
374 void setNoUnsignedWrap(bool b) {
378 void setNoSignedWrap(bool b) {
382 void setExact(bool b) {
386 void setUnsafeAlgebra(bool b) {
390 void setNoNaNs(bool b) {
394 void setNoInfs(bool b) {
398 void setNoSignedZeros(bool b) {
402 void setAllowReciprocal(bool b) {
406 void setVectorReduction(bool b) {
410 void setAllowContract(bool b) {
415 // These are accessors for each flag.
416 bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
417 bool hasNoSignedWrap() const { return NoSignedWrap; }
418 bool hasExact() const { return Exact; }
419 bool hasUnsafeAlgebra() const { return UnsafeAlgebra; }
420 bool hasNoNaNs() const { return NoNaNs; }
421 bool hasNoInfs() const { return NoInfs; }
422 bool hasNoSignedZeros() const { return NoSignedZeros; }
423 bool hasAllowReciprocal() const { return AllowReciprocal; }
424 bool hasVectorReduction() const { return VectorReduction; }
425 bool hasAllowContract() const { return AllowContract; }
427 /// Clear any flags in this flag set that aren't also set in Flags.
428 /// If the given Flags are undefined then don't do anything.
429 void intersectWith(const SDNodeFlags Flags) {
430 if (!Flags.isDefined())
432 NoUnsignedWrap &= Flags.NoUnsignedWrap;
433 NoSignedWrap &= Flags.NoSignedWrap;
434 Exact &= Flags.Exact;
435 UnsafeAlgebra &= Flags.UnsafeAlgebra;
436 NoNaNs &= Flags.NoNaNs;
437 NoInfs &= Flags.NoInfs;
438 NoSignedZeros &= Flags.NoSignedZeros;
439 AllowReciprocal &= Flags.AllowReciprocal;
440 VectorReduction &= Flags.VectorReduction;
441 AllowContract &= Flags.AllowContract;
445 /// Represents one node in the SelectionDAG.
447 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
449 /// The operation that this node performs.
453 // We define a set of mini-helper classes to help us interpret the bits in our
454 // SubclassData. These are designed to fit within a uint16_t so they pack
457 class SDNodeBitfields {
459 friend class MemIntrinsicSDNode;
460 friend class MemSDNode;
462 uint16_t HasDebugValue : 1;
463 uint16_t IsMemIntrinsic : 1;
465 enum { NumSDNodeBits = 2 };
467 class ConstantSDNodeBitfields {
468 friend class ConstantSDNode;
470 uint16_t : NumSDNodeBits;
472 uint16_t IsOpaque : 1;
475 class MemSDNodeBitfields {
476 friend class MemSDNode;
477 friend class MemIntrinsicSDNode;
478 friend class AtomicSDNode;
480 uint16_t : NumSDNodeBits;
482 uint16_t IsVolatile : 1;
483 uint16_t IsNonTemporal : 1;
484 uint16_t IsDereferenceable : 1;
485 uint16_t IsInvariant : 1;
487 enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
489 class LSBaseSDNodeBitfields {
490 friend class LSBaseSDNode;
492 uint16_t : NumMemSDNodeBits;
494 uint16_t AddressingMode : 3; // enum ISD::MemIndexedMode
496 enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
498 class LoadSDNodeBitfields {
499 friend class LoadSDNode;
500 friend class MaskedLoadSDNode;
502 uint16_t : NumLSBaseSDNodeBits;
504 uint16_t ExtTy : 2; // enum ISD::LoadExtType
505 uint16_t IsExpanding : 1;
508 class StoreSDNodeBitfields {
509 friend class StoreSDNode;
510 friend class MaskedStoreSDNode;
512 uint16_t : NumLSBaseSDNodeBits;
514 uint16_t IsTruncating : 1;
515 uint16_t IsCompressing : 1;
519 char RawSDNodeBits[sizeof(uint16_t)];
520 SDNodeBitfields SDNodeBits;
521 ConstantSDNodeBitfields ConstantSDNodeBits;
522 MemSDNodeBitfields MemSDNodeBits;
523 LSBaseSDNodeBitfields LSBaseSDNodeBits;
524 LoadSDNodeBitfields LoadSDNodeBits;
525 StoreSDNodeBitfields StoreSDNodeBits;
528 // RawSDNodeBits must cover the entirety of the union. This means that all of
529 // the union's members must have size <= RawSDNodeBits. We write the RHS as
530 // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
531 static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
532 static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
533 static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
534 static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
535 static_assert(sizeof(LoadSDNodeBitfields) <= 4, "field too wide");
536 static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
539 friend class SelectionDAG;
540 // TODO: unfriend HandleSDNode once we fix its operand handling.
541 friend class HandleSDNode;
543 /// Unique id per SDNode in the DAG.
546 /// The values that are used by this operation.
547 SDUse *OperandList = nullptr;
549 /// The types of the values this node defines. SDNode's may
550 /// define multiple values simultaneously.
551 const EVT *ValueList;
553 /// List of uses for this SDNode.
554 SDUse *UseList = nullptr;
556 /// The number of entries in the Operand/Value list.
557 unsigned short NumOperands = 0;
558 unsigned short NumValues;
560 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
561 // original LLVM instructions.
562 // This is used for turning off scheduling, because we'll forgo
563 // the normal scheduling algorithms and output the instructions according to
567 /// Source line information.
570 /// Return a pointer to the specified value type.
571 static const EVT *getValueTypeList(EVT VT);
576 /// Unique and persistent id per SDNode in the DAG.
577 /// Used for debug printing.
578 uint16_t PersistentId;
580 //===--------------------------------------------------------------------===//
584 /// Return the SelectionDAG opcode value for this node. For
585 /// pre-isel nodes (those for which isMachineOpcode returns false), these
586 /// are the opcode values in the ISD and <target>ISD namespaces. For
587 /// post-isel opcodes, see getMachineOpcode.
588 unsigned getOpcode() const { return (unsigned short)NodeType; }
590 /// Test if this node has a target-specific opcode (in the
591 /// \<target\>ISD namespace).
592 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
594 /// Test if this node has a target-specific
595 /// memory-referencing opcode (in the \<target\>ISD namespace and
596 /// greater than FIRST_TARGET_MEMORY_OPCODE).
597 bool isTargetMemoryOpcode() const {
598 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
601 /// Return true if the type of the node type undefined.
602 bool isUndef() const { return NodeType == ISD::UNDEF; }
604 /// Test if this node is a memory intrinsic (with valid pointer information).
605 /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
606 /// non-memory intrinsics (with chains) that are not really instances of
607 /// MemSDNode. For such nodes, we need some extra state to determine the
608 /// proper classof relationship.
609 bool isMemIntrinsic() const {
610 return (NodeType == ISD::INTRINSIC_W_CHAIN ||
611 NodeType == ISD::INTRINSIC_VOID) &&
612 SDNodeBits.IsMemIntrinsic;
615 /// Test if this node is a strict floating point pseudo-op.
616 bool isStrictFPOpcode() {
620 case ISD::STRICT_FADD:
621 case ISD::STRICT_FSUB:
622 case ISD::STRICT_FMUL:
623 case ISD::STRICT_FDIV:
624 case ISD::STRICT_FREM:
625 case ISD::STRICT_FSQRT:
626 case ISD::STRICT_FPOW:
627 case ISD::STRICT_FPOWI:
628 case ISD::STRICT_FSIN:
629 case ISD::STRICT_FCOS:
630 case ISD::STRICT_FEXP:
631 case ISD::STRICT_FEXP2:
632 case ISD::STRICT_FLOG:
633 case ISD::STRICT_FLOG10:
634 case ISD::STRICT_FLOG2:
635 case ISD::STRICT_FRINT:
636 case ISD::STRICT_FNEARBYINT:
641 /// Test if this node has a post-isel opcode, directly
642 /// corresponding to a MachineInstr opcode.
643 bool isMachineOpcode() const { return NodeType < 0; }
645 /// This may only be called if isMachineOpcode returns
646 /// true. It returns the MachineInstr opcode value that the node's opcode
648 unsigned getMachineOpcode() const {
649 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
653 bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
654 void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
656 /// Return true if there are no uses of this node.
657 bool use_empty() const { return UseList == nullptr; }
659 /// Return true if there is exactly one use of this node.
660 bool hasOneUse() const {
661 return !use_empty() && std::next(use_begin()) == use_end();
664 /// Return the number of uses of this node. This method takes
665 /// time proportional to the number of uses.
666 size_t use_size() const { return std::distance(use_begin(), use_end()); }
668 /// Return the unique node id.
669 int getNodeId() const { return NodeId; }
671 /// Set unique node id.
672 void setNodeId(int Id) { NodeId = Id; }
674 /// Return the node ordering.
675 unsigned getIROrder() const { return IROrder; }
677 /// Set the node ordering.
678 void setIROrder(unsigned Order) { IROrder = Order; }
680 /// Return the source location info.
681 const DebugLoc &getDebugLoc() const { return debugLoc; }
683 /// Set source location info. Try to avoid this, putting
684 /// it in the constructor is preferable.
685 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
687 /// This class provides iterator support for SDUse
688 /// operands that use a specific SDNode.
690 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
695 explicit use_iterator(SDUse *op) : Op(op) {}
698 typedef std::iterator<std::forward_iterator_tag,
699 SDUse, ptrdiff_t>::reference reference;
700 typedef std::iterator<std::forward_iterator_tag,
701 SDUse, ptrdiff_t>::pointer pointer;
703 use_iterator() = default;
704 use_iterator(const use_iterator &I) : Op(I.Op) {}
706 bool operator==(const use_iterator &x) const {
709 bool operator!=(const use_iterator &x) const {
710 return !operator==(x);
713 /// Return true if this iterator is at the end of uses list.
714 bool atEnd() const { return Op == nullptr; }
716 // Iterator traversal: forward iteration only.
717 use_iterator &operator++() { // Preincrement
718 assert(Op && "Cannot increment end iterator!");
723 use_iterator operator++(int) { // Postincrement
724 use_iterator tmp = *this; ++*this; return tmp;
727 /// Retrieve a pointer to the current user node.
728 SDNode *operator*() const {
729 assert(Op && "Cannot dereference end iterator!");
730 return Op->getUser();
733 SDNode *operator->() const { return operator*(); }
735 SDUse &getUse() const { return *Op; }
737 /// Retrieve the operand # of this use in its user.
738 unsigned getOperandNo() const {
739 assert(Op && "Cannot dereference end iterator!");
740 return (unsigned)(Op - Op->getUser()->OperandList);
744 /// Provide iteration support to walk over all uses of an SDNode.
745 use_iterator use_begin() const {
746 return use_iterator(UseList);
749 static use_iterator use_end() { return use_iterator(nullptr); }
751 inline iterator_range<use_iterator> uses() {
752 return make_range(use_begin(), use_end());
754 inline iterator_range<use_iterator> uses() const {
755 return make_range(use_begin(), use_end());
758 /// Return true if there are exactly NUSES uses of the indicated value.
759 /// This method ignores uses of other values defined by this operation.
760 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
762 /// Return true if there are any use of the indicated value.
763 /// This method ignores uses of other values defined by this operation.
764 bool hasAnyUseOfValue(unsigned Value) const;
766 /// Return true if this node is the only use of N.
767 bool isOnlyUserOf(const SDNode *N) const;
769 /// Return true if this node is an operand of N.
770 bool isOperandOf(const SDNode *N) const;
772 /// Return true if this node is a predecessor of N.
773 /// NOTE: Implemented on top of hasPredecessor and every bit as
774 /// expensive. Use carefully.
775 bool isPredecessorOf(const SDNode *N) const {
776 return N->hasPredecessor(this);
779 /// Return true if N is a predecessor of this node.
780 /// N is either an operand of this node, or can be reached by recursively
781 /// traversing up the operands.
782 /// NOTE: This is an expensive method. Use it carefully.
783 bool hasPredecessor(const SDNode *N) const;
785 /// Returns true if N is a predecessor of any node in Worklist. This
786 /// helper keeps Visited and Worklist sets externally to allow unions
787 /// searches to be performed in parallel, caching of results across
788 /// queries and incremental addition to Worklist. Stops early if N is
789 /// found but will resume. Remember to clear Visited and Worklists
791 static bool hasPredecessorHelper(const SDNode *N,
792 SmallPtrSetImpl<const SDNode *> &Visited,
793 SmallVectorImpl<const SDNode *> &Worklist) {
794 if (Visited.count(N))
796 while (!Worklist.empty()) {
797 const SDNode *M = Worklist.pop_back_val();
799 for (const SDValue &OpV : M->op_values()) {
800 SDNode *Op = OpV.getNode();
801 if (Visited.insert(Op).second)
802 Worklist.push_back(Op);
812 /// Return true if all the users of N are contained in Nodes.
813 /// NOTE: Requires at least one match, but doesn't require them all.
814 static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
816 /// Return the number of values used by this operation.
817 unsigned getNumOperands() const { return NumOperands; }
819 /// Helper method returns the integer value of a ConstantSDNode operand.
820 inline uint64_t getConstantOperandVal(unsigned Num) const;
822 const SDValue &getOperand(unsigned Num) const {
823 assert(Num < NumOperands && "Invalid child # of SDNode!");
824 return OperandList[Num];
827 typedef SDUse* op_iterator;
829 op_iterator op_begin() const { return OperandList; }
830 op_iterator op_end() const { return OperandList+NumOperands; }
831 ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
833 /// Iterator for directly iterating over the operand SDValue's.
834 struct value_op_iterator
835 : iterator_adaptor_base<value_op_iterator, op_iterator,
836 std::random_access_iterator_tag, SDValue,
837 ptrdiff_t, value_op_iterator *,
838 value_op_iterator *> {
839 explicit value_op_iterator(SDUse *U = nullptr)
840 : iterator_adaptor_base(U) {}
842 const SDValue &operator*() const { return I->get(); }
845 iterator_range<value_op_iterator> op_values() const {
846 return make_range(value_op_iterator(op_begin()),
847 value_op_iterator(op_end()));
850 SDVTList getVTList() const {
851 SDVTList X = { ValueList, NumValues };
855 /// If this node has a glue operand, return the node
856 /// to which the glue operand points. Otherwise return NULL.
857 SDNode *getGluedNode() const {
858 if (getNumOperands() != 0 &&
859 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
860 return getOperand(getNumOperands()-1).getNode();
864 /// If this node has a glue value with a user, return
865 /// the user (there is at most one). Otherwise return NULL.
866 SDNode *getGluedUser() const {
867 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
868 if (UI.getUse().get().getValueType() == MVT::Glue)
873 const SDNodeFlags getFlags() const { return Flags; }
874 void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
876 /// Clear any flags in this node that aren't also set in Flags.
877 /// If Flags is not in a defined state then this has no effect.
878 void intersectFlagsWith(const SDNodeFlags Flags);
880 /// Return the number of values defined/returned by this operator.
881 unsigned getNumValues() const { return NumValues; }
883 /// Return the type of a specified result.
884 EVT getValueType(unsigned ResNo) const {
885 assert(ResNo < NumValues && "Illegal result number!");
886 return ValueList[ResNo];
889 /// Return the type of a specified result as a simple type.
890 MVT getSimpleValueType(unsigned ResNo) const {
891 return getValueType(ResNo).getSimpleVT();
894 /// Returns MVT::getSizeInBits(getValueType(ResNo)).
895 unsigned getValueSizeInBits(unsigned ResNo) const {
896 return getValueType(ResNo).getSizeInBits();
899 typedef const EVT* value_iterator;
900 value_iterator value_begin() const { return ValueList; }
901 value_iterator value_end() const { return ValueList+NumValues; }
903 /// Return the opcode of this operation for printing.
904 std::string getOperationName(const SelectionDAG *G = nullptr) const;
905 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
906 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
907 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
908 void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
909 void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
911 /// Print a SelectionDAG node and all children down to
912 /// the leaves. The given SelectionDAG allows target-specific nodes
913 /// to be printed in human-readable form. Unlike printr, this will
914 /// print the whole DAG, including children that appear multiple
917 void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
919 /// Print a SelectionDAG node and children up to
920 /// depth "depth." The given SelectionDAG allows target-specific
921 /// nodes to be printed in human-readable form. Unlike printr, this
922 /// will print children that appear multiple times wherever they are
925 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
926 unsigned depth = 100) const;
928 /// Dump this node, for debugging.
931 /// Dump (recursively) this node and its use-def subgraph.
934 /// Dump this node, for debugging.
935 /// The given SelectionDAG allows target-specific nodes to be printed
936 /// in human-readable form.
937 void dump(const SelectionDAG *G) const;
939 /// Dump (recursively) this node and its use-def subgraph.
940 /// The given SelectionDAG allows target-specific nodes to be printed
941 /// in human-readable form.
942 void dumpr(const SelectionDAG *G) const;
944 /// printrFull to dbgs(). The given SelectionDAG allows
945 /// target-specific nodes to be printed in human-readable form.
946 /// Unlike dumpr, this will print the whole DAG, including children
947 /// that appear multiple times.
948 void dumprFull(const SelectionDAG *G = nullptr) const;
950 /// printrWithDepth to dbgs(). The given
951 /// SelectionDAG allows target-specific nodes to be printed in
952 /// human-readable form. Unlike dumpr, this will print children
953 /// that appear multiple times wherever they are used.
955 void dumprWithDepth(const SelectionDAG *G = nullptr,
956 unsigned depth = 100) const;
958 /// Gather unique data for the node.
959 void Profile(FoldingSetNodeID &ID) const;
961 /// This method should only be used by the SDUse class.
962 void addUse(SDUse &U) { U.addToList(&UseList); }
965 static SDVTList getSDVTList(EVT VT) {
966 SDVTList Ret = { getValueTypeList(VT), 1 };
970 /// Create an SDNode.
972 /// SDNodes are created without any operands, and never own the operand
973 /// storage. To add operands, see SelectionDAG::createOperands.
974 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
975 : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
976 IROrder(Order), debugLoc(std::move(dl)) {
977 memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
978 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
979 assert(NumValues == VTs.NumVTs &&
980 "NumValues wasn't wide enough for its operands!");
983 /// Release the operands and set this node to have zero operands.
987 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
988 /// into SDNode creation functions.
989 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
990 /// from the original Instruction, and IROrder is the ordinal position of
992 /// When an SDNode is created after the DAG is being built, both DebugLoc and
993 /// the IROrder are propagated from the original SDNode.
994 /// So SDLoc class provides two constructors besides the default one, one to
995 /// be used by the DAGBuilder, the other to be used by others.
1003 SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1004 SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1005 SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1006 assert(Order >= 0 && "bad IROrder");
1008 DL = I->getDebugLoc();
1011 unsigned getIROrder() const { return IROrder; }
1012 const DebugLoc &getDebugLoc() const { return DL; }
1015 // Define inline functions from the SDValue class.
1017 inline SDValue::SDValue(SDNode *node, unsigned resno)
1018 : Node(node), ResNo(resno) {
1019 // Explicitly check for !ResNo to avoid use-after-free, because there are
1020 // callers that use SDValue(N, 0) with a deleted N to indicate successful
1022 assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&
1023 "Invalid result number for the given node!");
1024 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
1027 inline unsigned SDValue::getOpcode() const {
1028 return Node->getOpcode();
1031 inline EVT SDValue::getValueType() const {
1032 return Node->getValueType(ResNo);
1035 inline unsigned SDValue::getNumOperands() const {
1036 return Node->getNumOperands();
1039 inline const SDValue &SDValue::getOperand(unsigned i) const {
1040 return Node->getOperand(i);
1043 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1044 return Node->getConstantOperandVal(i);
1047 inline bool SDValue::isTargetOpcode() const {
1048 return Node->isTargetOpcode();
1051 inline bool SDValue::isTargetMemoryOpcode() const {
1052 return Node->isTargetMemoryOpcode();
1055 inline bool SDValue::isMachineOpcode() const {
1056 return Node->isMachineOpcode();
1059 inline unsigned SDValue::getMachineOpcode() const {
1060 return Node->getMachineOpcode();
1063 inline bool SDValue::isUndef() const {
1064 return Node->isUndef();
1067 inline bool SDValue::use_empty() const {
1068 return !Node->hasAnyUseOfValue(ResNo);
1071 inline bool SDValue::hasOneUse() const {
1072 return Node->hasNUsesOfValue(1, ResNo);
1075 inline const DebugLoc &SDValue::getDebugLoc() const {
1076 return Node->getDebugLoc();
1079 inline void SDValue::dump() const {
1080 return Node->dump();
1083 inline void SDValue::dumpr() const {
1084 return Node->dumpr();
1087 // Define inline functions from the SDUse class.
1089 inline void SDUse::set(const SDValue &V) {
1090 if (Val.getNode()) removeFromList();
1092 if (V.getNode()) V.getNode()->addUse(*this);
1095 inline void SDUse::setInitial(const SDValue &V) {
1097 V.getNode()->addUse(*this);
1100 inline void SDUse::setNode(SDNode *N) {
1101 if (Val.getNode()) removeFromList();
1103 if (N) N->addUse(*this);
1106 /// This class is used to form a handle around another node that
1107 /// is persistent and is updated across invocations of replaceAllUsesWith on its
1108 /// operand. This node should be directly created by end-users and not added to
1109 /// the AllNodes list.
1110 class HandleSDNode : public SDNode {
1114 explicit HandleSDNode(SDValue X)
1115 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1116 // HandleSDNodes are never inserted into the DAG, so they won't be
1117 // auto-numbered. Use ID 65535 as a sentinel.
1118 PersistentId = 0xffff;
1120 // Manually set up the operand list. This node type is special in that it's
1121 // always stack allocated and SelectionDAG does not manage its operands.
1122 // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1131 const SDValue &getValue() const { return Op; }
1134 class AddrSpaceCastSDNode : public SDNode {
1136 unsigned SrcAddrSpace;
1137 unsigned DestAddrSpace;
1140 AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1141 unsigned SrcAS, unsigned DestAS);
1143 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1144 unsigned getDestAddressSpace() const { return DestAddrSpace; }
1146 static bool classof(const SDNode *N) {
1147 return N->getOpcode() == ISD::ADDRSPACECAST;
1151 /// This is an abstract virtual class for memory operations.
1152 class MemSDNode : public SDNode {
1154 // VT of in-memory value.
1158 /// Memory reference information.
1159 MachineMemOperand *MMO;
1162 MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1163 EVT MemoryVT, MachineMemOperand *MMO);
1165 bool readMem() const { return MMO->isLoad(); }
1166 bool writeMem() const { return MMO->isStore(); }
1168 /// Returns alignment and volatility of the memory access
1169 unsigned getOriginalAlignment() const {
1170 return MMO->getBaseAlignment();
1172 unsigned getAlignment() const {
1173 return MMO->getAlignment();
1176 /// Return the SubclassData value, without HasDebugValue. This contains an
1177 /// encoding of the volatile flag, as well as bits used by subclasses. This
1178 /// function should only be used to compute a FoldingSetNodeID value.
1179 /// The HasDebugValue bit is masked out because CSE map needs to match
1180 /// nodes with debug info with nodes without debug info.
1181 unsigned getRawSubclassData() const {
1184 char RawSDNodeBits[sizeof(uint16_t)];
1185 SDNodeBitfields SDNodeBits;
1187 memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1188 SDNodeBits.HasDebugValue = 0;
1189 memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1193 bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1194 bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1195 bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1196 bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1198 // Returns the offset from the location of the access.
1199 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1201 /// Returns the AA info that describes the dereference.
1202 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1204 /// Returns the Ranges that describes the dereference.
1205 const MDNode *getRanges() const { return MMO->getRanges(); }
1207 /// Return the synchronization scope for this memory operation.
1208 SynchronizationScope getSynchScope() const { return MMO->getSynchScope(); }
1210 /// Return the atomic ordering requirements for this memory operation. For
1211 /// cmpxchg atomic operations, return the atomic ordering requirements when
1213 AtomicOrdering getOrdering() const { return MMO->getOrdering(); }
1215 /// Return the type of the in-memory value.
1216 EVT getMemoryVT() const { return MemoryVT; }
1218 /// Return a MachineMemOperand object describing the memory
1219 /// reference performed by operation.
1220 MachineMemOperand *getMemOperand() const { return MMO; }
1222 const MachinePointerInfo &getPointerInfo() const {
1223 return MMO->getPointerInfo();
1226 /// Return the address space for the associated pointer
1227 unsigned getAddressSpace() const {
1228 return getPointerInfo().getAddrSpace();
1231 /// Update this MemSDNode's MachineMemOperand information
1232 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1233 /// This must only be used when the new alignment applies to all users of
1234 /// this MachineMemOperand.
1235 void refineAlignment(const MachineMemOperand *NewMMO) {
1236 MMO->refineAlignment(NewMMO);
1239 const SDValue &getChain() const { return getOperand(0); }
1240 const SDValue &getBasePtr() const {
1241 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1244 // Methods to support isa and dyn_cast
1245 static bool classof(const SDNode *N) {
1246 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1247 // with either an intrinsic or a target opcode.
1248 return N->getOpcode() == ISD::LOAD ||
1249 N->getOpcode() == ISD::STORE ||
1250 N->getOpcode() == ISD::PREFETCH ||
1251 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1252 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1253 N->getOpcode() == ISD::ATOMIC_SWAP ||
1254 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1255 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1256 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1257 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1258 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1259 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1260 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1261 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1262 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1263 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1264 N->getOpcode() == ISD::ATOMIC_LOAD ||
1265 N->getOpcode() == ISD::ATOMIC_STORE ||
1266 N->getOpcode() == ISD::MLOAD ||
1267 N->getOpcode() == ISD::MSTORE ||
1268 N->getOpcode() == ISD::MGATHER ||
1269 N->getOpcode() == ISD::MSCATTER ||
1270 N->isMemIntrinsic() ||
1271 N->isTargetMemoryOpcode();
1275 /// This is an SDNode representing atomic operations.
1276 class AtomicSDNode : public MemSDNode {
1278 AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
1279 EVT MemVT, MachineMemOperand *MMO)
1280 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {}
1282 const SDValue &getBasePtr() const { return getOperand(1); }
1283 const SDValue &getVal() const { return getOperand(2); }
1285 /// Returns true if this SDNode represents cmpxchg atomic operation, false
1287 bool isCompareAndSwap() const {
1288 unsigned Op = getOpcode();
1289 return Op == ISD::ATOMIC_CMP_SWAP ||
1290 Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1293 /// For cmpxchg atomic operations, return the atomic ordering requirements
1294 /// when store does not occur.
1295 AtomicOrdering getFailureOrdering() const {
1296 assert(isCompareAndSwap() && "Must be cmpxchg operation");
1297 return MMO->getFailureOrdering();
1300 // Methods to support isa and dyn_cast
1301 static bool classof(const SDNode *N) {
1302 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1303 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1304 N->getOpcode() == ISD::ATOMIC_SWAP ||
1305 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1306 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1307 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1308 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1309 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1310 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1311 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1312 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1313 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1314 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1315 N->getOpcode() == ISD::ATOMIC_LOAD ||
1316 N->getOpcode() == ISD::ATOMIC_STORE;
1320 /// This SDNode is used for target intrinsics that touch
1321 /// memory and need an associated MachineMemOperand. Its opcode may be
1322 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1323 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1324 class MemIntrinsicSDNode : public MemSDNode {
1326 MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
1327 SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
1328 : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
1329 SDNodeBits.IsMemIntrinsic = true;
1332 // Methods to support isa and dyn_cast
1333 static bool classof(const SDNode *N) {
1334 // We lower some target intrinsics to their target opcode
1335 // early a node with a target opcode can be of this class
1336 return N->isMemIntrinsic() ||
1337 N->getOpcode() == ISD::PREFETCH ||
1338 N->isTargetMemoryOpcode();
1342 /// This SDNode is used to implement the code generator
1343 /// support for the llvm IR shufflevector instruction. It combines elements
1344 /// from two input vectors into a new input vector, with the selection and
1345 /// ordering of elements determined by an array of integers, referred to as
1346 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1347 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1348 /// An index of -1 is treated as undef, such that the code generator may put
1349 /// any value in the corresponding element of the result.
1350 class ShuffleVectorSDNode : public SDNode {
1351 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1352 // is freed when the SelectionDAG object is destroyed.
1356 friend class SelectionDAG;
1358 ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
1359 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
1362 ArrayRef<int> getMask() const {
1363 EVT VT = getValueType(0);
1364 return makeArrayRef(Mask, VT.getVectorNumElements());
1367 int getMaskElt(unsigned Idx) const {
1368 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1372 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1374 int getSplatIndex() const {
1375 assert(isSplat() && "Cannot get splat index for non-splat!");
1376 EVT VT = getValueType(0);
1377 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1381 llvm_unreachable("Splat with all undef indices?");
1384 static bool isSplatMask(const int *Mask, EVT VT);
1386 /// Change values in a shuffle permute mask assuming
1387 /// the two vector operands have swapped position.
1388 static void commuteMask(MutableArrayRef<int> Mask) {
1389 unsigned NumElems = Mask.size();
1390 for (unsigned i = 0; i != NumElems; ++i) {
1394 else if (idx < (int)NumElems)
1395 Mask[i] = idx + NumElems;
1397 Mask[i] = idx - NumElems;
1401 static bool classof(const SDNode *N) {
1402 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1406 class ConstantSDNode : public SDNode {
1407 friend class SelectionDAG;
1409 const ConstantInt *Value;
1411 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val,
1412 const DebugLoc &DL, EVT VT)
1413 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DL,
1416 ConstantSDNodeBits.IsOpaque = isOpaque;
1420 const ConstantInt *getConstantIntValue() const { return Value; }
1421 const APInt &getAPIntValue() const { return Value->getValue(); }
1422 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1423 int64_t getSExtValue() const { return Value->getSExtValue(); }
1425 bool isOne() const { return Value->isOne(); }
1426 bool isNullValue() const { return Value->isNullValue(); }
1427 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1429 bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
1431 static bool classof(const SDNode *N) {
1432 return N->getOpcode() == ISD::Constant ||
1433 N->getOpcode() == ISD::TargetConstant;
1437 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
1438 return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
1441 class ConstantFPSDNode : public SDNode {
1442 friend class SelectionDAG;
1444 const ConstantFP *Value;
1446 ConstantFPSDNode(bool isTarget, const ConstantFP *val, const DebugLoc &DL,
1448 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0, DL,
1453 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1454 const ConstantFP *getConstantFPValue() const { return Value; }
1456 /// Return true if the value is positive or negative zero.
1457 bool isZero() const { return Value->isZero(); }
1459 /// Return true if the value is a NaN.
1460 bool isNaN() const { return Value->isNaN(); }
1462 /// Return true if the value is an infinity
1463 bool isInfinity() const { return Value->isInfinity(); }
1465 /// Return true if the value is negative.
1466 bool isNegative() const { return Value->isNegative(); }
1468 /// We don't rely on operator== working on double values, as
1469 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1470 /// As such, this method can be used to do an exact bit-for-bit comparison of
1471 /// two floating point values.
1473 /// We leave the version with the double argument here because it's just so
1474 /// convenient to write "2.0" and the like. Without this function we'd
1475 /// have to duplicate its logic everywhere it's called.
1476 bool isExactlyValue(double V) const {
1479 Tmp.convert(Value->getValueAPF().getSemantics(),
1480 APFloat::rmNearestTiesToEven, &ignored);
1481 return isExactlyValue(Tmp);
1483 bool isExactlyValue(const APFloat& V) const;
1485 static bool isValueValidForType(EVT VT, const APFloat& Val);
1487 static bool classof(const SDNode *N) {
1488 return N->getOpcode() == ISD::ConstantFP ||
1489 N->getOpcode() == ISD::TargetConstantFP;
1493 /// Returns true if \p V is a constant integer zero.
1494 bool isNullConstant(SDValue V);
1496 /// Returns true if \p V is an FP constant with a value of positive zero.
1497 bool isNullFPConstant(SDValue V);
1499 /// Returns true if \p V is an integer constant with all bits set.
1500 bool isAllOnesConstant(SDValue V);
1502 /// Returns true if \p V is a constant integer one.
1503 bool isOneConstant(SDValue V);
1505 /// Returns true if \p V is a bitwise not operation. Assumes that an all ones
1506 /// constant is canonicalized to be operand 1.
1507 bool isBitwiseNot(SDValue V);
1509 /// Returns the SDNode if it is a constant splat BuildVector or constant int.
1510 ConstantSDNode *isConstOrConstSplat(SDValue V);
1512 /// Returns the SDNode if it is a constant splat BuildVector or constant float.
1513 ConstantFPSDNode *isConstOrConstSplatFP(SDValue V);
1515 class GlobalAddressSDNode : public SDNode {
1516 friend class SelectionDAG;
1518 const GlobalValue *TheGlobal;
1520 unsigned char TargetFlags;
1522 GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
1523 const GlobalValue *GA, EVT VT, int64_t o,
1524 unsigned char TargetFlags);
1527 const GlobalValue *getGlobal() const { return TheGlobal; }
1528 int64_t getOffset() const { return Offset; }
1529 unsigned char getTargetFlags() const { return TargetFlags; }
1530 // Return the address space this GlobalAddress belongs to.
1531 unsigned getAddressSpace() const;
1533 static bool classof(const SDNode *N) {
1534 return N->getOpcode() == ISD::GlobalAddress ||
1535 N->getOpcode() == ISD::TargetGlobalAddress ||
1536 N->getOpcode() == ISD::GlobalTLSAddress ||
1537 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1541 class FrameIndexSDNode : public SDNode {
1542 friend class SelectionDAG;
1546 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1547 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1548 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1552 int getIndex() const { return FI; }
1554 static bool classof(const SDNode *N) {
1555 return N->getOpcode() == ISD::FrameIndex ||
1556 N->getOpcode() == ISD::TargetFrameIndex;
1560 class JumpTableSDNode : public SDNode {
1561 friend class SelectionDAG;
1564 unsigned char TargetFlags;
1566 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1567 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1568 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1572 int getIndex() const { return JTI; }
1573 unsigned char getTargetFlags() const { return TargetFlags; }
1575 static bool classof(const SDNode *N) {
1576 return N->getOpcode() == ISD::JumpTable ||
1577 N->getOpcode() == ISD::TargetJumpTable;
1581 class ConstantPoolSDNode : public SDNode {
1582 friend class SelectionDAG;
1585 const Constant *ConstVal;
1586 MachineConstantPoolValue *MachineCPVal;
1588 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1589 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1590 unsigned char TargetFlags;
1592 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1593 unsigned Align, unsigned char TF)
1594 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1595 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1597 assert(Offset >= 0 && "Offset is too large");
1601 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1602 EVT VT, int o, unsigned Align, unsigned char TF)
1603 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1604 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1606 assert(Offset >= 0 && "Offset is too large");
1607 Val.MachineCPVal = v;
1608 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1612 bool isMachineConstantPoolEntry() const {
1616 const Constant *getConstVal() const {
1617 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1618 return Val.ConstVal;
1621 MachineConstantPoolValue *getMachineCPVal() const {
1622 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1623 return Val.MachineCPVal;
1626 int getOffset() const {
1627 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1630 // Return the alignment of this constant pool object, which is either 0 (for
1631 // default alignment) or the desired value.
1632 unsigned getAlignment() const { return Alignment; }
1633 unsigned char getTargetFlags() const { return TargetFlags; }
1635 Type *getType() const;
1637 static bool classof(const SDNode *N) {
1638 return N->getOpcode() == ISD::ConstantPool ||
1639 N->getOpcode() == ISD::TargetConstantPool;
1643 /// Completely target-dependent object reference.
1644 class TargetIndexSDNode : public SDNode {
1645 friend class SelectionDAG;
1647 unsigned char TargetFlags;
1652 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1653 : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1654 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1656 unsigned char getTargetFlags() const { return TargetFlags; }
1657 int getIndex() const { return Index; }
1658 int64_t getOffset() const { return Offset; }
1660 static bool classof(const SDNode *N) {
1661 return N->getOpcode() == ISD::TargetIndex;
1665 class BasicBlockSDNode : public SDNode {
1666 friend class SelectionDAG;
1668 MachineBasicBlock *MBB;
1670 /// Debug info is meaningful and potentially useful here, but we create
1671 /// blocks out of order when they're jumped to, which makes it a bit
1672 /// harder. Let's see if we need it first.
1673 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1674 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1678 MachineBasicBlock *getBasicBlock() const { return MBB; }
1680 static bool classof(const SDNode *N) {
1681 return N->getOpcode() == ISD::BasicBlock;
1685 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1686 class BuildVectorSDNode : public SDNode {
1688 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1689 explicit BuildVectorSDNode() = delete;
1691 /// Check if this is a constant splat, and if so, find the
1692 /// smallest element size that splats the vector. If MinSplatBits is
1693 /// nonzero, the element size must be at least that large. Note that the
1694 /// splat element may be the entire vector (i.e., a one element vector).
1695 /// Returns the splat element value in SplatValue. Any undefined bits in
1696 /// that value are zero, and the corresponding bits in the SplatUndef mask
1697 /// are set. The SplatBitSize value is set to the splat element size in
1698 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1699 /// undefined. isBigEndian describes the endianness of the target.
1700 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1701 unsigned &SplatBitSize, bool &HasAnyUndefs,
1702 unsigned MinSplatBits = 0,
1703 bool isBigEndian = false) const;
1705 /// \brief Returns the splatted value or a null value if this is not a splat.
1707 /// If passed a non-null UndefElements bitvector, it will resize it to match
1708 /// the vector width and set the bits where elements are undef.
1709 SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1711 /// \brief Returns the splatted constant or null if this is not a constant
1714 /// If passed a non-null UndefElements bitvector, it will resize it to match
1715 /// the vector width and set the bits where elements are undef.
1717 getConstantSplatNode(BitVector *UndefElements = nullptr) const;
1719 /// \brief Returns the splatted constant FP or null if this is not a constant
1722 /// If passed a non-null UndefElements bitvector, it will resize it to match
1723 /// the vector width and set the bits where elements are undef.
1725 getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
1727 /// \brief If this is a constant FP splat and the splatted constant FP is an
1728 /// exact power or 2, return the log base 2 integer value. Otherwise,
1731 /// The BitWidth specifies the necessary bit precision.
1732 int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
1733 uint32_t BitWidth) const;
1735 bool isConstant() const;
1737 static inline bool classof(const SDNode *N) {
1738 return N->getOpcode() == ISD::BUILD_VECTOR;
1742 /// An SDNode that holds an arbitrary LLVM IR Value. This is
1743 /// used when the SelectionDAG needs to make a simple reference to something
1744 /// in the LLVM IR representation.
1746 class SrcValueSDNode : public SDNode {
1747 friend class SelectionDAG;
1751 /// Create a SrcValue for a general value.
1752 explicit SrcValueSDNode(const Value *v)
1753 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1756 /// Return the contained Value.
1757 const Value *getValue() const { return V; }
1759 static bool classof(const SDNode *N) {
1760 return N->getOpcode() == ISD::SRCVALUE;
1764 class MDNodeSDNode : public SDNode {
1765 friend class SelectionDAG;
1769 explicit MDNodeSDNode(const MDNode *md)
1770 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1774 const MDNode *getMD() const { return MD; }
1776 static bool classof(const SDNode *N) {
1777 return N->getOpcode() == ISD::MDNODE_SDNODE;
1781 class RegisterSDNode : public SDNode {
1782 friend class SelectionDAG;
1786 RegisterSDNode(unsigned reg, EVT VT)
1787 : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
1790 unsigned getReg() const { return Reg; }
1792 static bool classof(const SDNode *N) {
1793 return N->getOpcode() == ISD::Register;
1797 class RegisterMaskSDNode : public SDNode {
1798 friend class SelectionDAG;
1800 // The memory for RegMask is not owned by the node.
1801 const uint32_t *RegMask;
1803 RegisterMaskSDNode(const uint32_t *mask)
1804 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1808 const uint32_t *getRegMask() const { return RegMask; }
1810 static bool classof(const SDNode *N) {
1811 return N->getOpcode() == ISD::RegisterMask;
1815 class BlockAddressSDNode : public SDNode {
1816 friend class SelectionDAG;
1818 const BlockAddress *BA;
1820 unsigned char TargetFlags;
1822 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1823 int64_t o, unsigned char Flags)
1824 : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1825 BA(ba), Offset(o), TargetFlags(Flags) {
1829 const BlockAddress *getBlockAddress() const { return BA; }
1830 int64_t getOffset() const { return Offset; }
1831 unsigned char getTargetFlags() const { return TargetFlags; }
1833 static bool classof(const SDNode *N) {
1834 return N->getOpcode() == ISD::BlockAddress ||
1835 N->getOpcode() == ISD::TargetBlockAddress;
1839 class EHLabelSDNode : public SDNode {
1840 friend class SelectionDAG;
1844 EHLabelSDNode(unsigned Order, const DebugLoc &dl, MCSymbol *L)
1845 : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {}
1848 MCSymbol *getLabel() const { return Label; }
1850 static bool classof(const SDNode *N) {
1851 return N->getOpcode() == ISD::EH_LABEL;
1855 class ExternalSymbolSDNode : public SDNode {
1856 friend class SelectionDAG;
1859 unsigned char TargetFlags;
1861 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1862 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1863 0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {}
1866 const char *getSymbol() const { return Symbol; }
1867 unsigned char getTargetFlags() const { return TargetFlags; }
1869 static bool classof(const SDNode *N) {
1870 return N->getOpcode() == ISD::ExternalSymbol ||
1871 N->getOpcode() == ISD::TargetExternalSymbol;
1875 class MCSymbolSDNode : public SDNode {
1876 friend class SelectionDAG;
1880 MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
1881 : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
1884 MCSymbol *getMCSymbol() const { return Symbol; }
1886 static bool classof(const SDNode *N) {
1887 return N->getOpcode() == ISD::MCSymbol;
1891 class CondCodeSDNode : public SDNode {
1892 friend class SelectionDAG;
1894 ISD::CondCode Condition;
1896 explicit CondCodeSDNode(ISD::CondCode Cond)
1897 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1901 ISD::CondCode get() const { return Condition; }
1903 static bool classof(const SDNode *N) {
1904 return N->getOpcode() == ISD::CONDCODE;
1908 /// This class is used to represent EVT's, which are used
1909 /// to parameterize some operations.
1910 class VTSDNode : public SDNode {
1911 friend class SelectionDAG;
1915 explicit VTSDNode(EVT VT)
1916 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1920 EVT getVT() const { return ValueType; }
1922 static bool classof(const SDNode *N) {
1923 return N->getOpcode() == ISD::VALUETYPE;
1927 /// Base class for LoadSDNode and StoreSDNode
1928 class LSBaseSDNode : public MemSDNode {
1930 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
1931 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1932 MachineMemOperand *MMO)
1933 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1934 LSBaseSDNodeBits.AddressingMode = AM;
1935 assert(getAddressingMode() == AM && "Value truncated");
1938 const SDValue &getOffset() const {
1939 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1942 /// Return the addressing mode for this load or store:
1943 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1944 ISD::MemIndexedMode getAddressingMode() const {
1945 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
1948 /// Return true if this is a pre/post inc/dec load/store.
1949 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1951 /// Return true if this is NOT a pre/post inc/dec load/store.
1952 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1954 static bool classof(const SDNode *N) {
1955 return N->getOpcode() == ISD::LOAD ||
1956 N->getOpcode() == ISD::STORE;
1960 /// This class is used to represent ISD::LOAD nodes.
1961 class LoadSDNode : public LSBaseSDNode {
1962 friend class SelectionDAG;
1964 LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
1965 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1966 MachineMemOperand *MMO)
1967 : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
1968 LoadSDNodeBits.ExtTy = ETy;
1969 assert(readMem() && "Load MachineMemOperand is not a load!");
1970 assert(!writeMem() && "Load MachineMemOperand is a store!");
1974 /// Return whether this is a plain node,
1975 /// or one of the varieties of value-extending loads.
1976 ISD::LoadExtType getExtensionType() const {
1977 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
1980 const SDValue &getBasePtr() const { return getOperand(1); }
1981 const SDValue &getOffset() const { return getOperand(2); }
1983 static bool classof(const SDNode *N) {
1984 return N->getOpcode() == ISD::LOAD;
1988 /// This class is used to represent ISD::STORE nodes.
1989 class StoreSDNode : public LSBaseSDNode {
1990 friend class SelectionDAG;
1992 StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
1993 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1994 MachineMemOperand *MMO)
1995 : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
1996 StoreSDNodeBits.IsTruncating = isTrunc;
1997 assert(!readMem() && "Store MachineMemOperand is a load!");
1998 assert(writeMem() && "Store MachineMemOperand is not a store!");
2002 /// Return true if the op does a truncation before store.
2003 /// For integers this is the same as doing a TRUNCATE and storing the result.
2004 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2005 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2007 const SDValue &getValue() const { return getOperand(1); }
2008 const SDValue &getBasePtr() const { return getOperand(2); }
2009 const SDValue &getOffset() const { return getOperand(3); }
2011 static bool classof(const SDNode *N) {
2012 return N->getOpcode() == ISD::STORE;
2016 /// This base class is used to represent MLOAD and MSTORE nodes
2017 class MaskedLoadStoreSDNode : public MemSDNode {
2019 friend class SelectionDAG;
2021 MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2022 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2023 MachineMemOperand *MMO)
2024 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {}
2026 // In the both nodes address is Op1, mask is Op2:
2027 // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
2028 // MaskedStoreSDNode (Chain, ptr, mask, data)
2029 // Mask is a vector of i1 elements
2030 const SDValue &getBasePtr() const { return getOperand(1); }
2031 const SDValue &getMask() const { return getOperand(2); }
2033 static bool classof(const SDNode *N) {
2034 return N->getOpcode() == ISD::MLOAD ||
2035 N->getOpcode() == ISD::MSTORE;
2039 /// This class is used to represent an MLOAD node
2040 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
2042 friend class SelectionDAG;
2044 MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2045 ISD::LoadExtType ETy, bool IsExpanding, EVT MemVT,
2046 MachineMemOperand *MMO)
2047 : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, MemVT, MMO) {
2048 LoadSDNodeBits.ExtTy = ETy;
2049 LoadSDNodeBits.IsExpanding = IsExpanding;
2052 ISD::LoadExtType getExtensionType() const {
2053 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2056 const SDValue &getSrc0() const { return getOperand(3); }
2057 static bool classof(const SDNode *N) {
2058 return N->getOpcode() == ISD::MLOAD;
2061 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2064 /// This class is used to represent an MSTORE node
2065 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2067 friend class SelectionDAG;
2069 MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2070 bool isTrunc, bool isCompressing, EVT MemVT,
2071 MachineMemOperand *MMO)
2072 : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, MemVT, MMO) {
2073 StoreSDNodeBits.IsTruncating = isTrunc;
2074 StoreSDNodeBits.IsCompressing = isCompressing;
2077 /// Return true if the op does a truncation before store.
2078 /// For integers this is the same as doing a TRUNCATE and storing the result.
2079 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2080 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2082 /// Returns true if the op does a compression to the vector before storing.
2083 /// The node contiguously stores the active elements (integers or floats)
2084 /// in src (those with their respective bit set in writemask k) to unaligned
2085 /// memory at base_addr.
2086 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2088 const SDValue &getValue() const { return getOperand(3); }
2090 static bool classof(const SDNode *N) {
2091 return N->getOpcode() == ISD::MSTORE;
2095 /// This is a base class used to represent
2096 /// MGATHER and MSCATTER nodes
2098 class MaskedGatherScatterSDNode : public MemSDNode {
2100 friend class SelectionDAG;
2102 MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2103 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2104 MachineMemOperand *MMO)
2105 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {}
2107 // In the both nodes address is Op1, mask is Op2:
2108 // MaskedGatherSDNode (Chain, src0, mask, base, index), src0 is a passthru value
2109 // MaskedScatterSDNode (Chain, value, mask, base, index)
2110 // Mask is a vector of i1 elements
2111 const SDValue &getBasePtr() const { return getOperand(3); }
2112 const SDValue &getIndex() const { return getOperand(4); }
2113 const SDValue &getMask() const { return getOperand(2); }
2114 const SDValue &getValue() const { return getOperand(1); }
2116 static bool classof(const SDNode *N) {
2117 return N->getOpcode() == ISD::MGATHER ||
2118 N->getOpcode() == ISD::MSCATTER;
2122 /// This class is used to represent an MGATHER node
2124 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2126 friend class SelectionDAG;
2128 MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2129 EVT MemVT, MachineMemOperand *MMO)
2130 : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO) {}
2132 static bool classof(const SDNode *N) {
2133 return N->getOpcode() == ISD::MGATHER;
2137 /// This class is used to represent an MSCATTER node
2139 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2141 friend class SelectionDAG;
2143 MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2144 EVT MemVT, MachineMemOperand *MMO)
2145 : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO) {}
2147 static bool classof(const SDNode *N) {
2148 return N->getOpcode() == ISD::MSCATTER;
2152 /// An SDNode that represents everything that will be needed
2153 /// to construct a MachineInstr. These nodes are created during the
2154 /// instruction selection proper phase.
2155 class MachineSDNode : public SDNode {
2157 typedef MachineMemOperand **mmo_iterator;
2160 friend class SelectionDAG;
2162 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
2163 : SDNode(Opc, Order, DL, VTs) {}
2165 /// Memory reference descriptions for this instruction.
2166 mmo_iterator MemRefs = nullptr;
2167 mmo_iterator MemRefsEnd = nullptr;
2170 mmo_iterator memoperands_begin() const { return MemRefs; }
2171 mmo_iterator memoperands_end() const { return MemRefsEnd; }
2172 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
2174 /// Assign this MachineSDNodes's memory reference descriptor
2175 /// list. This does not transfer ownership.
2176 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
2177 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
2178 assert(*MMI && "Null mem ref detected!");
2179 MemRefs = NewMemRefs;
2180 MemRefsEnd = NewMemRefsEnd;
2183 static bool classof(const SDNode *N) {
2184 return N->isMachineOpcode();
2188 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
2189 SDNode, ptrdiff_t> {
2193 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2196 bool operator==(const SDNodeIterator& x) const {
2197 return Operand == x.Operand;
2199 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2201 pointer operator*() const {
2202 return Node->getOperand(Operand).getNode();
2204 pointer operator->() const { return operator*(); }
2206 SDNodeIterator& operator++() { // Preincrement
2210 SDNodeIterator operator++(int) { // Postincrement
2211 SDNodeIterator tmp = *this; ++*this; return tmp;
2213 size_t operator-(SDNodeIterator Other) const {
2214 assert(Node == Other.Node &&
2215 "Cannot compare iterators of two different nodes!");
2216 return Operand - Other.Operand;
2219 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2220 static SDNodeIterator end (const SDNode *N) {
2221 return SDNodeIterator(N, N->getNumOperands());
2224 unsigned getOperand() const { return Operand; }
2225 const SDNode *getNode() const { return Node; }
2228 template <> struct GraphTraits<SDNode*> {
2229 typedef SDNode *NodeRef;
2230 typedef SDNodeIterator ChildIteratorType;
2232 static NodeRef getEntryNode(SDNode *N) { return N; }
2234 static ChildIteratorType child_begin(NodeRef N) {
2235 return SDNodeIterator::begin(N);
2238 static ChildIteratorType child_end(NodeRef N) {
2239 return SDNodeIterator::end(N);
2243 /// A representation of the largest SDNode, for use in sizeof().
2245 /// This needs to be a union because the largest node differs on 32 bit systems
2246 /// with 4 and 8 byte pointer alignment, respectively.
2247 typedef AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
2248 BlockAddressSDNode, GlobalAddressSDNode>
2251 /// The SDNode class with the greatest alignment requirement.
2252 typedef GlobalAddressSDNode MostAlignedSDNode;
2256 /// Returns true if the specified node is a non-extending and unindexed load.
2257 inline bool isNormalLoad(const SDNode *N) {
2258 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2259 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2260 Ld->getAddressingMode() == ISD::UNINDEXED;
2263 /// Returns true if the specified node is a non-extending load.
2264 inline bool isNON_EXTLoad(const SDNode *N) {
2265 return isa<LoadSDNode>(N) &&
2266 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2269 /// Returns true if the specified node is a EXTLOAD.
2270 inline bool isEXTLoad(const SDNode *N) {
2271 return isa<LoadSDNode>(N) &&
2272 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2275 /// Returns true if the specified node is a SEXTLOAD.
2276 inline bool isSEXTLoad(const SDNode *N) {
2277 return isa<LoadSDNode>(N) &&
2278 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2281 /// Returns true if the specified node is a ZEXTLOAD.
2282 inline bool isZEXTLoad(const SDNode *N) {
2283 return isa<LoadSDNode>(N) &&
2284 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2287 /// Returns true if the specified node is an unindexed load.
2288 inline bool isUNINDEXEDLoad(const SDNode *N) {
2289 return isa<LoadSDNode>(N) &&
2290 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2293 /// Returns true if the specified node is a non-truncating
2294 /// and unindexed store.
2295 inline bool isNormalStore(const SDNode *N) {
2296 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2297 return St && !St->isTruncatingStore() &&
2298 St->getAddressingMode() == ISD::UNINDEXED;
2301 /// Returns true if the specified node is a non-truncating store.
2302 inline bool isNON_TRUNCStore(const SDNode *N) {
2303 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2306 /// Returns true if the specified node is a truncating store.
2307 inline bool isTRUNCStore(const SDNode *N) {
2308 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2311 /// Returns true if the specified node is an unindexed store.
2312 inline bool isUNINDEXEDStore(const SDNode *N) {
2313 return isa<StoreSDNode>(N) &&
2314 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2317 } // end namespace ISD
2319 } // end namespace llvm
2321 #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H