Convert getMemIntrinsicNode to take ArrayRef of SDValue instead of pointer and size.
[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);
53
54 /// SDVTList - This represents a list of ValueType's that has been intern'd by
55 /// a SelectionDAG.  Instances of this simple value class are returned by
56 /// SelectionDAG::getVTList(...).
57 ///
58 struct SDVTList {
59   const EVT *VTs;
60   unsigned int NumVTs;
61 };
62
63 namespace ISD {
64   /// Node predicates
65
66   /// isBuildVectorAllOnes - Return true if the specified node is a
67   /// BUILD_VECTOR where all of the elements are ~0 or undef.
68   bool isBuildVectorAllOnes(const SDNode *N);
69
70   /// isBuildVectorAllZeros - Return true if the specified node is a
71   /// BUILD_VECTOR where all of the elements are 0 or undef.
72   bool isBuildVectorAllZeros(const SDNode *N);
73
74   /// \brief Return true if the specified node is a BUILD_VECTOR node of
75   /// all ConstantSDNode or undef.
76   bool isBuildVectorOfConstantSDNodes(const SDNode *N);
77
78   /// isScalarToVector - Return true if the specified node is a
79   /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
80   /// element is not an undef.
81   bool isScalarToVector(const SDNode *N);
82
83   /// allOperandsUndef - Return true if the node has at least one operand
84   /// and all operands of the specified node are ISD::UNDEF.
85   bool allOperandsUndef(const SDNode *N);
86 }  // end llvm:ISD namespace
87
88 //===----------------------------------------------------------------------===//
89 /// SDValue - Unlike LLVM values, Selection DAG nodes may return multiple
90 /// values as the result of a computation.  Many nodes return multiple values,
91 /// from loads (which define a token and a return value) to ADDC (which returns
92 /// a result and a carry value), to calls (which may return an arbitrary number
93 /// of values).
94 ///
95 /// As such, each use of a SelectionDAG computation must indicate the node that
96 /// computes it as well as which return value to use from that node.  This pair
97 /// of information is represented with the SDValue value type.
98 ///
99 class SDValue {
100   SDNode *Node;       // The node defining the value we are using.
101   unsigned ResNo;     // Which return value of the node we are using.
102 public:
103   SDValue() : Node(nullptr), ResNo(0) {}
104   SDValue(SDNode *node, unsigned resno) : Node(node), ResNo(resno) {}
105
106   /// get the index which selects a specific result in the SDNode
107   unsigned getResNo() const { return ResNo; }
108
109   /// get the SDNode which holds the desired result
110   SDNode *getNode() const { return Node; }
111
112   /// set the SDNode
113   void setNode(SDNode *N) { Node = N; }
114
115   inline SDNode *operator->() const { return Node; }
116
117   bool operator==(const SDValue &O) const {
118     return Node == O.Node && ResNo == O.ResNo;
119   }
120   bool operator!=(const SDValue &O) const {
121     return !operator==(O);
122   }
123   bool operator<(const SDValue &O) const {
124     return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
125   }
126
127   SDValue getValue(unsigned R) const {
128     return SDValue(Node, R);
129   }
130
131   // isOperandOf - Return true if this node is an operand of N.
132   bool isOperandOf(SDNode *N) const;
133
134   /// getValueType - Return the ValueType of the referenced return value.
135   ///
136   inline EVT getValueType() const;
137
138   /// Return the simple ValueType of the referenced return value.
139   MVT getSimpleValueType() const {
140     return getValueType().getSimpleVT();
141   }
142
143   /// getValueSizeInBits - Returns the size of the value in bits.
144   ///
145   unsigned getValueSizeInBits() const {
146     return getValueType().getSizeInBits();
147   }
148
149   unsigned getScalarValueSizeInBits() const {
150     return getValueType().getScalarType().getSizeInBits();
151   }
152
153   // Forwarding methods - These forward to the corresponding methods in SDNode.
154   inline unsigned getOpcode() const;
155   inline unsigned getNumOperands() const;
156   inline const SDValue &getOperand(unsigned i) const;
157   inline uint64_t getConstantOperandVal(unsigned i) const;
158   inline bool isTargetMemoryOpcode() const;
159   inline bool isTargetOpcode() const;
160   inline bool isMachineOpcode() const;
161   inline unsigned getMachineOpcode() const;
162   inline const DebugLoc getDebugLoc() const;
163   inline void dump() const;
164   inline void dumpr() const;
165
166   /// reachesChainWithoutSideEffects - Return true if this operand (which must
167   /// be a chain) reaches the specified operand without crossing any
168   /// side-effecting instructions.  In practice, this looks through token
169   /// factors and non-volatile loads.  In order to remain efficient, this only
170   /// looks a couple of nodes in, it does not do an exhaustive search.
171   bool reachesChainWithoutSideEffects(SDValue Dest,
172                                       unsigned Depth = 2) const;
173
174   /// use_empty - Return true if there are no nodes using value ResNo
175   /// of Node.
176   ///
177   inline bool use_empty() const;
178
179   /// hasOneUse - Return true if there is exactly one node using value
180   /// ResNo of Node.
181   ///
182   inline bool hasOneUse() const;
183 };
184
185
186 template<> struct DenseMapInfo<SDValue> {
187   static inline SDValue getEmptyKey() {
188     return SDValue((SDNode*)-1, -1U);
189   }
190   static inline SDValue getTombstoneKey() {
191     return SDValue((SDNode*)-1, 0);
192   }
193   static unsigned getHashValue(const SDValue &Val) {
194     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
195             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
196   }
197   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
198     return LHS == RHS;
199   }
200 };
201 template <> struct isPodLike<SDValue> { static const bool value = true; };
202
203
204 /// simplify_type specializations - Allow casting operators to work directly on
205 /// SDValues as if they were SDNode*'s.
206 template<> struct simplify_type<SDValue> {
207   typedef SDNode* SimpleType;
208   static SimpleType getSimplifiedValue(SDValue &Val) {
209     return Val.getNode();
210   }
211 };
212 template<> struct simplify_type<const SDValue> {
213   typedef /*const*/ SDNode* SimpleType;
214   static SimpleType getSimplifiedValue(const SDValue &Val) {
215     return Val.getNode();
216   }
217 };
218
219 /// SDUse - Represents a use of a SDNode. This class holds an SDValue,
220 /// which records the SDNode being used and the result number, a
221 /// pointer to the SDNode using the value, and Next and Prev pointers,
222 /// which link together all the uses of an SDNode.
223 ///
224 class SDUse {
225   /// Val - The value being used.
226   SDValue Val;
227   /// User - The user of this value.
228   SDNode *User;
229   /// Prev, Next - Pointers to the uses list of the SDNode referred by
230   /// this operand.
231   SDUse **Prev, *Next;
232
233   SDUse(const SDUse &U) LLVM_DELETED_FUNCTION;
234   void operator=(const SDUse &U) LLVM_DELETED_FUNCTION;
235
236 public:
237   SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
238
239   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
240   operator const SDValue&() const { return Val; }
241
242   /// If implicit conversion to SDValue doesn't work, the get() method returns
243   /// the SDValue.
244   const SDValue &get() const { return Val; }
245
246   /// getUser - This returns the SDNode that contains this Use.
247   SDNode *getUser() { return User; }
248
249   /// getNext - Get the next SDUse in the use list.
250   SDUse *getNext() const { return Next; }
251
252   /// getNode - Convenience function for get().getNode().
253   SDNode *getNode() const { return Val.getNode(); }
254   /// getResNo - Convenience function for get().getResNo().
255   unsigned getResNo() const { return Val.getResNo(); }
256   /// getValueType - Convenience function for get().getValueType().
257   EVT getValueType() const { return Val.getValueType(); }
258
259   /// operator== - Convenience function for get().operator==
260   bool operator==(const SDValue &V) const {
261     return Val == V;
262   }
263
264   /// operator!= - Convenience function for get().operator!=
265   bool operator!=(const SDValue &V) const {
266     return Val != V;
267   }
268
269   /// operator< - Convenience function for get().operator<
270   bool operator<(const SDValue &V) const {
271     return Val < V;
272   }
273
274 private:
275   friend class SelectionDAG;
276   friend class SDNode;
277
278   void setUser(SDNode *p) { User = p; }
279
280   /// set - Remove this use from its existing use list, assign it the
281   /// given value, and add it to the new value's node's use list.
282   inline void set(const SDValue &V);
283   /// setInitial - like set, but only supports initializing a newly-allocated
284   /// SDUse with a non-null value.
285   inline void setInitial(const SDValue &V);
286   /// setNode - like set, but only sets the Node portion of the value,
287   /// leaving the ResNo portion unmodified.
288   inline void setNode(SDNode *N);
289
290   void addToList(SDUse **List) {
291     Next = *List;
292     if (Next) Next->Prev = &Next;
293     Prev = List;
294     *List = this;
295   }
296
297   void removeFromList() {
298     *Prev = Next;
299     if (Next) Next->Prev = Prev;
300   }
301 };
302
303 /// simplify_type specializations - Allow casting operators to work directly on
304 /// SDValues as if they were SDNode*'s.
305 template<> struct simplify_type<SDUse> {
306   typedef SDNode* SimpleType;
307   static SimpleType getSimplifiedValue(SDUse &Val) {
308     return Val.getNode();
309   }
310 };
311
312
313 /// SDNode - Represents one node in the SelectionDAG.
314 ///
315 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
316 private:
317   /// NodeType - The operation that this node performs.
318   ///
319   int16_t NodeType;
320
321   /// OperandsNeedDelete - This is true if OperandList was new[]'d.  If true,
322   /// then they will be delete[]'d when the node is destroyed.
323   uint16_t OperandsNeedDelete : 1;
324
325   /// HasDebugValue - This tracks whether this node has one or more dbg_value
326   /// nodes corresponding to it.
327   uint16_t HasDebugValue : 1;
328
329 protected:
330   /// SubclassData - This member is defined by this class, but is not used for
331   /// anything.  Subclasses can use it to hold whatever state they find useful.
332   /// This field is initialized to zero by the ctor.
333   uint16_t SubclassData : 14;
334
335 private:
336   /// NodeId - Unique id per SDNode in the DAG.
337   int NodeId;
338
339   /// OperandList - The values that are used by this operation.
340   ///
341   SDUse *OperandList;
342
343   /// ValueList - The types of the values this node defines.  SDNode's may
344   /// define multiple values simultaneously.
345   const EVT *ValueList;
346
347   /// UseList - List of uses for this SDNode.
348   SDUse *UseList;
349
350   /// NumOperands/NumValues - The number of entries in the Operand/Value list.
351   unsigned short NumOperands, NumValues;
352
353   /// debugLoc - source line information.
354   DebugLoc debugLoc;
355
356   // The ordering of the SDNodes. It roughly corresponds to the ordering of the
357   // original LLVM instructions.
358   // This is used for turning off scheduling, because we'll forgo
359   // the normal scheduling algorithms and output the instructions according to
360   // this ordering.
361   unsigned IROrder;
362
363   /// getValueTypeList - Return a pointer to the specified value type.
364   static const EVT *getValueTypeList(EVT VT);
365
366   friend class SelectionDAG;
367   friend struct ilist_traits<SDNode>;
368
369 public:
370   //===--------------------------------------------------------------------===//
371   //  Accessors
372   //
373
374   /// getOpcode - Return the SelectionDAG opcode value for this node. For
375   /// pre-isel nodes (those for which isMachineOpcode returns false), these
376   /// are the opcode values in the ISD and <target>ISD namespaces. For
377   /// post-isel opcodes, see getMachineOpcode.
378   unsigned getOpcode()  const { return (unsigned short)NodeType; }
379
380   /// isTargetOpcode - Test if this node has a target-specific opcode (in the
381   /// \<target\>ISD namespace).
382   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
383
384   /// isTargetMemoryOpcode - Test if this node has a target-specific
385   /// memory-referencing opcode (in the \<target\>ISD namespace and
386   /// greater than FIRST_TARGET_MEMORY_OPCODE).
387   bool isTargetMemoryOpcode() const {
388     return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
389   }
390
391   /// isMachineOpcode - Test if this node has a post-isel opcode, directly
392   /// corresponding to a MachineInstr opcode.
393   bool isMachineOpcode() const { return NodeType < 0; }
394
395   /// getMachineOpcode - This may only be called if isMachineOpcode returns
396   /// true. It returns the MachineInstr opcode value that the node's opcode
397   /// corresponds to.
398   unsigned getMachineOpcode() const {
399     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
400     return ~NodeType;
401   }
402
403   /// getHasDebugValue - get this bit.
404   bool getHasDebugValue() const { return HasDebugValue; }
405
406   /// setHasDebugValue - set this bit.
407   void setHasDebugValue(bool b) { HasDebugValue = b; }
408
409   /// use_empty - Return true if there are no uses of this node.
410   ///
411   bool use_empty() const { return UseList == nullptr; }
412
413   /// hasOneUse - Return true if there is exactly one use of this node.
414   ///
415   bool hasOneUse() const {
416     return !use_empty() && std::next(use_begin()) == use_end();
417   }
418
419   /// use_size - Return the number of uses of this node. This method takes
420   /// time proportional to the number of uses.
421   ///
422   size_t use_size() const { return std::distance(use_begin(), use_end()); }
423
424   /// getNodeId - Return the unique node id.
425   ///
426   int getNodeId() const { return NodeId; }
427
428   /// setNodeId - Set unique node id.
429   void setNodeId(int Id) { NodeId = Id; }
430
431   /// getIROrder - Return the node ordering.
432   ///
433   unsigned getIROrder() const { return IROrder; }
434
435   /// setIROrder - Set the node ordering.
436   ///
437   void setIROrder(unsigned Order) { IROrder = Order; }
438
439   /// getDebugLoc - Return the source location info.
440   const DebugLoc getDebugLoc() const { return debugLoc; }
441
442   /// setDebugLoc - Set source location info.  Try to avoid this, putting
443   /// it in the constructor is preferable.
444   void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
445
446   /// use_iterator - This class provides iterator support for SDUse
447   /// operands that use a specific SDNode.
448   class use_iterator
449     : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
450     SDUse *Op;
451     explicit use_iterator(SDUse *op) : Op(op) {
452     }
453     friend class SDNode;
454   public:
455     typedef std::iterator<std::forward_iterator_tag,
456                           SDUse, ptrdiff_t>::reference reference;
457     typedef std::iterator<std::forward_iterator_tag,
458                           SDUse, ptrdiff_t>::pointer pointer;
459
460     use_iterator(const use_iterator &I) : Op(I.Op) {}
461     use_iterator() : Op(nullptr) {}
462
463     bool operator==(const use_iterator &x) const {
464       return Op == x.Op;
465     }
466     bool operator!=(const use_iterator &x) const {
467       return !operator==(x);
468     }
469
470     /// atEnd - return true if this iterator is at the end of uses list.
471     bool atEnd() const { return Op == nullptr; }
472
473     // Iterator traversal: forward iteration only.
474     use_iterator &operator++() {          // Preincrement
475       assert(Op && "Cannot increment end iterator!");
476       Op = Op->getNext();
477       return *this;
478     }
479
480     use_iterator operator++(int) {        // Postincrement
481       use_iterator tmp = *this; ++*this; return tmp;
482     }
483
484     /// Retrieve a pointer to the current user node.
485     SDNode *operator*() const {
486       assert(Op && "Cannot dereference end iterator!");
487       return Op->getUser();
488     }
489
490     SDNode *operator->() const { return operator*(); }
491
492     SDUse &getUse() const { return *Op; }
493
494     /// getOperandNo - Retrieve the operand # of this use in its user.
495     ///
496     unsigned getOperandNo() const {
497       assert(Op && "Cannot dereference end iterator!");
498       return (unsigned)(Op - Op->getUser()->OperandList);
499     }
500   };
501
502   /// use_begin/use_end - Provide iteration support to walk over all uses
503   /// of an SDNode.
504
505   use_iterator use_begin() const {
506     return use_iterator(UseList);
507   }
508
509   static use_iterator use_end() { return use_iterator(nullptr); }
510
511   inline iterator_range<use_iterator> uses() {
512     return iterator_range<use_iterator>(use_begin(), use_end());
513   }
514   inline iterator_range<use_iterator> uses() const {
515     return iterator_range<use_iterator>(use_begin(), use_end());
516   }
517
518   /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
519   /// indicated value.  This method ignores uses of other values defined by this
520   /// operation.
521   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
522
523   /// hasAnyUseOfValue - Return true if there are any use of the indicated
524   /// value. This method ignores uses of other values defined by this operation.
525   bool hasAnyUseOfValue(unsigned Value) const;
526
527   /// isOnlyUserOf - Return true if this node is the only use of N.
528   ///
529   bool isOnlyUserOf(SDNode *N) const;
530
531   /// isOperandOf - Return true if this node is an operand of N.
532   ///
533   bool isOperandOf(SDNode *N) const;
534
535   /// isPredecessorOf - Return true if this node is a predecessor of N.
536   /// NOTE: Implemented on top of hasPredecessor and every bit as
537   /// expensive. Use carefully.
538   bool isPredecessorOf(const SDNode *N) const {
539     return N->hasPredecessor(this);
540   }
541
542   /// hasPredecessor - Return true if N is a predecessor of this node.
543   /// N is either an operand of this node, or can be reached by recursively
544   /// traversing up the operands.
545   /// NOTE: This is an expensive method. Use it carefully.
546   bool hasPredecessor(const SDNode *N) const;
547
548   /// hasPredecesorHelper - Return true if N is a predecessor of this node.
549   /// N is either an operand of this node, or can be reached by recursively
550   /// traversing up the operands.
551   /// In this helper the Visited and worklist sets are held externally to
552   /// cache predecessors over multiple invocations. If you want to test for
553   /// multiple predecessors this method is preferable to multiple calls to
554   /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
555   /// changes.
556   /// NOTE: This is still very expensive. Use carefully.
557   bool hasPredecessorHelper(const SDNode *N,
558                             SmallPtrSet<const SDNode *, 32> &Visited,
559                             SmallVectorImpl<const SDNode *> &Worklist) const;
560
561   /// getNumOperands - Return the number of values used by this operation.
562   ///
563   unsigned getNumOperands() const { return NumOperands; }
564
565   /// getConstantOperandVal - Helper method returns the integer value of a
566   /// ConstantSDNode operand.
567   uint64_t getConstantOperandVal(unsigned Num) const;
568
569   const SDValue &getOperand(unsigned Num) const {
570     assert(Num < NumOperands && "Invalid child # of SDNode!");
571     return OperandList[Num];
572   }
573
574   typedef SDUse* op_iterator;
575   op_iterator op_begin() const { return OperandList; }
576   op_iterator op_end() const { return OperandList+NumOperands; }
577
578   SDVTList getVTList() const {
579     SDVTList X = { ValueList, NumValues };
580     return X;
581   }
582
583   /// getGluedNode - If this node has a glue operand, return the node
584   /// to which the glue operand points. Otherwise return NULL.
585   SDNode *getGluedNode() const {
586     if (getNumOperands() != 0 &&
587       getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
588       return getOperand(getNumOperands()-1).getNode();
589     return nullptr;
590   }
591
592   // If this is a pseudo op, like copyfromreg, look to see if there is a
593   // real target node glued to it.  If so, return the target node.
594   const SDNode *getGluedMachineNode() const {
595     const SDNode *FoundNode = this;
596
597     // Climb up glue edges until a machine-opcode node is found, or the
598     // end of the chain is reached.
599     while (!FoundNode->isMachineOpcode()) {
600       const SDNode *N = FoundNode->getGluedNode();
601       if (!N) break;
602       FoundNode = N;
603     }
604
605     return FoundNode;
606   }
607
608   /// getGluedUser - If this node has a glue value with a user, return
609   /// the user (there is at most one). Otherwise return NULL.
610   SDNode *getGluedUser() const {
611     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
612       if (UI.getUse().get().getValueType() == MVT::Glue)
613         return *UI;
614     return nullptr;
615   }
616
617   /// getNumValues - Return the number of values defined/returned by this
618   /// operator.
619   ///
620   unsigned getNumValues() const { return NumValues; }
621
622   /// getValueType - Return the type of a specified result.
623   ///
624   EVT getValueType(unsigned ResNo) const {
625     assert(ResNo < NumValues && "Illegal result number!");
626     return ValueList[ResNo];
627   }
628
629   /// Return the type of a specified result as a simple type.
630   ///
631   MVT getSimpleValueType(unsigned ResNo) const {
632     return getValueType(ResNo).getSimpleVT();
633   }
634
635   /// getValueSizeInBits - Returns MVT::getSizeInBits(getValueType(ResNo)).
636   ///
637   unsigned getValueSizeInBits(unsigned ResNo) const {
638     return getValueType(ResNo).getSizeInBits();
639   }
640
641   typedef const EVT* value_iterator;
642   value_iterator value_begin() const { return ValueList; }
643   value_iterator value_end() const { return ValueList+NumValues; }
644
645   /// getOperationName - Return the opcode of this operation for printing.
646   ///
647   std::string getOperationName(const SelectionDAG *G = nullptr) const;
648   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
649   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
650   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
651   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
652   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
653
654   /// printrFull - Print a SelectionDAG node and all children down to
655   /// the leaves.  The given SelectionDAG allows target-specific nodes
656   /// to be printed in human-readable form.  Unlike printr, this will
657   /// print the whole DAG, including children that appear multiple
658   /// times.
659   ///
660   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
661
662   /// printrWithDepth - Print a SelectionDAG node and children up to
663   /// depth "depth."  The given SelectionDAG allows target-specific
664   /// nodes to be printed in human-readable form.  Unlike printr, this
665   /// will print children that appear multiple times wherever they are
666   /// used.
667   ///
668   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
669                        unsigned depth = 100) const;
670
671
672   /// dump - Dump this node, for debugging.
673   void dump() const;
674
675   /// dumpr - Dump (recursively) this node and its use-def subgraph.
676   void dumpr() const;
677
678   /// dump - Dump this node, for debugging.
679   /// The given SelectionDAG allows target-specific nodes to be printed
680   /// in human-readable form.
681   void dump(const SelectionDAG *G) const;
682
683   /// dumpr - Dump (recursively) this node and its use-def subgraph.
684   /// The given SelectionDAG allows target-specific nodes to be printed
685   /// in human-readable form.
686   void dumpr(const SelectionDAG *G) const;
687
688   /// dumprFull - printrFull to dbgs().  The given SelectionDAG allows
689   /// target-specific nodes to be printed in human-readable form.
690   /// Unlike dumpr, this will print the whole DAG, including children
691   /// that appear multiple times.
692   ///
693   void dumprFull(const SelectionDAG *G = nullptr) const;
694
695   /// dumprWithDepth - printrWithDepth to dbgs().  The given
696   /// SelectionDAG allows target-specific nodes to be printed in
697   /// human-readable form.  Unlike dumpr, this will print children
698   /// that appear multiple times wherever they are used.
699   ///
700   void dumprWithDepth(const SelectionDAG *G = nullptr,
701                       unsigned depth = 100) const;
702
703   /// Profile - Gather unique data for the node.
704   ///
705   void Profile(FoldingSetNodeID &ID) const;
706
707   /// addUse - This method should only be used by the SDUse class.
708   ///
709   void addUse(SDUse &U) { U.addToList(&UseList); }
710
711 protected:
712   static SDVTList getSDVTList(EVT VT) {
713     SDVTList Ret = { getValueTypeList(VT), 1 };
714     return Ret;
715   }
716
717   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs,
718          const SDValue *Ops, unsigned NumOps)
719     : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
720       SubclassData(0), NodeId(-1),
721       OperandList(NumOps ? new SDUse[NumOps] : nullptr),
722       ValueList(VTs.VTs), UseList(nullptr),
723       NumOperands(NumOps), NumValues(VTs.NumVTs),
724       debugLoc(dl), IROrder(Order) {
725     for (unsigned i = 0; i != NumOps; ++i) {
726       OperandList[i].setUser(this);
727       OperandList[i].setInitial(Ops[i]);
728     }
729     checkForCycles(this);
730   }
731
732   /// This constructor adds no operands itself; operands can be
733   /// set later with InitOperands.
734   SDNode(unsigned Opc, unsigned Order, const DebugLoc dl, SDVTList VTs)
735     : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
736       SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
737       UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs), debugLoc(dl),
738       IROrder(Order) {}
739
740   /// InitOperands - Initialize the operands list of this with 1 operand.
741   void InitOperands(SDUse *Ops, const SDValue &Op0) {
742     Ops[0].setUser(this);
743     Ops[0].setInitial(Op0);
744     NumOperands = 1;
745     OperandList = Ops;
746     checkForCycles(this);
747   }
748
749   /// InitOperands - Initialize the operands list of this with 2 operands.
750   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
751     Ops[0].setUser(this);
752     Ops[0].setInitial(Op0);
753     Ops[1].setUser(this);
754     Ops[1].setInitial(Op1);
755     NumOperands = 2;
756     OperandList = Ops;
757     checkForCycles(this);
758   }
759
760   /// InitOperands - Initialize the operands list of this with 3 operands.
761   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
762                     const SDValue &Op2) {
763     Ops[0].setUser(this);
764     Ops[0].setInitial(Op0);
765     Ops[1].setUser(this);
766     Ops[1].setInitial(Op1);
767     Ops[2].setUser(this);
768     Ops[2].setInitial(Op2);
769     NumOperands = 3;
770     OperandList = Ops;
771     checkForCycles(this);
772   }
773
774   /// InitOperands - Initialize the operands list of this with 4 operands.
775   void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
776                     const SDValue &Op2, const SDValue &Op3) {
777     Ops[0].setUser(this);
778     Ops[0].setInitial(Op0);
779     Ops[1].setUser(this);
780     Ops[1].setInitial(Op1);
781     Ops[2].setUser(this);
782     Ops[2].setInitial(Op2);
783     Ops[3].setUser(this);
784     Ops[3].setInitial(Op3);
785     NumOperands = 4;
786     OperandList = Ops;
787     checkForCycles(this);
788   }
789
790   /// InitOperands - Initialize the operands list of this with N operands.
791   void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
792     for (unsigned i = 0; i != N; ++i) {
793       Ops[i].setUser(this);
794       Ops[i].setInitial(Vals[i]);
795     }
796     NumOperands = N;
797     OperandList = Ops;
798     checkForCycles(this);
799   }
800
801   /// DropOperands - Release the operands and set this node to have
802   /// zero operands.
803   void DropOperands();
804 };
805
806 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
807 /// into SDNode creation functions.
808 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
809 /// from the original Instruction, and IROrder is the ordinal position of
810 /// the instruction.
811 /// When an SDNode is created after the DAG is being built, both DebugLoc and
812 /// the IROrder are propagated from the original SDNode.
813 /// So SDLoc class provides two constructors besides the default one, one to
814 /// be used by the DAGBuilder, the other to be used by others.
815 class SDLoc {
816 private:
817   // Ptr could be used for either Instruction* or SDNode*. It is used for
818   // Instruction* if IROrder is not -1.
819   const void *Ptr;
820   int IROrder;
821
822 public:
823   SDLoc() : Ptr(nullptr), IROrder(0) {}
824   SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
825     assert(N && "null SDNode");
826   }
827   SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
828     assert(Ptr && "null SDNode");
829   }
830   SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
831     assert(Order >= 0 && "bad IROrder");
832   }
833   unsigned getIROrder() {
834     if (IROrder >= 0 || Ptr == nullptr) {
835       return (unsigned)IROrder;
836     }
837     const SDNode *N = (const SDNode*)(Ptr);
838     return N->getIROrder();
839   }
840   DebugLoc getDebugLoc() {
841     if (!Ptr) {
842       return DebugLoc();
843     }
844     if (IROrder >= 0) {
845       const Instruction *I = (const Instruction*)(Ptr);
846       return I->getDebugLoc();
847     }
848     const SDNode *N = (const SDNode*)(Ptr);
849     return N->getDebugLoc();
850   }
851 };
852
853
854 // Define inline functions from the SDValue class.
855
856 inline unsigned SDValue::getOpcode() const {
857   return Node->getOpcode();
858 }
859 inline EVT SDValue::getValueType() const {
860   return Node->getValueType(ResNo);
861 }
862 inline unsigned SDValue::getNumOperands() const {
863   return Node->getNumOperands();
864 }
865 inline const SDValue &SDValue::getOperand(unsigned i) const {
866   return Node->getOperand(i);
867 }
868 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
869   return Node->getConstantOperandVal(i);
870 }
871 inline bool SDValue::isTargetOpcode() const {
872   return Node->isTargetOpcode();
873 }
874 inline bool SDValue::isTargetMemoryOpcode() const {
875   return Node->isTargetMemoryOpcode();
876 }
877 inline bool SDValue::isMachineOpcode() const {
878   return Node->isMachineOpcode();
879 }
880 inline unsigned SDValue::getMachineOpcode() const {
881   return Node->getMachineOpcode();
882 }
883 inline bool SDValue::use_empty() const {
884   return !Node->hasAnyUseOfValue(ResNo);
885 }
886 inline bool SDValue::hasOneUse() const {
887   return Node->hasNUsesOfValue(1, ResNo);
888 }
889 inline const DebugLoc SDValue::getDebugLoc() const {
890   return Node->getDebugLoc();
891 }
892 inline void SDValue::dump() const {
893   return Node->dump();
894 }
895 inline void SDValue::dumpr() const {
896   return Node->dumpr();
897 }
898 // Define inline functions from the SDUse class.
899
900 inline void SDUse::set(const SDValue &V) {
901   if (Val.getNode()) removeFromList();
902   Val = V;
903   if (V.getNode()) V.getNode()->addUse(*this);
904 }
905
906 inline void SDUse::setInitial(const SDValue &V) {
907   Val = V;
908   V.getNode()->addUse(*this);
909 }
910
911 inline void SDUse::setNode(SDNode *N) {
912   if (Val.getNode()) removeFromList();
913   Val.setNode(N);
914   if (N) N->addUse(*this);
915 }
916
917 /// UnarySDNode - This class is used for single-operand SDNodes.  This is solely
918 /// to allow co-allocation of node operands with the node itself.
919 class UnarySDNode : public SDNode {
920   SDUse Op;
921 public:
922   UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
923               SDValue X)
924     : SDNode(Opc, Order, dl, VTs) {
925     InitOperands(&Op, X);
926   }
927 };
928
929 /// BinarySDNode - This class is used for two-operand SDNodes.  This is solely
930 /// to allow co-allocation of node operands with the node itself.
931 class BinarySDNode : public SDNode {
932   SDUse Ops[2];
933 public:
934   BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
935                SDValue X, SDValue Y)
936     : SDNode(Opc, Order, dl, VTs) {
937     InitOperands(Ops, X, Y);
938   }
939 };
940
941 /// TernarySDNode - This class is used for three-operand SDNodes. This is solely
942 /// to allow co-allocation of node operands with the node itself.
943 class TernarySDNode : public SDNode {
944   SDUse Ops[3];
945 public:
946   TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
947                 SDValue X, SDValue Y, SDValue Z)
948     : SDNode(Opc, Order, dl, VTs) {
949     InitOperands(Ops, X, Y, Z);
950   }
951 };
952
953
954 /// HandleSDNode - This class is used to form a handle around another node that
955 /// is persistent and is updated across invocations of replaceAllUsesWith on its
956 /// operand.  This node should be directly created by end-users and not added to
957 /// the AllNodes list.
958 class HandleSDNode : public SDNode {
959   SDUse Op;
960 public:
961   explicit HandleSDNode(SDValue X)
962     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
963     InitOperands(&Op, X);
964   }
965   ~HandleSDNode();
966   const SDValue &getValue() const { return Op; }
967 };
968
969 class AddrSpaceCastSDNode : public UnarySDNode {
970 private:
971   unsigned SrcAddrSpace;
972   unsigned DestAddrSpace;
973
974 public:
975   AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
976                       unsigned SrcAS, unsigned DestAS);
977
978   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
979   unsigned getDestAddressSpace() const { return DestAddrSpace; }
980
981   static bool classof(const SDNode *N) {
982     return N->getOpcode() == ISD::ADDRSPACECAST;
983   }
984 };
985
986 /// Abstact virtual class for operations for memory operations
987 class MemSDNode : public SDNode {
988 private:
989   // MemoryVT - VT of in-memory value.
990   EVT MemoryVT;
991
992 protected:
993   /// MMO - Memory reference information.
994   MachineMemOperand *MMO;
995
996 public:
997   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
998             EVT MemoryVT, MachineMemOperand *MMO);
999
1000   MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
1001             const SDValue *Ops,
1002             unsigned NumOps, 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                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.data(), Ops.size(), 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                            const SDValue *Ops, unsigned NumOps,
1691                            ISD::CvtCode Code)
1692     : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops, NumOps),
1693       CvtCode(Code) {
1694     assert(NumOps == 5 && "wrong number of operations");
1695   }
1696 public:
1697   ISD::CvtCode getCvtCode() const { return CvtCode; }
1698
1699   static bool classof(const SDNode *N) {
1700     return N->getOpcode() == ISD::CONVERT_RNDSAT;
1701   }
1702 };
1703
1704 /// VTSDNode - This class is used to represent EVT's, which are used
1705 /// to parameterize some operations.
1706 class VTSDNode : public SDNode {
1707   EVT ValueType;
1708   friend class SelectionDAG;
1709   explicit VTSDNode(EVT VT)
1710     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1711       ValueType(VT) {
1712   }
1713 public:
1714
1715   EVT getVT() const { return ValueType; }
1716
1717   static bool classof(const SDNode *N) {
1718     return N->getOpcode() == ISD::VALUETYPE;
1719   }
1720 };
1721
1722 /// LSBaseSDNode - Base class for LoadSDNode and StoreSDNode
1723 ///
1724 class LSBaseSDNode : public MemSDNode {
1725   //! Operand array for load and store
1726   /*!
1727     \note Moving this array to the base class captures more
1728     common functionality shared between LoadSDNode and
1729     StoreSDNode
1730    */
1731   SDUse Ops[4];
1732 public:
1733   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
1734                SDValue *Operands, unsigned numOperands,
1735                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1736                MachineMemOperand *MMO)
1737     : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1738     SubclassData |= AM << 2;
1739     assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
1740     InitOperands(Ops, Operands, numOperands);
1741     assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
1742            "Only indexed loads and stores have a non-undef offset operand");
1743   }
1744
1745   const SDValue &getOffset() const {
1746     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1747   }
1748
1749   /// getAddressingMode - Return the addressing mode for this load or store:
1750   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1751   ISD::MemIndexedMode getAddressingMode() const {
1752     return ISD::MemIndexedMode((SubclassData >> 2) & 7);
1753   }
1754
1755   /// isIndexed - Return true if this is a pre/post inc/dec load/store.
1756   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1757
1758   /// isUnindexed - Return true if this is NOT a pre/post inc/dec load/store.
1759   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1760
1761   static bool classof(const SDNode *N) {
1762     return N->getOpcode() == ISD::LOAD ||
1763            N->getOpcode() == ISD::STORE;
1764   }
1765 };
1766
1767 /// LoadSDNode - This class is used to represent ISD::LOAD nodes.
1768 ///
1769 class LoadSDNode : public LSBaseSDNode {
1770   friend class SelectionDAG;
1771   LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
1772              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1773              MachineMemOperand *MMO)
1774     : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
1775     SubclassData |= (unsigned short)ETy;
1776     assert(getExtensionType() == ETy && "LoadExtType encoding error!");
1777     assert(readMem() && "Load MachineMemOperand is not a load!");
1778     assert(!writeMem() && "Load MachineMemOperand is a store!");
1779   }
1780 public:
1781
1782   /// getExtensionType - Return whether this is a plain node,
1783   /// or one of the varieties of value-extending loads.
1784   ISD::LoadExtType getExtensionType() const {
1785     return ISD::LoadExtType(SubclassData & 3);
1786   }
1787
1788   const SDValue &getBasePtr() const { return getOperand(1); }
1789   const SDValue &getOffset() const { return getOperand(2); }
1790
1791   static bool classof(const SDNode *N) {
1792     return N->getOpcode() == ISD::LOAD;
1793   }
1794 };
1795
1796 /// StoreSDNode - This class is used to represent ISD::STORE nodes.
1797 ///
1798 class StoreSDNode : public LSBaseSDNode {
1799   friend class SelectionDAG;
1800   StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
1801               SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1802               MachineMemOperand *MMO)
1803     : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
1804                    VTs, AM, MemVT, MMO) {
1805     SubclassData |= (unsigned short)isTrunc;
1806     assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
1807     assert(!readMem() && "Store MachineMemOperand is a load!");
1808     assert(writeMem() && "Store MachineMemOperand is not a store!");
1809   }
1810 public:
1811
1812   /// isTruncatingStore - Return true if the op does a truncation before store.
1813   /// For integers this is the same as doing a TRUNCATE and storing the result.
1814   /// For floats, it is the same as doing an FP_ROUND and storing the result.
1815   bool isTruncatingStore() const { return SubclassData & 1; }
1816
1817   const SDValue &getValue() const { return getOperand(1); }
1818   const SDValue &getBasePtr() const { return getOperand(2); }
1819   const SDValue &getOffset() const { return getOperand(3); }
1820
1821   static bool classof(const SDNode *N) {
1822     return N->getOpcode() == ISD::STORE;
1823   }
1824 };
1825
1826 /// MachineSDNode - An SDNode that represents everything that will be needed
1827 /// to construct a MachineInstr. These nodes are created during the
1828 /// instruction selection proper phase.
1829 ///
1830 class MachineSDNode : public SDNode {
1831 public:
1832   typedef MachineMemOperand **mmo_iterator;
1833
1834 private:
1835   friend class SelectionDAG;
1836   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
1837     : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
1838
1839   /// LocalOperands - Operands for this instruction, if they fit here. If
1840   /// they don't, this field is unused.
1841   SDUse LocalOperands[4];
1842
1843   /// MemRefs - Memory reference descriptions for this instruction.
1844   mmo_iterator MemRefs;
1845   mmo_iterator MemRefsEnd;
1846
1847 public:
1848   mmo_iterator memoperands_begin() const { return MemRefs; }
1849   mmo_iterator memoperands_end() const { return MemRefsEnd; }
1850   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
1851
1852   /// setMemRefs - Assign this MachineSDNodes's memory reference descriptor
1853   /// list. This does not transfer ownership.
1854   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1855     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
1856       assert(*MMI && "Null mem ref detected!");
1857     MemRefs = NewMemRefs;
1858     MemRefsEnd = NewMemRefsEnd;
1859   }
1860
1861   static bool classof(const SDNode *N) {
1862     return N->isMachineOpcode();
1863   }
1864 };
1865
1866 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
1867                                             SDNode, ptrdiff_t> {
1868   const SDNode *Node;
1869   unsigned Operand;
1870
1871   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
1872 public:
1873   bool operator==(const SDNodeIterator& x) const {
1874     return Operand == x.Operand;
1875   }
1876   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
1877
1878   const SDNodeIterator &operator=(const SDNodeIterator &I) {
1879     assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
1880     Operand = I.Operand;
1881     return *this;
1882   }
1883
1884   pointer operator*() const {
1885     return Node->getOperand(Operand).getNode();
1886   }
1887   pointer operator->() const { return operator*(); }
1888
1889   SDNodeIterator& operator++() {                // Preincrement
1890     ++Operand;
1891     return *this;
1892   }
1893   SDNodeIterator operator++(int) { // Postincrement
1894     SDNodeIterator tmp = *this; ++*this; return tmp;
1895   }
1896   size_t operator-(SDNodeIterator Other) const {
1897     assert(Node == Other.Node &&
1898            "Cannot compare iterators of two different nodes!");
1899     return Operand - Other.Operand;
1900   }
1901
1902   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
1903   static SDNodeIterator end  (const SDNode *N) {
1904     return SDNodeIterator(N, N->getNumOperands());
1905   }
1906
1907   unsigned getOperand() const { return Operand; }
1908   const SDNode *getNode() const { return Node; }
1909 };
1910
1911 template <> struct GraphTraits<SDNode*> {
1912   typedef SDNode NodeType;
1913   typedef SDNodeIterator ChildIteratorType;
1914   static inline NodeType *getEntryNode(SDNode *N) { return N; }
1915   static inline ChildIteratorType child_begin(NodeType *N) {
1916     return SDNodeIterator::begin(N);
1917   }
1918   static inline ChildIteratorType child_end(NodeType *N) {
1919     return SDNodeIterator::end(N);
1920   }
1921 };
1922
1923 /// LargestSDNode - The largest SDNode class.
1924 ///
1925 typedef AtomicSDNode LargestSDNode;
1926
1927 /// MostAlignedSDNode - The SDNode class with the greatest alignment
1928 /// requirement.
1929 ///
1930 typedef GlobalAddressSDNode MostAlignedSDNode;
1931
1932 namespace ISD {
1933   /// isNormalLoad - Returns true if the specified node is a non-extending
1934   /// and unindexed load.
1935   inline bool isNormalLoad(const SDNode *N) {
1936     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
1937     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
1938       Ld->getAddressingMode() == ISD::UNINDEXED;
1939   }
1940
1941   /// isNON_EXTLoad - Returns true if the specified node is a non-extending
1942   /// load.
1943   inline bool isNON_EXTLoad(const SDNode *N) {
1944     return isa<LoadSDNode>(N) &&
1945       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
1946   }
1947
1948   /// isEXTLoad - Returns true if the specified node is a EXTLOAD.
1949   ///
1950   inline bool isEXTLoad(const SDNode *N) {
1951     return isa<LoadSDNode>(N) &&
1952       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
1953   }
1954
1955   /// isSEXTLoad - Returns true if the specified node is a SEXTLOAD.
1956   ///
1957   inline bool isSEXTLoad(const SDNode *N) {
1958     return isa<LoadSDNode>(N) &&
1959       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
1960   }
1961
1962   /// isZEXTLoad - Returns true if the specified node is a ZEXTLOAD.
1963   ///
1964   inline bool isZEXTLoad(const SDNode *N) {
1965     return isa<LoadSDNode>(N) &&
1966       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
1967   }
1968
1969   /// isUNINDEXEDLoad - Returns true if the specified node is an unindexed load.
1970   ///
1971   inline bool isUNINDEXEDLoad(const SDNode *N) {
1972     return isa<LoadSDNode>(N) &&
1973       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
1974   }
1975
1976   /// isNormalStore - Returns true if the specified node is a non-truncating
1977   /// and unindexed store.
1978   inline bool isNormalStore(const SDNode *N) {
1979     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
1980     return St && !St->isTruncatingStore() &&
1981       St->getAddressingMode() == ISD::UNINDEXED;
1982   }
1983
1984   /// isNON_TRUNCStore - Returns true if the specified node is a non-truncating
1985   /// store.
1986   inline bool isNON_TRUNCStore(const SDNode *N) {
1987     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
1988   }
1989
1990   /// isTRUNCStore - Returns true if the specified node is a truncating
1991   /// store.
1992   inline bool isTRUNCStore(const SDNode *N) {
1993     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
1994   }
1995
1996   /// isUNINDEXEDStore - Returns true if the specified node is an
1997   /// unindexed store.
1998   inline bool isUNINDEXEDStore(const SDNode *N) {
1999     return isa<StoreSDNode>(N) &&
2000       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2001   }
2002 }
2003
2004 } // end llvm namespace
2005
2006 #endif