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/BitVector.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/STLExtras.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_range.h"
30 #include "llvm/CodeGen/ISDOpcodes.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/CodeGen/ValueTypes.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DebugLoc.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/Support/DataTypes.h"
37 #include "llvm/Support/MathExtras.h"
44 class MachineBasicBlock;
45 class MachineConstantPoolValue;
49 template <typename T> struct DenseMapInfo;
50 template <typename T> struct simplify_type;
51 template <typename T> struct ilist_traits;
53 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
56 /// This represents a list of ValueType's that has been intern'd by
57 /// a SelectionDAG. Instances of this simple value class are returned by
58 /// SelectionDAG::getVTList(...).
68 /// Return true if the specified node is a
69 /// BUILD_VECTOR where all of the elements are ~0 or undef.
70 bool isBuildVectorAllOnes(const SDNode *N);
72 /// Return true if the specified node is a
73 /// BUILD_VECTOR where all of the elements are 0 or undef.
74 bool isBuildVectorAllZeros(const SDNode *N);
76 /// \brief Return true if the specified node is a BUILD_VECTOR node of
77 /// all ConstantSDNode or undef.
78 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
80 /// \brief Return true if the specified node is a BUILD_VECTOR node of
81 /// all ConstantFPSDNode or undef.
82 bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
84 /// Return true if the specified node is a
85 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
86 /// element is not an undef.
87 bool isScalarToVector(const SDNode *N);
89 /// Return true if the node has at least one operand
90 /// and all operands of the specified node are ISD::UNDEF.
91 bool allOperandsUndef(const SDNode *N);
92 } // end llvm:ISD namespace
94 //===----------------------------------------------------------------------===//
95 /// Unlike LLVM values, Selection DAG nodes may return multiple
96 /// values as the result of a computation. Many nodes return multiple values,
97 /// from loads (which define a token and a return value) to ADDC (which returns
98 /// a result and a carry value), to calls (which may return an arbitrary number
101 /// As such, each use of a SelectionDAG computation must indicate the node that
102 /// computes it as well as which return value to use from that node. This pair
103 /// of information is represented with the SDValue value type.
106 friend struct DenseMapInfo<SDValue>;
108 SDNode *Node; // The node defining the value we are using.
109 unsigned ResNo; // Which return value of the node we are using.
111 SDValue() : Node(nullptr), ResNo(0) {}
112 SDValue(SDNode *node, unsigned resno);
114 /// get the index which selects a specific result in the SDNode
115 unsigned getResNo() const { return ResNo; }
117 /// get the SDNode which holds the desired result
118 SDNode *getNode() const { return Node; }
121 void setNode(SDNode *N) { Node = N; }
123 inline SDNode *operator->() const { return Node; }
125 bool operator==(const SDValue &O) const {
126 return Node == O.Node && ResNo == O.ResNo;
128 bool operator!=(const SDValue &O) const {
129 return !operator==(O);
131 bool operator<(const SDValue &O) const {
132 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
134 explicit operator bool() const {
135 return Node != nullptr;
138 SDValue getValue(unsigned R) const {
139 return SDValue(Node, R);
142 // Return true if this node is an operand of N.
143 bool isOperandOf(SDNode *N) const;
145 /// Return the ValueType of the referenced return value.
146 inline EVT getValueType() const;
148 /// Return the simple ValueType of the referenced return value.
149 MVT getSimpleValueType() const {
150 return getValueType().getSimpleVT();
153 /// Returns the size of the value in bits.
154 unsigned getValueSizeInBits() const {
155 return getValueType().getSizeInBits();
158 unsigned getScalarValueSizeInBits() const {
159 return getValueType().getScalarType().getSizeInBits();
162 // Forwarding methods - These forward to the corresponding methods in SDNode.
163 inline unsigned getOpcode() const;
164 inline unsigned getNumOperands() const;
165 inline const SDValue &getOperand(unsigned i) const;
166 inline uint64_t getConstantOperandVal(unsigned i) const;
167 inline bool isTargetMemoryOpcode() const;
168 inline bool isTargetOpcode() const;
169 inline bool isMachineOpcode() const;
170 inline unsigned getMachineOpcode() const;
171 inline const DebugLoc &getDebugLoc() const;
172 inline void dump() const;
173 inline void dumpr() const;
175 /// Return true if this operand (which must be a chain) reaches the
176 /// specified operand without crossing any side-effecting instructions.
177 /// In practice, this looks through token factors and non-volatile loads.
178 /// In order to remain efficient, this only
179 /// looks a couple of nodes in, it does not do an exhaustive search.
180 bool reachesChainWithoutSideEffects(SDValue Dest,
181 unsigned Depth = 2) const;
183 /// Return true if there are no nodes using value ResNo of Node.
184 inline bool use_empty() const;
186 /// Return true if there is exactly one node using value ResNo of Node.
187 inline bool hasOneUse() const;
191 template<> struct DenseMapInfo<SDValue> {
192 static inline SDValue getEmptyKey() {
197 static inline SDValue getTombstoneKey() {
202 static unsigned getHashValue(const SDValue &Val) {
203 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
204 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
206 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
210 template <> struct isPodLike<SDValue> { static const bool value = true; };
213 /// Allow casting operators to work directly on
214 /// SDValues as if they were SDNode*'s.
215 template<> struct simplify_type<SDValue> {
216 typedef SDNode* SimpleType;
217 static SimpleType getSimplifiedValue(SDValue &Val) {
218 return Val.getNode();
221 template<> struct simplify_type<const SDValue> {
222 typedef /*const*/ SDNode* SimpleType;
223 static SimpleType getSimplifiedValue(const SDValue &Val) {
224 return Val.getNode();
228 /// Represents a use of a SDNode. This class holds an SDValue,
229 /// which records the SDNode being used and the result number, a
230 /// pointer to the SDNode using the value, and Next and Prev pointers,
231 /// which link together all the uses of an SDNode.
234 /// Val - The value being used.
236 /// User - The user of this value.
238 /// Prev, Next - Pointers to the uses list of the SDNode referred by
242 SDUse(const SDUse &U) = delete;
243 void operator=(const SDUse &U) = delete;
246 SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
248 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
249 operator const SDValue&() const { return Val; }
251 /// If implicit conversion to SDValue doesn't work, the get() method returns
253 const SDValue &get() const { return Val; }
255 /// This returns the SDNode that contains this Use.
256 SDNode *getUser() { return User; }
258 /// Get the next SDUse in the use list.
259 SDUse *getNext() const { return Next; }
261 /// Convenience function for get().getNode().
262 SDNode *getNode() const { return Val.getNode(); }
263 /// Convenience function for get().getResNo().
264 unsigned getResNo() const { return Val.getResNo(); }
265 /// Convenience function for get().getValueType().
266 EVT getValueType() const { return Val.getValueType(); }
268 /// Convenience function for get().operator==
269 bool operator==(const SDValue &V) const {
273 /// Convenience function for get().operator!=
274 bool operator!=(const SDValue &V) const {
278 /// Convenience function for get().operator<
279 bool operator<(const SDValue &V) const {
284 friend class SelectionDAG;
287 void setUser(SDNode *p) { User = p; }
289 /// Remove this use from its existing use list, assign it the
290 /// given value, and add it to the new value's node's use list.
291 inline void set(const SDValue &V);
292 /// Like set, but only supports initializing a newly-allocated
293 /// SDUse with a non-null value.
294 inline void setInitial(const SDValue &V);
295 /// Like set, but only sets the Node portion of the value,
296 /// leaving the ResNo portion unmodified.
297 inline void setNode(SDNode *N);
299 void addToList(SDUse **List) {
301 if (Next) Next->Prev = &Next;
306 void removeFromList() {
308 if (Next) Next->Prev = Prev;
312 /// simplify_type specializations - Allow casting operators to work directly on
313 /// SDValues as if they were SDNode*'s.
314 template<> struct simplify_type<SDUse> {
315 typedef SDNode* SimpleType;
316 static SimpleType getSimplifiedValue(SDUse &Val) {
317 return Val.getNode();
322 /// Represents one node in the SelectionDAG.
324 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
326 /// The operation that this node performs.
329 /// This is true if OperandList was new[]'d. If true,
330 /// then they will be delete[]'d when the node is destroyed.
331 uint16_t OperandsNeedDelete : 1;
333 /// This tracks whether this node has one or more dbg_value
334 /// nodes corresponding to it.
335 uint16_t HasDebugValue : 1;
338 /// This member is defined by this class, but is not used for
339 /// anything. Subclasses can use it to hold whatever state they find useful.
340 /// This field is initialized to zero by the ctor.
341 uint16_t SubclassData : 14;
344 /// Unique id per SDNode in the DAG.
347 /// The values that are used by this operation.
350 /// The types of the values this node defines. SDNode's may
351 /// define multiple values simultaneously.
352 const EVT *ValueList;
354 /// List of uses for this SDNode.
357 /// The number of entries in the Operand/Value list.
358 unsigned short NumOperands, NumValues;
360 /// Source line information.
363 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
364 // original LLVM instructions.
365 // This is used for turning off scheduling, because we'll forgo
366 // the normal scheduling algorithms and output the instructions according to
370 /// Return a pointer to the specified value type.
371 static const EVT *getValueTypeList(EVT VT);
373 friend class SelectionDAG;
374 friend struct ilist_traits<SDNode>;
377 //===--------------------------------------------------------------------===//
381 /// Return the SelectionDAG opcode value for this node. For
382 /// pre-isel nodes (those for which isMachineOpcode returns false), these
383 /// are the opcode values in the ISD and <target>ISD namespaces. For
384 /// post-isel opcodes, see getMachineOpcode.
385 unsigned getOpcode() const { return (unsigned short)NodeType; }
387 /// Test if this node has a target-specific opcode (in the
388 /// \<target\>ISD namespace).
389 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
391 /// Test if this node has a target-specific
392 /// memory-referencing opcode (in the \<target\>ISD namespace and
393 /// greater than FIRST_TARGET_MEMORY_OPCODE).
394 bool isTargetMemoryOpcode() const {
395 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
398 /// Test if this node is a memory intrinsic (with valid pointer information).
399 /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
400 /// non-memory intrinsics (with chains) that are not really instances of
401 /// MemSDNode. For such nodes, we need some extra state to determine the
402 /// proper classof relationship.
403 bool isMemIntrinsic() const {
404 return (NodeType == ISD::INTRINSIC_W_CHAIN ||
405 NodeType == ISD::INTRINSIC_VOID) && ((SubclassData >> 13) & 1);
408 /// Test if this node has a post-isel opcode, directly
409 /// corresponding to a MachineInstr opcode.
410 bool isMachineOpcode() const { return NodeType < 0; }
412 /// This may only be called if isMachineOpcode returns
413 /// true. It returns the MachineInstr opcode value that the node's opcode
415 unsigned getMachineOpcode() const {
416 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
421 bool getHasDebugValue() const { return HasDebugValue; }
424 void setHasDebugValue(bool b) { HasDebugValue = b; }
426 /// Return true if there are no uses of this node.
427 bool use_empty() const { return UseList == nullptr; }
429 /// Return true if there is exactly one use of this node.
430 bool hasOneUse() const {
431 return !use_empty() && std::next(use_begin()) == use_end();
434 /// Return the number of uses of this node. This method takes
435 /// time proportional to the number of uses.
436 size_t use_size() const { return std::distance(use_begin(), use_end()); }
438 /// Return the unique node id.
439 int getNodeId() const { return NodeId; }
441 /// Set unique node id.
442 void setNodeId(int Id) { NodeId = Id; }
444 /// Return the node ordering.
445 unsigned getIROrder() const { return IROrder; }
447 /// Set the node ordering.
448 void setIROrder(unsigned Order) { IROrder = Order; }
450 /// Return the source location info.
451 const DebugLoc &getDebugLoc() const { return debugLoc; }
453 /// Set source location info. Try to avoid this, putting
454 /// it in the constructor is preferable.
455 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
457 /// This class provides iterator support for SDUse
458 /// operands that use a specific SDNode.
460 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
462 explicit use_iterator(SDUse *op) : Op(op) {
466 typedef std::iterator<std::forward_iterator_tag,
467 SDUse, ptrdiff_t>::reference reference;
468 typedef std::iterator<std::forward_iterator_tag,
469 SDUse, ptrdiff_t>::pointer pointer;
471 use_iterator(const use_iterator &I) : Op(I.Op) {}
472 use_iterator() : Op(nullptr) {}
474 bool operator==(const use_iterator &x) const {
477 bool operator!=(const use_iterator &x) const {
478 return !operator==(x);
481 /// Return true if this iterator is at the end of uses list.
482 bool atEnd() const { return Op == nullptr; }
484 // Iterator traversal: forward iteration only.
485 use_iterator &operator++() { // Preincrement
486 assert(Op && "Cannot increment end iterator!");
491 use_iterator operator++(int) { // Postincrement
492 use_iterator tmp = *this; ++*this; return tmp;
495 /// Retrieve a pointer to the current user node.
496 SDNode *operator*() const {
497 assert(Op && "Cannot dereference end iterator!");
498 return Op->getUser();
501 SDNode *operator->() const { return operator*(); }
503 SDUse &getUse() const { return *Op; }
505 /// Retrieve the operand # of this use in its user.
506 unsigned getOperandNo() const {
507 assert(Op && "Cannot dereference end iterator!");
508 return (unsigned)(Op - Op->getUser()->OperandList);
512 /// Provide iteration support to walk over all uses of an SDNode.
513 use_iterator use_begin() const {
514 return use_iterator(UseList);
517 static use_iterator use_end() { return use_iterator(nullptr); }
519 inline iterator_range<use_iterator> uses() {
520 return iterator_range<use_iterator>(use_begin(), use_end());
522 inline iterator_range<use_iterator> uses() const {
523 return iterator_range<use_iterator>(use_begin(), use_end());
526 /// Return true if there are exactly NUSES uses of the indicated value.
527 /// This method ignores uses of other values defined by this operation.
528 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
530 /// Return true if there are any use of the indicated value.
531 /// This method ignores uses of other values defined by this operation.
532 bool hasAnyUseOfValue(unsigned Value) const;
534 /// Return true if this node is the only use of N.
535 bool isOnlyUserOf(SDNode *N) const;
537 /// Return true if this node is an operand of N.
538 bool isOperandOf(SDNode *N) const;
540 /// Return true if this node is a predecessor of N.
541 /// NOTE: Implemented on top of hasPredecessor and every bit as
542 /// expensive. Use carefully.
543 bool isPredecessorOf(const SDNode *N) const {
544 return N->hasPredecessor(this);
547 /// Return true if N is a predecessor of this node.
548 /// N is either an operand of this node, or can be reached by recursively
549 /// traversing up the operands.
550 /// NOTE: This is an expensive method. Use it carefully.
551 bool hasPredecessor(const SDNode *N) const;
553 /// Return true if N is a predecessor of this node.
554 /// N is either an operand of this node, or can be reached by recursively
555 /// traversing up the operands.
556 /// In this helper the Visited and worklist sets are held externally to
557 /// cache predecessors over multiple invocations. If you want to test for
558 /// multiple predecessors this method is preferable to multiple calls to
559 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
561 /// NOTE: This is still very expensive. Use carefully.
562 bool hasPredecessorHelper(const SDNode *N,
563 SmallPtrSetImpl<const SDNode *> &Visited,
564 SmallVectorImpl<const SDNode *> &Worklist) const;
566 /// Return the number of values used by this operation.
567 unsigned getNumOperands() const { return NumOperands; }
569 /// Helper method returns the integer value of a ConstantSDNode operand.
570 uint64_t getConstantOperandVal(unsigned Num) const;
572 const SDValue &getOperand(unsigned Num) const {
573 assert(Num < NumOperands && "Invalid child # of SDNode!");
574 return OperandList[Num];
577 typedef SDUse* op_iterator;
578 op_iterator op_begin() const { return OperandList; }
579 op_iterator op_end() const { return OperandList+NumOperands; }
580 ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
582 SDVTList getVTList() const {
583 SDVTList X = { ValueList, NumValues };
587 /// If this node has a glue operand, return the node
588 /// to which the glue operand points. Otherwise return NULL.
589 SDNode *getGluedNode() const {
590 if (getNumOperands() != 0 &&
591 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
592 return getOperand(getNumOperands()-1).getNode();
596 // If this is a pseudo op, like copyfromreg, look to see if there is a
597 // real target node glued to it. If so, return the target node.
598 const SDNode *getGluedMachineNode() const {
599 const SDNode *FoundNode = this;
601 // Climb up glue edges until a machine-opcode node is found, or the
602 // end of the chain is reached.
603 while (!FoundNode->isMachineOpcode()) {
604 const SDNode *N = FoundNode->getGluedNode();
612 /// If this node has a glue value with a user, return
613 /// the user (there is at most one). Otherwise return NULL.
614 SDNode *getGluedUser() const {
615 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
616 if (UI.getUse().get().getValueType() == MVT::Glue)
621 /// Return the number of values defined/returned by this operator.
622 unsigned getNumValues() const { return NumValues; }
624 /// Return the type of a specified result.
625 EVT getValueType(unsigned ResNo) const {
626 assert(ResNo < NumValues && "Illegal result number!");
627 return ValueList[ResNo];
630 /// Return the type of a specified result as a simple type.
631 MVT getSimpleValueType(unsigned ResNo) const {
632 return getValueType(ResNo).getSimpleVT();
635 /// Returns MVT::getSizeInBits(getValueType(ResNo)).
636 unsigned getValueSizeInBits(unsigned ResNo) const {
637 return getValueType(ResNo).getSizeInBits();
640 typedef const EVT* value_iterator;
641 value_iterator value_begin() const { return ValueList; }
642 value_iterator value_end() const { return ValueList+NumValues; }
644 /// Return the opcode of this operation for printing.
645 std::string getOperationName(const SelectionDAG *G = nullptr) const;
646 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
647 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
648 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
649 void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
650 void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
652 /// Print a SelectionDAG node and all children down to
653 /// the leaves. The given SelectionDAG allows target-specific nodes
654 /// to be printed in human-readable form. Unlike printr, this will
655 /// print the whole DAG, including children that appear multiple
658 void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
660 /// Print a SelectionDAG node and children up to
661 /// depth "depth." The given SelectionDAG allows target-specific
662 /// nodes to be printed in human-readable form. Unlike printr, this
663 /// will print children that appear multiple times wherever they are
666 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
667 unsigned depth = 100) const;
670 /// Dump this node, for debugging.
673 /// Dump (recursively) this node and its use-def subgraph.
676 /// Dump this node, for debugging.
677 /// The given SelectionDAG allows target-specific nodes to be printed
678 /// in human-readable form.
679 void dump(const SelectionDAG *G) const;
681 /// Dump (recursively) this node and its use-def subgraph.
682 /// The given SelectionDAG allows target-specific nodes to be printed
683 /// in human-readable form.
684 void dumpr(const SelectionDAG *G) const;
686 /// printrFull to dbgs(). The given SelectionDAG allows
687 /// target-specific nodes to be printed in human-readable form.
688 /// Unlike dumpr, this will print the whole DAG, including children
689 /// that appear multiple times.
690 void dumprFull(const SelectionDAG *G = nullptr) const;
692 /// printrWithDepth to dbgs(). The given
693 /// SelectionDAG allows target-specific nodes to be printed in
694 /// human-readable form. Unlike dumpr, this will print children
695 /// that appear multiple times wherever they are used.
697 void dumprWithDepth(const SelectionDAG *G = nullptr,
698 unsigned depth = 100) const;
700 /// Gather unique data for the node.
701 void Profile(FoldingSetNodeID &ID) const;
703 /// This method should only be used by the SDUse class.
704 void addUse(SDUse &U) { U.addToList(&UseList); }
707 static SDVTList getSDVTList(EVT VT) {
708 SDVTList Ret = { getValueTypeList(VT), 1 };
712 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
713 ArrayRef<SDValue> Ops)
714 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
715 SubclassData(0), NodeId(-1),
716 OperandList(Ops.size() ? new SDUse[Ops.size()] : nullptr),
717 ValueList(VTs.VTs), UseList(nullptr), NumOperands(Ops.size()),
718 NumValues(VTs.NumVTs), debugLoc(std::move(dl)), IROrder(Order) {
719 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
720 assert(NumOperands == Ops.size() &&
721 "NumOperands wasn't wide enough for its operands!");
722 assert(NumValues == VTs.NumVTs &&
723 "NumValues wasn't wide enough for its operands!");
724 for (unsigned i = 0; i != Ops.size(); ++i) {
725 assert(OperandList && "no operands available");
726 OperandList[i].setUser(this);
727 OperandList[i].setInitial(Ops[i]);
729 checkForCycles(this);
732 /// This constructor adds no operands itself; operands can be
733 /// set later with InitOperands.
734 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
735 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
736 SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
737 UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs),
738 debugLoc(std::move(dl)), IROrder(Order) {
739 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
740 assert(NumValues == VTs.NumVTs &&
741 "NumValues wasn't wide enough for its operands!");
744 /// Initialize the operands list of this with 1 operand.
745 void InitOperands(SDUse *Ops, const SDValue &Op0) {
746 Ops[0].setUser(this);
747 Ops[0].setInitial(Op0);
750 checkForCycles(this);
753 /// Initialize the operands list of this with 2 operands.
754 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
755 Ops[0].setUser(this);
756 Ops[0].setInitial(Op0);
757 Ops[1].setUser(this);
758 Ops[1].setInitial(Op1);
761 checkForCycles(this);
764 /// Initialize the operands list of this with 3 operands.
765 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
766 const SDValue &Op2) {
767 Ops[0].setUser(this);
768 Ops[0].setInitial(Op0);
769 Ops[1].setUser(this);
770 Ops[1].setInitial(Op1);
771 Ops[2].setUser(this);
772 Ops[2].setInitial(Op2);
775 checkForCycles(this);
778 /// Initialize the operands list of this with 4 operands.
779 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
780 const SDValue &Op2, const SDValue &Op3) {
781 Ops[0].setUser(this);
782 Ops[0].setInitial(Op0);
783 Ops[1].setUser(this);
784 Ops[1].setInitial(Op1);
785 Ops[2].setUser(this);
786 Ops[2].setInitial(Op2);
787 Ops[3].setUser(this);
788 Ops[3].setInitial(Op3);
791 checkForCycles(this);
794 /// Initialize the operands list of this with N operands.
795 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
796 for (unsigned i = 0; i != N; ++i) {
797 Ops[i].setUser(this);
798 Ops[i].setInitial(Vals[i]);
801 assert(NumOperands == N &&
802 "NumOperands wasn't wide enough for its operands!");
804 checkForCycles(this);
807 /// Release the operands and set this node to have zero operands.
811 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
812 /// into SDNode creation functions.
813 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
814 /// from the original Instruction, and IROrder is the ordinal position of
816 /// When an SDNode is created after the DAG is being built, both DebugLoc and
817 /// the IROrder are propagated from the original SDNode.
818 /// So SDLoc class provides two constructors besides the default one, one to
819 /// be used by the DAGBuilder, the other to be used by others.
822 // Ptr could be used for either Instruction* or SDNode*. It is used for
823 // Instruction* if IROrder is not -1.
828 SDLoc() : Ptr(nullptr), IROrder(0) {}
829 SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
830 assert(N && "null SDNode");
832 SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
833 assert(Ptr && "null SDNode");
835 SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
836 assert(Order >= 0 && "bad IROrder");
838 unsigned getIROrder() {
839 if (IROrder >= 0 || Ptr == nullptr) {
840 return (unsigned)IROrder;
842 const SDNode *N = (const SDNode*)(Ptr);
843 return N->getIROrder();
845 DebugLoc getDebugLoc() {
850 const Instruction *I = (const Instruction*)(Ptr);
851 return I->getDebugLoc();
853 const SDNode *N = (const SDNode*)(Ptr);
854 return N->getDebugLoc();
859 // Define inline functions from the SDValue class.
861 inline SDValue::SDValue(SDNode *node, unsigned resno)
862 : Node(node), ResNo(resno) {
863 assert((!Node || ResNo < Node->getNumValues()) &&
864 "Invalid result number for the given node!");
865 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
868 inline unsigned SDValue::getOpcode() const {
869 return Node->getOpcode();
871 inline EVT SDValue::getValueType() const {
872 return Node->getValueType(ResNo);
874 inline unsigned SDValue::getNumOperands() const {
875 return Node->getNumOperands();
877 inline const SDValue &SDValue::getOperand(unsigned i) const {
878 return Node->getOperand(i);
880 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
881 return Node->getConstantOperandVal(i);
883 inline bool SDValue::isTargetOpcode() const {
884 return Node->isTargetOpcode();
886 inline bool SDValue::isTargetMemoryOpcode() const {
887 return Node->isTargetMemoryOpcode();
889 inline bool SDValue::isMachineOpcode() const {
890 return Node->isMachineOpcode();
892 inline unsigned SDValue::getMachineOpcode() const {
893 return Node->getMachineOpcode();
895 inline bool SDValue::use_empty() const {
896 return !Node->hasAnyUseOfValue(ResNo);
898 inline bool SDValue::hasOneUse() const {
899 return Node->hasNUsesOfValue(1, ResNo);
901 inline const DebugLoc &SDValue::getDebugLoc() const {
902 return Node->getDebugLoc();
904 inline void SDValue::dump() const {
907 inline void SDValue::dumpr() const {
908 return Node->dumpr();
910 // Define inline functions from the SDUse class.
912 inline void SDUse::set(const SDValue &V) {
913 if (Val.getNode()) removeFromList();
915 if (V.getNode()) V.getNode()->addUse(*this);
918 inline void SDUse::setInitial(const SDValue &V) {
920 V.getNode()->addUse(*this);
923 inline void SDUse::setNode(SDNode *N) {
924 if (Val.getNode()) removeFromList();
926 if (N) N->addUse(*this);
929 /// These are IR-level optimization flags that may be propagated to SDNodes.
930 /// TODO: This data structure should be shared by the IR optimizer and the
934 bool NoUnsignedWrap : 1;
935 bool NoSignedWrap : 1;
937 bool UnsafeAlgebra : 1;
940 bool NoSignedZeros : 1;
941 bool AllowReciprocal : 1;
944 /// Default constructor turns off all optimization flags.
946 NoUnsignedWrap = false;
947 NoSignedWrap = false;
949 UnsafeAlgebra = false;
952 NoSignedZeros = false;
953 AllowReciprocal = false;
956 // These are mutators for each flag.
957 void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
958 void setNoSignedWrap(bool b) { NoSignedWrap = b; }
959 void setExact(bool b) { Exact = b; }
960 void setUnsafeAlgebra(bool b) { UnsafeAlgebra = b; }
961 void setNoNaNs(bool b) { NoNaNs = b; }
962 void setNoInfs(bool b) { NoInfs = b; }
963 void setNoSignedZeros(bool b) { NoSignedZeros = b; }
964 void setAllowReciprocal(bool b) { AllowReciprocal = b; }
966 // These are accessors for each flag.
967 bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
968 bool hasNoSignedWrap() const { return NoSignedWrap; }
969 bool hasExact() const { return Exact; }
970 bool hasUnsafeAlgebra() const { return UnsafeAlgebra; }
971 bool hasNoNaNs() const { return NoNaNs; }
972 bool hasNoInfs() const { return NoInfs; }
973 bool hasNoSignedZeros() const { return NoSignedZeros; }
974 bool hasAllowReciprocal() const { return AllowReciprocal; }
976 /// Return a raw encoding of the flags.
977 /// This function should only be used to add data to the NodeID value.
978 unsigned getRawFlags() const {
979 return (NoUnsignedWrap << 0) | (NoSignedWrap << 1) | (Exact << 2) |
980 (UnsafeAlgebra << 3) | (NoNaNs << 4) | (NoInfs << 5) |
981 (NoSignedZeros << 6) | (AllowReciprocal << 7);
985 /// This class is used for single-operand SDNodes. This is solely
986 /// to allow co-allocation of node operands with the node itself.
987 class UnarySDNode : public SDNode {
990 UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
992 : SDNode(Opc, Order, dl, VTs) {
993 InitOperands(&Op, X);
997 /// This class is used for two-operand SDNodes. This is solely
998 /// to allow co-allocation of node operands with the node itself.
999 class BinarySDNode : public SDNode {
1002 BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1003 SDValue X, SDValue Y)
1004 : SDNode(Opc, Order, dl, VTs) {
1005 InitOperands(Ops, X, Y);
1009 /// Returns true if the opcode is a binary operation with flags.
1010 static bool isBinOpWithFlags(unsigned Opcode) {
1031 /// This class is an extension of BinarySDNode
1032 /// used from those opcodes that have associated extra flags.
1033 class BinaryWithFlagsSDNode : public BinarySDNode {
1036 BinaryWithFlagsSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1037 SDValue X, SDValue Y, const SDNodeFlags &NodeFlags)
1038 : BinarySDNode(Opc, Order, dl, VTs, X, Y), Flags(NodeFlags) {}
1039 static bool classof(const SDNode *N) {
1040 return isBinOpWithFlags(N->getOpcode());
1044 /// This class is used for three-operand SDNodes. This is solely
1045 /// to allow co-allocation of node operands with the node itself.
1046 class TernarySDNode : public SDNode {
1049 TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1050 SDValue X, SDValue Y, SDValue Z)
1051 : SDNode(Opc, Order, dl, VTs) {
1052 InitOperands(Ops, X, Y, Z);
1057 /// This class is used to form a handle around another node that
1058 /// is persistent and is updated across invocations of replaceAllUsesWith on its
1059 /// operand. This node should be directly created by end-users and not added to
1060 /// the AllNodes list.
1061 class HandleSDNode : public SDNode {
1064 explicit HandleSDNode(SDValue X)
1065 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1066 InitOperands(&Op, X);
1069 const SDValue &getValue() const { return Op; }
1072 class AddrSpaceCastSDNode : public UnarySDNode {
1074 unsigned SrcAddrSpace;
1075 unsigned DestAddrSpace;
1078 AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
1079 unsigned SrcAS, unsigned DestAS);
1081 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1082 unsigned getDestAddressSpace() const { return DestAddrSpace; }
1084 static bool classof(const SDNode *N) {
1085 return N->getOpcode() == ISD::ADDRSPACECAST;
1089 /// This is an abstract virtual class for memory operations.
1090 class MemSDNode : public SDNode {
1092 // VT of in-memory value.
1096 /// Memory reference information.
1097 MachineMemOperand *MMO;
1100 MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1101 EVT MemoryVT, MachineMemOperand *MMO);
1103 MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1104 ArrayRef<SDValue> Ops, EVT MemoryVT, MachineMemOperand *MMO);
1106 bool readMem() const { return MMO->isLoad(); }
1107 bool writeMem() const { return MMO->isStore(); }
1109 /// Returns alignment and volatility of the memory access
1110 unsigned getOriginalAlignment() const {
1111 return MMO->getBaseAlignment();
1113 unsigned getAlignment() const {
1114 return MMO->getAlignment();
1117 /// Return the SubclassData value, which contains an
1118 /// encoding of the volatile flag, as well as bits used by subclasses. This
1119 /// function should only be used to compute a FoldingSetNodeID value.
1120 unsigned getRawSubclassData() const {
1121 return SubclassData;
1124 // We access subclass data here so that we can check consistency
1125 // with MachineMemOperand information.
1126 bool isVolatile() const { return (SubclassData >> 5) & 1; }
1127 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
1128 bool isInvariant() const { return (SubclassData >> 7) & 1; }
1130 AtomicOrdering getOrdering() const {
1131 return AtomicOrdering((SubclassData >> 8) & 15);
1133 SynchronizationScope getSynchScope() const {
1134 return SynchronizationScope((SubclassData >> 12) & 1);
1137 // Returns the offset from the location of the access.
1138 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1140 /// Returns the AA info that describes the dereference.
1141 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1143 /// Returns the Ranges that describes the dereference.
1144 const MDNode *getRanges() const { return MMO->getRanges(); }
1146 /// Return the type of the in-memory value.
1147 EVT getMemoryVT() const { return MemoryVT; }
1149 /// Return a MachineMemOperand object describing the memory
1150 /// reference performed by operation.
1151 MachineMemOperand *getMemOperand() const { return MMO; }
1153 const MachinePointerInfo &getPointerInfo() const {
1154 return MMO->getPointerInfo();
1157 /// Return the address space for the associated pointer
1158 unsigned getAddressSpace() const {
1159 return getPointerInfo().getAddrSpace();
1162 /// Update this MemSDNode's MachineMemOperand information
1163 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1164 /// This must only be used when the new alignment applies to all users of
1165 /// this MachineMemOperand.
1166 void refineAlignment(const MachineMemOperand *NewMMO) {
1167 MMO->refineAlignment(NewMMO);
1170 const SDValue &getChain() const { return getOperand(0); }
1171 const SDValue &getBasePtr() const {
1172 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1175 // Methods to support isa and dyn_cast
1176 static bool classof(const SDNode *N) {
1177 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1178 // with either an intrinsic or a target opcode.
1179 return N->getOpcode() == ISD::LOAD ||
1180 N->getOpcode() == ISD::STORE ||
1181 N->getOpcode() == ISD::PREFETCH ||
1182 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1183 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1184 N->getOpcode() == ISD::ATOMIC_SWAP ||
1185 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1186 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1187 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1188 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1189 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1190 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1191 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1192 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1193 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1194 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1195 N->getOpcode() == ISD::ATOMIC_LOAD ||
1196 N->getOpcode() == ISD::ATOMIC_STORE ||
1197 N->getOpcode() == ISD::MLOAD ||
1198 N->getOpcode() == ISD::MSTORE ||
1199 N->getOpcode() == ISD::MGATHER ||
1200 N->getOpcode() == ISD::MSCATTER ||
1201 N->isMemIntrinsic() ||
1202 N->isTargetMemoryOpcode();
1206 /// This is an SDNode representing atomic operations.
1207 class AtomicSDNode : public MemSDNode {
1210 /// For cmpxchg instructions, the ordering requirements when a store does not
1212 AtomicOrdering FailureOrdering;
1214 void InitAtomic(AtomicOrdering SuccessOrdering,
1215 AtomicOrdering FailureOrdering,
1216 SynchronizationScope SynchScope) {
1217 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1218 assert((SuccessOrdering & 15) == SuccessOrdering &&
1219 "Ordering may not require more than 4 bits!");
1220 assert((FailureOrdering & 15) == FailureOrdering &&
1221 "Ordering may not require more than 4 bits!");
1222 assert((SynchScope & 1) == SynchScope &&
1223 "SynchScope may not require more than 1 bit!");
1224 SubclassData |= SuccessOrdering << 8;
1225 SubclassData |= SynchScope << 12;
1226 this->FailureOrdering = FailureOrdering;
1227 assert(getSuccessOrdering() == SuccessOrdering &&
1228 "Ordering encoding error!");
1229 assert(getFailureOrdering() == FailureOrdering &&
1230 "Ordering encoding error!");
1231 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1235 // Opc: opcode for atomic
1236 // VTL: value type list
1237 // Chain: memory chain for operaand
1238 // Ptr: address to update as a SDValue
1239 // Cmp: compare value
1241 // SrcVal: address to update as a Value (used for MemOperand)
1242 // Align: alignment of memory
1243 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1244 EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
1245 MachineMemOperand *MMO, AtomicOrdering Ordering,
1246 SynchronizationScope SynchScope)
1247 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1248 InitAtomic(Ordering, Ordering, SynchScope);
1249 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1251 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1253 SDValue Chain, SDValue Ptr,
1254 SDValue Val, MachineMemOperand *MMO,
1255 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1256 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1257 InitAtomic(Ordering, Ordering, SynchScope);
1258 InitOperands(Ops, Chain, Ptr, Val);
1260 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1262 SDValue Chain, SDValue Ptr,
1263 MachineMemOperand *MMO,
1264 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1265 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1266 InitAtomic(Ordering, Ordering, SynchScope);
1267 InitOperands(Ops, Chain, Ptr);
1269 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1270 const SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
1271 MachineMemOperand *MMO,
1272 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1273 SynchronizationScope SynchScope)
1274 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1275 InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
1276 assert((DynOps || NumOps <= array_lengthof(Ops)) &&
1277 "Too many ops for internal storage!");
1278 InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
1281 const SDValue &getBasePtr() const { return getOperand(1); }
1282 const SDValue &getVal() const { return getOperand(2); }
1284 AtomicOrdering getSuccessOrdering() const {
1285 return getOrdering();
1288 // Not quite enough room in SubclassData for everything, so failure gets its
1290 AtomicOrdering getFailureOrdering() const {
1291 return FailureOrdering;
1294 bool isCompareAndSwap() const {
1295 unsigned Op = getOpcode();
1296 return Op == ISD::ATOMIC_CMP_SWAP || Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1299 // Methods to support isa and dyn_cast
1300 static bool classof(const SDNode *N) {
1301 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1302 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1303 N->getOpcode() == ISD::ATOMIC_SWAP ||
1304 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1305 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1306 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1307 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1308 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1309 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1310 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1311 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1312 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1313 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1314 N->getOpcode() == ISD::ATOMIC_LOAD ||
1315 N->getOpcode() == ISD::ATOMIC_STORE;
1319 /// This SDNode is used for target intrinsics that touch
1320 /// memory and need an associated MachineMemOperand. Its opcode may be
1321 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1322 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1323 class MemIntrinsicSDNode : public MemSDNode {
1325 MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1326 ArrayRef<SDValue> Ops, EVT MemoryVT,
1327 MachineMemOperand *MMO)
1328 : MemSDNode(Opc, Order, dl, VTs, Ops, MemoryVT, MMO) {
1329 SubclassData |= 1u << 13;
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 {
1353 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1354 // is freed when the SelectionDAG object is destroyed.
1357 friend class SelectionDAG;
1358 ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
1359 SDValue N2, const int *M)
1360 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
1361 InitOperands(Ops, N1, N2);
1365 ArrayRef<int> getMask() const {
1366 EVT VT = getValueType(0);
1367 return makeArrayRef(Mask, VT.getVectorNumElements());
1369 int getMaskElt(unsigned Idx) const {
1370 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1374 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1375 int getSplatIndex() const {
1376 assert(isSplat() && "Cannot get splat index for non-splat!");
1377 EVT VT = getValueType(0);
1378 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1382 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(SmallVectorImpl<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 const ConstantInt *Value;
1408 friend class SelectionDAG;
1409 // XXX: DebugLoc is unused intentionally until constant coalescing is resolved
1410 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val,
1412 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1413 0, DebugLoc(), getSDVTList(VT)), Value(val) {
1414 SubclassData |= (uint16_t)isOpaque;
1418 const ConstantInt *getConstantIntValue() const { return Value; }
1419 const APInt &getAPIntValue() const { return Value->getValue(); }
1420 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1421 int64_t getSExtValue() const { return Value->getSExtValue(); }
1423 bool isOne() const { return Value->isOne(); }
1424 bool isNullValue() const { return Value->isNullValue(); }
1425 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1427 bool isOpaque() const { return SubclassData & 1; }
1429 static bool classof(const SDNode *N) {
1430 return N->getOpcode() == ISD::Constant ||
1431 N->getOpcode() == ISD::TargetConstant;
1435 class ConstantFPSDNode : public SDNode {
1436 const ConstantFP *Value;
1437 friend class SelectionDAG;
1438 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1439 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1440 0, DebugLoc(), getSDVTList(VT)), Value(val) {
1444 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1445 const ConstantFP *getConstantFPValue() const { return Value; }
1447 /// Return true if the value is positive or negative zero.
1448 bool isZero() const { return Value->isZero(); }
1450 /// Return true if the value is a NaN.
1451 bool isNaN() const { return Value->isNaN(); }
1453 /// Return true if the value is an infinity
1454 bool isInfinity() const { return Value->isInfinity(); }
1456 /// Return true if the value is negative.
1457 bool isNegative() const { return Value->isNegative(); }
1459 /// We don't rely on operator== working on double values, as
1460 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1461 /// As such, this method can be used to do an exact bit-for-bit comparison of
1462 /// two floating point values.
1464 /// We leave the version with the double argument here because it's just so
1465 /// convenient to write "2.0" and the like. Without this function we'd
1466 /// have to duplicate its logic everywhere it's called.
1467 bool isExactlyValue(double V) const {
1470 Tmp.convert(Value->getValueAPF().getSemantics(),
1471 APFloat::rmNearestTiesToEven, &ignored);
1472 return isExactlyValue(Tmp);
1474 bool isExactlyValue(const APFloat& V) const;
1476 static bool isValueValidForType(EVT VT, const APFloat& Val);
1478 static bool classof(const SDNode *N) {
1479 return N->getOpcode() == ISD::ConstantFP ||
1480 N->getOpcode() == ISD::TargetConstantFP;
1484 class GlobalAddressSDNode : public SDNode {
1485 const GlobalValue *TheGlobal;
1487 unsigned char TargetFlags;
1488 friend class SelectionDAG;
1489 GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
1490 const GlobalValue *GA, EVT VT, int64_t o,
1491 unsigned char TargetFlags);
1494 const GlobalValue *getGlobal() const { return TheGlobal; }
1495 int64_t getOffset() const { return Offset; }
1496 unsigned char getTargetFlags() const { return TargetFlags; }
1497 // Return the address space this GlobalAddress belongs to.
1498 unsigned getAddressSpace() const;
1500 static bool classof(const SDNode *N) {
1501 return N->getOpcode() == ISD::GlobalAddress ||
1502 N->getOpcode() == ISD::TargetGlobalAddress ||
1503 N->getOpcode() == ISD::GlobalTLSAddress ||
1504 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1508 class FrameIndexSDNode : public SDNode {
1510 friend class SelectionDAG;
1511 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1512 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1513 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1517 int getIndex() const { return FI; }
1519 static bool classof(const SDNode *N) {
1520 return N->getOpcode() == ISD::FrameIndex ||
1521 N->getOpcode() == ISD::TargetFrameIndex;
1525 class JumpTableSDNode : public SDNode {
1527 unsigned char TargetFlags;
1528 friend class SelectionDAG;
1529 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1530 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1531 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1535 int getIndex() const { return JTI; }
1536 unsigned char getTargetFlags() const { return TargetFlags; }
1538 static bool classof(const SDNode *N) {
1539 return N->getOpcode() == ISD::JumpTable ||
1540 N->getOpcode() == ISD::TargetJumpTable;
1544 class ConstantPoolSDNode : public SDNode {
1546 const Constant *ConstVal;
1547 MachineConstantPoolValue *MachineCPVal;
1549 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1550 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1551 unsigned char TargetFlags;
1552 friend class SelectionDAG;
1553 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1554 unsigned Align, unsigned char TF)
1555 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1556 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1558 assert(Offset >= 0 && "Offset is too large");
1561 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1562 EVT VT, int o, unsigned Align, unsigned char TF)
1563 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1564 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1566 assert(Offset >= 0 && "Offset is too large");
1567 Val.MachineCPVal = v;
1568 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1572 bool isMachineConstantPoolEntry() const {
1576 const Constant *getConstVal() const {
1577 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1578 return Val.ConstVal;
1581 MachineConstantPoolValue *getMachineCPVal() const {
1582 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1583 return Val.MachineCPVal;
1586 int getOffset() const {
1587 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1590 // Return the alignment of this constant pool object, which is either 0 (for
1591 // default alignment) or the desired value.
1592 unsigned getAlignment() const { return Alignment; }
1593 unsigned char getTargetFlags() const { return TargetFlags; }
1595 Type *getType() const;
1597 static bool classof(const SDNode *N) {
1598 return N->getOpcode() == ISD::ConstantPool ||
1599 N->getOpcode() == ISD::TargetConstantPool;
1603 /// Completely target-dependent object reference.
1604 class TargetIndexSDNode : public SDNode {
1605 unsigned char TargetFlags;
1608 friend class SelectionDAG;
1611 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1612 : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1613 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1616 unsigned char getTargetFlags() const { return TargetFlags; }
1617 int getIndex() const { return Index; }
1618 int64_t getOffset() const { return Offset; }
1620 static bool classof(const SDNode *N) {
1621 return N->getOpcode() == ISD::TargetIndex;
1625 class BasicBlockSDNode : public SDNode {
1626 MachineBasicBlock *MBB;
1627 friend class SelectionDAG;
1628 /// Debug info is meaningful and potentially useful here, but we create
1629 /// blocks out of order when they're jumped to, which makes it a bit
1630 /// harder. Let's see if we need it first.
1631 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1632 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1636 MachineBasicBlock *getBasicBlock() const { return MBB; }
1638 static bool classof(const SDNode *N) {
1639 return N->getOpcode() == ISD::BasicBlock;
1643 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1644 class BuildVectorSDNode : public SDNode {
1645 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1646 explicit BuildVectorSDNode() = delete;
1648 /// Check if this is a constant splat, and if so, find the
1649 /// smallest element size that splats the vector. If MinSplatBits is
1650 /// nonzero, the element size must be at least that large. Note that the
1651 /// splat element may be the entire vector (i.e., a one element vector).
1652 /// Returns the splat element value in SplatValue. Any undefined bits in
1653 /// that value are zero, and the corresponding bits in the SplatUndef mask
1654 /// are set. The SplatBitSize value is set to the splat element size in
1655 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1656 /// undefined. isBigEndian describes the endianness of the target.
1657 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1658 unsigned &SplatBitSize, bool &HasAnyUndefs,
1659 unsigned MinSplatBits = 0,
1660 bool isBigEndian = false) const;
1662 /// \brief Returns the splatted value or a null value if this is not a splat.
1664 /// If passed a non-null UndefElements bitvector, it will resize it to match
1665 /// the vector width and set the bits where elements are undef.
1666 SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1668 /// \brief Returns the splatted constant or null if this is not a constant
1671 /// If passed a non-null UndefElements bitvector, it will resize it to match
1672 /// the vector width and set the bits where elements are undef.
1674 getConstantSplatNode(BitVector *UndefElements = nullptr) const;
1676 /// \brief Returns the splatted constant FP or null if this is not a constant
1679 /// If passed a non-null UndefElements bitvector, it will resize it to match
1680 /// the vector width and set the bits where elements are undef.
1682 getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
1684 bool isConstant() const;
1686 static inline bool classof(const SDNode *N) {
1687 return N->getOpcode() == ISD::BUILD_VECTOR;
1691 /// An SDNode that holds an arbitrary LLVM IR Value. This is
1692 /// used when the SelectionDAG needs to make a simple reference to something
1693 /// in the LLVM IR representation.
1695 class SrcValueSDNode : public SDNode {
1697 friend class SelectionDAG;
1698 /// Create a SrcValue for a general value.
1699 explicit SrcValueSDNode(const Value *v)
1700 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1703 /// Return the contained Value.
1704 const Value *getValue() const { return V; }
1706 static bool classof(const SDNode *N) {
1707 return N->getOpcode() == ISD::SRCVALUE;
1711 class MDNodeSDNode : public SDNode {
1713 friend class SelectionDAG;
1714 explicit MDNodeSDNode(const MDNode *md)
1715 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1719 const MDNode *getMD() const { return MD; }
1721 static bool classof(const SDNode *N) {
1722 return N->getOpcode() == ISD::MDNODE_SDNODE;
1726 class RegisterSDNode : public SDNode {
1728 friend class SelectionDAG;
1729 RegisterSDNode(unsigned reg, EVT VT)
1730 : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1734 unsigned getReg() const { return Reg; }
1736 static bool classof(const SDNode *N) {
1737 return N->getOpcode() == ISD::Register;
1741 class RegisterMaskSDNode : public SDNode {
1742 // The memory for RegMask is not owned by the node.
1743 const uint32_t *RegMask;
1744 friend class SelectionDAG;
1745 RegisterMaskSDNode(const uint32_t *mask)
1746 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1750 const uint32_t *getRegMask() const { return RegMask; }
1752 static bool classof(const SDNode *N) {
1753 return N->getOpcode() == ISD::RegisterMask;
1757 class BlockAddressSDNode : public SDNode {
1758 const BlockAddress *BA;
1760 unsigned char TargetFlags;
1761 friend class SelectionDAG;
1762 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1763 int64_t o, unsigned char Flags)
1764 : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1765 BA(ba), Offset(o), TargetFlags(Flags) {
1768 const BlockAddress *getBlockAddress() const { return BA; }
1769 int64_t getOffset() const { return Offset; }
1770 unsigned char getTargetFlags() const { return TargetFlags; }
1772 static bool classof(const SDNode *N) {
1773 return N->getOpcode() == ISD::BlockAddress ||
1774 N->getOpcode() == ISD::TargetBlockAddress;
1778 class EHLabelSDNode : public SDNode {
1781 friend class SelectionDAG;
1782 EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
1783 : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
1784 InitOperands(&Chain, ch);
1787 MCSymbol *getLabel() const { return Label; }
1789 static bool classof(const SDNode *N) {
1790 return N->getOpcode() == ISD::EH_LABEL;
1794 class ExternalSymbolSDNode : public SDNode {
1796 unsigned char TargetFlags;
1798 friend class SelectionDAG;
1799 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1800 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1801 0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1805 const char *getSymbol() const { return Symbol; }
1806 unsigned char getTargetFlags() const { return TargetFlags; }
1808 static bool classof(const SDNode *N) {
1809 return N->getOpcode() == ISD::ExternalSymbol ||
1810 N->getOpcode() == ISD::TargetExternalSymbol;
1814 class CondCodeSDNode : public SDNode {
1815 ISD::CondCode Condition;
1816 friend class SelectionDAG;
1817 explicit CondCodeSDNode(ISD::CondCode Cond)
1818 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1823 ISD::CondCode get() const { return Condition; }
1825 static bool classof(const SDNode *N) {
1826 return N->getOpcode() == ISD::CONDCODE;
1830 /// NOTE: avoid using this node as this may disappear in the
1831 /// future and most targets don't support it.
1832 class CvtRndSatSDNode : public SDNode {
1833 ISD::CvtCode CvtCode;
1834 friend class SelectionDAG;
1835 explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
1836 ArrayRef<SDValue> Ops, ISD::CvtCode Code)
1837 : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops),
1839 assert(Ops.size() == 5 && "wrong number of operations");
1842 ISD::CvtCode getCvtCode() const { return CvtCode; }
1844 static bool classof(const SDNode *N) {
1845 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1849 /// This class is used to represent EVT's, which are used
1850 /// to parameterize some operations.
1851 class VTSDNode : public SDNode {
1853 friend class SelectionDAG;
1854 explicit VTSDNode(EVT VT)
1855 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1860 EVT getVT() const { return ValueType; }
1862 static bool classof(const SDNode *N) {
1863 return N->getOpcode() == ISD::VALUETYPE;
1867 /// Base class for LoadSDNode and StoreSDNode
1868 class LSBaseSDNode : public MemSDNode {
1869 //! Operand array for load and store
1871 \note Moving this array to the base class captures more
1872 common functionality shared between LoadSDNode and
1877 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1878 SDValue *Operands, unsigned numOperands,
1879 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1880 MachineMemOperand *MMO)
1881 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1882 SubclassData |= AM << 2;
1883 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1884 InitOperands(Ops, Operands, numOperands);
1885 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1886 "Only indexed loads and stores have a non-undef offset operand");
1889 const SDValue &getOffset() const {
1890 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1893 /// Return the addressing mode for this load or store:
1894 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1895 ISD::MemIndexedMode getAddressingMode() const {
1896 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1899 /// Return true if this is a pre/post inc/dec load/store.
1900 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1902 /// Return true if this is NOT a pre/post inc/dec load/store.
1903 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1905 static bool classof(const SDNode *N) {
1906 return N->getOpcode() == ISD::LOAD ||
1907 N->getOpcode() == ISD::STORE;
1911 /// This class is used to represent ISD::LOAD nodes.
1912 class LoadSDNode : public LSBaseSDNode {
1913 friend class SelectionDAG;
1914 LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1915 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1916 MachineMemOperand *MMO)
1917 : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1918 SubclassData |= (unsigned short)ETy;
1919 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1920 assert(readMem() && "Load MachineMemOperand is not a load!");
1921 assert(!writeMem() && "Load MachineMemOperand is a store!");
1925 /// Return whether this is a plain node,
1926 /// or one of the varieties of value-extending loads.
1927 ISD::LoadExtType getExtensionType() const {
1928 return ISD::LoadExtType(SubclassData & 3);
1931 const SDValue &getBasePtr() const { return getOperand(1); }
1932 const SDValue &getOffset() const { return getOperand(2); }
1934 static bool classof(const SDNode *N) {
1935 return N->getOpcode() == ISD::LOAD;
1939 /// This class is used to represent ISD::STORE nodes.
1940 class StoreSDNode : public LSBaseSDNode {
1941 friend class SelectionDAG;
1942 StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1943 SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1944 MachineMemOperand *MMO)
1945 : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1946 VTs, AM, MemVT, MMO) {
1947 SubclassData |= (unsigned short)isTrunc;
1948 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1949 assert(!readMem() && "Store MachineMemOperand is a load!");
1950 assert(writeMem() && "Store MachineMemOperand is not a store!");
1954 /// Return true if the op does a truncation before store.
1955 /// For integers this is the same as doing a TRUNCATE and storing the result.
1956 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1957 bool isTruncatingStore() const { return SubclassData & 1; }
1959 const SDValue &getValue() const { return getOperand(1); }
1960 const SDValue &getBasePtr() const { return getOperand(2); }
1961 const SDValue &getOffset() const { return getOperand(3); }
1963 static bool classof(const SDNode *N) {
1964 return N->getOpcode() == ISD::STORE;
1968 /// This base class is used to represent MLOAD and MSTORE nodes
1969 class MaskedLoadStoreSDNode : public MemSDNode {
1973 friend class SelectionDAG;
1974 MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1975 SDValue *Operands, unsigned numOperands,
1976 SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
1977 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1978 InitOperands(Ops, Operands, numOperands);
1981 // In the both nodes address is Op1, mask is Op2:
1982 // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
1983 // MaskedStoreSDNode (Chain, ptr, mask, data)
1984 // Mask is a vector of i1 elements
1985 const SDValue &getBasePtr() const { return getOperand(1); }
1986 const SDValue &getMask() const { return getOperand(2); }
1988 static bool classof(const SDNode *N) {
1989 return N->getOpcode() == ISD::MLOAD ||
1990 N->getOpcode() == ISD::MSTORE;
1994 /// This class is used to represent an MLOAD node
1995 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
1997 friend class SelectionDAG;
1998 MaskedLoadSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
1999 unsigned numOperands, SDVTList VTs, ISD::LoadExtType ETy,
2000 EVT MemVT, MachineMemOperand *MMO)
2001 : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, Operands, numOperands,
2003 SubclassData |= (unsigned short)ETy;
2006 ISD::LoadExtType getExtensionType() const {
2007 return ISD::LoadExtType(SubclassData & 3);
2009 const SDValue &getSrc0() const { return getOperand(3); }
2010 static bool classof(const SDNode *N) {
2011 return N->getOpcode() == ISD::MLOAD;
2015 /// This class is used to represent an MSTORE node
2016 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2019 friend class SelectionDAG;
2020 MaskedStoreSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
2021 unsigned numOperands, SDVTList VTs, bool isTrunc, EVT MemVT,
2022 MachineMemOperand *MMO)
2023 : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, Operands, numOperands,
2025 SubclassData |= (unsigned short)isTrunc;
2027 /// Return true if the op does a truncation before store.
2028 /// For integers this is the same as doing a TRUNCATE and storing the result.
2029 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2030 bool isTruncatingStore() const { return SubclassData & 1; }
2032 const SDValue &getValue() const { return getOperand(3); }
2034 static bool classof(const SDNode *N) {
2035 return N->getOpcode() == ISD::MSTORE;
2039 /// This is a base class used to represent
2040 /// MGATHER and MSCATTER nodes
2042 class MaskedGatherScatterSDNode : public MemSDNode {
2046 friend class SelectionDAG;
2047 MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
2048 ArrayRef<SDValue> Operands, SDVTList VTs, EVT MemVT,
2049 MachineMemOperand *MMO)
2050 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2051 assert(Operands.size() == 5 && "Incompatible number of operands");
2052 InitOperands(Ops, Operands.data(), Operands.size());
2055 // In the both nodes address is Op1, mask is Op2:
2056 // MaskedGatherSDNode (Chain, src0, mask, base, index), src0 is a passthru value
2057 // MaskedScatterSDNode (Chain, value, mask, base, index)
2058 // Mask is a vector of i1 elements
2059 const SDValue &getBasePtr() const { return getOperand(3); }
2060 const SDValue &getIndex() const { return getOperand(4); }
2061 const SDValue &getMask() const { return getOperand(2); }
2062 const SDValue &getValue() const { return getOperand(1); }
2064 static bool classof(const SDNode *N) {
2065 return N->getOpcode() == ISD::MGATHER ||
2066 N->getOpcode() == ISD::MSCATTER;
2070 /// This class is used to represent an MGATHER node
2072 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2074 friend class SelectionDAG;
2075 MaskedGatherSDNode(unsigned Order, DebugLoc dl, ArrayRef<SDValue> Operands,
2076 SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
2077 : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, Operands, VTs, MemVT,
2079 assert(getValue().getValueType() == getValueType(0) &&
2080 "Incompatible type of the PathThru value in MaskedGatherSDNode");
2081 assert(getMask().getValueType().getVectorNumElements() ==
2082 getValueType(0).getVectorNumElements() &&
2083 "Vector width mismatch between mask and data");
2084 assert(getMask().getValueType().getScalarType() == MVT::i1 &&
2085 "Vector width mismatch between mask and data");
2088 static bool classof(const SDNode *N) {
2089 return N->getOpcode() == ISD::MGATHER;
2093 /// This class is used to represent an MSCATTER node
2095 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2098 friend class SelectionDAG;
2099 MaskedScatterSDNode(unsigned Order, DebugLoc dl,ArrayRef<SDValue> Operands,
2100 SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
2101 : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, Operands, VTs, MemVT,
2103 assert(getMask().getValueType().getVectorNumElements() ==
2104 getValue().getValueType().getVectorNumElements() &&
2105 "Vector width mismatch between mask and data");
2106 assert(getMask().getValueType().getScalarType() == MVT::i1 &&
2107 "Vector width mismatch between mask and data");
2110 static bool classof(const SDNode *N) {
2111 return N->getOpcode() == ISD::MSCATTER;
2115 /// An SDNode that represents everything that will be needed
2116 /// to construct a MachineInstr. These nodes are created during the
2117 /// instruction selection proper phase.
2118 class MachineSDNode : public SDNode {
2120 typedef MachineMemOperand **mmo_iterator;
2123 friend class SelectionDAG;
2124 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
2125 : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
2127 /// Operands for this instruction, if they fit here. If
2128 /// they don't, this field is unused.
2129 SDUse LocalOperands[4];
2131 /// Memory reference descriptions for this instruction.
2132 mmo_iterator MemRefs;
2133 mmo_iterator MemRefsEnd;
2136 mmo_iterator memoperands_begin() const { return MemRefs; }
2137 mmo_iterator memoperands_end() const { return MemRefsEnd; }
2138 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
2140 /// Assign this MachineSDNodes's memory reference descriptor
2141 /// list. This does not transfer ownership.
2142 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
2143 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
2144 assert(*MMI && "Null mem ref detected!");
2145 MemRefs = NewMemRefs;
2146 MemRefsEnd = NewMemRefsEnd;
2149 static bool classof(const SDNode *N) {
2150 return N->isMachineOpcode();
2154 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
2155 SDNode, ptrdiff_t> {
2159 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2161 bool operator==(const SDNodeIterator& x) const {
2162 return Operand == x.Operand;
2164 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2166 pointer operator*() const {
2167 return Node->getOperand(Operand).getNode();
2169 pointer operator->() const { return operator*(); }
2171 SDNodeIterator& operator++() { // Preincrement
2175 SDNodeIterator operator++(int) { // Postincrement
2176 SDNodeIterator tmp = *this; ++*this; return tmp;
2178 size_t operator-(SDNodeIterator Other) const {
2179 assert(Node == Other.Node &&
2180 "Cannot compare iterators of two different nodes!");
2181 return Operand - Other.Operand;
2184 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2185 static SDNodeIterator end (const SDNode *N) {
2186 return SDNodeIterator(N, N->getNumOperands());
2189 unsigned getOperand() const { return Operand; }
2190 const SDNode *getNode() const { return Node; }
2193 template <> struct GraphTraits<SDNode*> {
2194 typedef SDNode NodeType;
2195 typedef SDNodeIterator ChildIteratorType;
2196 static inline NodeType *getEntryNode(SDNode *N) { return N; }
2197 static inline ChildIteratorType child_begin(NodeType *N) {
2198 return SDNodeIterator::begin(N);
2200 static inline ChildIteratorType child_end(NodeType *N) {
2201 return SDNodeIterator::end(N);
2205 /// The largest SDNode class.
2206 typedef MaskedGatherScatterSDNode LargestSDNode;
2208 /// The SDNode class with the greatest alignment requirement.
2209 typedef GlobalAddressSDNode MostAlignedSDNode;
2212 /// Returns true if the specified node is a non-extending and unindexed load.
2213 inline bool isNormalLoad(const SDNode *N) {
2214 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2215 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2216 Ld->getAddressingMode() == ISD::UNINDEXED;
2219 /// Returns true if the specified node is a non-extending load.
2220 inline bool isNON_EXTLoad(const SDNode *N) {
2221 return isa<LoadSDNode>(N) &&
2222 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2225 /// Returns true if the specified node is a EXTLOAD.
2226 inline bool isEXTLoad(const SDNode *N) {
2227 return isa<LoadSDNode>(N) &&
2228 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2231 /// Returns true if the specified node is a SEXTLOAD.
2232 inline bool isSEXTLoad(const SDNode *N) {
2233 return isa<LoadSDNode>(N) &&
2234 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2237 /// Returns true if the specified node is a ZEXTLOAD.
2238 inline bool isZEXTLoad(const SDNode *N) {
2239 return isa<LoadSDNode>(N) &&
2240 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2243 /// Returns true if the specified node is an unindexed load.
2244 inline bool isUNINDEXEDLoad(const SDNode *N) {
2245 return isa<LoadSDNode>(N) &&
2246 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2249 /// Returns true if the specified node is a non-truncating
2250 /// and unindexed store.
2251 inline bool isNormalStore(const SDNode *N) {
2252 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2253 return St && !St->isTruncatingStore() &&
2254 St->getAddressingMode() == ISD::UNINDEXED;
2257 /// Returns true if the specified node is a non-truncating store.
2258 inline bool isNON_TRUNCStore(const SDNode *N) {
2259 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2262 /// Returns true if the specified node is a truncating store.
2263 inline bool isTRUNCStore(const SDNode *N) {
2264 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2267 /// Returns true if the specified node is an unindexed store.
2268 inline bool isUNINDEXEDStore(const SDNode *N) {
2269 return isa<StoreSDNode>(N) &&
2270 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2274 } // end llvm namespace