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 /// getValueSizeInBits - Returns the size of the value in bits.
135 unsigned getValueSizeInBits() const {
136 return getValueType().getSizeInBits();
139 // Forwarding methods - These forward to the corresponding methods in SDNode.
140 inline unsigned getOpcode() const;
141 inline unsigned getNumOperands() const;
142 inline const SDValue &getOperand(unsigned i) const;
143 inline uint64_t getConstantOperandVal(unsigned i) const;
144 inline bool isTargetMemoryOpcode() const;
145 inline bool isTargetOpcode() const;
146 inline bool isMachineOpcode() const;
147 inline unsigned getMachineOpcode() const;
148 inline const DebugLoc getDebugLoc() const;
149 inline void dump() const;
150 inline void dumpr() const;
152 /// reachesChainWithoutSideEffects - Return true if this operand (which must
153 /// be a chain) reaches the specified operand without crossing any
154 /// side-effecting instructions. In practice, this looks through token
155 /// factors and non-volatile loads. In order to remain efficient, this only
156 /// looks a couple of nodes in, it does not do an exhaustive search.
157 bool reachesChainWithoutSideEffects(SDValue Dest,
158 unsigned Depth = 2) const;
160 /// use_empty - Return true if there are no nodes using value ResNo
163 inline bool use_empty() const;
165 /// hasOneUse - Return true if there is exactly one node using value
168 inline bool hasOneUse() const;
172 template<> struct DenseMapInfo<SDValue> {
173 static inline SDValue getEmptyKey() {
174 return SDValue((SDNode*)-1, -1U);
176 static inline SDValue getTombstoneKey() {
177 return SDValue((SDNode*)-1, 0);
179 static unsigned getHashValue(const SDValue &Val) {
180 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
181 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
183 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
187 template <> struct isPodLike<SDValue> { static const bool value = true; };
190 /// simplify_type specializations - Allow casting operators to work directly on
191 /// SDValues as if they were SDNode*'s.
192 template<> struct simplify_type<SDValue> {
193 typedef SDNode* SimpleType;
194 static SimpleType getSimplifiedValue(const SDValue &Val) {
195 return static_cast<SimpleType>(Val.getNode());
198 template<> struct simplify_type<const SDValue> {
199 typedef SDNode* SimpleType;
200 static SimpleType getSimplifiedValue(const SDValue &Val) {
201 return static_cast<SimpleType>(Val.getNode());
205 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
206 /// which records the SDNode being used and the result number, a
207 /// pointer to the SDNode using the value, and Next and Prev pointers,
208 /// which link together all the uses of an SDNode.
211 /// Val - The value being used.
213 /// User - The user of this value.
215 /// Prev, Next - Pointers to the uses list of the SDNode referred by
219 SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
220 void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
223 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
225 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
226 operator const SDValue&() const { return Val; }
228 /// If implicit conversion to SDValue doesn't work, the get() method returns
230 const SDValue &get() const { return Val; }
232 /// getUser - This returns the SDNode that contains this Use.
233 SDNode *getUser() { return User; }
235 /// getNext - Get the next SDUse in the use list.
236 SDUse *getNext() const { return Next; }
238 /// getNode - Convenience function for get().getNode().
239 SDNode *getNode() const { return Val.getNode(); }
240 /// getResNo - Convenience function for get().getResNo().
241 unsigned getResNo() const { return Val.getResNo(); }
242 /// getValueType - Convenience function for get().getValueType().
243 EVT getValueType() const { return Val.getValueType(); }
245 /// operator== - Convenience function for get().operator==
246 bool operator==(const SDValue &V) const {
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 {
261 friend class SelectionDAG;
264 void setUser(SDNode *p) { User = p; }
266 /// set - Remove this use from its existing use list, assign it the
267 /// given value, and add it to the new value's node's use list.
268 inline void set(const SDValue &V);
269 /// setInitial - like set, but only supports initializing a newly-allocated
270 /// SDUse with a non-null value.
271 inline void setInitial(const SDValue &V);
272 /// setNode - like set, but only sets the Node portion of the value,
273 /// leaving the ResNo portion unmodified.
274 inline void setNode(SDNode *N);
276 void addToList(SDUse **List) {
278 if (Next) Next->Prev = &Next;
283 void removeFromList() {
285 if (Next) Next->Prev = Prev;
289 /// simplify_type specializations - Allow casting operators to work directly on
290 /// SDValues as if they were SDNode*'s.
291 template<> struct simplify_type<SDUse> {
292 typedef SDNode* SimpleType;
293 static SimpleType getSimplifiedValue(const SDUse &Val) {
294 return static_cast<SimpleType>(Val.getNode());
297 template<> struct simplify_type<const SDUse> {
298 typedef SDNode* SimpleType;
299 static SimpleType getSimplifiedValue(const SDUse &Val) {
300 return static_cast<SimpleType>(Val.getNode());
305 /// SDNode - Represents one node in the SelectionDAG.
307 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
309 /// NodeType - The operation that this node performs.
313 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
314 /// then they will be delete[]'d when the node is destroyed.
315 uint16_t OperandsNeedDelete : 1;
317 /// HasDebugValue - This tracks whether this node has one or more dbg_value
318 /// nodes corresponding to it.
319 uint16_t HasDebugValue : 1;
322 /// SubclassData - This member is defined by this class, but is not used for
323 /// anything. Subclasses can use it to hold whatever state they find useful.
324 /// This field is initialized to zero by the ctor.
325 uint16_t SubclassData : 14;
328 /// NodeId - Unique id per SDNode in the DAG.
331 /// OperandList - The values that are used by this operation.
335 /// ValueList - The types of the values this node defines. SDNode's may
336 /// define multiple values simultaneously.
337 const EVT *ValueList;
339 /// UseList - List of uses for this SDNode.
342 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
343 unsigned short NumOperands, NumValues;
345 /// debugLoc - source line information.
348 /// getValueTypeList - Return a pointer to the specified value type.
349 static const EVT *getValueTypeList(EVT VT);
351 friend class SelectionDAG;
352 friend struct ilist_traits<SDNode>;
355 //===--------------------------------------------------------------------===//
359 /// getOpcode - Return the SelectionDAG opcode value for this node. For
360 /// pre-isel nodes (those for which isMachineOpcode returns false), these
361 /// are the opcode values in the ISD and <target>ISD namespaces. For
362 /// post-isel opcodes, see getMachineOpcode.
363 unsigned getOpcode() const { return (unsigned short)NodeType; }
365 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
366 /// \<target\>ISD namespace).
367 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
369 /// isTargetMemoryOpcode - Test if this node has a target-specific
370 /// memory-referencing opcode (in the \<target\>ISD namespace and
371 /// greater than FIRST_TARGET_MEMORY_OPCODE).
372 bool isTargetMemoryOpcode() const {
373 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
376 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
377 /// corresponding to a MachineInstr opcode.
378 bool isMachineOpcode() const { return NodeType < 0; }
380 /// getMachineOpcode - This may only be called if isMachineOpcode returns
381 /// true. It returns the MachineInstr opcode value that the node's opcode
383 unsigned getMachineOpcode() const {
384 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
388 /// getHasDebugValue - get this bit.
389 bool getHasDebugValue() const { return HasDebugValue; }
391 /// setHasDebugValue - set this bit.
392 void setHasDebugValue(bool b) { HasDebugValue = b; }
394 /// use_empty - Return true if there are no uses of this node.
396 bool use_empty() const { return UseList == NULL; }
398 /// hasOneUse - Return true if there is exactly one use of this node.
400 bool hasOneUse() const {
401 return !use_empty() && llvm::next(use_begin()) == use_end();
404 /// use_size - Return the number of uses of this node. This method takes
405 /// time proportional to the number of uses.
407 size_t use_size() const { return std::distance(use_begin(), use_end()); }
409 /// getNodeId - Return the unique node id.
411 int getNodeId() const { return NodeId; }
413 /// setNodeId - Set unique node id.
414 void setNodeId(int Id) { NodeId = Id; }
416 /// getDebugLoc - Return the source location info.
417 const DebugLoc getDebugLoc() const { return debugLoc; }
419 /// setDebugLoc - Set source location info. Try to avoid this, putting
420 /// it in the constructor is preferable.
421 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
423 /// use_iterator - This class provides iterator support for SDUse
424 /// operands that use a specific SDNode.
426 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
428 explicit use_iterator(SDUse *op) : Op(op) {
432 typedef std::iterator<std::forward_iterator_tag,
433 SDUse, ptrdiff_t>::reference reference;
434 typedef std::iterator<std::forward_iterator_tag,
435 SDUse, ptrdiff_t>::pointer pointer;
437 use_iterator(const use_iterator &I) : Op(I.Op) {}
438 use_iterator() : Op(0) {}
440 bool operator==(const use_iterator &x) const {
443 bool operator!=(const use_iterator &x) const {
444 return !operator==(x);
447 /// atEnd - return true if this iterator is at the end of uses list.
448 bool atEnd() const { return Op == 0; }
450 // Iterator traversal: forward iteration only.
451 use_iterator &operator++() { // Preincrement
452 assert(Op && "Cannot increment end iterator!");
457 use_iterator operator++(int) { // Postincrement
458 use_iterator tmp = *this; ++*this; return tmp;
461 /// Retrieve a pointer to the current user node.
462 SDNode *operator*() const {
463 assert(Op && "Cannot dereference end iterator!");
464 return Op->getUser();
467 SDNode *operator->() const { return operator*(); }
469 SDUse &getUse() const { return *Op; }
471 /// getOperandNo - Retrieve the operand # of this use in its user.
473 unsigned getOperandNo() const {
474 assert(Op && "Cannot dereference end iterator!");
475 return (unsigned)(Op - Op->getUser()->OperandList);
479 /// use_begin/use_end - Provide iteration support to walk over all uses
482 use_iterator use_begin() const {
483 return use_iterator(UseList);
486 static use_iterator use_end() { return use_iterator(0); }
489 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
490 /// indicated value. This method ignores uses of other values defined by this
492 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
494 /// hasAnyUseOfValue - Return true if there are any use of the indicated
495 /// value. This method ignores uses of other values defined by this operation.
496 bool hasAnyUseOfValue(unsigned Value) const;
498 /// isOnlyUserOf - Return true if this node is the only use of N.
500 bool isOnlyUserOf(SDNode *N) const;
502 /// isOperandOf - Return true if this node is an operand of N.
504 bool isOperandOf(SDNode *N) const;
506 /// isPredecessorOf - Return true if this node is a predecessor of N.
507 /// NOTE: Implemented on top of hasPredecessor and every bit as
508 /// expensive. Use carefully.
509 bool isPredecessorOf(const SDNode *N) const { return N->hasPredecessor(this); }
511 /// hasPredecessor - 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 /// NOTE: This is an expensive method. Use it carefully.
515 bool hasPredecessor(const SDNode *N) const;
517 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
518 /// N is either an operand of this node, or can be reached by recursively
519 /// traversing up the operands.
520 /// In this helper the Visited and worklist sets are held externally to
521 /// cache predecessors over multiple invocations. If you want to test for
522 /// multiple predecessors this method is preferable to multiple calls to
523 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
525 /// NOTE: This is still very expensive. Use carefully.
526 bool hasPredecessorHelper(const SDNode *N,
527 SmallPtrSet<const SDNode *, 32> &Visited,
528 SmallVector<const SDNode *, 16> &Worklist) const;
530 /// getNumOperands - Return the number of values used by this operation.
532 unsigned getNumOperands() const { return NumOperands; }
534 /// getConstantOperandVal - Helper method returns the integer value of a
535 /// ConstantSDNode operand.
536 uint64_t getConstantOperandVal(unsigned Num) const;
538 const SDValue &getOperand(unsigned Num) const {
539 assert(Num < NumOperands && "Invalid child # of SDNode!");
540 return OperandList[Num];
543 typedef SDUse* op_iterator;
544 op_iterator op_begin() const { return OperandList; }
545 op_iterator op_end() const { return OperandList+NumOperands; }
547 SDVTList getVTList() const {
548 SDVTList X = { ValueList, NumValues };
552 /// getGluedNode - If this node has a glue operand, return the node
553 /// to which the glue operand points. Otherwise return NULL.
554 SDNode *getGluedNode() const {
555 if (getNumOperands() != 0 &&
556 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
557 return getOperand(getNumOperands()-1).getNode();
561 // If this is a pseudo op, like copyfromreg, look to see if there is a
562 // real target node glued to it. If so, return the target node.
563 const SDNode *getGluedMachineNode() const {
564 const SDNode *FoundNode = this;
566 // Climb up glue edges until a machine-opcode node is found, or the
567 // end of the chain is reached.
568 while (!FoundNode->isMachineOpcode()) {
569 const SDNode *N = FoundNode->getGluedNode();
577 /// getGluedUser - If this node has a glue value with a user, return
578 /// the user (there is at most one). Otherwise return NULL.
579 SDNode *getGluedUser() const {
580 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
581 if (UI.getUse().get().getValueType() == MVT::Glue)
586 /// getNumValues - Return the number of values defined/returned by this
589 unsigned getNumValues() const { return NumValues; }
591 /// getValueType - Return the type of a specified result.
593 EVT getValueType(unsigned ResNo) const {
594 assert(ResNo < NumValues && "Illegal result number!");
595 return ValueList[ResNo];
598 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
600 unsigned getValueSizeInBits(unsigned ResNo) const {
601 return getValueType(ResNo).getSizeInBits();
604 typedef const EVT* value_iterator;
605 value_iterator value_begin() const { return ValueList; }
606 value_iterator value_end() const { return ValueList+NumValues; }
608 /// getOperationName - Return the opcode of this operation for printing.
610 std::string getOperationName(const SelectionDAG *G = 0) const;
611 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
612 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
613 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
614 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
615 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
617 /// printrFull - Print a SelectionDAG node and all children down to
618 /// the leaves. The given SelectionDAG allows target-specific nodes
619 /// to be printed in human-readable form. Unlike printr, this will
620 /// print the whole DAG, including children that appear multiple
623 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
625 /// printrWithDepth - Print a SelectionDAG node and children up to
626 /// depth "depth." The given SelectionDAG allows target-specific
627 /// nodes to be printed in human-readable form. Unlike printr, this
628 /// will print children that appear multiple times wherever they are
631 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
632 unsigned depth = 100) const;
635 /// dump - Dump this node, for debugging.
638 /// dumpr - Dump (recursively) this node and its use-def subgraph.
641 /// dump - Dump this node, for debugging.
642 /// The given SelectionDAG allows target-specific nodes to be printed
643 /// in human-readable form.
644 void dump(const SelectionDAG *G) const;
646 /// dumpr - Dump (recursively) this node and its use-def subgraph.
647 /// The given SelectionDAG allows target-specific nodes to be printed
648 /// in human-readable form.
649 void dumpr(const SelectionDAG *G) const;
651 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
652 /// target-specific nodes to be printed in human-readable form.
653 /// Unlike dumpr, this will print the whole DAG, including children
654 /// that appear multiple times.
656 void dumprFull(const SelectionDAG *G = 0) const;
658 /// dumprWithDepth - printrWithDepth to dbgs(). The given
659 /// SelectionDAG allows target-specific nodes to be printed in
660 /// human-readable form. Unlike dumpr, this will print children
661 /// that appear multiple times wherever they are used.
663 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
665 /// Profile - Gather unique data for the node.
667 void Profile(FoldingSetNodeID &ID) const;
669 /// addUse - This method should only be used by the SDUse class.
671 void addUse(SDUse &U) { U.addToList(&UseList); }
674 static SDVTList getSDVTList(EVT VT) {
675 SDVTList Ret = { getValueTypeList(VT), 1 };
679 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
681 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
682 SubclassData(0), NodeId(-1),
683 OperandList(NumOps ? new SDUse[NumOps] : 0),
684 ValueList(VTs.VTs), UseList(NULL),
685 NumOperands(NumOps), NumValues(VTs.NumVTs),
687 for (unsigned i = 0; i != NumOps; ++i) {
688 OperandList[i].setUser(this);
689 OperandList[i].setInitial(Ops[i]);
691 checkForCycles(this);
694 /// This constructor adds no operands itself; operands can be
695 /// set later with InitOperands.
696 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
697 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
698 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
699 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
702 /// InitOperands - Initialize the operands list of this with 1 operand.
703 void InitOperands(SDUse *Ops, const SDValue &Op0) {
704 Ops[0].setUser(this);
705 Ops[0].setInitial(Op0);
708 checkForCycles(this);
711 /// InitOperands - Initialize the operands list of this with 2 operands.
712 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
713 Ops[0].setUser(this);
714 Ops[0].setInitial(Op0);
715 Ops[1].setUser(this);
716 Ops[1].setInitial(Op1);
719 checkForCycles(this);
722 /// InitOperands - Initialize the operands list of this with 3 operands.
723 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
724 const SDValue &Op2) {
725 Ops[0].setUser(this);
726 Ops[0].setInitial(Op0);
727 Ops[1].setUser(this);
728 Ops[1].setInitial(Op1);
729 Ops[2].setUser(this);
730 Ops[2].setInitial(Op2);
733 checkForCycles(this);
736 /// InitOperands - Initialize the operands list of this with 4 operands.
737 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
738 const SDValue &Op2, const SDValue &Op3) {
739 Ops[0].setUser(this);
740 Ops[0].setInitial(Op0);
741 Ops[1].setUser(this);
742 Ops[1].setInitial(Op1);
743 Ops[2].setUser(this);
744 Ops[2].setInitial(Op2);
745 Ops[3].setUser(this);
746 Ops[3].setInitial(Op3);
749 checkForCycles(this);
752 /// InitOperands - Initialize the operands list of this with N operands.
753 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
754 for (unsigned i = 0; i != N; ++i) {
755 Ops[i].setUser(this);
756 Ops[i].setInitial(Vals[i]);
760 checkForCycles(this);
763 /// DropOperands - Release the operands and set this node to have
769 // Define inline functions from the SDValue class.
771 inline unsigned SDValue::getOpcode() const {
772 return Node->getOpcode();
774 inline EVT SDValue::getValueType() const {
775 return Node->getValueType(ResNo);
777 inline unsigned SDValue::getNumOperands() const {
778 return Node->getNumOperands();
780 inline const SDValue &SDValue::getOperand(unsigned i) const {
781 return Node->getOperand(i);
783 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
784 return Node->getConstantOperandVal(i);
786 inline bool SDValue::isTargetOpcode() const {
787 return Node->isTargetOpcode();
789 inline bool SDValue::isTargetMemoryOpcode() const {
790 return Node->isTargetMemoryOpcode();
792 inline bool SDValue::isMachineOpcode() const {
793 return Node->isMachineOpcode();
795 inline unsigned SDValue::getMachineOpcode() const {
796 return Node->getMachineOpcode();
798 inline bool SDValue::use_empty() const {
799 return !Node->hasAnyUseOfValue(ResNo);
801 inline bool SDValue::hasOneUse() const {
802 return Node->hasNUsesOfValue(1, ResNo);
804 inline const DebugLoc SDValue::getDebugLoc() const {
805 return Node->getDebugLoc();
807 inline void SDValue::dump() const {
810 inline void SDValue::dumpr() const {
811 return Node->dumpr();
813 // Define inline functions from the SDUse class.
815 inline void SDUse::set(const SDValue &V) {
816 if (Val.getNode()) removeFromList();
818 if (V.getNode()) V.getNode()->addUse(*this);
821 inline void SDUse::setInitial(const SDValue &V) {
823 V.getNode()->addUse(*this);
826 inline void SDUse::setNode(SDNode *N) {
827 if (Val.getNode()) removeFromList();
829 if (N) N->addUse(*this);
832 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
833 /// to allow co-allocation of node operands with the node itself.
834 class UnarySDNode : public SDNode {
837 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
838 : SDNode(Opc, dl, VTs) {
839 InitOperands(&Op, X);
843 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
844 /// to allow co-allocation of node operands with the node itself.
845 class BinarySDNode : public SDNode {
848 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
849 : SDNode(Opc, dl, VTs) {
850 InitOperands(Ops, X, Y);
854 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
855 /// to allow co-allocation of node operands with the node itself.
856 class TernarySDNode : public SDNode {
859 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
861 : SDNode(Opc, dl, VTs) {
862 InitOperands(Ops, X, Y, Z);
867 /// HandleSDNode - This class is used to form a handle around another node that
868 /// is persistent and is updated across invocations of replaceAllUsesWith on its
869 /// operand. This node should be directly created by end-users and not added to
870 /// the AllNodes list.
871 class HandleSDNode : public SDNode {
874 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
876 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
877 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
879 explicit HandleSDNode(SDValue X)
881 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
882 InitOperands(&Op, X);
885 const SDValue &getValue() const { return Op; }
888 /// Abstact virtual class for operations for memory operations
889 class MemSDNode : public SDNode {
891 // MemoryVT - VT of in-memory value.
895 /// MMO - Memory reference information.
896 MachineMemOperand *MMO;
899 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
900 MachineMemOperand *MMO);
902 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
903 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
905 bool readMem() const { return MMO->isLoad(); }
906 bool writeMem() const { return MMO->isStore(); }
908 /// Returns alignment and volatility of the memory access
909 unsigned getOriginalAlignment() const {
910 return MMO->getBaseAlignment();
912 unsigned getAlignment() const {
913 return MMO->getAlignment();
916 /// getRawSubclassData - Return the SubclassData value, which contains an
917 /// encoding of the volatile flag, as well as bits used by subclasses. This
918 /// function should only be used to compute a FoldingSetNodeID value.
919 unsigned getRawSubclassData() const {
923 // We access subclass data here so that we can check consistency
924 // with MachineMemOperand information.
925 bool isVolatile() const { return (SubclassData >> 5) & 1; }
926 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
927 bool isInvariant() const { return (SubclassData >> 7) & 1; }
929 AtomicOrdering getOrdering() const {
930 return AtomicOrdering((SubclassData >> 8) & 15);
932 SynchronizationScope getSynchScope() const {
933 return SynchronizationScope((SubclassData >> 12) & 1);
936 /// Returns the SrcValue and offset that describes the location of the access
937 const Value *getSrcValue() const { return MMO->getValue(); }
938 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
940 /// Returns the TBAAInfo that describes the dereference.
941 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
943 /// Returns the Ranges that describes the dereference.
944 const MDNode *getRanges() const { return MMO->getRanges(); }
946 /// getMemoryVT - Return the type of the in-memory value.
947 EVT getMemoryVT() const { return MemoryVT; }
949 /// getMemOperand - Return a MachineMemOperand object describing the memory
950 /// reference performed by operation.
951 MachineMemOperand *getMemOperand() const { return MMO; }
953 const MachinePointerInfo &getPointerInfo() const {
954 return MMO->getPointerInfo();
957 /// getAddressSpace - Return the address space for the associated pointer
958 unsigned getAddressSpace() const {
959 return getPointerInfo().getAddrSpace();
962 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
963 /// to reflect the alignment of NewMMO, if it has a greater alignment.
964 /// This must only be used when the new alignment applies to all users of
965 /// this MachineMemOperand.
966 void refineAlignment(const MachineMemOperand *NewMMO) {
967 MMO->refineAlignment(NewMMO);
970 const SDValue &getChain() const { return getOperand(0); }
971 const SDValue &getBasePtr() const {
972 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
975 // Methods to support isa and dyn_cast
976 static bool classof(const SDNode *N) {
977 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
978 // with either an intrinsic or a target opcode.
979 return N->getOpcode() == ISD::LOAD ||
980 N->getOpcode() == ISD::STORE ||
981 N->getOpcode() == ISD::PREFETCH ||
982 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
983 N->getOpcode() == ISD::ATOMIC_SWAP ||
984 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
985 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
986 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
987 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
988 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
989 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
990 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
991 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
992 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
993 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
994 N->getOpcode() == ISD::ATOMIC_LOAD ||
995 N->getOpcode() == ISD::ATOMIC_STORE ||
996 N->isTargetMemoryOpcode();
1000 /// AtomicSDNode - A SDNode reprenting atomic operations.
1002 class AtomicSDNode : public MemSDNode {
1005 void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
1006 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1007 assert((Ordering & 15) == Ordering &&
1008 "Ordering may not require more than 4 bits!");
1009 assert((SynchScope & 1) == SynchScope &&
1010 "SynchScope may not require more than 1 bit!");
1011 SubclassData |= Ordering << 8;
1012 SubclassData |= SynchScope << 12;
1013 assert(getOrdering() == Ordering && "Ordering encoding error!");
1014 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1018 // Opc: opcode for atomic
1019 // VTL: value type list
1020 // Chain: memory chain for operaand
1021 // Ptr: address to update as a SDValue
1022 // Cmp: compare value
1024 // SrcVal: address to update as a Value (used for MemOperand)
1025 // Align: alignment of memory
1026 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1027 SDValue Chain, SDValue Ptr,
1028 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1029 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1030 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1031 InitAtomic(Ordering, SynchScope);
1032 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1034 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1035 SDValue Chain, SDValue Ptr,
1036 SDValue Val, MachineMemOperand *MMO,
1037 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1038 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1039 InitAtomic(Ordering, SynchScope);
1040 InitOperands(Ops, Chain, Ptr, Val);
1042 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1043 SDValue Chain, SDValue Ptr,
1044 MachineMemOperand *MMO,
1045 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1046 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1047 InitAtomic(Ordering, SynchScope);
1048 InitOperands(Ops, Chain, Ptr);
1051 const SDValue &getBasePtr() const { return getOperand(1); }
1052 const SDValue &getVal() const { return getOperand(2); }
1054 bool isCompareAndSwap() const {
1055 unsigned Op = getOpcode();
1056 return Op == ISD::ATOMIC_CMP_SWAP;
1059 // Methods to support isa and dyn_cast
1060 static bool classof(const SDNode *N) {
1061 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1062 N->getOpcode() == ISD::ATOMIC_SWAP ||
1063 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1064 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1065 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1066 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1067 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1068 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1069 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1070 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1071 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1072 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1073 N->getOpcode() == ISD::ATOMIC_LOAD ||
1074 N->getOpcode() == ISD::ATOMIC_STORE;
1078 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1079 /// memory and need an associated MachineMemOperand. Its opcode may be
1080 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1081 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1082 class MemIntrinsicSDNode : public MemSDNode {
1084 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1085 const SDValue *Ops, unsigned NumOps,
1086 EVT MemoryVT, MachineMemOperand *MMO)
1087 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1090 // Methods to support isa and dyn_cast
1091 static bool classof(const SDNode *N) {
1092 // We lower some target intrinsics to their target opcode
1093 // early a node with a target opcode can be of this class
1094 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1095 N->getOpcode() == ISD::INTRINSIC_VOID ||
1096 N->getOpcode() == ISD::PREFETCH ||
1097 N->isTargetMemoryOpcode();
1101 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1102 /// support for the llvm IR shufflevector instruction. It combines elements
1103 /// from two input vectors into a new input vector, with the selection and
1104 /// ordering of elements determined by an array of integers, referred to as
1105 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1106 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1107 /// An index of -1 is treated as undef, such that the code generator may put
1108 /// any value in the corresponding element of the result.
1109 class ShuffleVectorSDNode : public SDNode {
1112 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1113 // is freed when the SelectionDAG object is destroyed.
1116 friend class SelectionDAG;
1117 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1119 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1120 InitOperands(Ops, N1, N2);
1124 ArrayRef<int> getMask() const {
1125 EVT VT = getValueType(0);
1126 return makeArrayRef(Mask, VT.getVectorNumElements());
1128 int getMaskElt(unsigned Idx) const {
1129 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1133 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1134 int getSplatIndex() const {
1135 assert(isSplat() && "Cannot get splat index for non-splat!");
1136 EVT VT = getValueType(0);
1137 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1143 static bool isSplatMask(const int *Mask, EVT VT);
1145 static bool classof(const SDNode *N) {
1146 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1150 class ConstantSDNode : public SDNode {
1151 const ConstantInt *Value;
1152 friend class SelectionDAG;
1153 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1154 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1155 DebugLoc(), getSDVTList(VT)), Value(val) {
1159 const ConstantInt *getConstantIntValue() const { return Value; }
1160 const APInt &getAPIntValue() const { return Value->getValue(); }
1161 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1162 int64_t getSExtValue() const { return Value->getSExtValue(); }
1164 bool isOne() const { return Value->isOne(); }
1165 bool isNullValue() const { return Value->isNullValue(); }
1166 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1168 static bool classof(const SDNode *N) {
1169 return N->getOpcode() == ISD::Constant ||
1170 N->getOpcode() == ISD::TargetConstant;
1174 class ConstantFPSDNode : public SDNode {
1175 const ConstantFP *Value;
1176 friend class SelectionDAG;
1177 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1178 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1179 DebugLoc(), getSDVTList(VT)), Value(val) {
1183 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1184 const ConstantFP *getConstantFPValue() const { return Value; }
1186 /// isZero - Return true if the value is positive or negative zero.
1187 bool isZero() const { return Value->isZero(); }
1189 /// isNaN - Return true if the value is a NaN.
1190 bool isNaN() const { return Value->isNaN(); }
1192 /// isExactlyValue - We don't rely on operator== working on double values, as
1193 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1194 /// As such, this method can be used to do an exact bit-for-bit comparison of
1195 /// two floating point values.
1197 /// We leave the version with the double argument here because it's just so
1198 /// convenient to write "2.0" and the like. Without this function we'd
1199 /// have to duplicate its logic everywhere it's called.
1200 bool isExactlyValue(double V) const {
1203 Tmp.convert(Value->getValueAPF().getSemantics(),
1204 APFloat::rmNearestTiesToEven, &ignored);
1205 return isExactlyValue(Tmp);
1207 bool isExactlyValue(const APFloat& V) const;
1209 static bool isValueValidForType(EVT VT, const APFloat& Val);
1211 static bool classof(const SDNode *N) {
1212 return N->getOpcode() == ISD::ConstantFP ||
1213 N->getOpcode() == ISD::TargetConstantFP;
1217 class GlobalAddressSDNode : public SDNode {
1218 const GlobalValue *TheGlobal;
1220 unsigned char TargetFlags;
1221 friend class SelectionDAG;
1222 GlobalAddressSDNode(unsigned Opc, DebugLoc DL, const GlobalValue *GA, EVT VT,
1223 int64_t o, unsigned char TargetFlags);
1226 const GlobalValue *getGlobal() const { return TheGlobal; }
1227 int64_t getOffset() const { return Offset; }
1228 unsigned char getTargetFlags() const { return TargetFlags; }
1229 // Return the address space this GlobalAddress belongs to.
1230 unsigned getAddressSpace() const;
1232 static bool classof(const SDNode *N) {
1233 return N->getOpcode() == ISD::GlobalAddress ||
1234 N->getOpcode() == ISD::TargetGlobalAddress ||
1235 N->getOpcode() == ISD::GlobalTLSAddress ||
1236 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1240 class FrameIndexSDNode : public SDNode {
1242 friend class SelectionDAG;
1243 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1244 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1245 DebugLoc(), getSDVTList(VT)), FI(fi) {
1249 int getIndex() const { return FI; }
1251 static bool classof(const SDNode *N) {
1252 return N->getOpcode() == ISD::FrameIndex ||
1253 N->getOpcode() == ISD::TargetFrameIndex;
1257 class JumpTableSDNode : public SDNode {
1259 unsigned char TargetFlags;
1260 friend class SelectionDAG;
1261 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1262 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1263 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1267 int getIndex() const { return JTI; }
1268 unsigned char getTargetFlags() const { return TargetFlags; }
1270 static bool classof(const SDNode *N) {
1271 return N->getOpcode() == ISD::JumpTable ||
1272 N->getOpcode() == ISD::TargetJumpTable;
1276 class ConstantPoolSDNode : public SDNode {
1278 const Constant *ConstVal;
1279 MachineConstantPoolValue *MachineCPVal;
1281 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1282 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1283 unsigned char TargetFlags;
1284 friend class SelectionDAG;
1285 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1286 unsigned Align, unsigned char TF)
1287 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1289 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1290 assert((int)Offset >= 0 && "Offset is too large");
1293 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1294 EVT VT, int o, unsigned Align, unsigned char TF)
1295 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1297 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1298 assert((int)Offset >= 0 && "Offset is too large");
1299 Val.MachineCPVal = v;
1300 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1305 bool isMachineConstantPoolEntry() const {
1306 return (int)Offset < 0;
1309 const Constant *getConstVal() const {
1310 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1311 return Val.ConstVal;
1314 MachineConstantPoolValue *getMachineCPVal() const {
1315 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1316 return Val.MachineCPVal;
1319 int getOffset() const {
1320 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1323 // Return the alignment of this constant pool object, which is either 0 (for
1324 // default alignment) or the desired value.
1325 unsigned getAlignment() const { return Alignment; }
1326 unsigned char getTargetFlags() const { return TargetFlags; }
1328 Type *getType() const;
1330 static bool classof(const SDNode *N) {
1331 return N->getOpcode() == ISD::ConstantPool ||
1332 N->getOpcode() == ISD::TargetConstantPool;
1336 /// Completely target-dependent object reference.
1337 class TargetIndexSDNode : public SDNode {
1338 unsigned char TargetFlags;
1341 friend class SelectionDAG;
1344 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1345 : SDNode(ISD::TargetIndex, DebugLoc(), getSDVTList(VT)),
1346 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1349 unsigned char getTargetFlags() const { return TargetFlags; }
1350 int getIndex() const { return Index; }
1351 int64_t getOffset() const { return Offset; }
1353 static bool classof(const SDNode *N) {
1354 return N->getOpcode() == ISD::TargetIndex;
1358 class BasicBlockSDNode : public SDNode {
1359 MachineBasicBlock *MBB;
1360 friend class SelectionDAG;
1361 /// Debug info is meaningful and potentially useful here, but we create
1362 /// blocks out of order when they're jumped to, which makes it a bit
1363 /// harder. Let's see if we need it first.
1364 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1365 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1369 MachineBasicBlock *getBasicBlock() const { return MBB; }
1371 static bool classof(const SDNode *N) {
1372 return N->getOpcode() == ISD::BasicBlock;
1376 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1378 class BuildVectorSDNode : public SDNode {
1379 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1380 explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1382 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1383 /// smallest element size that splats the vector. If MinSplatBits is
1384 /// nonzero, the element size must be at least that large. Note that the
1385 /// splat element may be the entire vector (i.e., a one element vector).
1386 /// Returns the splat element value in SplatValue. Any undefined bits in
1387 /// that value are zero, and the corresponding bits in the SplatUndef mask
1388 /// are set. The SplatBitSize value is set to the splat element size in
1389 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1390 /// undefined. isBigEndian describes the endianness of the target.
1391 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1392 unsigned &SplatBitSize, bool &HasAnyUndefs,
1393 unsigned MinSplatBits = 0, bool isBigEndian = false);
1395 static inline bool classof(const SDNode *N) {
1396 return N->getOpcode() == ISD::BUILD_VECTOR;
1400 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1401 /// used when the SelectionDAG needs to make a simple reference to something
1402 /// in the LLVM IR representation.
1404 class SrcValueSDNode : public SDNode {
1406 friend class SelectionDAG;
1407 /// Create a SrcValue for a general value.
1408 explicit SrcValueSDNode(const Value *v)
1409 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1412 /// getValue - return the contained Value.
1413 const Value *getValue() const { return V; }
1415 static bool classof(const SDNode *N) {
1416 return N->getOpcode() == ISD::SRCVALUE;
1420 class MDNodeSDNode : public SDNode {
1422 friend class SelectionDAG;
1423 explicit MDNodeSDNode(const MDNode *md)
1424 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1427 const MDNode *getMD() const { return MD; }
1429 static bool classof(const SDNode *N) {
1430 return N->getOpcode() == ISD::MDNODE_SDNODE;
1435 class RegisterSDNode : public SDNode {
1437 friend class SelectionDAG;
1438 RegisterSDNode(unsigned reg, EVT VT)
1439 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1443 unsigned getReg() const { return Reg; }
1445 static bool classof(const SDNode *N) {
1446 return N->getOpcode() == ISD::Register;
1450 class RegisterMaskSDNode : public SDNode {
1451 // The memory for RegMask is not owned by the node.
1452 const uint32_t *RegMask;
1453 friend class SelectionDAG;
1454 RegisterMaskSDNode(const uint32_t *mask)
1455 : SDNode(ISD::RegisterMask, DebugLoc(), getSDVTList(MVT::Untyped)),
1459 const uint32_t *getRegMask() const { return RegMask; }
1461 static bool classof(const SDNode *N) {
1462 return N->getOpcode() == ISD::RegisterMask;
1466 class BlockAddressSDNode : public SDNode {
1467 const BlockAddress *BA;
1469 unsigned char TargetFlags;
1470 friend class SelectionDAG;
1471 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1472 int64_t o, unsigned char Flags)
1473 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1474 BA(ba), Offset(o), TargetFlags(Flags) {
1477 const BlockAddress *getBlockAddress() const { return BA; }
1478 int64_t getOffset() const { return Offset; }
1479 unsigned char getTargetFlags() const { return TargetFlags; }
1481 static bool classof(const SDNode *N) {
1482 return N->getOpcode() == ISD::BlockAddress ||
1483 N->getOpcode() == ISD::TargetBlockAddress;
1487 class EHLabelSDNode : public SDNode {
1490 friend class SelectionDAG;
1491 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1492 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1493 InitOperands(&Chain, ch);
1496 MCSymbol *getLabel() const { return Label; }
1498 static bool classof(const SDNode *N) {
1499 return N->getOpcode() == ISD::EH_LABEL;
1503 class ExternalSymbolSDNode : public SDNode {
1505 unsigned char TargetFlags;
1507 friend class SelectionDAG;
1508 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1509 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1510 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1514 const char *getSymbol() const { return Symbol; }
1515 unsigned char getTargetFlags() const { return TargetFlags; }
1517 static bool classof(const SDNode *N) {
1518 return N->getOpcode() == ISD::ExternalSymbol ||
1519 N->getOpcode() == ISD::TargetExternalSymbol;
1523 class CondCodeSDNode : public SDNode {
1524 ISD::CondCode Condition;
1525 friend class SelectionDAG;
1526 explicit CondCodeSDNode(ISD::CondCode Cond)
1527 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1532 ISD::CondCode get() const { return Condition; }
1534 static bool classof(const SDNode *N) {
1535 return N->getOpcode() == ISD::CONDCODE;
1539 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1540 /// future and most targets don't support it.
1541 class CvtRndSatSDNode : public SDNode {
1542 ISD::CvtCode CvtCode;
1543 friend class SelectionDAG;
1544 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1545 unsigned NumOps, ISD::CvtCode Code)
1546 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1548 assert(NumOps == 5 && "wrong number of operations");
1551 ISD::CvtCode getCvtCode() const { return CvtCode; }
1553 static bool classof(const SDNode *N) {
1554 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1558 /// VTSDNode - This class is used to represent EVT's, which are used
1559 /// to parameterize some operations.
1560 class VTSDNode : public SDNode {
1562 friend class SelectionDAG;
1563 explicit VTSDNode(EVT VT)
1564 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1569 EVT getVT() const { return ValueType; }
1571 static bool classof(const SDNode *N) {
1572 return N->getOpcode() == ISD::VALUETYPE;
1576 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1578 class LSBaseSDNode : public MemSDNode {
1579 //! Operand array for load and store
1581 \note Moving this array to the base class captures more
1582 common functionality shared between LoadSDNode and
1587 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1588 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1589 EVT MemVT, MachineMemOperand *MMO)
1590 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1591 SubclassData |= AM << 2;
1592 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1593 InitOperands(Ops, Operands, numOperands);
1594 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1595 "Only indexed loads and stores have a non-undef offset operand");
1598 const SDValue &getOffset() const {
1599 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1602 /// getAddressingMode - Return the addressing mode for this load or store:
1603 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1604 ISD::MemIndexedMode getAddressingMode() const {
1605 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1608 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1609 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1611 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1612 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1614 static bool classof(const SDNode *N) {
1615 return N->getOpcode() == ISD::LOAD ||
1616 N->getOpcode() == ISD::STORE;
1620 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1622 class LoadSDNode : public LSBaseSDNode {
1623 friend class SelectionDAG;
1624 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1625 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1626 MachineMemOperand *MMO)
1627 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1628 VTs, AM, MemVT, MMO) {
1629 SubclassData |= (unsigned short)ETy;
1630 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1631 assert(readMem() && "Load MachineMemOperand is not a load!");
1632 assert(!writeMem() && "Load MachineMemOperand is a store!");
1636 /// getExtensionType - Return whether this is a plain node,
1637 /// or one of the varieties of value-extending loads.
1638 ISD::LoadExtType getExtensionType() const {
1639 return ISD::LoadExtType(SubclassData & 3);
1642 const SDValue &getBasePtr() const { return getOperand(1); }
1643 const SDValue &getOffset() const { return getOperand(2); }
1645 static bool classof(const SDNode *N) {
1646 return N->getOpcode() == ISD::LOAD;
1650 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1652 class StoreSDNode : public LSBaseSDNode {
1653 friend class SelectionDAG;
1654 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1655 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1656 MachineMemOperand *MMO)
1657 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1658 VTs, AM, MemVT, MMO) {
1659 SubclassData |= (unsigned short)isTrunc;
1660 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1661 assert(!readMem() && "Store MachineMemOperand is a load!");
1662 assert(writeMem() && "Store MachineMemOperand is not a store!");
1666 /// isTruncatingStore - Return true if the op does a truncation before store.
1667 /// For integers this is the same as doing a TRUNCATE and storing the result.
1668 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1669 bool isTruncatingStore() const { return SubclassData & 1; }
1671 const SDValue &getValue() const { return getOperand(1); }
1672 const SDValue &getBasePtr() const { return getOperand(2); }
1673 const SDValue &getOffset() const { return getOperand(3); }
1675 static bool classof(const SDNode *N) {
1676 return N->getOpcode() == ISD::STORE;
1680 /// MachineSDNode - An SDNode that represents everything that will be needed
1681 /// to construct a MachineInstr. These nodes are created during the
1682 /// instruction selection proper phase.
1684 class MachineSDNode : public SDNode {
1686 typedef MachineMemOperand **mmo_iterator;
1689 friend class SelectionDAG;
1690 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1691 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1693 /// LocalOperands - Operands for this instruction, if they fit here. If
1694 /// they don't, this field is unused.
1695 SDUse LocalOperands[4];
1697 /// MemRefs - Memory reference descriptions for this instruction.
1698 mmo_iterator MemRefs;
1699 mmo_iterator MemRefsEnd;
1702 mmo_iterator memoperands_begin() const { return MemRefs; }
1703 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1704 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1706 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1707 /// list. This does not transfer ownership.
1708 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1709 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1710 assert(*MMI && "Null mem ref detected!");
1711 MemRefs = NewMemRefs;
1712 MemRefsEnd = NewMemRefsEnd;
1715 static bool classof(const SDNode *N) {
1716 return N->isMachineOpcode();
1720 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1721 SDNode, ptrdiff_t> {
1725 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1727 bool operator==(const SDNodeIterator& x) const {
1728 return Operand == x.Operand;
1730 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1732 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1733 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1734 Operand = I.Operand;
1738 pointer operator*() const {
1739 return Node->getOperand(Operand).getNode();
1741 pointer operator->() const { return operator*(); }
1743 SDNodeIterator& operator++() { // Preincrement
1747 SDNodeIterator operator++(int) { // Postincrement
1748 SDNodeIterator tmp = *this; ++*this; return tmp;
1750 size_t operator-(SDNodeIterator Other) const {
1751 assert(Node == Other.Node &&
1752 "Cannot compare iterators of two different nodes!");
1753 return Operand - Other.Operand;
1756 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1757 static SDNodeIterator end (const SDNode *N) {
1758 return SDNodeIterator(N, N->getNumOperands());
1761 unsigned getOperand() const { return Operand; }
1762 const SDNode *getNode() const { return Node; }
1765 template <> struct GraphTraits<SDNode*> {
1766 typedef SDNode NodeType;
1767 typedef SDNodeIterator ChildIteratorType;
1768 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1769 static inline ChildIteratorType child_begin(NodeType *N) {
1770 return SDNodeIterator::begin(N);
1772 static inline ChildIteratorType child_end(NodeType *N) {
1773 return SDNodeIterator::end(N);
1777 /// LargestSDNode - The largest SDNode class.
1779 typedef LoadSDNode LargestSDNode;
1781 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1784 typedef GlobalAddressSDNode MostAlignedSDNode;
1787 /// isNormalLoad - Returns true if the specified node is a non-extending
1788 /// and unindexed load.
1789 inline bool isNormalLoad(const SDNode *N) {
1790 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1791 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1792 Ld->getAddressingMode() == ISD::UNINDEXED;
1795 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1797 inline bool isNON_EXTLoad(const SDNode *N) {
1798 return isa<LoadSDNode>(N) &&
1799 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1802 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1804 inline bool isEXTLoad(const SDNode *N) {
1805 return isa<LoadSDNode>(N) &&
1806 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1809 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1811 inline bool isSEXTLoad(const SDNode *N) {
1812 return isa<LoadSDNode>(N) &&
1813 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1816 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1818 inline bool isZEXTLoad(const SDNode *N) {
1819 return isa<LoadSDNode>(N) &&
1820 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1823 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1825 inline bool isUNINDEXEDLoad(const SDNode *N) {
1826 return isa<LoadSDNode>(N) &&
1827 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1830 /// isNormalStore - Returns true if the specified node is a non-truncating
1831 /// and unindexed store.
1832 inline bool isNormalStore(const SDNode *N) {
1833 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1834 return St && !St->isTruncatingStore() &&
1835 St->getAddressingMode() == ISD::UNINDEXED;
1838 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1840 inline bool isNON_TRUNCStore(const SDNode *N) {
1841 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1844 /// isTRUNCStore - Returns true if the specified node is a truncating
1846 inline bool isTRUNCStore(const SDNode *N) {
1847 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1850 /// isUNINDEXEDStore - Returns true if the specified node is an
1851 /// unindexed store.
1852 inline bool isUNINDEXEDStore(const SDNode *N) {
1853 return isa<StoreSDNode>(N) &&
1854 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1858 } // end llvm namespace