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/Instructions.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/GraphTraits.h"
26 #include "llvm/ADT/ilist_node.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/CodeGen/ISDOpcodes.h"
31 #include "llvm/CodeGen/ValueTypes.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/Support/DebugLoc.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);
77 } // end llvm:ISD namespace
79 //===----------------------------------------------------------------------===//
80 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
81 /// values as the result of a computation. Many nodes return multiple values,
82 /// from loads (which define a token and a return value) to ADDC (which returns
83 /// a result and a carry value), to calls (which may return an arbitrary number
86 /// As such, each use of a SelectionDAG computation must indicate the node that
87 /// computes it as well as which return value to use from that node. This pair
88 /// of information is represented with the SDValue value type.
91 SDNode *Node; // The node defining the value we are using.
92 unsigned ResNo; // Which return value of the node we are using.
94 SDValue() : Node(0), ResNo(0) {}
95 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
97 /// get the index which selects a specific result in the SDNode
98 unsigned getResNo() const { return ResNo; }
100 /// get the SDNode which holds the desired result
101 SDNode *getNode() const { return Node; }
104 void setNode(SDNode *N) { Node = N; }
106 inline SDNode *operator->() const { return Node; }
108 bool operator==(const SDValue &O) const {
109 return Node == O.Node && ResNo == O.ResNo;
111 bool operator!=(const SDValue &O) const {
112 return !operator==(O);
114 bool operator<(const SDValue &O) const {
115 return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
118 SDValue getValue(unsigned R) const {
119 return SDValue(Node, R);
122 // isOperandOf - Return true if this node is an operand of N.
123 bool isOperandOf(SDNode *N) const;
125 /// getValueType - Return the ValueType of the referenced return value.
127 inline EVT getValueType() const;
129 /// getValueSizeInBits - Returns the size of the value in bits.
131 unsigned getValueSizeInBits() const {
132 return getValueType().getSizeInBits();
135 // Forwarding methods - These forward to the corresponding methods in SDNode.
136 inline unsigned getOpcode() const;
137 inline unsigned getNumOperands() const;
138 inline const SDValue &getOperand(unsigned i) const;
139 inline uint64_t getConstantOperandVal(unsigned i) const;
140 inline bool isTargetMemoryOpcode() const;
141 inline bool isTargetOpcode() const;
142 inline bool isMachineOpcode() const;
143 inline unsigned getMachineOpcode() const;
144 inline const DebugLoc getDebugLoc() const;
147 /// reachesChainWithoutSideEffects - Return true if this operand (which must
148 /// be a chain) reaches the specified operand without crossing any
149 /// side-effecting instructions. In practice, this looks through token
150 /// factors and non-volatile loads. In order to remain efficient, this only
151 /// looks a couple of nodes in, it does not do an exhaustive search.
152 bool reachesChainWithoutSideEffects(SDValue Dest,
153 unsigned Depth = 2) const;
155 /// use_empty - Return true if there are no nodes using value ResNo
158 inline bool use_empty() const;
160 /// hasOneUse - Return true if there is exactly one node using value
163 inline bool hasOneUse() const;
167 template<> struct DenseMapInfo<SDValue> {
168 static inline SDValue getEmptyKey() {
169 return SDValue((SDNode*)-1, -1U);
171 static inline SDValue getTombstoneKey() {
172 return SDValue((SDNode*)-1, 0);
174 static unsigned getHashValue(const SDValue &Val) {
175 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
176 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
178 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
182 template <> struct isPodLike<SDValue> { static const bool value = true; };
185 /// simplify_type specializations - Allow casting operators to work directly on
186 /// SDValues as if they were SDNode*'s.
187 template<> struct simplify_type<SDValue> {
188 typedef SDNode* SimpleType;
189 static SimpleType getSimplifiedValue(const SDValue &Val) {
190 return static_cast<SimpleType>(Val.getNode());
193 template<> struct simplify_type<const SDValue> {
194 typedef SDNode* SimpleType;
195 static SimpleType getSimplifiedValue(const SDValue &Val) {
196 return static_cast<SimpleType>(Val.getNode());
200 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
201 /// which records the SDNode being used and the result number, a
202 /// pointer to the SDNode using the value, and Next and Prev pointers,
203 /// which link together all the uses of an SDNode.
206 /// Val - The value being used.
208 /// User - The user of this value.
210 /// Prev, Next - Pointers to the uses list of the SDNode referred by
214 SDUse(const SDUse &U); // Do not implement
215 void operator=(const SDUse &U); // Do not implement
218 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
220 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
221 operator const SDValue&() const { return Val; }
223 /// If implicit conversion to SDValue doesn't work, the get() method returns
225 const SDValue &get() const { return Val; }
227 /// getUser - This returns the SDNode that contains this Use.
228 SDNode *getUser() { return User; }
230 /// getNext - Get the next SDUse in the use list.
231 SDUse *getNext() const { return Next; }
233 /// getNode - Convenience function for get().getNode().
234 SDNode *getNode() const { return Val.getNode(); }
235 /// getResNo - Convenience function for get().getResNo().
236 unsigned getResNo() const { return Val.getResNo(); }
237 /// getValueType - Convenience function for get().getValueType().
238 EVT getValueType() const { return Val.getValueType(); }
240 /// operator== - Convenience function for get().operator==
241 bool operator==(const SDValue &V) const {
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 {
256 friend class SelectionDAG;
259 void setUser(SDNode *p) { User = p; }
261 /// set - Remove this use from its existing use list, assign it the
262 /// given value, and add it to the new value's node's use list.
263 inline void set(const SDValue &V);
264 /// setInitial - like set, but only supports initializing a newly-allocated
265 /// SDUse with a non-null value.
266 inline void setInitial(const SDValue &V);
267 /// setNode - like set, but only sets the Node portion of the value,
268 /// leaving the ResNo portion unmodified.
269 inline void setNode(SDNode *N);
271 void addToList(SDUse **List) {
273 if (Next) Next->Prev = &Next;
278 void removeFromList() {
280 if (Next) Next->Prev = Prev;
284 /// simplify_type specializations - Allow casting operators to work directly on
285 /// SDValues as if they were SDNode*'s.
286 template<> struct simplify_type<SDUse> {
287 typedef SDNode* SimpleType;
288 static SimpleType getSimplifiedValue(const SDUse &Val) {
289 return static_cast<SimpleType>(Val.getNode());
292 template<> struct simplify_type<const SDUse> {
293 typedef SDNode* SimpleType;
294 static SimpleType getSimplifiedValue(const SDUse &Val) {
295 return static_cast<SimpleType>(Val.getNode());
300 /// SDNode - Represents one node in the SelectionDAG.
302 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
304 /// NodeType - The operation that this node performs.
308 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
309 /// then they will be delete[]'d when the node is destroyed.
310 uint16_t OperandsNeedDelete : 1;
312 /// HasDebugValue - This tracks whether this node has one or more dbg_value
313 /// nodes corresponding to it.
314 uint16_t HasDebugValue : 1;
317 /// SubclassData - This member is defined by this class, but is not used for
318 /// anything. Subclasses can use it to hold whatever state they find useful.
319 /// This field is initialized to zero by the ctor.
320 uint16_t SubclassData : 14;
323 /// NodeId - Unique id per SDNode in the DAG.
326 /// OperandList - The values that are used by this operation.
330 /// ValueList - The types of the values this node defines. SDNode's may
331 /// define multiple values simultaneously.
332 const EVT *ValueList;
334 /// UseList - List of uses for this SDNode.
337 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
338 unsigned short NumOperands, NumValues;
340 /// debugLoc - source line information.
343 /// getValueTypeList - Return a pointer to the specified value type.
344 static const EVT *getValueTypeList(EVT VT);
346 friend class SelectionDAG;
347 friend struct ilist_traits<SDNode>;
350 //===--------------------------------------------------------------------===//
354 /// getOpcode - Return the SelectionDAG opcode value for this node. For
355 /// pre-isel nodes (those for which isMachineOpcode returns false), these
356 /// are the opcode values in the ISD and <target>ISD namespaces. For
357 /// post-isel opcodes, see getMachineOpcode.
358 unsigned getOpcode() const { return (unsigned short)NodeType; }
360 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
361 /// \<target\>ISD namespace).
362 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
364 /// isTargetMemoryOpcode - Test if this node has a target-specific
365 /// memory-referencing opcode (in the \<target\>ISD namespace and
366 /// greater than FIRST_TARGET_MEMORY_OPCODE).
367 bool isTargetMemoryOpcode() const {
368 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
371 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
372 /// corresponding to a MachineInstr opcode.
373 bool isMachineOpcode() const { return NodeType < 0; }
375 /// getMachineOpcode - This may only be called if isMachineOpcode returns
376 /// true. It returns the MachineInstr opcode value that the node's opcode
378 unsigned getMachineOpcode() const {
379 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
383 /// getHasDebugValue - get this bit.
384 bool getHasDebugValue() const { return HasDebugValue; }
386 /// setHasDebugValue - set this bit.
387 void setHasDebugValue(bool b) { HasDebugValue = b; }
389 /// use_empty - Return true if there are no uses of this node.
391 bool use_empty() const { return UseList == NULL; }
393 /// hasOneUse - Return true if there is exactly one use of this node.
395 bool hasOneUse() const {
396 return !use_empty() && llvm::next(use_begin()) == use_end();
399 /// use_size - Return the number of uses of this node. This method takes
400 /// time proportional to the number of uses.
402 size_t use_size() const { return std::distance(use_begin(), use_end()); }
404 /// getNodeId - Return the unique node id.
406 int getNodeId() const { return NodeId; }
408 /// setNodeId - Set unique node id.
409 void setNodeId(int Id) { NodeId = Id; }
411 /// getDebugLoc - Return the source location info.
412 const DebugLoc getDebugLoc() const { return debugLoc; }
414 /// setDebugLoc - Set source location info. Try to avoid this, putting
415 /// it in the constructor is preferable.
416 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
418 /// use_iterator - This class provides iterator support for SDUse
419 /// operands that use a specific SDNode.
421 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
423 explicit use_iterator(SDUse *op) : Op(op) {
427 typedef std::iterator<std::forward_iterator_tag,
428 SDUse, ptrdiff_t>::reference reference;
429 typedef std::iterator<std::forward_iterator_tag,
430 SDUse, ptrdiff_t>::pointer pointer;
432 use_iterator(const use_iterator &I) : Op(I.Op) {}
433 use_iterator() : Op(0) {}
435 bool operator==(const use_iterator &x) const {
438 bool operator!=(const use_iterator &x) const {
439 return !operator==(x);
442 /// atEnd - return true if this iterator is at the end of uses list.
443 bool atEnd() const { return Op == 0; }
445 // Iterator traversal: forward iteration only.
446 use_iterator &operator++() { // Preincrement
447 assert(Op && "Cannot increment end iterator!");
452 use_iterator operator++(int) { // Postincrement
453 use_iterator tmp = *this; ++*this; return tmp;
456 /// Retrieve a pointer to the current user node.
457 SDNode *operator*() const {
458 assert(Op && "Cannot dereference end iterator!");
459 return Op->getUser();
462 SDNode *operator->() const { return operator*(); }
464 SDUse &getUse() const { return *Op; }
466 /// getOperandNo - Retrieve the operand # of this use in its user.
468 unsigned getOperandNo() const {
469 assert(Op && "Cannot dereference end iterator!");
470 return (unsigned)(Op - Op->getUser()->OperandList);
474 /// use_begin/use_end - Provide iteration support to walk over all uses
477 use_iterator use_begin() const {
478 return use_iterator(UseList);
481 static use_iterator use_end() { return use_iterator(0); }
484 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
485 /// indicated value. This method ignores uses of other values defined by this
487 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
489 /// hasAnyUseOfValue - Return true if there are any use of the indicated
490 /// value. This method ignores uses of other values defined by this operation.
491 bool hasAnyUseOfValue(unsigned Value) const;
493 /// isOnlyUserOf - Return true if this node is the only use of N.
495 bool isOnlyUserOf(SDNode *N) const;
497 /// isOperandOf - Return true if this node is an operand of N.
499 bool isOperandOf(SDNode *N) const;
501 /// isPredecessorOf - Return true if this node is a predecessor of N.
502 /// NOTE: Implemented on top of hasPredecessor and every bit as
503 /// expensive. Use carefully.
504 bool isPredecessorOf(const SDNode *N) const { return N->hasPredecessor(this); }
506 /// hasPredecessor - Return true if N is a predecessor of this node.
507 /// N is either an operand of this node, or can be reached by recursively
508 /// traversing up the operands.
509 /// NOTE: This is an expensive method. Use it carefully.
510 bool hasPredecessor(const SDNode *N) const;
512 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
513 /// N is either an operand of this node, or can be reached by recursively
514 /// traversing up the operands.
515 /// In this helper the Visited and worklist sets are held externally to
516 /// cache predecessors over multiple invocations. If you want to test for
517 /// multiple predecessors this method is preferable to multiple calls to
518 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
520 /// NOTE: This is still very expensive. Use carefully.
521 bool hasPredecessorHelper(const SDNode *N,
522 SmallPtrSet<const SDNode *, 32> &Visited,
523 SmallVector<const SDNode *, 16> &Worklist) const;
525 /// getNumOperands - Return the number of values used by this operation.
527 unsigned getNumOperands() const { return NumOperands; }
529 /// getConstantOperandVal - Helper method returns the integer value of a
530 /// ConstantSDNode operand.
531 uint64_t getConstantOperandVal(unsigned Num) const;
533 const SDValue &getOperand(unsigned Num) const {
534 assert(Num < NumOperands && "Invalid child # of SDNode!");
535 return OperandList[Num];
538 typedef SDUse* op_iterator;
539 op_iterator op_begin() const { return OperandList; }
540 op_iterator op_end() const { return OperandList+NumOperands; }
542 SDVTList getVTList() const {
543 SDVTList X = { ValueList, NumValues };
547 /// getGluedNode - If this node has a glue operand, return the node
548 /// to which the glue operand points. Otherwise return NULL.
549 SDNode *getGluedNode() const {
550 if (getNumOperands() != 0 &&
551 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
552 return getOperand(getNumOperands()-1).getNode();
556 // If this is a pseudo op, like copyfromreg, look to see if there is a
557 // real target node glued to it. If so, return the target node.
558 const SDNode *getGluedMachineNode() const {
559 const SDNode *FoundNode = this;
561 // Climb up glue edges until a machine-opcode node is found, or the
562 // end of the chain is reached.
563 while (!FoundNode->isMachineOpcode()) {
564 const SDNode *N = FoundNode->getGluedNode();
572 /// getGluedUser - If this node has a glue value with a user, return
573 /// the user (there is at most one). Otherwise return NULL.
574 SDNode *getGluedUser() const {
575 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
576 if (UI.getUse().get().getValueType() == MVT::Glue)
581 /// getNumValues - Return the number of values defined/returned by this
584 unsigned getNumValues() const { return NumValues; }
586 /// getValueType - Return the type of a specified result.
588 EVT getValueType(unsigned ResNo) const {
589 assert(ResNo < NumValues && "Illegal result number!");
590 return ValueList[ResNo];
593 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
595 unsigned getValueSizeInBits(unsigned ResNo) const {
596 return getValueType(ResNo).getSizeInBits();
599 typedef const EVT* value_iterator;
600 value_iterator value_begin() const { return ValueList; }
601 value_iterator value_end() const { return ValueList+NumValues; }
603 /// getOperationName - Return the opcode of this operation for printing.
605 std::string getOperationName(const SelectionDAG *G = 0) const;
606 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
607 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
608 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
609 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
610 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
612 /// printrFull - Print a SelectionDAG node and all children down to
613 /// the leaves. The given SelectionDAG allows target-specific nodes
614 /// to be printed in human-readable form. Unlike printr, this will
615 /// print the whole DAG, including children that appear multiple
618 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
620 /// printrWithDepth - Print a SelectionDAG node and children up to
621 /// depth "depth." The given SelectionDAG allows target-specific
622 /// nodes to be printed in human-readable form. Unlike printr, this
623 /// will print children that appear multiple times wherever they are
626 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
627 unsigned depth = 100) const;
630 /// dump - Dump this node, for debugging.
633 /// dumpr - Dump (recursively) this node and its use-def subgraph.
636 /// dump - Dump this node, for debugging.
637 /// The given SelectionDAG allows target-specific nodes to be printed
638 /// in human-readable form.
639 void dump(const SelectionDAG *G) const;
641 /// dumpr - Dump (recursively) this node and its use-def subgraph.
642 /// The given SelectionDAG allows target-specific nodes to be printed
643 /// in human-readable form.
644 void dumpr(const SelectionDAG *G) const;
646 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
647 /// target-specific nodes to be printed in human-readable form.
648 /// Unlike dumpr, this will print the whole DAG, including children
649 /// that appear multiple times.
651 void dumprFull(const SelectionDAG *G = 0) const;
653 /// dumprWithDepth - printrWithDepth to dbgs(). The given
654 /// SelectionDAG allows target-specific nodes to be printed in
655 /// human-readable form. Unlike dumpr, this will print children
656 /// that appear multiple times wherever they are used.
658 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
661 static bool classof(const SDNode *) { return true; }
663 /// Profile - Gather unique data for the node.
665 void Profile(FoldingSetNodeID &ID) const;
667 /// addUse - This method should only be used by the SDUse class.
669 void addUse(SDUse &U) { U.addToList(&UseList); }
672 static SDVTList getSDVTList(EVT VT) {
673 SDVTList Ret = { getValueTypeList(VT), 1 };
677 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
679 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
680 SubclassData(0), NodeId(-1),
681 OperandList(NumOps ? new SDUse[NumOps] : 0),
682 ValueList(VTs.VTs), UseList(NULL),
683 NumOperands(NumOps), NumValues(VTs.NumVTs),
685 for (unsigned i = 0; i != NumOps; ++i) {
686 OperandList[i].setUser(this);
687 OperandList[i].setInitial(Ops[i]);
689 checkForCycles(this);
692 /// This constructor adds no operands itself; operands can be
693 /// set later with InitOperands.
694 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
695 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
696 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
697 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
700 /// InitOperands - Initialize the operands list of this with 1 operand.
701 void InitOperands(SDUse *Ops, const SDValue &Op0) {
702 Ops[0].setUser(this);
703 Ops[0].setInitial(Op0);
706 checkForCycles(this);
709 /// InitOperands - Initialize the operands list of this with 2 operands.
710 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
711 Ops[0].setUser(this);
712 Ops[0].setInitial(Op0);
713 Ops[1].setUser(this);
714 Ops[1].setInitial(Op1);
717 checkForCycles(this);
720 /// InitOperands - Initialize the operands list of this with 3 operands.
721 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
722 const SDValue &Op2) {
723 Ops[0].setUser(this);
724 Ops[0].setInitial(Op0);
725 Ops[1].setUser(this);
726 Ops[1].setInitial(Op1);
727 Ops[2].setUser(this);
728 Ops[2].setInitial(Op2);
731 checkForCycles(this);
734 /// InitOperands - Initialize the operands list of this with 4 operands.
735 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
736 const SDValue &Op2, const SDValue &Op3) {
737 Ops[0].setUser(this);
738 Ops[0].setInitial(Op0);
739 Ops[1].setUser(this);
740 Ops[1].setInitial(Op1);
741 Ops[2].setUser(this);
742 Ops[2].setInitial(Op2);
743 Ops[3].setUser(this);
744 Ops[3].setInitial(Op3);
747 checkForCycles(this);
750 /// InitOperands - Initialize the operands list of this with N operands.
751 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
752 for (unsigned i = 0; i != N; ++i) {
753 Ops[i].setUser(this);
754 Ops[i].setInitial(Vals[i]);
758 checkForCycles(this);
761 /// DropOperands - Release the operands and set this node to have
767 // Define inline functions from the SDValue class.
769 inline unsigned SDValue::getOpcode() const {
770 return Node->getOpcode();
772 inline EVT SDValue::getValueType() const {
773 return Node->getValueType(ResNo);
775 inline unsigned SDValue::getNumOperands() const {
776 return Node->getNumOperands();
778 inline const SDValue &SDValue::getOperand(unsigned i) const {
779 return Node->getOperand(i);
781 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
782 return Node->getConstantOperandVal(i);
784 inline bool SDValue::isTargetOpcode() const {
785 return Node->isTargetOpcode();
787 inline bool SDValue::isTargetMemoryOpcode() const {
788 return Node->isTargetMemoryOpcode();
790 inline bool SDValue::isMachineOpcode() const {
791 return Node->isMachineOpcode();
793 inline unsigned SDValue::getMachineOpcode() const {
794 return Node->getMachineOpcode();
796 inline bool SDValue::use_empty() const {
797 return !Node->hasAnyUseOfValue(ResNo);
799 inline bool SDValue::hasOneUse() const {
800 return Node->hasNUsesOfValue(1, ResNo);
802 inline const DebugLoc SDValue::getDebugLoc() const {
803 return Node->getDebugLoc();
806 // Define inline functions from the SDUse class.
808 inline void SDUse::set(const SDValue &V) {
809 if (Val.getNode()) removeFromList();
811 if (V.getNode()) V.getNode()->addUse(*this);
814 inline void SDUse::setInitial(const SDValue &V) {
816 V.getNode()->addUse(*this);
819 inline void SDUse::setNode(SDNode *N) {
820 if (Val.getNode()) removeFromList();
822 if (N) N->addUse(*this);
825 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
826 /// to allow co-allocation of node operands with the node itself.
827 class UnarySDNode : public SDNode {
830 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
831 : SDNode(Opc, dl, VTs) {
832 InitOperands(&Op, X);
836 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
837 /// to allow co-allocation of node operands with the node itself.
838 class BinarySDNode : public SDNode {
841 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
842 : SDNode(Opc, dl, VTs) {
843 InitOperands(Ops, X, Y);
847 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
848 /// to allow co-allocation of node operands with the node itself.
849 class TernarySDNode : public SDNode {
852 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
854 : SDNode(Opc, dl, VTs) {
855 InitOperands(Ops, X, Y, Z);
860 /// HandleSDNode - This class is used to form a handle around another node that
861 /// is persistent and is updated across invocations of replaceAllUsesWith on its
862 /// operand. This node should be directly created by end-users and not added to
863 /// the AllNodes list.
864 class HandleSDNode : public SDNode {
867 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
869 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
870 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
872 explicit HandleSDNode(SDValue X)
874 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
875 InitOperands(&Op, X);
878 const SDValue &getValue() const { return Op; }
881 /// Abstact virtual class for operations for memory operations
882 class MemSDNode : public SDNode {
884 // MemoryVT - VT of in-memory value.
888 /// MMO - Memory reference information.
889 MachineMemOperand *MMO;
892 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
893 MachineMemOperand *MMO);
895 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
896 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
898 bool readMem() const { return MMO->isLoad(); }
899 bool writeMem() const { return MMO->isStore(); }
901 /// Returns alignment and volatility of the memory access
902 unsigned getOriginalAlignment() const {
903 return MMO->getBaseAlignment();
905 unsigned getAlignment() const {
906 return MMO->getAlignment();
909 /// getRawSubclassData - Return the SubclassData value, which contains an
910 /// encoding of the volatile flag, as well as bits used by subclasses. This
911 /// function should only be used to compute a FoldingSetNodeID value.
912 unsigned getRawSubclassData() const {
916 // We access subclass data here so that we can check consistency
917 // with MachineMemOperand information.
918 bool isVolatile() const { return (SubclassData >> 5) & 1; }
919 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
920 bool isInvariant() const { return (SubclassData >> 7) & 1; }
922 AtomicOrdering getOrdering() const {
923 return AtomicOrdering((SubclassData >> 8) & 15);
925 SynchronizationScope getSynchScope() const {
926 return SynchronizationScope((SubclassData >> 12) & 1);
929 /// Returns the SrcValue and offset that describes the location of the access
930 const Value *getSrcValue() const { return MMO->getValue(); }
931 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
933 /// Returns the TBAAInfo that describes the dereference.
934 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
936 /// getMemoryVT - Return the type of the in-memory value.
937 EVT getMemoryVT() const { return MemoryVT; }
939 /// getMemOperand - Return a MachineMemOperand object describing the memory
940 /// reference performed by operation.
941 MachineMemOperand *getMemOperand() const { return MMO; }
943 const MachinePointerInfo &getPointerInfo() const {
944 return MMO->getPointerInfo();
947 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
948 /// to reflect the alignment of NewMMO, if it has a greater alignment.
949 /// This must only be used when the new alignment applies to all users of
950 /// this MachineMemOperand.
951 void refineAlignment(const MachineMemOperand *NewMMO) {
952 MMO->refineAlignment(NewMMO);
955 const SDValue &getChain() const { return getOperand(0); }
956 const SDValue &getBasePtr() const {
957 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
960 // Methods to support isa and dyn_cast
961 static bool classof(const MemSDNode *) { return true; }
962 static bool classof(const SDNode *N) {
963 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
964 // with either an intrinsic or a target opcode.
965 return N->getOpcode() == ISD::LOAD ||
966 N->getOpcode() == ISD::STORE ||
967 N->getOpcode() == ISD::PREFETCH ||
968 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
969 N->getOpcode() == ISD::ATOMIC_SWAP ||
970 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
971 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
972 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
973 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
974 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
975 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
976 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
977 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
978 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
979 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
980 N->getOpcode() == ISD::ATOMIC_LOAD ||
981 N->getOpcode() == ISD::ATOMIC_STORE ||
982 N->isTargetMemoryOpcode();
986 /// AtomicSDNode - A SDNode reprenting atomic operations.
988 class AtomicSDNode : public MemSDNode {
991 void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
992 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
993 assert((Ordering & 15) == Ordering &&
994 "Ordering may not require more than 4 bits!");
995 assert((SynchScope & 1) == SynchScope &&
996 "SynchScope may not require more than 1 bit!");
997 SubclassData |= Ordering << 8;
998 SubclassData |= SynchScope << 12;
999 assert(getOrdering() == Ordering && "Ordering encoding error!");
1000 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1002 assert((readMem() || getOrdering() <= Monotonic) &&
1003 "Acquire/Release MachineMemOperand must be a load!");
1004 assert((writeMem() || getOrdering() <= Monotonic) &&
1005 "Acquire/Release MachineMemOperand must be a store!");
1009 // Opc: opcode for atomic
1010 // VTL: value type list
1011 // Chain: memory chain for operaand
1012 // Ptr: address to update as a SDValue
1013 // Cmp: compare value
1015 // SrcVal: address to update as a Value (used for MemOperand)
1016 // Align: alignment of memory
1017 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1018 SDValue Chain, SDValue Ptr,
1019 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1020 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1021 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1022 InitAtomic(Ordering, SynchScope);
1023 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1025 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1026 SDValue Chain, SDValue Ptr,
1027 SDValue Val, MachineMemOperand *MMO,
1028 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1029 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1030 InitAtomic(Ordering, SynchScope);
1031 InitOperands(Ops, Chain, Ptr, Val);
1033 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1034 SDValue Chain, SDValue Ptr,
1035 MachineMemOperand *MMO,
1036 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1037 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1038 InitAtomic(Ordering, SynchScope);
1039 InitOperands(Ops, Chain, Ptr);
1042 const SDValue &getBasePtr() const { return getOperand(1); }
1043 const SDValue &getVal() const { return getOperand(2); }
1045 bool isCompareAndSwap() const {
1046 unsigned Op = getOpcode();
1047 return Op == ISD::ATOMIC_CMP_SWAP;
1050 // Methods to support isa and dyn_cast
1051 static bool classof(const AtomicSDNode *) { return true; }
1052 static bool classof(const SDNode *N) {
1053 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1054 N->getOpcode() == ISD::ATOMIC_SWAP ||
1055 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1056 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1057 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1058 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1059 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1060 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1061 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1062 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1063 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1064 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1065 N->getOpcode() == ISD::ATOMIC_LOAD ||
1066 N->getOpcode() == ISD::ATOMIC_STORE;
1070 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1071 /// memory and need an associated MachineMemOperand. Its opcode may be
1072 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1073 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1074 class MemIntrinsicSDNode : public MemSDNode {
1076 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1077 const SDValue *Ops, unsigned NumOps,
1078 EVT MemoryVT, MachineMemOperand *MMO)
1079 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1082 // Methods to support isa and dyn_cast
1083 static bool classof(const MemIntrinsicSDNode *) { return true; }
1084 static bool classof(const SDNode *N) {
1085 // We lower some target intrinsics to their target opcode
1086 // early a node with a target opcode can be of this class
1087 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1088 N->getOpcode() == ISD::INTRINSIC_VOID ||
1089 N->getOpcode() == ISD::PREFETCH ||
1090 N->isTargetMemoryOpcode();
1094 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1095 /// support for the llvm IR shufflevector instruction. It combines elements
1096 /// from two input vectors into a new input vector, with the selection and
1097 /// ordering of elements determined by an array of integers, referred to as
1098 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1099 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1100 /// An index of -1 is treated as undef, such that the code generator may put
1101 /// any value in the corresponding element of the result.
1102 class ShuffleVectorSDNode : public SDNode {
1105 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1106 // is freed when the SelectionDAG object is destroyed.
1109 friend class SelectionDAG;
1110 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1112 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1113 InitOperands(Ops, N1, N2);
1117 ArrayRef<int> getMask() const {
1118 EVT VT = getValueType(0);
1119 return makeArrayRef(Mask, VT.getVectorNumElements());
1121 int getMaskElt(unsigned Idx) const {
1122 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1126 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1127 int getSplatIndex() const {
1128 assert(isSplat() && "Cannot get splat index for non-splat!");
1129 EVT VT = getValueType(0);
1130 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1136 static bool isSplatMask(const int *Mask, EVT VT);
1138 static bool classof(const ShuffleVectorSDNode *) { return true; }
1139 static bool classof(const SDNode *N) {
1140 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1144 class ConstantSDNode : public SDNode {
1145 const ConstantInt *Value;
1146 friend class SelectionDAG;
1147 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1148 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1149 DebugLoc(), getSDVTList(VT)), Value(val) {
1153 const ConstantInt *getConstantIntValue() const { return Value; }
1154 const APInt &getAPIntValue() const { return Value->getValue(); }
1155 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1156 int64_t getSExtValue() const { return Value->getSExtValue(); }
1158 bool isOne() const { return Value->isOne(); }
1159 bool isNullValue() const { return Value->isNullValue(); }
1160 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1162 static bool classof(const ConstantSDNode *) { return true; }
1163 static bool classof(const SDNode *N) {
1164 return N->getOpcode() == ISD::Constant ||
1165 N->getOpcode() == ISD::TargetConstant;
1169 class ConstantFPSDNode : public SDNode {
1170 const ConstantFP *Value;
1171 friend class SelectionDAG;
1172 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1173 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1174 DebugLoc(), getSDVTList(VT)), Value(val) {
1178 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1179 const ConstantFP *getConstantFPValue() const { return Value; }
1181 /// isZero - Return true if the value is positive or negative zero.
1182 bool isZero() const { return Value->isZero(); }
1184 /// isNaN - Return true if the value is a NaN.
1185 bool isNaN() const { return Value->isNaN(); }
1187 /// isExactlyValue - We don't rely on operator== working on double values, as
1188 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1189 /// As such, this method can be used to do an exact bit-for-bit comparison of
1190 /// two floating point values.
1192 /// We leave the version with the double argument here because it's just so
1193 /// convenient to write "2.0" and the like. Without this function we'd
1194 /// have to duplicate its logic everywhere it's called.
1195 bool isExactlyValue(double V) const {
1197 // convert is not supported on this type
1198 if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1201 Tmp.convert(Value->getValueAPF().getSemantics(),
1202 APFloat::rmNearestTiesToEven, &ignored);
1203 return isExactlyValue(Tmp);
1205 bool isExactlyValue(const APFloat& V) const;
1207 static bool isValueValidForType(EVT VT, const APFloat& Val);
1209 static bool classof(const ConstantFPSDNode *) { return true; }
1210 static bool classof(const SDNode *N) {
1211 return N->getOpcode() == ISD::ConstantFP ||
1212 N->getOpcode() == ISD::TargetConstantFP;
1216 class GlobalAddressSDNode : public SDNode {
1217 const GlobalValue *TheGlobal;
1219 unsigned char TargetFlags;
1220 friend class SelectionDAG;
1221 GlobalAddressSDNode(unsigned Opc, DebugLoc DL, const GlobalValue *GA, EVT VT,
1222 int64_t o, unsigned char TargetFlags);
1225 const GlobalValue *getGlobal() const { return TheGlobal; }
1226 int64_t getOffset() const { return Offset; }
1227 unsigned char getTargetFlags() const { return TargetFlags; }
1228 // Return the address space this GlobalAddress belongs to.
1229 unsigned getAddressSpace() const;
1231 static bool classof(const GlobalAddressSDNode *) { return true; }
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 FrameIndexSDNode *) { return true; }
1252 static bool classof(const SDNode *N) {
1253 return N->getOpcode() == ISD::FrameIndex ||
1254 N->getOpcode() == ISD::TargetFrameIndex;
1258 class JumpTableSDNode : public SDNode {
1260 unsigned char TargetFlags;
1261 friend class SelectionDAG;
1262 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1263 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1264 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1268 int getIndex() const { return JTI; }
1269 unsigned char getTargetFlags() const { return TargetFlags; }
1271 static bool classof(const JumpTableSDNode *) { return true; }
1272 static bool classof(const SDNode *N) {
1273 return N->getOpcode() == ISD::JumpTable ||
1274 N->getOpcode() == ISD::TargetJumpTable;
1278 class ConstantPoolSDNode : public SDNode {
1280 const Constant *ConstVal;
1281 MachineConstantPoolValue *MachineCPVal;
1283 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1284 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1285 unsigned char TargetFlags;
1286 friend class SelectionDAG;
1287 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1288 unsigned Align, unsigned char TF)
1289 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1291 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1292 assert((int)Offset >= 0 && "Offset is too large");
1295 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1296 EVT VT, int o, unsigned Align, unsigned char TF)
1297 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1299 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1300 assert((int)Offset >= 0 && "Offset is too large");
1301 Val.MachineCPVal = v;
1302 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1307 bool isMachineConstantPoolEntry() const {
1308 return (int)Offset < 0;
1311 const Constant *getConstVal() const {
1312 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1313 return Val.ConstVal;
1316 MachineConstantPoolValue *getMachineCPVal() const {
1317 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1318 return Val.MachineCPVal;
1321 int getOffset() const {
1322 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1325 // Return the alignment of this constant pool object, which is either 0 (for
1326 // default alignment) or the desired value.
1327 unsigned getAlignment() const { return Alignment; }
1328 unsigned char getTargetFlags() const { return TargetFlags; }
1330 Type *getType() const;
1332 static bool classof(const ConstantPoolSDNode *) { return true; }
1333 static bool classof(const SDNode *N) {
1334 return N->getOpcode() == ISD::ConstantPool ||
1335 N->getOpcode() == ISD::TargetConstantPool;
1339 class BasicBlockSDNode : public SDNode {
1340 MachineBasicBlock *MBB;
1341 friend class SelectionDAG;
1342 /// Debug info is meaningful and potentially useful here, but we create
1343 /// blocks out of order when they're jumped to, which makes it a bit
1344 /// harder. Let's see if we need it first.
1345 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1346 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1350 MachineBasicBlock *getBasicBlock() const { return MBB; }
1352 static bool classof(const BasicBlockSDNode *) { return true; }
1353 static bool classof(const SDNode *N) {
1354 return N->getOpcode() == ISD::BasicBlock;
1358 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1360 class BuildVectorSDNode : public SDNode {
1361 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1362 explicit BuildVectorSDNode(); // Do not implement
1364 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1365 /// smallest element size that splats the vector. If MinSplatBits is
1366 /// nonzero, the element size must be at least that large. Note that the
1367 /// splat element may be the entire vector (i.e., a one element vector).
1368 /// Returns the splat element value in SplatValue. Any undefined bits in
1369 /// that value are zero, and the corresponding bits in the SplatUndef mask
1370 /// are set. The SplatBitSize value is set to the splat element size in
1371 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1372 /// undefined. isBigEndian describes the endianness of the target.
1373 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1374 unsigned &SplatBitSize, bool &HasAnyUndefs,
1375 unsigned MinSplatBits = 0, bool isBigEndian = false);
1377 static inline bool classof(const BuildVectorSDNode *) { return true; }
1378 static inline bool classof(const SDNode *N) {
1379 return N->getOpcode() == ISD::BUILD_VECTOR;
1383 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1384 /// used when the SelectionDAG needs to make a simple reference to something
1385 /// in the LLVM IR representation.
1387 class SrcValueSDNode : public SDNode {
1389 friend class SelectionDAG;
1390 /// Create a SrcValue for a general value.
1391 explicit SrcValueSDNode(const Value *v)
1392 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1395 /// getValue - return the contained Value.
1396 const Value *getValue() const { return V; }
1398 static bool classof(const SrcValueSDNode *) { return true; }
1399 static bool classof(const SDNode *N) {
1400 return N->getOpcode() == ISD::SRCVALUE;
1404 class MDNodeSDNode : public SDNode {
1406 friend class SelectionDAG;
1407 explicit MDNodeSDNode(const MDNode *md)
1408 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1411 const MDNode *getMD() const { return MD; }
1413 static bool classof(const MDNodeSDNode *) { return true; }
1414 static bool classof(const SDNode *N) {
1415 return N->getOpcode() == ISD::MDNODE_SDNODE;
1420 class RegisterSDNode : public SDNode {
1422 friend class SelectionDAG;
1423 RegisterSDNode(unsigned reg, EVT VT)
1424 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1428 unsigned getReg() const { return Reg; }
1430 static bool classof(const RegisterSDNode *) { return true; }
1431 static bool classof(const SDNode *N) {
1432 return N->getOpcode() == ISD::Register;
1436 class RegisterMaskSDNode : public SDNode {
1437 // The memory for RegMask is not owned by the node.
1438 const uint32_t *RegMask;
1439 friend class SelectionDAG;
1440 RegisterMaskSDNode(const uint32_t *mask)
1441 : SDNode(ISD::RegisterMask, DebugLoc(), getSDVTList(MVT::Untyped)),
1445 const uint32_t *getRegMask() const { return RegMask; }
1447 static bool classof(const RegisterMaskSDNode *) { return true; }
1448 static bool classof(const SDNode *N) {
1449 return N->getOpcode() == ISD::RegisterMask;
1453 class BlockAddressSDNode : public SDNode {
1454 const BlockAddress *BA;
1455 unsigned char TargetFlags;
1456 friend class SelectionDAG;
1457 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1458 unsigned char Flags)
1459 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1460 BA(ba), TargetFlags(Flags) {
1463 const BlockAddress *getBlockAddress() const { return BA; }
1464 unsigned char getTargetFlags() const { return TargetFlags; }
1466 static bool classof(const BlockAddressSDNode *) { return true; }
1467 static bool classof(const SDNode *N) {
1468 return N->getOpcode() == ISD::BlockAddress ||
1469 N->getOpcode() == ISD::TargetBlockAddress;
1473 class EHLabelSDNode : public SDNode {
1476 friend class SelectionDAG;
1477 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1478 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1479 InitOperands(&Chain, ch);
1482 MCSymbol *getLabel() const { return Label; }
1484 static bool classof(const EHLabelSDNode *) { return true; }
1485 static bool classof(const SDNode *N) {
1486 return N->getOpcode() == ISD::EH_LABEL;
1490 class ExternalSymbolSDNode : public SDNode {
1492 unsigned char TargetFlags;
1494 friend class SelectionDAG;
1495 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1496 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1497 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1501 const char *getSymbol() const { return Symbol; }
1502 unsigned char getTargetFlags() const { return TargetFlags; }
1504 static bool classof(const ExternalSymbolSDNode *) { return true; }
1505 static bool classof(const SDNode *N) {
1506 return N->getOpcode() == ISD::ExternalSymbol ||
1507 N->getOpcode() == ISD::TargetExternalSymbol;
1511 class CondCodeSDNode : public SDNode {
1512 ISD::CondCode Condition;
1513 friend class SelectionDAG;
1514 explicit CondCodeSDNode(ISD::CondCode Cond)
1515 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1520 ISD::CondCode get() const { return Condition; }
1522 static bool classof(const CondCodeSDNode *) { return true; }
1523 static bool classof(const SDNode *N) {
1524 return N->getOpcode() == ISD::CONDCODE;
1528 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1529 /// future and most targets don't support it.
1530 class CvtRndSatSDNode : public SDNode {
1531 ISD::CvtCode CvtCode;
1532 friend class SelectionDAG;
1533 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1534 unsigned NumOps, ISD::CvtCode Code)
1535 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1537 assert(NumOps == 5 && "wrong number of operations");
1540 ISD::CvtCode getCvtCode() const { return CvtCode; }
1542 static bool classof(const CvtRndSatSDNode *) { return true; }
1543 static bool classof(const SDNode *N) {
1544 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1548 /// VTSDNode - This class is used to represent EVT's, which are used
1549 /// to parameterize some operations.
1550 class VTSDNode : public SDNode {
1552 friend class SelectionDAG;
1553 explicit VTSDNode(EVT VT)
1554 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1559 EVT getVT() const { return ValueType; }
1561 static bool classof(const VTSDNode *) { return true; }
1562 static bool classof(const SDNode *N) {
1563 return N->getOpcode() == ISD::VALUETYPE;
1567 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1569 class LSBaseSDNode : public MemSDNode {
1570 //! Operand array for load and store
1572 \note Moving this array to the base class captures more
1573 common functionality shared between LoadSDNode and
1578 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1579 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1580 EVT MemVT, MachineMemOperand *MMO)
1581 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1582 SubclassData |= AM << 2;
1583 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1584 InitOperands(Ops, Operands, numOperands);
1585 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1586 "Only indexed loads and stores have a non-undef offset operand");
1589 const SDValue &getOffset() const {
1590 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1593 /// getAddressingMode - Return the addressing mode for this load or store:
1594 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1595 ISD::MemIndexedMode getAddressingMode() const {
1596 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1599 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1600 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1602 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1603 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1605 static bool classof(const LSBaseSDNode *) { return true; }
1606 static bool classof(const SDNode *N) {
1607 return N->getOpcode() == ISD::LOAD ||
1608 N->getOpcode() == ISD::STORE;
1612 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1614 class LoadSDNode : public LSBaseSDNode {
1615 friend class SelectionDAG;
1616 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1617 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1618 MachineMemOperand *MMO)
1619 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1620 VTs, AM, MemVT, MMO) {
1621 SubclassData |= (unsigned short)ETy;
1622 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1623 assert(readMem() && "Load MachineMemOperand is not a load!");
1624 assert(!writeMem() && "Load MachineMemOperand is a store!");
1628 /// getExtensionType - Return whether this is a plain node,
1629 /// or one of the varieties of value-extending loads.
1630 ISD::LoadExtType getExtensionType() const {
1631 return ISD::LoadExtType(SubclassData & 3);
1634 const SDValue &getBasePtr() const { return getOperand(1); }
1635 const SDValue &getOffset() const { return getOperand(2); }
1637 static bool classof(const LoadSDNode *) { return true; }
1638 static bool classof(const SDNode *N) {
1639 return N->getOpcode() == ISD::LOAD;
1643 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1645 class StoreSDNode : public LSBaseSDNode {
1646 friend class SelectionDAG;
1647 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1648 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1649 MachineMemOperand *MMO)
1650 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1651 VTs, AM, MemVT, MMO) {
1652 SubclassData |= (unsigned short)isTrunc;
1653 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1654 assert(!readMem() && "Store MachineMemOperand is a load!");
1655 assert(writeMem() && "Store MachineMemOperand is not a store!");
1659 /// isTruncatingStore - Return true if the op does a truncation before store.
1660 /// For integers this is the same as doing a TRUNCATE and storing the result.
1661 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1662 bool isTruncatingStore() const { return SubclassData & 1; }
1664 const SDValue &getValue() const { return getOperand(1); }
1665 const SDValue &getBasePtr() const { return getOperand(2); }
1666 const SDValue &getOffset() const { return getOperand(3); }
1668 static bool classof(const StoreSDNode *) { return true; }
1669 static bool classof(const SDNode *N) {
1670 return N->getOpcode() == ISD::STORE;
1674 /// MachineSDNode - An SDNode that represents everything that will be needed
1675 /// to construct a MachineInstr. These nodes are created during the
1676 /// instruction selection proper phase.
1678 class MachineSDNode : public SDNode {
1680 typedef MachineMemOperand **mmo_iterator;
1683 friend class SelectionDAG;
1684 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1685 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1687 /// LocalOperands - Operands for this instruction, if they fit here. If
1688 /// they don't, this field is unused.
1689 SDUse LocalOperands[4];
1691 /// MemRefs - Memory reference descriptions for this instruction.
1692 mmo_iterator MemRefs;
1693 mmo_iterator MemRefsEnd;
1696 mmo_iterator memoperands_begin() const { return MemRefs; }
1697 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1698 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1700 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1701 /// list. This does not transfer ownership.
1702 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1703 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1704 assert(*MMI && "Null mem ref detected!");
1705 MemRefs = NewMemRefs;
1706 MemRefsEnd = NewMemRefsEnd;
1709 static bool classof(const MachineSDNode *) { return true; }
1710 static bool classof(const SDNode *N) {
1711 return N->isMachineOpcode();
1715 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1716 SDNode, ptrdiff_t> {
1720 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1722 bool operator==(const SDNodeIterator& x) const {
1723 return Operand == x.Operand;
1725 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1727 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1728 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1729 Operand = I.Operand;
1733 pointer operator*() const {
1734 return Node->getOperand(Operand).getNode();
1736 pointer operator->() const { return operator*(); }
1738 SDNodeIterator& operator++() { // Preincrement
1742 SDNodeIterator operator++(int) { // Postincrement
1743 SDNodeIterator tmp = *this; ++*this; return tmp;
1745 size_t operator-(SDNodeIterator Other) const {
1746 assert(Node == Other.Node &&
1747 "Cannot compare iterators of two different nodes!");
1748 return Operand - Other.Operand;
1751 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1752 static SDNodeIterator end (SDNode *N) {
1753 return SDNodeIterator(N, N->getNumOperands());
1756 unsigned getOperand() const { return Operand; }
1757 const SDNode *getNode() const { return Node; }
1760 template <> struct GraphTraits<SDNode*> {
1761 typedef SDNode NodeType;
1762 typedef SDNodeIterator ChildIteratorType;
1763 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1764 static inline ChildIteratorType child_begin(NodeType *N) {
1765 return SDNodeIterator::begin(N);
1767 static inline ChildIteratorType child_end(NodeType *N) {
1768 return SDNodeIterator::end(N);
1772 /// LargestSDNode - The largest SDNode class.
1774 typedef LoadSDNode LargestSDNode;
1776 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1779 typedef GlobalAddressSDNode MostAlignedSDNode;
1782 /// isNormalLoad - Returns true if the specified node is a non-extending
1783 /// and unindexed load.
1784 inline bool isNormalLoad(const SDNode *N) {
1785 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1786 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1787 Ld->getAddressingMode() == ISD::UNINDEXED;
1790 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1792 inline bool isNON_EXTLoad(const SDNode *N) {
1793 return isa<LoadSDNode>(N) &&
1794 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1797 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1799 inline bool isEXTLoad(const SDNode *N) {
1800 return isa<LoadSDNode>(N) &&
1801 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1804 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1806 inline bool isSEXTLoad(const SDNode *N) {
1807 return isa<LoadSDNode>(N) &&
1808 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1811 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1813 inline bool isZEXTLoad(const SDNode *N) {
1814 return isa<LoadSDNode>(N) &&
1815 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1818 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1820 inline bool isUNINDEXEDLoad(const SDNode *N) {
1821 return isa<LoadSDNode>(N) &&
1822 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1825 /// isNormalStore - Returns true if the specified node is a non-truncating
1826 /// and unindexed store.
1827 inline bool isNormalStore(const SDNode *N) {
1828 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1829 return St && !St->isTruncatingStore() &&
1830 St->getAddressingMode() == ISD::UNINDEXED;
1833 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1835 inline bool isNON_TRUNCStore(const SDNode *N) {
1836 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1839 /// isTRUNCStore - Returns true if the specified node is a truncating
1841 inline bool isTRUNCStore(const SDNode *N) {
1842 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1845 /// isUNINDEXEDStore - Returns true if the specified node is an
1846 /// unindexed store.
1847 inline bool isUNINDEXEDStore(const SDNode *N) {
1848 return isa<StoreSDNode>(N) &&
1849 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1853 } // end llvm namespace