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