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/FoldingSet.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/ilist_node.h"
28 #include "llvm/CodeGen/ISDOpcodes.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/Constants.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/DebugLoc.h"
35 #include "llvm/Support/MathExtras.h"
42 class MachineBasicBlock;
43 class MachineConstantPoolValue;
47 template <typename T> struct DenseMapInfo;
48 template <typename T> struct simplify_type;
49 template <typename T> struct ilist_traits;
51 void checkForCycles(const SDNode *N);
53 /// SDVTList - This represents a list of ValueType's that has been intern'd by
54 /// a SelectionDAG. Instances of this simple value class are returned by
55 /// SelectionDAG::getVTList(...).
65 /// isBuildVectorAllOnes - Return true if the specified node is a
66 /// BUILD_VECTOR where all of the elements are ~0 or undef.
67 bool isBuildVectorAllOnes(const SDNode *N);
69 /// isBuildVectorAllZeros - Return true if the specified node is a
70 /// BUILD_VECTOR where all of the elements are 0 or undef.
71 bool isBuildVectorAllZeros(const SDNode *N);
73 /// isScalarToVector - Return true if the specified node is a
74 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
75 /// element is not an undef.
76 bool isScalarToVector(const SDNode *N);
78 /// allOperandsUndef - Return true if the node has at least one operand
79 /// and all operands of the specified node are ISD::UNDEF.
80 bool allOperandsUndef(const SDNode *N);
81 } // end llvm:ISD namespace
83 //===----------------------------------------------------------------------===//
84 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
85 /// values as the result of a computation. Many nodes return multiple values,
86 /// from loads (which define a token and a return value) to ADDC (which returns
87 /// a result and a carry value), to calls (which may return an arbitrary number
90 /// As such, each use of a SelectionDAG computation must indicate the node that
91 /// computes it as well as which return value to use from that node. This pair
92 /// of information is represented with the SDValue value type.
95 SDNode *Node; // The node defining the value we are using.
96 unsigned ResNo; // Which return value of the node we are using.
98 SDValue() : Node(0), ResNo(0) {}
99 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
101 /// get the index which selects a specific result in the SDNode
102 unsigned getResNo() const { return ResNo; }
104 /// get the SDNode which holds the desired result
105 SDNode *getNode() const { return Node; }
108 void setNode(SDNode *N) { Node = N; }
110 inline SDNode *operator->() const { return Node; }
112 bool operator==(const SDValue &O) const {
113 return Node == O.Node && ResNo == O.ResNo;
115 bool operator!=(const SDValue &O) const {
116 return !operator==(O);
118 bool operator<(const SDValue &O) const {
119 return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
122 SDValue getValue(unsigned R) const {
123 return SDValue(Node, R);
126 // isOperandOf - Return true if this node is an operand of N.
127 bool isOperandOf(SDNode *N) const;
129 /// getValueType - Return the ValueType of the referenced return value.
131 inline EVT getValueType() const;
133 /// Return the simple ValueType of the referenced return value.
134 MVT getSimpleValueType() const {
135 return getValueType().getSimpleVT();
138 /// getValueSizeInBits - Returns the size of the value in bits.
140 unsigned getValueSizeInBits() const {
141 return getValueType().getSizeInBits();
144 // Forwarding methods - These forward to the corresponding methods in SDNode.
145 inline unsigned getOpcode() const;
146 inline unsigned getNumOperands() const;
147 inline const SDValue &getOperand(unsigned i) const;
148 inline uint64_t getConstantOperandVal(unsigned i) const;
149 inline bool isTargetMemoryOpcode() const;
150 inline bool isTargetOpcode() const;
151 inline bool isMachineOpcode() const;
152 inline unsigned getMachineOpcode() const;
153 inline const DebugLoc getDebugLoc() const;
154 inline void dump() const;
155 inline void dumpr() const;
157 /// reachesChainWithoutSideEffects - Return true if this operand (which must
158 /// be a chain) reaches the specified operand without crossing any
159 /// side-effecting instructions. In practice, this looks through token
160 /// factors and non-volatile loads. In order to remain efficient, this only
161 /// looks a couple of nodes in, it does not do an exhaustive search.
162 bool reachesChainWithoutSideEffects(SDValue Dest,
163 unsigned Depth = 2) const;
165 /// use_empty - Return true if there are no nodes using value ResNo
168 inline bool use_empty() const;
170 /// hasOneUse - Return true if there is exactly one node using value
173 inline bool hasOneUse() const;
177 template<> struct DenseMapInfo<SDValue> {
178 static inline SDValue getEmptyKey() {
179 return SDValue((SDNode*)-1, -1U);
181 static inline SDValue getTombstoneKey() {
182 return SDValue((SDNode*)-1, 0);
184 static unsigned getHashValue(const SDValue &Val) {
185 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
186 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
188 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
192 template <> struct isPodLike<SDValue> { static const bool value = true; };
195 /// simplify_type specializations - Allow casting operators to work directly on
196 /// SDValues as if they were SDNode*'s.
197 template<> struct simplify_type<SDValue> {
198 typedef SDNode* SimpleType;
199 static SimpleType getSimplifiedValue(const SDValue &Val) {
200 return static_cast<SimpleType>(Val.getNode());
203 template<> struct simplify_type<const SDValue> {
204 typedef SDNode* SimpleType;
205 static SimpleType getSimplifiedValue(const SDValue &Val) {
206 return static_cast<SimpleType>(Val.getNode());
210 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
211 /// which records the SDNode being used and the result number, a
212 /// pointer to the SDNode using the value, and Next and Prev pointers,
213 /// which link together all the uses of an SDNode.
216 /// Val - The value being used.
218 /// User - The user of this value.
220 /// Prev, Next - Pointers to the uses list of the SDNode referred by
224 SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
225 void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
228 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
230 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
231 operator const SDValue&() const { return Val; }
233 /// If implicit conversion to SDValue doesn't work, the get() method returns
235 const SDValue &get() const { return Val; }
237 /// getUser - This returns the SDNode that contains this Use.
238 SDNode *getUser() { return User; }
240 /// getNext - Get the next SDUse in the use list.
241 SDUse *getNext() const { return Next; }
243 /// getNode - Convenience function for get().getNode().
244 SDNode *getNode() const { return Val.getNode(); }
245 /// getResNo - Convenience function for get().getResNo().
246 unsigned getResNo() const { return Val.getResNo(); }
247 /// getValueType - Convenience function for get().getValueType().
248 EVT getValueType() const { return Val.getValueType(); }
250 /// operator== - Convenience function for get().operator==
251 bool operator==(const SDValue &V) const {
255 /// operator!= - Convenience function for get().operator!=
256 bool operator!=(const SDValue &V) const {
260 /// operator< - Convenience function for get().operator<
261 bool operator<(const SDValue &V) const {
266 friend class SelectionDAG;
269 void setUser(SDNode *p) { User = p; }
271 /// set - Remove this use from its existing use list, assign it the
272 /// given value, and add it to the new value's node's use list.
273 inline void set(const SDValue &V);
274 /// setInitial - like set, but only supports initializing a newly-allocated
275 /// SDUse with a non-null value.
276 inline void setInitial(const SDValue &V);
277 /// setNode - like set, but only sets the Node portion of the value,
278 /// leaving the ResNo portion unmodified.
279 inline void setNode(SDNode *N);
281 void addToList(SDUse **List) {
283 if (Next) Next->Prev = &Next;
288 void removeFromList() {
290 if (Next) Next->Prev = Prev;
294 /// simplify_type specializations - Allow casting operators to work directly on
295 /// SDValues as if they were SDNode*'s.
296 template<> struct simplify_type<SDUse> {
297 typedef SDNode* SimpleType;
298 static SimpleType getSimplifiedValue(const SDUse &Val) {
299 return static_cast<SimpleType>(Val.getNode());
302 template<> struct simplify_type<const SDUse> {
303 typedef SDNode* SimpleType;
304 static SimpleType getSimplifiedValue(const SDUse &Val) {
305 return static_cast<SimpleType>(Val.getNode());
310 /// SDNode - Represents one node in the SelectionDAG.
312 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
314 /// NodeType - The operation that this node performs.
318 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
319 /// then they will be delete[]'d when the node is destroyed.
320 uint16_t OperandsNeedDelete : 1;
322 /// HasDebugValue - This tracks whether this node has one or more dbg_value
323 /// nodes corresponding to it.
324 uint16_t HasDebugValue : 1;
327 /// SubclassData - This member is defined by this class, but is not used for
328 /// anything. Subclasses can use it to hold whatever state they find useful.
329 /// This field is initialized to zero by the ctor.
330 uint16_t SubclassData : 14;
333 /// NodeId - Unique id per SDNode in the DAG.
336 /// OperandList - The values that are used by this operation.
340 /// ValueList - The types of the values this node defines. SDNode's may
341 /// define multiple values simultaneously.
342 const EVT *ValueList;
344 /// UseList - List of uses for this SDNode.
347 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
348 unsigned short NumOperands, NumValues;
350 /// debugLoc - source line information.
353 /// getValueTypeList - Return a pointer to the specified value type.
354 static const EVT *getValueTypeList(EVT VT);
356 friend class SelectionDAG;
357 friend struct ilist_traits<SDNode>;
360 //===--------------------------------------------------------------------===//
364 /// getOpcode - Return the SelectionDAG opcode value for this node. For
365 /// pre-isel nodes (those for which isMachineOpcode returns false), these
366 /// are the opcode values in the ISD and <target>ISD namespaces. For
367 /// post-isel opcodes, see getMachineOpcode.
368 unsigned getOpcode() const { return (unsigned short)NodeType; }
370 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
371 /// \<target\>ISD namespace).
372 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
374 /// isTargetMemoryOpcode - Test if this node has a target-specific
375 /// memory-referencing opcode (in the \<target\>ISD namespace and
376 /// greater than FIRST_TARGET_MEMORY_OPCODE).
377 bool isTargetMemoryOpcode() const {
378 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
381 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
382 /// corresponding to a MachineInstr opcode.
383 bool isMachineOpcode() const { return NodeType < 0; }
385 /// getMachineOpcode - This may only be called if isMachineOpcode returns
386 /// true. It returns the MachineInstr opcode value that the node's opcode
388 unsigned getMachineOpcode() const {
389 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
393 /// getHasDebugValue - get this bit.
394 bool getHasDebugValue() const { return HasDebugValue; }
396 /// setHasDebugValue - set this bit.
397 void setHasDebugValue(bool b) { HasDebugValue = b; }
399 /// use_empty - Return true if there are no uses of this node.
401 bool use_empty() const { return UseList == NULL; }
403 /// hasOneUse - Return true if there is exactly one use of this node.
405 bool hasOneUse() const {
406 return !use_empty() && llvm::next(use_begin()) == use_end();
409 /// use_size - Return the number of uses of this node. This method takes
410 /// time proportional to the number of uses.
412 size_t use_size() const { return std::distance(use_begin(), use_end()); }
414 /// getNodeId - Return the unique node id.
416 int getNodeId() const { return NodeId; }
418 /// setNodeId - Set unique node id.
419 void setNodeId(int Id) { NodeId = Id; }
421 /// getDebugLoc - Return the source location info.
422 const DebugLoc getDebugLoc() const { return debugLoc; }
424 /// setDebugLoc - Set source location info. Try to avoid this, putting
425 /// it in the constructor is preferable.
426 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
428 /// use_iterator - This class provides iterator support for SDUse
429 /// operands that use a specific SDNode.
431 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
433 explicit use_iterator(SDUse *op) : Op(op) {
437 typedef std::iterator<std::forward_iterator_tag,
438 SDUse, ptrdiff_t>::reference reference;
439 typedef std::iterator<std::forward_iterator_tag,
440 SDUse, ptrdiff_t>::pointer pointer;
442 use_iterator(const use_iterator &I) : Op(I.Op) {}
443 use_iterator() : Op(0) {}
445 bool operator==(const use_iterator &x) const {
448 bool operator!=(const use_iterator &x) const {
449 return !operator==(x);
452 /// atEnd - return true if this iterator is at the end of uses list.
453 bool atEnd() const { return Op == 0; }
455 // Iterator traversal: forward iteration only.
456 use_iterator &operator++() { // Preincrement
457 assert(Op && "Cannot increment end iterator!");
462 use_iterator operator++(int) { // Postincrement
463 use_iterator tmp = *this; ++*this; return tmp;
466 /// Retrieve a pointer to the current user node.
467 SDNode *operator*() const {
468 assert(Op && "Cannot dereference end iterator!");
469 return Op->getUser();
472 SDNode *operator->() const { return operator*(); }
474 SDUse &getUse() const { return *Op; }
476 /// getOperandNo - Retrieve the operand # of this use in its user.
478 unsigned getOperandNo() const {
479 assert(Op && "Cannot dereference end iterator!");
480 return (unsigned)(Op - Op->getUser()->OperandList);
484 /// use_begin/use_end - Provide iteration support to walk over all uses
487 use_iterator use_begin() const {
488 return use_iterator(UseList);
491 static use_iterator use_end() { return use_iterator(0); }
494 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
495 /// indicated value. This method ignores uses of other values defined by this
497 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
499 /// hasAnyUseOfValue - Return true if there are any use of the indicated
500 /// value. This method ignores uses of other values defined by this operation.
501 bool hasAnyUseOfValue(unsigned Value) const;
503 /// isOnlyUserOf - Return true if this node is the only use of N.
505 bool isOnlyUserOf(SDNode *N) const;
507 /// isOperandOf - Return true if this node is an operand of N.
509 bool isOperandOf(SDNode *N) const;
511 /// isPredecessorOf - Return true if this node is a predecessor of N.
512 /// NOTE: Implemented on top of hasPredecessor and every bit as
513 /// expensive. Use carefully.
514 bool isPredecessorOf(const SDNode *N) const { return N->hasPredecessor(this); }
516 /// hasPredecessor - Return true if N is a predecessor of this node.
517 /// N is either an operand of this node, or can be reached by recursively
518 /// traversing up the operands.
519 /// NOTE: This is an expensive method. Use it carefully.
520 bool hasPredecessor(const SDNode *N) const;
522 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
523 /// N is either an operand of this node, or can be reached by recursively
524 /// traversing up the operands.
525 /// In this helper the Visited and worklist sets are held externally to
526 /// cache predecessors over multiple invocations. If you want to test for
527 /// multiple predecessors this method is preferable to multiple calls to
528 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
530 /// NOTE: This is still very expensive. Use carefully.
531 bool hasPredecessorHelper(const SDNode *N,
532 SmallPtrSet<const SDNode *, 32> &Visited,
533 SmallVector<const SDNode *, 16> &Worklist) const;
535 /// getNumOperands - Return the number of values used by this operation.
537 unsigned getNumOperands() const { return NumOperands; }
539 /// getConstantOperandVal - Helper method returns the integer value of a
540 /// ConstantSDNode operand.
541 uint64_t getConstantOperandVal(unsigned Num) const;
543 const SDValue &getOperand(unsigned Num) const {
544 assert(Num < NumOperands && "Invalid child # of SDNode!");
545 return OperandList[Num];
548 typedef SDUse* op_iterator;
549 op_iterator op_begin() const { return OperandList; }
550 op_iterator op_end() const { return OperandList+NumOperands; }
552 SDVTList getVTList() const {
553 SDVTList X = { ValueList, NumValues };
557 /// getGluedNode - If this node has a glue operand, return the node
558 /// to which the glue operand points. Otherwise return NULL.
559 SDNode *getGluedNode() const {
560 if (getNumOperands() != 0 &&
561 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
562 return getOperand(getNumOperands()-1).getNode();
566 // If this is a pseudo op, like copyfromreg, look to see if there is a
567 // real target node glued to it. If so, return the target node.
568 const SDNode *getGluedMachineNode() const {
569 const SDNode *FoundNode = this;
571 // Climb up glue edges until a machine-opcode node is found, or the
572 // end of the chain is reached.
573 while (!FoundNode->isMachineOpcode()) {
574 const SDNode *N = FoundNode->getGluedNode();
582 /// getGluedUser - If this node has a glue value with a user, return
583 /// the user (there is at most one). Otherwise return NULL.
584 SDNode *getGluedUser() const {
585 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
586 if (UI.getUse().get().getValueType() == MVT::Glue)
591 /// getNumValues - Return the number of values defined/returned by this
594 unsigned getNumValues() const { return NumValues; }
596 /// getValueType - Return the type of a specified result.
598 EVT getValueType(unsigned ResNo) const {
599 assert(ResNo < NumValues && "Illegal result number!");
600 return ValueList[ResNo];
603 /// Return the type of a specified result as a simple type.
605 MVT getSimpleValueType(unsigned ResNo) const {
606 return getValueType(ResNo).getSimpleVT();
609 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
611 unsigned getValueSizeInBits(unsigned ResNo) const {
612 return getValueType(ResNo).getSizeInBits();
615 typedef const EVT* value_iterator;
616 value_iterator value_begin() const { return ValueList; }
617 value_iterator value_end() const { return ValueList+NumValues; }
619 /// getOperationName - Return the opcode of this operation for printing.
621 std::string getOperationName(const SelectionDAG *G = 0) const;
622 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
623 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
624 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
625 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
626 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
628 /// printrFull - Print a SelectionDAG node and all children down to
629 /// the leaves. The given SelectionDAG allows target-specific nodes
630 /// to be printed in human-readable form. Unlike printr, this will
631 /// print the whole DAG, including children that appear multiple
634 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
636 /// printrWithDepth - Print a SelectionDAG node and children up to
637 /// depth "depth." The given SelectionDAG allows target-specific
638 /// nodes to be printed in human-readable form. Unlike printr, this
639 /// will print children that appear multiple times wherever they are
642 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
643 unsigned depth = 100) const;
646 /// dump - Dump this node, for debugging.
649 /// dumpr - Dump (recursively) this node and its use-def subgraph.
652 /// dump - Dump this node, for debugging.
653 /// The given SelectionDAG allows target-specific nodes to be printed
654 /// in human-readable form.
655 void dump(const SelectionDAG *G) const;
657 /// dumpr - Dump (recursively) this node and its use-def subgraph.
658 /// The given SelectionDAG allows target-specific nodes to be printed
659 /// in human-readable form.
660 void dumpr(const SelectionDAG *G) const;
662 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
663 /// target-specific nodes to be printed in human-readable form.
664 /// Unlike dumpr, this will print the whole DAG, including children
665 /// that appear multiple times.
667 void dumprFull(const SelectionDAG *G = 0) const;
669 /// dumprWithDepth - printrWithDepth to dbgs(). The given
670 /// SelectionDAG allows target-specific nodes to be printed in
671 /// human-readable form. Unlike dumpr, this will print children
672 /// that appear multiple times wherever they are used.
674 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
676 /// Profile - Gather unique data for the node.
678 void Profile(FoldingSetNodeID &ID) const;
680 /// addUse - This method should only be used by the SDUse class.
682 void addUse(SDUse &U) { U.addToList(&UseList); }
685 static SDVTList getSDVTList(EVT VT) {
686 SDVTList Ret = { getValueTypeList(VT), 1 };
690 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
692 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
693 SubclassData(0), NodeId(-1),
694 OperandList(NumOps ? new SDUse[NumOps] : 0),
695 ValueList(VTs.VTs), UseList(NULL),
696 NumOperands(NumOps), NumValues(VTs.NumVTs),
698 for (unsigned i = 0; i != NumOps; ++i) {
699 OperandList[i].setUser(this);
700 OperandList[i].setInitial(Ops[i]);
702 checkForCycles(this);
705 /// This constructor adds no operands itself; operands can be
706 /// set later with InitOperands.
707 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
708 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
709 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
710 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
713 /// InitOperands - Initialize the operands list of this with 1 operand.
714 void InitOperands(SDUse *Ops, const SDValue &Op0) {
715 Ops[0].setUser(this);
716 Ops[0].setInitial(Op0);
719 checkForCycles(this);
722 /// InitOperands - Initialize the operands list of this with 2 operands.
723 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
724 Ops[0].setUser(this);
725 Ops[0].setInitial(Op0);
726 Ops[1].setUser(this);
727 Ops[1].setInitial(Op1);
730 checkForCycles(this);
733 /// InitOperands - Initialize the operands list of this with 3 operands.
734 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
735 const SDValue &Op2) {
736 Ops[0].setUser(this);
737 Ops[0].setInitial(Op0);
738 Ops[1].setUser(this);
739 Ops[1].setInitial(Op1);
740 Ops[2].setUser(this);
741 Ops[2].setInitial(Op2);
744 checkForCycles(this);
747 /// InitOperands - Initialize the operands list of this with 4 operands.
748 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
749 const SDValue &Op2, const SDValue &Op3) {
750 Ops[0].setUser(this);
751 Ops[0].setInitial(Op0);
752 Ops[1].setUser(this);
753 Ops[1].setInitial(Op1);
754 Ops[2].setUser(this);
755 Ops[2].setInitial(Op2);
756 Ops[3].setUser(this);
757 Ops[3].setInitial(Op3);
760 checkForCycles(this);
763 /// InitOperands - Initialize the operands list of this with N operands.
764 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
765 for (unsigned i = 0; i != N; ++i) {
766 Ops[i].setUser(this);
767 Ops[i].setInitial(Vals[i]);
771 checkForCycles(this);
774 /// DropOperands - Release the operands and set this node to have
780 // Define inline functions from the SDValue class.
782 inline unsigned SDValue::getOpcode() const {
783 return Node->getOpcode();
785 inline EVT SDValue::getValueType() const {
786 return Node->getValueType(ResNo);
788 inline unsigned SDValue::getNumOperands() const {
789 return Node->getNumOperands();
791 inline const SDValue &SDValue::getOperand(unsigned i) const {
792 return Node->getOperand(i);
794 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
795 return Node->getConstantOperandVal(i);
797 inline bool SDValue::isTargetOpcode() const {
798 return Node->isTargetOpcode();
800 inline bool SDValue::isTargetMemoryOpcode() const {
801 return Node->isTargetMemoryOpcode();
803 inline bool SDValue::isMachineOpcode() const {
804 return Node->isMachineOpcode();
806 inline unsigned SDValue::getMachineOpcode() const {
807 return Node->getMachineOpcode();
809 inline bool SDValue::use_empty() const {
810 return !Node->hasAnyUseOfValue(ResNo);
812 inline bool SDValue::hasOneUse() const {
813 return Node->hasNUsesOfValue(1, ResNo);
815 inline const DebugLoc SDValue::getDebugLoc() const {
816 return Node->getDebugLoc();
818 inline void SDValue::dump() const {
821 inline void SDValue::dumpr() const {
822 return Node->dumpr();
824 // Define inline functions from the SDUse class.
826 inline void SDUse::set(const SDValue &V) {
827 if (Val.getNode()) removeFromList();
829 if (V.getNode()) V.getNode()->addUse(*this);
832 inline void SDUse::setInitial(const SDValue &V) {
834 V.getNode()->addUse(*this);
837 inline void SDUse::setNode(SDNode *N) {
838 if (Val.getNode()) removeFromList();
840 if (N) N->addUse(*this);
843 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
844 /// to allow co-allocation of node operands with the node itself.
845 class UnarySDNode : public SDNode {
848 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
849 : SDNode(Opc, dl, VTs) {
850 InitOperands(&Op, X);
854 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
855 /// to allow co-allocation of node operands with the node itself.
856 class BinarySDNode : public SDNode {
859 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
860 : SDNode(Opc, dl, VTs) {
861 InitOperands(Ops, X, Y);
865 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
866 /// to allow co-allocation of node operands with the node itself.
867 class TernarySDNode : public SDNode {
870 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
872 : SDNode(Opc, dl, VTs) {
873 InitOperands(Ops, X, Y, Z);
878 /// HandleSDNode - This class is used to form a handle around another node that
879 /// is persistent and is updated across invocations of replaceAllUsesWith on its
880 /// operand. This node should be directly created by end-users and not added to
881 /// the AllNodes list.
882 class HandleSDNode : public SDNode {
885 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
887 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
888 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
890 explicit HandleSDNode(SDValue X)
892 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
893 InitOperands(&Op, X);
896 const SDValue &getValue() const { return Op; }
899 /// Abstact virtual class for operations for memory operations
900 class MemSDNode : public SDNode {
902 // MemoryVT - VT of in-memory value.
906 /// MMO - Memory reference information.
907 MachineMemOperand *MMO;
910 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
911 MachineMemOperand *MMO);
913 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
914 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
916 bool readMem() const { return MMO->isLoad(); }
917 bool writeMem() const { return MMO->isStore(); }
919 /// Returns alignment and volatility of the memory access
920 unsigned getOriginalAlignment() const {
921 return MMO->getBaseAlignment();
923 unsigned getAlignment() const {
924 return MMO->getAlignment();
927 /// getRawSubclassData - Return the SubclassData value, which contains an
928 /// encoding of the volatile flag, as well as bits used by subclasses. This
929 /// function should only be used to compute a FoldingSetNodeID value.
930 unsigned getRawSubclassData() const {
934 // We access subclass data here so that we can check consistency
935 // with MachineMemOperand information.
936 bool isVolatile() const { return (SubclassData >> 5) & 1; }
937 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
938 bool isInvariant() const { return (SubclassData >> 7) & 1; }
940 AtomicOrdering getOrdering() const {
941 return AtomicOrdering((SubclassData >> 8) & 15);
943 SynchronizationScope getSynchScope() const {
944 return SynchronizationScope((SubclassData >> 12) & 1);
947 /// Returns the SrcValue and offset that describes the location of the access
948 const Value *getSrcValue() const { return MMO->getValue(); }
949 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
951 /// Returns the TBAAInfo that describes the dereference.
952 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
954 /// Returns the Ranges that describes the dereference.
955 const MDNode *getRanges() const { return MMO->getRanges(); }
957 /// getMemoryVT - Return the type of the in-memory value.
958 EVT getMemoryVT() const { return MemoryVT; }
960 /// getMemOperand - Return a MachineMemOperand object describing the memory
961 /// reference performed by operation.
962 MachineMemOperand *getMemOperand() const { return MMO; }
964 const MachinePointerInfo &getPointerInfo() const {
965 return MMO->getPointerInfo();
968 /// getAddressSpace - Return the address space for the associated pointer
969 unsigned getAddressSpace() const {
970 return getPointerInfo().getAddrSpace();
973 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
974 /// to reflect the alignment of NewMMO, if it has a greater alignment.
975 /// This must only be used when the new alignment applies to all users of
976 /// this MachineMemOperand.
977 void refineAlignment(const MachineMemOperand *NewMMO) {
978 MMO->refineAlignment(NewMMO);
981 const SDValue &getChain() const { return getOperand(0); }
982 const SDValue &getBasePtr() const {
983 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
986 // Methods to support isa and dyn_cast
987 static bool classof(const SDNode *N) {
988 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
989 // with either an intrinsic or a target opcode.
990 return N->getOpcode() == ISD::LOAD ||
991 N->getOpcode() == ISD::STORE ||
992 N->getOpcode() == ISD::PREFETCH ||
993 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
994 N->getOpcode() == ISD::ATOMIC_SWAP ||
995 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
996 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
997 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
998 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
999 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1000 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1001 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1002 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1003 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1004 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1005 N->getOpcode() == ISD::ATOMIC_LOAD ||
1006 N->getOpcode() == ISD::ATOMIC_STORE ||
1007 N->isTargetMemoryOpcode();
1011 /// AtomicSDNode - A SDNode reprenting atomic operations.
1013 class AtomicSDNode : public MemSDNode {
1016 void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
1017 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1018 assert((Ordering & 15) == Ordering &&
1019 "Ordering may not require more than 4 bits!");
1020 assert((SynchScope & 1) == SynchScope &&
1021 "SynchScope may not require more than 1 bit!");
1022 SubclassData |= Ordering << 8;
1023 SubclassData |= SynchScope << 12;
1024 assert(getOrdering() == Ordering && "Ordering encoding error!");
1025 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1029 // Opc: opcode for atomic
1030 // VTL: value type list
1031 // Chain: memory chain for operaand
1032 // Ptr: address to update as a SDValue
1033 // Cmp: compare value
1035 // SrcVal: address to update as a Value (used for MemOperand)
1036 // Align: alignment of memory
1037 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1038 SDValue Chain, SDValue Ptr,
1039 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1040 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1041 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1042 InitAtomic(Ordering, SynchScope);
1043 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1045 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1046 SDValue Chain, SDValue Ptr,
1047 SDValue Val, MachineMemOperand *MMO,
1048 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1049 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1050 InitAtomic(Ordering, SynchScope);
1051 InitOperands(Ops, Chain, Ptr, Val);
1053 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1054 SDValue Chain, SDValue Ptr,
1055 MachineMemOperand *MMO,
1056 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1057 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1058 InitAtomic(Ordering, SynchScope);
1059 InitOperands(Ops, Chain, Ptr);
1062 const SDValue &getBasePtr() const { return getOperand(1); }
1063 const SDValue &getVal() const { return getOperand(2); }
1065 bool isCompareAndSwap() const {
1066 unsigned Op = getOpcode();
1067 return Op == ISD::ATOMIC_CMP_SWAP;
1070 // Methods to support isa and dyn_cast
1071 static bool classof(const SDNode *N) {
1072 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1073 N->getOpcode() == ISD::ATOMIC_SWAP ||
1074 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1075 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1076 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1077 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1078 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1079 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1080 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1081 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1082 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1083 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1084 N->getOpcode() == ISD::ATOMIC_LOAD ||
1085 N->getOpcode() == ISD::ATOMIC_STORE;
1089 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1090 /// memory and need an associated MachineMemOperand. Its opcode may be
1091 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1092 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1093 class MemIntrinsicSDNode : public MemSDNode {
1095 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1096 const SDValue *Ops, unsigned NumOps,
1097 EVT MemoryVT, MachineMemOperand *MMO)
1098 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1101 // Methods to support isa and dyn_cast
1102 static bool classof(const SDNode *N) {
1103 // We lower some target intrinsics to their target opcode
1104 // early a node with a target opcode can be of this class
1105 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1106 N->getOpcode() == ISD::INTRINSIC_VOID ||
1107 N->getOpcode() == ISD::PREFETCH ||
1108 N->isTargetMemoryOpcode();
1112 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1113 /// support for the llvm IR shufflevector instruction. It combines elements
1114 /// from two input vectors into a new input vector, with the selection and
1115 /// ordering of elements determined by an array of integers, referred to as
1116 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1117 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1118 /// An index of -1 is treated as undef, such that the code generator may put
1119 /// any value in the corresponding element of the result.
1120 class ShuffleVectorSDNode : public SDNode {
1123 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1124 // is freed when the SelectionDAG object is destroyed.
1127 friend class SelectionDAG;
1128 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1130 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1131 InitOperands(Ops, N1, N2);
1135 ArrayRef<int> getMask() const {
1136 EVT VT = getValueType(0);
1137 return makeArrayRef(Mask, VT.getVectorNumElements());
1139 int getMaskElt(unsigned Idx) const {
1140 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1144 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1145 int getSplatIndex() const {
1146 assert(isSplat() && "Cannot get splat index for non-splat!");
1147 EVT VT = getValueType(0);
1148 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1154 static bool isSplatMask(const int *Mask, EVT VT);
1156 static bool classof(const SDNode *N) {
1157 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1161 class ConstantSDNode : public SDNode {
1162 const ConstantInt *Value;
1163 friend class SelectionDAG;
1164 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1165 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1166 DebugLoc(), getSDVTList(VT)), Value(val) {
1170 const ConstantInt *getConstantIntValue() const { return Value; }
1171 const APInt &getAPIntValue() const { return Value->getValue(); }
1172 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1173 int64_t getSExtValue() const { return Value->getSExtValue(); }
1175 bool isOne() const { return Value->isOne(); }
1176 bool isNullValue() const { return Value->isNullValue(); }
1177 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1179 static bool classof(const SDNode *N) {
1180 return N->getOpcode() == ISD::Constant ||
1181 N->getOpcode() == ISD::TargetConstant;
1185 class ConstantFPSDNode : public SDNode {
1186 const ConstantFP *Value;
1187 friend class SelectionDAG;
1188 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1189 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1190 DebugLoc(), getSDVTList(VT)), Value(val) {
1194 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1195 const ConstantFP *getConstantFPValue() const { return Value; }
1197 /// isZero - Return true if the value is positive or negative zero.
1198 bool isZero() const { return Value->isZero(); }
1200 /// isNaN - Return true if the value is a NaN.
1201 bool isNaN() const { return Value->isNaN(); }
1203 /// isExactlyValue - We don't rely on operator== working on double values, as
1204 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1205 /// As such, this method can be used to do an exact bit-for-bit comparison of
1206 /// two floating point values.
1208 /// We leave the version with the double argument here because it's just so
1209 /// convenient to write "2.0" and the like. Without this function we'd
1210 /// have to duplicate its logic everywhere it's called.
1211 bool isExactlyValue(double V) const {
1214 Tmp.convert(Value->getValueAPF().getSemantics(),
1215 APFloat::rmNearestTiesToEven, &ignored);
1216 return isExactlyValue(Tmp);
1218 bool isExactlyValue(const APFloat& V) const;
1220 static bool isValueValidForType(EVT VT, const APFloat& Val);
1222 static bool classof(const SDNode *N) {
1223 return N->getOpcode() == ISD::ConstantFP ||
1224 N->getOpcode() == ISD::TargetConstantFP;
1228 class GlobalAddressSDNode : public SDNode {
1229 const GlobalValue *TheGlobal;
1231 unsigned char TargetFlags;
1232 friend class SelectionDAG;
1233 GlobalAddressSDNode(unsigned Opc, DebugLoc DL, const GlobalValue *GA, EVT VT,
1234 int64_t o, unsigned char TargetFlags);
1237 const GlobalValue *getGlobal() const { return TheGlobal; }
1238 int64_t getOffset() const { return Offset; }
1239 unsigned char getTargetFlags() const { return TargetFlags; }
1240 // Return the address space this GlobalAddress belongs to.
1241 unsigned getAddressSpace() const;
1243 static bool classof(const SDNode *N) {
1244 return N->getOpcode() == ISD::GlobalAddress ||
1245 N->getOpcode() == ISD::TargetGlobalAddress ||
1246 N->getOpcode() == ISD::GlobalTLSAddress ||
1247 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1251 class FrameIndexSDNode : public SDNode {
1253 friend class SelectionDAG;
1254 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1255 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1256 DebugLoc(), getSDVTList(VT)), FI(fi) {
1260 int getIndex() const { return FI; }
1262 static bool classof(const SDNode *N) {
1263 return N->getOpcode() == ISD::FrameIndex ||
1264 N->getOpcode() == ISD::TargetFrameIndex;
1268 class JumpTableSDNode : public SDNode {
1270 unsigned char TargetFlags;
1271 friend class SelectionDAG;
1272 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1273 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1274 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1278 int getIndex() const { return JTI; }
1279 unsigned char getTargetFlags() const { return TargetFlags; }
1281 static bool classof(const SDNode *N) {
1282 return N->getOpcode() == ISD::JumpTable ||
1283 N->getOpcode() == ISD::TargetJumpTable;
1287 class ConstantPoolSDNode : public SDNode {
1289 const Constant *ConstVal;
1290 MachineConstantPoolValue *MachineCPVal;
1292 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1293 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1294 unsigned char TargetFlags;
1295 friend class SelectionDAG;
1296 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1297 unsigned Align, unsigned char TF)
1298 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1300 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1301 assert((int)Offset >= 0 && "Offset is too large");
1304 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1305 EVT VT, int o, unsigned Align, unsigned char TF)
1306 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1308 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1309 assert((int)Offset >= 0 && "Offset is too large");
1310 Val.MachineCPVal = v;
1311 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1316 bool isMachineConstantPoolEntry() const {
1317 return (int)Offset < 0;
1320 const Constant *getConstVal() const {
1321 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1322 return Val.ConstVal;
1325 MachineConstantPoolValue *getMachineCPVal() const {
1326 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1327 return Val.MachineCPVal;
1330 int getOffset() const {
1331 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1334 // Return the alignment of this constant pool object, which is either 0 (for
1335 // default alignment) or the desired value.
1336 unsigned getAlignment() const { return Alignment; }
1337 unsigned char getTargetFlags() const { return TargetFlags; }
1339 Type *getType() const;
1341 static bool classof(const SDNode *N) {
1342 return N->getOpcode() == ISD::ConstantPool ||
1343 N->getOpcode() == ISD::TargetConstantPool;
1347 /// Completely target-dependent object reference.
1348 class TargetIndexSDNode : public SDNode {
1349 unsigned char TargetFlags;
1352 friend class SelectionDAG;
1355 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1356 : SDNode(ISD::TargetIndex, DebugLoc(), getSDVTList(VT)),
1357 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1360 unsigned char getTargetFlags() const { return TargetFlags; }
1361 int getIndex() const { return Index; }
1362 int64_t getOffset() const { return Offset; }
1364 static bool classof(const SDNode *N) {
1365 return N->getOpcode() == ISD::TargetIndex;
1369 class BasicBlockSDNode : public SDNode {
1370 MachineBasicBlock *MBB;
1371 friend class SelectionDAG;
1372 /// Debug info is meaningful and potentially useful here, but we create
1373 /// blocks out of order when they're jumped to, which makes it a bit
1374 /// harder. Let's see if we need it first.
1375 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1376 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1380 MachineBasicBlock *getBasicBlock() const { return MBB; }
1382 static bool classof(const SDNode *N) {
1383 return N->getOpcode() == ISD::BasicBlock;
1387 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1389 class BuildVectorSDNode : public SDNode {
1390 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1391 explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1393 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1394 /// smallest element size that splats the vector. If MinSplatBits is
1395 /// nonzero, the element size must be at least that large. Note that the
1396 /// splat element may be the entire vector (i.e., a one element vector).
1397 /// Returns the splat element value in SplatValue. Any undefined bits in
1398 /// that value are zero, and the corresponding bits in the SplatUndef mask
1399 /// are set. The SplatBitSize value is set to the splat element size in
1400 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1401 /// undefined. isBigEndian describes the endianness of the target.
1402 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1403 unsigned &SplatBitSize, bool &HasAnyUndefs,
1404 unsigned MinSplatBits = 0, bool isBigEndian = false);
1406 static inline bool classof(const SDNode *N) {
1407 return N->getOpcode() == ISD::BUILD_VECTOR;
1411 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1412 /// used when the SelectionDAG needs to make a simple reference to something
1413 /// in the LLVM IR representation.
1415 class SrcValueSDNode : public SDNode {
1417 friend class SelectionDAG;
1418 /// Create a SrcValue for a general value.
1419 explicit SrcValueSDNode(const Value *v)
1420 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1423 /// getValue - return the contained Value.
1424 const Value *getValue() const { return V; }
1426 static bool classof(const SDNode *N) {
1427 return N->getOpcode() == ISD::SRCVALUE;
1431 class MDNodeSDNode : public SDNode {
1433 friend class SelectionDAG;
1434 explicit MDNodeSDNode(const MDNode *md)
1435 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1438 const MDNode *getMD() const { return MD; }
1440 static bool classof(const SDNode *N) {
1441 return N->getOpcode() == ISD::MDNODE_SDNODE;
1446 class RegisterSDNode : public SDNode {
1448 friend class SelectionDAG;
1449 RegisterSDNode(unsigned reg, EVT VT)
1450 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1454 unsigned getReg() const { return Reg; }
1456 static bool classof(const SDNode *N) {
1457 return N->getOpcode() == ISD::Register;
1461 class RegisterMaskSDNode : public SDNode {
1462 // The memory for RegMask is not owned by the node.
1463 const uint32_t *RegMask;
1464 friend class SelectionDAG;
1465 RegisterMaskSDNode(const uint32_t *mask)
1466 : SDNode(ISD::RegisterMask, DebugLoc(), getSDVTList(MVT::Untyped)),
1470 const uint32_t *getRegMask() const { return RegMask; }
1472 static bool classof(const SDNode *N) {
1473 return N->getOpcode() == ISD::RegisterMask;
1477 class BlockAddressSDNode : public SDNode {
1478 const BlockAddress *BA;
1480 unsigned char TargetFlags;
1481 friend class SelectionDAG;
1482 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1483 int64_t o, unsigned char Flags)
1484 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1485 BA(ba), Offset(o), TargetFlags(Flags) {
1488 const BlockAddress *getBlockAddress() const { return BA; }
1489 int64_t getOffset() const { return Offset; }
1490 unsigned char getTargetFlags() const { return TargetFlags; }
1492 static bool classof(const SDNode *N) {
1493 return N->getOpcode() == ISD::BlockAddress ||
1494 N->getOpcode() == ISD::TargetBlockAddress;
1498 class EHLabelSDNode : public SDNode {
1501 friend class SelectionDAG;
1502 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1503 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1504 InitOperands(&Chain, ch);
1507 MCSymbol *getLabel() const { return Label; }
1509 static bool classof(const SDNode *N) {
1510 return N->getOpcode() == ISD::EH_LABEL;
1514 class ExternalSymbolSDNode : public SDNode {
1516 unsigned char TargetFlags;
1518 friend class SelectionDAG;
1519 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1520 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1521 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1525 const char *getSymbol() const { return Symbol; }
1526 unsigned char getTargetFlags() const { return TargetFlags; }
1528 static bool classof(const SDNode *N) {
1529 return N->getOpcode() == ISD::ExternalSymbol ||
1530 N->getOpcode() == ISD::TargetExternalSymbol;
1534 class CondCodeSDNode : public SDNode {
1535 ISD::CondCode Condition;
1536 friend class SelectionDAG;
1537 explicit CondCodeSDNode(ISD::CondCode Cond)
1538 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1543 ISD::CondCode get() const { return Condition; }
1545 static bool classof(const SDNode *N) {
1546 return N->getOpcode() == ISD::CONDCODE;
1550 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1551 /// future and most targets don't support it.
1552 class CvtRndSatSDNode : public SDNode {
1553 ISD::CvtCode CvtCode;
1554 friend class SelectionDAG;
1555 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1556 unsigned NumOps, ISD::CvtCode Code)
1557 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1559 assert(NumOps == 5 && "wrong number of operations");
1562 ISD::CvtCode getCvtCode() const { return CvtCode; }
1564 static bool classof(const SDNode *N) {
1565 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1569 /// VTSDNode - This class is used to represent EVT's, which are used
1570 /// to parameterize some operations.
1571 class VTSDNode : public SDNode {
1573 friend class SelectionDAG;
1574 explicit VTSDNode(EVT VT)
1575 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1580 EVT getVT() const { return ValueType; }
1582 static bool classof(const SDNode *N) {
1583 return N->getOpcode() == ISD::VALUETYPE;
1587 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1589 class LSBaseSDNode : public MemSDNode {
1590 //! Operand array for load and store
1592 \note Moving this array to the base class captures more
1593 common functionality shared between LoadSDNode and
1598 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1599 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1600 EVT MemVT, MachineMemOperand *MMO)
1601 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1602 SubclassData |= AM << 2;
1603 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1604 InitOperands(Ops, Operands, numOperands);
1605 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1606 "Only indexed loads and stores have a non-undef offset operand");
1609 const SDValue &getOffset() const {
1610 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1613 /// getAddressingMode - Return the addressing mode for this load or store:
1614 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1615 ISD::MemIndexedMode getAddressingMode() const {
1616 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1619 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1620 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1622 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1623 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1625 static bool classof(const SDNode *N) {
1626 return N->getOpcode() == ISD::LOAD ||
1627 N->getOpcode() == ISD::STORE;
1631 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1633 class LoadSDNode : public LSBaseSDNode {
1634 friend class SelectionDAG;
1635 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1636 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1637 MachineMemOperand *MMO)
1638 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1639 VTs, AM, MemVT, MMO) {
1640 SubclassData |= (unsigned short)ETy;
1641 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1642 assert(readMem() && "Load MachineMemOperand is not a load!");
1643 assert(!writeMem() && "Load MachineMemOperand is a store!");
1647 /// getExtensionType - Return whether this is a plain node,
1648 /// or one of the varieties of value-extending loads.
1649 ISD::LoadExtType getExtensionType() const {
1650 return ISD::LoadExtType(SubclassData & 3);
1653 const SDValue &getBasePtr() const { return getOperand(1); }
1654 const SDValue &getOffset() const { return getOperand(2); }
1656 static bool classof(const SDNode *N) {
1657 return N->getOpcode() == ISD::LOAD;
1661 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1663 class StoreSDNode : public LSBaseSDNode {
1664 friend class SelectionDAG;
1665 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1666 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1667 MachineMemOperand *MMO)
1668 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1669 VTs, AM, MemVT, MMO) {
1670 SubclassData |= (unsigned short)isTrunc;
1671 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1672 assert(!readMem() && "Store MachineMemOperand is a load!");
1673 assert(writeMem() && "Store MachineMemOperand is not a store!");
1677 /// isTruncatingStore - Return true if the op does a truncation before store.
1678 /// For integers this is the same as doing a TRUNCATE and storing the result.
1679 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1680 bool isTruncatingStore() const { return SubclassData & 1; }
1682 const SDValue &getValue() const { return getOperand(1); }
1683 const SDValue &getBasePtr() const { return getOperand(2); }
1684 const SDValue &getOffset() const { return getOperand(3); }
1686 static bool classof(const SDNode *N) {
1687 return N->getOpcode() == ISD::STORE;
1691 /// MachineSDNode - An SDNode that represents everything that will be needed
1692 /// to construct a MachineInstr. These nodes are created during the
1693 /// instruction selection proper phase.
1695 class MachineSDNode : public SDNode {
1697 typedef MachineMemOperand **mmo_iterator;
1700 friend class SelectionDAG;
1701 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1702 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1704 /// LocalOperands - Operands for this instruction, if they fit here. If
1705 /// they don't, this field is unused.
1706 SDUse LocalOperands[4];
1708 /// MemRefs - Memory reference descriptions for this instruction.
1709 mmo_iterator MemRefs;
1710 mmo_iterator MemRefsEnd;
1713 mmo_iterator memoperands_begin() const { return MemRefs; }
1714 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1715 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1717 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1718 /// list. This does not transfer ownership.
1719 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1720 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1721 assert(*MMI && "Null mem ref detected!");
1722 MemRefs = NewMemRefs;
1723 MemRefsEnd = NewMemRefsEnd;
1726 static bool classof(const SDNode *N) {
1727 return N->isMachineOpcode();
1731 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1732 SDNode, ptrdiff_t> {
1736 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1738 bool operator==(const SDNodeIterator& x) const {
1739 return Operand == x.Operand;
1741 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1743 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1744 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1745 Operand = I.Operand;
1749 pointer operator*() const {
1750 return Node->getOperand(Operand).getNode();
1752 pointer operator->() const { return operator*(); }
1754 SDNodeIterator& operator++() { // Preincrement
1758 SDNodeIterator operator++(int) { // Postincrement
1759 SDNodeIterator tmp = *this; ++*this; return tmp;
1761 size_t operator-(SDNodeIterator Other) const {
1762 assert(Node == Other.Node &&
1763 "Cannot compare iterators of two different nodes!");
1764 return Operand - Other.Operand;
1767 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1768 static SDNodeIterator end (const SDNode *N) {
1769 return SDNodeIterator(N, N->getNumOperands());
1772 unsigned getOperand() const { return Operand; }
1773 const SDNode *getNode() const { return Node; }
1776 template <> struct GraphTraits<SDNode*> {
1777 typedef SDNode NodeType;
1778 typedef SDNodeIterator ChildIteratorType;
1779 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1780 static inline ChildIteratorType child_begin(NodeType *N) {
1781 return SDNodeIterator::begin(N);
1783 static inline ChildIteratorType child_end(NodeType *N) {
1784 return SDNodeIterator::end(N);
1788 /// LargestSDNode - The largest SDNode class.
1790 typedef LoadSDNode LargestSDNode;
1792 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1795 typedef GlobalAddressSDNode MostAlignedSDNode;
1798 /// isNormalLoad - Returns true if the specified node is a non-extending
1799 /// and unindexed load.
1800 inline bool isNormalLoad(const SDNode *N) {
1801 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1802 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1803 Ld->getAddressingMode() == ISD::UNINDEXED;
1806 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1808 inline bool isNON_EXTLoad(const SDNode *N) {
1809 return isa<LoadSDNode>(N) &&
1810 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1813 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1815 inline bool isEXTLoad(const SDNode *N) {
1816 return isa<LoadSDNode>(N) &&
1817 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1820 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1822 inline bool isSEXTLoad(const SDNode *N) {
1823 return isa<LoadSDNode>(N) &&
1824 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1827 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1829 inline bool isZEXTLoad(const SDNode *N) {
1830 return isa<LoadSDNode>(N) &&
1831 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1834 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1836 inline bool isUNINDEXEDLoad(const SDNode *N) {
1837 return isa<LoadSDNode>(N) &&
1838 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1841 /// isNormalStore - Returns true if the specified node is a non-truncating
1842 /// and unindexed store.
1843 inline bool isNormalStore(const SDNode *N) {
1844 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1845 return St && !St->isTruncatingStore() &&
1846 St->getAddressingMode() == ISD::UNINDEXED;
1849 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1851 inline bool isNON_TRUNCStore(const SDNode *N) {
1852 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1855 /// isTRUNCStore - Returns true if the specified node is a truncating
1857 inline bool isTRUNCStore(const SDNode *N) {
1858 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1861 /// isUNINDEXEDStore - Returns true if the specified node is an
1862 /// unindexed store.
1863 inline bool isUNINDEXEDStore(const SDNode *N) {
1864 return isa<StoreSDNode>(N) &&
1865 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1869 } // end llvm namespace