]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, 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 is a strict floating point pseudo-op.
616   bool isStrictFPOpcode() {
617     switch (NodeType) {
618       default: 
619         return false;
620       case ISD::STRICT_FADD:
621       case ISD::STRICT_FSUB:
622       case ISD::STRICT_FMUL:
623       case ISD::STRICT_FDIV:
624       case ISD::STRICT_FREM:
625       case ISD::STRICT_FSQRT:
626       case ISD::STRICT_FPOW:
627       case ISD::STRICT_FPOWI:
628       case ISD::STRICT_FSIN:
629       case ISD::STRICT_FCOS:
630       case ISD::STRICT_FEXP:
631       case ISD::STRICT_FEXP2:
632       case ISD::STRICT_FLOG:
633       case ISD::STRICT_FLOG10:
634       case ISD::STRICT_FLOG2:
635       case ISD::STRICT_FRINT:
636       case ISD::STRICT_FNEARBYINT:
637         return true;
638     }
639   }
640
641   /// Test if this node has a post-isel opcode, directly
642   /// corresponding to a MachineInstr opcode.
643   bool isMachineOpcode() const { return NodeType < 0; }
644
645   /// This may only be called if isMachineOpcode returns
646   /// true. It returns the MachineInstr opcode value that the node's opcode
647   /// corresponds to.
648   unsigned getMachineOpcode() const {
649     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
650     return ~NodeType;
651   }
652
653   bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
654   void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
655
656   /// Return true if there are no uses of this node.
657   bool use_empty() const { return UseList == nullptr; }
658
659   /// Return true if there is exactly one use of this node.
660   bool hasOneUse() const {
661     return !use_empty() && std::next(use_begin()) == use_end();
662   }
663
664   /// Return the number of uses of this node. This method takes
665   /// time proportional to the number of uses.
666   size_t use_size() const { return std::distance(use_begin(), use_end()); }
667
668   /// Return the unique node id.
669   int getNodeId() const { return NodeId; }
670
671   /// Set unique node id.
672   void setNodeId(int Id) { NodeId = Id; }
673
674   /// Return the node ordering.
675   unsigned getIROrder() const { return IROrder; }
676
677   /// Set the node ordering.
678   void setIROrder(unsigned Order) { IROrder = Order; }
679
680   /// Return the source location info.
681   const DebugLoc &getDebugLoc() const { return debugLoc; }
682
683   /// Set source location info.  Try to avoid this, putting
684   /// it in the constructor is preferable.
685   void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
686
687   /// This class provides iterator support for SDUse
688   /// operands that use a specific SDNode.
689   class use_iterator
690     : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
691     friend class SDNode;
692
693     SDUse *Op = nullptr;
694
695     explicit use_iterator(SDUse *op) : Op(op) {}
696
697   public:
698     typedef std::iterator<std::forward_iterator_tag,
699                           SDUse, ptrdiff_t>::reference reference;
700     typedef std::iterator<std::forward_iterator_tag,
701                           SDUse, ptrdiff_t>::pointer pointer;
702
703     use_iterator() = default;
704     use_iterator(const use_iterator &I) : Op(I.Op) {}
705
706     bool operator==(const use_iterator &x) const {
707       return Op == x.Op;
708     }
709     bool operator!=(const use_iterator &x) const {
710       return !operator==(x);
711     }
712
713     /// Return true if this iterator is at the end of uses list.
714     bool atEnd() const { return Op == nullptr; }
715
716     // Iterator traversal: forward iteration only.
717     use_iterator &operator++() {          // Preincrement
718       assert(Op && "Cannot increment end iterator!");
719       Op = Op->getNext();
720       return *this;
721     }
722
723     use_iterator operator++(int) {        // Postincrement
724       use_iterator tmp = *this; ++*this; return tmp;
725     }
726
727     /// Retrieve a pointer to the current user node.
728     SDNode *operator*() const {
729       assert(Op && "Cannot dereference end iterator!");
730       return Op->getUser();
731     }
732
733     SDNode *operator->() const { return operator*(); }
734
735     SDUse &getUse() const { return *Op; }
736
737     /// Retrieve the operand # of this use in its user.
738     unsigned getOperandNo() const {
739       assert(Op && "Cannot dereference end iterator!");
740       return (unsigned)(Op - Op->getUser()->OperandList);
741     }
742   };
743
744   /// Provide iteration support to walk over all uses of an SDNode.
745   use_iterator use_begin() const {
746     return use_iterator(UseList);
747   }
748
749   static use_iterator use_end() { return use_iterator(nullptr); }
750
751   inline iterator_range<use_iterator> uses() {
752     return make_range(use_begin(), use_end());
753   }
754   inline iterator_range<use_iterator> uses() const {
755     return make_range(use_begin(), use_end());
756   }
757
758   /// Return true if there are exactly NUSES uses of the indicated value.
759   /// This method ignores uses of other values defined by this operation.
760   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
761
762   /// Return true if there are any use of the indicated value.
763   /// This method ignores uses of other values defined by this operation.
764   bool hasAnyUseOfValue(unsigned Value) const;
765
766   /// Return true if this node is the only use of N.
767   bool isOnlyUserOf(const SDNode *N) const;
768
769   /// Return true if this node is an operand of N.
770   bool isOperandOf(const SDNode *N) const;
771
772   /// Return true if this node is a predecessor of N.
773   /// NOTE: Implemented on top of hasPredecessor and every bit as
774   /// expensive. Use carefully.
775   bool isPredecessorOf(const SDNode *N) const {
776     return N->hasPredecessor(this);
777   }
778
779   /// Return true if N is a predecessor of this node.
780   /// N is either an operand of this node, or can be reached by recursively
781   /// traversing up the operands.
782   /// NOTE: This is an expensive method. Use it carefully.
783   bool hasPredecessor(const SDNode *N) const;
784
785   /// Returns true if N is a predecessor of any node in Worklist. This
786   /// helper keeps Visited and Worklist sets externally to allow unions
787   /// searches to be performed in parallel, caching of results across
788   /// queries and incremental addition to Worklist. Stops early if N is
789   /// found but will resume. Remember to clear Visited and Worklists
790   /// if DAG changes.
791   static bool hasPredecessorHelper(const SDNode *N,
792                                    SmallPtrSetImpl<const SDNode *> &Visited,
793                                    SmallVectorImpl<const SDNode *> &Worklist) {
794     if (Visited.count(N))
795       return true;
796     while (!Worklist.empty()) {
797       const SDNode *M = Worklist.pop_back_val();
798       bool Found = false;
799       for (const SDValue &OpV : M->op_values()) {
800         SDNode *Op = OpV.getNode();
801         if (Visited.insert(Op).second)
802           Worklist.push_back(Op);
803         if (Op == N)
804           Found = true;
805       }
806       if (Found)
807         return true;
808     }
809     return false;
810   }
811
812   /// Return true if all the users of N are contained in Nodes.
813   /// NOTE: Requires at least one match, but doesn't require them all.
814   static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
815
816   /// Return the number of values used by this operation.
817   unsigned getNumOperands() const { return NumOperands; }
818
819   /// Helper method returns the integer value of a ConstantSDNode operand.
820   inline uint64_t getConstantOperandVal(unsigned Num) const;
821
822   const SDValue &getOperand(unsigned Num) const {
823     assert(Num < NumOperands && "Invalid child # of SDNode!");
824     return OperandList[Num];
825   }
826
827   typedef SDUse* op_iterator;
828
829   op_iterator op_begin() const { return OperandList; }
830   op_iterator op_end() const { return OperandList+NumOperands; }
831   ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
832
833   /// Iterator for directly iterating over the operand SDValue's.
834   struct value_op_iterator
835       : iterator_adaptor_base<value_op_iterator, op_iterator,
836                               std::random_access_iterator_tag, SDValue,
837                               ptrdiff_t, value_op_iterator *,
838                               value_op_iterator *> {
839     explicit value_op_iterator(SDUse *U = nullptr)
840       : iterator_adaptor_base(U) {}
841
842     const SDValue &operator*() const { return I->get(); }
843   };
844
845   iterator_range<value_op_iterator> op_values() const {
846     return make_range(value_op_iterator(op_begin()),
847                       value_op_iterator(op_end()));
848   }
849
850   SDVTList getVTList() const {
851     SDVTList X = { ValueList, NumValues };
852     return X;
853   }
854
855   /// If this node has a glue operand, return the node
856   /// to which the glue operand points. Otherwise return NULL.
857   SDNode *getGluedNode() const {
858     if (getNumOperands() != 0 &&
859         getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
860       return getOperand(getNumOperands()-1).getNode();
861     return nullptr;
862   }
863
864   /// If this node has a glue value with a user, return
865   /// the user (there is at most one). Otherwise return NULL.
866   SDNode *getGluedUser() const {
867     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
868       if (UI.getUse().get().getValueType() == MVT::Glue)
869         return *UI;
870     return nullptr;
871   }
872
873   const SDNodeFlags getFlags() const { return Flags; }
874   void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
875
876   /// Clear any flags in this node that aren't also set in Flags.
877   /// If Flags is not in a defined state then this has no effect.
878   void intersectFlagsWith(const SDNodeFlags Flags);
879
880   /// Return the number of values defined/returned by this operator.
881   unsigned getNumValues() const { return NumValues; }
882
883   /// Return the type of a specified result.
884   EVT getValueType(unsigned ResNo) const {
885     assert(ResNo < NumValues && "Illegal result number!");
886     return ValueList[ResNo];
887   }
888
889   /// Return the type of a specified result as a simple type.
890   MVT getSimpleValueType(unsigned ResNo) const {
891     return getValueType(ResNo).getSimpleVT();
892   }
893
894   /// Returns MVT::getSizeInBits(getValueType(ResNo)).
895   unsigned getValueSizeInBits(unsigned ResNo) const {
896     return getValueType(ResNo).getSizeInBits();
897   }
898
899   typedef const EVT* value_iterator;
900   value_iterator value_begin() const { return ValueList; }
901   value_iterator value_end() const { return ValueList+NumValues; }
902
903   /// Return the opcode of this operation for printing.
904   std::string getOperationName(const SelectionDAG *G = nullptr) const;
905   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
906   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
907   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
908   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
909   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
910
911   /// Print a SelectionDAG node and all children down to
912   /// the leaves.  The given SelectionDAG allows target-specific nodes
913   /// to be printed in human-readable form.  Unlike printr, this will
914   /// print the whole DAG, including children that appear multiple
915   /// times.
916   ///
917   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
918
919   /// Print a SelectionDAG node and children up to
920   /// depth "depth."  The given SelectionDAG allows target-specific
921   /// nodes to be printed in human-readable form.  Unlike printr, this
922   /// will print children that appear multiple times wherever they are
923   /// used.
924   ///
925   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
926                        unsigned depth = 100) const;
927
928   /// Dump this node, for debugging.
929   void dump() const;
930
931   /// Dump (recursively) this node and its use-def subgraph.
932   void dumpr() const;
933
934   /// Dump this node, for debugging.
935   /// The given SelectionDAG allows target-specific nodes to be printed
936   /// in human-readable form.
937   void dump(const SelectionDAG *G) const;
938
939   /// Dump (recursively) this node and its use-def subgraph.
940   /// The given SelectionDAG allows target-specific nodes to be printed
941   /// in human-readable form.
942   void dumpr(const SelectionDAG *G) const;
943
944   /// printrFull to dbgs().  The given SelectionDAG allows
945   /// target-specific nodes to be printed in human-readable form.
946   /// Unlike dumpr, this will print the whole DAG, including children
947   /// that appear multiple times.
948   void dumprFull(const SelectionDAG *G = nullptr) const;
949
950   /// printrWithDepth to dbgs().  The given
951   /// SelectionDAG allows target-specific nodes to be printed in
952   /// human-readable form.  Unlike dumpr, this will print children
953   /// that appear multiple times wherever they are used.
954   ///
955   void dumprWithDepth(const SelectionDAG *G = nullptr,
956                       unsigned depth = 100) const;
957
958   /// Gather unique data for the node.
959   void Profile(FoldingSetNodeID &ID) const;
960
961   /// This method should only be used by the SDUse class.
962   void addUse(SDUse &U) { U.addToList(&UseList); }
963
964 protected:
965   static SDVTList getSDVTList(EVT VT) {
966     SDVTList Ret = { getValueTypeList(VT), 1 };
967     return Ret;
968   }
969
970   /// Create an SDNode.
971   ///
972   /// SDNodes are created without any operands, and never own the operand
973   /// storage. To add operands, see SelectionDAG::createOperands.
974   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
975       : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
976         IROrder(Order), debugLoc(std::move(dl)) {
977     memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
978     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
979     assert(NumValues == VTs.NumVTs &&
980            "NumValues wasn't wide enough for its operands!");
981   }
982
983   /// Release the operands and set this node to have zero operands.
984   void DropOperands();
985 };
986
987 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
988 /// into SDNode creation functions.
989 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
990 /// from the original Instruction, and IROrder is the ordinal position of
991 /// the instruction.
992 /// When an SDNode is created after the DAG is being built, both DebugLoc and
993 /// the IROrder are propagated from the original SDNode.
994 /// So SDLoc class provides two constructors besides the default one, one to
995 /// be used by the DAGBuilder, the other to be used by others.
996 class SDLoc {
997 private:
998   DebugLoc DL;
999   int IROrder = 0;
1000
1001 public:
1002   SDLoc() = default;
1003   SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1004   SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1005   SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1006     assert(Order >= 0 && "bad IROrder");
1007     if (I)
1008       DL = I->getDebugLoc();
1009   }
1010
1011   unsigned getIROrder() const { return IROrder; }
1012   const DebugLoc &getDebugLoc() const { return DL; }
1013 };
1014
1015 // Define inline functions from the SDValue class.
1016
1017 inline SDValue::SDValue(SDNode *node, unsigned resno)
1018     : Node(node), ResNo(resno) {
1019   // Explicitly check for !ResNo to avoid use-after-free, because there are
1020   // callers that use SDValue(N, 0) with a deleted N to indicate successful
1021   // combines.
1022   assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&
1023          "Invalid result number for the given node!");
1024   assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
1025 }
1026
1027 inline unsigned SDValue::getOpcode() const {
1028   return Node->getOpcode();
1029 }
1030
1031 inline EVT SDValue::getValueType() const {
1032   return Node->getValueType(ResNo);
1033 }
1034
1035 inline unsigned SDValue::getNumOperands() const {
1036   return Node->getNumOperands();
1037 }
1038
1039 inline const SDValue &SDValue::getOperand(unsigned i) const {
1040   return Node->getOperand(i);
1041 }
1042
1043 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1044   return Node->getConstantOperandVal(i);
1045 }
1046
1047 inline bool SDValue::isTargetOpcode() const {
1048   return Node->isTargetOpcode();
1049 }
1050
1051 inline bool SDValue::isTargetMemoryOpcode() const {
1052   return Node->isTargetMemoryOpcode();
1053 }
1054
1055 inline bool SDValue::isMachineOpcode() const {
1056   return Node->isMachineOpcode();
1057 }
1058
1059 inline unsigned SDValue::getMachineOpcode() const {
1060   return Node->getMachineOpcode();
1061 }
1062
1063 inline bool SDValue::isUndef() const {
1064   return Node->isUndef();
1065 }
1066
1067 inline bool SDValue::use_empty() const {
1068   return !Node->hasAnyUseOfValue(ResNo);
1069 }
1070
1071 inline bool SDValue::hasOneUse() const {
1072   return Node->hasNUsesOfValue(1, ResNo);
1073 }
1074
1075 inline const DebugLoc &SDValue::getDebugLoc() const {
1076   return Node->getDebugLoc();
1077 }
1078
1079 inline void SDValue::dump() const {
1080   return Node->dump();
1081 }
1082
1083 inline void SDValue::dumpr() const {
1084   return Node->dumpr();
1085 }
1086
1087 // Define inline functions from the SDUse class.
1088
1089 inline void SDUse::set(const SDValue &V) {
1090   if (Val.getNode()) removeFromList();
1091   Val = V;
1092   if (V.getNode()) V.getNode()->addUse(*this);
1093 }
1094
1095 inline void SDUse::setInitial(const SDValue &V) {
1096   Val = V;
1097   V.getNode()->addUse(*this);
1098 }
1099
1100 inline void SDUse::setNode(SDNode *N) {
1101   if (Val.getNode()) removeFromList();
1102   Val.setNode(N);
1103   if (N) N->addUse(*this);
1104 }
1105
1106 /// This class is used to form a handle around another node that
1107 /// is persistent and is updated across invocations of replaceAllUsesWith on its
1108 /// operand.  This node should be directly created by end-users and not added to
1109 /// the AllNodes list.
1110 class HandleSDNode : public SDNode {
1111   SDUse Op;
1112
1113 public:
1114   explicit HandleSDNode(SDValue X)
1115     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1116     // HandleSDNodes are never inserted into the DAG, so they won't be
1117     // auto-numbered. Use ID 65535 as a sentinel.
1118     PersistentId = 0xffff;
1119
1120     // Manually set up the operand list. This node type is special in that it's
1121     // always stack allocated and SelectionDAG does not manage its operands.
1122     // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1123     // be so special.
1124     Op.setUser(this);
1125     Op.setInitial(X);
1126     NumOperands = 1;
1127     OperandList = &Op;
1128   }
1129   ~HandleSDNode();
1130
1131   const SDValue &getValue() const { return Op; }
1132 };
1133
1134 class AddrSpaceCastSDNode : public SDNode {
1135 private:
1136   unsigned SrcAddrSpace;
1137   unsigned DestAddrSpace;
1138
1139 public:
1140   AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1141                       unsigned SrcAS, unsigned DestAS);
1142
1143   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1144   unsigned getDestAddressSpace() const { return DestAddrSpace; }
1145
1146   static bool classof(const SDNode *N) {
1147     return N->getOpcode() == ISD::ADDRSPACECAST;
1148   }
1149 };
1150
1151 /// This is an abstract virtual class for memory operations.
1152 class MemSDNode : public SDNode {
1153 private:
1154   // VT of in-memory value.
1155   EVT MemoryVT;
1156
1157 protected:
1158   /// Memory reference information.
1159   MachineMemOperand *MMO;
1160
1161 public:
1162   MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1163             EVT MemoryVT, MachineMemOperand *MMO);
1164
1165   bool readMem() const { return MMO->isLoad(); }
1166   bool writeMem() const { return MMO->isStore(); }
1167
1168   /// Returns alignment and volatility of the memory access
1169   unsigned getOriginalAlignment() const {
1170     return MMO->getBaseAlignment();
1171   }
1172   unsigned getAlignment() const {
1173     return MMO->getAlignment();
1174   }
1175
1176   /// Return the SubclassData value, without HasDebugValue. This contains an
1177   /// encoding of the volatile flag, as well as bits used by subclasses. This
1178   /// function should only be used to compute a FoldingSetNodeID value.
1179   /// The HasDebugValue bit is masked out because CSE map needs to match
1180   /// nodes with debug info with nodes without debug info.
1181   unsigned getRawSubclassData() const {
1182     uint16_t Data;
1183     union {
1184       char RawSDNodeBits[sizeof(uint16_t)];
1185       SDNodeBitfields SDNodeBits;
1186     };
1187     memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1188     SDNodeBits.HasDebugValue = 0;
1189     memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1190     return Data;
1191   }
1192
1193   bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1194   bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1195   bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1196   bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1197
1198   // Returns the offset from the location of the access.
1199   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1200
1201   /// Returns the AA info that describes the dereference.
1202   AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1203
1204   /// Returns the Ranges that describes the dereference.
1205   const MDNode *getRanges() const { return MMO->getRanges(); }
1206
1207   /// Return the synchronization scope for this memory operation.
1208   SynchronizationScope getSynchScope() const { return MMO->getSynchScope(); }
1209
1210   /// Return the atomic ordering requirements for this memory operation. For
1211   /// cmpxchg atomic operations, return the atomic ordering requirements when
1212   /// store occurs.
1213   AtomicOrdering getOrdering() const { return MMO->getOrdering(); }
1214
1215   /// Return the type of the in-memory value.
1216   EVT getMemoryVT() const { return MemoryVT; }
1217
1218   /// Return a MachineMemOperand object describing the memory
1219   /// reference performed by operation.
1220   MachineMemOperand *getMemOperand() const { return MMO; }
1221
1222   const MachinePointerInfo &getPointerInfo() const {
1223     return MMO->getPointerInfo();
1224   }
1225
1226   /// Return the address space for the associated pointer
1227   unsigned getAddressSpace() const {
1228     return getPointerInfo().getAddrSpace();
1229   }
1230
1231   /// Update this MemSDNode's MachineMemOperand information
1232   /// to reflect the alignment of NewMMO, if it has a greater alignment.
1233   /// This must only be used when the new alignment applies to all users of
1234   /// this MachineMemOperand.
1235   void refineAlignment(const MachineMemOperand *NewMMO) {
1236     MMO->refineAlignment(NewMMO);
1237   }
1238
1239   const SDValue &getChain() const { return getOperand(0); }
1240   const SDValue &getBasePtr() const {
1241     return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1242   }
1243
1244   // Methods to support isa and dyn_cast
1245   static bool classof(const SDNode *N) {
1246     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1247     // with either an intrinsic or a target opcode.
1248     return N->getOpcode() == ISD::LOAD                ||
1249            N->getOpcode() == ISD::STORE               ||
1250            N->getOpcode() == ISD::PREFETCH            ||
1251            N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1252            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1253            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1254            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1255            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1256            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1257            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1258            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1259            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1260            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1261            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1262            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1263            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1264            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1265            N->getOpcode() == ISD::ATOMIC_STORE        ||
1266            N->getOpcode() == ISD::MLOAD               ||
1267            N->getOpcode() == ISD::MSTORE              ||
1268            N->getOpcode() == ISD::MGATHER             ||
1269            N->getOpcode() == ISD::MSCATTER            ||
1270            N->isMemIntrinsic()                        ||
1271            N->isTargetMemoryOpcode();
1272   }
1273 };
1274
1275 /// This is an SDNode representing atomic operations.
1276 class AtomicSDNode : public MemSDNode {
1277 public:
1278   AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
1279                EVT MemVT, MachineMemOperand *MMO)
1280       : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {}
1281
1282   const SDValue &getBasePtr() const { return getOperand(1); }
1283   const SDValue &getVal() const { return getOperand(2); }
1284
1285   /// Returns true if this SDNode represents cmpxchg atomic operation, false
1286   /// otherwise.
1287   bool isCompareAndSwap() const {
1288     unsigned Op = getOpcode();
1289     return Op == ISD::ATOMIC_CMP_SWAP ||
1290            Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1291   }
1292
1293   /// For cmpxchg atomic operations, return the atomic ordering requirements
1294   /// when store does not occur.
1295   AtomicOrdering getFailureOrdering() const {
1296     assert(isCompareAndSwap() && "Must be cmpxchg operation");
1297     return MMO->getFailureOrdering();
1298   }
1299
1300   // Methods to support isa and dyn_cast
1301   static bool classof(const SDNode *N) {
1302     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1303            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1304            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1305            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1306            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1307            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1308            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1309            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1310            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1311            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1312            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1313            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1314            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1315            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1316            N->getOpcode() == ISD::ATOMIC_STORE;
1317   }
1318 };
1319
1320 /// This SDNode is used for target intrinsics that touch
1321 /// memory and need an associated MachineMemOperand. Its opcode may be
1322 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1323 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1324 class MemIntrinsicSDNode : public MemSDNode {
1325 public:
1326   MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
1327                      SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
1328       : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
1329     SDNodeBits.IsMemIntrinsic = true;
1330   }
1331
1332   // Methods to support isa and dyn_cast
1333   static bool classof(const SDNode *N) {
1334     // We lower some target intrinsics to their target opcode
1335     // early a node with a target opcode can be of this class
1336     return N->isMemIntrinsic()             ||
1337            N->getOpcode() == ISD::PREFETCH ||
1338            N->isTargetMemoryOpcode();
1339   }
1340 };
1341
1342 /// This SDNode is used to implement the code generator
1343 /// support for the llvm IR shufflevector instruction.  It combines elements
1344 /// from two input vectors into a new input vector, with the selection and
1345 /// ordering of elements determined by an array of integers, referred to as
1346 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
1347 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1348 /// An index of -1 is treated as undef, such that the code generator may put
1349 /// any value in the corresponding element of the result.
1350 class ShuffleVectorSDNode : public SDNode {
1351   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1352   // is freed when the SelectionDAG object is destroyed.
1353   const int *Mask;
1354
1355 protected:
1356   friend class SelectionDAG;
1357
1358   ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
1359       : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
1360
1361 public:
1362   ArrayRef<int> getMask() const {
1363     EVT VT = getValueType(0);
1364     return makeArrayRef(Mask, VT.getVectorNumElements());
1365   }
1366
1367   int getMaskElt(unsigned Idx) const {
1368     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1369     return Mask[Idx];
1370   }
1371
1372   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1373
1374   int  getSplatIndex() const {
1375     assert(isSplat() && "Cannot get splat index for non-splat!");
1376     EVT VT = getValueType(0);
1377     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
1378       if (Mask[i] >= 0)
1379         return Mask[i];
1380     }
1381     llvm_unreachable("Splat with all undef indices?");
1382   }
1383
1384   static bool isSplatMask(const int *Mask, EVT VT);
1385
1386   /// Change values in a shuffle permute mask assuming
1387   /// the two vector operands have swapped position.
1388   static void commuteMask(MutableArrayRef<int> Mask) {
1389     unsigned NumElems = Mask.size();
1390     for (unsigned i = 0; i != NumElems; ++i) {
1391       int idx = Mask[i];
1392       if (idx < 0)
1393         continue;
1394       else if (idx < (int)NumElems)
1395         Mask[i] = idx + NumElems;
1396       else
1397         Mask[i] = idx - NumElems;
1398     }
1399   }
1400
1401   static bool classof(const SDNode *N) {
1402     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1403   }
1404 };
1405
1406 class ConstantSDNode : public SDNode {
1407   friend class SelectionDAG;
1408
1409   const ConstantInt *Value;
1410
1411   ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val,
1412                  const DebugLoc &DL, EVT VT)
1413       : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DL,
1414                getSDVTList(VT)),
1415         Value(val) {
1416     ConstantSDNodeBits.IsOpaque = isOpaque;
1417   }
1418
1419 public:
1420   const ConstantInt *getConstantIntValue() const { return Value; }
1421   const APInt &getAPIntValue() const { return Value->getValue(); }
1422   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1423   int64_t getSExtValue() const { return Value->getSExtValue(); }
1424
1425   bool isOne() const { return Value->isOne(); }
1426   bool isNullValue() const { return Value->isNullValue(); }
1427   bool isAllOnesValue() const { return Value->isAllOnesValue(); }
1428
1429   bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
1430
1431   static bool classof(const SDNode *N) {
1432     return N->getOpcode() == ISD::Constant ||
1433            N->getOpcode() == ISD::TargetConstant;
1434   }
1435 };
1436
1437 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
1438   return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
1439 }
1440
1441 class ConstantFPSDNode : public SDNode {
1442   friend class SelectionDAG;
1443
1444   const ConstantFP *Value;
1445
1446   ConstantFPSDNode(bool isTarget, const ConstantFP *val, const DebugLoc &DL,
1447                    EVT VT)
1448       : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0, DL,
1449                getSDVTList(VT)),
1450         Value(val) {}
1451
1452 public:
1453   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1454   const ConstantFP *getConstantFPValue() const { return Value; }
1455
1456   /// Return true if the value is positive or negative zero.
1457   bool isZero() const { return Value->isZero(); }
1458
1459   /// Return true if the value is a NaN.
1460   bool isNaN() const { return Value->isNaN(); }
1461
1462   /// Return true if the value is an infinity
1463   bool isInfinity() const { return Value->isInfinity(); }
1464
1465   /// Return true if the value is negative.
1466   bool isNegative() const { return Value->isNegative(); }
1467
1468   /// We don't rely on operator== working on double values, as
1469   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1470   /// As such, this method can be used to do an exact bit-for-bit comparison of
1471   /// two floating point values.
1472
1473   /// We leave the version with the double argument here because it's just so
1474   /// convenient to write "2.0" and the like.  Without this function we'd
1475   /// have to duplicate its logic everywhere it's called.
1476   bool isExactlyValue(double V) const {
1477     bool ignored;
1478     APFloat Tmp(V);
1479     Tmp.convert(Value->getValueAPF().getSemantics(),
1480                 APFloat::rmNearestTiesToEven, &ignored);
1481     return isExactlyValue(Tmp);
1482   }
1483   bool isExactlyValue(const APFloat& V) const;
1484
1485   static bool isValueValidForType(EVT VT, const APFloat& Val);
1486
1487   static bool classof(const SDNode *N) {
1488     return N->getOpcode() == ISD::ConstantFP ||
1489            N->getOpcode() == ISD::TargetConstantFP;
1490   }
1491 };
1492
1493 /// Returns true if \p V is a constant integer zero.
1494 bool isNullConstant(SDValue V);
1495
1496 /// Returns true if \p V is an FP constant with a value of positive zero.
1497 bool isNullFPConstant(SDValue V);
1498
1499 /// Returns true if \p V is an integer constant with all bits set.
1500 bool isAllOnesConstant(SDValue V);
1501
1502 /// Returns true if \p V is a constant integer one.
1503 bool isOneConstant(SDValue V);
1504
1505 /// Returns true if \p V is a bitwise not operation. Assumes that an all ones
1506 /// constant is canonicalized to be operand 1.
1507 bool isBitwiseNot(SDValue V);
1508
1509 /// Returns the SDNode if it is a constant splat BuildVector or constant int.
1510 ConstantSDNode *isConstOrConstSplat(SDValue V);
1511
1512 /// Returns the SDNode if it is a constant splat BuildVector or constant float.
1513 ConstantFPSDNode *isConstOrConstSplatFP(SDValue V);
1514
1515 class GlobalAddressSDNode : public SDNode {
1516   friend class SelectionDAG;
1517
1518   const GlobalValue *TheGlobal;
1519   int64_t Offset;
1520   unsigned char TargetFlags;
1521
1522   GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
1523                       const GlobalValue *GA, EVT VT, int64_t o,
1524                       unsigned char TargetFlags);
1525
1526 public:
1527   const GlobalValue *getGlobal() const { return TheGlobal; }
1528   int64_t getOffset() const { return Offset; }
1529   unsigned char getTargetFlags() const { return TargetFlags; }
1530   // Return the address space this GlobalAddress belongs to.
1531   unsigned getAddressSpace() const;
1532
1533   static bool classof(const SDNode *N) {
1534     return N->getOpcode() == ISD::GlobalAddress ||
1535            N->getOpcode() == ISD::TargetGlobalAddress ||
1536            N->getOpcode() == ISD::GlobalTLSAddress ||
1537            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1538   }
1539 };
1540
1541 class FrameIndexSDNode : public SDNode {
1542   friend class SelectionDAG;
1543
1544   int FI;
1545
1546   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1547     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1548       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1549   }
1550
1551 public:
1552   int getIndex() const { return FI; }
1553
1554   static bool classof(const SDNode *N) {
1555     return N->getOpcode() == ISD::FrameIndex ||
1556            N->getOpcode() == ISD::TargetFrameIndex;
1557   }
1558 };
1559
1560 class JumpTableSDNode : public SDNode {
1561   friend class SelectionDAG;
1562
1563   int JTI;
1564   unsigned char TargetFlags;
1565
1566   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
1567     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1568       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1569   }
1570
1571 public:
1572   int getIndex() const { return JTI; }
1573   unsigned char getTargetFlags() const { return TargetFlags; }
1574
1575   static bool classof(const SDNode *N) {
1576     return N->getOpcode() == ISD::JumpTable ||
1577            N->getOpcode() == ISD::TargetJumpTable;
1578   }
1579 };
1580
1581 class ConstantPoolSDNode : public SDNode {
1582   friend class SelectionDAG;
1583
1584   union {
1585     const Constant *ConstVal;
1586     MachineConstantPoolValue *MachineCPVal;
1587   } Val;
1588   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1589   unsigned Alignment;  // Minimum alignment requirement of CP (not log2 value).
1590   unsigned char TargetFlags;
1591
1592   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1593                      unsigned Align, unsigned char TF)
1594     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1595              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1596              TargetFlags(TF) {
1597     assert(Offset >= 0 && "Offset is too large");
1598     Val.ConstVal = c;
1599   }
1600
1601   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1602                      EVT VT, int o, unsigned Align, unsigned char TF)
1603     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1604              DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1605              TargetFlags(TF) {
1606     assert(Offset >= 0 && "Offset is too large");
1607     Val.MachineCPVal = v;
1608     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1609   }
1610
1611 public:
1612   bool isMachineConstantPoolEntry() const {
1613     return Offset < 0;
1614   }
1615
1616   const Constant *getConstVal() const {
1617     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1618     return Val.ConstVal;
1619   }
1620
1621   MachineConstantPoolValue *getMachineCPVal() const {
1622     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1623     return Val.MachineCPVal;
1624   }
1625
1626   int getOffset() const {
1627     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1628   }
1629
1630   // Return the alignment of this constant pool object, which is either 0 (for
1631   // default alignment) or the desired value.
1632   unsigned getAlignment() const { return Alignment; }
1633   unsigned char getTargetFlags() const { return TargetFlags; }
1634
1635   Type *getType() const;
1636
1637   static bool classof(const SDNode *N) {
1638     return N->getOpcode() == ISD::ConstantPool ||
1639            N->getOpcode() == ISD::TargetConstantPool;
1640   }
1641 };
1642
1643 /// Completely target-dependent object reference.
1644 class TargetIndexSDNode : public SDNode {
1645   friend class SelectionDAG;
1646
1647   unsigned char TargetFlags;
1648   int Index;
1649   int64_t Offset;
1650
1651 public:
1652   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
1653     : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1654       TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1655
1656   unsigned char getTargetFlags() const { return TargetFlags; }
1657   int getIndex() const { return Index; }
1658   int64_t getOffset() const { return Offset; }
1659
1660   static bool classof(const SDNode *N) {
1661     return N->getOpcode() == ISD::TargetIndex;
1662   }
1663 };
1664
1665 class BasicBlockSDNode : public SDNode {
1666   friend class SelectionDAG;
1667
1668   MachineBasicBlock *MBB;
1669
1670   /// Debug info is meaningful and potentially useful here, but we create
1671   /// blocks out of order when they're jumped to, which makes it a bit
1672   /// harder.  Let's see if we need it first.
1673   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1674     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1675   {}
1676
1677 public:
1678   MachineBasicBlock *getBasicBlock() const { return MBB; }
1679
1680   static bool classof(const SDNode *N) {
1681     return N->getOpcode() == ISD::BasicBlock;
1682   }
1683 };
1684
1685 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1686 class BuildVectorSDNode : public SDNode {
1687 public:
1688   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1689   explicit BuildVectorSDNode() = delete;
1690
1691   /// Check if this is a constant splat, and if so, find the
1692   /// smallest element size that splats the vector.  If MinSplatBits is
1693   /// nonzero, the element size must be at least that large.  Note that the
1694   /// splat element may be the entire vector (i.e., a one element vector).
1695   /// Returns the splat element value in SplatValue.  Any undefined bits in
1696   /// that value are zero, and the corresponding bits in the SplatUndef mask
1697   /// are set.  The SplatBitSize value is set to the splat element size in
1698   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
1699   /// undefined.  isBigEndian describes the endianness of the target.
1700   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1701                        unsigned &SplatBitSize, bool &HasAnyUndefs,
1702                        unsigned MinSplatBits = 0,
1703                        bool isBigEndian = false) const;
1704
1705   /// \brief Returns the splatted value or a null value if this is not a splat.
1706   ///
1707   /// If passed a non-null UndefElements bitvector, it will resize it to match
1708   /// the vector width and set the bits where elements are undef.
1709   SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1710
1711   /// \brief Returns the splatted constant or null if this is not a constant
1712   /// splat.
1713   ///
1714   /// If passed a non-null UndefElements bitvector, it will resize it to match
1715   /// the vector width and set the bits where elements are undef.
1716   ConstantSDNode *
1717   getConstantSplatNode(BitVector *UndefElements = nullptr) const;
1718
1719   /// \brief Returns the splatted constant FP or null if this is not a constant
1720   /// FP splat.
1721   ///
1722   /// If passed a non-null UndefElements bitvector, it will resize it to match
1723   /// the vector width and set the bits where elements are undef.
1724   ConstantFPSDNode *
1725   getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
1726
1727   /// \brief If this is a constant FP splat and the splatted constant FP is an
1728   /// exact power or 2, return the log base 2 integer value.  Otherwise,
1729   /// return -1.
1730   ///
1731   /// The BitWidth specifies the necessary bit precision.
1732   int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
1733                                           uint32_t BitWidth) const;
1734
1735   bool isConstant() const;
1736
1737   static inline bool classof(const SDNode *N) {
1738     return N->getOpcode() == ISD::BUILD_VECTOR;
1739   }
1740 };
1741
1742 /// An SDNode that holds an arbitrary LLVM IR Value. This is
1743 /// used when the SelectionDAG needs to make a simple reference to something
1744 /// in the LLVM IR representation.
1745 ///
1746 class SrcValueSDNode : public SDNode {
1747   friend class SelectionDAG;
1748
1749   const Value *V;
1750
1751   /// Create a SrcValue for a general value.
1752   explicit SrcValueSDNode(const Value *v)
1753     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
1754
1755 public:
1756   /// Return the contained Value.
1757   const Value *getValue() const { return V; }
1758
1759   static bool classof(const SDNode *N) {
1760     return N->getOpcode() == ISD::SRCVALUE;
1761   }
1762 };
1763
1764 class MDNodeSDNode : public SDNode {
1765   friend class SelectionDAG;
1766
1767   const MDNode *MD;
1768
1769   explicit MDNodeSDNode(const MDNode *md)
1770   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
1771   {}
1772
1773 public:
1774   const MDNode *getMD() const { return MD; }
1775
1776   static bool classof(const SDNode *N) {
1777     return N->getOpcode() == ISD::MDNODE_SDNODE;
1778   }
1779 };
1780
1781 class RegisterSDNode : public SDNode {
1782   friend class SelectionDAG;
1783
1784   unsigned Reg;
1785
1786   RegisterSDNode(unsigned reg, EVT VT)
1787     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
1788
1789 public:
1790   unsigned getReg() const { return Reg; }
1791
1792   static bool classof(const SDNode *N) {
1793     return N->getOpcode() == ISD::Register;
1794   }
1795 };
1796
1797 class RegisterMaskSDNode : public SDNode {
1798   friend class SelectionDAG;
1799
1800   // The memory for RegMask is not owned by the node.
1801   const uint32_t *RegMask;
1802
1803   RegisterMaskSDNode(const uint32_t *mask)
1804     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
1805       RegMask(mask) {}
1806
1807 public:
1808   const uint32_t *getRegMask() const { return RegMask; }
1809
1810   static bool classof(const SDNode *N) {
1811     return N->getOpcode() == ISD::RegisterMask;
1812   }
1813 };
1814
1815 class BlockAddressSDNode : public SDNode {
1816   friend class SelectionDAG;
1817
1818   const BlockAddress *BA;
1819   int64_t Offset;
1820   unsigned char TargetFlags;
1821
1822   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
1823                      int64_t o, unsigned char Flags)
1824     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
1825              BA(ba), Offset(o), TargetFlags(Flags) {
1826   }
1827
1828 public:
1829   const BlockAddress *getBlockAddress() const { return BA; }
1830   int64_t getOffset() const { return Offset; }
1831   unsigned char getTargetFlags() const { return TargetFlags; }
1832
1833   static bool classof(const SDNode *N) {
1834     return N->getOpcode() == ISD::BlockAddress ||
1835            N->getOpcode() == ISD::TargetBlockAddress;
1836   }
1837 };
1838
1839 class EHLabelSDNode : public SDNode {
1840   friend class SelectionDAG;
1841
1842   MCSymbol *Label;
1843
1844   EHLabelSDNode(unsigned Order, const DebugLoc &dl, MCSymbol *L)
1845       : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {}
1846
1847 public:
1848   MCSymbol *getLabel() const { return Label; }
1849
1850   static bool classof(const SDNode *N) {
1851     return N->getOpcode() == ISD::EH_LABEL;
1852   }
1853 };
1854
1855 class ExternalSymbolSDNode : public SDNode {
1856   friend class SelectionDAG;
1857
1858   const char *Symbol;
1859   unsigned char TargetFlags;
1860
1861   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
1862     : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
1863              0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {}
1864
1865 public:
1866   const char *getSymbol() const { return Symbol; }
1867   unsigned char getTargetFlags() const { return TargetFlags; }
1868
1869   static bool classof(const SDNode *N) {
1870     return N->getOpcode() == ISD::ExternalSymbol ||
1871            N->getOpcode() == ISD::TargetExternalSymbol;
1872   }
1873 };
1874
1875 class MCSymbolSDNode : public SDNode {
1876   friend class SelectionDAG;
1877
1878   MCSymbol *Symbol;
1879
1880   MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
1881       : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
1882
1883 public:
1884   MCSymbol *getMCSymbol() const { return Symbol; }
1885
1886   static bool classof(const SDNode *N) {
1887     return N->getOpcode() == ISD::MCSymbol;
1888   }
1889 };
1890
1891 class CondCodeSDNode : public SDNode {
1892   friend class SelectionDAG;
1893
1894   ISD::CondCode Condition;
1895
1896   explicit CondCodeSDNode(ISD::CondCode Cond)
1897     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1898       Condition(Cond) {}
1899
1900 public:
1901   ISD::CondCode get() const { return Condition; }
1902
1903   static bool classof(const SDNode *N) {
1904     return N->getOpcode() == ISD::CONDCODE;
1905   }
1906 };
1907
1908 /// This class is used to represent EVT's, which are used
1909 /// to parameterize some operations.
1910 class VTSDNode : public SDNode {
1911   friend class SelectionDAG;
1912
1913   EVT ValueType;
1914
1915   explicit VTSDNode(EVT VT)
1916     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
1917       ValueType(VT) {}
1918
1919 public:
1920   EVT getVT() const { return ValueType; }
1921
1922   static bool classof(const SDNode *N) {
1923     return N->getOpcode() == ISD::VALUETYPE;
1924   }
1925 };
1926
1927 /// Base class for LoadSDNode and StoreSDNode
1928 class LSBaseSDNode : public MemSDNode {
1929 public:
1930   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
1931                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
1932                MachineMemOperand *MMO)
1933       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
1934     LSBaseSDNodeBits.AddressingMode = AM;
1935     assert(getAddressingMode() == AM && "Value truncated");
1936   }
1937
1938   const SDValue &getOffset() const {
1939     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
1940   }
1941
1942   /// Return the addressing mode for this load or store:
1943   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
1944   ISD::MemIndexedMode getAddressingMode() const {
1945     return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
1946   }
1947
1948   /// Return true if this is a pre/post inc/dec load/store.
1949   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
1950
1951   /// Return true if this is NOT a pre/post inc/dec load/store.
1952   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
1953
1954   static bool classof(const SDNode *N) {
1955     return N->getOpcode() == ISD::LOAD ||
1956            N->getOpcode() == ISD::STORE;
1957   }
1958 };
1959
1960 /// This class is used to represent ISD::LOAD nodes.
1961 class LoadSDNode : public LSBaseSDNode {
1962   friend class SelectionDAG;
1963
1964   LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
1965              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
1966              MachineMemOperand *MMO)
1967       : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
1968     LoadSDNodeBits.ExtTy = ETy;
1969     assert(readMem() && "Load MachineMemOperand is not a load!");
1970     assert(!writeMem() && "Load MachineMemOperand is a store!");
1971   }
1972
1973 public:
1974   /// Return whether this is a plain node,
1975   /// or one of the varieties of value-extending loads.
1976   ISD::LoadExtType getExtensionType() const {
1977     return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
1978   }
1979
1980   const SDValue &getBasePtr() const { return getOperand(1); }
1981   const SDValue &getOffset() const { return getOperand(2); }
1982
1983   static bool classof(const SDNode *N) {
1984     return N->getOpcode() == ISD::LOAD;
1985   }
1986 };
1987
1988 /// This class is used to represent ISD::STORE nodes.
1989 class StoreSDNode : public LSBaseSDNode {
1990   friend class SelectionDAG;
1991
1992   StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
1993               ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
1994               MachineMemOperand *MMO)
1995       : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
1996     StoreSDNodeBits.IsTruncating = isTrunc;
1997     assert(!readMem() && "Store MachineMemOperand is a load!");
1998     assert(writeMem() && "Store MachineMemOperand is not a store!");
1999   }
2000
2001 public:
2002   /// Return true if the op does a truncation before store.
2003   /// For integers this is the same as doing a TRUNCATE and storing the result.
2004   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2005   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2006
2007   const SDValue &getValue() const { return getOperand(1); }
2008   const SDValue &getBasePtr() const { return getOperand(2); }
2009   const SDValue &getOffset() const { return getOperand(3); }
2010
2011   static bool classof(const SDNode *N) {
2012     return N->getOpcode() == ISD::STORE;
2013   }
2014 };
2015
2016 /// This base class is used to represent MLOAD and MSTORE nodes
2017 class MaskedLoadStoreSDNode : public MemSDNode {
2018 public:
2019   friend class SelectionDAG;
2020
2021   MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2022                         const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2023                         MachineMemOperand *MMO)
2024       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {}
2025
2026   // In the both nodes address is Op1, mask is Op2:
2027   // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
2028   // MaskedStoreSDNode (Chain, ptr, mask, data)
2029   // Mask is a vector of i1 elements
2030   const SDValue &getBasePtr() const { return getOperand(1); }
2031   const SDValue &getMask() const    { return getOperand(2); }
2032
2033   static bool classof(const SDNode *N) {
2034     return N->getOpcode() == ISD::MLOAD ||
2035            N->getOpcode() == ISD::MSTORE;
2036   }
2037 };
2038
2039 /// This class is used to represent an MLOAD node
2040 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
2041 public:
2042   friend class SelectionDAG;
2043
2044   MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2045                    ISD::LoadExtType ETy, bool IsExpanding, EVT MemVT,
2046                    MachineMemOperand *MMO)
2047       : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, MemVT, MMO) {
2048     LoadSDNodeBits.ExtTy = ETy;
2049     LoadSDNodeBits.IsExpanding = IsExpanding;
2050   }
2051
2052   ISD::LoadExtType getExtensionType() const {
2053     return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2054   }
2055
2056   const SDValue &getSrc0() const { return getOperand(3); }
2057   static bool classof(const SDNode *N) {
2058     return N->getOpcode() == ISD::MLOAD;
2059   }
2060
2061   bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2062 };
2063
2064 /// This class is used to represent an MSTORE node
2065 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2066 public:
2067   friend class SelectionDAG;
2068
2069   MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2070                     bool isTrunc, bool isCompressing, EVT MemVT,
2071                     MachineMemOperand *MMO)
2072       : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, MemVT, MMO) {
2073     StoreSDNodeBits.IsTruncating = isTrunc;
2074     StoreSDNodeBits.IsCompressing = isCompressing;
2075   }
2076
2077   /// Return true if the op does a truncation before store.
2078   /// For integers this is the same as doing a TRUNCATE and storing the result.
2079   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2080   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2081
2082   /// Returns true if the op does a compression to the vector before storing.
2083   /// The node contiguously stores the active elements (integers or floats)
2084   /// in src (those with their respective bit set in writemask k) to unaligned
2085   /// memory at base_addr.
2086   bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2087
2088   const SDValue &getValue() const { return getOperand(3); }
2089
2090   static bool classof(const SDNode *N) {
2091     return N->getOpcode() == ISD::MSTORE;
2092   }
2093 };
2094
2095 /// This is a base class used to represent
2096 /// MGATHER and MSCATTER nodes
2097 ///
2098 class MaskedGatherScatterSDNode : public MemSDNode {
2099 public:
2100   friend class SelectionDAG;
2101
2102   MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2103                             const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2104                             MachineMemOperand *MMO)
2105       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {}
2106
2107   // In the both nodes address is Op1, mask is Op2:
2108   // MaskedGatherSDNode  (Chain, src0, mask, base, index), src0 is a passthru value
2109   // MaskedScatterSDNode (Chain, value, mask, base, index)
2110   // Mask is a vector of i1 elements
2111   const SDValue &getBasePtr() const { return getOperand(3); }
2112   const SDValue &getIndex()   const { return getOperand(4); }
2113   const SDValue &getMask()    const { return getOperand(2); }
2114   const SDValue &getValue()   const { return getOperand(1); }
2115
2116   static bool classof(const SDNode *N) {
2117     return N->getOpcode() == ISD::MGATHER ||
2118            N->getOpcode() == ISD::MSCATTER;
2119   }
2120 };
2121
2122 /// This class is used to represent an MGATHER node
2123 ///
2124 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2125 public:
2126   friend class SelectionDAG;
2127
2128   MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2129                      EVT MemVT, MachineMemOperand *MMO)
2130       : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO) {}
2131
2132   static bool classof(const SDNode *N) {
2133     return N->getOpcode() == ISD::MGATHER;
2134   }
2135 };
2136
2137 /// This class is used to represent an MSCATTER node
2138 ///
2139 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2140 public:
2141   friend class SelectionDAG;
2142
2143   MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2144                       EVT MemVT, MachineMemOperand *MMO)
2145       : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO) {}
2146
2147   static bool classof(const SDNode *N) {
2148     return N->getOpcode() == ISD::MSCATTER;
2149   }
2150 };
2151
2152 /// An SDNode that represents everything that will be needed
2153 /// to construct a MachineInstr. These nodes are created during the
2154 /// instruction selection proper phase.
2155 class MachineSDNode : public SDNode {
2156 public:
2157   typedef MachineMemOperand **mmo_iterator;
2158
2159 private:
2160   friend class SelectionDAG;
2161
2162   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
2163       : SDNode(Opc, Order, DL, VTs) {}
2164
2165   /// Memory reference descriptions for this instruction.
2166   mmo_iterator MemRefs = nullptr;
2167   mmo_iterator MemRefsEnd = nullptr;
2168
2169 public:
2170   mmo_iterator memoperands_begin() const { return MemRefs; }
2171   mmo_iterator memoperands_end() const { return MemRefsEnd; }
2172   bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
2173
2174   /// Assign this MachineSDNodes's memory reference descriptor
2175   /// list. This does not transfer ownership.
2176   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
2177     for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
2178       assert(*MMI && "Null mem ref detected!");
2179     MemRefs = NewMemRefs;
2180     MemRefsEnd = NewMemRefsEnd;
2181   }
2182
2183   static bool classof(const SDNode *N) {
2184     return N->isMachineOpcode();
2185   }
2186 };
2187
2188 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
2189                                             SDNode, ptrdiff_t> {
2190   const SDNode *Node;
2191   unsigned Operand;
2192
2193   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2194
2195 public:
2196   bool operator==(const SDNodeIterator& x) const {
2197     return Operand == x.Operand;
2198   }
2199   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2200
2201   pointer operator*() const {
2202     return Node->getOperand(Operand).getNode();
2203   }
2204   pointer operator->() const { return operator*(); }
2205
2206   SDNodeIterator& operator++() {                // Preincrement
2207     ++Operand;
2208     return *this;
2209   }
2210   SDNodeIterator operator++(int) { // Postincrement
2211     SDNodeIterator tmp = *this; ++*this; return tmp;
2212   }
2213   size_t operator-(SDNodeIterator Other) const {
2214     assert(Node == Other.Node &&
2215            "Cannot compare iterators of two different nodes!");
2216     return Operand - Other.Operand;
2217   }
2218
2219   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2220   static SDNodeIterator end  (const SDNode *N) {
2221     return SDNodeIterator(N, N->getNumOperands());
2222   }
2223
2224   unsigned getOperand() const { return Operand; }
2225   const SDNode *getNode() const { return Node; }
2226 };
2227
2228 template <> struct GraphTraits<SDNode*> {
2229   typedef SDNode *NodeRef;
2230   typedef SDNodeIterator ChildIteratorType;
2231
2232   static NodeRef getEntryNode(SDNode *N) { return N; }
2233
2234   static ChildIteratorType child_begin(NodeRef N) {
2235     return SDNodeIterator::begin(N);
2236   }
2237
2238   static ChildIteratorType child_end(NodeRef N) {
2239     return SDNodeIterator::end(N);
2240   }
2241 };
2242
2243 /// A representation of the largest SDNode, for use in sizeof().
2244 ///
2245 /// This needs to be a union because the largest node differs on 32 bit systems
2246 /// with 4 and 8 byte pointer alignment, respectively.
2247 typedef AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
2248                               BlockAddressSDNode, GlobalAddressSDNode>
2249     LargestSDNode;
2250
2251 /// The SDNode class with the greatest alignment requirement.
2252 typedef GlobalAddressSDNode MostAlignedSDNode;
2253
2254 namespace ISD {
2255
2256   /// Returns true if the specified node is a non-extending and unindexed load.
2257   inline bool isNormalLoad(const SDNode *N) {
2258     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2259     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2260       Ld->getAddressingMode() == ISD::UNINDEXED;
2261   }
2262
2263   /// Returns true if the specified node is a non-extending load.
2264   inline bool isNON_EXTLoad(const SDNode *N) {
2265     return isa<LoadSDNode>(N) &&
2266       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2267   }
2268
2269   /// Returns true if the specified node is a EXTLOAD.
2270   inline bool isEXTLoad(const SDNode *N) {
2271     return isa<LoadSDNode>(N) &&
2272       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2273   }
2274
2275   /// Returns true if the specified node is a SEXTLOAD.
2276   inline bool isSEXTLoad(const SDNode *N) {
2277     return isa<LoadSDNode>(N) &&
2278       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2279   }
2280
2281   /// Returns true if the specified node is a ZEXTLOAD.
2282   inline bool isZEXTLoad(const SDNode *N) {
2283     return isa<LoadSDNode>(N) &&
2284       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2285   }
2286
2287   /// Returns true if the specified node is an unindexed load.
2288   inline bool isUNINDEXEDLoad(const SDNode *N) {
2289     return isa<LoadSDNode>(N) &&
2290       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2291   }
2292
2293   /// Returns true if the specified node is a non-truncating
2294   /// and unindexed store.
2295   inline bool isNormalStore(const SDNode *N) {
2296     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2297     return St && !St->isTruncatingStore() &&
2298       St->getAddressingMode() == ISD::UNINDEXED;
2299   }
2300
2301   /// Returns true if the specified node is a non-truncating store.
2302   inline bool isNON_TRUNCStore(const SDNode *N) {
2303     return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2304   }
2305
2306   /// Returns true if the specified node is a truncating store.
2307   inline bool isTRUNCStore(const SDNode *N) {
2308     return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2309   }
2310
2311   /// Returns true if the specified node is an unindexed store.
2312   inline bool isUNINDEXEDStore(const SDNode *N) {
2313     return isa<StoreSDNode>(N) &&
2314       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2315   }
2316
2317 } // end namespace ISD
2318
2319 } // end namespace llvm
2320
2321 #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H