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/Constants.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/CodeGen/ISDOpcodes.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/DebugLoc.h"
41 class MachineBasicBlock;
42 class MachineConstantPoolValue;
46 template <typename T> struct DenseMapInfo;
47 template <typename T> struct simplify_type;
48 template <typename T> struct ilist_traits;
50 void checkForCycles(const SDNode *N);
52 /// SDVTList - This represents a list of ValueType's that has been intern'd by
53 /// a SelectionDAG. Instances of this simple value class are returned by
54 /// SelectionDAG::getVTList(...).
64 /// isBuildVectorAllOnes - Return true if the specified node is a
65 /// BUILD_VECTOR where all of the elements are ~0 or undef.
66 bool isBuildVectorAllOnes(const SDNode *N);
68 /// isBuildVectorAllZeros - Return true if the specified node is a
69 /// BUILD_VECTOR where all of the elements are 0 or undef.
70 bool isBuildVectorAllZeros(const SDNode *N);
72 /// isScalarToVector - Return true if the specified node is a
73 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
74 /// element is not an undef.
75 bool isScalarToVector(const SDNode *N);
76 } // end llvm:ISD namespace
78 //===----------------------------------------------------------------------===//
79 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
80 /// values as the result of a computation. Many nodes return multiple values,
81 /// from loads (which define a token and a return value) to ADDC (which returns
82 /// a result and a carry value), to calls (which may return an arbitrary number
85 /// As such, each use of a SelectionDAG computation must indicate the node that
86 /// computes it as well as which return value to use from that node. This pair
87 /// of information is represented with the SDValue value type.
90 SDNode *Node; // The node defining the value we are using.
91 unsigned ResNo; // Which return value of the node we are using.
93 SDValue() : Node(0), ResNo(0) {}
94 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
96 /// get the index which selects a specific result in the SDNode
97 unsigned getResNo() const { return ResNo; }
99 /// get the SDNode which holds the desired result
100 SDNode *getNode() const { return Node; }
103 void setNode(SDNode *N) { Node = N; }
105 inline SDNode *operator->() const { return Node; }
107 bool operator==(const SDValue &O) const {
108 return Node == O.Node && ResNo == O.ResNo;
110 bool operator!=(const SDValue &O) const {
111 return !operator==(O);
113 bool operator<(const SDValue &O) const {
114 return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
117 SDValue getValue(unsigned R) const {
118 return SDValue(Node, R);
121 // isOperandOf - Return true if this node is an operand of N.
122 bool isOperandOf(SDNode *N) const;
124 /// getValueType - Return the ValueType of the referenced return value.
126 inline EVT getValueType() const;
128 /// getValueSizeInBits - Returns the size of the value in bits.
130 unsigned getValueSizeInBits() const {
131 return getValueType().getSizeInBits();
134 // Forwarding methods - These forward to the corresponding methods in SDNode.
135 inline unsigned getOpcode() const;
136 inline unsigned getNumOperands() const;
137 inline const SDValue &getOperand(unsigned i) const;
138 inline uint64_t getConstantOperandVal(unsigned i) const;
139 inline bool isTargetMemoryOpcode() const;
140 inline bool isTargetOpcode() const;
141 inline bool isMachineOpcode() const;
142 inline unsigned getMachineOpcode() const;
143 inline const DebugLoc getDebugLoc() const;
146 /// reachesChainWithoutSideEffects - Return true if this operand (which must
147 /// be a chain) reaches the specified operand without crossing any
148 /// side-effecting instructions. In practice, this looks through token
149 /// factors and non-volatile loads. In order to remain efficient, this only
150 /// looks a couple of nodes in, it does not do an exhaustive search.
151 bool reachesChainWithoutSideEffects(SDValue Dest,
152 unsigned Depth = 2) const;
154 /// use_empty - Return true if there are no nodes using value ResNo
157 inline bool use_empty() const;
159 /// hasOneUse - Return true if there is exactly one node using value
162 inline bool hasOneUse() const;
166 template<> struct DenseMapInfo<SDValue> {
167 static inline SDValue getEmptyKey() {
168 return SDValue((SDNode*)-1, -1U);
170 static inline SDValue getTombstoneKey() {
171 return SDValue((SDNode*)-1, 0);
173 static unsigned getHashValue(const SDValue &Val) {
174 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
175 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
177 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
181 template <> struct isPodLike<SDValue> { static const bool value = true; };
184 /// simplify_type specializations - Allow casting operators to work directly on
185 /// SDValues as if they were SDNode*'s.
186 template<> struct simplify_type<SDValue> {
187 typedef SDNode* SimpleType;
188 static SimpleType getSimplifiedValue(const SDValue &Val) {
189 return static_cast<SimpleType>(Val.getNode());
192 template<> struct simplify_type<const SDValue> {
193 typedef SDNode* SimpleType;
194 static SimpleType getSimplifiedValue(const SDValue &Val) {
195 return static_cast<SimpleType>(Val.getNode());
199 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
200 /// which records the SDNode being used and the result number, a
201 /// pointer to the SDNode using the value, and Next and Prev pointers,
202 /// which link together all the uses of an SDNode.
205 /// Val - The value being used.
207 /// User - The user of this value.
209 /// Prev, Next - Pointers to the uses list of the SDNode referred by
213 SDUse(const SDUse &U); // Do not implement
214 void operator=(const SDUse &U); // Do not implement
217 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
219 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
220 operator const SDValue&() const { return Val; }
222 /// If implicit conversion to SDValue doesn't work, the get() method returns
224 const SDValue &get() const { return Val; }
226 /// getUser - This returns the SDNode that contains this Use.
227 SDNode *getUser() { return User; }
229 /// getNext - Get the next SDUse in the use list.
230 SDUse *getNext() const { return Next; }
232 /// getNode - Convenience function for get().getNode().
233 SDNode *getNode() const { return Val.getNode(); }
234 /// getResNo - Convenience function for get().getResNo().
235 unsigned getResNo() const { return Val.getResNo(); }
236 /// getValueType - Convenience function for get().getValueType().
237 EVT getValueType() const { return Val.getValueType(); }
239 /// operator== - Convenience function for get().operator==
240 bool operator==(const SDValue &V) const {
244 /// operator!= - Convenience function for get().operator!=
245 bool operator!=(const SDValue &V) const {
249 /// operator< - Convenience function for get().operator<
250 bool operator<(const SDValue &V) const {
255 friend class SelectionDAG;
258 void setUser(SDNode *p) { User = p; }
260 /// set - Remove this use from its existing use list, assign it the
261 /// given value, and add it to the new value's node's use list.
262 inline void set(const SDValue &V);
263 /// setInitial - like set, but only supports initializing a newly-allocated
264 /// SDUse with a non-null value.
265 inline void setInitial(const SDValue &V);
266 /// setNode - like set, but only sets the Node portion of the value,
267 /// leaving the ResNo portion unmodified.
268 inline void setNode(SDNode *N);
270 void addToList(SDUse **List) {
272 if (Next) Next->Prev = &Next;
277 void removeFromList() {
279 if (Next) Next->Prev = Prev;
283 /// simplify_type specializations - Allow casting operators to work directly on
284 /// SDValues as if they were SDNode*'s.
285 template<> struct simplify_type<SDUse> {
286 typedef SDNode* SimpleType;
287 static SimpleType getSimplifiedValue(const SDUse &Val) {
288 return static_cast<SimpleType>(Val.getNode());
291 template<> struct simplify_type<const SDUse> {
292 typedef SDNode* SimpleType;
293 static SimpleType getSimplifiedValue(const SDUse &Val) {
294 return static_cast<SimpleType>(Val.getNode());
299 /// SDNode - Represents one node in the SelectionDAG.
301 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
303 /// NodeType - The operation that this node performs.
307 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
308 /// then they will be delete[]'d when the node is destroyed.
309 uint16_t OperandsNeedDelete : 1;
311 /// HasDebugValue - This tracks whether this node has one or more dbg_value
312 /// nodes corresponding to it.
313 uint16_t HasDebugValue : 1;
316 /// SubclassData - This member is defined by this class, but is not used for
317 /// anything. Subclasses can use it to hold whatever state they find useful.
318 /// This field is initialized to zero by the ctor.
319 uint16_t SubclassData : 14;
322 /// NodeId - Unique id per SDNode in the DAG.
325 /// OperandList - The values that are used by this operation.
329 /// ValueList - The types of the values this node defines. SDNode's may
330 /// define multiple values simultaneously.
331 const EVT *ValueList;
333 /// UseList - List of uses for this SDNode.
336 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
337 unsigned short NumOperands, NumValues;
339 /// debugLoc - source line information.
342 /// getValueTypeList - Return a pointer to the specified value type.
343 static const EVT *getValueTypeList(EVT VT);
345 friend class SelectionDAG;
346 friend struct ilist_traits<SDNode>;
349 //===--------------------------------------------------------------------===//
353 /// getOpcode - Return the SelectionDAG opcode value for this node. For
354 /// pre-isel nodes (those for which isMachineOpcode returns false), these
355 /// are the opcode values in the ISD and <target>ISD namespaces. For
356 /// post-isel opcodes, see getMachineOpcode.
357 unsigned getOpcode() const { return (unsigned short)NodeType; }
359 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
360 /// \<target\>ISD namespace).
361 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
363 /// isTargetMemoryOpcode - Test if this node has a target-specific
364 /// memory-referencing opcode (in the \<target\>ISD namespace and
365 /// greater than FIRST_TARGET_MEMORY_OPCODE).
366 bool isTargetMemoryOpcode() const {
367 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
370 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
371 /// corresponding to a MachineInstr opcode.
372 bool isMachineOpcode() const { return NodeType < 0; }
374 /// getMachineOpcode - This may only be called if isMachineOpcode returns
375 /// true. It returns the MachineInstr opcode value that the node's opcode
377 unsigned getMachineOpcode() const {
378 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
382 /// getHasDebugValue - get this bit.
383 bool getHasDebugValue() const { return HasDebugValue; }
385 /// setHasDebugValue - set this bit.
386 void setHasDebugValue(bool b) { HasDebugValue = b; }
388 /// use_empty - Return true if there are no uses of this node.
390 bool use_empty() const { return UseList == NULL; }
392 /// hasOneUse - Return true if there is exactly one use of this node.
394 bool hasOneUse() const {
395 return !use_empty() && llvm::next(use_begin()) == use_end();
398 /// use_size - Return the number of uses of this node. This method takes
399 /// time proportional to the number of uses.
401 size_t use_size() const { return std::distance(use_begin(), use_end()); }
403 /// getNodeId - Return the unique node id.
405 int getNodeId() const { return NodeId; }
407 /// setNodeId - Set unique node id.
408 void setNodeId(int Id) { NodeId = Id; }
410 /// getDebugLoc - Return the source location info.
411 const DebugLoc getDebugLoc() const { return debugLoc; }
413 /// setDebugLoc - Set source location info. Try to avoid this, putting
414 /// it in the constructor is preferable.
415 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
417 /// use_iterator - This class provides iterator support for SDUse
418 /// operands that use a specific SDNode.
420 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
422 explicit use_iterator(SDUse *op) : Op(op) {
426 typedef std::iterator<std::forward_iterator_tag,
427 SDUse, ptrdiff_t>::reference reference;
428 typedef std::iterator<std::forward_iterator_tag,
429 SDUse, ptrdiff_t>::pointer pointer;
431 use_iterator(const use_iterator &I) : Op(I.Op) {}
432 use_iterator() : Op(0) {}
434 bool operator==(const use_iterator &x) const {
437 bool operator!=(const use_iterator &x) const {
438 return !operator==(x);
441 /// atEnd - return true if this iterator is at the end of uses list.
442 bool atEnd() const { return Op == 0; }
444 // Iterator traversal: forward iteration only.
445 use_iterator &operator++() { // Preincrement
446 assert(Op && "Cannot increment end iterator!");
451 use_iterator operator++(int) { // Postincrement
452 use_iterator tmp = *this; ++*this; return tmp;
455 /// Retrieve a pointer to the current user node.
456 SDNode *operator*() const {
457 assert(Op && "Cannot dereference end iterator!");
458 return Op->getUser();
461 SDNode *operator->() const { return operator*(); }
463 SDUse &getUse() const { return *Op; }
465 /// getOperandNo - Retrieve the operand # of this use in its user.
467 unsigned getOperandNo() const {
468 assert(Op && "Cannot dereference end iterator!");
469 return (unsigned)(Op - Op->getUser()->OperandList);
473 /// use_begin/use_end - Provide iteration support to walk over all uses
476 use_iterator use_begin() const {
477 return use_iterator(UseList);
480 static use_iterator use_end() { return use_iterator(0); }
483 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
484 /// indicated value. This method ignores uses of other values defined by this
486 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
488 /// hasAnyUseOfValue - Return true if there are any use of the indicated
489 /// value. This method ignores uses of other values defined by this operation.
490 bool hasAnyUseOfValue(unsigned Value) const;
492 /// isOnlyUserOf - Return true if this node is the only use of N.
494 bool isOnlyUserOf(SDNode *N) const;
496 /// isOperandOf - Return true if this node is an operand of N.
498 bool isOperandOf(SDNode *N) const;
500 /// isPredecessorOf - Return true if this node is a predecessor of N.
501 /// NOTE: Implemented on top of hasPredecessor and every bit as
502 /// expensive. Use carefully.
503 bool isPredecessorOf(const SDNode *N) const { return N->hasPredecessor(this); }
505 /// hasPredecessor - Return true if N is a predecessor of this node.
506 /// N is either an operand of this node, or can be reached by recursively
507 /// traversing up the operands.
508 /// NOTE: This is an expensive method. Use it carefully.
509 bool hasPredecessor(const SDNode *N) const;
511 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
512 /// N is either an operand of this node, or can be reached by recursively
513 /// traversing up the operands.
514 /// In this helper the Visited and worklist sets are held externally to
515 /// cache predecessors over multiple invocations. If you want to test for
516 /// multiple predecessors this method is preferable to multiple calls to
517 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
519 /// NOTE: This is still very expensive. Use carefully.
520 bool hasPredecessorHelper(const SDNode *N,
521 SmallPtrSet<const SDNode *, 32> &Visited,
522 SmallVector<const SDNode *, 16> &Worklist) const;
524 /// getNumOperands - Return the number of values used by this operation.
526 unsigned getNumOperands() const { return NumOperands; }
528 /// getConstantOperandVal - Helper method returns the integer value of a
529 /// ConstantSDNode operand.
530 uint64_t getConstantOperandVal(unsigned Num) const;
532 const SDValue &getOperand(unsigned Num) const {
533 assert(Num < NumOperands && "Invalid child # of SDNode!");
534 return OperandList[Num];
537 typedef SDUse* op_iterator;
538 op_iterator op_begin() const { return OperandList; }
539 op_iterator op_end() const { return OperandList+NumOperands; }
541 SDVTList getVTList() const {
542 SDVTList X = { ValueList, NumValues };
546 /// getGluedNode - If this node has a glue operand, return the node
547 /// to which the glue operand points. Otherwise return NULL.
548 SDNode *getGluedNode() const {
549 if (getNumOperands() != 0 &&
550 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
551 return getOperand(getNumOperands()-1).getNode();
555 // If this is a pseudo op, like copyfromreg, look to see if there is a
556 // real target node glued to it. If so, return the target node.
557 const SDNode *getGluedMachineNode() const {
558 const SDNode *FoundNode = this;
560 // Climb up glue edges until a machine-opcode node is found, or the
561 // end of the chain is reached.
562 while (!FoundNode->isMachineOpcode()) {
563 const SDNode *N = FoundNode->getGluedNode();
571 /// getGluedUser - If this node has a glue value with a user, return
572 /// the user (there is at most one). Otherwise return NULL.
573 SDNode *getGluedUser() const {
574 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
575 if (UI.getUse().get().getValueType() == MVT::Glue)
580 /// getNumValues - Return the number of values defined/returned by this
583 unsigned getNumValues() const { return NumValues; }
585 /// getValueType - Return the type of a specified result.
587 EVT getValueType(unsigned ResNo) const {
588 assert(ResNo < NumValues && "Illegal result number!");
589 return ValueList[ResNo];
592 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
594 unsigned getValueSizeInBits(unsigned ResNo) const {
595 return getValueType(ResNo).getSizeInBits();
598 typedef const EVT* value_iterator;
599 value_iterator value_begin() const { return ValueList; }
600 value_iterator value_end() const { return ValueList+NumValues; }
602 /// getOperationName - Return the opcode of this operation for printing.
604 std::string getOperationName(const SelectionDAG *G = 0) const;
605 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
606 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
607 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
608 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
609 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
611 /// printrFull - Print a SelectionDAG node and all children down to
612 /// the leaves. The given SelectionDAG allows target-specific nodes
613 /// to be printed in human-readable form. Unlike printr, this will
614 /// print the whole DAG, including children that appear multiple
617 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
619 /// printrWithDepth - Print a SelectionDAG node and children up to
620 /// depth "depth." The given SelectionDAG allows target-specific
621 /// nodes to be printed in human-readable form. Unlike printr, this
622 /// will print children that appear multiple times wherever they are
625 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
626 unsigned depth = 100) const;
629 /// dump - Dump this node, for debugging.
632 /// dumpr - Dump (recursively) this node and its use-def subgraph.
635 /// dump - Dump this node, for debugging.
636 /// The given SelectionDAG allows target-specific nodes to be printed
637 /// in human-readable form.
638 void dump(const SelectionDAG *G) const;
640 /// dumpr - Dump (recursively) this node and its use-def subgraph.
641 /// The given SelectionDAG allows target-specific nodes to be printed
642 /// in human-readable form.
643 void dumpr(const SelectionDAG *G) const;
645 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
646 /// target-specific nodes to be printed in human-readable form.
647 /// Unlike dumpr, this will print the whole DAG, including children
648 /// that appear multiple times.
650 void dumprFull(const SelectionDAG *G = 0) const;
652 /// dumprWithDepth - printrWithDepth to dbgs(). The given
653 /// SelectionDAG allows target-specific nodes to be printed in
654 /// human-readable form. Unlike dumpr, this will print children
655 /// that appear multiple times wherever they are used.
657 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
660 static bool classof(const SDNode *) { return true; }
662 /// Profile - Gather unique data for the node.
664 void Profile(FoldingSetNodeID &ID) const;
666 /// addUse - This method should only be used by the SDUse class.
668 void addUse(SDUse &U) { U.addToList(&UseList); }
671 static SDVTList getSDVTList(EVT VT) {
672 SDVTList Ret = { getValueTypeList(VT), 1 };
676 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
678 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
679 SubclassData(0), NodeId(-1),
680 OperandList(NumOps ? new SDUse[NumOps] : 0),
681 ValueList(VTs.VTs), UseList(NULL),
682 NumOperands(NumOps), NumValues(VTs.NumVTs),
684 for (unsigned i = 0; i != NumOps; ++i) {
685 OperandList[i].setUser(this);
686 OperandList[i].setInitial(Ops[i]);
688 checkForCycles(this);
691 /// This constructor adds no operands itself; operands can be
692 /// set later with InitOperands.
693 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
694 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
695 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
696 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
699 /// InitOperands - Initialize the operands list of this with 1 operand.
700 void InitOperands(SDUse *Ops, const SDValue &Op0) {
701 Ops[0].setUser(this);
702 Ops[0].setInitial(Op0);
705 checkForCycles(this);
708 /// InitOperands - Initialize the operands list of this with 2 operands.
709 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
710 Ops[0].setUser(this);
711 Ops[0].setInitial(Op0);
712 Ops[1].setUser(this);
713 Ops[1].setInitial(Op1);
716 checkForCycles(this);
719 /// InitOperands - Initialize the operands list of this with 3 operands.
720 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
721 const SDValue &Op2) {
722 Ops[0].setUser(this);
723 Ops[0].setInitial(Op0);
724 Ops[1].setUser(this);
725 Ops[1].setInitial(Op1);
726 Ops[2].setUser(this);
727 Ops[2].setInitial(Op2);
730 checkForCycles(this);
733 /// InitOperands - Initialize the operands list of this with 4 operands.
734 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
735 const SDValue &Op2, const SDValue &Op3) {
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);
742 Ops[3].setUser(this);
743 Ops[3].setInitial(Op3);
746 checkForCycles(this);
749 /// InitOperands - Initialize the operands list of this with N operands.
750 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
751 for (unsigned i = 0; i != N; ++i) {
752 Ops[i].setUser(this);
753 Ops[i].setInitial(Vals[i]);
757 checkForCycles(this);
760 /// DropOperands - Release the operands and set this node to have
766 // Define inline functions from the SDValue class.
768 inline unsigned SDValue::getOpcode() const {
769 return Node->getOpcode();
771 inline EVT SDValue::getValueType() const {
772 return Node->getValueType(ResNo);
774 inline unsigned SDValue::getNumOperands() const {
775 return Node->getNumOperands();
777 inline const SDValue &SDValue::getOperand(unsigned i) const {
778 return Node->getOperand(i);
780 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
781 return Node->getConstantOperandVal(i);
783 inline bool SDValue::isTargetOpcode() const {
784 return Node->isTargetOpcode();
786 inline bool SDValue::isTargetMemoryOpcode() const {
787 return Node->isTargetMemoryOpcode();
789 inline bool SDValue::isMachineOpcode() const {
790 return Node->isMachineOpcode();
792 inline unsigned SDValue::getMachineOpcode() const {
793 return Node->getMachineOpcode();
795 inline bool SDValue::use_empty() const {
796 return !Node->hasAnyUseOfValue(ResNo);
798 inline bool SDValue::hasOneUse() const {
799 return Node->hasNUsesOfValue(1, ResNo);
801 inline const DebugLoc SDValue::getDebugLoc() const {
802 return Node->getDebugLoc();
805 // Define inline functions from the SDUse class.
807 inline void SDUse::set(const SDValue &V) {
808 if (Val.getNode()) removeFromList();
810 if (V.getNode()) V.getNode()->addUse(*this);
813 inline void SDUse::setInitial(const SDValue &V) {
815 V.getNode()->addUse(*this);
818 inline void SDUse::setNode(SDNode *N) {
819 if (Val.getNode()) removeFromList();
821 if (N) N->addUse(*this);
824 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
825 /// to allow co-allocation of node operands with the node itself.
826 class UnarySDNode : public SDNode {
829 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
830 : SDNode(Opc, dl, VTs) {
831 InitOperands(&Op, X);
835 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
836 /// to allow co-allocation of node operands with the node itself.
837 class BinarySDNode : public SDNode {
840 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
841 : SDNode(Opc, dl, VTs) {
842 InitOperands(Ops, X, Y);
846 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
847 /// to allow co-allocation of node operands with the node itself.
848 class TernarySDNode : public SDNode {
851 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
853 : SDNode(Opc, dl, VTs) {
854 InitOperands(Ops, X, Y, Z);
859 /// HandleSDNode - This class is used to form a handle around another node that
860 /// is persistent and is updated across invocations of replaceAllUsesWith on its
861 /// operand. This node should be directly created by end-users and not added to
862 /// the AllNodes list.
863 class HandleSDNode : public SDNode {
866 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
868 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
869 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
871 explicit HandleSDNode(SDValue X)
873 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
874 InitOperands(&Op, X);
877 const SDValue &getValue() const { return Op; }
880 /// Abstact virtual class for operations for memory operations
881 class MemSDNode : public SDNode {
883 // MemoryVT - VT of in-memory value.
887 /// MMO - Memory reference information.
888 MachineMemOperand *MMO;
891 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
892 MachineMemOperand *MMO);
894 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
895 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
897 bool readMem() const { return MMO->isLoad(); }
898 bool writeMem() const { return MMO->isStore(); }
900 /// Returns alignment and volatility of the memory access
901 unsigned getOriginalAlignment() const {
902 return MMO->getBaseAlignment();
904 unsigned getAlignment() const {
905 return MMO->getAlignment();
908 /// getRawSubclassData - Return the SubclassData value, which contains an
909 /// encoding of the volatile flag, as well as bits used by subclasses. This
910 /// function should only be used to compute a FoldingSetNodeID value.
911 unsigned getRawSubclassData() const {
915 // We access subclass data here so that we can check consistency
916 // with MachineMemOperand information.
917 bool isVolatile() const { return (SubclassData >> 5) & 1; }
918 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
920 /// Returns the SrcValue and offset that describes the location of the access
921 const Value *getSrcValue() const { return MMO->getValue(); }
922 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
924 /// Returns the TBAAInfo that describes the dereference.
925 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
927 /// getMemoryVT - Return the type of the in-memory value.
928 EVT getMemoryVT() const { return MemoryVT; }
930 /// getMemOperand - Return a MachineMemOperand object describing the memory
931 /// reference performed by operation.
932 MachineMemOperand *getMemOperand() const { return MMO; }
934 const MachinePointerInfo &getPointerInfo() const {
935 return MMO->getPointerInfo();
938 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
939 /// to reflect the alignment of NewMMO, if it has a greater alignment.
940 /// This must only be used when the new alignment applies to all users of
941 /// this MachineMemOperand.
942 void refineAlignment(const MachineMemOperand *NewMMO) {
943 MMO->refineAlignment(NewMMO);
946 const SDValue &getChain() const { return getOperand(0); }
947 const SDValue &getBasePtr() const {
948 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
951 // Methods to support isa and dyn_cast
952 static bool classof(const MemSDNode *) { return true; }
953 static bool classof(const SDNode *N) {
954 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
955 // with either an intrinsic or a target opcode.
956 return N->getOpcode() == ISD::LOAD ||
957 N->getOpcode() == ISD::STORE ||
958 N->getOpcode() == ISD::PREFETCH ||
959 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
960 N->getOpcode() == ISD::ATOMIC_SWAP ||
961 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
962 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
963 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
964 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
965 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
966 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
967 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
968 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
969 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
970 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
971 N->isTargetMemoryOpcode();
975 /// AtomicSDNode - A SDNode reprenting atomic operations.
977 class AtomicSDNode : public MemSDNode {
981 // Opc: opcode for atomic
982 // VTL: value type list
983 // Chain: memory chain for operaand
984 // Ptr: address to update as a SDValue
985 // Cmp: compare value
987 // SrcVal: address to update as a Value (used for MemOperand)
988 // Align: alignment of memory
989 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
990 SDValue Chain, SDValue Ptr,
991 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
992 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
993 assert(readMem() && "Atomic MachineMemOperand is not a load!");
994 assert(writeMem() && "Atomic MachineMemOperand is not a store!");
995 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
997 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
998 SDValue Chain, SDValue Ptr,
999 SDValue Val, MachineMemOperand *MMO)
1000 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1001 assert(readMem() && "Atomic MachineMemOperand is not a load!");
1002 assert(writeMem() && "Atomic MachineMemOperand is not a store!");
1003 InitOperands(Ops, Chain, Ptr, Val);
1006 const SDValue &getBasePtr() const { return getOperand(1); }
1007 const SDValue &getVal() const { return getOperand(2); }
1009 bool isCompareAndSwap() const {
1010 unsigned Op = getOpcode();
1011 return Op == ISD::ATOMIC_CMP_SWAP;
1014 // Methods to support isa and dyn_cast
1015 static bool classof(const AtomicSDNode *) { return true; }
1016 static bool classof(const SDNode *N) {
1017 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1018 N->getOpcode() == ISD::ATOMIC_SWAP ||
1019 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1020 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1021 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1022 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1023 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1024 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1025 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1026 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1027 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1028 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX;
1032 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1033 /// memory and need an associated MachineMemOperand. Its opcode may be
1034 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1035 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1036 class MemIntrinsicSDNode : public MemSDNode {
1038 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1039 const SDValue *Ops, unsigned NumOps,
1040 EVT MemoryVT, MachineMemOperand *MMO)
1041 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1044 // Methods to support isa and dyn_cast
1045 static bool classof(const MemIntrinsicSDNode *) { return true; }
1046 static bool classof(const SDNode *N) {
1047 // We lower some target intrinsics to their target opcode
1048 // early a node with a target opcode can be of this class
1049 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1050 N->getOpcode() == ISD::INTRINSIC_VOID ||
1051 N->getOpcode() == ISD::PREFETCH ||
1052 N->isTargetMemoryOpcode();
1056 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1057 /// support for the llvm IR shufflevector instruction. It combines elements
1058 /// from two input vectors into a new input vector, with the selection and
1059 /// ordering of elements determined by an array of integers, referred to as
1060 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1061 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1062 /// An index of -1 is treated as undef, such that the code generator may put
1063 /// any value in the corresponding element of the result.
1064 class ShuffleVectorSDNode : public SDNode {
1067 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1068 // is freed when the SelectionDAG object is destroyed.
1071 friend class SelectionDAG;
1072 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1074 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1075 InitOperands(Ops, N1, N2);
1079 void getMask(SmallVectorImpl<int> &M) const {
1080 EVT VT = getValueType(0);
1082 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1083 M.push_back(Mask[i]);
1085 int getMaskElt(unsigned Idx) const {
1086 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1090 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1091 int getSplatIndex() const {
1092 assert(isSplat() && "Cannot get splat index for non-splat!");
1093 EVT VT = getValueType(0);
1094 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1100 static bool isSplatMask(const int *Mask, EVT VT);
1102 static bool classof(const ShuffleVectorSDNode *) { return true; }
1103 static bool classof(const SDNode *N) {
1104 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1108 class ConstantSDNode : public SDNode {
1109 const ConstantInt *Value;
1110 friend class SelectionDAG;
1111 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1112 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1113 DebugLoc(), getSDVTList(VT)), Value(val) {
1117 const ConstantInt *getConstantIntValue() const { return Value; }
1118 const APInt &getAPIntValue() const { return Value->getValue(); }
1119 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1120 int64_t getSExtValue() const { return Value->getSExtValue(); }
1122 bool isOne() const { return Value->isOne(); }
1123 bool isNullValue() const { return Value->isNullValue(); }
1124 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1126 static bool classof(const ConstantSDNode *) { return true; }
1127 static bool classof(const SDNode *N) {
1128 return N->getOpcode() == ISD::Constant ||
1129 N->getOpcode() == ISD::TargetConstant;
1133 class ConstantFPSDNode : public SDNode {
1134 const ConstantFP *Value;
1135 friend class SelectionDAG;
1136 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1137 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1138 DebugLoc(), getSDVTList(VT)), Value(val) {
1142 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1143 const ConstantFP *getConstantFPValue() const { return Value; }
1145 /// isZero - Return true if the value is positive or negative zero.
1146 bool isZero() const { return Value->isZero(); }
1148 /// isNaN - Return true if the value is a NaN.
1149 bool isNaN() const { return Value->isNaN(); }
1151 /// isExactlyValue - We don't rely on operator== working on double values, as
1152 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1153 /// As such, this method can be used to do an exact bit-for-bit comparison of
1154 /// two floating point values.
1156 /// We leave the version with the double argument here because it's just so
1157 /// convenient to write "2.0" and the like. Without this function we'd
1158 /// have to duplicate its logic everywhere it's called.
1159 bool isExactlyValue(double V) const {
1161 // convert is not supported on this type
1162 if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1165 Tmp.convert(Value->getValueAPF().getSemantics(),
1166 APFloat::rmNearestTiesToEven, &ignored);
1167 return isExactlyValue(Tmp);
1169 bool isExactlyValue(const APFloat& V) const;
1171 static bool isValueValidForType(EVT VT, const APFloat& Val);
1173 static bool classof(const ConstantFPSDNode *) { return true; }
1174 static bool classof(const SDNode *N) {
1175 return N->getOpcode() == ISD::ConstantFP ||
1176 N->getOpcode() == ISD::TargetConstantFP;
1180 class GlobalAddressSDNode : public SDNode {
1181 const GlobalValue *TheGlobal;
1183 unsigned char TargetFlags;
1184 friend class SelectionDAG;
1185 GlobalAddressSDNode(unsigned Opc, DebugLoc DL, const GlobalValue *GA, EVT VT,
1186 int64_t o, unsigned char TargetFlags);
1189 const GlobalValue *getGlobal() const { return TheGlobal; }
1190 int64_t getOffset() const { return Offset; }
1191 unsigned char getTargetFlags() const { return TargetFlags; }
1192 // Return the address space this GlobalAddress belongs to.
1193 unsigned getAddressSpace() const;
1195 static bool classof(const GlobalAddressSDNode *) { return true; }
1196 static bool classof(const SDNode *N) {
1197 return N->getOpcode() == ISD::GlobalAddress ||
1198 N->getOpcode() == ISD::TargetGlobalAddress ||
1199 N->getOpcode() == ISD::GlobalTLSAddress ||
1200 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1204 class FrameIndexSDNode : public SDNode {
1206 friend class SelectionDAG;
1207 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1208 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1209 DebugLoc(), getSDVTList(VT)), FI(fi) {
1213 int getIndex() const { return FI; }
1215 static bool classof(const FrameIndexSDNode *) { return true; }
1216 static bool classof(const SDNode *N) {
1217 return N->getOpcode() == ISD::FrameIndex ||
1218 N->getOpcode() == ISD::TargetFrameIndex;
1222 class JumpTableSDNode : public SDNode {
1224 unsigned char TargetFlags;
1225 friend class SelectionDAG;
1226 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1227 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1228 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1232 int getIndex() const { return JTI; }
1233 unsigned char getTargetFlags() const { return TargetFlags; }
1235 static bool classof(const JumpTableSDNode *) { return true; }
1236 static bool classof(const SDNode *N) {
1237 return N->getOpcode() == ISD::JumpTable ||
1238 N->getOpcode() == ISD::TargetJumpTable;
1242 class ConstantPoolSDNode : public SDNode {
1244 const Constant *ConstVal;
1245 MachineConstantPoolValue *MachineCPVal;
1247 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1248 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1249 unsigned char TargetFlags;
1250 friend class SelectionDAG;
1251 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1252 unsigned Align, unsigned char TF)
1253 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1255 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1256 assert((int)Offset >= 0 && "Offset is too large");
1259 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1260 EVT VT, int o, unsigned Align, unsigned char TF)
1261 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1263 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1264 assert((int)Offset >= 0 && "Offset is too large");
1265 Val.MachineCPVal = v;
1266 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1271 bool isMachineConstantPoolEntry() const {
1272 return (int)Offset < 0;
1275 const Constant *getConstVal() const {
1276 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1277 return Val.ConstVal;
1280 MachineConstantPoolValue *getMachineCPVal() const {
1281 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1282 return Val.MachineCPVal;
1285 int getOffset() const {
1286 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1289 // Return the alignment of this constant pool object, which is either 0 (for
1290 // default alignment) or the desired value.
1291 unsigned getAlignment() const { return Alignment; }
1292 unsigned char getTargetFlags() const { return TargetFlags; }
1294 const Type *getType() const;
1296 static bool classof(const ConstantPoolSDNode *) { return true; }
1297 static bool classof(const SDNode *N) {
1298 return N->getOpcode() == ISD::ConstantPool ||
1299 N->getOpcode() == ISD::TargetConstantPool;
1303 class BasicBlockSDNode : public SDNode {
1304 MachineBasicBlock *MBB;
1305 friend class SelectionDAG;
1306 /// Debug info is meaningful and potentially useful here, but we create
1307 /// blocks out of order when they're jumped to, which makes it a bit
1308 /// harder. Let's see if we need it first.
1309 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1310 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1314 MachineBasicBlock *getBasicBlock() const { return MBB; }
1316 static bool classof(const BasicBlockSDNode *) { return true; }
1317 static bool classof(const SDNode *N) {
1318 return N->getOpcode() == ISD::BasicBlock;
1322 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1324 class BuildVectorSDNode : public SDNode {
1325 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1326 explicit BuildVectorSDNode(); // Do not implement
1328 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1329 /// smallest element size that splats the vector. If MinSplatBits is
1330 /// nonzero, the element size must be at least that large. Note that the
1331 /// splat element may be the entire vector (i.e., a one element vector).
1332 /// Returns the splat element value in SplatValue. Any undefined bits in
1333 /// that value are zero, and the corresponding bits in the SplatUndef mask
1334 /// are set. The SplatBitSize value is set to the splat element size in
1335 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1336 /// undefined. isBigEndian describes the endianness of the target.
1337 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1338 unsigned &SplatBitSize, bool &HasAnyUndefs,
1339 unsigned MinSplatBits = 0, bool isBigEndian = false);
1341 static inline bool classof(const BuildVectorSDNode *) { return true; }
1342 static inline bool classof(const SDNode *N) {
1343 return N->getOpcode() == ISD::BUILD_VECTOR;
1347 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1348 /// used when the SelectionDAG needs to make a simple reference to something
1349 /// in the LLVM IR representation.
1351 class SrcValueSDNode : public SDNode {
1353 friend class SelectionDAG;
1354 /// Create a SrcValue for a general value.
1355 explicit SrcValueSDNode(const Value *v)
1356 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1359 /// getValue - return the contained Value.
1360 const Value *getValue() const { return V; }
1362 static bool classof(const SrcValueSDNode *) { return true; }
1363 static bool classof(const SDNode *N) {
1364 return N->getOpcode() == ISD::SRCVALUE;
1368 class MDNodeSDNode : public SDNode {
1370 friend class SelectionDAG;
1371 explicit MDNodeSDNode(const MDNode *md)
1372 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1375 const MDNode *getMD() const { return MD; }
1377 static bool classof(const MDNodeSDNode *) { return true; }
1378 static bool classof(const SDNode *N) {
1379 return N->getOpcode() == ISD::MDNODE_SDNODE;
1384 class RegisterSDNode : public SDNode {
1386 friend class SelectionDAG;
1387 RegisterSDNode(unsigned reg, EVT VT)
1388 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1392 unsigned getReg() const { return Reg; }
1394 static bool classof(const RegisterSDNode *) { return true; }
1395 static bool classof(const SDNode *N) {
1396 return N->getOpcode() == ISD::Register;
1400 class BlockAddressSDNode : public SDNode {
1401 const BlockAddress *BA;
1402 unsigned char TargetFlags;
1403 friend class SelectionDAG;
1404 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1405 unsigned char Flags)
1406 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1407 BA(ba), TargetFlags(Flags) {
1410 const BlockAddress *getBlockAddress() const { return BA; }
1411 unsigned char getTargetFlags() const { return TargetFlags; }
1413 static bool classof(const BlockAddressSDNode *) { return true; }
1414 static bool classof(const SDNode *N) {
1415 return N->getOpcode() == ISD::BlockAddress ||
1416 N->getOpcode() == ISD::TargetBlockAddress;
1420 class EHLabelSDNode : public SDNode {
1423 friend class SelectionDAG;
1424 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1425 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1426 InitOperands(&Chain, ch);
1429 MCSymbol *getLabel() const { return Label; }
1431 static bool classof(const EHLabelSDNode *) { return true; }
1432 static bool classof(const SDNode *N) {
1433 return N->getOpcode() == ISD::EH_LABEL;
1437 class ExternalSymbolSDNode : public SDNode {
1439 unsigned char TargetFlags;
1441 friend class SelectionDAG;
1442 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1443 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1444 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1448 const char *getSymbol() const { return Symbol; }
1449 unsigned char getTargetFlags() const { return TargetFlags; }
1451 static bool classof(const ExternalSymbolSDNode *) { return true; }
1452 static bool classof(const SDNode *N) {
1453 return N->getOpcode() == ISD::ExternalSymbol ||
1454 N->getOpcode() == ISD::TargetExternalSymbol;
1458 class CondCodeSDNode : public SDNode {
1459 ISD::CondCode Condition;
1460 friend class SelectionDAG;
1461 explicit CondCodeSDNode(ISD::CondCode Cond)
1462 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1467 ISD::CondCode get() const { return Condition; }
1469 static bool classof(const CondCodeSDNode *) { return true; }
1470 static bool classof(const SDNode *N) {
1471 return N->getOpcode() == ISD::CONDCODE;
1475 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1476 /// future and most targets don't support it.
1477 class CvtRndSatSDNode : public SDNode {
1478 ISD::CvtCode CvtCode;
1479 friend class SelectionDAG;
1480 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1481 unsigned NumOps, ISD::CvtCode Code)
1482 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1484 assert(NumOps == 5 && "wrong number of operations");
1487 ISD::CvtCode getCvtCode() const { return CvtCode; }
1489 static bool classof(const CvtRndSatSDNode *) { return true; }
1490 static bool classof(const SDNode *N) {
1491 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1495 /// VTSDNode - This class is used to represent EVT's, which are used
1496 /// to parameterize some operations.
1497 class VTSDNode : public SDNode {
1499 friend class SelectionDAG;
1500 explicit VTSDNode(EVT VT)
1501 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1506 EVT getVT() const { return ValueType; }
1508 static bool classof(const VTSDNode *) { return true; }
1509 static bool classof(const SDNode *N) {
1510 return N->getOpcode() == ISD::VALUETYPE;
1514 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1516 class LSBaseSDNode : public MemSDNode {
1517 //! Operand array for load and store
1519 \note Moving this array to the base class captures more
1520 common functionality shared between LoadSDNode and
1525 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1526 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1527 EVT MemVT, MachineMemOperand *MMO)
1528 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1529 SubclassData |= AM << 2;
1530 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1531 InitOperands(Ops, Operands, numOperands);
1532 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1533 "Only indexed loads and stores have a non-undef offset operand");
1536 const SDValue &getOffset() const {
1537 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1540 /// getAddressingMode - Return the addressing mode for this load or store:
1541 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1542 ISD::MemIndexedMode getAddressingMode() const {
1543 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1546 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1547 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1549 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1550 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1552 static bool classof(const LSBaseSDNode *) { return true; }
1553 static bool classof(const SDNode *N) {
1554 return N->getOpcode() == ISD::LOAD ||
1555 N->getOpcode() == ISD::STORE;
1559 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1561 class LoadSDNode : public LSBaseSDNode {
1562 friend class SelectionDAG;
1563 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1564 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1565 MachineMemOperand *MMO)
1566 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1567 VTs, AM, MemVT, MMO) {
1568 SubclassData |= (unsigned short)ETy;
1569 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1570 assert(readMem() && "Load MachineMemOperand is not a load!");
1571 assert(!writeMem() && "Load MachineMemOperand is a store!");
1575 /// getExtensionType - Return whether this is a plain node,
1576 /// or one of the varieties of value-extending loads.
1577 ISD::LoadExtType getExtensionType() const {
1578 return ISD::LoadExtType(SubclassData & 3);
1581 const SDValue &getBasePtr() const { return getOperand(1); }
1582 const SDValue &getOffset() const { return getOperand(2); }
1584 static bool classof(const LoadSDNode *) { return true; }
1585 static bool classof(const SDNode *N) {
1586 return N->getOpcode() == ISD::LOAD;
1590 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1592 class StoreSDNode : public LSBaseSDNode {
1593 friend class SelectionDAG;
1594 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1595 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1596 MachineMemOperand *MMO)
1597 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1598 VTs, AM, MemVT, MMO) {
1599 SubclassData |= (unsigned short)isTrunc;
1600 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1601 assert(!readMem() && "Store MachineMemOperand is a load!");
1602 assert(writeMem() && "Store MachineMemOperand is not a store!");
1606 /// isTruncatingStore - Return true if the op does a truncation before store.
1607 /// For integers this is the same as doing a TRUNCATE and storing the result.
1608 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1609 bool isTruncatingStore() const { return SubclassData & 1; }
1611 const SDValue &getValue() const { return getOperand(1); }
1612 const SDValue &getBasePtr() const { return getOperand(2); }
1613 const SDValue &getOffset() const { return getOperand(3); }
1615 static bool classof(const StoreSDNode *) { return true; }
1616 static bool classof(const SDNode *N) {
1617 return N->getOpcode() == ISD::STORE;
1621 /// MachineSDNode - An SDNode that represents everything that will be needed
1622 /// to construct a MachineInstr. These nodes are created during the
1623 /// instruction selection proper phase.
1625 class MachineSDNode : public SDNode {
1627 typedef MachineMemOperand **mmo_iterator;
1630 friend class SelectionDAG;
1631 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1632 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1634 /// LocalOperands - Operands for this instruction, if they fit here. If
1635 /// they don't, this field is unused.
1636 SDUse LocalOperands[4];
1638 /// MemRefs - Memory reference descriptions for this instruction.
1639 mmo_iterator MemRefs;
1640 mmo_iterator MemRefsEnd;
1643 mmo_iterator memoperands_begin() const { return MemRefs; }
1644 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1645 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1647 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1648 /// list. This does not transfer ownership.
1649 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1650 MemRefs = NewMemRefs;
1651 MemRefsEnd = NewMemRefsEnd;
1654 static bool classof(const MachineSDNode *) { return true; }
1655 static bool classof(const SDNode *N) {
1656 return N->isMachineOpcode();
1660 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1661 SDNode, ptrdiff_t> {
1665 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1667 bool operator==(const SDNodeIterator& x) const {
1668 return Operand == x.Operand;
1670 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1672 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1673 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1674 Operand = I.Operand;
1678 pointer operator*() const {
1679 return Node->getOperand(Operand).getNode();
1681 pointer operator->() const { return operator*(); }
1683 SDNodeIterator& operator++() { // Preincrement
1687 SDNodeIterator operator++(int) { // Postincrement
1688 SDNodeIterator tmp = *this; ++*this; return tmp;
1690 size_t operator-(SDNodeIterator Other) const {
1691 assert(Node == Other.Node &&
1692 "Cannot compare iterators of two different nodes!");
1693 return Operand - Other.Operand;
1696 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1697 static SDNodeIterator end (SDNode *N) {
1698 return SDNodeIterator(N, N->getNumOperands());
1701 unsigned getOperand() const { return Operand; }
1702 const SDNode *getNode() const { return Node; }
1705 template <> struct GraphTraits<SDNode*> {
1706 typedef SDNode NodeType;
1707 typedef SDNodeIterator ChildIteratorType;
1708 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1709 static inline ChildIteratorType child_begin(NodeType *N) {
1710 return SDNodeIterator::begin(N);
1712 static inline ChildIteratorType child_end(NodeType *N) {
1713 return SDNodeIterator::end(N);
1717 /// LargestSDNode - The largest SDNode class.
1719 typedef LoadSDNode LargestSDNode;
1721 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1724 typedef GlobalAddressSDNode MostAlignedSDNode;
1727 /// isNormalLoad - Returns true if the specified node is a non-extending
1728 /// and unindexed load.
1729 inline bool isNormalLoad(const SDNode *N) {
1730 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1731 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1732 Ld->getAddressingMode() == ISD::UNINDEXED;
1735 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1737 inline bool isNON_EXTLoad(const SDNode *N) {
1738 return isa<LoadSDNode>(N) &&
1739 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1742 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1744 inline bool isEXTLoad(const SDNode *N) {
1745 return isa<LoadSDNode>(N) &&
1746 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1749 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1751 inline bool isSEXTLoad(const SDNode *N) {
1752 return isa<LoadSDNode>(N) &&
1753 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1756 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1758 inline bool isZEXTLoad(const SDNode *N) {
1759 return isa<LoadSDNode>(N) &&
1760 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1763 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1765 inline bool isUNINDEXEDLoad(const SDNode *N) {
1766 return isa<LoadSDNode>(N) &&
1767 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1770 /// isNormalStore - Returns true if the specified node is a non-truncating
1771 /// and unindexed store.
1772 inline bool isNormalStore(const SDNode *N) {
1773 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1774 return St && !St->isTruncatingStore() &&
1775 St->getAddressingMode() == ISD::UNINDEXED;
1778 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1780 inline bool isNON_TRUNCStore(const SDNode *N) {
1781 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1784 /// isTRUNCStore - Returns true if the specified node is a truncating
1786 inline bool isTRUNCStore(const SDNode *N) {
1787 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1790 /// isUNINDEXEDStore - Returns true if the specified node is an
1791 /// unindexed store.
1792 inline bool isUNINDEXEDStore(const SDNode *N) {
1793 return isa<StoreSDNode>(N) &&
1794 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1798 } // end llvm namespace