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