[SelectionDAG] Force cycle detection in AssignTopologicalOrder before aborting
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAGNodes.h
1 //===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 // Clients should include the SelectionDAG.h file instead of this file directly.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
21
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/GraphTraits.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/ilist_node.h"
29 #include "llvm/CodeGen/ISDOpcodes.h"
30 #include "llvm/CodeGen/MachineMemOperand.h"
31 #include "llvm/CodeGen/ValueTypes.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DebugLoc.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/Support/DataTypes.h"
36 #include "llvm/Support/MathExtras.h"
37 #include <cassert>
38
39 namespace llvm {
40
41 class SelectionDAG;
42 class GlobalValue;
43 class MachineBasicBlock;
44 class MachineConstantPoolValue;
45 class SDNode;
46 class Value;
47 class MCSymbol;
48 template <typename T> struct DenseMapInfo;
49 template <typename T> struct simplify_type;
50 template <typename T> struct ilist_traits;
51
52 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
53                     bool force = false);
54
55 /// SDVTList - This represents a list of ValueType's that has been intern'd by
56 /// a SelectionDAG.  Instances of this simple value class are returned by
57 /// SelectionDAG::getVTList(...).
58 ///
59 struct SDVTList {
60   const EVT *VTs;
61   unsigned int NumVTs;
62 };
63
64 namespace ISD {
65   /// Node predicates
66
67   /// isBuildVectorAllOnes - Return true if the specified node is a
68   /// BUILD_VECTOR where all of the elements are ~0 or undef.
69   bool isBuildVectorAllOnes(const SDNode *N);
70
71   /// isBuildVectorAllZeros - Return true if the specified node is a
72   /// BUILD_VECTOR where all of the elements are 0 or undef.
73   bool isBuildVectorAllZeros(const SDNode *N);
74
75   /// \brief Return true if the specified node is a BUILD_VECTOR node of
76   /// all ConstantSDNode or undef.
77   bool isBuildVectorOfConstantSDNodes(const SDNode *N);
78
79   /// isScalarToVector - Return true if the specified node is a
80   /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
81   /// element is not an undef.
82   bool isScalarToVector(const SDNode *N);
83
84   /// allOperandsUndef - Return true if the node has at least one operand
85   /// and all operands of the specified node are ISD::UNDEF.
86   bool allOperandsUndef(const SDNode *N);
87 }  // end llvm:ISD namespace
88
89 //===----------------------------------------------------------------------===//
90 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
91 /// values as the result of a computation.  Many nodes return multiple values,
92 /// from loads (which define a token and a return value) to ADDC (which returns
93 /// a result and a carry value), to calls (which may return an arbitrary number
94 /// of values).
95 ///
96 /// As such, each use of a SelectionDAG computation must indicate the node that
97 /// computes it as well as which return value to use from that node.  This pair
98 /// of information is represented with the SDValue value type.
99 ///
100 class SDValue {
101   SDNode *Node;       // The node defining the value we are using.
102   unsigned ResNo;     // Which return value of the node we are using.
103 public:
104   SDValue() : Node(nullptr), ResNo(0) {}
105   SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
106
107   /// get the index which selects a specific result in the SDNode
108   unsigned getResNo() const { return ResNo; }
109
110   /// get the SDNode which holds the desired result
111   SDNode *getNode() const { return Node; }
112
113   /// set the SDNode
114   void setNode(SDNode *N) { Node = N; }
115
116   inline SDNode *operator->() const { return Node; }
117
118   bool operator==(const SDValue &O) const {
119     return Node == O.Node && ResNo == O.ResNo;
120   }
121   bool operator!=(const SDValue &O) const {
122     return !operator==(O);
123   }
124   bool operator<(const SDValue &O) const {
125     return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
126   }
127
128   SDValue getValue(unsigned R) const {
129     return SDValue(Node, R);
130   }
131
132   // isOperandOf - Return true if this node is an operand of N.
133   bool isOperandOf(SDNode *N) const;
134
135   /// getValueType - Return the ValueType of the referenced return value.
136   ///
137   inline EVT getValueType() const;
138
139   /// Return the simple ValueType of the referenced return value.
140   MVT getSimpleValueType() const {
141     return getValueType().getSimpleVT();
142   }
143
144   /// getValueSizeInBits - Returns the size of the value in bits.
145   ///
146   unsigned getValueSizeInBits() const {
147     return getValueType().getSizeInBits();
148   }
149
150   unsigned getScalarValueSizeInBits() const {
151     return getValueType().getScalarType().getSizeInBits();
152   }
153
154   // Forwarding methods - These forward to the corresponding methods in SDNode.
155   inline unsigned getOpcode() const;
156   inline unsigned getNumOperands() const;
157   inline const SDValue &getOperand(unsigned i) const;
158   inline uint64_t getConstantOperandVal(unsigned i) const;
159   inline bool isTargetMemoryOpcode() const;
160   inline bool isTargetOpcode() const;
161   inline bool isMachineOpcode() const;
162   inline unsigned getMachineOpcode() const;
163   inline const DebugLoc getDebugLoc() const;
164   inline void dump() const;
165   inline void dumpr() const;
166
167   /// reachesChainWithoutSideEffects - Return true if this operand (which must
168   /// be a chain) reaches the specified operand without crossing any
169   /// side-effecting instructions.  In practice, this looks through token
170   /// factors and non-volatile loads.  In order to remain efficient, this only
171   /// looks a couple of nodes in, it does not do an exhaustive search.
172   bool reachesChainWithoutSideEffects(SDValue Dest,
173                                       unsigned Depth = 2) const;
174
175   /// use_empty - Return true if there are no nodes using value ResNo
176   /// of Node.
177   ///
178   inline bool use_empty() const;
179
180   /// hasOneUse - Return true if there is exactly one node using value
181   /// ResNo of Node.
182   ///
183   inline bool hasOneUse() const;
184 };
185
186
187 template<> struct DenseMapInfo<SDValue> {
188   static inline SDValue getEmptyKey() {
189     return SDValue((SDNode*)-1, -1U);
190   }
191   static inline SDValue getTombstoneKey() {
192     return SDValue((SDNode*)-1, 0);
193   }
194   static unsigned getHashValue(const SDValue &Val) {
195     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
196             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
197   }
198   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
199     return LHS == RHS;
200   }
201 };
202 template <> struct isPodLike<SDValue> { static const bool value = true; };
203
204
205 /// simplify_type specializations - Allow casting operators to work directly on
206 /// SDValues as if they were SDNode*'s.
207 template<> struct simplify_type<SDValue> {
208   typedef SDNode* SimpleType;
209   static SimpleType getSimplifiedValue(SDValue &Val) {
210     return Val.getNode();
211   }
212 };
213 template<> struct simplify_type<const SDValue> {
214   typedef /*const*/ SDNode* SimpleType;
215   static SimpleType getSimplifiedValue(const SDValue &Val) {
216     return Val.getNode();
217   }
218 };
219
220 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
221 /// which records the SDNode being used and the result number, a
222 /// pointer to the SDNode using the value, and Next and Prev pointers,
223 /// which link together all the uses of an SDNode.
224 ///
225 class SDUse {
226   /// Val - The value being used.
227   SDValue Val;
228   /// User - The user of this value.
229   SDNode *User;
230   /// Prev, Next - Pointers to the uses list of the SDNode referred by
231   /// this operand.
232   SDUse **Prev, *Next;
233
234   SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
235   void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
236
237 public:
238   SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
239
240   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
241   operator const SDValue&() const { return Val; }
242
243   /// If implicit conversion to SDValue doesn't work, the get() method returns
244   /// the SDValue.
245   const SDValue &get() const { return Val; }
246
247   /// getUser - This returns the SDNode that contains this Use.
248   SDNode *getUser() { return User; }
249
250   /// getNext - Get the next SDUse in the use list.
251   SDUse *getNext() const { return Next; }
252
253   /// getNode - Convenience function for get().getNode().
254   SDNode *getNode() const { return Val.getNode(); }
255   /// getResNo - Convenience function for get().getResNo().
256   unsigned getResNo() const { return Val.getResNo(); }
257   /// getValueType - Convenience function for get().getValueType().
258   EVT getValueType() const { return Val.getValueType(); }
259
260   /// operator== - Convenience function for get().operator==
261   bool operator==(const SDValue &V) const {
262     return Val == V;
263   }
264
265   /// operator!= - Convenience function for get().operator!=
266   bool operator!=(const SDValue &V) const {
267     return Val != V;
268   }
269
270   /// operator< - Convenience function for get().operator<
271   bool operator<(const SDValue &V) const {
272     return Val < V;
273   }
274
275 private:
276   friend class SelectionDAG;
277   friend class SDNode;
278
279   void setUser(SDNode *p) { User = p; }
280
281   /// set - Remove this use from its existing use list, assign it the
282   /// given value, and add it to the new value's node's use list.
283   inline void set(const SDValue &V);
284   /// setInitial - like set, but only supports initializing a newly-allocated
285   /// SDUse with a non-null value.
286   inline void setInitial(const SDValue &V);
287   /// setNode - like set, but only sets the Node portion of the value,
288   /// leaving the ResNo portion unmodified.
289   inline void setNode(SDNode *N);
290
291   void addToList(SDUse **List) {
292     Next = *List;
293     if (Next) Next->Prev = &Next;
294     Prev = List;
295     *List = this;
296   }
297
298   void removeFromList() {
299     *Prev = Next;
300     if (Next) Next->Prev = Prev;
301   }
302 };
303
304 /// simplify_type specializations - Allow casting operators to work directly on
305 /// SDValues as if they were SDNode*'s.
306 template<> struct simplify_type<SDUse> {
307   typedef SDNode* SimpleType;
308   static SimpleType getSimplifiedValue(SDUse &Val) {
309     return Val.getNode();
310   }
311 };
312
313
314 /// SDNode - Represents one node in the SelectionDAG.
315 ///
316 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
317 private:
318   /// NodeType - The operation that this node performs.
319   ///
320   int16_t NodeType;
321
322   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
323   /// then they will be delete[]'d when the node is destroyed.
324   uint16_t OperandsNeedDelete : 1;
325
326   /// HasDebugValue - This tracks whether this node has one or more dbg_value
327   /// nodes corresponding to it.
328   uint16_t HasDebugValue : 1;
329
330 protected:
331   /// SubclassData - This member is defined by this class, but is not used for
332   /// anything.  Subclasses can use it to hold whatever state they find useful.
333   /// This field is initialized to zero by the ctor.
334   uint16_t SubclassData : 14;
335
336 private:
337   /// NodeId - Unique id per SDNode in the DAG.
338   int NodeId;
339
340   /// OperandList - The values that are used by this operation.
341   ///
342   SDUse *OperandList;
343
344   /// ValueList - The types of the values this node defines.  SDNode's may
345   /// define multiple values simultaneously.
346   const EVT *ValueList;
347
348   /// UseList - List of uses for this SDNode.
349   SDUse *UseList;
350
351   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
352   unsigned short NumOperands, NumValues;
353
354   /// debugLoc - source line information.
355   DebugLoc debugLoc;
356
357   // The ordering of the SDNodes. It roughly corresponds to the ordering of the
358   // original LLVM instructions.
359   // This is used for turning off scheduling, because we'll forgo
360   // the normal scheduling algorithms and output the instructions according to
361   // this ordering.
362   unsigned IROrder;
363
364   /// getValueTypeList - Return a pointer to the specified value type.
365   static const EVT *getValueTypeList(EVT VT);
366
367   friend class SelectionDAG;
368   friend struct ilist_traits<SDNode>;
369
370 public:
371   //===--------------------------------------------------------------------===//
372   //  Accessors
373   //
374
375   /// getOpcode - Return the SelectionDAG opcode value for this node. For
376   /// pre-isel nodes (those for which isMachineOpcode returns false), these
377   /// are the opcode values in the ISD and <target>ISD namespaces. For
378   /// post-isel opcodes, see getMachineOpcode.
379   unsigned getOpcode()  const { return (unsigned short)NodeType; }
380
381   /// isTargetOpcode - Test if this node has a target-specific opcode (in the
382   /// \<target\>ISD namespace).
383   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
384
385   /// isTargetMemoryOpcode - Test if this node has a target-specific
386   /// memory-referencing opcode (in the \<target\>ISD namespace and
387   /// greater than FIRST_TARGET_MEMORY_OPCODE).
388   bool isTargetMemoryOpcode() const {
389     return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
390   }
391
392   /// isMachineOpcode - Test if this node has a post-isel opcode, directly
393   /// corresponding to a MachineInstr opcode.
394   bool isMachineOpcode() const { return NodeType < 0; }
395
396   /// getMachineOpcode - This may only be called if isMachineOpcode returns
397   /// true. It returns the MachineInstr opcode value that the node's opcode
398   /// corresponds to.
399   unsigned getMachineOpcode() const {
400     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
401     return ~NodeType;
402   }
403
404   /// getHasDebugValue - get this bit.
405   bool getHasDebugValue() const { return HasDebugValue; }
406
407   /// setHasDebugValue - set this bit.
408   void setHasDebugValue(bool b) { HasDebugValue = b; }
409
410   /// use_empty - Return true if there are no uses of this node.
411   ///
412   bool use_empty() const { return UseList == nullptr; }
413
414   /// hasOneUse - Return true if there is exactly one use of this node.
415   ///
416   bool hasOneUse() const {
417     return !use_empty() && std::next(use_begin()) == use_end();
418   }
419
420   /// use_size - Return the number of uses of this node. This method takes
421   /// time proportional to the number of uses.
422   ///
423   size_t use_size() const { return std::distance(use_begin(), use_end()); }
424
425   /// getNodeId - Return the unique node id.
426   ///
427   int getNodeId() const { return NodeId; }
428
429   /// setNodeId - Set unique node id.
430   void setNodeId(int Id) { NodeId = Id; }
431
432   /// getIROrder - Return the node ordering.
433   ///
434   unsigned getIROrder() const { return IROrder; }
435
436   /// setIROrder - Set the node ordering.
437   ///
438   void setIROrder(unsigned Order) { IROrder = Order; }
439
440   /// getDebugLoc - Return the source location info.
441   const DebugLoc getDebugLoc() const { return debugLoc; }
442
443   /// setDebugLoc - Set source location info.  Try to avoid this, putting
444   /// it in the constructor is preferable.
445   void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
446
447   /// use_iterator - This class provides iterator support for SDUse
448   /// operands that use a specific SDNode.
449   class use_iterator
450     : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
451     SDUse *Op;
452     explicit use_iterator(SDUse *op) : Op(op) {
453     }
454     friend class SDNode;
455   public:
456     typedef std::iterator<std::forward_iterator_tag,
457                           SDUse, ptrdiff_t>::reference reference;
458     typedef std::iterator<std::forward_iterator_tag,
459                           SDUse, ptrdiff_t>::pointer pointer;
460
461     use_iterator(const use_iterator &I) : Op(I.Op) {}
462     use_iterator() : Op(nullptr) {}
463
464     bool operator==(const use_iterator &x) const {
465       return Op == x.Op;
466     }
467     bool operator!=(const use_iterator &x) const {
468       return !operator==(x);
469     }
470
471     /// atEnd - return true if this iterator is at the end of uses list.
472     bool atEnd() const { return Op == nullptr; }
473
474     // Iterator traversal: forward iteration only.
475     use_iterator &operator++() {          // Preincrement
476       assert(Op && "Cannot increment end iterator!");
477       Op = Op->getNext();
478       return *this;
479     }
480
481     use_iterator operator++(int) {        // Postincrement
482       use_iterator tmp = *this; ++*this; return tmp;
483     }
484
485     /// Retrieve a pointer to the current user node.
486     SDNode *operator*() const {
487       assert(Op && "Cannot dereference end iterator!");
488       return Op->getUser();
489     }
490
491     SDNode *operator->() const { return operator*(); }
492
493     SDUse &getUse() const { return *Op; }
494
495     /// getOperandNo - Retrieve the operand # of this use in its user.
496     ///
497     unsigned getOperandNo() const {
498       assert(Op && "Cannot dereference end iterator!");
499       return (unsigned)(Op - Op->getUser()->OperandList);
500     }
501   };
502
503   /// use_begin/use_end - Provide iteration support to walk over all uses
504   /// of an SDNode.
505
506   use_iterator use_begin() const {
507     return use_iterator(UseList);
508   }
509
510   static use_iterator use_end() { return use_iterator(nullptr); }
511
512   inline iterator_range<use_iterator> uses() {
513     return iterator_range<use_iterator>(use_begin(), use_end());
514   }
515   inline iterator_range<use_iterator> uses() const {
516     return iterator_range<use_iterator>(use_begin(), use_end());
517   }
518
519   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
520   /// indicated value.  This method ignores uses of other values defined by this
521   /// operation.
522   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
523
524   /// hasAnyUseOfValue - Return true if there are any use of the indicated
525   /// value. This method ignores uses of other values defined by this operation.
526   bool hasAnyUseOfValue(unsigned Value) const;
527
528   /// isOnlyUserOf - Return true if this node is the only use of N.
529   ///
530   bool isOnlyUserOf(SDNode *N) const;
531
532   /// isOperandOf - Return true if this node is an operand of N.
533   ///
534   bool isOperandOf(SDNode *N) const;
535
536   /// isPredecessorOf - Return true if this node is a predecessor of N.
537   /// NOTE: Implemented on top of hasPredecessor and every bit as
538   /// expensive. Use carefully.
539   bool isPredecessorOf(const SDNode *N) const {
540     return N->hasPredecessor(this);
541   }
542
543   /// hasPredecessor - Return true if N is a predecessor of this node.
544   /// N is either an operand of this node, or can be reached by recursively
545   /// traversing up the operands.
546   /// NOTE: This is an expensive method. Use it carefully.
547   bool hasPredecessor(const SDNode *N) const;
548
549   /// hasPredecesorHelper - Return true if N is a predecessor of this node.
550   /// N is either an operand of this node, or can be reached by recursively
551   /// traversing up the operands.
552   /// In this helper the Visited and worklist sets are held externally to
553   /// cache predecessors over multiple invocations. If you want to test for
554   /// multiple predecessors this method is preferable to multiple calls to
555   /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
556   /// changes.
557   /// NOTE: This is still very expensive. Use carefully.
558   bool hasPredecessorHelper(const SDNode *N,
559                             SmallPtrSet<const SDNode *, 32> &Visited,
560                             SmallVectorImpl<const SDNode *> &Worklist) const;
561
562   /// getNumOperands - Return the number of values used by this operation.
563   ///
564   unsigned getNumOperands() const { return NumOperands; }
565
566   /// getConstantOperandVal - Helper method returns the integer value of a
567   /// ConstantSDNode operand.
568   uint64_t getConstantOperandVal(unsigned Num) const;
569
570   const SDValue &getOperand(unsigned Num) const {
571     assert(Num < NumOperands && "Invalid child # of SDNode!");
572     return OperandList[Num];
573   }
574
575   typedef SDUse* op_iterator;
576   op_iterator op_begin() const { return OperandList; }
577   op_iterator op_end() const { return OperandList+NumOperands; }
578
579   SDVTList getVTList() const {
580     SDVTList X = { ValueList, NumValues };
581     return X;
582   }
583
584   /// getGluedNode - If this node has a glue operand, return the node
585   /// to which the glue operand points. Otherwise return NULL.
586   SDNode *getGluedNode() const {
587     if (getNumOperands() != 0 &&
588       getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
589       return getOperand(getNumOperands()-1).getNode();
590     return nullptr;
591   }
592
593   // If this is a pseudo op, like copyfromreg, look to see if there is a
594   // real target node glued to it.  If so, return the target node.
595   const SDNode *getGluedMachineNode() const {
596     const SDNode *FoundNode = this;
597
598     // Climb up glue edges until a machine-opcode node is found, or the
599     // end of the chain is reached.
600     while (!FoundNode->isMachineOpcode()) {
601       const SDNode *N = FoundNode->getGluedNode();
602       if (!N) break;
603       FoundNode = N;
604     }
605
606     return FoundNode;
607   }
608
609   /// getGluedUser - If this node has a glue value with a user, return
610   /// the user (there is at most one). Otherwise return NULL.
611   SDNode *getGluedUser() const {
612     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
613       if (UI.getUse().get().getValueType() == MVT::Glue)
614         return *UI;
615     return nullptr;
616   }
617
618   /// getNumValues - Return the number of values defined/returned by this
619   /// operator.
620   ///
621   unsigned getNumValues() const { return NumValues; }
622
623   /// getValueType - Return the type of a specified result.
624   ///
625   EVT getValueType(unsigned ResNo) const {
626     assert(ResNo < NumValues && "Illegal result number!");
627     return ValueList[ResNo];
628   }
629
630   /// Return the type of a specified result as a simple type.
631   ///
632   MVT getSimpleValueType(unsigned ResNo) const {
633     return getValueType(ResNo).getSimpleVT();
634   }
635
636   /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
637   ///
638   unsigned getValueSizeInBits(unsigned ResNo) const {
639     return getValueType(ResNo).getSizeInBits();
640   }
641
642   typedef const EVT* value_iterator;
643   value_iterator value_begin() const { return ValueList; }
644   value_iterator value_end() const { return ValueList+NumValues; }
645
646   /// getOperationName - Return the opcode of this operation for printing.
647   ///
648   std::string getOperationName(const SelectionDAG *G = nullptr) const;
649   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
650   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
651   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
652   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
653   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
654
655   /// printrFull - Print a SelectionDAG node and all children down to
656   /// the leaves.  The given SelectionDAG allows target-specific nodes
657   /// to be printed in human-readable form.  Unlike printr, this will
658   /// print the whole DAG, including children that appear multiple
659   /// times.
660   ///
661   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
662
663   /// printrWithDepth - Print a SelectionDAG node and children up to
664   /// depth "depth."  The given SelectionDAG allows target-specific
665   /// nodes to be printed in human-readable form.  Unlike printr, this
666   /// will print children that appear multiple times wherever they are
667   /// used.
668   ///
669   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
670                        unsigned depth = 100) const;
671
672
673   /// dump - Dump this node, for debugging.
674   void dump() const;
675
676   /// dumpr - Dump (recursively) this node and its use-def subgraph.
677   void dumpr() const;
678
679   /// dump - Dump this node, for debugging.
680   /// The given SelectionDAG allows target-specific nodes to be printed
681   /// in human-readable form.
682   void dump(const SelectionDAG *G) const;
683
684   /// dumpr - Dump (recursively) this node and its use-def subgraph.
685   /// The given SelectionDAG allows target-specific nodes to be printed
686   /// in human-readable form.
687   void dumpr(const SelectionDAG *G) const;
688
689   /// dumprFull - printrFull to dbgs().  The given SelectionDAG allows
690   /// target-specific nodes to be printed in human-readable form.
691   /// Unlike dumpr, this will print the whole DAG, including children
692   /// that appear multiple times.
693   ///
694   void dumprFull(const SelectionDAG *G = nullptr) const;
695
696   /// dumprWithDepth - printrWithDepth to dbgs().  The given
697   /// SelectionDAG allows target-specific nodes to be printed in
698   /// human-readable form.  Unlike dumpr, this will print children
699   /// that appear multiple times wherever they are used.
700   ///
701   void dumprWithDepth(const SelectionDAG *G = nullptr,
702                       unsigned depth = 100) const;
703
704   /// Profile - Gather unique data for the node.
705   ///
706   void Profile(FoldingSetNodeID &ID) const;
707
708   /// addUse - This method should only be used by the SDUse class.
709   ///
710   void addUse(SDUse &U) { U.addToList(&UseList); }
711
712 protected:
713   static SDVTList getSDVTList(EVT VT) {
714     SDVTList Ret = { getValueTypeList(VT), 1 };
715     return Ret;
716   }
717
718   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs,
719          ArrayRef<SDValue> Ops)
720     : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
721       SubclassData(0), NodeId(-1),
722       OperandList(Ops.size() ? new SDUse[Ops.size()] : nullptr),
723       ValueList(VTs.VTs), UseList(nullptr),
724       NumOperands(Ops.size()), NumValues(VTs.NumVTs),
725       debugLoc(dl), IROrder(Order) {
726     for (unsigned i = 0; i != Ops.size(); ++i) {
727       OperandList[i].setUser(this);
728       OperandList[i].setInitial(Ops[i]);
729     }
730     checkForCycles(this);
731   }
732
733   /// This constructor adds no operands itself; operands can be
734   /// set later with InitOperands.
735   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs)
736     : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
737       SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
738       UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs), debugLoc(dl),
739       IROrder(Order) {}
740
741   /// InitOperands - Initialize the operands list of this with 1 operand.
742   void InitOperands(SDUse *Ops, const SDValue &Op0) {
743     Ops[0].setUser(this);
744     Ops[0].setInitial(Op0);
745     NumOperands = 1;
746     OperandList = Ops;
747     checkForCycles(this);
748   }
749
750   /// InitOperands - Initialize the operands list of this with 2 operands.
751   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
752     Ops[0].setUser(this);
753     Ops[0].setInitial(Op0);
754     Ops[1].setUser(this);
755     Ops[1].setInitial(Op1);
756     NumOperands = 2;
757     OperandList = Ops;
758     checkForCycles(this);
759   }
760
761   /// InitOperands - Initialize the operands list of this with 3 operands.
762   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
763                     const SDValue &Op2) {
764     Ops[0].setUser(this);
765     Ops[0].setInitial(Op0);
766     Ops[1].setUser(this);
767     Ops[1].setInitial(Op1);
768     Ops[2].setUser(this);
769     Ops[2].setInitial(Op2);
770     NumOperands = 3;
771     OperandList = Ops;
772     checkForCycles(this);
773   }
774
775   /// InitOperands - Initialize the operands list of this with 4 operands.
776   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
777                     const SDValue &Op2, const SDValue &Op3) {
778     Ops[0].setUser(this);
779     Ops[0].setInitial(Op0);
780     Ops[1].setUser(this);
781     Ops[1].setInitial(Op1);
782     Ops[2].setUser(this);
783     Ops[2].setInitial(Op2);
784     Ops[3].setUser(this);
785     Ops[3].setInitial(Op3);
786     NumOperands = 4;
787     OperandList = Ops;
788     checkForCycles(this);
789   }
790
791   /// InitOperands - Initialize the operands list of this with N operands.
792   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
793     for (unsigned i = 0; i != N; ++i) {
794       Ops[i].setUser(this);
795       Ops[i].setInitial(Vals[i]);
796     }
797     NumOperands = N;
798     OperandList = Ops;
799     checkForCycles(this);
800   }
801
802   /// DropOperands - Release the operands and set this node to have
803   /// zero operands.
804   void DropOperands();
805 };
806
807 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
808 /// into SDNode creation functions.
809 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
810 /// from the original Instruction, and IROrder is the ordinal position of
811 /// the instruction.
812 /// When an SDNode is created after the DAG is being built, both DebugLoc and
813 /// the IROrder are propagated from the original SDNode.
814 /// So SDLoc class provides two constructors besides the default one, one to
815 /// be used by the DAGBuilder, the other to be used by others.
816 class SDLoc {
817 private:
818   // Ptr could be used for either Instruction* or SDNode*. It is used for
819   // Instruction* if IROrder is not -1.
820   const void *Ptr;
821   int IROrder;
822
823 public:
824   SDLoc() : Ptr(nullptr), IROrder(0) {}
825   SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
826     assert(N && "null SDNode");
827   }
828   SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
829     assert(Ptr && "null SDNode");
830   }
831   SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
832     assert(Order >= 0 && "bad IROrder");
833   }
834   unsigned getIROrder() {
835     if (IROrder >= 0 || Ptr == nullptr) {
836       return (unsigned)IROrder;
837     }
838     const SDNode *N = (const SDNode*)(Ptr);
839     return N->getIROrder();
840   }
841   DebugLoc getDebugLoc() {
842     if (!Ptr) {
843       return DebugLoc();
844     }
845     if (IROrder >= 0) {
846       const Instruction *I = (const Instruction*)(Ptr);
847       return I->getDebugLoc();
848     }
849     const SDNode *N = (const SDNode*)(Ptr);
850     return N->getDebugLoc();
851   }
852 };
853
854
855 // Define inline functions from the SDValue class.
856
857 inline unsigned SDValue::getOpcode() const {
858   return Node->getOpcode();
859 }
860 inline EVT SDValue::getValueType() const {
861   return Node->getValueType(ResNo);
862 }
863 inline unsigned SDValue::getNumOperands() const {
864   return Node->getNumOperands();
865 }
866 inline const SDValue &SDValue::getOperand(unsigned i) const {
867   return Node->getOperand(i);
868 }
869 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
870   return Node->getConstantOperandVal(i);
871 }
872 inline bool SDValue::isTargetOpcode() const {
873   return Node->isTargetOpcode();
874 }
875 inline bool SDValue::isTargetMemoryOpcode() const {
876   return Node->isTargetMemoryOpcode();
877 }
878 inline bool SDValue::isMachineOpcode() const {
879   return Node->isMachineOpcode();
880 }
881 inline unsigned SDValue::getMachineOpcode() const {
882   return Node->getMachineOpcode();
883 }
884 inline bool SDValue::use_empty() const {
885   return !Node->hasAnyUseOfValue(ResNo);
886 }
887 inline bool SDValue::hasOneUse() const {
888   return Node->hasNUsesOfValue(1, ResNo);
889 }
890 inline const DebugLoc SDValue::getDebugLoc() const {
891   return Node->getDebugLoc();
892 }
893 inline void SDValue::dump() const {
894   return Node->dump();
895 }
896 inline void SDValue::dumpr() const {
897   return Node->dumpr();
898 }
899 // Define inline functions from the SDUse class.
900
901 inline void SDUse::set(const SDValue &V) {
902   if (Val.getNode()) removeFromList();
903   Val = V;
904   if (V.getNode()) V.getNode()->addUse(*this);
905 }
906
907 inline void SDUse::setInitial(const SDValue &V) {
908   Val = V;
909   V.getNode()->addUse(*this);
910 }
911
912 inline void SDUse::setNode(SDNode *N) {
913   if (Val.getNode()) removeFromList();
914   Val.setNode(N);
915   if (N) N->addUse(*this);
916 }
917
918 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
919 /// to allow co-allocation of node operands with the node itself.
920 class UnarySDNode : public SDNode {
921   SDUse Op;
922 public:
923   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
924               SDValue X)
925     : SDNode(Opc, Order, dl, VTs) {
926     InitOperands(&Op, X);
927   }
928 };
929
930 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
931 /// to allow co-allocation of node operands with the node itself.
932 class BinarySDNode : public SDNode {
933   SDUse Ops[2];
934 public:
935   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
936                SDValue X, SDValue Y)
937     : SDNode(Opc, Order, dl, VTs) {
938     InitOperands(Ops, X, Y);
939   }
940 };
941
942 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
943 /// to allow co-allocation of node operands with the node itself.
944 class TernarySDNode : public SDNode {
945   SDUse Ops[3];
946 public:
947   TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
948                 SDValue X, SDValue Y, SDValue Z)
949     : SDNode(Opc, Order, dl, VTs) {
950     InitOperands(Ops, X, Y, Z);
951   }
952 };
953
954
955 /// HandleSDNode - This class is used to form a handle around another node that
956 /// is persistent and is updated across invocations of replaceAllUsesWith on its
957 /// operand.  This node should be directly created by end-users and not added to
958 /// the AllNodes list.
959 class HandleSDNode : public SDNode {
960   SDUse Op;
961 public:
962   explicit HandleSDNode(SDValue X)
963     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
964     InitOperands(&Op, X);
965   }
966   ~HandleSDNode();
967   const SDValue &getValue() const { return Op; }
968 };
969
970 class AddrSpaceCastSDNode : public UnarySDNode {
971 private:
972   unsigned SrcAddrSpace;
973   unsigned DestAddrSpace;
974
975 public:
976   AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
977                       unsigned SrcAS, unsigned DestAS);
978
979   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
980   unsigned getDestAddressSpace() const { return DestAddrSpace; }
981
982   static bool classof(const SDNode *N) {
983     return N->getOpcode() == ISD::ADDRSPACECAST;
984   }
985 };
986
987 /// Abstact virtual class for operations for memory operations
988 class MemSDNode : public SDNode {
989 private:
990   // MemoryVT - VT of in-memory value.
991   EVT MemoryVT;
992
993 protected:
994   /// MMO - Memory reference information.
995   MachineMemOperand *MMO;
996
997 public:
998   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
999             EVT MemoryVT, MachineMemOperand *MMO);
1000
1001   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1002             ArrayRef<SDValue> Ops, EVT MemoryVT, MachineMemOperand *MMO);
1003
1004   bool readMem() const { return MMO->isLoad(); }
1005   bool writeMem() const { return MMO->isStore(); }
1006
1007   /// Returns alignment and volatility of the memory access
1008   unsigned getOriginalAlignment() const {
1009     return MMO->getBaseAlignment();
1010   }
1011   unsigned getAlignment() const {
1012     return MMO->getAlignment();
1013   }
1014
1015   /// getRawSubclassData - Return the SubclassData value, which contains an
1016   /// encoding of the volatile flag, as well as bits used by subclasses. This
1017   /// function should only be used to compute a FoldingSetNodeID value.
1018   unsigned getRawSubclassData() const {
1019     return SubclassData;
1020   }
1021
1022   // We access subclass data here so that we can check consistency
1023   // with MachineMemOperand information.
1024   bool isVolatile() const { return (SubclassData >> 5) & 1; }
1025   bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
1026   bool isInvariant() const { return (SubclassData >> 7) & 1; }
1027
1028   AtomicOrdering getOrdering() const {
1029     return AtomicOrdering((SubclassData >> 8) & 15);
1030   }
1031   SynchronizationScope getSynchScope() const {
1032     return SynchronizationScope((SubclassData >> 12) & 1);
1033   }
1034
1035   // Returns the offset from the location of the access.
1036   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1037
1038   /// Returns the TBAAInfo that describes the dereference.
1039   const MDNode *getTBAAInfo() const { return MMO->getTBAAInfo(); }
1040
1041   /// Returns the Ranges that describes the dereference.
1042   const MDNode *getRanges() const { return MMO->getRanges(); }
1043
1044   /// getMemoryVT - Return the type of the in-memory value.
1045   EVT getMemoryVT() const { return MemoryVT; }
1046
1047   /// getMemOperand - Return a MachineMemOperand object describing the memory
1048   /// reference performed by operation.
1049   MachineMemOperand *getMemOperand() const { return MMO; }
1050
1051   const MachinePointerInfo &getPointerInfo() const {
1052     return MMO->getPointerInfo();
1053   }
1054
1055   /// getAddressSpace - Return the address space for the associated pointer
1056   unsigned getAddressSpace() const {
1057     return getPointerInfo().getAddrSpace();
1058   }
1059
1060   /// refineAlignment - Update this MemSDNode's MachineMemOperand information
1061   /// to reflect the alignment of NewMMO, if it has a greater alignment.
1062   /// This must only be used when the new alignment applies to all users of
1063   /// this MachineMemOperand.
1064   void refineAlignment(const MachineMemOperand *NewMMO) {
1065     MMO->refineAlignment(NewMMO);
1066   }
1067
1068   const SDValue &getChain() const { return getOperand(0); }
1069   const SDValue &getBasePtr() const {
1070     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1071   }
1072
1073   // Methods to support isa and dyn_cast
1074   static bool classof(const SDNode *N) {
1075     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1076     // with either an intrinsic or a target opcode.
1077     return N->getOpcode() == ISD::LOAD                ||
1078            N->getOpcode() == ISD::STORE               ||
1079            N->getOpcode() == ISD::PREFETCH            ||
1080            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1081            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1082            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1083            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1084            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1085            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1086            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1087            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1088            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1089            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1090            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1091            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1092            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1093            N->getOpcode() == ISD::ATOMIC_STORE        ||
1094            N->isTargetMemoryOpcode();
1095   }
1096 };
1097
1098 /// AtomicSDNode - A SDNode reprenting atomic operations.
1099 ///
1100 class AtomicSDNode : public MemSDNode {
1101   SDUse Ops[4];
1102
1103   /// For cmpxchg instructions, the ordering requirements when a store does not
1104   /// occur.
1105   AtomicOrdering FailureOrdering;
1106
1107   void InitAtomic(AtomicOrdering SuccessOrdering,
1108                   AtomicOrdering FailureOrdering,
1109                   SynchronizationScope SynchScope) {
1110     // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
1111     assert((SuccessOrdering & 15) == SuccessOrdering &&
1112            "Ordering may not require more than 4 bits!");
1113     assert((FailureOrdering & 15) == FailureOrdering &&
1114            "Ordering may not require more than 4 bits!");
1115     assert((SynchScope & 1) == SynchScope &&
1116            "SynchScope may not require more than 1 bit!");
1117     SubclassData |= SuccessOrdering << 8;
1118     SubclassData |= SynchScope << 12;
1119     this->FailureOrdering = FailureOrdering;
1120     assert(getSuccessOrdering() == SuccessOrdering &&
1121            "Ordering encoding error!");
1122     assert(getFailureOrdering() == FailureOrdering &&
1123            "Ordering encoding error!");
1124     assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
1125   }
1126
1127 public:
1128   // Opc:   opcode for atomic
1129   // VTL:    value type list
1130   // Chain:  memory chain for operaand
1131   // Ptr:    address to update as a SDValue
1132   // Cmp:    compare value
1133   // Swp:    swap value
1134   // SrcVal: address to update as a Value (used for MemOperand)
1135   // Align:  alignment of memory
1136   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1137                EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
1138                MachineMemOperand *MMO, AtomicOrdering Ordering,
1139                SynchronizationScope SynchScope)
1140       : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1141     InitAtomic(Ordering, Ordering, SynchScope);
1142     InitOperands(Ops, Chain, Ptr, Cmp, Swp);
1143   }
1144   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1145                EVT MemVT,
1146                SDValue Chain, SDValue Ptr,
1147                SDValue Val, MachineMemOperand *MMO,
1148                AtomicOrdering Ordering, SynchronizationScope SynchScope)
1149     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1150     InitAtomic(Ordering, Ordering, SynchScope);
1151     InitOperands(Ops, Chain, Ptr, Val);
1152   }
1153   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
1154                EVT MemVT,
1155                SDValue Chain, SDValue Ptr,
1156                MachineMemOperand *MMO,
1157                AtomicOrdering Ordering, SynchronizationScope SynchScope)
1158     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1159     InitAtomic(Ordering, Ordering, SynchScope);
1160     InitOperands(Ops, Chain, Ptr);
1161   }
1162   AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
1163                const SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
1164                MachineMemOperand *MMO,
1165                AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1166                SynchronizationScope SynchScope)
1167     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1168     InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
1169     assert((DynOps || NumOps <= array_lengthof(Ops)) &&
1170            "Too many ops for internal storage!");
1171     InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
1172   }
1173
1174   const SDValue &getBasePtr() const { return getOperand(1); }
1175   const SDValue &getVal() const { return getOperand(2); }
1176
1177   AtomicOrdering getSuccessOrdering() const {
1178     return getOrdering();
1179   }
1180
1181   // Not quite enough room in SubclassData for everything, so failure gets its
1182   // own field.
1183   AtomicOrdering getFailureOrdering() const {
1184     return FailureOrdering;
1185   }
1186
1187   bool isCompareAndSwap() const {
1188     unsigned Op = getOpcode();
1189     return Op == ISD::ATOMIC_CMP_SWAP;
1190   }
1191
1192   // Methods to support isa and dyn_cast
1193   static bool classof(const SDNode *N) {
1194     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1195            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1196            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1197            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1198            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1199            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1200            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1201            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1202            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1203            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1204            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1205            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1206            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1207            N->getOpcode() == ISD::ATOMIC_STORE;
1208   }
1209 };
1210
1211 /// MemIntrinsicSDNode - This SDNode is used for target intrinsics that touch
1212 /// memory and need an associated MachineMemOperand. Its opcode may be
1213 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1214 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1215 class MemIntrinsicSDNode : public MemSDNode {
1216 public:
1217   MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1218                      ArrayRef<SDValue> Ops, EVT MemoryVT,
1219                      MachineMemOperand *MMO)
1220     : MemSDNode(Opc, Order, dl, VTs, Ops, MemoryVT, MMO) {
1221   }
1222
1223   // Methods to support isa and dyn_cast
1224   static bool classof(const SDNode *N) {
1225     // We lower some target intrinsics to their target opcode
1226     // early a node with a target opcode can be of this class
1227     return N->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1228            N->getOpcode() == ISD::INTRINSIC_VOID ||
1229            N->getOpcode() == ISD::PREFETCH ||
1230            N->isTargetMemoryOpcode();
1231   }
1232 };
1233
1234 /// ShuffleVectorSDNode - This SDNode is used to implement the code generator
1235 /// support for the llvm IR shufflevector instruction.  It combines elements
1236 /// from two input vectors into a new input vector, with the selection and
1237 /// ordering of elements determined by an array of integers, referred to as
1238 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
1239 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1240 /// An index of -1 is treated as undef, such that the code generator may put
1241 /// any value in the corresponding element of the result.
1242 class ShuffleVectorSDNode : public SDNode {
1243   SDUse Ops[2];
1244
1245   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1246   // is freed when the SelectionDAG object is destroyed.
1247   const int *Mask;
1248 protected:
1249   friend class SelectionDAG;
1250   ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
1251                       SDValue N2, const int *M)
1252     : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
1253     InitOperands(Ops, N1, N2);
1254   }
1255 public:
1256
1257   ArrayRef<int> getMask() const {
1258     EVT VT = getValueType(0);
1259     return makeArrayRef(Mask, VT.getVectorNumElements());
1260   }
1261   int getMaskElt(unsigned Idx) const {
1262     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1263     return Mask[Idx];
1264   }
1265
1266   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1267   int  getSplatIndex() const {
1268     assert(isSplat() && "Cannot get splat index for non-splat!");
1269     EVT VT = getValueType(0);
1270     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1271       if (Mask[i] >= 0)
1272         return Mask[i];
1273     }
1274     llvm_unreachable("Splat with all undef indices?");
1275   }
1276   static bool isSplatMask(const int *Mask, EVT VT);
1277
1278   static bool classof(const SDNode *N) {
1279     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1280   }
1281 };
1282
1283 class ConstantSDNode : public SDNode {
1284   const ConstantInt *Value;
1285   friend class SelectionDAG;
1286   ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
1287     : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
1288              0, DebugLoc(), getSDVTList(VT)), Value(val) {
1289     SubclassData |= (uint16_t)isOpaque;
1290   }
1291 public:
1292
1293   const ConstantInt *getConstantIntValue() const { return Value; }
1294   const APInt &getAPIntValue() const { return Value->getValue(); }
1295   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1296   int64_t getSExtValue() const { return Value->getSExtValue(); }
1297
1298   bool isOne() const { return Value->isOne(); }
1299   bool isNullValue() const { return Value->isNullValue(); }
1300   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1301
1302   bool isOpaque() const { return SubclassData & 1; }
1303
1304   static bool classof(const SDNode *N) {
1305     return N->getOpcode() == ISD::Constant ||
1306            N->getOpcode() == ISD::TargetConstant;
1307   }
1308 };
1309
1310 class ConstantFPSDNode : public SDNode {
1311   const ConstantFP *Value;
1312   friend class SelectionDAG;
1313   ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1314     : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
1315              0, DebugLoc(), getSDVTList(VT)), Value(val) {
1316   }
1317 public:
1318
1319   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1320   const ConstantFP *getConstantFPValue() const { return Value; }
1321
1322   /// isZero - Return true if the value is positive or negative zero.
1323   bool isZero() const { return Value->isZero(); }
1324
1325   /// isNaN - Return true if the value is a NaN.
1326   bool isNaN() const { return Value->isNaN(); }
1327
1328   /// isExactlyValue - We don't rely on operator== working on double values, as
1329   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1330   /// As such, this method can be used to do an exact bit-for-bit comparison of
1331   /// two floating point values.
1332
1333   /// We leave the version with the double argument here because it's just so
1334   /// convenient to write "2.0" and the like.  Without this function we'd
1335   /// have to duplicate its logic everywhere it's called.
1336   bool isExactlyValue(double V) const {
1337     bool ignored;
1338     APFloat Tmp(V);
1339     Tmp.convert(Value->getValueAPF().getSemantics(),
1340                 APFloat::rmNearestTiesToEven, &ignored);
1341     return isExactlyValue(Tmp);
1342   }
1343   bool isExactlyValue(const APFloat& V) const;
1344
1345   static bool isValueValidForType(EVT VT, const APFloat& Val);
1346
1347   static bool classof(const SDNode *N) {
1348     return N->getOpcode() == ISD::ConstantFP ||
1349            N->getOpcode() == ISD::TargetConstantFP;
1350   }
1351 };
1352
1353 class GlobalAddressSDNode : public SDNode {
1354   const GlobalValue *TheGlobal;
1355   int64_t Offset;
1356   unsigned char TargetFlags;
1357   friend class SelectionDAG;
1358   GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
1359                       const GlobalValue *GA, EVT VT, int64_t o,
1360                       unsigned char TargetFlags);
1361 public:
1362
1363   const GlobalValue *getGlobal() const { return TheGlobal; }
1364   int64_t getOffset() const { return Offset; }
1365   unsigned char getTargetFlags() const { return TargetFlags; }
1366   // Return the address space this GlobalAddress belongs to.
1367   unsigned getAddressSpace() const;
1368
1369   static bool classof(const SDNode *N) {
1370     return N->getOpcode() == ISD::GlobalAddress ||
1371            N->getOpcode() == ISD::TargetGlobalAddress ||
1372            N->getOpcode() == ISD::GlobalTLSAddress ||
1373            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1374   }
1375 };
1376
1377 class FrameIndexSDNode : public SDNode {
1378   int FI;
1379   friend class SelectionDAG;
1380   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1381     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1382       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1383   }
1384 public:
1385
1386   int getIndex() const { return FI; }
1387
1388   static bool classof(const SDNode *N) {
1389     return N->getOpcode() == ISD::FrameIndex ||
1390            N->getOpcode() == ISD::TargetFrameIndex;
1391   }
1392 };
1393
1394 class JumpTableSDNode : public SDNode {
1395   int JTI;
1396   unsigned char TargetFlags;
1397   friend class SelectionDAG;
1398   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1399     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1400       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1401   }
1402 public:
1403
1404   int getIndex() const { return JTI; }
1405   unsigned char getTargetFlags() const { return TargetFlags; }
1406
1407   static bool classof(const SDNode *N) {
1408     return N->getOpcode() == ISD::JumpTable ||
1409            N->getOpcode() == ISD::TargetJumpTable;
1410   }
1411 };
1412
1413 class ConstantPoolSDNode : public SDNode {
1414   union {
1415     const Constant *ConstVal;
1416     MachineConstantPoolValue *MachineCPVal;
1417   } Val;
1418   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1419   unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
1420   unsigned char TargetFlags;
1421   friend class SelectionDAG;
1422   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1423                      unsigned Align, unsigned char TF)
1424     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1425              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1426              TargetFlags(TF) {
1427     assert(Offset >= 0 && "Offset is too large");
1428     Val.ConstVal = c;
1429   }
1430   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1431                      EVT VT, int o, unsigned Align, unsigned char TF)
1432     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1433              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1434              TargetFlags(TF) {
1435     assert(Offset >= 0 && "Offset is too large");
1436     Val.MachineCPVal = v;
1437     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1438   }
1439 public:
1440
1441   bool isMachineConstantPoolEntry() const {
1442     return Offset < 0;
1443   }
1444
1445   const Constant *getConstVal() const {
1446     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1447     return Val.ConstVal;
1448   }
1449
1450   MachineConstantPoolValue *getMachineCPVal() const {
1451     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1452     return Val.MachineCPVal;
1453   }
1454
1455   int getOffset() const {
1456     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1457   }
1458
1459   // Return the alignment of this constant pool object, which is either 0 (for
1460   // default alignment) or the desired value.
1461   unsigned getAlignment() const { return Alignment; }
1462   unsigned char getTargetFlags() const { return TargetFlags; }
1463
1464   Type *getType() const;
1465
1466   static bool classof(const SDNode *N) {
1467     return N->getOpcode() == ISD::ConstantPool ||
1468            N->getOpcode() == ISD::TargetConstantPool;
1469   }
1470 };
1471
1472 /// Completely target-dependent object reference.
1473 class TargetIndexSDNode : public SDNode {
1474   unsigned char TargetFlags;
1475   int Index;
1476   int64_t Offset;
1477   friend class SelectionDAG;
1478 public:
1479
1480   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1481     : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1482       TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1483 public:
1484
1485   unsigned char getTargetFlags() const { return TargetFlags; }
1486   int getIndex() const { return Index; }
1487   int64_t getOffset() const { return Offset; }
1488
1489   static bool classof(const SDNode *N) {
1490     return N->getOpcode() == ISD::TargetIndex;
1491   }
1492 };
1493
1494 class BasicBlockSDNode : public SDNode {
1495   MachineBasicBlock *MBB;
1496   friend class SelectionDAG;
1497   /// Debug info is meaningful and potentially useful here, but we create
1498   /// blocks out of order when they're jumped to, which makes it a bit
1499   /// harder.  Let's see if we need it first.
1500   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1501     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1502   {}
1503 public:
1504
1505   MachineBasicBlock *getBasicBlock() const { return MBB; }
1506
1507   static bool classof(const SDNode *N) {
1508     return N->getOpcode() == ISD::BasicBlock;
1509   }
1510 };
1511
1512 /// BuildVectorSDNode - A "pseudo-class" with methods for operating on
1513 /// BUILD_VECTORs.
1514 class BuildVectorSDNode : public SDNode {
1515   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1516   explicit BuildVectorSDNode() LLVM_DELETED_FUNCTION;
1517 public:
1518   /// isConstantSplat - Check if this is a constant splat, and if so, find the
1519   /// smallest element size that splats the vector.  If MinSplatBits is
1520   /// nonzero, the element size must be at least that large.  Note that the
1521   /// splat element may be the entire vector (i.e., a one element vector).
1522   /// Returns the splat element value in SplatValue.  Any undefined bits in
1523   /// that value are zero, and the corresponding bits in the SplatUndef mask
1524   /// are set.  The SplatBitSize value is set to the splat element size in
1525   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
1526   /// undefined.  isBigEndian describes the endianness of the target.
1527   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1528                        unsigned &SplatBitSize, bool &HasAnyUndefs,
1529                        unsigned MinSplatBits = 0,
1530                        bool isBigEndian = false) const;
1531
1532   /// getConstantSplatValue - Check if this is a constant splat, and if so,
1533   /// return the splat value only if it is a ConstantSDNode. Otherwise
1534   /// return nullptr. This is a simpler form of isConstantSplat.
1535   /// Get the constant splat only if you care about the splat value.
1536   ConstantSDNode *getConstantSplatValue() const;
1537
1538   bool isConstant() const;
1539
1540   static inline bool classof(const SDNode *N) {
1541     return N->getOpcode() == ISD::BUILD_VECTOR;
1542   }
1543 };
1544
1545 /// SrcValueSDNode - An SDNode that holds an arbitrary LLVM IR Value. This is
1546 /// used when the SelectionDAG needs to make a simple reference to something
1547 /// in the LLVM IR representation.
1548 ///
1549 class SrcValueSDNode : public SDNode {
1550   const Value *V;
1551   friend class SelectionDAG;
1552   /// Create a SrcValue for a general value.
1553   explicit SrcValueSDNode(const Value *v)
1554     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1555
1556 public:
1557   /// getValue - return the contained Value.
1558   const Value *getValue() const { return V; }
1559
1560   static bool classof(const SDNode *N) {
1561     return N->getOpcode() == ISD::SRCVALUE;
1562   }
1563 };
1564
1565 class MDNodeSDNode : public SDNode {
1566   const MDNode *MD;
1567   friend class SelectionDAG;
1568   explicit MDNodeSDNode(const MDNode *md)
1569   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1570   {}
1571 public:
1572
1573   const MDNode *getMD() const { return MD; }
1574
1575   static bool classof(const SDNode *N) {
1576     return N->getOpcode() == ISD::MDNODE_SDNODE;
1577   }
1578 };
1579
1580 class RegisterSDNode : public SDNode {
1581   unsigned Reg;
1582   friend class SelectionDAG;
1583   RegisterSDNode(unsigned reg, EVT VT)
1584     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
1585   }
1586 public:
1587
1588   unsigned getReg() const { return Reg; }
1589
1590   static bool classof(const SDNode *N) {
1591     return N->getOpcode() == ISD::Register;
1592   }
1593 };
1594
1595 class RegisterMaskSDNode : public SDNode {
1596   // The memory for RegMask is not owned by the node.
1597   const uint32_t *RegMask;
1598   friend class SelectionDAG;
1599   RegisterMaskSDNode(const uint32_t *mask)
1600     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1601       RegMask(mask) {}
1602 public:
1603
1604   const uint32_t *getRegMask() const { return RegMask; }
1605
1606   static bool classof(const SDNode *N) {
1607     return N->getOpcode() == ISD::RegisterMask;
1608   }
1609 };
1610
1611 class BlockAddressSDNode : public SDNode {
1612   const BlockAddress *BA;
1613   int64_t Offset;
1614   unsigned char TargetFlags;
1615   friend class SelectionDAG;
1616   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1617                      int64_t o, unsigned char Flags)
1618     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1619              BA(ba), Offset(o), TargetFlags(Flags) {
1620   }
1621 public:
1622   const BlockAddress *getBlockAddress() const { return BA; }
1623   int64_t getOffset() const { return Offset; }
1624   unsigned char getTargetFlags() const { return TargetFlags; }
1625
1626   static bool classof(const SDNode *N) {
1627     return N->getOpcode() == ISD::BlockAddress ||
1628            N->getOpcode() == ISD::TargetBlockAddress;
1629   }
1630 };
1631
1632 class EHLabelSDNode : public SDNode {
1633   SDUse Chain;
1634   MCSymbol *Label;
1635   friend class SelectionDAG;
1636   EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
1637     : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
1638     InitOperands(&Chain, ch);
1639   }
1640 public:
1641   MCSymbol *getLabel() const { return Label; }
1642
1643   static bool classof(const SDNode *N) {
1644     return N->getOpcode() == ISD::EH_LABEL;
1645   }
1646 };
1647
1648 class ExternalSymbolSDNode : public SDNode {
1649   const char *Symbol;
1650   unsigned char TargetFlags;
1651
1652   friend class SelectionDAG;
1653   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1654     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1655              0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
1656   }
1657 public:
1658
1659   const char *getSymbol() const { return Symbol; }
1660   unsigned char getTargetFlags() const { return TargetFlags; }
1661
1662   static bool classof(const SDNode *N) {
1663     return N->getOpcode() == ISD::ExternalSymbol ||
1664            N->getOpcode() == ISD::TargetExternalSymbol;
1665   }
1666 };
1667
1668 class CondCodeSDNode : public SDNode {
1669   ISD::CondCode Condition;
1670   friend class SelectionDAG;
1671   explicit CondCodeSDNode(ISD::CondCode Cond)
1672     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1673       Condition(Cond) {
1674   }
1675 public:
1676
1677   ISD::CondCode get() const { return Condition; }
1678
1679   static bool classof(const SDNode *N) {
1680     return N->getOpcode() == ISD::CONDCODE;
1681   }
1682 };
1683
1684 /// CvtRndSatSDNode - NOTE: avoid using this node as this may disappear in the
1685 /// future and most targets don't support it.
1686 class CvtRndSatSDNode : public SDNode {
1687   ISD::CvtCode CvtCode;
1688   friend class SelectionDAG;
1689   explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
1690                            ArrayRef<SDValue> Ops, ISD::CvtCode Code)
1691     : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops),
1692       CvtCode(Code) {
1693     assert(Ops.size() == 5 && "wrong number of operations");
1694   }
1695 public:
1696   ISD::CvtCode getCvtCode() const { return CvtCode; }
1697
1698   static bool classof(const SDNode *N) {
1699     return N->getOpcode() == ISD::CONVERT_RNDSAT;
1700   }
1701 };
1702
1703 /// VTSDNode - This class is used to represent EVT's, which are used
1704 /// to parameterize some operations.
1705 class VTSDNode : public SDNode {
1706   EVT ValueType;
1707   friend class SelectionDAG;
1708   explicit VTSDNode(EVT VT)
1709     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1710       ValueType(VT) {
1711   }
1712 public:
1713
1714   EVT getVT() const { return ValueType; }
1715
1716   static bool classof(const SDNode *N) {
1717     return N->getOpcode() == ISD::VALUETYPE;
1718   }
1719 };
1720
1721 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1722 ///
1723 class LSBaseSDNode : public MemSDNode {
1724   //! Operand array for load and store
1725   /*!
1726     \note Moving this array to the base class captures more
1727     common functionality shared between LoadSDNode and
1728     StoreSDNode
1729    */
1730   SDUse Ops[4];
1731 public:
1732   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1733                SDValue *Operands, unsigned numOperands,
1734                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1735                MachineMemOperand *MMO)
1736     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1737     SubclassData |= AM << 2;
1738     assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1739     InitOperands(Ops, Operands, numOperands);
1740     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1741            "Only indexed loads and stores have a non-undef offset operand");
1742   }
1743
1744   const SDValue &getOffset() const {
1745     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1746   }
1747
1748   /// getAddressingMode - Return the addressing mode for this load or store:
1749   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1750   ISD::MemIndexedMode getAddressingMode() const {
1751     return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1752   }
1753
1754   /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1755   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1756
1757   /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1758   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1759
1760   static bool classof(const SDNode *N) {
1761     return N->getOpcode() == ISD::LOAD ||
1762            N->getOpcode() == ISD::STORE;
1763   }
1764 };
1765
1766 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1767 ///
1768 class LoadSDNode : public LSBaseSDNode {
1769   friend class SelectionDAG;
1770   LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1771              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1772              MachineMemOperand *MMO)
1773     : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1774     SubclassData |= (unsigned short)ETy;
1775     assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1776     assert(readMem() && "Load MachineMemOperand is not a load!");
1777     assert(!writeMem() && "Load MachineMemOperand is a store!");
1778   }
1779 public:
1780
1781   /// getExtensionType - Return whether this is a plain node,
1782   /// or one of the varieties of value-extending loads.
1783   ISD::LoadExtType getExtensionType() const {
1784     return ISD::LoadExtType(SubclassData & 3);
1785   }
1786
1787   const SDValue &getBasePtr() const { return getOperand(1); }
1788   const SDValue &getOffset() const { return getOperand(2); }
1789
1790   static bool classof(const SDNode *N) {
1791     return N->getOpcode() == ISD::LOAD;
1792   }
1793 };
1794
1795 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1796 ///
1797 class StoreSDNode : public LSBaseSDNode {
1798   friend class SelectionDAG;
1799   StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1800               SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1801               MachineMemOperand *MMO)
1802     : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1803                    VTs, AM, MemVT, MMO) {
1804     SubclassData |= (unsigned short)isTrunc;
1805     assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1806     assert(!readMem() && "Store MachineMemOperand is a load!");
1807     assert(writeMem() && "Store MachineMemOperand is not a store!");
1808   }
1809 public:
1810
1811   /// isTruncatingStore - Return true if the op does a truncation before store.
1812   /// For integers this is the same as doing a TRUNCATE and storing the result.
1813   /// For floats, it is the same as doing an FP_ROUND and storing the result.
1814   bool isTruncatingStore() const { return SubclassData & 1; }
1815
1816   const SDValue &getValue() const { return getOperand(1); }
1817   const SDValue &getBasePtr() const { return getOperand(2); }
1818   const SDValue &getOffset() const { return getOperand(3); }
1819
1820   static bool classof(const SDNode *N) {
1821     return N->getOpcode() == ISD::STORE;
1822   }
1823 };
1824
1825 /// MachineSDNode - An SDNode that represents everything that will be needed
1826 /// to construct a MachineInstr. These nodes are created during the
1827 /// instruction selection proper phase.
1828 ///
1829 class MachineSDNode : public SDNode {
1830 public:
1831   typedef MachineMemOperand **mmo_iterator;
1832
1833 private:
1834   friend class SelectionDAG;
1835   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
1836     : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
1837
1838   /// LocalOperands - Operands for this instruction, if they fit here. If
1839   /// they don't, this field is unused.
1840   SDUse LocalOperands[4];
1841
1842   /// MemRefs - Memory reference descriptions for this instruction.
1843   mmo_iterator MemRefs;
1844   mmo_iterator MemRefsEnd;
1845
1846 public:
1847   mmo_iterator memoperands_begin() const { return MemRefs; }
1848   mmo_iterator memoperands_end() const { return MemRefsEnd; }
1849   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1850
1851   /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1852   /// list. This does not transfer ownership.
1853   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1854     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1855       assert(*MMI && "Null mem ref detected!");
1856     MemRefs = NewMemRefs;
1857     MemRefsEnd = NewMemRefsEnd;
1858   }
1859
1860   static bool classof(const SDNode *N) {
1861     return N->isMachineOpcode();
1862   }
1863 };
1864
1865 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1866                                             SDNode, ptrdiff_t> {
1867   const SDNode *Node;
1868   unsigned Operand;
1869
1870   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1871 public:
1872   bool operator==(const SDNodeIterator& x) const {
1873     return Operand == x.Operand;
1874   }
1875   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1876
1877   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1878     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1879     Operand = I.Operand;
1880     return *this;
1881   }
1882
1883   pointer operator*() const {
1884     return Node->getOperand(Operand).getNode();
1885   }
1886   pointer operator->() const { return operator*(); }
1887
1888   SDNodeIterator& operator++() {                // Preincrement
1889     ++Operand;
1890     return *this;
1891   }
1892   SDNodeIterator operator++(int) { // Postincrement
1893     SDNodeIterator tmp = *this; ++*this; return tmp;
1894   }
1895   size_t operator-(SDNodeIterator Other) const {
1896     assert(Node == Other.Node &&
1897            "Cannot compare iterators of two different nodes!");
1898     return Operand - Other.Operand;
1899   }
1900
1901   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1902   static SDNodeIterator end  (const SDNode *N) {
1903     return SDNodeIterator(N, N->getNumOperands());
1904   }
1905
1906   unsigned getOperand() const { return Operand; }
1907   const SDNode *getNode() const { return Node; }
1908 };
1909
1910 template <> struct GraphTraits<SDNode*> {
1911   typedef SDNode NodeType;
1912   typedef SDNodeIterator ChildIteratorType;
1913   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1914   static inline ChildIteratorType child_begin(NodeType *N) {
1915     return SDNodeIterator::begin(N);
1916   }
1917   static inline ChildIteratorType child_end(NodeType *N) {
1918     return SDNodeIterator::end(N);
1919   }
1920 };
1921
1922 /// LargestSDNode - The largest SDNode class.
1923 ///
1924 typedef AtomicSDNode LargestSDNode;
1925
1926 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1927 /// requirement.
1928 ///
1929 typedef GlobalAddressSDNode MostAlignedSDNode;
1930
1931 namespace ISD {
1932   /// isNormalLoad - Returns true if the specified node is a non-extending
1933   /// and unindexed load.
1934   inline bool isNormalLoad(const SDNode *N) {
1935     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1936     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1937       Ld->getAddressingMode() == ISD::UNINDEXED;
1938   }
1939
1940   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1941   /// load.
1942   inline bool isNON_EXTLoad(const SDNode *N) {
1943     return isa<LoadSDNode>(N) &&
1944       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1945   }
1946
1947   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1948   ///
1949   inline bool isEXTLoad(const SDNode *N) {
1950     return isa<LoadSDNode>(N) &&
1951       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1952   }
1953
1954   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1955   ///
1956   inline bool isSEXTLoad(const SDNode *N) {
1957     return isa<LoadSDNode>(N) &&
1958       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1959   }
1960
1961   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1962   ///
1963   inline bool isZEXTLoad(const SDNode *N) {
1964     return isa<LoadSDNode>(N) &&
1965       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1966   }
1967
1968   /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1969   ///
1970   inline bool isUNINDEXEDLoad(const SDNode *N) {
1971     return isa<LoadSDNode>(N) &&
1972       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1973   }
1974
1975   /// isNormalStore - Returns true if the specified node is a non-truncating
1976   /// and unindexed store.
1977   inline bool isNormalStore(const SDNode *N) {
1978     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1979     return St && !St->isTruncatingStore() &&
1980       St->getAddressingMode() == ISD::UNINDEXED;
1981   }
1982
1983   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1984   /// store.
1985   inline bool isNON_TRUNCStore(const SDNode *N) {
1986     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1987   }
1988
1989   /// isTRUNCStore - Returns true if the specified node is a truncating
1990   /// store.
1991   inline bool isTRUNCStore(const SDNode *N) {
1992     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1993   }
1994
1995   /// isUNINDEXEDStore - Returns true if the specified node is an
1996   /// unindexed store.
1997   inline bool isUNINDEXEDStore(const SDNode *N) {
1998     return isa<StoreSDNode>(N) &&
1999       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2000   }
2001 }
2002
2003 } // end llvm namespace
2004
2005 #endif