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