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