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