1 //===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SDNode class and derived classes, which are used to
11 // represent the nodes and operations present in a SelectionDAG. These nodes
12 // and operations are machine code level operations, with some similarities to
13 // the GCC RTL representation.
15 // Clients should include the SelectionDAG.h file instead of this file directly.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/ilist_node.h"
28 #include "llvm/CodeGen/ISDOpcodes.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DebugLoc.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/Support/MathExtras.h"
42 class MachineBasicBlock;
43 class MachineConstantPoolValue;
47 template <typename T> struct DenseMapInfo;
48 template <typename T> struct simplify_type;
49 template <typename T> struct ilist_traits;
51 void checkForCycles(const SDNode *N);
53 /// SDVTList - This represents a list of ValueType's that has been intern'd by
54 /// a SelectionDAG. Instances of this simple value class are returned by
55 /// SelectionDAG::getVTList(...).
65 /// isBuildVectorAllOnes - Return true if the specified node is a
66 /// BUILD_VECTOR where all of the elements are ~0 or undef.
67 bool isBuildVectorAllOnes(const SDNode *N);
69 /// isBuildVectorAllZeros - Return true if the specified node is a
70 /// BUILD_VECTOR where all of the elements are 0 or undef.
71 bool isBuildVectorAllZeros(const SDNode *N);
73 /// \brief Return true if the specified node is a BUILD_VECTOR node of
74 /// all ConstantSDNode or undef.
75 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
77 /// isScalarToVector - Return true if the specified node is a
78 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
79 /// element is not an undef.
80 bool isScalarToVector(const SDNode *N);
82 /// allOperandsUndef - Return true if the node has at least one operand
83 /// and all operands of the specified node are ISD::UNDEF.
84 bool allOperandsUndef(const SDNode *N);
85 } // end llvm:ISD namespace
87 //===----------------------------------------------------------------------===//
88 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
89 /// values as the result of a computation. Many nodes return multiple values,
90 /// from loads (which define a token and a return value) to ADDC (which returns
91 /// a result and a carry value), to calls (which may return an arbitrary number
94 /// As such, each use of a SelectionDAG computation must indicate the node that
95 /// computes it as well as which return value to use from that node. This pair
96 /// of information is represented with the SDValue value type.
99 SDNode *Node; // The node defining the value we are using.
100 unsigned ResNo; // Which return value of the node we are using.
102 SDValue() : Node(0), ResNo(0) {}
103 SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
105 /// get the index which selects a specific result in the SDNode
106 unsigned getResNo() const { return ResNo; }
108 /// get the SDNode which holds the desired result
109 SDNode *getNode() const { return Node; }
112 void setNode(SDNode *N) { Node = N; }
114 inline SDNode *operator->() const { return Node; }
116 bool operator==(const SDValue &O) const {
117 return Node == O.Node && ResNo == O.ResNo;
119 bool operator!=(const SDValue &O) const {
120 return !operator==(O);
122 bool operator<(const SDValue &O) const {
123 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
126 SDValue getValue(unsigned R) const {
127 return SDValue(Node, R);
130 // isOperandOf - Return true if this node is an operand of N.
131 bool isOperandOf(SDNode *N) const;
133 /// getValueType - Return the ValueType of the referenced return value.
135 inline EVT getValueType() const;
137 /// Return the simple ValueType of the referenced return value.
138 MVT getSimpleValueType() const {
139 return getValueType().getSimpleVT();
142 /// getValueSizeInBits - Returns the size of the value in bits.
144 unsigned getValueSizeInBits() const {
145 return getValueType().getSizeInBits();
148 unsigned getScalarValueSizeInBits() const {
149 return getValueType().getScalarType().getSizeInBits();
152 // Forwarding methods - These forward to the corresponding methods in SDNode.
153 inline unsigned getOpcode() const;
154 inline unsigned getNumOperands() const;
155 inline const SDValue &getOperand(unsigned i) const;
156 inline uint64_t getConstantOperandVal(unsigned i) const;
157 inline bool isTargetMemoryOpcode() const;
158 inline bool isTargetOpcode() const;
159 inline bool isMachineOpcode() const;
160 inline unsigned getMachineOpcode() const;
161 inline const DebugLoc getDebugLoc() const;
162 inline void dump() const;
163 inline void dumpr() const;
165 /// reachesChainWithoutSideEffects - Return true if this operand (which must
166 /// be a chain) reaches the specified operand without crossing any
167 /// side-effecting instructions. In practice, this looks through token
168 /// factors and non-volatile loads. In order to remain efficient, this only
169 /// looks a couple of nodes in, it does not do an exhaustive search.
170 bool reachesChainWithoutSideEffects(SDValue Dest,
171 unsigned Depth = 2) const;
173 /// use_empty - Return true if there are no nodes using value ResNo
176 inline bool use_empty() const;
178 /// hasOneUse - Return true if there is exactly one node using value
181 inline bool hasOneUse() const;
185 template<> struct DenseMapInfo<SDValue> {
186 static inline SDValue getEmptyKey() {
187 return SDValue((SDNode*)-1, -1U);
189 static inline SDValue getTombstoneKey() {
190 return SDValue((SDNode*)-1, 0);
192 static unsigned getHashValue(const SDValue &Val) {
193 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
194 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
196 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
200 template <> struct isPodLike<SDValue> { static const bool value = true; };
203 /// simplify_type specializations - Allow casting operators to work directly on
204 /// SDValues as if they were SDNode*'s.
205 template<> struct simplify_type<SDValue> {
206 typedef SDNode* SimpleType;
207 static SimpleType getSimplifiedValue(SDValue &Val) {
208 return Val.getNode();
211 template<> struct simplify_type<const SDValue> {
212 typedef /*const*/ SDNode* SimpleType;
213 static SimpleType getSimplifiedValue(const SDValue &Val) {
214 return Val.getNode();
218 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
219 /// which records the SDNode being used and the result number, a
220 /// pointer to the SDNode using the value, and Next and Prev pointers,
221 /// which link together all the uses of an SDNode.
224 /// Val - The value being used.
226 /// User - The user of this value.
228 /// Prev, Next - Pointers to the uses list of the SDNode referred by
232 SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
233 void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
236 SDUse() : Val(), User(NULL), Prev(NULL), Next(NULL) {}
238 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
239 operator const SDValue&() const { return Val; }
241 /// If implicit conversion to SDValue doesn't work, the get() method returns
243 const SDValue &get() const { return Val; }
245 /// getUser - This returns the SDNode that contains this Use.
246 SDNode *getUser() { return User; }
248 /// getNext - Get the next SDUse in the use list.
249 SDUse *getNext() const { return Next; }
251 /// getNode - Convenience function for get().getNode().
252 SDNode *getNode() const { return Val.getNode(); }
253 /// getResNo - Convenience function for get().getResNo().
254 unsigned getResNo() const { return Val.getResNo(); }
255 /// getValueType - Convenience function for get().getValueType().
256 EVT getValueType() const { return Val.getValueType(); }
258 /// operator== - Convenience function for get().operator==
259 bool operator==(const SDValue &V) const {
263 /// operator!= - Convenience function for get().operator!=
264 bool operator!=(const SDValue &V) const {
268 /// operator< - Convenience function for get().operator<
269 bool operator<(const SDValue &V) const {
274 friend class SelectionDAG;
277 void setUser(SDNode *p) { User = p; }
279 /// set - Remove this use from its existing use list, assign it the
280 /// given value, and add it to the new value's node's use list.
281 inline void set(const SDValue &V);
282 /// setInitial - like set, but only supports initializing a newly-allocated
283 /// SDUse with a non-null value.
284 inline void setInitial(const SDValue &V);
285 /// setNode - like set, but only sets the Node portion of the value,
286 /// leaving the ResNo portion unmodified.
287 inline void setNode(SDNode *N);
289 void addToList(SDUse **List) {
291 if (Next) Next->Prev = &Next;
296 void removeFromList() {
298 if (Next) Next->Prev = Prev;
302 /// simplify_type specializations - Allow casting operators to work directly on
303 /// SDValues as if they were SDNode*'s.
304 template<> struct simplify_type<SDUse> {
305 typedef SDNode* SimpleType;
306 static SimpleType getSimplifiedValue(SDUse &Val) {
307 return Val.getNode();
312 /// SDNode - Represents one node in the SelectionDAG.
314 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
316 /// NodeType - The operation that this node performs.
320 /// OperandsNeedDelete - This is true if OperandList was new[]'d. If true,
321 /// then they will be delete[]'d when the node is destroyed.
322 uint16_t OperandsNeedDelete : 1;
324 /// HasDebugValue - This tracks whether this node has one or more dbg_value
325 /// nodes corresponding to it.
326 uint16_t HasDebugValue : 1;
329 /// SubclassData - This member is defined by this class, but is not used for
330 /// anything. Subclasses can use it to hold whatever state they find useful.
331 /// This field is initialized to zero by the ctor.
332 uint16_t SubclassData : 14;
335 /// NodeId - Unique id per SDNode in the DAG.
338 /// OperandList - The values that are used by this operation.
342 /// ValueList - The types of the values this node defines. SDNode's may
343 /// define multiple values simultaneously.
344 const EVT *ValueList;
346 /// UseList - List of uses for this SDNode.
349 /// NumOperands/NumValues - The number of entries in the Operand/Value list.
350 unsigned short NumOperands, NumValues;
352 /// debugLoc - source line information.
355 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
356 // original LLVM instructions.
357 // This is used for turning off scheduling, because we'll forgo
358 // the normal scheduling algorithms and output the instructions according to
362 /// getValueTypeList - Return a pointer to the specified value type.
363 static const EVT *getValueTypeList(EVT VT);
365 friend class SelectionDAG;
366 friend struct ilist_traits<SDNode>;
369 //===--------------------------------------------------------------------===//
373 /// getOpcode - Return the SelectionDAG opcode value for this node. For
374 /// pre-isel nodes (those for which isMachineOpcode returns false), these
375 /// are the opcode values in the ISD and <target>ISD namespaces. For
376 /// post-isel opcodes, see getMachineOpcode.
377 unsigned getOpcode() const { return (unsigned short)NodeType; }
379 /// isTargetOpcode - Test if this node has a target-specific opcode (in the
380 /// \<target\>ISD namespace).
381 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
383 /// isTargetMemoryOpcode - Test if this node has a target-specific
384 /// memory-referencing opcode (in the \<target\>ISD namespace and
385 /// greater than FIRST_TARGET_MEMORY_OPCODE).
386 bool isTargetMemoryOpcode() const {
387 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
390 /// isMachineOpcode - Test if this node has a post-isel opcode, directly
391 /// corresponding to a MachineInstr opcode.
392 bool isMachineOpcode() const { return NodeType < 0; }
394 /// getMachineOpcode - This may only be called if isMachineOpcode returns
395 /// true. It returns the MachineInstr opcode value that the node's opcode
397 unsigned getMachineOpcode() const {
398 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
402 /// getHasDebugValue - get this bit.
403 bool getHasDebugValue() const { return HasDebugValue; }
405 /// setHasDebugValue - set this bit.
406 void setHasDebugValue(bool b) { HasDebugValue = b; }
408 /// use_empty - Return true if there are no uses of this node.
410 bool use_empty() const { return UseList == NULL; }
412 /// hasOneUse - Return true if there is exactly one use of this node.
414 bool hasOneUse() const {
415 return !use_empty() && std::next(use_begin()) == use_end();
418 /// use_size - Return the number of uses of this node. This method takes
419 /// time proportional to the number of uses.
421 size_t use_size() const { return std::distance(use_begin(), use_end()); }
423 /// getNodeId - Return the unique node id.
425 int getNodeId() const { return NodeId; }
427 /// setNodeId - Set unique node id.
428 void setNodeId(int Id) { NodeId = Id; }
430 /// getIROrder - Return the node ordering.
432 unsigned getIROrder() const { return IROrder; }
434 /// setIROrder - Set the node ordering.
436 void setIROrder(unsigned Order) { IROrder = Order; }
438 /// getDebugLoc - Return the source location info.
439 const DebugLoc getDebugLoc() const { return debugLoc; }
441 /// setDebugLoc - Set source location info. Try to avoid this, putting
442 /// it in the constructor is preferable.
443 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
445 /// use_iterator - This class provides iterator support for SDUse
446 /// operands that use a specific SDNode.
448 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
450 explicit use_iterator(SDUse *op) : Op(op) {
454 typedef std::iterator<std::forward_iterator_tag,
455 SDUse, ptrdiff_t>::reference reference;
456 typedef std::iterator<std::forward_iterator_tag,
457 SDUse, ptrdiff_t>::pointer pointer;
459 use_iterator(const use_iterator &I) : Op(I.Op) {}
460 use_iterator() : Op(0) {}
462 bool operator==(const use_iterator &x) const {
465 bool operator!=(const use_iterator &x) const {
466 return !operator==(x);
469 /// atEnd - return true if this iterator is at the end of uses list.
470 bool atEnd() const { return Op == 0; }
472 // Iterator traversal: forward iteration only.
473 use_iterator &operator++() { // Preincrement
474 assert(Op && "Cannot increment end iterator!");
479 use_iterator operator++(int) { // Postincrement
480 use_iterator tmp = *this; ++*this; return tmp;
483 /// Retrieve a pointer to the current user node.
484 SDNode *operator*() const {
485 assert(Op && "Cannot dereference end iterator!");
486 return Op->getUser();
489 SDNode *operator->() const { return operator*(); }
491 SDUse &getUse() const { return *Op; }
493 /// getOperandNo - Retrieve the operand # of this use in its user.
495 unsigned getOperandNo() const {
496 assert(Op && "Cannot dereference end iterator!");
497 return (unsigned)(Op - Op->getUser()->OperandList);
501 /// use_begin/use_end - Provide iteration support to walk over all uses
504 use_iterator use_begin() const {
505 return use_iterator(UseList);
508 static use_iterator use_end() { return use_iterator(0); }
511 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
512 /// indicated value. This method ignores uses of other values defined by this
514 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
516 /// hasAnyUseOfValue - Return true if there are any use of the indicated
517 /// value. This method ignores uses of other values defined by this operation.
518 bool hasAnyUseOfValue(unsigned Value) const;
520 /// isOnlyUserOf - Return true if this node is the only use of N.
522 bool isOnlyUserOf(SDNode *N) const;
524 /// isOperandOf - Return true if this node is an operand of N.
526 bool isOperandOf(SDNode *N) const;
528 /// isPredecessorOf - Return true if this node is a predecessor of N.
529 /// NOTE: Implemented on top of hasPredecessor and every bit as
530 /// expensive. Use carefully.
531 bool isPredecessorOf(const SDNode *N) const {
532 return N->hasPredecessor(this);
535 /// hasPredecessor - Return true if N is a predecessor of this node.
536 /// N is either an operand of this node, or can be reached by recursively
537 /// traversing up the operands.
538 /// NOTE: This is an expensive method. Use it carefully.
539 bool hasPredecessor(const SDNode *N) const;
541 /// hasPredecesorHelper - Return true if N is a predecessor of this node.
542 /// N is either an operand of this node, or can be reached by recursively
543 /// traversing up the operands.
544 /// In this helper the Visited and worklist sets are held externally to
545 /// cache predecessors over multiple invocations. If you want to test for
546 /// multiple predecessors this method is preferable to multiple calls to
547 /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
549 /// NOTE: This is still very expensive. Use carefully.
550 bool hasPredecessorHelper(const SDNode *N,
551 SmallPtrSet<const SDNode *, 32> &Visited,
552 SmallVectorImpl<const SDNode *> &Worklist) const;
554 /// getNumOperands - Return the number of values used by this operation.
556 unsigned getNumOperands() const { return NumOperands; }
558 /// getConstantOperandVal - Helper method returns the integer value of a
559 /// ConstantSDNode operand.
560 uint64_t getConstantOperandVal(unsigned Num) const;
562 const SDValue &getOperand(unsigned Num) const {
563 assert(Num < NumOperands && "Invalid child # of SDNode!");
564 return OperandList[Num];
567 typedef SDUse* op_iterator;
568 op_iterator op_begin() const { return OperandList; }
569 op_iterator op_end() const { return OperandList+NumOperands; }
571 SDVTList getVTList() const {
572 SDVTList X = { ValueList, NumValues };
576 /// getGluedNode - If this node has a glue operand, return the node
577 /// to which the glue operand points. Otherwise return NULL.
578 SDNode *getGluedNode() const {
579 if (getNumOperands() != 0 &&
580 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
581 return getOperand(getNumOperands()-1).getNode();
585 // If this is a pseudo op, like copyfromreg, look to see if there is a
586 // real target node glued to it. If so, return the target node.
587 const SDNode *getGluedMachineNode() const {
588 const SDNode *FoundNode = this;
590 // Climb up glue edges until a machine-opcode node is found, or the
591 // end of the chain is reached.
592 while (!FoundNode->isMachineOpcode()) {
593 const SDNode *N = FoundNode->getGluedNode();
601 /// getGluedUser - If this node has a glue value with a user, return
602 /// the user (there is at most one). Otherwise return NULL.
603 SDNode *getGluedUser() const {
604 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
605 if (UI.getUse().get().getValueType() == MVT::Glue)
610 /// getNumValues - Return the number of values defined/returned by this
613 unsigned getNumValues() const { return NumValues; }
615 /// getValueType - Return the type of a specified result.
617 EVT getValueType(unsigned ResNo) const {
618 assert(ResNo < NumValues && "Illegal result number!");
619 return ValueList[ResNo];
622 /// Return the type of a specified result as a simple type.
624 MVT getSimpleValueType(unsigned ResNo) const {
625 return getValueType(ResNo).getSimpleVT();
628 /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
630 unsigned getValueSizeInBits(unsigned ResNo) const {
631 return getValueType(ResNo).getSizeInBits();
634 typedef const EVT* value_iterator;
635 value_iterator value_begin() const { return ValueList; }
636 value_iterator value_end() const { return ValueList+NumValues; }
638 /// getOperationName - Return the opcode of this operation for printing.
640 std::string getOperationName(const SelectionDAG *G = 0) const;
641 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
642 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
643 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
644 void print(raw_ostream &OS, const SelectionDAG *G = 0) const;
645 void printr(raw_ostream &OS, const SelectionDAG *G = 0) const;
647 /// printrFull - Print a SelectionDAG node and all children down to
648 /// the leaves. The given SelectionDAG allows target-specific nodes
649 /// to be printed in human-readable form. Unlike printr, this will
650 /// print the whole DAG, including children that appear multiple
653 void printrFull(raw_ostream &O, const SelectionDAG *G = 0) const;
655 /// printrWithDepth - Print a SelectionDAG node and children up to
656 /// depth "depth." The given SelectionDAG allows target-specific
657 /// nodes to be printed in human-readable form. Unlike printr, this
658 /// will print children that appear multiple times wherever they are
661 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = 0,
662 unsigned depth = 100) const;
665 /// dump - Dump this node, for debugging.
668 /// dumpr - Dump (recursively) this node and its use-def subgraph.
671 /// dump - Dump this node, for debugging.
672 /// The given SelectionDAG allows target-specific nodes to be printed
673 /// in human-readable form.
674 void dump(const SelectionDAG *G) const;
676 /// dumpr - Dump (recursively) this node and its use-def subgraph.
677 /// The given SelectionDAG allows target-specific nodes to be printed
678 /// in human-readable form.
679 void dumpr(const SelectionDAG *G) const;
681 /// dumprFull - printrFull to dbgs(). The given SelectionDAG allows
682 /// target-specific nodes to be printed in human-readable form.
683 /// Unlike dumpr, this will print the whole DAG, including children
684 /// that appear multiple times.
686 void dumprFull(const SelectionDAG *G = 0) const;
688 /// dumprWithDepth - printrWithDepth to dbgs(). The given
689 /// SelectionDAG allows target-specific nodes to be printed in
690 /// human-readable form. Unlike dumpr, this will print children
691 /// that appear multiple times wherever they are used.
693 void dumprWithDepth(const SelectionDAG *G = 0, unsigned depth = 100) const;
695 /// Profile - Gather unique data for the node.
697 void Profile(FoldingSetNodeID &ID) const;
699 /// addUse - This method should only be used by the SDUse class.
701 void addUse(SDUse &U) { U.addToList(&UseList); }
704 static SDVTList getSDVTList(EVT VT) {
705 SDVTList Ret = { getValueTypeList(VT), 1 };
709 SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs,
710 const SDValue *Ops, unsigned NumOps)
711 : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
712 SubclassData(0), NodeId(-1),
713 OperandList(NumOps ? new SDUse[NumOps] : 0),
714 ValueList(VTs.VTs), UseList(NULL),
715 NumOperands(NumOps), NumValues(VTs.NumVTs),
716 debugLoc(dl), IROrder(Order) {
717 for (unsigned i = 0; i != NumOps; ++i) {
718 OperandList[i].setUser(this);
719 OperandList[i].setInitial(Ops[i]);
721 checkForCycles(this);
724 /// This constructor adds no operands itself; operands can be
725 /// set later with InitOperands.
726 SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs)
727 : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
728 SubclassData(0), NodeId(-1), OperandList(0),
729 ValueList(VTs.VTs), UseList(NULL), NumOperands(0), NumValues(VTs.NumVTs),
730 debugLoc(dl), IROrder(Order) {}
732 /// InitOperands - Initialize the operands list of this with 1 operand.
733 void InitOperands(SDUse *Ops, const SDValue &Op0) {
734 Ops[0].setUser(this);
735 Ops[0].setInitial(Op0);
738 checkForCycles(this);
741 /// InitOperands - Initialize the operands list of this with 2 operands.
742 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
743 Ops[0].setUser(this);
744 Ops[0].setInitial(Op0);
745 Ops[1].setUser(this);
746 Ops[1].setInitial(Op1);
749 checkForCycles(this);
752 /// InitOperands - Initialize the operands list of this with 3 operands.
753 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
754 const SDValue &Op2) {
755 Ops[0].setUser(this);
756 Ops[0].setInitial(Op0);
757 Ops[1].setUser(this);
758 Ops[1].setInitial(Op1);
759 Ops[2].setUser(this);
760 Ops[2].setInitial(Op2);
763 checkForCycles(this);
766 /// InitOperands - Initialize the operands list of this with 4 operands.
767 void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
768 const SDValue &Op2, const SDValue &Op3) {
769 Ops[0].setUser(this);
770 Ops[0].setInitial(Op0);
771 Ops[1].setUser(this);
772 Ops[1].setInitial(Op1);
773 Ops[2].setUser(this);
774 Ops[2].setInitial(Op2);
775 Ops[3].setUser(this);
776 Ops[3].setInitial(Op3);
779 checkForCycles(this);
782 /// InitOperands - Initialize the operands list of this with N operands.
783 void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
784 for (unsigned i = 0; i != N; ++i) {
785 Ops[i].setUser(this);
786 Ops[i].setInitial(Vals[i]);
790 checkForCycles(this);
793 /// DropOperands - Release the operands and set this node to have
798 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
799 /// into SDNode creation functions.
800 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
801 /// from the original Instruction, and IROrder is the ordinal position of
803 /// When an SDNode is created after the DAG is being built, both DebugLoc and
804 /// the IROrder are propagated from the original SDNode.
805 /// So SDLoc class provides two constructors besides the default one, one to
806 /// be used by the DAGBuilder, the other to be used by others.
809 // Ptr could be used for either Instruction* or SDNode*. It is used for
810 // Instruction* if IROrder is not -1.
815 SDLoc() : Ptr(NULL), IROrder(0) {}
816 SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
817 assert(N && "null SDNode");
819 SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
820 assert(Ptr && "null SDNode");
822 SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
823 assert(Order >= 0 && "bad IROrder");
825 unsigned getIROrder() {
826 if (IROrder >= 0 || Ptr == NULL) {
827 return (unsigned)IROrder;
829 const SDNode *N = (const SDNode*)(Ptr);
830 return N->getIROrder();
832 DebugLoc getDebugLoc() {
837 const Instruction *I = (const Instruction*)(Ptr);
838 return I->getDebugLoc();
840 const SDNode *N = (const SDNode*)(Ptr);
841 return N->getDebugLoc();
846 // Define inline functions from the SDValue class.
848 inline unsigned SDValue::getOpcode() const {
849 return Node->getOpcode();
851 inline EVT SDValue::getValueType() const {
852 return Node->getValueType(ResNo);
854 inline unsigned SDValue::getNumOperands() const {
855 return Node->getNumOperands();
857 inline const SDValue &SDValue::getOperand(unsigned i) const {
858 return Node->getOperand(i);
860 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
861 return Node->getConstantOperandVal(i);
863 inline bool SDValue::isTargetOpcode() const {
864 return Node->isTargetOpcode();
866 inline bool SDValue::isTargetMemoryOpcode() const {
867 return Node->isTargetMemoryOpcode();
869 inline bool SDValue::isMachineOpcode() const {
870 return Node->isMachineOpcode();
872 inline unsigned SDValue::getMachineOpcode() const {
873 return Node->getMachineOpcode();
875 inline bool SDValue::use_empty() const {
876 return !Node->hasAnyUseOfValue(ResNo);
878 inline bool SDValue::hasOneUse() const {
879 return Node->hasNUsesOfValue(1, ResNo);
881 inline const DebugLoc SDValue::getDebugLoc() const {
882 return Node->getDebugLoc();
884 inline void SDValue::dump() const {
887 inline void SDValue::dumpr() const {
888 return Node->dumpr();
890 // Define inline functions from the SDUse class.
892 inline void SDUse::set(const SDValue &V) {
893 if (Val.getNode()) removeFromList();
895 if (V.getNode()) V.getNode()->addUse(*this);
898 inline void SDUse::setInitial(const SDValue &V) {
900 V.getNode()->addUse(*this);
903 inline void SDUse::setNode(SDNode *N) {
904 if (Val.getNode()) removeFromList();
906 if (N) N->addUse(*this);
909 /// UnarySDNode - This class is used for single-operand SDNodes. This is solely
910 /// to allow co-allocation of node operands with the node itself.
911 class UnarySDNode : public SDNode {
914 UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
916 : SDNode(Opc, Order, dl, VTs) {
917 InitOperands(&Op, X);
921 /// BinarySDNode - This class is used for two-operand SDNodes. This is solely
922 /// to allow co-allocation of node operands with the node itself.
923 class BinarySDNode : public SDNode {
926 BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
927 SDValue X, SDValue Y)
928 : SDNode(Opc, Order, dl, VTs) {
929 InitOperands(Ops, X, Y);
933 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
934 /// to allow co-allocation of node operands with the node itself.
935 class TernarySDNode : public SDNode {
938 TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
939 SDValue X, SDValue Y, SDValue Z)
940 : SDNode(Opc, Order, dl, VTs) {
941 InitOperands(Ops, X, Y, Z);
946 /// HandleSDNode - This class is used to form a handle around another node that
947 /// is persistent and is updated across invocations of replaceAllUsesWith on its
948 /// operand. This node should be directly created by end-users and not added to
949 /// the AllNodes list.
950 class HandleSDNode : public SDNode {
953 explicit HandleSDNode(SDValue X)
954 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
955 InitOperands(&Op, X);
958 const SDValue &getValue() const { return Op; }
961 class AddrSpaceCastSDNode : public UnarySDNode {
963 unsigned SrcAddrSpace;
964 unsigned DestAddrSpace;
967 AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
968 unsigned SrcAS, unsigned DestAS);
970 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
971 unsigned getDestAddressSpace() const { return DestAddrSpace; }
973 static bool classof(const SDNode *N) {
974 return N->getOpcode() == ISD::ADDRSPACECAST;
978 /// Abstact virtual class for operations for memory operations
979 class MemSDNode : public SDNode {
981 // MemoryVT - VT of in-memory value.
985 /// MMO - Memory reference information.
986 MachineMemOperand *MMO;
989 MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
990 EVT MemoryVT, MachineMemOperand *MMO);
992 MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
994 unsigned NumOps, EVT MemoryVT, MachineMemOperand *MMO);
996 bool readMem() const { return MMO->isLoad(); }
997 bool writeMem() const { return MMO->isStore(); }
999 /// Returns alignment and volatility of the memory access
1000 unsigned getOriginalAlignment() const {
1001 return MMO->getBaseAlignment();
1003 unsigned getAlignment() const {
1004 return MMO->getAlignment();
1007 /// getRawSubclassData - Return the SubclassData value, which contains an
1008 /// encoding of the volatile flag, as well as bits used by subclasses. This
1009 /// function should only be used to compute a FoldingSetNodeID value.
1010 unsigned getRawSubclassData() const {
1011 return SubclassData;
1014 // We access subclass data here so that we can check consistency
1015 // with MachineMemOperand information.
1016 bool isVolatile() const { return (SubclassData >> 5) & 1; }
1017 bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
1018 bool isInvariant() const { return (SubclassData >> 7) & 1; }
1020 AtomicOrdering getOrdering() const {
1021 return AtomicOrdering((SubclassData >> 8) & 15);
1023 SynchronizationScope getSynchScope() const {
1024 return SynchronizationScope((SubclassData >> 12) & 1);
1027 /// Returns the SrcValue and offset that describes the location of the access
1028 const Value *getSrcValue() const { return MMO->getValue(); }
1029 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1031 /// Returns the TBAAInfo that describes the dereference.
1032 const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
1034 /// Returns the Ranges that describes the dereference.
1035 const MDNode *getRanges() const { return MMO->getRanges(); }
1037 /// getMemoryVT - Return the type of the in-memory value.
1038 EVT getMemoryVT() const { return MemoryVT; }
1040 /// getMemOperand - Return a MachineMemOperand object describing the memory
1041 /// reference performed by operation.
1042 MachineMemOperand *getMemOperand() const { return MMO; }
1044 const MachinePointerInfo &getPointerInfo() const {
1045 return MMO->getPointerInfo();
1048 /// getAddressSpace - Return the address space for the associated pointer
1049 unsigned getAddressSpace() const {
1050 return getPointerInfo().getAddrSpace();
1053 /// refineAlignment - Update this MemSDNode's MachineMemOperand information
1054 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1055 /// This must only be used when the new alignment applies to all users of
1056 /// this MachineMemOperand.
1057 void refineAlignment(const MachineMemOperand *NewMMO) {
1058 MMO->refineAlignment(NewMMO);
1061 const SDValue &getChain() const { return getOperand(0); }
1062 const SDValue &getBasePtr() const {
1063 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1066 // Methods to support isa and dyn_cast
1067 static bool classof(const SDNode *N) {
1068 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1069 // with either an intrinsic or a target opcode.
1070 return N->getOpcode() == ISD::LOAD ||
1071 N->getOpcode() == ISD::STORE ||
1072 N->getOpcode() == ISD::PREFETCH ||
1073 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1074 N->getOpcode() == ISD::ATOMIC_SWAP ||
1075 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1076 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1077 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1078 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1079 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1080 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1081 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1082 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1083 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1084 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1085 N->getOpcode() == ISD::ATOMIC_LOAD ||
1086 N->getOpcode() == ISD::ATOMIC_STORE ||
1087 N->isTargetMemoryOpcode();
1091 /// AtomicSDNode - A SDNode reprenting atomic operations.
1093 class AtomicSDNode : public MemSDNode {
1096 /// For cmpxchg instructions, the ordering requirements when a store does not
1098 AtomicOrdering FailureOrdering;
1100 void InitAtomic(AtomicOrdering SuccessOrdering,
1101 AtomicOrdering FailureOrdering,
1102 SynchronizationScope SynchScope) {
1103 // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1104 assert((SuccessOrdering & 15) == SuccessOrdering &&
1105 "Ordering may not require more than 4 bits!");
1106 assert((FailureOrdering & 15) == FailureOrdering &&
1107 "Ordering may not require more than 4 bits!");
1108 assert((SynchScope & 1) == SynchScope &&
1109 "SynchScope may not require more than 1 bit!");
1110 SubclassData |= SuccessOrdering << 8;
1111 SubclassData |= SynchScope << 12;
1112 this->FailureOrdering = FailureOrdering;
1113 assert(getSuccessOrdering() == SuccessOrdering &&
1114 "Ordering encoding error!");
1115 assert(getFailureOrdering() == FailureOrdering &&
1116 "Ordering encoding error!");
1117 assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1121 // Opc: opcode for atomic
1122 // VTL: value type list
1123 // Chain: memory chain for operaand
1124 // Ptr: address to update as a SDValue
1125 // Cmp: compare value
1127 // SrcVal: address to update as a Value (used for MemOperand)
1128 // Align: alignment of memory
1129 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1130 EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
1131 MachineMemOperand *MMO, AtomicOrdering Ordering,
1132 SynchronizationScope SynchScope)
1133 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1134 InitAtomic(Ordering, Ordering, SynchScope);
1135 InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1137 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1139 SDValue Chain, SDValue Ptr,
1140 SDValue Val, MachineMemOperand *MMO,
1141 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1142 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1143 InitAtomic(Ordering, Ordering, SynchScope);
1144 InitOperands(Ops, Chain, Ptr, Val);
1146 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1148 SDValue Chain, SDValue Ptr,
1149 MachineMemOperand *MMO,
1150 AtomicOrdering Ordering, SynchronizationScope SynchScope)
1151 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1152 InitAtomic(Ordering, Ordering, SynchScope);
1153 InitOperands(Ops, Chain, Ptr);
1155 AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1156 SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
1157 MachineMemOperand *MMO,
1158 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1159 SynchronizationScope SynchScope)
1160 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1161 InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
1162 assert((DynOps || NumOps <= array_lengthof(Ops)) &&
1163 "Too many ops for internal storage!");
1164 InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
1167 const SDValue &getBasePtr() const { return getOperand(1); }
1168 const SDValue &getVal() const { return getOperand(2); }
1170 AtomicOrdering getSuccessOrdering() const {
1171 return getOrdering();
1174 // Not quite enough room in SubclassData for everything, so failure gets its
1176 AtomicOrdering getFailureOrdering() const {
1177 return FailureOrdering;
1180 bool isCompareAndSwap() const {
1181 unsigned Op = getOpcode();
1182 return Op == ISD::ATOMIC_CMP_SWAP;
1185 // Methods to support isa and dyn_cast
1186 static bool classof(const SDNode *N) {
1187 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1188 N->getOpcode() == ISD::ATOMIC_SWAP ||
1189 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1190 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1191 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1192 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1193 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1194 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1195 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1196 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1197 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1198 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1199 N->getOpcode() == ISD::ATOMIC_LOAD ||
1200 N->getOpcode() == ISD::ATOMIC_STORE;
1204 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1205 /// memory and need an associated MachineMemOperand. Its opcode may be
1206 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1207 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1208 class MemIntrinsicSDNode : public MemSDNode {
1210 MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1211 const SDValue *Ops, unsigned NumOps,
1212 EVT MemoryVT, MachineMemOperand *MMO)
1213 : MemSDNode(Opc, Order, dl, VTs, Ops, NumOps, MemoryVT, MMO) {
1216 // Methods to support isa and dyn_cast
1217 static bool classof(const SDNode *N) {
1218 // We lower some target intrinsics to their target opcode
1219 // early a node with a target opcode can be of this class
1220 return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1221 N->getOpcode() == ISD::INTRINSIC_VOID ||
1222 N->getOpcode() == ISD::PREFETCH ||
1223 N->isTargetMemoryOpcode();
1227 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1228 /// support for the llvm IR shufflevector instruction. It combines elements
1229 /// from two input vectors into a new input vector, with the selection and
1230 /// ordering of elements determined by an array of integers, referred to as
1231 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1232 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1233 /// An index of -1 is treated as undef, such that the code generator may put
1234 /// any value in the corresponding element of the result.
1235 class ShuffleVectorSDNode : public SDNode {
1238 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1239 // is freed when the SelectionDAG object is destroyed.
1242 friend class SelectionDAG;
1243 ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
1244 SDValue N2, const int *M)
1245 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
1246 InitOperands(Ops, N1, N2);
1250 ArrayRef<int> getMask() const {
1251 EVT VT = getValueType(0);
1252 return makeArrayRef(Mask, VT.getVectorNumElements());
1254 int getMaskElt(unsigned Idx) const {
1255 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1259 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1260 int getSplatIndex() const {
1261 assert(isSplat() && "Cannot get splat index for non-splat!");
1262 EVT VT = getValueType(0);
1263 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1267 llvm_unreachable("Splat with all undef indices?");
1269 static bool isSplatMask(const int *Mask, EVT VT);
1271 static bool classof(const SDNode *N) {
1272 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1276 class ConstantSDNode : public SDNode {
1277 const ConstantInt *Value;
1278 friend class SelectionDAG;
1279 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
1280 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1281 0, DebugLoc(), getSDVTList(VT)), Value(val) {
1282 SubclassData |= (uint16_t)isOpaque;
1286 const ConstantInt *getConstantIntValue() const { return Value; }
1287 const APInt &getAPIntValue() const { return Value->getValue(); }
1288 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1289 int64_t getSExtValue() const { return Value->getSExtValue(); }
1291 bool isOne() const { return Value->isOne(); }
1292 bool isNullValue() const { return Value->isNullValue(); }
1293 bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1295 bool isOpaque() const { return SubclassData & 1; }
1297 static bool classof(const SDNode *N) {
1298 return N->getOpcode() == ISD::Constant ||
1299 N->getOpcode() == ISD::TargetConstant;
1303 class ConstantFPSDNode : public SDNode {
1304 const ConstantFP *Value;
1305 friend class SelectionDAG;
1306 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1307 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1308 0, DebugLoc(), getSDVTList(VT)), Value(val) {
1312 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1313 const ConstantFP *getConstantFPValue() const { return Value; }
1315 /// isZero - Return true if the value is positive or negative zero.
1316 bool isZero() const { return Value->isZero(); }
1318 /// isNaN - Return true if the value is a NaN.
1319 bool isNaN() const { return Value->isNaN(); }
1321 /// isExactlyValue - We don't rely on operator== working on double values, as
1322 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1323 /// As such, this method can be used to do an exact bit-for-bit comparison of
1324 /// two floating point values.
1326 /// We leave the version with the double argument here because it's just so
1327 /// convenient to write "2.0" and the like. Without this function we'd
1328 /// have to duplicate its logic everywhere it's called.
1329 bool isExactlyValue(double V) const {
1332 Tmp.convert(Value->getValueAPF().getSemantics(),
1333 APFloat::rmNearestTiesToEven, &ignored);
1334 return isExactlyValue(Tmp);
1336 bool isExactlyValue(const APFloat& V) const;
1338 static bool isValueValidForType(EVT VT, const APFloat& Val);
1340 static bool classof(const SDNode *N) {
1341 return N->getOpcode() == ISD::ConstantFP ||
1342 N->getOpcode() == ISD::TargetConstantFP;
1346 class GlobalAddressSDNode : public SDNode {
1347 const GlobalValue *TheGlobal;
1349 unsigned char TargetFlags;
1350 friend class SelectionDAG;
1351 GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
1352 const GlobalValue *GA, EVT VT, int64_t o,
1353 unsigned char TargetFlags);
1356 const GlobalValue *getGlobal() const { return TheGlobal; }
1357 int64_t getOffset() const { return Offset; }
1358 unsigned char getTargetFlags() const { return TargetFlags; }
1359 // Return the address space this GlobalAddress belongs to.
1360 unsigned getAddressSpace() const;
1362 static bool classof(const SDNode *N) {
1363 return N->getOpcode() == ISD::GlobalAddress ||
1364 N->getOpcode() == ISD::TargetGlobalAddress ||
1365 N->getOpcode() == ISD::GlobalTLSAddress ||
1366 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1370 class FrameIndexSDNode : public SDNode {
1372 friend class SelectionDAG;
1373 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1374 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1375 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1379 int getIndex() const { return FI; }
1381 static bool classof(const SDNode *N) {
1382 return N->getOpcode() == ISD::FrameIndex ||
1383 N->getOpcode() == ISD::TargetFrameIndex;
1387 class JumpTableSDNode : public SDNode {
1389 unsigned char TargetFlags;
1390 friend class SelectionDAG;
1391 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1392 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1393 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1397 int getIndex() const { return JTI; }
1398 unsigned char getTargetFlags() const { return TargetFlags; }
1400 static bool classof(const SDNode *N) {
1401 return N->getOpcode() == ISD::JumpTable ||
1402 N->getOpcode() == ISD::TargetJumpTable;
1406 class ConstantPoolSDNode : public SDNode {
1408 const Constant *ConstVal;
1409 MachineConstantPoolValue *MachineCPVal;
1411 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1412 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1413 unsigned char TargetFlags;
1414 friend class SelectionDAG;
1415 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1416 unsigned Align, unsigned char TF)
1417 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1418 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1420 assert(Offset >= 0 && "Offset is too large");
1423 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1424 EVT VT, int o, unsigned Align, unsigned char TF)
1425 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1426 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1428 assert(Offset >= 0 && "Offset is too large");
1429 Val.MachineCPVal = v;
1430 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1434 bool isMachineConstantPoolEntry() const {
1438 const Constant *getConstVal() const {
1439 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1440 return Val.ConstVal;
1443 MachineConstantPoolValue *getMachineCPVal() const {
1444 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1445 return Val.MachineCPVal;
1448 int getOffset() const {
1449 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1452 // Return the alignment of this constant pool object, which is either 0 (for
1453 // default alignment) or the desired value.
1454 unsigned getAlignment() const { return Alignment; }
1455 unsigned char getTargetFlags() const { return TargetFlags; }
1457 Type *getType() const;
1459 static bool classof(const SDNode *N) {
1460 return N->getOpcode() == ISD::ConstantPool ||
1461 N->getOpcode() == ISD::TargetConstantPool;
1465 /// Completely target-dependent object reference.
1466 class TargetIndexSDNode : public SDNode {
1467 unsigned char TargetFlags;
1470 friend class SelectionDAG;
1473 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1474 : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1475 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1478 unsigned char getTargetFlags() const { return TargetFlags; }
1479 int getIndex() const { return Index; }
1480 int64_t getOffset() const { return Offset; }
1482 static bool classof(const SDNode *N) {
1483 return N->getOpcode() == ISD::TargetIndex;
1487 class BasicBlockSDNode : public SDNode {
1488 MachineBasicBlock *MBB;
1489 friend class SelectionDAG;
1490 /// Debug info is meaningful and potentially useful here, but we create
1491 /// blocks out of order when they're jumped to, which makes it a bit
1492 /// harder. Let's see if we need it first.
1493 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1494 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1498 MachineBasicBlock *getBasicBlock() const { return MBB; }
1500 static bool classof(const SDNode *N) {
1501 return N->getOpcode() == ISD::BasicBlock;
1505 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1507 class BuildVectorSDNode : public SDNode {
1508 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1509 explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1511 /// isConstantSplat - Check if this is a constant splat, and if so, find the
1512 /// smallest element size that splats the vector. If MinSplatBits is
1513 /// nonzero, the element size must be at least that large. Note that the
1514 /// splat element may be the entire vector (i.e., a one element vector).
1515 /// Returns the splat element value in SplatValue. Any undefined bits in
1516 /// that value are zero, and the corresponding bits in the SplatUndef mask
1517 /// are set. The SplatBitSize value is set to the splat element size in
1518 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1519 /// undefined. isBigEndian describes the endianness of the target.
1520 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1521 unsigned &SplatBitSize, bool &HasAnyUndefs,
1522 unsigned MinSplatBits = 0,
1523 bool isBigEndian = false) const;
1525 /// getConstantSplatValue - Check if this is a constant splat, and if so,
1526 /// return the splat value only if it is a ConstantSDNode. Otherwise
1527 /// return nullptr. This is a simpler form of isConstantSplat.
1528 /// Get the constant splat only if you care about the splat value.
1529 ConstantSDNode *getConstantSplatValue() const;
1531 bool isConstant() const;
1533 static inline bool classof(const SDNode *N) {
1534 return N->getOpcode() == ISD::BUILD_VECTOR;
1538 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1539 /// used when the SelectionDAG needs to make a simple reference to something
1540 /// in the LLVM IR representation.
1542 class SrcValueSDNode : public SDNode {
1544 friend class SelectionDAG;
1545 /// Create a SrcValue for a general value.
1546 explicit SrcValueSDNode(const Value *v)
1547 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1550 /// getValue - return the contained Value.
1551 const Value *getValue() const { return V; }
1553 static bool classof(const SDNode *N) {
1554 return N->getOpcode() == ISD::SRCVALUE;
1558 class MDNodeSDNode : public SDNode {
1560 friend class SelectionDAG;
1561 explicit MDNodeSDNode(const MDNode *md)
1562 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1566 const MDNode *getMD() const { return MD; }
1568 static bool classof(const SDNode *N) {
1569 return N->getOpcode() == ISD::MDNODE_SDNODE;
1573 class RegisterSDNode : public SDNode {
1575 friend class SelectionDAG;
1576 RegisterSDNode(unsigned reg, EVT VT)
1577 : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1581 unsigned getReg() const { return Reg; }
1583 static bool classof(const SDNode *N) {
1584 return N->getOpcode() == ISD::Register;
1588 class RegisterMaskSDNode : public SDNode {
1589 // The memory for RegMask is not owned by the node.
1590 const uint32_t *RegMask;
1591 friend class SelectionDAG;
1592 RegisterMaskSDNode(const uint32_t *mask)
1593 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1597 const uint32_t *getRegMask() const { return RegMask; }
1599 static bool classof(const SDNode *N) {
1600 return N->getOpcode() == ISD::RegisterMask;
1604 class BlockAddressSDNode : public SDNode {
1605 const BlockAddress *BA;
1607 unsigned char TargetFlags;
1608 friend class SelectionDAG;
1609 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1610 int64_t o, unsigned char Flags)
1611 : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1612 BA(ba), Offset(o), TargetFlags(Flags) {
1615 const BlockAddress *getBlockAddress() const { return BA; }
1616 int64_t getOffset() const { return Offset; }
1617 unsigned char getTargetFlags() const { return TargetFlags; }
1619 static bool classof(const SDNode *N) {
1620 return N->getOpcode() == ISD::BlockAddress ||
1621 N->getOpcode() == ISD::TargetBlockAddress;
1625 class EHLabelSDNode : public SDNode {
1628 friend class SelectionDAG;
1629 EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
1630 : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
1631 InitOperands(&Chain, ch);
1634 MCSymbol *getLabel() const { return Label; }
1636 static bool classof(const SDNode *N) {
1637 return N->getOpcode() == ISD::EH_LABEL;
1641 class ExternalSymbolSDNode : public SDNode {
1643 unsigned char TargetFlags;
1645 friend class SelectionDAG;
1646 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1647 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1648 0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1652 const char *getSymbol() const { return Symbol; }
1653 unsigned char getTargetFlags() const { return TargetFlags; }
1655 static bool classof(const SDNode *N) {
1656 return N->getOpcode() == ISD::ExternalSymbol ||
1657 N->getOpcode() == ISD::TargetExternalSymbol;
1661 class CondCodeSDNode : public SDNode {
1662 ISD::CondCode Condition;
1663 friend class SelectionDAG;
1664 explicit CondCodeSDNode(ISD::CondCode Cond)
1665 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1670 ISD::CondCode get() const { return Condition; }
1672 static bool classof(const SDNode *N) {
1673 return N->getOpcode() == ISD::CONDCODE;
1677 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1678 /// future and most targets don't support it.
1679 class CvtRndSatSDNode : public SDNode {
1680 ISD::CvtCode CvtCode;
1681 friend class SelectionDAG;
1682 explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
1683 const SDValue *Ops, unsigned NumOps,
1685 : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops, NumOps),
1687 assert(NumOps == 5 && "wrong number of operations");
1690 ISD::CvtCode getCvtCode() const { return CvtCode; }
1692 static bool classof(const SDNode *N) {
1693 return N->getOpcode() == ISD::CONVERT_RNDSAT;
1697 /// VTSDNode - This class is used to represent EVT's, which are used
1698 /// to parameterize some operations.
1699 class VTSDNode : public SDNode {
1701 friend class SelectionDAG;
1702 explicit VTSDNode(EVT VT)
1703 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1708 EVT getVT() const { return ValueType; }
1710 static bool classof(const SDNode *N) {
1711 return N->getOpcode() == ISD::VALUETYPE;
1715 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1717 class LSBaseSDNode : public MemSDNode {
1718 //! Operand array for load and store
1720 \note Moving this array to the base class captures more
1721 common functionality shared between LoadSDNode and
1726 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1727 SDValue *Operands, unsigned numOperands,
1728 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1729 MachineMemOperand *MMO)
1730 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1731 SubclassData |= AM << 2;
1732 assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1733 InitOperands(Ops, Operands, numOperands);
1734 assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1735 "Only indexed loads and stores have a non-undef offset operand");
1738 const SDValue &getOffset() const {
1739 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1742 /// getAddressingMode - Return the addressing mode for this load or store:
1743 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1744 ISD::MemIndexedMode getAddressingMode() const {
1745 return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1748 /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1749 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1751 /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1752 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1754 static bool classof(const SDNode *N) {
1755 return N->getOpcode() == ISD::LOAD ||
1756 N->getOpcode() == ISD::STORE;
1760 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1762 class LoadSDNode : public LSBaseSDNode {
1763 friend class SelectionDAG;
1764 LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1765 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1766 MachineMemOperand *MMO)
1767 : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1768 SubclassData |= (unsigned short)ETy;
1769 assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1770 assert(readMem() && "Load MachineMemOperand is not a load!");
1771 assert(!writeMem() && "Load MachineMemOperand is a store!");
1775 /// getExtensionType - Return whether this is a plain node,
1776 /// or one of the varieties of value-extending loads.
1777 ISD::LoadExtType getExtensionType() const {
1778 return ISD::LoadExtType(SubclassData & 3);
1781 const SDValue &getBasePtr() const { return getOperand(1); }
1782 const SDValue &getOffset() const { return getOperand(2); }
1784 static bool classof(const SDNode *N) {
1785 return N->getOpcode() == ISD::LOAD;
1789 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1791 class StoreSDNode : public LSBaseSDNode {
1792 friend class SelectionDAG;
1793 StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1794 SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1795 MachineMemOperand *MMO)
1796 : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1797 VTs, AM, MemVT, MMO) {
1798 SubclassData |= (unsigned short)isTrunc;
1799 assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1800 assert(!readMem() && "Store MachineMemOperand is a load!");
1801 assert(writeMem() && "Store MachineMemOperand is not a store!");
1805 /// isTruncatingStore - Return true if the op does a truncation before store.
1806 /// For integers this is the same as doing a TRUNCATE and storing the result.
1807 /// For floats, it is the same as doing an FP_ROUND and storing the result.
1808 bool isTruncatingStore() const { return SubclassData & 1; }
1810 const SDValue &getValue() const { return getOperand(1); }
1811 const SDValue &getBasePtr() const { return getOperand(2); }
1812 const SDValue &getOffset() const { return getOperand(3); }
1814 static bool classof(const SDNode *N) {
1815 return N->getOpcode() == ISD::STORE;
1819 /// MachineSDNode - An SDNode that represents everything that will be needed
1820 /// to construct a MachineInstr. These nodes are created during the
1821 /// instruction selection proper phase.
1823 class MachineSDNode : public SDNode {
1825 typedef MachineMemOperand **mmo_iterator;
1828 friend class SelectionDAG;
1829 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
1830 : SDNode(Opc, Order, DL, VTs), MemRefs(0), MemRefsEnd(0) {}
1832 /// LocalOperands - Operands for this instruction, if they fit here. If
1833 /// they don't, this field is unused.
1834 SDUse LocalOperands[4];
1836 /// MemRefs - Memory reference descriptions for this instruction.
1837 mmo_iterator MemRefs;
1838 mmo_iterator MemRefsEnd;
1841 mmo_iterator memoperands_begin() const { return MemRefs; }
1842 mmo_iterator memoperands_end() const { return MemRefsEnd; }
1843 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1845 /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1846 /// list. This does not transfer ownership.
1847 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1848 for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1849 assert(*MMI && "Null mem ref detected!");
1850 MemRefs = NewMemRefs;
1851 MemRefsEnd = NewMemRefsEnd;
1854 static bool classof(const SDNode *N) {
1855 return N->isMachineOpcode();
1859 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1860 SDNode, ptrdiff_t> {
1864 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1866 bool operator==(const SDNodeIterator& x) const {
1867 return Operand == x.Operand;
1869 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1871 const SDNodeIterator &operator=(const SDNodeIterator &I) {
1872 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1873 Operand = I.Operand;
1877 pointer operator*() const {
1878 return Node->getOperand(Operand).getNode();
1880 pointer operator->() const { return operator*(); }
1882 SDNodeIterator& operator++() { // Preincrement
1886 SDNodeIterator operator++(int) { // Postincrement
1887 SDNodeIterator tmp = *this; ++*this; return tmp;
1889 size_t operator-(SDNodeIterator Other) const {
1890 assert(Node == Other.Node &&
1891 "Cannot compare iterators of two different nodes!");
1892 return Operand - Other.Operand;
1895 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1896 static SDNodeIterator end (const SDNode *N) {
1897 return SDNodeIterator(N, N->getNumOperands());
1900 unsigned getOperand() const { return Operand; }
1901 const SDNode *getNode() const { return Node; }
1904 template <> struct GraphTraits<SDNode*> {
1905 typedef SDNode NodeType;
1906 typedef SDNodeIterator ChildIteratorType;
1907 static inline NodeType *getEntryNode(SDNode *N) { return N; }
1908 static inline ChildIteratorType child_begin(NodeType *N) {
1909 return SDNodeIterator::begin(N);
1911 static inline ChildIteratorType child_end(NodeType *N) {
1912 return SDNodeIterator::end(N);
1916 /// LargestSDNode - The largest SDNode class.
1918 typedef AtomicSDNode LargestSDNode;
1920 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1923 typedef GlobalAddressSDNode MostAlignedSDNode;
1926 /// isNormalLoad - Returns true if the specified node is a non-extending
1927 /// and unindexed load.
1928 inline bool isNormalLoad(const SDNode *N) {
1929 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1930 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1931 Ld->getAddressingMode() == ISD::UNINDEXED;
1934 /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1936 inline bool isNON_EXTLoad(const SDNode *N) {
1937 return isa<LoadSDNode>(N) &&
1938 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1941 /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1943 inline bool isEXTLoad(const SDNode *N) {
1944 return isa<LoadSDNode>(N) &&
1945 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1948 /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1950 inline bool isSEXTLoad(const SDNode *N) {
1951 return isa<LoadSDNode>(N) &&
1952 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1955 /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1957 inline bool isZEXTLoad(const SDNode *N) {
1958 return isa<LoadSDNode>(N) &&
1959 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1962 /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1964 inline bool isUNINDEXEDLoad(const SDNode *N) {
1965 return isa<LoadSDNode>(N) &&
1966 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1969 /// isNormalStore - Returns true if the specified node is a non-truncating
1970 /// and unindexed store.
1971 inline bool isNormalStore(const SDNode *N) {
1972 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1973 return St && !St->isTruncatingStore() &&
1974 St->getAddressingMode() == ISD::UNINDEXED;
1977 /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1979 inline bool isNON_TRUNCStore(const SDNode *N) {
1980 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1983 /// isTRUNCStore - Returns true if the specified node is a truncating
1985 inline bool isTRUNCStore(const SDNode *N) {
1986 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1989 /// isUNINDEXEDStore - Returns true if the specified node is an
1990 /// unindexed store.
1991 inline bool isUNINDEXEDStore(const SDNode *N) {
1992 return isa<StoreSDNode>(N) &&
1993 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1997 } // end llvm namespace