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