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/SmallVector.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/CodeGen/ISDOpcodes.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/CodeGen/MachineMemOperand.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/System/DataTypes.h"
33 #include "llvm/Support/DebugLoc.h"
40 class MachineBasicBlock;
41 class MachineConstantPoolValue;
45 template <typename T> struct DenseMapInfo;
46 template <typename T> struct simplify_type;
47 template <typename T> struct ilist_traits;
49 void checkForCycles(const SDNode *N);
51 /// SDVTList - This represents a list of ValueType's that has been intern'd by
52 /// a SelectionDAG. Instances of this simple value class are returned by
53 /// SelectionDAG::getVTList(...).
63 /// isBuildVectorAllOnes - Return true if the specified node is a
64 /// BUILD_VECTOR where all of the elements are ~0 or undef.
65 bool isBuildVectorAllOnes(const SDNode *N);
67 /// isBuildVectorAllZeros - Return true if the specified node is a
68 /// BUILD_VECTOR where all of the elements are 0 or undef.
69 bool isBuildVectorAllZeros(const SDNode *N);
71 /// isScalarToVector - Return true if the specified node is a
72 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
73 /// element is not an undef.
74 bool isScalarToVector(const SDNode *N);
75 } // end llvm:ISD namespace
77 //===----------------------------------------------------------------------===//
78 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
79 /// values as the result of a computation. Many nodes return multiple values,
80 /// from loads (which define a token and a return value) to ADDC (which returns
81 /// a result and a carry value), to calls (which may return an arbitrary number
84 /// As such, each use of a SelectionDAG computation must indicate the node that
85 /// computes it as well as which return value to use from that node. This pair
86 /// of information is represented with the SDValue value type.
89 SDNode *Node; // The node defining the value we are using.
90 unsigned ResNo; // Which return value of the node we are using.
92 SDValue() : Node(0), ResNo(0) {}
93 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
95 /// get the index which selects a specific result in the SDNode
96 unsigned getResNo() const { return ResNo; }
98 /// get the SDNode which holds the desired result
99 SDNode *getNode() const { return Node; }
102 void setNode(SDNode *N) { Node = N; }
104 inline SDNode *operator->() const { return Node; }
106 bool operator==(const SDValue &O) const {
107 return Node == O.Node && ResNo == O.ResNo;
109 bool operator!=(const SDValue &O) const {
110 return !operator==(O);
112 bool operator<(const SDValue &O) const {
113 return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
116 SDValue getValue(unsigned R) const {
117 return SDValue(Node, R);
120 // isOperandOf - Return true if this node is an operand of N.
121 bool isOperandOf(SDNode *N) const;
123 /// getValueType - Return the ValueType of the referenced return value.
125 inline EVT getValueType() const;
127 /// getValueSizeInBits - Returns the size of the value in bits.
129 unsigned getValueSizeInBits() const {
130 return getValueType().getSizeInBits();
133 // Forwarding methods - These forward to the corresponding methods in SDNode.
134 inline unsigned getOpcode() const;
135 inline unsigned getNumOperands() const;
136 inline const SDValue &getOperand(unsigned i) const;
137 inline uint64_t getConstantOperandVal(unsigned i) const;
138 inline bool isTargetMemoryOpcode() const;
139 inline bool isTargetOpcode() const;
140 inline bool isMachineOpcode() const;
141 inline unsigned getMachineOpcode() const;
142 inline const DebugLoc getDebugLoc() const;
145 /// reachesChainWithoutSideEffects - Return true if this operand (which must
146 /// be a chain) reaches the specified operand without crossing any
147 /// side-effecting instructions. In practice, this looks through token
148 /// factors and non-volatile loads. In order to remain efficient, this only
149 /// looks a couple of nodes in, it does not do an exhaustive search.
150 bool reachesChainWithoutSideEffects(SDValue Dest,
151 unsigned Depth = 2) const;
153 /// use_empty - Return true if there are no nodes using value ResNo
156 inline bool use_empty() const;
158 /// hasOneUse - Return true if there is exactly one node using value
161 inline bool hasOneUse() const;
165 template<> struct DenseMapInfo<SDValue> {
166 static inline SDValue getEmptyKey() {
167 return SDValue((SDNode*)-1, -1U);
169 static inline SDValue getTombstoneKey() {
170 return SDValue((SDNode*)-1, 0);
172 static unsigned getHashValue(const SDValue &Val) {
173 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
174 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
176 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
180 template <> struct isPodLike<SDValue> { static const bool value = true; };
183 /// simplify_type specializations - Allow casting operators to work directly on
184 /// SDValues as if they were SDNode*'s.
185 template<> struct simplify_type<SDValue> {
186 typedef SDNode* SimpleType;
187 static SimpleType getSimplifiedValue(const SDValue &Val) {
188 return static_cast<SimpleType>(Val.getNode());
191 template<> struct simplify_type<const SDValue> {
192 typedef SDNode* SimpleType;
193 static SimpleType getSimplifiedValue(const SDValue &Val) {
194 return static_cast<SimpleType>(Val.getNode());
198 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
199 /// which records the SDNode being used and the result number, a
200 /// pointer to the SDNode using the value, and Next and Prev pointers,
201 /// which link together all the uses of an SDNode.
204 /// Val - The value being used.
206 /// User - The user of this value.
208 /// Prev, Next - Pointers to the uses list of the SDNode referred by
212 SDUse(const SDUse &U); // Do not implement
213 void operator=(const SDUse &U); // Do not implement
216 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
218 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
219 operator const SDValue&() const { return Val; }
221 /// If implicit conversion to SDValue doesn't work, the get() method returns
223 const SDValue &get() const { return Val; }
225 /// getUser - This returns the SDNode that contains this Use.
226 SDNode *getUser() { return User; }
228 /// getNext - Get the next SDUse in the use list.
229 SDUse *getNext() const { return Next; }
231 /// getNode - Convenience function for get().getNode().
232 SDNode *getNode() const { return Val.getNode(); }
233 /// getResNo - Convenience function for get().getResNo().
234 unsigned getResNo() const { return Val.getResNo(); }
235 /// getValueType - Convenience function for get().getValueType().
236 EVT getValueType() const { return Val.getValueType(); }
238 /// operator== - Convenience function for get().operator==
239 bool operator==(const SDValue &V) const {
243 /// operator!= - Convenience function for get().operator!=
244 bool operator!=(const SDValue &V) const {
248 /// operator< - Convenience function for get().operator<
249 bool operator<(const SDValue &V) const {
254 friend class SelectionDAG;
257 void setUser(SDNode *p) { User = p; }
259 /// set - Remove this use from its existing use list, assign it the
260 /// given value, and add it to the new value's node's use list.
261 inline void set(const SDValue &V);
262 /// setInitial - like set, but only supports initializing a newly-allocated
263 /// SDUse with a non-null value.
264 inline void setInitial(const SDValue &V);
265 /// setNode - like set, but only sets the Node portion of the value,
266 /// leaving the ResNo portion unmodified.
267 inline void setNode(SDNode *N);
269 void addToList(SDUse **List) {
271 if (Next) Next->Prev = &Next;
276 void removeFromList() {
278 if (Next) Next->Prev = Prev;
282 /// simplify_type specializations - Allow casting operators to work directly on
283 /// SDValues as if they were SDNode*'s.
284 template<> struct simplify_type<SDUse> {
285 typedef SDNode* SimpleType;
286 static SimpleType getSimplifiedValue(const SDUse &Val) {
287 return static_cast<SimpleType>(Val.getNode());
290 template<> struct simplify_type<const SDUse> {
291 typedef SDNode* SimpleType;
292 static SimpleType getSimplifiedValue(const SDUse &Val) {
293 return static_cast<SimpleType>(Val.getNode());
298 /// SDNode - Represents one node in the SelectionDAG.
300 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
302 /// NodeType - The operation that this node performs.
306 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
307 /// then they will be delete[]'d when the node is destroyed.
308 uint16_t OperandsNeedDelete : 1;
310 /// HasDebugValue - This tracks whether this node has one or more dbg_value
311 /// nodes corresponding to it.
312 uint16_t HasDebugValue : 1;
315 /// SubclassData - This member is defined by this class, but is not used for
316 /// anything. Subclasses can use it to hold whatever state they find useful.
317 /// This field is initialized to zero by the ctor.
318 uint16_t SubclassData : 14;
321 /// NodeId - Unique id per SDNode in the DAG.
324 /// OperandList - The values that are used by this operation.
328 /// ValueList - The types of the values this node defines. SDNode's may
329 /// define multiple values simultaneously.
330 const EVT *ValueList;
332 /// UseList - List of uses for this SDNode.
335 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
336 unsigned short NumOperands, NumValues;
338 /// debugLoc - source line information.
341 /// getValueTypeList - Return a pointer to the specified value type.
342 static const EVT *getValueTypeList(EVT VT);
344 friend class SelectionDAG;
345 friend struct ilist_traits<SDNode>;
348 //===--------------------------------------------------------------------===//
352 /// getOpcode - Return the SelectionDAG opcode value for this node. For
353 /// pre-isel nodes (those for which isMachineOpcode returns false), these
354 /// are the opcode values in the ISD and <target>ISD namespaces. For
355 /// post-isel opcodes, see getMachineOpcode.
356 unsigned getOpcode() const { return (unsigned short)NodeType; }
358 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
359 /// \<target\>ISD namespace).
360 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
362 /// isTargetMemoryOpcode - Test if this node has a target-specific
363 /// memory-referencing opcode (in the \<target\>ISD namespace and
364 /// greater than FIRST_TARGET_MEMORY_OPCODE).
365 bool isTargetMemoryOpcode() const {
366 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
369 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
370 /// corresponding to a MachineInstr opcode.
371 bool isMachineOpcode() const { return NodeType < 0; }
373 /// getMachineOpcode - This may only be called if isMachineOpcode returns
374 /// true. It returns the MachineInstr opcode value that the node's opcode
376 unsigned getMachineOpcode() const {
377 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
381 /// getHasDebugValue - get this bit.
382 bool getHasDebugValue() const { return HasDebugValue; }
384 /// setHasDebugValue - set this bit.
385 void setHasDebugValue(bool b) { HasDebugValue = b; }
387 /// use_empty - Return true if there are no uses of this node.
389 bool use_empty() const { return UseList == NULL; }
391 /// hasOneUse - Return true if there is exactly one use of this node.
393 bool hasOneUse() const {
394 return !use_empty() && llvm::next(use_begin()) == use_end();
397 /// use_size - Return the number of uses of this node. This method takes
398 /// time proportional to the number of uses.
400 size_t use_size() const { return std::distance(use_begin(), use_end()); }
402 /// getNodeId - Return the unique node id.
404 int getNodeId() const { return NodeId; }
406 /// setNodeId - Set unique node id.
407 void setNodeId(int Id) { NodeId = Id; }
409 /// getDebugLoc - Return the source location info.
410 const DebugLoc getDebugLoc() const { return debugLoc; }
412 /// setDebugLoc - Set source location info. Try to avoid this, putting
413 /// it in the constructor is preferable.
414 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
416 /// use_iterator - This class provides iterator support for SDUse
417 /// operands that use a specific SDNode.
419 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
421 explicit use_iterator(SDUse *op) : Op(op) {
425 typedef std::iterator<std::forward_iterator_tag,
426 SDUse, ptrdiff_t>::reference reference;
427 typedef std::iterator<std::forward_iterator_tag,
428 SDUse, ptrdiff_t>::pointer pointer;
430 use_iterator(const use_iterator &I) : Op(I.Op) {}
431 use_iterator() : Op(0) {}
433 bool operator==(const use_iterator &x) const {
436 bool operator!=(const use_iterator &x) const {
437 return !operator==(x);
440 /// atEnd - return true if this iterator is at the end of uses list.
441 bool atEnd() const { return Op == 0; }
443 // Iterator traversal: forward iteration only.
444 use_iterator &operator++() { // Preincrement
445 assert(Op && "Cannot increment end iterator!");
450 use_iterator operator++(int) { // Postincrement
451 use_iterator tmp = *this; ++*this; return tmp;
454 /// Retrieve a pointer to the current user node.
455 SDNode *operator*() const {
456 assert(Op && "Cannot dereference end iterator!");
457 return Op->getUser();
460 SDNode *operator->() const { return operator*(); }
462 SDUse &getUse() const { return *Op; }
464 /// getOperandNo - Retrieve the operand # of this use in its user.
466 unsigned getOperandNo() const {
467 assert(Op && "Cannot dereference end iterator!");
468 return (unsigned)(Op - Op->getUser()->OperandList);
472 /// use_begin/use_end - Provide iteration support to walk over all uses
475 use_iterator use_begin() const {
476 return use_iterator(UseList);
479 static use_iterator use_end() { return use_iterator(0); }
482 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
483 /// indicated value. This method ignores uses of other values defined by this
485 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
487 /// hasAnyUseOfValue - Return true if there are any use of the indicated
488 /// value. This method ignores uses of other values defined by this operation.
489 bool hasAnyUseOfValue(unsigned Value) const;
491 /// isOnlyUserOf - Return true if this node is the only use of N.
493 bool isOnlyUserOf(SDNode *N) const;
495 /// isOperandOf - Return true if this node is an operand of N.
497 bool isOperandOf(SDNode *N) const;
499 /// isPredecessorOf - Return true if this node is a predecessor of N. This
500 /// node is either an operand of N or it can be reached by recursively
501 /// traversing up the operands.
502 /// NOTE: this is an expensive method. Use it carefully.
503 bool isPredecessorOf(SDNode *N) const;
505 /// getNumOperands - Return the number of values used by this operation.
507 unsigned getNumOperands() const { return NumOperands; }
509 /// getConstantOperandVal - Helper method returns the integer value of a
510 /// ConstantSDNode operand.
511 uint64_t getConstantOperandVal(unsigned Num) const;
513 const SDValue &getOperand(unsigned Num) const {
514 assert(Num < NumOperands && "Invalid child # of SDNode!");
515 return OperandList[Num];
518 typedef SDUse* op_iterator;
519 op_iterator op_begin() const { return OperandList; }
520 op_iterator op_end() const { return OperandList+NumOperands; }
522 SDVTList getVTList() const {
523 SDVTList X = { ValueList, NumValues };
527 /// getFlaggedNode - If this node has a flag operand, return the node
528 /// to which the flag operand points. Otherwise return NULL.
529 SDNode *getFlaggedNode() const {
530 if (getNumOperands() != 0 &&
531 getOperand(getNumOperands()-1).getValueType().getSimpleVT() == MVT::Flag)
532 return getOperand(getNumOperands()-1).getNode();
536 // If this is a pseudo op, like copyfromreg, look to see if there is a
537 // real target node flagged to it. If so, return the target node.
538 const SDNode *getFlaggedMachineNode() const {
539 const SDNode *FoundNode = this;
541 // Climb up flag edges until a machine-opcode node is found, or the
542 // end of the chain is reached.
543 while (!FoundNode->isMachineOpcode()) {
544 const SDNode *N = FoundNode->getFlaggedNode();
552 /// getNumValues - Return the number of values defined/returned by this
555 unsigned getNumValues() const { return NumValues; }
557 /// getValueType - Return the type of a specified result.
559 EVT getValueType(unsigned ResNo) const {
560 assert(ResNo < NumValues && "Illegal result number!");
561 return ValueList[ResNo];
564 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
566 unsigned getValueSizeInBits(unsigned ResNo) const {
567 return getValueType(ResNo).getSizeInBits();
570 typedef const EVT* value_iterator;
571 value_iterator value_begin() const { return ValueList; }
572 value_iterator value_end() const { return ValueList+NumValues; }
574 /// getOperationName - Return the opcode of this operation for printing.
576 std::string getOperationName(const SelectionDAG *G = 0) const;
577 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
578 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
579 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
580 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
581 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
583 /// printrFull - Print a SelectionDAG node and all children down to
584 /// the leaves. The given SelectionDAG allows target-specific nodes
585 /// to be printed in human-readable form. Unlike printr, this will
586 /// print the whole DAG, including children that appear multiple
589 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
591 /// printrWithDepth - Print a SelectionDAG node and children up to
592 /// depth "depth." The given SelectionDAG allows target-specific
593 /// nodes to be printed in human-readable form. Unlike printr, this
594 /// will print children that appear multiple times wherever they are
597 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
598 unsigned depth = 100) const;
601 /// dump - Dump this node, for debugging.
604 /// dumpr - Dump (recursively) this node and its use-def subgraph.
607 /// dump - Dump this node, for debugging.
608 /// The given SelectionDAG allows target-specific nodes to be printed
609 /// in human-readable form.
610 void dump(const SelectionDAG *G) const;
612 /// dumpr - Dump (recursively) this node and its use-def subgraph.
613 /// The given SelectionDAG allows target-specific nodes to be printed
614 /// in human-readable form.
615 void dumpr(const SelectionDAG *G) const;
617 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
618 /// target-specific nodes to be printed in human-readable form.
619 /// Unlike dumpr, this will print the whole DAG, including children
620 /// that appear multiple times.
622 void dumprFull(const SelectionDAG *G = 0) const;
624 /// dumprWithDepth - printrWithDepth to dbgs(). The given
625 /// SelectionDAG allows target-specific nodes to be printed in
626 /// human-readable form. Unlike dumpr, this will print children
627 /// that appear multiple times wherever they are used.
629 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
632 static bool classof(const SDNode *) { return true; }
634 /// Profile - Gather unique data for the node.
636 void Profile(FoldingSetNodeID &ID) const;
638 /// addUse - This method should only be used by the SDUse class.
640 void addUse(SDUse &U) { U.addToList(&UseList); }
643 static SDVTList getSDVTList(EVT VT) {
644 SDVTList Ret = { getValueTypeList(VT), 1 };
648 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
650 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
651 SubclassData(0), NodeId(-1),
652 OperandList(NumOps ? new SDUse[NumOps] : 0),
653 ValueList(VTs.VTs), UseList(NULL),
654 NumOperands(NumOps), NumValues(VTs.NumVTs),
656 for (unsigned i = 0; i != NumOps; ++i) {
657 OperandList[i].setUser(this);
658 OperandList[i].setInitial(Ops[i]);
660 checkForCycles(this);
663 /// This constructor adds no operands itself; operands can be
664 /// set later with InitOperands.
665 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
666 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
667 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
668 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
671 /// InitOperands - Initialize the operands list of this with 1 operand.
672 void InitOperands(SDUse *Ops, const SDValue &Op0) {
673 Ops[0].setUser(this);
674 Ops[0].setInitial(Op0);
677 checkForCycles(this);
680 /// InitOperands - Initialize the operands list of this with 2 operands.
681 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
682 Ops[0].setUser(this);
683 Ops[0].setInitial(Op0);
684 Ops[1].setUser(this);
685 Ops[1].setInitial(Op1);
688 checkForCycles(this);
691 /// InitOperands - Initialize the operands list of this with 3 operands.
692 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
693 const SDValue &Op2) {
694 Ops[0].setUser(this);
695 Ops[0].setInitial(Op0);
696 Ops[1].setUser(this);
697 Ops[1].setInitial(Op1);
698 Ops[2].setUser(this);
699 Ops[2].setInitial(Op2);
702 checkForCycles(this);
705 /// InitOperands - Initialize the operands list of this with 4 operands.
706 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
707 const SDValue &Op2, const SDValue &Op3) {
708 Ops[0].setUser(this);
709 Ops[0].setInitial(Op0);
710 Ops[1].setUser(this);
711 Ops[1].setInitial(Op1);
712 Ops[2].setUser(this);
713 Ops[2].setInitial(Op2);
714 Ops[3].setUser(this);
715 Ops[3].setInitial(Op3);
718 checkForCycles(this);
721 /// InitOperands - Initialize the operands list of this with N operands.
722 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
723 for (unsigned i = 0; i != N; ++i) {
724 Ops[i].setUser(this);
725 Ops[i].setInitial(Vals[i]);
729 checkForCycles(this);
732 /// DropOperands - Release the operands and set this node to have
738 // Define inline functions from the SDValue class.
740 inline unsigned SDValue::getOpcode() const {
741 return Node->getOpcode();
743 inline EVT SDValue::getValueType() const {
744 return Node->getValueType(ResNo);
746 inline unsigned SDValue::getNumOperands() const {
747 return Node->getNumOperands();
749 inline const SDValue &SDValue::getOperand(unsigned i) const {
750 return Node->getOperand(i);
752 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
753 return Node->getConstantOperandVal(i);
755 inline bool SDValue::isTargetOpcode() const {
756 return Node->isTargetOpcode();
758 inline bool SDValue::isTargetMemoryOpcode() const {
759 return Node->isTargetMemoryOpcode();
761 inline bool SDValue::isMachineOpcode() const {
762 return Node->isMachineOpcode();
764 inline unsigned SDValue::getMachineOpcode() const {
765 return Node->getMachineOpcode();
767 inline bool SDValue::use_empty() const {
768 return !Node->hasAnyUseOfValue(ResNo);
770 inline bool SDValue::hasOneUse() const {
771 return Node->hasNUsesOfValue(1, ResNo);
773 inline const DebugLoc SDValue::getDebugLoc() const {
774 return Node->getDebugLoc();
777 // Define inline functions from the SDUse class.
779 inline void SDUse::set(const SDValue &V) {
780 if (Val.getNode()) removeFromList();
782 if (V.getNode()) V.getNode()->addUse(*this);
785 inline void SDUse::setInitial(const SDValue &V) {
787 V.getNode()->addUse(*this);
790 inline void SDUse::setNode(SDNode *N) {
791 if (Val.getNode()) removeFromList();
793 if (N) N->addUse(*this);
796 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
797 /// to allow co-allocation of node operands with the node itself.
798 class UnarySDNode : public SDNode {
801 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
802 : SDNode(Opc, dl, VTs) {
803 InitOperands(&Op, X);
807 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
808 /// to allow co-allocation of node operands with the node itself.
809 class BinarySDNode : public SDNode {
812 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
813 : SDNode(Opc, dl, VTs) {
814 InitOperands(Ops, X, Y);
818 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
819 /// to allow co-allocation of node operands with the node itself.
820 class TernarySDNode : public SDNode {
823 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
825 : SDNode(Opc, dl, VTs) {
826 InitOperands(Ops, X, Y, Z);
831 /// HandleSDNode - This class is used to form a handle around another node that
832 /// is persistant and is updated across invocations of replaceAllUsesWith on its
833 /// operand. This node should be directly created by end-users and not added to
834 /// the AllNodes list.
835 class HandleSDNode : public SDNode {
838 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
840 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
841 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
843 explicit HandleSDNode(SDValue X)
845 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
846 InitOperands(&Op, X);
849 const SDValue &getValue() const { return Op; }
852 /// Abstact virtual class for operations for memory operations
853 class MemSDNode : public SDNode {
855 // MemoryVT - VT of in-memory value.
859 /// MMO - Memory reference information.
860 MachineMemOperand *MMO;
863 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
864 MachineMemOperand *MMO);
866 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
867 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
869 bool readMem() const { return MMO->isLoad(); }
870 bool writeMem() const { return MMO->isStore(); }
872 /// Returns alignment and volatility of the memory access
873 unsigned getOriginalAlignment() const {
874 return MMO->getBaseAlignment();
876 unsigned getAlignment() const {
877 return MMO->getAlignment();
880 /// getRawSubclassData - Return the SubclassData value, which contains an
881 /// encoding of the volatile flag, as well as bits used by subclasses. This
882 /// function should only be used to compute a FoldingSetNodeID value.
883 unsigned getRawSubclassData() const {
887 // We access subclass data here so that we can check consistency
888 // with MachineMemOperand information.
889 bool isVolatile() const { return (SubclassData >> 5) & 1; }
890 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
892 /// Returns the SrcValue and offset that describes the location of the access
893 const Value *getSrcValue() const { return MMO->getValue(); }
894 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
896 /// getMemoryVT - Return the type of the in-memory value.
897 EVT getMemoryVT() const { return MemoryVT; }
899 /// getMemOperand - Return a MachineMemOperand object describing the memory
900 /// reference performed by operation.
901 MachineMemOperand *getMemOperand() const { return MMO; }
903 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
904 /// to reflect the alignment of NewMMO, if it has a greater alignment.
905 /// This must only be used when the new alignment applies to all users of
906 /// this MachineMemOperand.
907 void refineAlignment(const MachineMemOperand *NewMMO) {
908 MMO->refineAlignment(NewMMO);
911 const SDValue &getChain() const { return getOperand(0); }
912 const SDValue &getBasePtr() const {
913 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
916 // Methods to support isa and dyn_cast
917 static bool classof(const MemSDNode *) { return true; }
918 static bool classof(const SDNode *N) {
919 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
920 // with either an intrinsic or a target opcode.
921 return N->getOpcode() == ISD::LOAD ||
922 N->getOpcode() == ISD::STORE ||
923 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
924 N->getOpcode() == ISD::ATOMIC_SWAP ||
925 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
926 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
927 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
928 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
929 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
930 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
931 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
932 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
933 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
934 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
935 N->isTargetMemoryOpcode();
939 /// AtomicSDNode - A SDNode reprenting atomic operations.
941 class AtomicSDNode : public MemSDNode {
945 // Opc: opcode for atomic
946 // VTL: value type list
947 // Chain: memory chain for operaand
948 // Ptr: address to update as a SDValue
949 // Cmp: compare value
951 // SrcVal: address to update as a Value (used for MemOperand)
952 // Align: alignment of memory
953 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
954 SDValue Chain, SDValue Ptr,
955 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
956 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
957 assert(readMem() && "Atomic MachineMemOperand is not a load!");
958 assert(writeMem() && "Atomic MachineMemOperand is not a store!");
959 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
961 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
962 SDValue Chain, SDValue Ptr,
963 SDValue Val, MachineMemOperand *MMO)
964 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
965 assert(readMem() && "Atomic MachineMemOperand is not a load!");
966 assert(writeMem() && "Atomic MachineMemOperand is not a store!");
967 InitOperands(Ops, Chain, Ptr, Val);
970 const SDValue &getBasePtr() const { return getOperand(1); }
971 const SDValue &getVal() const { return getOperand(2); }
973 bool isCompareAndSwap() const {
974 unsigned Op = getOpcode();
975 return Op == ISD::ATOMIC_CMP_SWAP;
978 // Methods to support isa and dyn_cast
979 static bool classof(const AtomicSDNode *) { return true; }
980 static bool classof(const SDNode *N) {
981 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
982 N->getOpcode() == ISD::ATOMIC_SWAP ||
983 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
984 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
985 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
986 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
987 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
988 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
989 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
990 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
991 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
992 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX;
996 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
997 /// memory and need an associated MachineMemOperand. Its opcode may be
998 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, or a target-specific opcode with a
999 /// value not less than FIRST_TARGET_MEMORY_OPCODE.
1000 class MemIntrinsicSDNode : public MemSDNode {
1002 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1003 const SDValue *Ops, unsigned NumOps,
1004 EVT MemoryVT, MachineMemOperand *MMO)
1005 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1008 // Methods to support isa and dyn_cast
1009 static bool classof(const MemIntrinsicSDNode *) { return true; }
1010 static bool classof(const SDNode *N) {
1011 // We lower some target intrinsics to their target opcode
1012 // early a node with a target opcode can be of this class
1013 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1014 N->getOpcode() == ISD::INTRINSIC_VOID ||
1015 N->isTargetMemoryOpcode();
1019 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1020 /// support for the llvm IR shufflevector instruction. It combines elements
1021 /// from two input vectors into a new input vector, with the selection and
1022 /// ordering of elements determined by an array of integers, referred to as
1023 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1024 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1025 /// An index of -1 is treated as undef, such that the code generator may put
1026 /// any value in the corresponding element of the result.
1027 class ShuffleVectorSDNode : public SDNode {
1030 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1031 // is freed when the SelectionDAG object is destroyed.
1034 friend class SelectionDAG;
1035 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1037 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1038 InitOperands(Ops, N1, N2);
1042 void getMask(SmallVectorImpl<int> &M) const {
1043 EVT VT = getValueType(0);
1045 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1046 M.push_back(Mask[i]);
1048 int getMaskElt(unsigned Idx) const {
1049 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1053 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1054 int getSplatIndex() const {
1055 assert(isSplat() && "Cannot get splat index for non-splat!");
1056 EVT VT = getValueType(0);
1057 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1063 static bool isSplatMask(const int *Mask, EVT VT);
1065 static bool classof(const ShuffleVectorSDNode *) { return true; }
1066 static bool classof(const SDNode *N) {
1067 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1071 class ConstantSDNode : public SDNode {
1072 const ConstantInt *Value;
1073 friend class SelectionDAG;
1074 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1075 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1076 DebugLoc(), getSDVTList(VT)), Value(val) {
1080 const ConstantInt *getConstantIntValue() const { return Value; }
1081 const APInt &getAPIntValue() const { return Value->getValue(); }
1082 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1083 int64_t getSExtValue() const { return Value->getSExtValue(); }
1085 bool isOne() const { return Value->isOne(); }
1086 bool isNullValue() const { return Value->isNullValue(); }
1087 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1089 static bool classof(const ConstantSDNode *) { return true; }
1090 static bool classof(const SDNode *N) {
1091 return N->getOpcode() == ISD::Constant ||
1092 N->getOpcode() == ISD::TargetConstant;
1096 class ConstantFPSDNode : public SDNode {
1097 const ConstantFP *Value;
1098 friend class SelectionDAG;
1099 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1100 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1101 DebugLoc(), getSDVTList(VT)), Value(val) {
1105 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1106 const ConstantFP *getConstantFPValue() const { return Value; }
1108 /// isZero - Return true if the value is positive or negative zero.
1109 bool isZero() const { return Value->isZero(); }
1111 /// isNaN - Return true if the value is a NaN.
1112 bool isNaN() const { return Value->isNaN(); }
1114 /// isExactlyValue - We don't rely on operator== working on double values, as
1115 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1116 /// As such, this method can be used to do an exact bit-for-bit comparison of
1117 /// two floating point values.
1119 /// We leave the version with the double argument here because it's just so
1120 /// convenient to write "2.0" and the like. Without this function we'd
1121 /// have to duplicate its logic everywhere it's called.
1122 bool isExactlyValue(double V) const {
1124 // convert is not supported on this type
1125 if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1128 Tmp.convert(Value->getValueAPF().getSemantics(),
1129 APFloat::rmNearestTiesToEven, &ignored);
1130 return isExactlyValue(Tmp);
1132 bool isExactlyValue(const APFloat& V) const;
1134 static bool isValueValidForType(EVT VT, const APFloat& Val);
1136 static bool classof(const ConstantFPSDNode *) { return true; }
1137 static bool classof(const SDNode *N) {
1138 return N->getOpcode() == ISD::ConstantFP ||
1139 N->getOpcode() == ISD::TargetConstantFP;
1143 class GlobalAddressSDNode : public SDNode {
1144 const GlobalValue *TheGlobal;
1146 unsigned char TargetFlags;
1147 friend class SelectionDAG;
1148 GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA, EVT VT,
1149 int64_t o, unsigned char TargetFlags);
1152 const GlobalValue *getGlobal() const { return TheGlobal; }
1153 int64_t getOffset() const { return Offset; }
1154 unsigned char getTargetFlags() const { return TargetFlags; }
1155 // Return the address space this GlobalAddress belongs to.
1156 unsigned getAddressSpace() const;
1158 static bool classof(const GlobalAddressSDNode *) { return true; }
1159 static bool classof(const SDNode *N) {
1160 return N->getOpcode() == ISD::GlobalAddress ||
1161 N->getOpcode() == ISD::TargetGlobalAddress ||
1162 N->getOpcode() == ISD::GlobalTLSAddress ||
1163 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1167 class FrameIndexSDNode : public SDNode {
1169 friend class SelectionDAG;
1170 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1171 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1172 DebugLoc(), getSDVTList(VT)), FI(fi) {
1176 int getIndex() const { return FI; }
1178 static bool classof(const FrameIndexSDNode *) { return true; }
1179 static bool classof(const SDNode *N) {
1180 return N->getOpcode() == ISD::FrameIndex ||
1181 N->getOpcode() == ISD::TargetFrameIndex;
1185 class JumpTableSDNode : public SDNode {
1187 unsigned char TargetFlags;
1188 friend class SelectionDAG;
1189 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1190 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1191 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1195 int getIndex() const { return JTI; }
1196 unsigned char getTargetFlags() const { return TargetFlags; }
1198 static bool classof(const JumpTableSDNode *) { return true; }
1199 static bool classof(const SDNode *N) {
1200 return N->getOpcode() == ISD::JumpTable ||
1201 N->getOpcode() == ISD::TargetJumpTable;
1205 class ConstantPoolSDNode : public SDNode {
1207 const Constant *ConstVal;
1208 MachineConstantPoolValue *MachineCPVal;
1210 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1211 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1212 unsigned char TargetFlags;
1213 friend class SelectionDAG;
1214 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1215 unsigned Align, unsigned char TF)
1216 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1218 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1219 assert((int)Offset >= 0 && "Offset is too large");
1222 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1223 EVT VT, int o, unsigned Align, unsigned char TF)
1224 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1226 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1227 assert((int)Offset >= 0 && "Offset is too large");
1228 Val.MachineCPVal = v;
1229 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1234 bool isMachineConstantPoolEntry() const {
1235 return (int)Offset < 0;
1238 const Constant *getConstVal() const {
1239 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1240 return Val.ConstVal;
1243 MachineConstantPoolValue *getMachineCPVal() const {
1244 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1245 return Val.MachineCPVal;
1248 int getOffset() const {
1249 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1252 // Return the alignment of this constant pool object, which is either 0 (for
1253 // default alignment) or the desired value.
1254 unsigned getAlignment() const { return Alignment; }
1255 unsigned char getTargetFlags() const { return TargetFlags; }
1257 const Type *getType() const;
1259 static bool classof(const ConstantPoolSDNode *) { return true; }
1260 static bool classof(const SDNode *N) {
1261 return N->getOpcode() == ISD::ConstantPool ||
1262 N->getOpcode() == ISD::TargetConstantPool;
1266 class BasicBlockSDNode : public SDNode {
1267 MachineBasicBlock *MBB;
1268 friend class SelectionDAG;
1269 /// Debug info is meaningful and potentially useful here, but we create
1270 /// blocks out of order when they're jumped to, which makes it a bit
1271 /// harder. Let's see if we need it first.
1272 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1273 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1277 MachineBasicBlock *getBasicBlock() const { return MBB; }
1279 static bool classof(const BasicBlockSDNode *) { return true; }
1280 static bool classof(const SDNode *N) {
1281 return N->getOpcode() == ISD::BasicBlock;
1285 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1287 class BuildVectorSDNode : public SDNode {
1288 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1289 explicit BuildVectorSDNode(); // Do not implement
1291 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1292 /// smallest element size that splats the vector. If MinSplatBits is
1293 /// nonzero, the element size must be at least that large. Note that the
1294 /// splat element may be the entire vector (i.e., a one element vector).
1295 /// Returns the splat element value in SplatValue. Any undefined bits in
1296 /// that value are zero, and the corresponding bits in the SplatUndef mask
1297 /// are set. The SplatBitSize value is set to the splat element size in
1298 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1299 /// undefined. isBigEndian describes the endianness of the target.
1300 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1301 unsigned &SplatBitSize, bool &HasAnyUndefs,
1302 unsigned MinSplatBits = 0, bool isBigEndian = false);
1304 static inline bool classof(const BuildVectorSDNode *) { return true; }
1305 static inline bool classof(const SDNode *N) {
1306 return N->getOpcode() == ISD::BUILD_VECTOR;
1310 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1311 /// used when the SelectionDAG needs to make a simple reference to something
1312 /// in the LLVM IR representation.
1314 class SrcValueSDNode : public SDNode {
1316 friend class SelectionDAG;
1317 /// Create a SrcValue for a general value.
1318 explicit SrcValueSDNode(const Value *v)
1319 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1322 /// getValue - return the contained Value.
1323 const Value *getValue() const { return V; }
1325 static bool classof(const SrcValueSDNode *) { return true; }
1326 static bool classof(const SDNode *N) {
1327 return N->getOpcode() == ISD::SRCVALUE;
1331 class MDNodeSDNode : public SDNode {
1333 friend class SelectionDAG;
1334 explicit MDNodeSDNode(const MDNode *md)
1335 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1338 const MDNode *getMD() const { return MD; }
1340 static bool classof(const MDNodeSDNode *) { return true; }
1341 static bool classof(const SDNode *N) {
1342 return N->getOpcode() == ISD::MDNODE_SDNODE;
1347 class RegisterSDNode : public SDNode {
1349 friend class SelectionDAG;
1350 RegisterSDNode(unsigned reg, EVT VT)
1351 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1355 unsigned getReg() const { return Reg; }
1357 static bool classof(const RegisterSDNode *) { return true; }
1358 static bool classof(const SDNode *N) {
1359 return N->getOpcode() == ISD::Register;
1363 class BlockAddressSDNode : public SDNode {
1364 const BlockAddress *BA;
1365 unsigned char TargetFlags;
1366 friend class SelectionDAG;
1367 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1368 unsigned char Flags)
1369 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1370 BA(ba), TargetFlags(Flags) {
1373 const BlockAddress *getBlockAddress() const { return BA; }
1374 unsigned char getTargetFlags() const { return TargetFlags; }
1376 static bool classof(const BlockAddressSDNode *) { return true; }
1377 static bool classof(const SDNode *N) {
1378 return N->getOpcode() == ISD::BlockAddress ||
1379 N->getOpcode() == ISD::TargetBlockAddress;
1383 class EHLabelSDNode : public SDNode {
1386 friend class SelectionDAG;
1387 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1388 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1389 InitOperands(&Chain, ch);
1392 MCSymbol *getLabel() const { return Label; }
1394 static bool classof(const EHLabelSDNode *) { return true; }
1395 static bool classof(const SDNode *N) {
1396 return N->getOpcode() == ISD::EH_LABEL;
1400 class ExternalSymbolSDNode : public SDNode {
1402 unsigned char TargetFlags;
1404 friend class SelectionDAG;
1405 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1406 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1407 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1411 const char *getSymbol() const { return Symbol; }
1412 unsigned char getTargetFlags() const { return TargetFlags; }
1414 static bool classof(const ExternalSymbolSDNode *) { return true; }
1415 static bool classof(const SDNode *N) {
1416 return N->getOpcode() == ISD::ExternalSymbol ||
1417 N->getOpcode() == ISD::TargetExternalSymbol;
1421 class CondCodeSDNode : public SDNode {
1422 ISD::CondCode Condition;
1423 friend class SelectionDAG;
1424 explicit CondCodeSDNode(ISD::CondCode Cond)
1425 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1430 ISD::CondCode get() const { return Condition; }
1432 static bool classof(const CondCodeSDNode *) { return true; }
1433 static bool classof(const SDNode *N) {
1434 return N->getOpcode() == ISD::CONDCODE;
1438 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1439 /// future and most targets don't support it.
1440 class CvtRndSatSDNode : public SDNode {
1441 ISD::CvtCode CvtCode;
1442 friend class SelectionDAG;
1443 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1444 unsigned NumOps, ISD::CvtCode Code)
1445 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1447 assert(NumOps == 5 && "wrong number of operations");
1450 ISD::CvtCode getCvtCode() const { return CvtCode; }
1452 static bool classof(const CvtRndSatSDNode *) { return true; }
1453 static bool classof(const SDNode *N) {
1454 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1461 static const uint64_t NoFlagSet = 0ULL;
1462 static const uint64_t ZExt = 1ULL<<0; ///< Zero extended
1463 static const uint64_t ZExtOffs = 0;
1464 static const uint64_t SExt = 1ULL<<1; ///< Sign extended
1465 static const uint64_t SExtOffs = 1;
1466 static const uint64_t InReg = 1ULL<<2; ///< Passed in register
1467 static const uint64_t InRegOffs = 2;
1468 static const uint64_t SRet = 1ULL<<3; ///< Hidden struct-ret ptr
1469 static const uint64_t SRetOffs = 3;
1470 static const uint64_t ByVal = 1ULL<<4; ///< Struct passed by value
1471 static const uint64_t ByValOffs = 4;
1472 static const uint64_t Nest = 1ULL<<5; ///< Nested fn static chain
1473 static const uint64_t NestOffs = 5;
1474 static const uint64_t ByValAlign = 0xFULL << 6; //< Struct alignment
1475 static const uint64_t ByValAlignOffs = 6;
1476 static const uint64_t Split = 1ULL << 10;
1477 static const uint64_t SplitOffs = 10;
1478 static const uint64_t OrigAlign = 0x1FULL<<27;
1479 static const uint64_t OrigAlignOffs = 27;
1480 static const uint64_t ByValSize = 0xffffffffULL << 32; //< Struct size
1481 static const uint64_t ByValSizeOffs = 32;
1483 static const uint64_t One = 1ULL; //< 1 of this type, for shifts
1487 ArgFlagsTy() : Flags(0) { }
1489 bool isZExt() const { return Flags & ZExt; }
1490 void setZExt() { Flags |= One << ZExtOffs; }
1492 bool isSExt() const { return Flags & SExt; }
1493 void setSExt() { Flags |= One << SExtOffs; }
1495 bool isInReg() const { return Flags & InReg; }
1496 void setInReg() { Flags |= One << InRegOffs; }
1498 bool isSRet() const { return Flags & SRet; }
1499 void setSRet() { Flags |= One << SRetOffs; }
1501 bool isByVal() const { return Flags & ByVal; }
1502 void setByVal() { Flags |= One << ByValOffs; }
1504 bool isNest() const { return Flags & Nest; }
1505 void setNest() { Flags |= One << NestOffs; }
1507 unsigned getByValAlign() const {
1509 ((One << ((Flags & ByValAlign) >> ByValAlignOffs)) / 2);
1511 void setByValAlign(unsigned A) {
1512 Flags = (Flags & ~ByValAlign) |
1513 (uint64_t(Log2_32(A) + 1) << ByValAlignOffs);
1516 bool isSplit() const { return Flags & Split; }
1517 void setSplit() { Flags |= One << SplitOffs; }
1519 unsigned getOrigAlign() const {
1521 ((One << ((Flags & OrigAlign) >> OrigAlignOffs)) / 2);
1523 void setOrigAlign(unsigned A) {
1524 Flags = (Flags & ~OrigAlign) |
1525 (uint64_t(Log2_32(A) + 1) << OrigAlignOffs);
1528 unsigned getByValSize() const {
1529 return (unsigned)((Flags & ByValSize) >> ByValSizeOffs);
1531 void setByValSize(unsigned S) {
1532 Flags = (Flags & ~ByValSize) | (uint64_t(S) << ByValSizeOffs);
1535 /// getArgFlagsString - Returns the flags as a string, eg: "zext align:4".
1536 std::string getArgFlagsString();
1538 /// getRawBits - Represent the flags as a bunch of bits.
1539 uint64_t getRawBits() const { return Flags; }
1542 /// InputArg - This struct carries flags and type information about a
1543 /// single incoming (formal) argument or incoming (from the perspective
1544 /// of the caller) return value virtual register.
1551 InputArg() : VT(MVT::Other), Used(false) {}
1552 InputArg(ISD::ArgFlagsTy flags, EVT vt, bool used)
1553 : Flags(flags), VT(vt), Used(used) {
1554 assert(VT.isSimple() &&
1555 "InputArg value type must be Simple!");
1559 /// OutputArg - This struct carries flags and a value for a
1560 /// single outgoing (actual) argument or outgoing (from the perspective
1561 /// of the caller) return value virtual register.
1568 OutputArg() : IsFixed(false) {}
1569 OutputArg(ISD::ArgFlagsTy flags, SDValue val, bool isfixed)
1570 : Flags(flags), Val(val), IsFixed(isfixed) {
1571 assert(Val.getValueType().isSimple() &&
1572 "OutputArg value type must be Simple!");
1577 /// VTSDNode - This class is used to represent EVT's, which are used
1578 /// to parameterize some operations.
1579 class VTSDNode : public SDNode {
1581 friend class SelectionDAG;
1582 explicit VTSDNode(EVT VT)
1583 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1588 EVT getVT() const { return ValueType; }
1590 static bool classof(const VTSDNode *) { return true; }
1591 static bool classof(const SDNode *N) {
1592 return N->getOpcode() == ISD::VALUETYPE;
1596 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1598 class LSBaseSDNode : public MemSDNode {
1599 //! Operand array for load and store
1601 \note Moving this array to the base class captures more
1602 common functionality shared between LoadSDNode and
1607 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1608 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1609 EVT MemVT, MachineMemOperand *MMO)
1610 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1611 SubclassData |= AM << 2;
1612 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1613 InitOperands(Ops, Operands, numOperands);
1614 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1615 "Only indexed loads and stores have a non-undef offset operand");
1618 const SDValue &getOffset() const {
1619 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1622 /// getAddressingMode - Return the addressing mode for this load or store:
1623 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1624 ISD::MemIndexedMode getAddressingMode() const {
1625 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1628 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1629 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1631 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1632 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1634 static bool classof(const LSBaseSDNode *) { return true; }
1635 static bool classof(const SDNode *N) {
1636 return N->getOpcode() == ISD::LOAD ||
1637 N->getOpcode() == ISD::STORE;
1641 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1643 class LoadSDNode : public LSBaseSDNode {
1644 friend class SelectionDAG;
1645 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1646 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1647 MachineMemOperand *MMO)
1648 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1649 VTs, AM, MemVT, MMO) {
1650 SubclassData |= (unsigned short)ETy;
1651 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1652 assert(readMem() && "Load MachineMemOperand is not a load!");
1653 assert(!writeMem() && "Load MachineMemOperand is a store!");
1657 /// getExtensionType - Return whether this is a plain node,
1658 /// or one of the varieties of value-extending loads.
1659 ISD::LoadExtType getExtensionType() const {
1660 return ISD::LoadExtType(SubclassData & 3);
1663 const SDValue &getBasePtr() const { return getOperand(1); }
1664 const SDValue &getOffset() const { return getOperand(2); }
1666 static bool classof(const LoadSDNode *) { return true; }
1667 static bool classof(const SDNode *N) {
1668 return N->getOpcode() == ISD::LOAD;
1672 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1674 class StoreSDNode : public LSBaseSDNode {
1675 friend class SelectionDAG;
1676 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1677 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1678 MachineMemOperand *MMO)
1679 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1680 VTs, AM, MemVT, MMO) {
1681 SubclassData |= (unsigned short)isTrunc;
1682 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1683 assert(!readMem() && "Store MachineMemOperand is a load!");
1684 assert(writeMem() && "Store MachineMemOperand is not a store!");
1688 /// isTruncatingStore - Return true if the op does a truncation before store.
1689 /// For integers this is the same as doing a TRUNCATE and storing the result.
1690 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1691 bool isTruncatingStore() const { return SubclassData & 1; }
1693 const SDValue &getValue() const { return getOperand(1); }
1694 const SDValue &getBasePtr() const { return getOperand(2); }
1695 const SDValue &getOffset() const { return getOperand(3); }
1697 static bool classof(const StoreSDNode *) { return true; }
1698 static bool classof(const SDNode *N) {
1699 return N->getOpcode() == ISD::STORE;
1703 /// MachineSDNode - An SDNode that represents everything that will be needed
1704 /// to construct a MachineInstr. These nodes are created during the
1705 /// instruction selection proper phase.
1707 class MachineSDNode : public SDNode {
1709 typedef MachineMemOperand **mmo_iterator;
1712 friend class SelectionDAG;
1713 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1714 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1716 /// LocalOperands - Operands for this instruction, if they fit here. If
1717 /// they don't, this field is unused.
1718 SDUse LocalOperands[4];
1720 /// MemRefs - Memory reference descriptions for this instruction.
1721 mmo_iterator MemRefs;
1722 mmo_iterator MemRefsEnd;
1725 mmo_iterator memoperands_begin() const { return MemRefs; }
1726 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1727 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1729 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1730 /// list. This does not transfer ownership.
1731 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1732 MemRefs = NewMemRefs;
1733 MemRefsEnd = NewMemRefsEnd;
1736 static bool classof(const MachineSDNode *) { return true; }
1737 static bool classof(const SDNode *N) {
1738 return N->isMachineOpcode();
1742 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1743 SDNode, ptrdiff_t> {
1747 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1749 bool operator==(const SDNodeIterator& x) const {
1750 return Operand == x.Operand;
1752 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1754 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1755 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1756 Operand = I.Operand;
1760 pointer operator*() const {
1761 return Node->getOperand(Operand).getNode();
1763 pointer operator->() const { return operator*(); }
1765 SDNodeIterator& operator++() { // Preincrement
1769 SDNodeIterator operator++(int) { // Postincrement
1770 SDNodeIterator tmp = *this; ++*this; return tmp;
1772 size_t operator-(SDNodeIterator Other) const {
1773 assert(Node == Other.Node &&
1774 "Cannot compare iterators of two different nodes!");
1775 return Operand - Other.Operand;
1778 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1779 static SDNodeIterator end (SDNode *N) {
1780 return SDNodeIterator(N, N->getNumOperands());
1783 unsigned getOperand() const { return Operand; }
1784 const SDNode *getNode() const { return Node; }
1787 template <> struct GraphTraits<SDNode*> {
1788 typedef SDNode NodeType;
1789 typedef SDNodeIterator ChildIteratorType;
1790 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1791 static inline ChildIteratorType child_begin(NodeType *N) {
1792 return SDNodeIterator::begin(N);
1794 static inline ChildIteratorType child_end(NodeType *N) {
1795 return SDNodeIterator::end(N);
1799 /// LargestSDNode - The largest SDNode class.
1801 typedef LoadSDNode LargestSDNode;
1803 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1806 typedef GlobalAddressSDNode MostAlignedSDNode;
1809 /// isNormalLoad - Returns true if the specified node is a non-extending
1810 /// and unindexed load.
1811 inline bool isNormalLoad(const SDNode *N) {
1812 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1813 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1814 Ld->getAddressingMode() == ISD::UNINDEXED;
1817 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1819 inline bool isNON_EXTLoad(const SDNode *N) {
1820 return isa<LoadSDNode>(N) &&
1821 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1824 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1826 inline bool isEXTLoad(const SDNode *N) {
1827 return isa<LoadSDNode>(N) &&
1828 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1831 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1833 inline bool isSEXTLoad(const SDNode *N) {
1834 return isa<LoadSDNode>(N) &&
1835 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1838 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1840 inline bool isZEXTLoad(const SDNode *N) {
1841 return isa<LoadSDNode>(N) &&
1842 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1845 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1847 inline bool isUNINDEXEDLoad(const SDNode *N) {
1848 return isa<LoadSDNode>(N) &&
1849 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1852 /// isNormalStore - Returns true if the specified node is a non-truncating
1853 /// and unindexed store.
1854 inline bool isNormalStore(const SDNode *N) {
1855 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1856 return St && !St->isTruncatingStore() &&
1857 St->getAddressingMode() == ISD::UNINDEXED;
1860 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1862 inline bool isNON_TRUNCStore(const SDNode *N) {
1863 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1866 /// isTRUNCStore - Returns true if the specified node is a truncating
1868 inline bool isTRUNCStore(const SDNode *N) {
1869 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1872 /// isUNINDEXEDStore - Returns true if the specified node is an
1873 /// unindexed store.
1874 inline bool isUNINDEXEDStore(const SDNode *N) {
1875 return isa<StoreSDNode>(N) &&
1876 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1880 } // end llvm namespace