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);
78 /// allOperandsUndef - Return true if the node has at least one operand
79 /// and all operands of the specified node are ISD::UNDEF.
80 bool allOperandsUndef(const SDNode *N);
81 } // end llvm:ISD namespace
83 //===----------------------------------------------------------------------===//
84 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
85 /// values as the result of a computation. Many nodes return multiple values,
86 /// from loads (which define a token and a return value) to ADDC (which returns
87 /// a result and a carry value), to calls (which may return an arbitrary number
90 /// As such, each use of a SelectionDAG computation must indicate the node that
91 /// computes it as well as which return value to use from that node. This pair
92 /// of information is represented with the SDValue value type.
95 SDNode *Node; // The node defining the value we are using.
96 unsigned ResNo; // Which return value of the node we are using.
98 SDValue() : Node(0), ResNo(0) {}
99 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
101 /// get the index which selects a specific result in the SDNode
102 unsigned getResNo() const { return ResNo; }
104 /// get the SDNode which holds the desired result
105 SDNode *getNode() const { return Node; }
108 void setNode(SDNode *N) { Node = N; }
110 inline SDNode *operator->() const { return Node; }
112 bool operator==(const SDValue &O) const {
113 return Node == O.Node && ResNo == O.ResNo;
115 bool operator!=(const SDValue &O) const {
116 return !operator==(O);
118 bool operator<(const SDValue &O) const {
119 return Node < O.Node || (Node == O.Node && ResNo < O.ResNo);
122 SDValue getValue(unsigned R) const {
123 return SDValue(Node, R);
126 // isOperandOf - Return true if this node is an operand of N.
127 bool isOperandOf(SDNode *N) const;
129 /// getValueType - Return the ValueType of the referenced return value.
131 inline EVT getValueType() const;
133 /// getValueSizeInBits - Returns the size of the value in bits.
135 unsigned getValueSizeInBits() const {
136 return getValueType().getSizeInBits();
139 // Forwarding methods - These forward to the corresponding methods in SDNode.
140 inline unsigned getOpcode() const;
141 inline unsigned getNumOperands() const;
142 inline const SDValue &getOperand(unsigned i) const;
143 inline uint64_t getConstantOperandVal(unsigned i) const;
144 inline bool isTargetMemoryOpcode() const;
145 inline bool isTargetOpcode() const;
146 inline bool isMachineOpcode() const;
147 inline unsigned getMachineOpcode() const;
148 inline const DebugLoc getDebugLoc() const;
151 /// reachesChainWithoutSideEffects - Return true if this operand (which must
152 /// be a chain) reaches the specified operand without crossing any
153 /// side-effecting instructions. In practice, this looks through token
154 /// factors and non-volatile loads. In order to remain efficient, this only
155 /// looks a couple of nodes in, it does not do an exhaustive search.
156 bool reachesChainWithoutSideEffects(SDValue Dest,
157 unsigned Depth = 2) const;
159 /// use_empty - Return true if there are no nodes using value ResNo
162 inline bool use_empty() const;
164 /// hasOneUse - Return true if there is exactly one node using value
167 inline bool hasOneUse() const;
171 template<> struct DenseMapInfo<SDValue> {
172 static inline SDValue getEmptyKey() {
173 return SDValue((SDNode*)-1, -1U);
175 static inline SDValue getTombstoneKey() {
176 return SDValue((SDNode*)-1, 0);
178 static unsigned getHashValue(const SDValue &Val) {
179 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
180 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
182 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
186 template <> struct isPodLike<SDValue> { static const bool value = true; };
189 /// simplify_type specializations - Allow casting operators to work directly on
190 /// SDValues as if they were SDNode*'s.
191 template<> struct simplify_type<SDValue> {
192 typedef SDNode* SimpleType;
193 static SimpleType getSimplifiedValue(const SDValue &Val) {
194 return static_cast<SimpleType>(Val.getNode());
197 template<> struct simplify_type<const SDValue> {
198 typedef SDNode* SimpleType;
199 static SimpleType getSimplifiedValue(const SDValue &Val) {
200 return static_cast<SimpleType>(Val.getNode());
204 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
205 /// which records the SDNode being used and the result number, a
206 /// pointer to the SDNode using the value, and Next and Prev pointers,
207 /// which link together all the uses of an SDNode.
210 /// Val - The value being used.
212 /// User - The user of this value.
214 /// Prev, Next - Pointers to the uses list of the SDNode referred by
218 SDUse(const SDUse &U); // Do not implement
219 void operator=(const SDUse &U); // Do not implement
222 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
224 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
225 operator const SDValue&() const { return Val; }
227 /// If implicit conversion to SDValue doesn't work, the get() method returns
229 const SDValue &get() const { return Val; }
231 /// getUser - This returns the SDNode that contains this Use.
232 SDNode *getUser() { return User; }
234 /// getNext - Get the next SDUse in the use list.
235 SDUse *getNext() const { return Next; }
237 /// getNode - Convenience function for get().getNode().
238 SDNode *getNode() const { return Val.getNode(); }
239 /// getResNo - Convenience function for get().getResNo().
240 unsigned getResNo() const { return Val.getResNo(); }
241 /// getValueType - Convenience function for get().getValueType().
242 EVT getValueType() const { return Val.getValueType(); }
244 /// operator== - Convenience function for get().operator==
245 bool operator==(const SDValue &V) const {
249 /// operator!= - Convenience function for get().operator!=
250 bool operator!=(const SDValue &V) const {
254 /// operator< - Convenience function for get().operator<
255 bool operator<(const SDValue &V) const {
260 friend class SelectionDAG;
263 void setUser(SDNode *p) { User = p; }
265 /// set - Remove this use from its existing use list, assign it the
266 /// given value, and add it to the new value's node's use list.
267 inline void set(const SDValue &V);
268 /// setInitial - like set, but only supports initializing a newly-allocated
269 /// SDUse with a non-null value.
270 inline void setInitial(const SDValue &V);
271 /// setNode - like set, but only sets the Node portion of the value,
272 /// leaving the ResNo portion unmodified.
273 inline void setNode(SDNode *N);
275 void addToList(SDUse **List) {
277 if (Next) Next->Prev = &Next;
282 void removeFromList() {
284 if (Next) Next->Prev = Prev;
288 /// simplify_type specializations - Allow casting operators to work directly on
289 /// SDValues as if they were SDNode*'s.
290 template<> struct simplify_type<SDUse> {
291 typedef SDNode* SimpleType;
292 static SimpleType getSimplifiedValue(const SDUse &Val) {
293 return static_cast<SimpleType>(Val.getNode());
296 template<> struct simplify_type<const SDUse> {
297 typedef SDNode* SimpleType;
298 static SimpleType getSimplifiedValue(const SDUse &Val) {
299 return static_cast<SimpleType>(Val.getNode());
304 /// SDNode - Represents one node in the SelectionDAG.
306 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
308 /// NodeType - The operation that this node performs.
312 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
313 /// then they will be delete[]'d when the node is destroyed.
314 uint16_t OperandsNeedDelete : 1;
316 /// HasDebugValue - This tracks whether this node has one or more dbg_value
317 /// nodes corresponding to it.
318 uint16_t HasDebugValue : 1;
321 /// SubclassData - This member is defined by this class, but is not used for
322 /// anything. Subclasses can use it to hold whatever state they find useful.
323 /// This field is initialized to zero by the ctor.
324 uint16_t SubclassData : 14;
327 /// NodeId - Unique id per SDNode in the DAG.
330 /// OperandList - The values that are used by this operation.
334 /// ValueList - The types of the values this node defines. SDNode's may
335 /// define multiple values simultaneously.
336 const EVT *ValueList;
338 /// UseList - List of uses for this SDNode.
341 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
342 unsigned short NumOperands, NumValues;
344 /// debugLoc - source line information.
347 /// getValueTypeList - Return a pointer to the specified value type.
348 static const EVT *getValueTypeList(EVT VT);
350 friend class SelectionDAG;
351 friend struct ilist_traits<SDNode>;
354 //===--------------------------------------------------------------------===//
358 /// getOpcode - Return the SelectionDAG opcode value for this node. For
359 /// pre-isel nodes (those for which isMachineOpcode returns false), these
360 /// are the opcode values in the ISD and <target>ISD namespaces. For
361 /// post-isel opcodes, see getMachineOpcode.
362 unsigned getOpcode() const { return (unsigned short)NodeType; }
364 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
365 /// \<target\>ISD namespace).
366 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
368 /// isTargetMemoryOpcode - Test if this node has a target-specific
369 /// memory-referencing opcode (in the \<target\>ISD namespace and
370 /// greater than FIRST_TARGET_MEMORY_OPCODE).
371 bool isTargetMemoryOpcode() const {
372 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
375 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
376 /// corresponding to a MachineInstr opcode.
377 bool isMachineOpcode() const { return NodeType < 0; }
379 /// getMachineOpcode - This may only be called if isMachineOpcode returns
380 /// true. It returns the MachineInstr opcode value that the node's opcode
382 unsigned getMachineOpcode() const {
383 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
387 /// getHasDebugValue - get this bit.
388 bool getHasDebugValue() const { return HasDebugValue; }
390 /// setHasDebugValue - set this bit.
391 void setHasDebugValue(bool b) { HasDebugValue = b; }
393 /// use_empty - Return true if there are no uses of this node.
395 bool use_empty() const { return UseList == NULL; }
397 /// hasOneUse - Return true if there is exactly one use of this node.
399 bool hasOneUse() const {
400 return !use_empty() && llvm::next(use_begin()) == use_end();
403 /// use_size - Return the number of uses of this node. This method takes
404 /// time proportional to the number of uses.
406 size_t use_size() const { return std::distance(use_begin(), use_end()); }
408 /// getNodeId - Return the unique node id.
410 int getNodeId() const { return NodeId; }
412 /// setNodeId - Set unique node id.
413 void setNodeId(int Id) { NodeId = Id; }
415 /// getDebugLoc - Return the source location info.
416 const DebugLoc getDebugLoc() const { return debugLoc; }
418 /// setDebugLoc - Set source location info. Try to avoid this, putting
419 /// it in the constructor is preferable.
420 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
422 /// use_iterator - This class provides iterator support for SDUse
423 /// operands that use a specific SDNode.
425 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
427 explicit use_iterator(SDUse *op) : Op(op) {
431 typedef std::iterator<std::forward_iterator_tag,
432 SDUse, ptrdiff_t>::reference reference;
433 typedef std::iterator<std::forward_iterator_tag,
434 SDUse, ptrdiff_t>::pointer pointer;
436 use_iterator(const use_iterator &I) : Op(I.Op) {}
437 use_iterator() : Op(0) {}
439 bool operator==(const use_iterator &x) const {
442 bool operator!=(const use_iterator &x) const {
443 return !operator==(x);
446 /// atEnd - return true if this iterator is at the end of uses list.
447 bool atEnd() const { return Op == 0; }
449 // Iterator traversal: forward iteration only.
450 use_iterator &operator++() { // Preincrement
451 assert(Op && "Cannot increment end iterator!");
456 use_iterator operator++(int) { // Postincrement
457 use_iterator tmp = *this; ++*this; return tmp;
460 /// Retrieve a pointer to the current user node.
461 SDNode *operator*() const {
462 assert(Op && "Cannot dereference end iterator!");
463 return Op->getUser();
466 SDNode *operator->() const { return operator*(); }
468 SDUse &getUse() const { return *Op; }
470 /// getOperandNo - Retrieve the operand # of this use in its user.
472 unsigned getOperandNo() const {
473 assert(Op && "Cannot dereference end iterator!");
474 return (unsigned)(Op - Op->getUser()->OperandList);
478 /// use_begin/use_end - Provide iteration support to walk over all uses
481 use_iterator use_begin() const {
482 return use_iterator(UseList);
485 static use_iterator use_end() { return use_iterator(0); }
488 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
489 /// indicated value. This method ignores uses of other values defined by this
491 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
493 /// hasAnyUseOfValue - Return true if there are any use of the indicated
494 /// value. This method ignores uses of other values defined by this operation.
495 bool hasAnyUseOfValue(unsigned Value) const;
497 /// isOnlyUserOf - Return true if this node is the only use of N.
499 bool isOnlyUserOf(SDNode *N) const;
501 /// isOperandOf - Return true if this node is an operand of N.
503 bool isOperandOf(SDNode *N) const;
505 /// isPredecessorOf - Return true if this node is a predecessor of N.
506 /// NOTE: Implemented on top of hasPredecessor and every bit as
507 /// expensive. Use carefully.
508 bool isPredecessorOf(const SDNode *N) const { return N->hasPredecessor(this); }
510 /// hasPredecessor - Return true if N is a predecessor of this node.
511 /// N is either an operand of this node, or can be reached by recursively
512 /// traversing up the operands.
513 /// NOTE: This is an expensive method. Use it carefully.
514 bool hasPredecessor(const SDNode *N) const;
516 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
517 /// N is either an operand of this node, or can be reached by recursively
518 /// traversing up the operands.
519 /// In this helper the Visited and worklist sets are held externally to
520 /// cache predecessors over multiple invocations. If you want to test for
521 /// multiple predecessors this method is preferable to multiple calls to
522 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
524 /// NOTE: This is still very expensive. Use carefully.
525 bool hasPredecessorHelper(const SDNode *N,
526 SmallPtrSet<const SDNode *, 32> &Visited,
527 SmallVector<const SDNode *, 16> &Worklist) const;
529 /// getNumOperands - Return the number of values used by this operation.
531 unsigned getNumOperands() const { return NumOperands; }
533 /// getConstantOperandVal - Helper method returns the integer value of a
534 /// ConstantSDNode operand.
535 uint64_t getConstantOperandVal(unsigned Num) const;
537 const SDValue &getOperand(unsigned Num) const {
538 assert(Num < NumOperands && "Invalid child # of SDNode!");
539 return OperandList[Num];
542 typedef SDUse* op_iterator;
543 op_iterator op_begin() const { return OperandList; }
544 op_iterator op_end() const { return OperandList+NumOperands; }
546 SDVTList getVTList() const {
547 SDVTList X = { ValueList, NumValues };
551 /// getGluedNode - If this node has a glue operand, return the node
552 /// to which the glue operand points. Otherwise return NULL.
553 SDNode *getGluedNode() const {
554 if (getNumOperands() != 0 &&
555 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
556 return getOperand(getNumOperands()-1).getNode();
560 // If this is a pseudo op, like copyfromreg, look to see if there is a
561 // real target node glued to it. If so, return the target node.
562 const SDNode *getGluedMachineNode() const {
563 const SDNode *FoundNode = this;
565 // Climb up glue edges until a machine-opcode node is found, or the
566 // end of the chain is reached.
567 while (!FoundNode->isMachineOpcode()) {
568 const SDNode *N = FoundNode->getGluedNode();
576 /// getGluedUser - If this node has a glue value with a user, return
577 /// the user (there is at most one). Otherwise return NULL.
578 SDNode *getGluedUser() const {
579 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
580 if (UI.getUse().get().getValueType() == MVT::Glue)
585 /// getNumValues - Return the number of values defined/returned by this
588 unsigned getNumValues() const { return NumValues; }
590 /// getValueType - Return the type of a specified result.
592 EVT getValueType(unsigned ResNo) const {
593 assert(ResNo < NumValues && "Illegal result number!");
594 return ValueList[ResNo];
597 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
599 unsigned getValueSizeInBits(unsigned ResNo) const {
600 return getValueType(ResNo).getSizeInBits();
603 typedef const EVT* value_iterator;
604 value_iterator value_begin() const { return ValueList; }
605 value_iterator value_end() const { return ValueList+NumValues; }
607 /// getOperationName - Return the opcode of this operation for printing.
609 std::string getOperationName(const SelectionDAG *G = 0) const;
610 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
611 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
612 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
613 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
614 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
616 /// printrFull - Print a SelectionDAG node and all children down to
617 /// the leaves. The given SelectionDAG allows target-specific nodes
618 /// to be printed in human-readable form. Unlike printr, this will
619 /// print the whole DAG, including children that appear multiple
622 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
624 /// printrWithDepth - Print a SelectionDAG node and children up to
625 /// depth "depth." The given SelectionDAG allows target-specific
626 /// nodes to be printed in human-readable form. Unlike printr, this
627 /// will print children that appear multiple times wherever they are
630 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
631 unsigned depth = 100) const;
634 /// dump - Dump this node, for debugging.
637 /// dumpr - Dump (recursively) this node and its use-def subgraph.
640 /// dump - Dump this node, for debugging.
641 /// The given SelectionDAG allows target-specific nodes to be printed
642 /// in human-readable form.
643 void dump(const SelectionDAG *G) const;
645 /// dumpr - Dump (recursively) this node and its use-def subgraph.
646 /// The given SelectionDAG allows target-specific nodes to be printed
647 /// in human-readable form.
648 void dumpr(const SelectionDAG *G) const;
650 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
651 /// target-specific nodes to be printed in human-readable form.
652 /// Unlike dumpr, this will print the whole DAG, including children
653 /// that appear multiple times.
655 void dumprFull(const SelectionDAG *G = 0) const;
657 /// dumprWithDepth - printrWithDepth to dbgs(). The given
658 /// SelectionDAG allows target-specific nodes to be printed in
659 /// human-readable form. Unlike dumpr, this will print children
660 /// that appear multiple times wherever they are used.
662 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
665 static bool classof(const SDNode *) { return true; }
667 /// Profile - Gather unique data for the node.
669 void Profile(FoldingSetNodeID &ID) const;
671 /// addUse - This method should only be used by the SDUse class.
673 void addUse(SDUse &U) { U.addToList(&UseList); }
676 static SDVTList getSDVTList(EVT VT) {
677 SDVTList Ret = { getValueTypeList(VT), 1 };
681 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs, const SDValue *Ops,
683 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
684 SubclassData(0), NodeId(-1),
685 OperandList(NumOps ? new SDUse[NumOps] : 0),
686 ValueList(VTs.VTs), UseList(NULL),
687 NumOperands(NumOps), NumValues(VTs.NumVTs),
689 for (unsigned i = 0; i != NumOps; ++i) {
690 OperandList[i].setUser(this);
691 OperandList[i].setInitial(Ops[i]);
693 checkForCycles(this);
696 /// This constructor adds no operands itself; operands can be
697 /// set later with InitOperands.
698 SDNode(unsigned Opc, const DebugLoc dl, SDVTList VTs)
699 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
700 SubclassData(0), NodeId(-1), OperandList(0), ValueList(VTs.VTs),
701 UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
704 /// InitOperands - Initialize the operands list of this with 1 operand.
705 void InitOperands(SDUse *Ops, const SDValue &Op0) {
706 Ops[0].setUser(this);
707 Ops[0].setInitial(Op0);
710 checkForCycles(this);
713 /// InitOperands - Initialize the operands list of this with 2 operands.
714 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
715 Ops[0].setUser(this);
716 Ops[0].setInitial(Op0);
717 Ops[1].setUser(this);
718 Ops[1].setInitial(Op1);
721 checkForCycles(this);
724 /// InitOperands - Initialize the operands list of this with 3 operands.
725 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
726 const SDValue &Op2) {
727 Ops[0].setUser(this);
728 Ops[0].setInitial(Op0);
729 Ops[1].setUser(this);
730 Ops[1].setInitial(Op1);
731 Ops[2].setUser(this);
732 Ops[2].setInitial(Op2);
735 checkForCycles(this);
738 /// InitOperands - Initialize the operands list of this with 4 operands.
739 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
740 const SDValue &Op2, const SDValue &Op3) {
741 Ops[0].setUser(this);
742 Ops[0].setInitial(Op0);
743 Ops[1].setUser(this);
744 Ops[1].setInitial(Op1);
745 Ops[2].setUser(this);
746 Ops[2].setInitial(Op2);
747 Ops[3].setUser(this);
748 Ops[3].setInitial(Op3);
751 checkForCycles(this);
754 /// InitOperands - Initialize the operands list of this with N operands.
755 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
756 for (unsigned i = 0; i != N; ++i) {
757 Ops[i].setUser(this);
758 Ops[i].setInitial(Vals[i]);
762 checkForCycles(this);
765 /// DropOperands - Release the operands and set this node to have
771 // Define inline functions from the SDValue class.
773 inline unsigned SDValue::getOpcode() const {
774 return Node->getOpcode();
776 inline EVT SDValue::getValueType() const {
777 return Node->getValueType(ResNo);
779 inline unsigned SDValue::getNumOperands() const {
780 return Node->getNumOperands();
782 inline const SDValue &SDValue::getOperand(unsigned i) const {
783 return Node->getOperand(i);
785 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
786 return Node->getConstantOperandVal(i);
788 inline bool SDValue::isTargetOpcode() const {
789 return Node->isTargetOpcode();
791 inline bool SDValue::isTargetMemoryOpcode() const {
792 return Node->isTargetMemoryOpcode();
794 inline bool SDValue::isMachineOpcode() const {
795 return Node->isMachineOpcode();
797 inline unsigned SDValue::getMachineOpcode() const {
798 return Node->getMachineOpcode();
800 inline bool SDValue::use_empty() const {
801 return !Node->hasAnyUseOfValue(ResNo);
803 inline bool SDValue::hasOneUse() const {
804 return Node->hasNUsesOfValue(1, ResNo);
806 inline const DebugLoc SDValue::getDebugLoc() const {
807 return Node->getDebugLoc();
810 // Define inline functions from the SDUse class.
812 inline void SDUse::set(const SDValue &V) {
813 if (Val.getNode()) removeFromList();
815 if (V.getNode()) V.getNode()->addUse(*this);
818 inline void SDUse::setInitial(const SDValue &V) {
820 V.getNode()->addUse(*this);
823 inline void SDUse::setNode(SDNode *N) {
824 if (Val.getNode()) removeFromList();
826 if (N) N->addUse(*this);
829 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
830 /// to allow co-allocation of node operands with the node itself.
831 class UnarySDNode : public SDNode {
834 UnarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X)
835 : SDNode(Opc, dl, VTs) {
836 InitOperands(&Op, X);
840 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
841 /// to allow co-allocation of node operands with the node itself.
842 class BinarySDNode : public SDNode {
845 BinarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y)
846 : SDNode(Opc, dl, VTs) {
847 InitOperands(Ops, X, Y);
851 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
852 /// to allow co-allocation of node operands with the node itself.
853 class TernarySDNode : public SDNode {
856 TernarySDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, SDValue X, SDValue Y,
858 : SDNode(Opc, dl, VTs) {
859 InitOperands(Ops, X, Y, Z);
864 /// HandleSDNode - This class is used to form a handle around another node that
865 /// is persistent and is updated across invocations of replaceAllUsesWith on its
866 /// operand. This node should be directly created by end-users and not added to
867 /// the AllNodes list.
868 class HandleSDNode : public SDNode {
871 // FIXME: Remove the "noinline" attribute once <rdar://problem/5852746> is
873 #if __GNUC__==4 && __GNUC_MINOR__==2 && defined(__APPLE__) && !defined(__llvm__)
874 explicit __attribute__((__noinline__)) HandleSDNode(SDValue X)
876 explicit HandleSDNode(SDValue X)
878 : SDNode(ISD::HANDLENODE, DebugLoc(), getSDVTList(MVT::Other)) {
879 InitOperands(&Op, X);
882 const SDValue &getValue() const { return Op; }
885 /// Abstact virtual class for operations for memory operations
886 class MemSDNode : public SDNode {
888 // MemoryVT - VT of in-memory value.
892 /// MMO - Memory reference information.
893 MachineMemOperand *MMO;
896 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT MemoryVT,
897 MachineMemOperand *MMO);
899 MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, const SDValue *Ops,
900 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
902 bool readMem() const { return MMO->isLoad(); }
903 bool writeMem() const { return MMO->isStore(); }
905 /// Returns alignment and volatility of the memory access
906 unsigned getOriginalAlignment() const {
907 return MMO->getBaseAlignment();
909 unsigned getAlignment() const {
910 return MMO->getAlignment();
913 /// getRawSubclassData - Return the SubclassData value, which contains an
914 /// encoding of the volatile flag, as well as bits used by subclasses. This
915 /// function should only be used to compute a FoldingSetNodeID value.
916 unsigned getRawSubclassData() const {
920 // We access subclass data here so that we can check consistency
921 // with MachineMemOperand information.
922 bool isVolatile() const { return (SubclassData >> 5) & 1; }
923 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
924 bool isInvariant() const { return (SubclassData >> 7) & 1; }
926 AtomicOrdering getOrdering() const {
927 return AtomicOrdering((SubclassData >> 8) & 15);
929 SynchronizationScope getSynchScope() const {
930 return SynchronizationScope((SubclassData >> 12) & 1);
933 /// Returns the SrcValue and offset that describes the location of the access
934 const Value *getSrcValue() const { return MMO->getValue(); }
935 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
937 /// Returns the TBAAInfo that describes the dereference.
938 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
940 /// Returns the Ranges that describes the dereference.
941 const MDNode *getRanges() const { return MMO->getRanges(); }
943 /// getMemoryVT - Return the type of the in-memory value.
944 EVT getMemoryVT() const { return MemoryVT; }
946 /// getMemOperand - Return a MachineMemOperand object describing the memory
947 /// reference performed by operation.
948 MachineMemOperand *getMemOperand() const { return MMO; }
950 const MachinePointerInfo &getPointerInfo() const {
951 return MMO->getPointerInfo();
954 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
955 /// to reflect the alignment of NewMMO, if it has a greater alignment.
956 /// This must only be used when the new alignment applies to all users of
957 /// this MachineMemOperand.
958 void refineAlignment(const MachineMemOperand *NewMMO) {
959 MMO->refineAlignment(NewMMO);
962 const SDValue &getChain() const { return getOperand(0); }
963 const SDValue &getBasePtr() const {
964 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
967 // Methods to support isa and dyn_cast
968 static bool classof(const MemSDNode *) { return true; }
969 static bool classof(const SDNode *N) {
970 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
971 // with either an intrinsic or a target opcode.
972 return N->getOpcode() == ISD::LOAD ||
973 N->getOpcode() == ISD::STORE ||
974 N->getOpcode() == ISD::PREFETCH ||
975 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
976 N->getOpcode() == ISD::ATOMIC_SWAP ||
977 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
978 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
979 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
980 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
981 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
982 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
983 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
984 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
985 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
986 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
987 N->getOpcode() == ISD::ATOMIC_LOAD ||
988 N->getOpcode() == ISD::ATOMIC_STORE ||
989 N->isTargetMemoryOpcode();
993 /// AtomicSDNode - A SDNode reprenting atomic operations.
995 class AtomicSDNode : public MemSDNode {
998 void InitAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope) {
999 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1000 assert((Ordering & 15) == Ordering &&
1001 "Ordering may not require more than 4 bits!");
1002 assert((SynchScope & 1) == SynchScope &&
1003 "SynchScope may not require more than 1 bit!");
1004 SubclassData |= Ordering << 8;
1005 SubclassData |= SynchScope << 12;
1006 assert(getOrdering() == Ordering && "Ordering encoding error!");
1007 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1009 assert((readMem() || getOrdering() <= Monotonic) &&
1010 "Acquire/Release MachineMemOperand must be a load!");
1011 assert((writeMem() || getOrdering() <= Monotonic) &&
1012 "Acquire/Release MachineMemOperand must be a store!");
1016 // Opc: opcode for atomic
1017 // VTL: value type list
1018 // Chain: memory chain for operaand
1019 // Ptr: address to update as a SDValue
1020 // Cmp: compare value
1022 // SrcVal: address to update as a Value (used for MemOperand)
1023 // Align: alignment of memory
1024 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1025 SDValue Chain, SDValue Ptr,
1026 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO,
1027 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1028 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1029 InitAtomic(Ordering, SynchScope);
1030 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1032 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1033 SDValue Chain, SDValue Ptr,
1034 SDValue Val, MachineMemOperand *MMO,
1035 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1036 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1037 InitAtomic(Ordering, SynchScope);
1038 InitOperands(Ops, Chain, Ptr, Val);
1040 AtomicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTL, EVT MemVT,
1041 SDValue Chain, SDValue Ptr,
1042 MachineMemOperand *MMO,
1043 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1044 : MemSDNode(Opc, dl, VTL, MemVT, MMO) {
1045 InitAtomic(Ordering, SynchScope);
1046 InitOperands(Ops, Chain, Ptr);
1049 const SDValue &getBasePtr() const { return getOperand(1); }
1050 const SDValue &getVal() const { return getOperand(2); }
1052 bool isCompareAndSwap() const {
1053 unsigned Op = getOpcode();
1054 return Op == ISD::ATOMIC_CMP_SWAP;
1057 // Methods to support isa and dyn_cast
1058 static bool classof(const AtomicSDNode *) { return true; }
1059 static bool classof(const SDNode *N) {
1060 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1061 N->getOpcode() == ISD::ATOMIC_SWAP ||
1062 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1063 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1064 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1065 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1066 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1067 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1068 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1069 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1070 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1071 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1072 N->getOpcode() == ISD::ATOMIC_LOAD ||
1073 N->getOpcode() == ISD::ATOMIC_STORE;
1077 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1078 /// memory and need an associated MachineMemOperand. Its opcode may be
1079 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1080 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1081 class MemIntrinsicSDNode : public MemSDNode {
1083 MemIntrinsicSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
1084 const SDValue *Ops, unsigned NumOps,
1085 EVT MemoryVT, MachineMemOperand *MMO)
1086 : MemSDNode(Opc, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1089 // Methods to support isa and dyn_cast
1090 static bool classof(const MemIntrinsicSDNode *) { return true; }
1091 static bool classof(const SDNode *N) {
1092 // We lower some target intrinsics to their target opcode
1093 // early a node with a target opcode can be of this class
1094 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1095 N->getOpcode() == ISD::INTRINSIC_VOID ||
1096 N->getOpcode() == ISD::PREFETCH ||
1097 N->isTargetMemoryOpcode();
1101 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1102 /// support for the llvm IR shufflevector instruction. It combines elements
1103 /// from two input vectors into a new input vector, with the selection and
1104 /// ordering of elements determined by an array of integers, referred to as
1105 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1106 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1107 /// An index of -1 is treated as undef, such that the code generator may put
1108 /// any value in the corresponding element of the result.
1109 class ShuffleVectorSDNode : public SDNode {
1112 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1113 // is freed when the SelectionDAG object is destroyed.
1116 friend class SelectionDAG;
1117 ShuffleVectorSDNode(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
1119 : SDNode(ISD::VECTOR_SHUFFLE, dl, getSDVTList(VT)), Mask(M) {
1120 InitOperands(Ops, N1, N2);
1124 ArrayRef<int> getMask() const {
1125 EVT VT = getValueType(0);
1126 return makeArrayRef(Mask, VT.getVectorNumElements());
1128 int getMaskElt(unsigned Idx) const {
1129 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1133 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1134 int getSplatIndex() const {
1135 assert(isSplat() && "Cannot get splat index for non-splat!");
1136 EVT VT = getValueType(0);
1137 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1143 static bool isSplatMask(const int *Mask, EVT VT);
1145 static bool classof(const ShuffleVectorSDNode *) { return true; }
1146 static bool classof(const SDNode *N) {
1147 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1151 class ConstantSDNode : public SDNode {
1152 const ConstantInt *Value;
1153 friend class SelectionDAG;
1154 ConstantSDNode(bool isTarget, const ConstantInt *val, EVT VT)
1155 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1156 DebugLoc(), getSDVTList(VT)), Value(val) {
1160 const ConstantInt *getConstantIntValue() const { return Value; }
1161 const APInt &getAPIntValue() const { return Value->getValue(); }
1162 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1163 int64_t getSExtValue() const { return Value->getSExtValue(); }
1165 bool isOne() const { return Value->isOne(); }
1166 bool isNullValue() const { return Value->isNullValue(); }
1167 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1169 static bool classof(const ConstantSDNode *) { return true; }
1170 static bool classof(const SDNode *N) {
1171 return N->getOpcode() == ISD::Constant ||
1172 N->getOpcode() == ISD::TargetConstant;
1176 class ConstantFPSDNode : public SDNode {
1177 const ConstantFP *Value;
1178 friend class SelectionDAG;
1179 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1180 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1181 DebugLoc(), getSDVTList(VT)), Value(val) {
1185 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1186 const ConstantFP *getConstantFPValue() const { return Value; }
1188 /// isZero - Return true if the value is positive or negative zero.
1189 bool isZero() const { return Value->isZero(); }
1191 /// isNaN - Return true if the value is a NaN.
1192 bool isNaN() const { return Value->isNaN(); }
1194 /// isExactlyValue - We don't rely on operator== working on double values, as
1195 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1196 /// As such, this method can be used to do an exact bit-for-bit comparison of
1197 /// two floating point values.
1199 /// We leave the version with the double argument here because it's just so
1200 /// convenient to write "2.0" and the like. Without this function we'd
1201 /// have to duplicate its logic everywhere it's called.
1202 bool isExactlyValue(double V) const {
1204 // convert is not supported on this type
1205 if (&Value->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1208 Tmp.convert(Value->getValueAPF().getSemantics(),
1209 APFloat::rmNearestTiesToEven, &ignored);
1210 return isExactlyValue(Tmp);
1212 bool isExactlyValue(const APFloat& V) const;
1214 static bool isValueValidForType(EVT VT, const APFloat& Val);
1216 static bool classof(const ConstantFPSDNode *) { return true; }
1217 static bool classof(const SDNode *N) {
1218 return N->getOpcode() == ISD::ConstantFP ||
1219 N->getOpcode() == ISD::TargetConstantFP;
1223 class GlobalAddressSDNode : public SDNode {
1224 const GlobalValue *TheGlobal;
1226 unsigned char TargetFlags;
1227 friend class SelectionDAG;
1228 GlobalAddressSDNode(unsigned Opc, DebugLoc DL, const GlobalValue *GA, EVT VT,
1229 int64_t o, unsigned char TargetFlags);
1232 const GlobalValue *getGlobal() const { return TheGlobal; }
1233 int64_t getOffset() const { return Offset; }
1234 unsigned char getTargetFlags() const { return TargetFlags; }
1235 // Return the address space this GlobalAddress belongs to.
1236 unsigned getAddressSpace() const;
1238 static bool classof(const GlobalAddressSDNode *) { return true; }
1239 static bool classof(const SDNode *N) {
1240 return N->getOpcode() == ISD::GlobalAddress ||
1241 N->getOpcode() == ISD::TargetGlobalAddress ||
1242 N->getOpcode() == ISD::GlobalTLSAddress ||
1243 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1247 class FrameIndexSDNode : public SDNode {
1249 friend class SelectionDAG;
1250 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1251 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1252 DebugLoc(), getSDVTList(VT)), FI(fi) {
1256 int getIndex() const { return FI; }
1258 static bool classof(const FrameIndexSDNode *) { return true; }
1259 static bool classof(const SDNode *N) {
1260 return N->getOpcode() == ISD::FrameIndex ||
1261 N->getOpcode() == ISD::TargetFrameIndex;
1265 class JumpTableSDNode : public SDNode {
1267 unsigned char TargetFlags;
1268 friend class SelectionDAG;
1269 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1270 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1271 DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1275 int getIndex() const { return JTI; }
1276 unsigned char getTargetFlags() const { return TargetFlags; }
1278 static bool classof(const JumpTableSDNode *) { return true; }
1279 static bool classof(const SDNode *N) {
1280 return N->getOpcode() == ISD::JumpTable ||
1281 N->getOpcode() == ISD::TargetJumpTable;
1285 class ConstantPoolSDNode : public SDNode {
1287 const Constant *ConstVal;
1288 MachineConstantPoolValue *MachineCPVal;
1290 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1291 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1292 unsigned char TargetFlags;
1293 friend class SelectionDAG;
1294 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1295 unsigned Align, unsigned char TF)
1296 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1298 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1299 assert((int)Offset >= 0 && "Offset is too large");
1302 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1303 EVT VT, int o, unsigned Align, unsigned char TF)
1304 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool,
1306 getSDVTList(VT)), Offset(o), Alignment(Align), TargetFlags(TF) {
1307 assert((int)Offset >= 0 && "Offset is too large");
1308 Val.MachineCPVal = v;
1309 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1314 bool isMachineConstantPoolEntry() const {
1315 return (int)Offset < 0;
1318 const Constant *getConstVal() const {
1319 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1320 return Val.ConstVal;
1323 MachineConstantPoolValue *getMachineCPVal() const {
1324 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1325 return Val.MachineCPVal;
1328 int getOffset() const {
1329 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1332 // Return the alignment of this constant pool object, which is either 0 (for
1333 // default alignment) or the desired value.
1334 unsigned getAlignment() const { return Alignment; }
1335 unsigned char getTargetFlags() const { return TargetFlags; }
1337 Type *getType() const;
1339 static bool classof(const ConstantPoolSDNode *) { return true; }
1340 static bool classof(const SDNode *N) {
1341 return N->getOpcode() == ISD::ConstantPool ||
1342 N->getOpcode() == ISD::TargetConstantPool;
1346 class BasicBlockSDNode : public SDNode {
1347 MachineBasicBlock *MBB;
1348 friend class SelectionDAG;
1349 /// Debug info is meaningful and potentially useful here, but we create
1350 /// blocks out of order when they're jumped to, which makes it a bit
1351 /// harder. Let's see if we need it first.
1352 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1353 : SDNode(ISD::BasicBlock, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb) {
1357 MachineBasicBlock *getBasicBlock() const { return MBB; }
1359 static bool classof(const BasicBlockSDNode *) { return true; }
1360 static bool classof(const SDNode *N) {
1361 return N->getOpcode() == ISD::BasicBlock;
1365 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1367 class BuildVectorSDNode : public SDNode {
1368 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1369 explicit BuildVectorSDNode(); // Do not implement
1371 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1372 /// smallest element size that splats the vector. If MinSplatBits is
1373 /// nonzero, the element size must be at least that large. Note that the
1374 /// splat element may be the entire vector (i.e., a one element vector).
1375 /// Returns the splat element value in SplatValue. Any undefined bits in
1376 /// that value are zero, and the corresponding bits in the SplatUndef mask
1377 /// are set. The SplatBitSize value is set to the splat element size in
1378 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1379 /// undefined. isBigEndian describes the endianness of the target.
1380 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1381 unsigned &SplatBitSize, bool &HasAnyUndefs,
1382 unsigned MinSplatBits = 0, bool isBigEndian = false);
1384 static inline bool classof(const BuildVectorSDNode *) { return true; }
1385 static inline bool classof(const SDNode *N) {
1386 return N->getOpcode() == ISD::BUILD_VECTOR;
1390 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1391 /// used when the SelectionDAG needs to make a simple reference to something
1392 /// in the LLVM IR representation.
1394 class SrcValueSDNode : public SDNode {
1396 friend class SelectionDAG;
1397 /// Create a SrcValue for a general value.
1398 explicit SrcValueSDNode(const Value *v)
1399 : SDNode(ISD::SRCVALUE, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1402 /// getValue - return the contained Value.
1403 const Value *getValue() const { return V; }
1405 static bool classof(const SrcValueSDNode *) { return true; }
1406 static bool classof(const SDNode *N) {
1407 return N->getOpcode() == ISD::SRCVALUE;
1411 class MDNodeSDNode : public SDNode {
1413 friend class SelectionDAG;
1414 explicit MDNodeSDNode(const MDNode *md)
1415 : SDNode(ISD::MDNODE_SDNODE, DebugLoc(), getSDVTList(MVT::Other)), MD(md) {}
1418 const MDNode *getMD() const { return MD; }
1420 static bool classof(const MDNodeSDNode *) { return true; }
1421 static bool classof(const SDNode *N) {
1422 return N->getOpcode() == ISD::MDNODE_SDNODE;
1427 class RegisterSDNode : public SDNode {
1429 friend class SelectionDAG;
1430 RegisterSDNode(unsigned reg, EVT VT)
1431 : SDNode(ISD::Register, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1435 unsigned getReg() const { return Reg; }
1437 static bool classof(const RegisterSDNode *) { return true; }
1438 static bool classof(const SDNode *N) {
1439 return N->getOpcode() == ISD::Register;
1443 class RegisterMaskSDNode : public SDNode {
1444 // The memory for RegMask is not owned by the node.
1445 const uint32_t *RegMask;
1446 friend class SelectionDAG;
1447 RegisterMaskSDNode(const uint32_t *mask)
1448 : SDNode(ISD::RegisterMask, DebugLoc(), getSDVTList(MVT::Untyped)),
1452 const uint32_t *getRegMask() const { return RegMask; }
1454 static bool classof(const RegisterMaskSDNode *) { return true; }
1455 static bool classof(const SDNode *N) {
1456 return N->getOpcode() == ISD::RegisterMask;
1460 class BlockAddressSDNode : public SDNode {
1461 const BlockAddress *BA;
1462 unsigned char TargetFlags;
1463 friend class SelectionDAG;
1464 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1465 unsigned char Flags)
1466 : SDNode(NodeTy, DebugLoc(), getSDVTList(VT)),
1467 BA(ba), TargetFlags(Flags) {
1470 const BlockAddress *getBlockAddress() const { return BA; }
1471 unsigned char getTargetFlags() const { return TargetFlags; }
1473 static bool classof(const BlockAddressSDNode *) { return true; }
1474 static bool classof(const SDNode *N) {
1475 return N->getOpcode() == ISD::BlockAddress ||
1476 N->getOpcode() == ISD::TargetBlockAddress;
1480 class EHLabelSDNode : public SDNode {
1483 friend class SelectionDAG;
1484 EHLabelSDNode(DebugLoc dl, SDValue ch, MCSymbol *L)
1485 : SDNode(ISD::EH_LABEL, dl, getSDVTList(MVT::Other)), Label(L) {
1486 InitOperands(&Chain, ch);
1489 MCSymbol *getLabel() const { return Label; }
1491 static bool classof(const EHLabelSDNode *) { return true; }
1492 static bool classof(const SDNode *N) {
1493 return N->getOpcode() == ISD::EH_LABEL;
1497 class ExternalSymbolSDNode : public SDNode {
1499 unsigned char TargetFlags;
1501 friend class SelectionDAG;
1502 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1503 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1504 DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1508 const char *getSymbol() const { return Symbol; }
1509 unsigned char getTargetFlags() const { return TargetFlags; }
1511 static bool classof(const ExternalSymbolSDNode *) { return true; }
1512 static bool classof(const SDNode *N) {
1513 return N->getOpcode() == ISD::ExternalSymbol ||
1514 N->getOpcode() == ISD::TargetExternalSymbol;
1518 class CondCodeSDNode : public SDNode {
1519 ISD::CondCode Condition;
1520 friend class SelectionDAG;
1521 explicit CondCodeSDNode(ISD::CondCode Cond)
1522 : SDNode(ISD::CONDCODE, DebugLoc(), getSDVTList(MVT::Other)),
1527 ISD::CondCode get() const { return Condition; }
1529 static bool classof(const CondCodeSDNode *) { return true; }
1530 static bool classof(const SDNode *N) {
1531 return N->getOpcode() == ISD::CONDCODE;
1535 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1536 /// future and most targets don't support it.
1537 class CvtRndSatSDNode : public SDNode {
1538 ISD::CvtCode CvtCode;
1539 friend class SelectionDAG;
1540 explicit CvtRndSatSDNode(EVT VT, DebugLoc dl, const SDValue *Ops,
1541 unsigned NumOps, ISD::CvtCode Code)
1542 : SDNode(ISD::CONVERT_RNDSAT, dl, getSDVTList(VT), Ops, NumOps),
1544 assert(NumOps == 5 && "wrong number of operations");
1547 ISD::CvtCode getCvtCode() const { return CvtCode; }
1549 static bool classof(const CvtRndSatSDNode *) { return true; }
1550 static bool classof(const SDNode *N) {
1551 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1555 /// VTSDNode - This class is used to represent EVT's, which are used
1556 /// to parameterize some operations.
1557 class VTSDNode : public SDNode {
1559 friend class SelectionDAG;
1560 explicit VTSDNode(EVT VT)
1561 : SDNode(ISD::VALUETYPE, DebugLoc(), getSDVTList(MVT::Other)),
1566 EVT getVT() const { return ValueType; }
1568 static bool classof(const VTSDNode *) { return true; }
1569 static bool classof(const SDNode *N) {
1570 return N->getOpcode() == ISD::VALUETYPE;
1574 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1576 class LSBaseSDNode : public MemSDNode {
1577 //! Operand array for load and store
1579 \note Moving this array to the base class captures more
1580 common functionality shared between LoadSDNode and
1585 LSBaseSDNode(ISD::NodeType NodeTy, DebugLoc dl, SDValue *Operands,
1586 unsigned numOperands, SDVTList VTs, ISD::MemIndexedMode AM,
1587 EVT MemVT, MachineMemOperand *MMO)
1588 : MemSDNode(NodeTy, dl, VTs, MemVT, MMO) {
1589 SubclassData |= AM << 2;
1590 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1591 InitOperands(Ops, Operands, numOperands);
1592 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1593 "Only indexed loads and stores have a non-undef offset operand");
1596 const SDValue &getOffset() const {
1597 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1600 /// getAddressingMode - Return the addressing mode for this load or store:
1601 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1602 ISD::MemIndexedMode getAddressingMode() const {
1603 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1606 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1607 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1609 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1610 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1612 static bool classof(const LSBaseSDNode *) { return true; }
1613 static bool classof(const SDNode *N) {
1614 return N->getOpcode() == ISD::LOAD ||
1615 N->getOpcode() == ISD::STORE;
1619 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1621 class LoadSDNode : public LSBaseSDNode {
1622 friend class SelectionDAG;
1623 LoadSDNode(SDValue *ChainPtrOff, DebugLoc dl, SDVTList VTs,
1624 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1625 MachineMemOperand *MMO)
1626 : LSBaseSDNode(ISD::LOAD, dl, ChainPtrOff, 3,
1627 VTs, AM, MemVT, MMO) {
1628 SubclassData |= (unsigned short)ETy;
1629 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1630 assert(readMem() && "Load MachineMemOperand is not a load!");
1631 assert(!writeMem() && "Load MachineMemOperand is a store!");
1635 /// getExtensionType - Return whether this is a plain node,
1636 /// or one of the varieties of value-extending loads.
1637 ISD::LoadExtType getExtensionType() const {
1638 return ISD::LoadExtType(SubclassData & 3);
1641 const SDValue &getBasePtr() const { return getOperand(1); }
1642 const SDValue &getOffset() const { return getOperand(2); }
1644 static bool classof(const LoadSDNode *) { return true; }
1645 static bool classof(const SDNode *N) {
1646 return N->getOpcode() == ISD::LOAD;
1650 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1652 class StoreSDNode : public LSBaseSDNode {
1653 friend class SelectionDAG;
1654 StoreSDNode(SDValue *ChainValuePtrOff, DebugLoc dl, SDVTList VTs,
1655 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1656 MachineMemOperand *MMO)
1657 : LSBaseSDNode(ISD::STORE, dl, ChainValuePtrOff, 4,
1658 VTs, AM, MemVT, MMO) {
1659 SubclassData |= (unsigned short)isTrunc;
1660 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1661 assert(!readMem() && "Store MachineMemOperand is a load!");
1662 assert(writeMem() && "Store MachineMemOperand is not a store!");
1666 /// isTruncatingStore - Return true if the op does a truncation before store.
1667 /// For integers this is the same as doing a TRUNCATE and storing the result.
1668 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1669 bool isTruncatingStore() const { return SubclassData & 1; }
1671 const SDValue &getValue() const { return getOperand(1); }
1672 const SDValue &getBasePtr() const { return getOperand(2); }
1673 const SDValue &getOffset() const { return getOperand(3); }
1675 static bool classof(const StoreSDNode *) { return true; }
1676 static bool classof(const SDNode *N) {
1677 return N->getOpcode() == ISD::STORE;
1681 /// MachineSDNode - An SDNode that represents everything that will be needed
1682 /// to construct a MachineInstr. These nodes are created during the
1683 /// instruction selection proper phase.
1685 class MachineSDNode : public SDNode {
1687 typedef MachineMemOperand **mmo_iterator;
1690 friend class SelectionDAG;
1691 MachineSDNode(unsigned Opc, const DebugLoc DL, SDVTList VTs)
1692 : SDNode(Opc, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1694 /// LocalOperands - Operands for this instruction, if they fit here. If
1695 /// they don't, this field is unused.
1696 SDUse LocalOperands[4];
1698 /// MemRefs - Memory reference descriptions for this instruction.
1699 mmo_iterator MemRefs;
1700 mmo_iterator MemRefsEnd;
1703 mmo_iterator memoperands_begin() const { return MemRefs; }
1704 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1705 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1707 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1708 /// list. This does not transfer ownership.
1709 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1710 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1711 assert(*MMI && "Null mem ref detected!");
1712 MemRefs = NewMemRefs;
1713 MemRefsEnd = NewMemRefsEnd;
1716 static bool classof(const MachineSDNode *) { return true; }
1717 static bool classof(const SDNode *N) {
1718 return N->isMachineOpcode();
1722 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1723 SDNode, ptrdiff_t> {
1727 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1729 bool operator==(const SDNodeIterator& x) const {
1730 return Operand == x.Operand;
1732 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1734 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1735 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1736 Operand = I.Operand;
1740 pointer operator*() const {
1741 return Node->getOperand(Operand).getNode();
1743 pointer operator->() const { return operator*(); }
1745 SDNodeIterator& operator++() { // Preincrement
1749 SDNodeIterator operator++(int) { // Postincrement
1750 SDNodeIterator tmp = *this; ++*this; return tmp;
1752 size_t operator-(SDNodeIterator Other) const {
1753 assert(Node == Other.Node &&
1754 "Cannot compare iterators of two different nodes!");
1755 return Operand - Other.Operand;
1758 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
1759 static SDNodeIterator end (SDNode *N) {
1760 return SDNodeIterator(N, N->getNumOperands());
1763 unsigned getOperand() const { return Operand; }
1764 const SDNode *getNode() const { return Node; }
1767 template <> struct GraphTraits<SDNode*> {
1768 typedef SDNode NodeType;
1769 typedef SDNodeIterator ChildIteratorType;
1770 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1771 static inline ChildIteratorType child_begin(NodeType *N) {
1772 return SDNodeIterator::begin(N);
1774 static inline ChildIteratorType child_end(NodeType *N) {
1775 return SDNodeIterator::end(N);
1779 /// LargestSDNode - The largest SDNode class.
1781 typedef LoadSDNode LargestSDNode;
1783 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1786 typedef GlobalAddressSDNode MostAlignedSDNode;
1789 /// isNormalLoad - Returns true if the specified node is a non-extending
1790 /// and unindexed load.
1791 inline bool isNormalLoad(const SDNode *N) {
1792 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1793 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1794 Ld->getAddressingMode() == ISD::UNINDEXED;
1797 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1799 inline bool isNON_EXTLoad(const SDNode *N) {
1800 return isa<LoadSDNode>(N) &&
1801 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1804 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1806 inline bool isEXTLoad(const SDNode *N) {
1807 return isa<LoadSDNode>(N) &&
1808 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1811 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1813 inline bool isSEXTLoad(const SDNode *N) {
1814 return isa<LoadSDNode>(N) &&
1815 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1818 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1820 inline bool isZEXTLoad(const SDNode *N) {
1821 return isa<LoadSDNode>(N) &&
1822 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1825 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1827 inline bool isUNINDEXEDLoad(const SDNode *N) {
1828 return isa<LoadSDNode>(N) &&
1829 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1832 /// isNormalStore - Returns true if the specified node is a non-truncating
1833 /// and unindexed store.
1834 inline bool isNormalStore(const SDNode *N) {
1835 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1836 return St && !St->isTruncatingStore() &&
1837 St->getAddressingMode() == ISD::UNINDEXED;
1840 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1842 inline bool isNON_TRUNCStore(const SDNode *N) {
1843 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1846 /// isTRUNCStore - Returns true if the specified node is a truncating
1848 inline bool isTRUNCStore(const SDNode *N) {
1849 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1852 /// isUNINDEXEDStore - Returns true if the specified node is an
1853 /// unindexed store.
1854 inline bool isUNINDEXEDStore(const SDNode *N) {
1855 return isa<StoreSDNode>(N) &&
1856 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1860 } // end llvm namespace