xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/SelectionDAGNodes.h (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
1 //===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the SDNode class and derived classes, which are used to
10 // represent the nodes and operations present in a SelectionDAG.  These nodes
11 // and operations are machine code level operations, with some similarities to
12 // the GCC RTL representation.
13 //
14 // Clients should include the SelectionDAG.h file instead of this file directly.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
19 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
20 
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/GraphTraits.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/ilist_node.h"
29 #include "llvm/ADT/iterator.h"
30 #include "llvm/ADT/iterator_range.h"
31 #include "llvm/CodeGen/ISDOpcodes.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/Register.h"
34 #include "llvm/CodeGen/ValueTypes.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/Metadata.h"
40 #include "llvm/IR/Operator.h"
41 #include "llvm/Support/AlignOf.h"
42 #include "llvm/Support/AtomicOrdering.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/MachineValueType.h"
46 #include "llvm/Support/TypeSize.h"
47 #include <algorithm>
48 #include <cassert>
49 #include <climits>
50 #include <cstddef>
51 #include <cstdint>
52 #include <cstring>
53 #include <iterator>
54 #include <string>
55 #include <tuple>
56 
57 namespace llvm {
58 
59 class APInt;
60 class Constant;
61 class GlobalValue;
62 class MachineBasicBlock;
63 class MachineConstantPoolValue;
64 class MCSymbol;
65 class raw_ostream;
66 class SDNode;
67 class SelectionDAG;
68 class Type;
69 class Value;
70 
71 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
72                     bool force = false);
73 
74 /// This represents a list of ValueType's that has been intern'd by
75 /// a SelectionDAG.  Instances of this simple value class are returned by
76 /// SelectionDAG::getVTList(...).
77 ///
78 struct SDVTList {
79   const EVT *VTs;
80   unsigned int NumVTs;
81 };
82 
83 namespace ISD {
84 
85   /// Node predicates
86 
87 /// If N is a BUILD_VECTOR or SPLAT_VECTOR node whose elements are all the
88 /// same constant or undefined, return true and return the constant value in
89 /// \p SplatValue.
90 bool isConstantSplatVector(const SDNode *N, APInt &SplatValue);
91 
92 /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
93 /// all of the elements are ~0 or undef. If \p BuildVectorOnly is set to
94 /// true, it only checks BUILD_VECTOR.
95 bool isConstantSplatVectorAllOnes(const SDNode *N,
96                                   bool BuildVectorOnly = false);
97 
98 /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
99 /// all of the elements are 0 or undef. If \p BuildVectorOnly is set to true, it
100 /// only checks BUILD_VECTOR.
101 bool isConstantSplatVectorAllZeros(const SDNode *N,
102                                    bool BuildVectorOnly = false);
103 
104 /// Return true if the specified node is a BUILD_VECTOR where all of the
105 /// elements are ~0 or undef.
106 bool isBuildVectorAllOnes(const SDNode *N);
107 
108 /// Return true if the specified node is a BUILD_VECTOR where all of the
109 /// elements are 0 or undef.
110 bool isBuildVectorAllZeros(const SDNode *N);
111 
112 /// Return true if the specified node is a BUILD_VECTOR node of all
113 /// ConstantSDNode or undef.
114 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
115 
116 /// Return true if the specified node is a BUILD_VECTOR node of all
117 /// ConstantFPSDNode or undef.
118 bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
119 
120 /// Return true if the node has at least one operand and all operands of the
121 /// specified node are ISD::UNDEF.
122 bool allOperandsUndef(const SDNode *N);
123 
124 } // end namespace ISD
125 
126 //===----------------------------------------------------------------------===//
127 /// Unlike LLVM values, Selection DAG nodes may return multiple
128 /// values as the result of a computation.  Many nodes return multiple values,
129 /// from loads (which define a token and a return value) to ADDC (which returns
130 /// a result and a carry value), to calls (which may return an arbitrary number
131 /// of values).
132 ///
133 /// As such, each use of a SelectionDAG computation must indicate the node that
134 /// computes it as well as which return value to use from that node.  This pair
135 /// of information is represented with the SDValue value type.
136 ///
137 class SDValue {
138   friend struct DenseMapInfo<SDValue>;
139 
140   SDNode *Node = nullptr; // The node defining the value we are using.
141   unsigned ResNo = 0;     // Which return value of the node we are using.
142 
143 public:
144   SDValue() = default;
145   SDValue(SDNode *node, unsigned resno);
146 
147   /// get the index which selects a specific result in the SDNode
148   unsigned getResNo() const { return ResNo; }
149 
150   /// get the SDNode which holds the desired result
151   SDNode *getNode() const { return Node; }
152 
153   /// set the SDNode
154   void setNode(SDNode *N) { Node = N; }
155 
156   inline SDNode *operator->() const { return Node; }
157 
158   bool operator==(const SDValue &O) const {
159     return Node == O.Node && ResNo == O.ResNo;
160   }
161   bool operator!=(const SDValue &O) const {
162     return !operator==(O);
163   }
164   bool operator<(const SDValue &O) const {
165     return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
166   }
167   explicit operator bool() const {
168     return Node != nullptr;
169   }
170 
171   SDValue getValue(unsigned R) const {
172     return SDValue(Node, R);
173   }
174 
175   /// Return true if this node is an operand of N.
176   bool isOperandOf(const SDNode *N) const;
177 
178   /// Return the ValueType of the referenced return value.
179   inline EVT getValueType() const;
180 
181   /// Return the simple ValueType of the referenced return value.
182   MVT getSimpleValueType() const {
183     return getValueType().getSimpleVT();
184   }
185 
186   /// Returns the size of the value in bits.
187   ///
188   /// If the value type is a scalable vector type, the scalable property will
189   /// be set and the runtime size will be a positive integer multiple of the
190   /// base size.
191   TypeSize getValueSizeInBits() const {
192     return getValueType().getSizeInBits();
193   }
194 
195   uint64_t getScalarValueSizeInBits() const {
196     return getValueType().getScalarType().getFixedSizeInBits();
197   }
198 
199   // Forwarding methods - These forward to the corresponding methods in SDNode.
200   inline unsigned getOpcode() const;
201   inline unsigned getNumOperands() const;
202   inline const SDValue &getOperand(unsigned i) const;
203   inline uint64_t getConstantOperandVal(unsigned i) const;
204   inline const APInt &getConstantOperandAPInt(unsigned i) const;
205   inline bool isTargetMemoryOpcode() const;
206   inline bool isTargetOpcode() const;
207   inline bool isMachineOpcode() const;
208   inline bool isUndef() const;
209   inline unsigned getMachineOpcode() const;
210   inline const DebugLoc &getDebugLoc() const;
211   inline void dump() const;
212   inline void dump(const SelectionDAG *G) const;
213   inline void dumpr() const;
214   inline void dumpr(const SelectionDAG *G) const;
215 
216   /// Return true if this operand (which must be a chain) reaches the
217   /// specified operand without crossing any side-effecting instructions.
218   /// In practice, this looks through token factors and non-volatile loads.
219   /// In order to remain efficient, this only
220   /// looks a couple of nodes in, it does not do an exhaustive search.
221   bool reachesChainWithoutSideEffects(SDValue Dest,
222                                       unsigned Depth = 2) const;
223 
224   /// Return true if there are no nodes using value ResNo of Node.
225   inline bool use_empty() const;
226 
227   /// Return true if there is exactly one node using value ResNo of Node.
228   inline bool hasOneUse() const;
229 };
230 
231 template<> struct DenseMapInfo<SDValue> {
232   static inline SDValue getEmptyKey() {
233     SDValue V;
234     V.ResNo = -1U;
235     return V;
236   }
237 
238   static inline SDValue getTombstoneKey() {
239     SDValue V;
240     V.ResNo = -2U;
241     return V;
242   }
243 
244   static unsigned getHashValue(const SDValue &Val) {
245     return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
246             (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
247   }
248 
249   static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
250     return LHS == RHS;
251   }
252 };
253 
254 /// Allow casting operators to work directly on
255 /// SDValues as if they were SDNode*'s.
256 template<> struct simplify_type<SDValue> {
257   using SimpleType = SDNode *;
258 
259   static SimpleType getSimplifiedValue(SDValue &Val) {
260     return Val.getNode();
261   }
262 };
263 template<> struct simplify_type<const SDValue> {
264   using SimpleType = /*const*/ SDNode *;
265 
266   static SimpleType getSimplifiedValue(const SDValue &Val) {
267     return Val.getNode();
268   }
269 };
270 
271 /// Represents a use of a SDNode. This class holds an SDValue,
272 /// which records the SDNode being used and the result number, a
273 /// pointer to the SDNode using the value, and Next and Prev pointers,
274 /// which link together all the uses of an SDNode.
275 ///
276 class SDUse {
277   /// Val - The value being used.
278   SDValue Val;
279   /// User - The user of this value.
280   SDNode *User = nullptr;
281   /// Prev, Next - Pointers to the uses list of the SDNode referred by
282   /// this operand.
283   SDUse **Prev = nullptr;
284   SDUse *Next = nullptr;
285 
286 public:
287   SDUse() = default;
288   SDUse(const SDUse &U) = delete;
289   SDUse &operator=(const SDUse &) = delete;
290 
291   /// Normally SDUse will just implicitly convert to an SDValue that it holds.
292   operator const SDValue&() const { return Val; }
293 
294   /// If implicit conversion to SDValue doesn't work, the get() method returns
295   /// the SDValue.
296   const SDValue &get() const { return Val; }
297 
298   /// This returns the SDNode that contains this Use.
299   SDNode *getUser() { return User; }
300 
301   /// Get the next SDUse in the use list.
302   SDUse *getNext() const { return Next; }
303 
304   /// Convenience function for get().getNode().
305   SDNode *getNode() const { return Val.getNode(); }
306   /// Convenience function for get().getResNo().
307   unsigned getResNo() const { return Val.getResNo(); }
308   /// Convenience function for get().getValueType().
309   EVT getValueType() const { return Val.getValueType(); }
310 
311   /// Convenience function for get().operator==
312   bool operator==(const SDValue &V) const {
313     return Val == V;
314   }
315 
316   /// Convenience function for get().operator!=
317   bool operator!=(const SDValue &V) const {
318     return Val != V;
319   }
320 
321   /// Convenience function for get().operator<
322   bool operator<(const SDValue &V) const {
323     return Val < V;
324   }
325 
326 private:
327   friend class SelectionDAG;
328   friend class SDNode;
329   // TODO: unfriend HandleSDNode once we fix its operand handling.
330   friend class HandleSDNode;
331 
332   void setUser(SDNode *p) { User = p; }
333 
334   /// Remove this use from its existing use list, assign it the
335   /// given value, and add it to the new value's node's use list.
336   inline void set(const SDValue &V);
337   /// Like set, but only supports initializing a newly-allocated
338   /// SDUse with a non-null value.
339   inline void setInitial(const SDValue &V);
340   /// Like set, but only sets the Node portion of the value,
341   /// leaving the ResNo portion unmodified.
342   inline void setNode(SDNode *N);
343 
344   void addToList(SDUse **List) {
345     Next = *List;
346     if (Next) Next->Prev = &Next;
347     Prev = List;
348     *List = this;
349   }
350 
351   void removeFromList() {
352     *Prev = Next;
353     if (Next) Next->Prev = Prev;
354   }
355 };
356 
357 /// simplify_type specializations - Allow casting operators to work directly on
358 /// SDValues as if they were SDNode*'s.
359 template<> struct simplify_type<SDUse> {
360   using SimpleType = SDNode *;
361 
362   static SimpleType getSimplifiedValue(SDUse &Val) {
363     return Val.getNode();
364   }
365 };
366 
367 /// These are IR-level optimization flags that may be propagated to SDNodes.
368 /// TODO: This data structure should be shared by the IR optimizer and the
369 /// the backend.
370 struct SDNodeFlags {
371 private:
372   bool NoUnsignedWrap : 1;
373   bool NoSignedWrap : 1;
374   bool Exact : 1;
375   bool NoNaNs : 1;
376   bool NoInfs : 1;
377   bool NoSignedZeros : 1;
378   bool AllowReciprocal : 1;
379   bool AllowContract : 1;
380   bool ApproximateFuncs : 1;
381   bool AllowReassociation : 1;
382 
383   // We assume instructions do not raise floating-point exceptions by default,
384   // and only those marked explicitly may do so.  We could choose to represent
385   // this via a positive "FPExcept" flags like on the MI level, but having a
386   // negative "NoFPExcept" flag here (that defaults to true) makes the flag
387   // intersection logic more straightforward.
388   bool NoFPExcept : 1;
389 
390 public:
391   /// Default constructor turns off all optimization flags.
392   SDNodeFlags()
393       : NoUnsignedWrap(false), NoSignedWrap(false), Exact(false), NoNaNs(false),
394         NoInfs(false), NoSignedZeros(false), AllowReciprocal(false),
395         AllowContract(false), ApproximateFuncs(false),
396         AllowReassociation(false), NoFPExcept(false) {}
397 
398   /// Propagate the fast-math-flags from an IR FPMathOperator.
399   void copyFMF(const FPMathOperator &FPMO) {
400     setNoNaNs(FPMO.hasNoNaNs());
401     setNoInfs(FPMO.hasNoInfs());
402     setNoSignedZeros(FPMO.hasNoSignedZeros());
403     setAllowReciprocal(FPMO.hasAllowReciprocal());
404     setAllowContract(FPMO.hasAllowContract());
405     setApproximateFuncs(FPMO.hasApproxFunc());
406     setAllowReassociation(FPMO.hasAllowReassoc());
407   }
408 
409   // These are mutators for each flag.
410   void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
411   void setNoSignedWrap(bool b) { NoSignedWrap = b; }
412   void setExact(bool b) { Exact = b; }
413   void setNoNaNs(bool b) { NoNaNs = b; }
414   void setNoInfs(bool b) { NoInfs = b; }
415   void setNoSignedZeros(bool b) { NoSignedZeros = b; }
416   void setAllowReciprocal(bool b) { AllowReciprocal = b; }
417   void setAllowContract(bool b) { AllowContract = b; }
418   void setApproximateFuncs(bool b) { ApproximateFuncs = b; }
419   void setAllowReassociation(bool b) { AllowReassociation = b; }
420   void setNoFPExcept(bool b) { NoFPExcept = b; }
421 
422   // These are accessors for each flag.
423   bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
424   bool hasNoSignedWrap() const { return NoSignedWrap; }
425   bool hasExact() const { return Exact; }
426   bool hasNoNaNs() const { return NoNaNs; }
427   bool hasNoInfs() const { return NoInfs; }
428   bool hasNoSignedZeros() const { return NoSignedZeros; }
429   bool hasAllowReciprocal() const { return AllowReciprocal; }
430   bool hasAllowContract() const { return AllowContract; }
431   bool hasApproximateFuncs() const { return ApproximateFuncs; }
432   bool hasAllowReassociation() const { return AllowReassociation; }
433   bool hasNoFPExcept() const { return NoFPExcept; }
434 
435   /// Clear any flags in this flag set that aren't also set in Flags. All
436   /// flags will be cleared if Flags are undefined.
437   void intersectWith(const SDNodeFlags Flags) {
438     NoUnsignedWrap &= Flags.NoUnsignedWrap;
439     NoSignedWrap &= Flags.NoSignedWrap;
440     Exact &= Flags.Exact;
441     NoNaNs &= Flags.NoNaNs;
442     NoInfs &= Flags.NoInfs;
443     NoSignedZeros &= Flags.NoSignedZeros;
444     AllowReciprocal &= Flags.AllowReciprocal;
445     AllowContract &= Flags.AllowContract;
446     ApproximateFuncs &= Flags.ApproximateFuncs;
447     AllowReassociation &= Flags.AllowReassociation;
448     NoFPExcept &= Flags.NoFPExcept;
449   }
450 };
451 
452 /// Represents one node in the SelectionDAG.
453 ///
454 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
455 private:
456   /// The operation that this node performs.
457   int16_t NodeType;
458 
459 protected:
460   // We define a set of mini-helper classes to help us interpret the bits in our
461   // SubclassData.  These are designed to fit within a uint16_t so they pack
462   // with NodeType.
463 
464 #if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__))
465 // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
466 // and give the `pack` pragma push semantics.
467 #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")
468 #define END_TWO_BYTE_PACK() _Pragma("pack(pop)")
469 #else
470 #define BEGIN_TWO_BYTE_PACK()
471 #define END_TWO_BYTE_PACK()
472 #endif
473 
474 BEGIN_TWO_BYTE_PACK()
475   class SDNodeBitfields {
476     friend class SDNode;
477     friend class MemIntrinsicSDNode;
478     friend class MemSDNode;
479     friend class SelectionDAG;
480 
481     uint16_t HasDebugValue : 1;
482     uint16_t IsMemIntrinsic : 1;
483     uint16_t IsDivergent : 1;
484   };
485   enum { NumSDNodeBits = 3 };
486 
487   class ConstantSDNodeBitfields {
488     friend class ConstantSDNode;
489 
490     uint16_t : NumSDNodeBits;
491 
492     uint16_t IsOpaque : 1;
493   };
494 
495   class MemSDNodeBitfields {
496     friend class MemSDNode;
497     friend class MemIntrinsicSDNode;
498     friend class AtomicSDNode;
499 
500     uint16_t : NumSDNodeBits;
501 
502     uint16_t IsVolatile : 1;
503     uint16_t IsNonTemporal : 1;
504     uint16_t IsDereferenceable : 1;
505     uint16_t IsInvariant : 1;
506   };
507   enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
508 
509   class LSBaseSDNodeBitfields {
510     friend class LSBaseSDNode;
511     friend class VPLoadStoreSDNode;
512     friend class MaskedLoadStoreSDNode;
513     friend class MaskedGatherScatterSDNode;
514     friend class VPGatherScatterSDNode;
515 
516     uint16_t : NumMemSDNodeBits;
517 
518     // This storage is shared between disparate class hierarchies to hold an
519     // enumeration specific to the class hierarchy in use.
520     //   LSBaseSDNode => enum ISD::MemIndexedMode
521     //   VPLoadStoreBaseSDNode => enum ISD::MemIndexedMode
522     //   MaskedLoadStoreBaseSDNode => enum ISD::MemIndexedMode
523     //   VPGatherScatterSDNode => enum ISD::MemIndexType
524     //   MaskedGatherScatterSDNode => enum ISD::MemIndexType
525     uint16_t AddressingMode : 3;
526   };
527   enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
528 
529   class LoadSDNodeBitfields {
530     friend class LoadSDNode;
531     friend class VPLoadSDNode;
532     friend class MaskedLoadSDNode;
533     friend class MaskedGatherSDNode;
534     friend class VPGatherSDNode;
535 
536     uint16_t : NumLSBaseSDNodeBits;
537 
538     uint16_t ExtTy : 2; // enum ISD::LoadExtType
539     uint16_t IsExpanding : 1;
540   };
541 
542   class StoreSDNodeBitfields {
543     friend class StoreSDNode;
544     friend class VPStoreSDNode;
545     friend class MaskedStoreSDNode;
546     friend class MaskedScatterSDNode;
547     friend class VPScatterSDNode;
548 
549     uint16_t : NumLSBaseSDNodeBits;
550 
551     uint16_t IsTruncating : 1;
552     uint16_t IsCompressing : 1;
553   };
554 
555   union {
556     char RawSDNodeBits[sizeof(uint16_t)];
557     SDNodeBitfields SDNodeBits;
558     ConstantSDNodeBitfields ConstantSDNodeBits;
559     MemSDNodeBitfields MemSDNodeBits;
560     LSBaseSDNodeBitfields LSBaseSDNodeBits;
561     LoadSDNodeBitfields LoadSDNodeBits;
562     StoreSDNodeBitfields StoreSDNodeBits;
563   };
564 END_TWO_BYTE_PACK()
565 #undef BEGIN_TWO_BYTE_PACK
566 #undef END_TWO_BYTE_PACK
567 
568   // RawSDNodeBits must cover the entirety of the union.  This means that all of
569   // the union's members must have size <= RawSDNodeBits.  We write the RHS as
570   // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
571   static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
572   static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
573   static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
574   static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
575   static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide");
576   static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
577 
578 private:
579   friend class SelectionDAG;
580   // TODO: unfriend HandleSDNode once we fix its operand handling.
581   friend class HandleSDNode;
582 
583   /// Unique id per SDNode in the DAG.
584   int NodeId = -1;
585 
586   /// The values that are used by this operation.
587   SDUse *OperandList = nullptr;
588 
589   /// The types of the values this node defines.  SDNode's may
590   /// define multiple values simultaneously.
591   const EVT *ValueList;
592 
593   /// List of uses for this SDNode.
594   SDUse *UseList = nullptr;
595 
596   /// The number of entries in the Operand/Value list.
597   unsigned short NumOperands = 0;
598   unsigned short NumValues;
599 
600   // The ordering of the SDNodes. It roughly corresponds to the ordering of the
601   // original LLVM instructions.
602   // This is used for turning off scheduling, because we'll forgo
603   // the normal scheduling algorithms and output the instructions according to
604   // this ordering.
605   unsigned IROrder;
606 
607   /// Source line information.
608   DebugLoc debugLoc;
609 
610   /// Return a pointer to the specified value type.
611   static const EVT *getValueTypeList(EVT VT);
612 
613   SDNodeFlags Flags;
614 
615 public:
616   /// Unique and persistent id per SDNode in the DAG.
617   /// Used for debug printing.
618   uint16_t PersistentId;
619 
620   //===--------------------------------------------------------------------===//
621   //  Accessors
622   //
623 
624   /// Return the SelectionDAG opcode value for this node. For
625   /// pre-isel nodes (those for which isMachineOpcode returns false), these
626   /// are the opcode values in the ISD and <target>ISD namespaces. For
627   /// post-isel opcodes, see getMachineOpcode.
628   unsigned getOpcode()  const { return (unsigned short)NodeType; }
629 
630   /// Test if this node has a target-specific opcode (in the
631   /// \<target\>ISD namespace).
632   bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
633 
634   /// Test if this node has a target-specific opcode that may raise
635   /// FP exceptions (in the \<target\>ISD namespace and greater than
636   /// FIRST_TARGET_STRICTFP_OPCODE).  Note that all target memory
637   /// opcode are currently automatically considered to possibly raise
638   /// FP exceptions as well.
639   bool isTargetStrictFPOpcode() const {
640     return NodeType >= ISD::FIRST_TARGET_STRICTFP_OPCODE;
641   }
642 
643   /// Test if this node has a target-specific
644   /// memory-referencing opcode (in the \<target\>ISD namespace and
645   /// greater than FIRST_TARGET_MEMORY_OPCODE).
646   bool isTargetMemoryOpcode() const {
647     return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
648   }
649 
650   /// Return true if the type of the node type undefined.
651   bool isUndef() const { return NodeType == ISD::UNDEF; }
652 
653   /// Test if this node is a memory intrinsic (with valid pointer information).
654   /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
655   /// non-memory intrinsics (with chains) that are not really instances of
656   /// MemSDNode. For such nodes, we need some extra state to determine the
657   /// proper classof relationship.
658   bool isMemIntrinsic() const {
659     return (NodeType == ISD::INTRINSIC_W_CHAIN ||
660             NodeType == ISD::INTRINSIC_VOID) &&
661            SDNodeBits.IsMemIntrinsic;
662   }
663 
664   /// Test if this node is a strict floating point pseudo-op.
665   bool isStrictFPOpcode() {
666     switch (NodeType) {
667       default:
668         return false;
669       case ISD::STRICT_FP16_TO_FP:
670       case ISD::STRICT_FP_TO_FP16:
671 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
672       case ISD::STRICT_##DAGN:
673 #include "llvm/IR/ConstrainedOps.def"
674         return true;
675     }
676   }
677 
678   /// Test if this node has a post-isel opcode, directly
679   /// corresponding to a MachineInstr opcode.
680   bool isMachineOpcode() const { return NodeType < 0; }
681 
682   /// This may only be called if isMachineOpcode returns
683   /// true. It returns the MachineInstr opcode value that the node's opcode
684   /// corresponds to.
685   unsigned getMachineOpcode() const {
686     assert(isMachineOpcode() && "Not a MachineInstr opcode!");
687     return ~NodeType;
688   }
689 
690   bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
691   void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
692 
693   bool isDivergent() const { return SDNodeBits.IsDivergent; }
694 
695   /// Return true if there are no uses of this node.
696   bool use_empty() const { return UseList == nullptr; }
697 
698   /// Return true if there is exactly one use of this node.
699   bool hasOneUse() const { return hasSingleElement(uses()); }
700 
701   /// Return the number of uses of this node. This method takes
702   /// time proportional to the number of uses.
703   size_t use_size() const { return std::distance(use_begin(), use_end()); }
704 
705   /// Return the unique node id.
706   int getNodeId() const { return NodeId; }
707 
708   /// Set unique node id.
709   void setNodeId(int Id) { NodeId = Id; }
710 
711   /// Return the node ordering.
712   unsigned getIROrder() const { return IROrder; }
713 
714   /// Set the node ordering.
715   void setIROrder(unsigned Order) { IROrder = Order; }
716 
717   /// Return the source location info.
718   const DebugLoc &getDebugLoc() const { return debugLoc; }
719 
720   /// Set source location info.  Try to avoid this, putting
721   /// it in the constructor is preferable.
722   void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
723 
724   /// This class provides iterator support for SDUse
725   /// operands that use a specific SDNode.
726   class use_iterator {
727     friend class SDNode;
728 
729     SDUse *Op = nullptr;
730 
731     explicit use_iterator(SDUse *op) : Op(op) {}
732 
733   public:
734     using iterator_category = std::forward_iterator_tag;
735     using value_type = SDUse;
736     using difference_type = std::ptrdiff_t;
737     using pointer = value_type *;
738     using reference = value_type &;
739 
740     use_iterator() = default;
741     use_iterator(const use_iterator &I) : Op(I.Op) {}
742 
743     bool operator==(const use_iterator &x) const {
744       return Op == x.Op;
745     }
746     bool operator!=(const use_iterator &x) const {
747       return !operator==(x);
748     }
749 
750     /// Return true if this iterator is at the end of uses list.
751     bool atEnd() const { return Op == nullptr; }
752 
753     // Iterator traversal: forward iteration only.
754     use_iterator &operator++() {          // Preincrement
755       assert(Op && "Cannot increment end iterator!");
756       Op = Op->getNext();
757       return *this;
758     }
759 
760     use_iterator operator++(int) {        // Postincrement
761       use_iterator tmp = *this; ++*this; return tmp;
762     }
763 
764     /// Retrieve a pointer to the current user node.
765     SDNode *operator*() const {
766       assert(Op && "Cannot dereference end iterator!");
767       return Op->getUser();
768     }
769 
770     SDNode *operator->() const { return operator*(); }
771 
772     SDUse &getUse() const { return *Op; }
773 
774     /// Retrieve the operand # of this use in its user.
775     unsigned getOperandNo() const {
776       assert(Op && "Cannot dereference end iterator!");
777       return (unsigned)(Op - Op->getUser()->OperandList);
778     }
779   };
780 
781   /// Provide iteration support to walk over all uses of an SDNode.
782   use_iterator use_begin() const {
783     return use_iterator(UseList);
784   }
785 
786   static use_iterator use_end() { return use_iterator(nullptr); }
787 
788   inline iterator_range<use_iterator> uses() {
789     return make_range(use_begin(), use_end());
790   }
791   inline iterator_range<use_iterator> uses() const {
792     return make_range(use_begin(), use_end());
793   }
794 
795   /// Return true if there are exactly NUSES uses of the indicated value.
796   /// This method ignores uses of other values defined by this operation.
797   bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
798 
799   /// Return true if there are any use of the indicated value.
800   /// This method ignores uses of other values defined by this operation.
801   bool hasAnyUseOfValue(unsigned Value) const;
802 
803   /// Return true if this node is the only use of N.
804   bool isOnlyUserOf(const SDNode *N) const;
805 
806   /// Return true if this node is an operand of N.
807   bool isOperandOf(const SDNode *N) const;
808 
809   /// Return true if this node is a predecessor of N.
810   /// NOTE: Implemented on top of hasPredecessor and every bit as
811   /// expensive. Use carefully.
812   bool isPredecessorOf(const SDNode *N) const {
813     return N->hasPredecessor(this);
814   }
815 
816   /// Return true if N is a predecessor of this node.
817   /// N is either an operand of this node, or can be reached by recursively
818   /// traversing up the operands.
819   /// NOTE: This is an expensive method. Use it carefully.
820   bool hasPredecessor(const SDNode *N) const;
821 
822   /// Returns true if N is a predecessor of any node in Worklist. This
823   /// helper keeps Visited and Worklist sets externally to allow unions
824   /// searches to be performed in parallel, caching of results across
825   /// queries and incremental addition to Worklist. Stops early if N is
826   /// found but will resume. Remember to clear Visited and Worklists
827   /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before
828   /// giving up. The TopologicalPrune flag signals that positive NodeIds are
829   /// topologically ordered (Operands have strictly smaller node id) and search
830   /// can be pruned leveraging this.
831   static bool hasPredecessorHelper(const SDNode *N,
832                                    SmallPtrSetImpl<const SDNode *> &Visited,
833                                    SmallVectorImpl<const SDNode *> &Worklist,
834                                    unsigned int MaxSteps = 0,
835                                    bool TopologicalPrune = false) {
836     SmallVector<const SDNode *, 8> DeferredNodes;
837     if (Visited.count(N))
838       return true;
839 
840     // Node Id's are assigned in three places: As a topological
841     // ordering (> 0), during legalization (results in values set to
842     // 0), new nodes (set to -1). If N has a topolgical id then we
843     // know that all nodes with ids smaller than it cannot be
844     // successors and we need not check them. Filter out all node
845     // that can't be matches. We add them to the worklist before exit
846     // in case of multiple calls. Note that during selection the topological id
847     // may be violated if a node's predecessor is selected before it. We mark
848     // this at selection negating the id of unselected successors and
849     // restricting topological pruning to positive ids.
850 
851     int NId = N->getNodeId();
852     // If we Invalidated the Id, reconstruct original NId.
853     if (NId < -1)
854       NId = -(NId + 1);
855 
856     bool Found = false;
857     while (!Worklist.empty()) {
858       const SDNode *M = Worklist.pop_back_val();
859       int MId = M->getNodeId();
860       if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) &&
861           (MId > 0) && (MId < NId)) {
862         DeferredNodes.push_back(M);
863         continue;
864       }
865       for (const SDValue &OpV : M->op_values()) {
866         SDNode *Op = OpV.getNode();
867         if (Visited.insert(Op).second)
868           Worklist.push_back(Op);
869         if (Op == N)
870           Found = true;
871       }
872       if (Found)
873         break;
874       if (MaxSteps != 0 && Visited.size() >= MaxSteps)
875         break;
876     }
877     // Push deferred nodes back on worklist.
878     Worklist.append(DeferredNodes.begin(), DeferredNodes.end());
879     // If we bailed early, conservatively return found.
880     if (MaxSteps != 0 && Visited.size() >= MaxSteps)
881       return true;
882     return Found;
883   }
884 
885   /// Return true if all the users of N are contained in Nodes.
886   /// NOTE: Requires at least one match, but doesn't require them all.
887   static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
888 
889   /// Return the number of values used by this operation.
890   unsigned getNumOperands() const { return NumOperands; }
891 
892   /// Return the maximum number of operands that a SDNode can hold.
893   static constexpr size_t getMaxNumOperands() {
894     return std::numeric_limits<decltype(SDNode::NumOperands)>::max();
895   }
896 
897   /// Helper method returns the integer value of a ConstantSDNode operand.
898   inline uint64_t getConstantOperandVal(unsigned Num) const;
899 
900   /// Helper method returns the APInt of a ConstantSDNode operand.
901   inline const APInt &getConstantOperandAPInt(unsigned Num) const;
902 
903   const SDValue &getOperand(unsigned Num) const {
904     assert(Num < NumOperands && "Invalid child # of SDNode!");
905     return OperandList[Num];
906   }
907 
908   using op_iterator = SDUse *;
909 
910   op_iterator op_begin() const { return OperandList; }
911   op_iterator op_end() const { return OperandList+NumOperands; }
912   ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
913 
914   /// Iterator for directly iterating over the operand SDValue's.
915   struct value_op_iterator
916       : iterator_adaptor_base<value_op_iterator, op_iterator,
917                               std::random_access_iterator_tag, SDValue,
918                               ptrdiff_t, value_op_iterator *,
919                               value_op_iterator *> {
920     explicit value_op_iterator(SDUse *U = nullptr)
921       : iterator_adaptor_base(U) {}
922 
923     const SDValue &operator*() const { return I->get(); }
924   };
925 
926   iterator_range<value_op_iterator> op_values() const {
927     return make_range(value_op_iterator(op_begin()),
928                       value_op_iterator(op_end()));
929   }
930 
931   SDVTList getVTList() const {
932     SDVTList X = { ValueList, NumValues };
933     return X;
934   }
935 
936   /// If this node has a glue operand, return the node
937   /// to which the glue operand points. Otherwise return NULL.
938   SDNode *getGluedNode() const {
939     if (getNumOperands() != 0 &&
940         getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
941       return getOperand(getNumOperands()-1).getNode();
942     return nullptr;
943   }
944 
945   /// If this node has a glue value with a user, return
946   /// the user (there is at most one). Otherwise return NULL.
947   SDNode *getGluedUser() const {
948     for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
949       if (UI.getUse().get().getValueType() == MVT::Glue)
950         return *UI;
951     return nullptr;
952   }
953 
954   SDNodeFlags getFlags() const { return Flags; }
955   void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
956 
957   /// Clear any flags in this node that aren't also set in Flags.
958   /// If Flags is not in a defined state then this has no effect.
959   void intersectFlagsWith(const SDNodeFlags Flags);
960 
961   /// Return the number of values defined/returned by this operator.
962   unsigned getNumValues() const { return NumValues; }
963 
964   /// Return the type of a specified result.
965   EVT getValueType(unsigned ResNo) const {
966     assert(ResNo < NumValues && "Illegal result number!");
967     return ValueList[ResNo];
968   }
969 
970   /// Return the type of a specified result as a simple type.
971   MVT getSimpleValueType(unsigned ResNo) const {
972     return getValueType(ResNo).getSimpleVT();
973   }
974 
975   /// Returns MVT::getSizeInBits(getValueType(ResNo)).
976   ///
977   /// If the value type is a scalable vector type, the scalable property will
978   /// be set and the runtime size will be a positive integer multiple of the
979   /// base size.
980   TypeSize getValueSizeInBits(unsigned ResNo) const {
981     return getValueType(ResNo).getSizeInBits();
982   }
983 
984   using value_iterator = const EVT *;
985 
986   value_iterator value_begin() const { return ValueList; }
987   value_iterator value_end() const { return ValueList+NumValues; }
988   iterator_range<value_iterator> values() const {
989     return llvm::make_range(value_begin(), value_end());
990   }
991 
992   /// Return the opcode of this operation for printing.
993   std::string getOperationName(const SelectionDAG *G = nullptr) const;
994   static const char* getIndexedModeName(ISD::MemIndexedMode AM);
995   void print_types(raw_ostream &OS, const SelectionDAG *G) const;
996   void print_details(raw_ostream &OS, const SelectionDAG *G) const;
997   void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
998   void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
999 
1000   /// Print a SelectionDAG node and all children down to
1001   /// the leaves.  The given SelectionDAG allows target-specific nodes
1002   /// to be printed in human-readable form.  Unlike printr, this will
1003   /// print the whole DAG, including children that appear multiple
1004   /// times.
1005   ///
1006   void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
1007 
1008   /// Print a SelectionDAG node and children up to
1009   /// depth "depth."  The given SelectionDAG allows target-specific
1010   /// nodes to be printed in human-readable form.  Unlike printr, this
1011   /// will print children that appear multiple times wherever they are
1012   /// used.
1013   ///
1014   void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
1015                        unsigned depth = 100) const;
1016 
1017   /// Dump this node, for debugging.
1018   void dump() const;
1019 
1020   /// Dump (recursively) this node and its use-def subgraph.
1021   void dumpr() const;
1022 
1023   /// Dump this node, for debugging.
1024   /// The given SelectionDAG allows target-specific nodes to be printed
1025   /// in human-readable form.
1026   void dump(const SelectionDAG *G) const;
1027 
1028   /// Dump (recursively) this node and its use-def subgraph.
1029   /// The given SelectionDAG allows target-specific nodes to be printed
1030   /// in human-readable form.
1031   void dumpr(const SelectionDAG *G) const;
1032 
1033   /// printrFull to dbgs().  The given SelectionDAG allows
1034   /// target-specific nodes to be printed in human-readable form.
1035   /// Unlike dumpr, this will print the whole DAG, including children
1036   /// that appear multiple times.
1037   void dumprFull(const SelectionDAG *G = nullptr) const;
1038 
1039   /// printrWithDepth to dbgs().  The given
1040   /// SelectionDAG allows target-specific nodes to be printed in
1041   /// human-readable form.  Unlike dumpr, this will print children
1042   /// that appear multiple times wherever they are used.
1043   ///
1044   void dumprWithDepth(const SelectionDAG *G = nullptr,
1045                       unsigned depth = 100) const;
1046 
1047   /// Gather unique data for the node.
1048   void Profile(FoldingSetNodeID &ID) const;
1049 
1050   /// This method should only be used by the SDUse class.
1051   void addUse(SDUse &U) { U.addToList(&UseList); }
1052 
1053 protected:
1054   static SDVTList getSDVTList(EVT VT) {
1055     SDVTList Ret = { getValueTypeList(VT), 1 };
1056     return Ret;
1057   }
1058 
1059   /// Create an SDNode.
1060   ///
1061   /// SDNodes are created without any operands, and never own the operand
1062   /// storage. To add operands, see SelectionDAG::createOperands.
1063   SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
1064       : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
1065         IROrder(Order), debugLoc(std::move(dl)) {
1066     memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
1067     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
1068     assert(NumValues == VTs.NumVTs &&
1069            "NumValues wasn't wide enough for its operands!");
1070   }
1071 
1072   /// Release the operands and set this node to have zero operands.
1073   void DropOperands();
1074 };
1075 
1076 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
1077 /// into SDNode creation functions.
1078 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
1079 /// from the original Instruction, and IROrder is the ordinal position of
1080 /// the instruction.
1081 /// When an SDNode is created after the DAG is being built, both DebugLoc and
1082 /// the IROrder are propagated from the original SDNode.
1083 /// So SDLoc class provides two constructors besides the default one, one to
1084 /// be used by the DAGBuilder, the other to be used by others.
1085 class SDLoc {
1086 private:
1087   DebugLoc DL;
1088   int IROrder = 0;
1089 
1090 public:
1091   SDLoc() = default;
1092   SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1093   SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1094   SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1095     assert(Order >= 0 && "bad IROrder");
1096     if (I)
1097       DL = I->getDebugLoc();
1098   }
1099 
1100   unsigned getIROrder() const { return IROrder; }
1101   const DebugLoc &getDebugLoc() const { return DL; }
1102 };
1103 
1104 // Define inline functions from the SDValue class.
1105 
1106 inline SDValue::SDValue(SDNode *node, unsigned resno)
1107     : Node(node), ResNo(resno) {
1108   // Explicitly check for !ResNo to avoid use-after-free, because there are
1109   // callers that use SDValue(N, 0) with a deleted N to indicate successful
1110   // combines.
1111   assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&
1112          "Invalid result number for the given node!");
1113   assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
1114 }
1115 
1116 inline unsigned SDValue::getOpcode() const {
1117   return Node->getOpcode();
1118 }
1119 
1120 inline EVT SDValue::getValueType() const {
1121   return Node->getValueType(ResNo);
1122 }
1123 
1124 inline unsigned SDValue::getNumOperands() const {
1125   return Node->getNumOperands();
1126 }
1127 
1128 inline const SDValue &SDValue::getOperand(unsigned i) const {
1129   return Node->getOperand(i);
1130 }
1131 
1132 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1133   return Node->getConstantOperandVal(i);
1134 }
1135 
1136 inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const {
1137   return Node->getConstantOperandAPInt(i);
1138 }
1139 
1140 inline bool SDValue::isTargetOpcode() const {
1141   return Node->isTargetOpcode();
1142 }
1143 
1144 inline bool SDValue::isTargetMemoryOpcode() const {
1145   return Node->isTargetMemoryOpcode();
1146 }
1147 
1148 inline bool SDValue::isMachineOpcode() const {
1149   return Node->isMachineOpcode();
1150 }
1151 
1152 inline unsigned SDValue::getMachineOpcode() const {
1153   return Node->getMachineOpcode();
1154 }
1155 
1156 inline bool SDValue::isUndef() const {
1157   return Node->isUndef();
1158 }
1159 
1160 inline bool SDValue::use_empty() const {
1161   return !Node->hasAnyUseOfValue(ResNo);
1162 }
1163 
1164 inline bool SDValue::hasOneUse() const {
1165   return Node->hasNUsesOfValue(1, ResNo);
1166 }
1167 
1168 inline const DebugLoc &SDValue::getDebugLoc() const {
1169   return Node->getDebugLoc();
1170 }
1171 
1172 inline void SDValue::dump() const {
1173   return Node->dump();
1174 }
1175 
1176 inline void SDValue::dump(const SelectionDAG *G) const {
1177   return Node->dump(G);
1178 }
1179 
1180 inline void SDValue::dumpr() const {
1181   return Node->dumpr();
1182 }
1183 
1184 inline void SDValue::dumpr(const SelectionDAG *G) const {
1185   return Node->dumpr(G);
1186 }
1187 
1188 // Define inline functions from the SDUse class.
1189 
1190 inline void SDUse::set(const SDValue &V) {
1191   if (Val.getNode()) removeFromList();
1192   Val = V;
1193   if (V.getNode()) V.getNode()->addUse(*this);
1194 }
1195 
1196 inline void SDUse::setInitial(const SDValue &V) {
1197   Val = V;
1198   V.getNode()->addUse(*this);
1199 }
1200 
1201 inline void SDUse::setNode(SDNode *N) {
1202   if (Val.getNode()) removeFromList();
1203   Val.setNode(N);
1204   if (N) N->addUse(*this);
1205 }
1206 
1207 /// This class is used to form a handle around another node that
1208 /// is persistent and is updated across invocations of replaceAllUsesWith on its
1209 /// operand.  This node should be directly created by end-users and not added to
1210 /// the AllNodes list.
1211 class HandleSDNode : public SDNode {
1212   SDUse Op;
1213 
1214 public:
1215   explicit HandleSDNode(SDValue X)
1216     : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1217     // HandleSDNodes are never inserted into the DAG, so they won't be
1218     // auto-numbered. Use ID 65535 as a sentinel.
1219     PersistentId = 0xffff;
1220 
1221     // Manually set up the operand list. This node type is special in that it's
1222     // always stack allocated and SelectionDAG does not manage its operands.
1223     // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1224     // be so special.
1225     Op.setUser(this);
1226     Op.setInitial(X);
1227     NumOperands = 1;
1228     OperandList = &Op;
1229   }
1230   ~HandleSDNode();
1231 
1232   const SDValue &getValue() const { return Op; }
1233 };
1234 
1235 class AddrSpaceCastSDNode : public SDNode {
1236 private:
1237   unsigned SrcAddrSpace;
1238   unsigned DestAddrSpace;
1239 
1240 public:
1241   AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1242                       unsigned SrcAS, unsigned DestAS);
1243 
1244   unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1245   unsigned getDestAddressSpace() const { return DestAddrSpace; }
1246 
1247   static bool classof(const SDNode *N) {
1248     return N->getOpcode() == ISD::ADDRSPACECAST;
1249   }
1250 };
1251 
1252 /// This is an abstract virtual class for memory operations.
1253 class MemSDNode : public SDNode {
1254 private:
1255   // VT of in-memory value.
1256   EVT MemoryVT;
1257 
1258 protected:
1259   /// Memory reference information.
1260   MachineMemOperand *MMO;
1261 
1262 public:
1263   MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1264             EVT memvt, MachineMemOperand *MMO);
1265 
1266   bool readMem() const { return MMO->isLoad(); }
1267   bool writeMem() const { return MMO->isStore(); }
1268 
1269   /// Returns alignment and volatility of the memory access
1270   Align getOriginalAlign() const { return MMO->getBaseAlign(); }
1271   Align getAlign() const { return MMO->getAlign(); }
1272   // FIXME: Remove once transition to getAlign is over.
1273   unsigned getAlignment() const { return MMO->getAlign().value(); }
1274 
1275   /// Return the SubclassData value, without HasDebugValue. This contains an
1276   /// encoding of the volatile flag, as well as bits used by subclasses. This
1277   /// function should only be used to compute a FoldingSetNodeID value.
1278   /// The HasDebugValue bit is masked out because CSE map needs to match
1279   /// nodes with debug info with nodes without debug info. Same is about
1280   /// isDivergent bit.
1281   unsigned getRawSubclassData() const {
1282     uint16_t Data;
1283     union {
1284       char RawSDNodeBits[sizeof(uint16_t)];
1285       SDNodeBitfields SDNodeBits;
1286     };
1287     memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1288     SDNodeBits.HasDebugValue = 0;
1289     SDNodeBits.IsDivergent = false;
1290     memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1291     return Data;
1292   }
1293 
1294   bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1295   bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1296   bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1297   bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1298 
1299   // Returns the offset from the location of the access.
1300   int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1301 
1302   /// Returns the AA info that describes the dereference.
1303   AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1304 
1305   /// Returns the Ranges that describes the dereference.
1306   const MDNode *getRanges() const { return MMO->getRanges(); }
1307 
1308   /// Returns the synchronization scope ID for this memory operation.
1309   SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); }
1310 
1311   /// Return the atomic ordering requirements for this memory operation. For
1312   /// cmpxchg atomic operations, return the atomic ordering requirements when
1313   /// store occurs.
1314   AtomicOrdering getSuccessOrdering() const {
1315     return MMO->getSuccessOrdering();
1316   }
1317 
1318   /// Return a single atomic ordering that is at least as strong as both the
1319   /// success and failure orderings for an atomic operation.  (For operations
1320   /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
1321   AtomicOrdering getMergedOrdering() const { return MMO->getMergedOrdering(); }
1322 
1323   /// Return true if the memory operation ordering is Unordered or higher.
1324   bool isAtomic() const { return MMO->isAtomic(); }
1325 
1326   /// Returns true if the memory operation doesn't imply any ordering
1327   /// constraints on surrounding memory operations beyond the normal memory
1328   /// aliasing rules.
1329   bool isUnordered() const { return MMO->isUnordered(); }
1330 
1331   /// Returns true if the memory operation is neither atomic or volatile.
1332   bool isSimple() const { return !isAtomic() && !isVolatile(); }
1333 
1334   /// Return the type of the in-memory value.
1335   EVT getMemoryVT() const { return MemoryVT; }
1336 
1337   /// Return a MachineMemOperand object describing the memory
1338   /// reference performed by operation.
1339   MachineMemOperand *getMemOperand() const { return MMO; }
1340 
1341   const MachinePointerInfo &getPointerInfo() const {
1342     return MMO->getPointerInfo();
1343   }
1344 
1345   /// Return the address space for the associated pointer
1346   unsigned getAddressSpace() const {
1347     return getPointerInfo().getAddrSpace();
1348   }
1349 
1350   /// Update this MemSDNode's MachineMemOperand information
1351   /// to reflect the alignment of NewMMO, if it has a greater alignment.
1352   /// This must only be used when the new alignment applies to all users of
1353   /// this MachineMemOperand.
1354   void refineAlignment(const MachineMemOperand *NewMMO) {
1355     MMO->refineAlignment(NewMMO);
1356   }
1357 
1358   const SDValue &getChain() const { return getOperand(0); }
1359 
1360   const SDValue &getBasePtr() const {
1361     switch (getOpcode()) {
1362     case ISD::STORE:
1363     case ISD::VP_STORE:
1364     case ISD::MSTORE:
1365     case ISD::VP_SCATTER:
1366       return getOperand(2);
1367     case ISD::MGATHER:
1368     case ISD::MSCATTER:
1369       return getOperand(3);
1370     default:
1371       return getOperand(1);
1372     }
1373   }
1374 
1375   // Methods to support isa and dyn_cast
1376   static bool classof(const SDNode *N) {
1377     // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1378     // with either an intrinsic or a target opcode.
1379     switch (N->getOpcode()) {
1380     case ISD::LOAD:
1381     case ISD::STORE:
1382     case ISD::PREFETCH:
1383     case ISD::ATOMIC_CMP_SWAP:
1384     case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1385     case ISD::ATOMIC_SWAP:
1386     case ISD::ATOMIC_LOAD_ADD:
1387     case ISD::ATOMIC_LOAD_SUB:
1388     case ISD::ATOMIC_LOAD_AND:
1389     case ISD::ATOMIC_LOAD_CLR:
1390     case ISD::ATOMIC_LOAD_OR:
1391     case ISD::ATOMIC_LOAD_XOR:
1392     case ISD::ATOMIC_LOAD_NAND:
1393     case ISD::ATOMIC_LOAD_MIN:
1394     case ISD::ATOMIC_LOAD_MAX:
1395     case ISD::ATOMIC_LOAD_UMIN:
1396     case ISD::ATOMIC_LOAD_UMAX:
1397     case ISD::ATOMIC_LOAD_FADD:
1398     case ISD::ATOMIC_LOAD_FSUB:
1399     case ISD::ATOMIC_LOAD:
1400     case ISD::ATOMIC_STORE:
1401     case ISD::MLOAD:
1402     case ISD::MSTORE:
1403     case ISD::MGATHER:
1404     case ISD::MSCATTER:
1405     case ISD::VP_LOAD:
1406     case ISD::VP_STORE:
1407     case ISD::VP_GATHER:
1408     case ISD::VP_SCATTER:
1409       return true;
1410     default:
1411       return N->isMemIntrinsic() || N->isTargetMemoryOpcode();
1412     }
1413   }
1414 };
1415 
1416 /// This is an SDNode representing atomic operations.
1417 class AtomicSDNode : public MemSDNode {
1418 public:
1419   AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
1420                EVT MemVT, MachineMemOperand *MMO)
1421     : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1422     assert(((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) ||
1423             MMO->isAtomic()) && "then why are we using an AtomicSDNode?");
1424   }
1425 
1426   const SDValue &getBasePtr() const { return getOperand(1); }
1427   const SDValue &getVal() const { return getOperand(2); }
1428 
1429   /// Returns true if this SDNode represents cmpxchg atomic operation, false
1430   /// otherwise.
1431   bool isCompareAndSwap() const {
1432     unsigned Op = getOpcode();
1433     return Op == ISD::ATOMIC_CMP_SWAP ||
1434            Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1435   }
1436 
1437   /// For cmpxchg atomic operations, return the atomic ordering requirements
1438   /// when store does not occur.
1439   AtomicOrdering getFailureOrdering() const {
1440     assert(isCompareAndSwap() && "Must be cmpxchg operation");
1441     return MMO->getFailureOrdering();
1442   }
1443 
1444   // Methods to support isa and dyn_cast
1445   static bool classof(const SDNode *N) {
1446     return N->getOpcode() == ISD::ATOMIC_CMP_SWAP     ||
1447            N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1448            N->getOpcode() == ISD::ATOMIC_SWAP         ||
1449            N->getOpcode() == ISD::ATOMIC_LOAD_ADD     ||
1450            N->getOpcode() == ISD::ATOMIC_LOAD_SUB     ||
1451            N->getOpcode() == ISD::ATOMIC_LOAD_AND     ||
1452            N->getOpcode() == ISD::ATOMIC_LOAD_CLR     ||
1453            N->getOpcode() == ISD::ATOMIC_LOAD_OR      ||
1454            N->getOpcode() == ISD::ATOMIC_LOAD_XOR     ||
1455            N->getOpcode() == ISD::ATOMIC_LOAD_NAND    ||
1456            N->getOpcode() == ISD::ATOMIC_LOAD_MIN     ||
1457            N->getOpcode() == ISD::ATOMIC_LOAD_MAX     ||
1458            N->getOpcode() == ISD::ATOMIC_LOAD_UMIN    ||
1459            N->getOpcode() == ISD::ATOMIC_LOAD_UMAX    ||
1460            N->getOpcode() == ISD::ATOMIC_LOAD_FADD    ||
1461            N->getOpcode() == ISD::ATOMIC_LOAD_FSUB    ||
1462            N->getOpcode() == ISD::ATOMIC_LOAD         ||
1463            N->getOpcode() == ISD::ATOMIC_STORE;
1464   }
1465 };
1466 
1467 /// This SDNode is used for target intrinsics that touch
1468 /// memory and need an associated MachineMemOperand. Its opcode may be
1469 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1470 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1471 class MemIntrinsicSDNode : public MemSDNode {
1472 public:
1473   MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
1474                      SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
1475       : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
1476     SDNodeBits.IsMemIntrinsic = true;
1477   }
1478 
1479   // Methods to support isa and dyn_cast
1480   static bool classof(const SDNode *N) {
1481     // We lower some target intrinsics to their target opcode
1482     // early a node with a target opcode can be of this class
1483     return N->isMemIntrinsic()             ||
1484            N->getOpcode() == ISD::PREFETCH ||
1485            N->isTargetMemoryOpcode();
1486   }
1487 };
1488 
1489 /// This SDNode is used to implement the code generator
1490 /// support for the llvm IR shufflevector instruction.  It combines elements
1491 /// from two input vectors into a new input vector, with the selection and
1492 /// ordering of elements determined by an array of integers, referred to as
1493 /// the shuffle mask.  For input vectors of width N, mask indices of 0..N-1
1494 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1495 /// An index of -1 is treated as undef, such that the code generator may put
1496 /// any value in the corresponding element of the result.
1497 class ShuffleVectorSDNode : public SDNode {
1498   // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1499   // is freed when the SelectionDAG object is destroyed.
1500   const int *Mask;
1501 
1502 protected:
1503   friend class SelectionDAG;
1504 
1505   ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
1506       : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
1507 
1508 public:
1509   ArrayRef<int> getMask() const {
1510     EVT VT = getValueType(0);
1511     return makeArrayRef(Mask, VT.getVectorNumElements());
1512   }
1513 
1514   int getMaskElt(unsigned Idx) const {
1515     assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1516     return Mask[Idx];
1517   }
1518 
1519   bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1520 
1521   int getSplatIndex() const {
1522     assert(isSplat() && "Cannot get splat index for non-splat!");
1523     EVT VT = getValueType(0);
1524     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1525       if (Mask[i] >= 0)
1526         return Mask[i];
1527 
1528     // We can choose any index value here and be correct because all elements
1529     // are undefined. Return 0 for better potential for callers to simplify.
1530     return 0;
1531   }
1532 
1533   static bool isSplatMask(const int *Mask, EVT VT);
1534 
1535   /// Change values in a shuffle permute mask assuming
1536   /// the two vector operands have swapped position.
1537   static void commuteMask(MutableArrayRef<int> Mask) {
1538     unsigned NumElems = Mask.size();
1539     for (unsigned i = 0; i != NumElems; ++i) {
1540       int idx = Mask[i];
1541       if (idx < 0)
1542         continue;
1543       else if (idx < (int)NumElems)
1544         Mask[i] = idx + NumElems;
1545       else
1546         Mask[i] = idx - NumElems;
1547     }
1548   }
1549 
1550   static bool classof(const SDNode *N) {
1551     return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1552   }
1553 };
1554 
1555 class ConstantSDNode : public SDNode {
1556   friend class SelectionDAG;
1557 
1558   const ConstantInt *Value;
1559 
1560   ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
1561       : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(),
1562                getSDVTList(VT)),
1563         Value(val) {
1564     ConstantSDNodeBits.IsOpaque = isOpaque;
1565   }
1566 
1567 public:
1568   const ConstantInt *getConstantIntValue() const { return Value; }
1569   const APInt &getAPIntValue() const { return Value->getValue(); }
1570   uint64_t getZExtValue() const { return Value->getZExtValue(); }
1571   int64_t getSExtValue() const { return Value->getSExtValue(); }
1572   uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) {
1573     return Value->getLimitedValue(Limit);
1574   }
1575   MaybeAlign getMaybeAlignValue() const { return Value->getMaybeAlignValue(); }
1576   Align getAlignValue() const { return Value->getAlignValue(); }
1577 
1578   bool isOne() const { return Value->isOne(); }
1579   bool isZero() const { return Value->isZero(); }
1580   // NOTE: This is soft-deprecated.  Please use `isZero()` instead.
1581   bool isNullValue() const { return isZero(); }
1582   bool isAllOnes() const { return Value->isMinusOne(); }
1583   // NOTE: This is soft-deprecated.  Please use `isAllOnes()` instead.
1584   bool isAllOnesValue() const { return isAllOnes(); }
1585   bool isMaxSignedValue() const { return Value->isMaxValue(true); }
1586   bool isMinSignedValue() const { return Value->isMinValue(true); }
1587 
1588   bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
1589 
1590   static bool classof(const SDNode *N) {
1591     return N->getOpcode() == ISD::Constant ||
1592            N->getOpcode() == ISD::TargetConstant;
1593   }
1594 };
1595 
1596 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
1597   return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
1598 }
1599 
1600 const APInt &SDNode::getConstantOperandAPInt(unsigned Num) const {
1601   return cast<ConstantSDNode>(getOperand(Num))->getAPIntValue();
1602 }
1603 
1604 class ConstantFPSDNode : public SDNode {
1605   friend class SelectionDAG;
1606 
1607   const ConstantFP *Value;
1608 
1609   ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1610       : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0,
1611                DebugLoc(), getSDVTList(VT)),
1612         Value(val) {}
1613 
1614 public:
1615   const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1616   const ConstantFP *getConstantFPValue() const { return Value; }
1617 
1618   /// Return true if the value is positive or negative zero.
1619   bool isZero() const { return Value->isZero(); }
1620 
1621   /// Return true if the value is a NaN.
1622   bool isNaN() const { return Value->isNaN(); }
1623 
1624   /// Return true if the value is an infinity
1625   bool isInfinity() const { return Value->isInfinity(); }
1626 
1627   /// Return true if the value is negative.
1628   bool isNegative() const { return Value->isNegative(); }
1629 
1630   /// We don't rely on operator== working on double values, as
1631   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1632   /// As such, this method can be used to do an exact bit-for-bit comparison of
1633   /// two floating point values.
1634 
1635   /// We leave the version with the double argument here because it's just so
1636   /// convenient to write "2.0" and the like.  Without this function we'd
1637   /// have to duplicate its logic everywhere it's called.
1638   bool isExactlyValue(double V) const {
1639     return Value->getValueAPF().isExactlyValue(V);
1640   }
1641   bool isExactlyValue(const APFloat& V) const;
1642 
1643   static bool isValueValidForType(EVT VT, const APFloat& Val);
1644 
1645   static bool classof(const SDNode *N) {
1646     return N->getOpcode() == ISD::ConstantFP ||
1647            N->getOpcode() == ISD::TargetConstantFP;
1648   }
1649 };
1650 
1651 /// Returns true if \p V is a constant integer zero.
1652 bool isNullConstant(SDValue V);
1653 
1654 /// Returns true if \p V is an FP constant with a value of positive zero.
1655 bool isNullFPConstant(SDValue V);
1656 
1657 /// Returns true if \p V is an integer constant with all bits set.
1658 bool isAllOnesConstant(SDValue V);
1659 
1660 /// Returns true if \p V is a constant integer one.
1661 bool isOneConstant(SDValue V);
1662 
1663 /// Return the non-bitcasted source operand of \p V if it exists.
1664 /// If \p V is not a bitcasted value, it is returned as-is.
1665 SDValue peekThroughBitcasts(SDValue V);
1666 
1667 /// Return the non-bitcasted and one-use source operand of \p V if it exists.
1668 /// If \p V is not a bitcasted one-use value, it is returned as-is.
1669 SDValue peekThroughOneUseBitcasts(SDValue V);
1670 
1671 /// Return the non-extracted vector source operand of \p V if it exists.
1672 /// If \p V is not an extracted subvector, it is returned as-is.
1673 SDValue peekThroughExtractSubvectors(SDValue V);
1674 
1675 /// Returns true if \p V is a bitwise not operation. Assumes that an all ones
1676 /// constant is canonicalized to be operand 1.
1677 bool isBitwiseNot(SDValue V, bool AllowUndefs = false);
1678 
1679 /// Returns the SDNode if it is a constant splat BuildVector or constant int.
1680 ConstantSDNode *isConstOrConstSplat(SDValue N, bool AllowUndefs = false,
1681                                     bool AllowTruncation = false);
1682 
1683 /// Returns the SDNode if it is a demanded constant splat BuildVector or
1684 /// constant int.
1685 ConstantSDNode *isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
1686                                     bool AllowUndefs = false,
1687                                     bool AllowTruncation = false);
1688 
1689 /// Returns the SDNode if it is a constant splat BuildVector or constant float.
1690 ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, bool AllowUndefs = false);
1691 
1692 /// Returns the SDNode if it is a demanded constant splat BuildVector or
1693 /// constant float.
1694 ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, const APInt &DemandedElts,
1695                                         bool AllowUndefs = false);
1696 
1697 /// Return true if the value is a constant 0 integer or a splatted vector of
1698 /// a constant 0 integer (with no undefs by default).
1699 /// Build vector implicit truncation is not an issue for null values.
1700 bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false);
1701 
1702 /// Return true if the value is a constant 1 integer or a splatted vector of a
1703 /// constant 1 integer (with no undefs).
1704 /// Does not permit build vector implicit truncation.
1705 bool isOneOrOneSplat(SDValue V, bool AllowUndefs = false);
1706 
1707 /// Return true if the value is a constant -1 integer or a splatted vector of a
1708 /// constant -1 integer (with no undefs).
1709 /// Does not permit build vector implicit truncation.
1710 bool isAllOnesOrAllOnesSplat(SDValue V, bool AllowUndefs = false);
1711 
1712 /// Return true if \p V is either a integer or FP constant.
1713 inline bool isIntOrFPConstant(SDValue V) {
1714   return isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V);
1715 }
1716 
1717 class GlobalAddressSDNode : public SDNode {
1718   friend class SelectionDAG;
1719 
1720   const GlobalValue *TheGlobal;
1721   int64_t Offset;
1722   unsigned TargetFlags;
1723 
1724   GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
1725                       const GlobalValue *GA, EVT VT, int64_t o,
1726                       unsigned TF);
1727 
1728 public:
1729   const GlobalValue *getGlobal() const { return TheGlobal; }
1730   int64_t getOffset() const { return Offset; }
1731   unsigned getTargetFlags() const { return TargetFlags; }
1732   // Return the address space this GlobalAddress belongs to.
1733   unsigned getAddressSpace() const;
1734 
1735   static bool classof(const SDNode *N) {
1736     return N->getOpcode() == ISD::GlobalAddress ||
1737            N->getOpcode() == ISD::TargetGlobalAddress ||
1738            N->getOpcode() == ISD::GlobalTLSAddress ||
1739            N->getOpcode() == ISD::TargetGlobalTLSAddress;
1740   }
1741 };
1742 
1743 class FrameIndexSDNode : public SDNode {
1744   friend class SelectionDAG;
1745 
1746   int FI;
1747 
1748   FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1749     : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1750       0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1751   }
1752 
1753 public:
1754   int getIndex() const { return FI; }
1755 
1756   static bool classof(const SDNode *N) {
1757     return N->getOpcode() == ISD::FrameIndex ||
1758            N->getOpcode() == ISD::TargetFrameIndex;
1759   }
1760 };
1761 
1762 /// This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate
1763 /// the offet and size that are started/ended in the underlying FrameIndex.
1764 class LifetimeSDNode : public SDNode {
1765   friend class SelectionDAG;
1766   int64_t Size;
1767   int64_t Offset; // -1 if offset is unknown.
1768 
1769   LifetimeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl,
1770                  SDVTList VTs, int64_t Size, int64_t Offset)
1771       : SDNode(Opcode, Order, dl, VTs), Size(Size), Offset(Offset) {}
1772 public:
1773   int64_t getFrameIndex() const {
1774     return cast<FrameIndexSDNode>(getOperand(1))->getIndex();
1775   }
1776 
1777   bool hasOffset() const { return Offset >= 0; }
1778   int64_t getOffset() const {
1779     assert(hasOffset() && "offset is unknown");
1780     return Offset;
1781   }
1782   int64_t getSize() const {
1783     assert(hasOffset() && "offset is unknown");
1784     return Size;
1785   }
1786 
1787   // Methods to support isa and dyn_cast
1788   static bool classof(const SDNode *N) {
1789     return N->getOpcode() == ISD::LIFETIME_START ||
1790            N->getOpcode() == ISD::LIFETIME_END;
1791   }
1792 };
1793 
1794 /// This SDNode is used for PSEUDO_PROBE values, which are the function guid and
1795 /// the index of the basic block being probed. A pseudo probe serves as a place
1796 /// holder and will be removed at the end of compilation. It does not have any
1797 /// operand because we do not want the instruction selection to deal with any.
1798 class PseudoProbeSDNode : public SDNode {
1799   friend class SelectionDAG;
1800   uint64_t Guid;
1801   uint64_t Index;
1802   uint32_t Attributes;
1803 
1804   PseudoProbeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &Dl,
1805                     SDVTList VTs, uint64_t Guid, uint64_t Index, uint32_t Attr)
1806       : SDNode(Opcode, Order, Dl, VTs), Guid(Guid), Index(Index),
1807         Attributes(Attr) {}
1808 
1809 public:
1810   uint64_t getGuid() const { return Guid; }
1811   uint64_t getIndex() const { return Index; }
1812   uint32_t getAttributes() const { return Attributes; }
1813 
1814   // Methods to support isa and dyn_cast
1815   static bool classof(const SDNode *N) {
1816     return N->getOpcode() == ISD::PSEUDO_PROBE;
1817   }
1818 };
1819 
1820 class JumpTableSDNode : public SDNode {
1821   friend class SelectionDAG;
1822 
1823   int JTI;
1824   unsigned TargetFlags;
1825 
1826   JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned TF)
1827     : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1828       0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1829   }
1830 
1831 public:
1832   int getIndex() const { return JTI; }
1833   unsigned getTargetFlags() const { return TargetFlags; }
1834 
1835   static bool classof(const SDNode *N) {
1836     return N->getOpcode() == ISD::JumpTable ||
1837            N->getOpcode() == ISD::TargetJumpTable;
1838   }
1839 };
1840 
1841 class ConstantPoolSDNode : public SDNode {
1842   friend class SelectionDAG;
1843 
1844   union {
1845     const Constant *ConstVal;
1846     MachineConstantPoolValue *MachineCPVal;
1847   } Val;
1848   int Offset;  // It's a MachineConstantPoolValue if top bit is set.
1849   Align Alignment; // Minimum alignment requirement of CP.
1850   unsigned TargetFlags;
1851 
1852   ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1853                      Align Alignment, unsigned TF)
1854       : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1855                DebugLoc(), getSDVTList(VT)),
1856         Offset(o), Alignment(Alignment), TargetFlags(TF) {
1857     assert(Offset >= 0 && "Offset is too large");
1858     Val.ConstVal = c;
1859   }
1860 
1861   ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v, EVT VT, int o,
1862                      Align Alignment, unsigned TF)
1863       : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1864                DebugLoc(), getSDVTList(VT)),
1865         Offset(o), Alignment(Alignment), TargetFlags(TF) {
1866     assert(Offset >= 0 && "Offset is too large");
1867     Val.MachineCPVal = v;
1868     Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1869   }
1870 
1871 public:
1872   bool isMachineConstantPoolEntry() const {
1873     return Offset < 0;
1874   }
1875 
1876   const Constant *getConstVal() const {
1877     assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1878     return Val.ConstVal;
1879   }
1880 
1881   MachineConstantPoolValue *getMachineCPVal() const {
1882     assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1883     return Val.MachineCPVal;
1884   }
1885 
1886   int getOffset() const {
1887     return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1888   }
1889 
1890   // Return the alignment of this constant pool object, which is either 0 (for
1891   // default alignment) or the desired value.
1892   Align getAlign() const { return Alignment; }
1893   unsigned getTargetFlags() const { return TargetFlags; }
1894 
1895   Type *getType() const;
1896 
1897   static bool classof(const SDNode *N) {
1898     return N->getOpcode() == ISD::ConstantPool ||
1899            N->getOpcode() == ISD::TargetConstantPool;
1900   }
1901 };
1902 
1903 /// Completely target-dependent object reference.
1904 class TargetIndexSDNode : public SDNode {
1905   friend class SelectionDAG;
1906 
1907   unsigned TargetFlags;
1908   int Index;
1909   int64_t Offset;
1910 
1911 public:
1912   TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned TF)
1913       : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1914         TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1915 
1916   unsigned getTargetFlags() const { return TargetFlags; }
1917   int getIndex() const { return Index; }
1918   int64_t getOffset() const { return Offset; }
1919 
1920   static bool classof(const SDNode *N) {
1921     return N->getOpcode() == ISD::TargetIndex;
1922   }
1923 };
1924 
1925 class BasicBlockSDNode : public SDNode {
1926   friend class SelectionDAG;
1927 
1928   MachineBasicBlock *MBB;
1929 
1930   /// Debug info is meaningful and potentially useful here, but we create
1931   /// blocks out of order when they're jumped to, which makes it a bit
1932   /// harder.  Let's see if we need it first.
1933   explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1934     : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1935   {}
1936 
1937 public:
1938   MachineBasicBlock *getBasicBlock() const { return MBB; }
1939 
1940   static bool classof(const SDNode *N) {
1941     return N->getOpcode() == ISD::BasicBlock;
1942   }
1943 };
1944 
1945 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1946 class BuildVectorSDNode : public SDNode {
1947 public:
1948   // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1949   explicit BuildVectorSDNode() = delete;
1950 
1951   /// Check if this is a constant splat, and if so, find the
1952   /// smallest element size that splats the vector.  If MinSplatBits is
1953   /// nonzero, the element size must be at least that large.  Note that the
1954   /// splat element may be the entire vector (i.e., a one element vector).
1955   /// Returns the splat element value in SplatValue.  Any undefined bits in
1956   /// that value are zero, and the corresponding bits in the SplatUndef mask
1957   /// are set.  The SplatBitSize value is set to the splat element size in
1958   /// bits.  HasAnyUndefs is set to true if any bits in the vector are
1959   /// undefined.  isBigEndian describes the endianness of the target.
1960   bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1961                        unsigned &SplatBitSize, bool &HasAnyUndefs,
1962                        unsigned MinSplatBits = 0,
1963                        bool isBigEndian = false) const;
1964 
1965   /// Returns the demanded splatted value or a null value if this is not a
1966   /// splat.
1967   ///
1968   /// The DemandedElts mask indicates the elements that must be in the splat.
1969   /// If passed a non-null UndefElements bitvector, it will resize it to match
1970   /// the vector width and set the bits where elements are undef.
1971   SDValue getSplatValue(const APInt &DemandedElts,
1972                         BitVector *UndefElements = nullptr) const;
1973 
1974   /// Returns the splatted value or a null value if this is not a splat.
1975   ///
1976   /// If passed a non-null UndefElements bitvector, it will resize it to match
1977   /// the vector width and set the bits where elements are undef.
1978   SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1979 
1980   /// Find the shortest repeating sequence of values in the build vector.
1981   ///
1982   /// e.g. { u, X, u, X, u, u, X, u } -> { X }
1983   ///      { X, Y, u, Y, u, u, X, u } -> { X, Y }
1984   ///
1985   /// Currently this must be a power-of-2 build vector.
1986   /// The DemandedElts mask indicates the elements that must be present,
1987   /// undemanded elements in Sequence may be null (SDValue()). If passed a
1988   /// non-null UndefElements bitvector, it will resize it to match the original
1989   /// vector width and set the bits where elements are undef. If result is
1990   /// false, Sequence will be empty.
1991   bool getRepeatedSequence(const APInt &DemandedElts,
1992                            SmallVectorImpl<SDValue> &Sequence,
1993                            BitVector *UndefElements = nullptr) const;
1994 
1995   /// Find the shortest repeating sequence of values in the build vector.
1996   ///
1997   /// e.g. { u, X, u, X, u, u, X, u } -> { X }
1998   ///      { X, Y, u, Y, u, u, X, u } -> { X, Y }
1999   ///
2000   /// Currently this must be a power-of-2 build vector.
2001   /// If passed a non-null UndefElements bitvector, it will resize it to match
2002   /// the original vector width and set the bits where elements are undef.
2003   /// If result is false, Sequence will be empty.
2004   bool getRepeatedSequence(SmallVectorImpl<SDValue> &Sequence,
2005                            BitVector *UndefElements = nullptr) const;
2006 
2007   /// Returns the demanded splatted constant or null if this is not a constant
2008   /// splat.
2009   ///
2010   /// The DemandedElts mask indicates the elements that must be in the splat.
2011   /// If passed a non-null UndefElements bitvector, it will resize it to match
2012   /// the vector width and set the bits where elements are undef.
2013   ConstantSDNode *
2014   getConstantSplatNode(const APInt &DemandedElts,
2015                        BitVector *UndefElements = nullptr) const;
2016 
2017   /// Returns the splatted constant or null if this is not a constant
2018   /// splat.
2019   ///
2020   /// If passed a non-null UndefElements bitvector, it will resize it to match
2021   /// the vector width and set the bits where elements are undef.
2022   ConstantSDNode *
2023   getConstantSplatNode(BitVector *UndefElements = nullptr) const;
2024 
2025   /// Returns the demanded splatted constant FP or null if this is not a
2026   /// constant FP splat.
2027   ///
2028   /// The DemandedElts mask indicates the elements that must be in the splat.
2029   /// If passed a non-null UndefElements bitvector, it will resize it to match
2030   /// the vector width and set the bits where elements are undef.
2031   ConstantFPSDNode *
2032   getConstantFPSplatNode(const APInt &DemandedElts,
2033                          BitVector *UndefElements = nullptr) const;
2034 
2035   /// Returns the splatted constant FP or null if this is not a constant
2036   /// FP splat.
2037   ///
2038   /// If passed a non-null UndefElements bitvector, it will resize it to match
2039   /// the vector width and set the bits where elements are undef.
2040   ConstantFPSDNode *
2041   getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
2042 
2043   /// If this is a constant FP splat and the splatted constant FP is an
2044   /// exact power or 2, return the log base 2 integer value.  Otherwise,
2045   /// return -1.
2046   ///
2047   /// The BitWidth specifies the necessary bit precision.
2048   int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
2049                                           uint32_t BitWidth) const;
2050 
2051   /// Extract the raw bit data from a build vector of Undef, Constant or
2052   /// ConstantFP node elements. Each raw bit element will be \p
2053   /// DstEltSizeInBits wide, undef elements are treated as zero, and entirely
2054   /// undefined elements are flagged in \p UndefElements.
2055   bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
2056                           SmallVectorImpl<APInt> &RawBitElements,
2057                           BitVector &UndefElements) const;
2058 
2059   bool isConstant() const;
2060 
2061   /// Recast bit data \p SrcBitElements to \p DstEltSizeInBits wide elements.
2062   /// Undef elements are treated as zero, and entirely undefined elements are
2063   /// flagged in \p DstUndefElements.
2064   static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
2065                             SmallVectorImpl<APInt> &DstBitElements,
2066                             ArrayRef<APInt> SrcBitElements,
2067                             BitVector &DstUndefElements,
2068                             const BitVector &SrcUndefElements);
2069 
2070   static bool classof(const SDNode *N) {
2071     return N->getOpcode() == ISD::BUILD_VECTOR;
2072   }
2073 };
2074 
2075 /// An SDNode that holds an arbitrary LLVM IR Value. This is
2076 /// used when the SelectionDAG needs to make a simple reference to something
2077 /// in the LLVM IR representation.
2078 ///
2079 class SrcValueSDNode : public SDNode {
2080   friend class SelectionDAG;
2081 
2082   const Value *V;
2083 
2084   /// Create a SrcValue for a general value.
2085   explicit SrcValueSDNode(const Value *v)
2086     : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
2087 
2088 public:
2089   /// Return the contained Value.
2090   const Value *getValue() const { return V; }
2091 
2092   static bool classof(const SDNode *N) {
2093     return N->getOpcode() == ISD::SRCVALUE;
2094   }
2095 };
2096 
2097 class MDNodeSDNode : public SDNode {
2098   friend class SelectionDAG;
2099 
2100   const MDNode *MD;
2101 
2102   explicit MDNodeSDNode(const MDNode *md)
2103   : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
2104   {}
2105 
2106 public:
2107   const MDNode *getMD() const { return MD; }
2108 
2109   static bool classof(const SDNode *N) {
2110     return N->getOpcode() == ISD::MDNODE_SDNODE;
2111   }
2112 };
2113 
2114 class RegisterSDNode : public SDNode {
2115   friend class SelectionDAG;
2116 
2117   Register Reg;
2118 
2119   RegisterSDNode(Register reg, EVT VT)
2120     : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
2121 
2122 public:
2123   Register getReg() const { return Reg; }
2124 
2125   static bool classof(const SDNode *N) {
2126     return N->getOpcode() == ISD::Register;
2127   }
2128 };
2129 
2130 class RegisterMaskSDNode : public SDNode {
2131   friend class SelectionDAG;
2132 
2133   // The memory for RegMask is not owned by the node.
2134   const uint32_t *RegMask;
2135 
2136   RegisterMaskSDNode(const uint32_t *mask)
2137     : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
2138       RegMask(mask) {}
2139 
2140 public:
2141   const uint32_t *getRegMask() const { return RegMask; }
2142 
2143   static bool classof(const SDNode *N) {
2144     return N->getOpcode() == ISD::RegisterMask;
2145   }
2146 };
2147 
2148 class BlockAddressSDNode : public SDNode {
2149   friend class SelectionDAG;
2150 
2151   const BlockAddress *BA;
2152   int64_t Offset;
2153   unsigned TargetFlags;
2154 
2155   BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
2156                      int64_t o, unsigned Flags)
2157     : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
2158              BA(ba), Offset(o), TargetFlags(Flags) {}
2159 
2160 public:
2161   const BlockAddress *getBlockAddress() const { return BA; }
2162   int64_t getOffset() const { return Offset; }
2163   unsigned getTargetFlags() const { return TargetFlags; }
2164 
2165   static bool classof(const SDNode *N) {
2166     return N->getOpcode() == ISD::BlockAddress ||
2167            N->getOpcode() == ISD::TargetBlockAddress;
2168   }
2169 };
2170 
2171 class LabelSDNode : public SDNode {
2172   friend class SelectionDAG;
2173 
2174   MCSymbol *Label;
2175 
2176   LabelSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, MCSymbol *L)
2177       : SDNode(Opcode, Order, dl, getSDVTList(MVT::Other)), Label(L) {
2178     assert(LabelSDNode::classof(this) && "not a label opcode");
2179   }
2180 
2181 public:
2182   MCSymbol *getLabel() const { return Label; }
2183 
2184   static bool classof(const SDNode *N) {
2185     return N->getOpcode() == ISD::EH_LABEL ||
2186            N->getOpcode() == ISD::ANNOTATION_LABEL;
2187   }
2188 };
2189 
2190 class ExternalSymbolSDNode : public SDNode {
2191   friend class SelectionDAG;
2192 
2193   const char *Symbol;
2194   unsigned TargetFlags;
2195 
2196   ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned TF, EVT VT)
2197       : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, 0,
2198                DebugLoc(), getSDVTList(VT)),
2199         Symbol(Sym), TargetFlags(TF) {}
2200 
2201 public:
2202   const char *getSymbol() const { return Symbol; }
2203   unsigned getTargetFlags() const { return TargetFlags; }
2204 
2205   static bool classof(const SDNode *N) {
2206     return N->getOpcode() == ISD::ExternalSymbol ||
2207            N->getOpcode() == ISD::TargetExternalSymbol;
2208   }
2209 };
2210 
2211 class MCSymbolSDNode : public SDNode {
2212   friend class SelectionDAG;
2213 
2214   MCSymbol *Symbol;
2215 
2216   MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
2217       : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
2218 
2219 public:
2220   MCSymbol *getMCSymbol() const { return Symbol; }
2221 
2222   static bool classof(const SDNode *N) {
2223     return N->getOpcode() == ISD::MCSymbol;
2224   }
2225 };
2226 
2227 class CondCodeSDNode : public SDNode {
2228   friend class SelectionDAG;
2229 
2230   ISD::CondCode Condition;
2231 
2232   explicit CondCodeSDNode(ISD::CondCode Cond)
2233     : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2234       Condition(Cond) {}
2235 
2236 public:
2237   ISD::CondCode get() const { return Condition; }
2238 
2239   static bool classof(const SDNode *N) {
2240     return N->getOpcode() == ISD::CONDCODE;
2241   }
2242 };
2243 
2244 /// This class is used to represent EVT's, which are used
2245 /// to parameterize some operations.
2246 class VTSDNode : public SDNode {
2247   friend class SelectionDAG;
2248 
2249   EVT ValueType;
2250 
2251   explicit VTSDNode(EVT VT)
2252     : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2253       ValueType(VT) {}
2254 
2255 public:
2256   EVT getVT() const { return ValueType; }
2257 
2258   static bool classof(const SDNode *N) {
2259     return N->getOpcode() == ISD::VALUETYPE;
2260   }
2261 };
2262 
2263 /// Base class for LoadSDNode and StoreSDNode
2264 class LSBaseSDNode : public MemSDNode {
2265 public:
2266   LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
2267                SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
2268                MachineMemOperand *MMO)
2269       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2270     LSBaseSDNodeBits.AddressingMode = AM;
2271     assert(getAddressingMode() == AM && "Value truncated");
2272   }
2273 
2274   const SDValue &getOffset() const {
2275     return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
2276   }
2277 
2278   /// Return the addressing mode for this load or store:
2279   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2280   ISD::MemIndexedMode getAddressingMode() const {
2281     return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2282   }
2283 
2284   /// Return true if this is a pre/post inc/dec load/store.
2285   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2286 
2287   /// Return true if this is NOT a pre/post inc/dec load/store.
2288   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2289 
2290   static bool classof(const SDNode *N) {
2291     return N->getOpcode() == ISD::LOAD ||
2292            N->getOpcode() == ISD::STORE;
2293   }
2294 };
2295 
2296 /// This class is used to represent ISD::LOAD nodes.
2297 class LoadSDNode : public LSBaseSDNode {
2298   friend class SelectionDAG;
2299 
2300   LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2301              ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
2302              MachineMemOperand *MMO)
2303       : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
2304     LoadSDNodeBits.ExtTy = ETy;
2305     assert(readMem() && "Load MachineMemOperand is not a load!");
2306     assert(!writeMem() && "Load MachineMemOperand is a store!");
2307   }
2308 
2309 public:
2310   /// Return whether this is a plain node,
2311   /// or one of the varieties of value-extending loads.
2312   ISD::LoadExtType getExtensionType() const {
2313     return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2314   }
2315 
2316   const SDValue &getBasePtr() const { return getOperand(1); }
2317   const SDValue &getOffset() const { return getOperand(2); }
2318 
2319   static bool classof(const SDNode *N) {
2320     return N->getOpcode() == ISD::LOAD;
2321   }
2322 };
2323 
2324 /// This class is used to represent ISD::STORE nodes.
2325 class StoreSDNode : public LSBaseSDNode {
2326   friend class SelectionDAG;
2327 
2328   StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2329               ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
2330               MachineMemOperand *MMO)
2331       : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
2332     StoreSDNodeBits.IsTruncating = isTrunc;
2333     assert(!readMem() && "Store MachineMemOperand is a load!");
2334     assert(writeMem() && "Store MachineMemOperand is not a store!");
2335   }
2336 
2337 public:
2338   /// Return true if the op does a truncation before store.
2339   /// For integers this is the same as doing a TRUNCATE and storing the result.
2340   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2341   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2342   void setTruncatingStore(bool Truncating) {
2343     StoreSDNodeBits.IsTruncating = Truncating;
2344   }
2345 
2346   const SDValue &getValue() const { return getOperand(1); }
2347   const SDValue &getBasePtr() const { return getOperand(2); }
2348   const SDValue &getOffset() const { return getOperand(3); }
2349 
2350   static bool classof(const SDNode *N) {
2351     return N->getOpcode() == ISD::STORE;
2352   }
2353 };
2354 
2355 /// This base class is used to represent VP_LOAD and VP_STORE nodes
2356 class VPLoadStoreSDNode : public MemSDNode {
2357 public:
2358   friend class SelectionDAG;
2359 
2360   VPLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
2361                     SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
2362                     MachineMemOperand *MMO)
2363       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2364     LSBaseSDNodeBits.AddressingMode = AM;
2365     assert(getAddressingMode() == AM && "Value truncated");
2366   }
2367 
2368   // VPLoadSDNode (Chain, Ptr, Offset, Mask, EVL)
2369   // VPStoreSDNode (Chain, Data, Ptr, Offset, Mask, EVL)
2370   // Mask is a vector of i1 elements;
2371   // the type of EVL is TLI.getVPExplicitVectorLengthTy().
2372   const SDValue &getOffset() const {
2373     return getOperand(getOpcode() == ISD::VP_LOAD ? 2 : 3);
2374   }
2375   const SDValue &getBasePtr() const {
2376     return getOperand(getOpcode() == ISD::VP_LOAD ? 1 : 2);
2377   }
2378   const SDValue &getMask() const {
2379     return getOperand(getOpcode() == ISD::VP_LOAD ? 3 : 4);
2380   }
2381   const SDValue &getVectorLength() const {
2382     return getOperand(getOpcode() == ISD::VP_LOAD ? 4 : 5);
2383   }
2384 
2385   /// Return the addressing mode for this load or store:
2386   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2387   ISD::MemIndexedMode getAddressingMode() const {
2388     return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2389   }
2390 
2391   /// Return true if this is a pre/post inc/dec load/store.
2392   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2393 
2394   /// Return true if this is NOT a pre/post inc/dec load/store.
2395   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2396 
2397   static bool classof(const SDNode *N) {
2398     return N->getOpcode() == ISD::VP_LOAD || N->getOpcode() == ISD::VP_STORE;
2399   }
2400 };
2401 
2402 /// This class is used to represent a VP_LOAD node
2403 class VPLoadSDNode : public VPLoadStoreSDNode {
2404 public:
2405   friend class SelectionDAG;
2406 
2407   VPLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2408                ISD::MemIndexedMode AM, ISD::LoadExtType ETy, bool isExpanding,
2409                EVT MemVT, MachineMemOperand *MMO)
2410       : VPLoadStoreSDNode(ISD::VP_LOAD, Order, dl, VTs, AM, MemVT, MMO) {
2411     LoadSDNodeBits.ExtTy = ETy;
2412     LoadSDNodeBits.IsExpanding = isExpanding;
2413   }
2414 
2415   ISD::LoadExtType getExtensionType() const {
2416     return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2417   }
2418 
2419   const SDValue &getBasePtr() const { return getOperand(1); }
2420   const SDValue &getOffset() const { return getOperand(2); }
2421   const SDValue &getMask() const { return getOperand(3); }
2422   const SDValue &getVectorLength() const { return getOperand(4); }
2423 
2424   static bool classof(const SDNode *N) {
2425     return N->getOpcode() == ISD::VP_LOAD;
2426   }
2427   bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2428 };
2429 
2430 /// This class is used to represent a VP_STORE node
2431 class VPStoreSDNode : public VPLoadStoreSDNode {
2432 public:
2433   friend class SelectionDAG;
2434 
2435   VPStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2436                 ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
2437                 EVT MemVT, MachineMemOperand *MMO)
2438       : VPLoadStoreSDNode(ISD::VP_STORE, Order, dl, VTs, AM, MemVT, MMO) {
2439     StoreSDNodeBits.IsTruncating = isTrunc;
2440     StoreSDNodeBits.IsCompressing = isCompressing;
2441   }
2442 
2443   /// Return true if this is a truncating store.
2444   /// For integers this is the same as doing a TRUNCATE and storing the result.
2445   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2446   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2447 
2448   /// Returns true if the op does a compression to the vector before storing.
2449   /// The node contiguously stores the active elements (integers or floats)
2450   /// in src (those with their respective bit set in writemask k) to unaligned
2451   /// memory at base_addr.
2452   bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2453 
2454   const SDValue &getValue() const { return getOperand(1); }
2455   const SDValue &getBasePtr() const { return getOperand(2); }
2456   const SDValue &getOffset() const { return getOperand(3); }
2457   const SDValue &getMask() const { return getOperand(4); }
2458   const SDValue &getVectorLength() const { return getOperand(5); }
2459 
2460   static bool classof(const SDNode *N) {
2461     return N->getOpcode() == ISD::VP_STORE;
2462   }
2463 };
2464 
2465 /// This base class is used to represent MLOAD and MSTORE nodes
2466 class MaskedLoadStoreSDNode : public MemSDNode {
2467 public:
2468   friend class SelectionDAG;
2469 
2470   MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2471                         const DebugLoc &dl, SDVTList VTs,
2472                         ISD::MemIndexedMode AM, EVT MemVT,
2473                         MachineMemOperand *MMO)
2474       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2475     LSBaseSDNodeBits.AddressingMode = AM;
2476     assert(getAddressingMode() == AM && "Value truncated");
2477   }
2478 
2479   // MaskedLoadSDNode (Chain, ptr, offset, mask, passthru)
2480   // MaskedStoreSDNode (Chain, data, ptr, offset, mask)
2481   // Mask is a vector of i1 elements
2482   const SDValue &getOffset() const {
2483     return getOperand(getOpcode() == ISD::MLOAD ? 2 : 3);
2484   }
2485   const SDValue &getMask() const {
2486     return getOperand(getOpcode() == ISD::MLOAD ? 3 : 4);
2487   }
2488 
2489   /// Return the addressing mode for this load or store:
2490   /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2491   ISD::MemIndexedMode getAddressingMode() const {
2492     return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2493   }
2494 
2495   /// Return true if this is a pre/post inc/dec load/store.
2496   bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2497 
2498   /// Return true if this is NOT a pre/post inc/dec load/store.
2499   bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2500 
2501   static bool classof(const SDNode *N) {
2502     return N->getOpcode() == ISD::MLOAD ||
2503            N->getOpcode() == ISD::MSTORE;
2504   }
2505 };
2506 
2507 /// This class is used to represent an MLOAD node
2508 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
2509 public:
2510   friend class SelectionDAG;
2511 
2512   MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2513                    ISD::MemIndexedMode AM, ISD::LoadExtType ETy,
2514                    bool IsExpanding, EVT MemVT, MachineMemOperand *MMO)
2515       : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, AM, MemVT, MMO) {
2516     LoadSDNodeBits.ExtTy = ETy;
2517     LoadSDNodeBits.IsExpanding = IsExpanding;
2518   }
2519 
2520   ISD::LoadExtType getExtensionType() const {
2521     return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2522   }
2523 
2524   const SDValue &getBasePtr() const { return getOperand(1); }
2525   const SDValue &getOffset() const { return getOperand(2); }
2526   const SDValue &getMask() const { return getOperand(3); }
2527   const SDValue &getPassThru() const { return getOperand(4); }
2528 
2529   static bool classof(const SDNode *N) {
2530     return N->getOpcode() == ISD::MLOAD;
2531   }
2532 
2533   bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2534 };
2535 
2536 /// This class is used to represent an MSTORE node
2537 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2538 public:
2539   friend class SelectionDAG;
2540 
2541   MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2542                     ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
2543                     EVT MemVT, MachineMemOperand *MMO)
2544       : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, AM, MemVT, MMO) {
2545     StoreSDNodeBits.IsTruncating = isTrunc;
2546     StoreSDNodeBits.IsCompressing = isCompressing;
2547   }
2548 
2549   /// Return true if the op does a truncation before store.
2550   /// For integers this is the same as doing a TRUNCATE and storing the result.
2551   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2552   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2553 
2554   /// Returns true if the op does a compression to the vector before storing.
2555   /// The node contiguously stores the active elements (integers or floats)
2556   /// in src (those with their respective bit set in writemask k) to unaligned
2557   /// memory at base_addr.
2558   bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2559 
2560   const SDValue &getValue() const { return getOperand(1); }
2561   const SDValue &getBasePtr() const { return getOperand(2); }
2562   const SDValue &getOffset() const { return getOperand(3); }
2563   const SDValue &getMask() const { return getOperand(4); }
2564 
2565   static bool classof(const SDNode *N) {
2566     return N->getOpcode() == ISD::MSTORE;
2567   }
2568 };
2569 
2570 /// This is a base class used to represent
2571 /// VP_GATHER and VP_SCATTER nodes
2572 ///
2573 class VPGatherScatterSDNode : public MemSDNode {
2574 public:
2575   friend class SelectionDAG;
2576 
2577   VPGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2578                         const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2579                         MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2580       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2581     LSBaseSDNodeBits.AddressingMode = IndexType;
2582     assert(getIndexType() == IndexType && "Value truncated");
2583   }
2584 
2585   /// How is Index applied to BasePtr when computing addresses.
2586   ISD::MemIndexType getIndexType() const {
2587     return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
2588   }
2589   bool isIndexScaled() const {
2590     return (getIndexType() == ISD::SIGNED_SCALED) ||
2591            (getIndexType() == ISD::UNSIGNED_SCALED);
2592   }
2593   bool isIndexSigned() const {
2594     return (getIndexType() == ISD::SIGNED_SCALED) ||
2595            (getIndexType() == ISD::SIGNED_UNSCALED);
2596   }
2597 
2598   // In the both nodes address is Op1, mask is Op2:
2599   // VPGatherSDNode  (Chain, base, index, scale, mask, vlen)
2600   // VPScatterSDNode (Chain, value, base, index, scale, mask, vlen)
2601   // Mask is a vector of i1 elements
2602   const SDValue &getBasePtr() const {
2603     return getOperand((getOpcode() == ISD::VP_GATHER) ? 1 : 2);
2604   }
2605   const SDValue &getIndex() const {
2606     return getOperand((getOpcode() == ISD::VP_GATHER) ? 2 : 3);
2607   }
2608   const SDValue &getScale() const {
2609     return getOperand((getOpcode() == ISD::VP_GATHER) ? 3 : 4);
2610   }
2611   const SDValue &getMask() const {
2612     return getOperand((getOpcode() == ISD::VP_GATHER) ? 4 : 5);
2613   }
2614   const SDValue &getVectorLength() const {
2615     return getOperand((getOpcode() == ISD::VP_GATHER) ? 5 : 6);
2616   }
2617 
2618   static bool classof(const SDNode *N) {
2619     return N->getOpcode() == ISD::VP_GATHER ||
2620            N->getOpcode() == ISD::VP_SCATTER;
2621   }
2622 };
2623 
2624 /// This class is used to represent an VP_GATHER node
2625 ///
2626 class VPGatherSDNode : public VPGatherScatterSDNode {
2627 public:
2628   friend class SelectionDAG;
2629 
2630   VPGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2631                  MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2632       : VPGatherScatterSDNode(ISD::VP_GATHER, Order, dl, VTs, MemVT, MMO,
2633                               IndexType) {}
2634 
2635   static bool classof(const SDNode *N) {
2636     return N->getOpcode() == ISD::VP_GATHER;
2637   }
2638 };
2639 
2640 /// This class is used to represent an VP_SCATTER node
2641 ///
2642 class VPScatterSDNode : public VPGatherScatterSDNode {
2643 public:
2644   friend class SelectionDAG;
2645 
2646   VPScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2647                   MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2648       : VPGatherScatterSDNode(ISD::VP_SCATTER, Order, dl, VTs, MemVT, MMO,
2649                               IndexType) {}
2650 
2651   const SDValue &getValue() const { return getOperand(1); }
2652 
2653   static bool classof(const SDNode *N) {
2654     return N->getOpcode() == ISD::VP_SCATTER;
2655   }
2656 };
2657 
2658 /// This is a base class used to represent
2659 /// MGATHER and MSCATTER nodes
2660 ///
2661 class MaskedGatherScatterSDNode : public MemSDNode {
2662 public:
2663   friend class SelectionDAG;
2664 
2665   MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2666                             const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2667                             MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2668       : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2669     LSBaseSDNodeBits.AddressingMode = IndexType;
2670     assert(getIndexType() == IndexType && "Value truncated");
2671   }
2672 
2673   /// How is Index applied to BasePtr when computing addresses.
2674   ISD::MemIndexType getIndexType() const {
2675     return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
2676   }
2677   void setIndexType(ISD::MemIndexType IndexType) {
2678     LSBaseSDNodeBits.AddressingMode = IndexType;
2679   }
2680   bool isIndexScaled() const {
2681     return (getIndexType() == ISD::SIGNED_SCALED) ||
2682            (getIndexType() == ISD::UNSIGNED_SCALED);
2683   }
2684   bool isIndexSigned() const {
2685     return (getIndexType() == ISD::SIGNED_SCALED) ||
2686            (getIndexType() == ISD::SIGNED_UNSCALED);
2687   }
2688 
2689   // In the both nodes address is Op1, mask is Op2:
2690   // MaskedGatherSDNode  (Chain, passthru, mask, base, index, scale)
2691   // MaskedScatterSDNode (Chain, value, mask, base, index, scale)
2692   // Mask is a vector of i1 elements
2693   const SDValue &getBasePtr() const { return getOperand(3); }
2694   const SDValue &getIndex()   const { return getOperand(4); }
2695   const SDValue &getMask()    const { return getOperand(2); }
2696   const SDValue &getScale()   const { return getOperand(5); }
2697 
2698   static bool classof(const SDNode *N) {
2699     return N->getOpcode() == ISD::MGATHER ||
2700            N->getOpcode() == ISD::MSCATTER;
2701   }
2702 };
2703 
2704 /// This class is used to represent an MGATHER node
2705 ///
2706 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2707 public:
2708   friend class SelectionDAG;
2709 
2710   MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2711                      EVT MemVT, MachineMemOperand *MMO,
2712                      ISD::MemIndexType IndexType, ISD::LoadExtType ETy)
2713       : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO,
2714                                   IndexType) {
2715     LoadSDNodeBits.ExtTy = ETy;
2716   }
2717 
2718   const SDValue &getPassThru() const { return getOperand(1); }
2719 
2720   ISD::LoadExtType getExtensionType() const {
2721     return ISD::LoadExtType(LoadSDNodeBits.ExtTy);
2722   }
2723 
2724   static bool classof(const SDNode *N) {
2725     return N->getOpcode() == ISD::MGATHER;
2726   }
2727 };
2728 
2729 /// This class is used to represent an MSCATTER node
2730 ///
2731 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2732 public:
2733   friend class SelectionDAG;
2734 
2735   MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2736                       EVT MemVT, MachineMemOperand *MMO,
2737                       ISD::MemIndexType IndexType, bool IsTrunc)
2738       : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO,
2739                                   IndexType) {
2740     StoreSDNodeBits.IsTruncating = IsTrunc;
2741   }
2742 
2743   /// Return true if the op does a truncation before store.
2744   /// For integers this is the same as doing a TRUNCATE and storing the result.
2745   /// For floats, it is the same as doing an FP_ROUND and storing the result.
2746   bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2747 
2748   const SDValue &getValue() const { return getOperand(1); }
2749 
2750   static bool classof(const SDNode *N) {
2751     return N->getOpcode() == ISD::MSCATTER;
2752   }
2753 };
2754 
2755 /// An SDNode that represents everything that will be needed
2756 /// to construct a MachineInstr. These nodes are created during the
2757 /// instruction selection proper phase.
2758 ///
2759 /// Note that the only supported way to set the `memoperands` is by calling the
2760 /// `SelectionDAG::setNodeMemRefs` function as the memory management happens
2761 /// inside the DAG rather than in the node.
2762 class MachineSDNode : public SDNode {
2763 private:
2764   friend class SelectionDAG;
2765 
2766   MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
2767       : SDNode(Opc, Order, DL, VTs) {}
2768 
2769   // We use a pointer union between a single `MachineMemOperand` pointer and
2770   // a pointer to an array of `MachineMemOperand` pointers. This is null when
2771   // the number of these is zero, the single pointer variant used when the
2772   // number is one, and the array is used for larger numbers.
2773   //
2774   // The array is allocated via the `SelectionDAG`'s allocator and so will
2775   // always live until the DAG is cleaned up and doesn't require ownership here.
2776   //
2777   // We can't use something simpler like `TinyPtrVector` here because `SDNode`
2778   // subclasses aren't managed in a conforming C++ manner. See the comments on
2779   // `SelectionDAG::MorphNodeTo` which details what all goes on, but the
2780   // constraint here is that these don't manage memory with their constructor or
2781   // destructor and can be initialized to a good state even if they start off
2782   // uninitialized.
2783   PointerUnion<MachineMemOperand *, MachineMemOperand **> MemRefs = {};
2784 
2785   // Note that this could be folded into the above `MemRefs` member if doing so
2786   // is advantageous at some point. We don't need to store this in most cases.
2787   // However, at the moment this doesn't appear to make the allocation any
2788   // smaller and makes the code somewhat simpler to read.
2789   int NumMemRefs = 0;
2790 
2791 public:
2792   using mmo_iterator = ArrayRef<MachineMemOperand *>::const_iterator;
2793 
2794   ArrayRef<MachineMemOperand *> memoperands() const {
2795     // Special case the common cases.
2796     if (NumMemRefs == 0)
2797       return {};
2798     if (NumMemRefs == 1)
2799       return makeArrayRef(MemRefs.getAddrOfPtr1(), 1);
2800 
2801     // Otherwise we have an actual array.
2802     return makeArrayRef(MemRefs.get<MachineMemOperand **>(), NumMemRefs);
2803   }
2804   mmo_iterator memoperands_begin() const { return memoperands().begin(); }
2805   mmo_iterator memoperands_end() const { return memoperands().end(); }
2806   bool memoperands_empty() const { return memoperands().empty(); }
2807 
2808   /// Clear out the memory reference descriptor list.
2809   void clearMemRefs() {
2810     MemRefs = nullptr;
2811     NumMemRefs = 0;
2812   }
2813 
2814   static bool classof(const SDNode *N) {
2815     return N->isMachineOpcode();
2816   }
2817 };
2818 
2819 /// An SDNode that records if a register contains a value that is guaranteed to
2820 /// be aligned accordingly.
2821 class AssertAlignSDNode : public SDNode {
2822   Align Alignment;
2823 
2824 public:
2825   AssertAlignSDNode(unsigned Order, const DebugLoc &DL, EVT VT, Align A)
2826       : SDNode(ISD::AssertAlign, Order, DL, getSDVTList(VT)), Alignment(A) {}
2827 
2828   Align getAlign() const { return Alignment; }
2829 
2830   static bool classof(const SDNode *N) {
2831     return N->getOpcode() == ISD::AssertAlign;
2832   }
2833 };
2834 
2835 class SDNodeIterator {
2836   const SDNode *Node;
2837   unsigned Operand;
2838 
2839   SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2840 
2841 public:
2842   using iterator_category = std::forward_iterator_tag;
2843   using value_type = SDNode;
2844   using difference_type = std::ptrdiff_t;
2845   using pointer = value_type *;
2846   using reference = value_type &;
2847 
2848   bool operator==(const SDNodeIterator& x) const {
2849     return Operand == x.Operand;
2850   }
2851   bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2852 
2853   pointer operator*() const {
2854     return Node->getOperand(Operand).getNode();
2855   }
2856   pointer operator->() const { return operator*(); }
2857 
2858   SDNodeIterator& operator++() {                // Preincrement
2859     ++Operand;
2860     return *this;
2861   }
2862   SDNodeIterator operator++(int) { // Postincrement
2863     SDNodeIterator tmp = *this; ++*this; return tmp;
2864   }
2865   size_t operator-(SDNodeIterator Other) const {
2866     assert(Node == Other.Node &&
2867            "Cannot compare iterators of two different nodes!");
2868     return Operand - Other.Operand;
2869   }
2870 
2871   static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2872   static SDNodeIterator end  (const SDNode *N) {
2873     return SDNodeIterator(N, N->getNumOperands());
2874   }
2875 
2876   unsigned getOperand() const { return Operand; }
2877   const SDNode *getNode() const { return Node; }
2878 };
2879 
2880 template <> struct GraphTraits<SDNode*> {
2881   using NodeRef = SDNode *;
2882   using ChildIteratorType = SDNodeIterator;
2883 
2884   static NodeRef getEntryNode(SDNode *N) { return N; }
2885 
2886   static ChildIteratorType child_begin(NodeRef N) {
2887     return SDNodeIterator::begin(N);
2888   }
2889 
2890   static ChildIteratorType child_end(NodeRef N) {
2891     return SDNodeIterator::end(N);
2892   }
2893 };
2894 
2895 /// A representation of the largest SDNode, for use in sizeof().
2896 ///
2897 /// This needs to be a union because the largest node differs on 32 bit systems
2898 /// with 4 and 8 byte pointer alignment, respectively.
2899 using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
2900                                             BlockAddressSDNode,
2901                                             GlobalAddressSDNode,
2902                                             PseudoProbeSDNode>;
2903 
2904 /// The SDNode class with the greatest alignment requirement.
2905 using MostAlignedSDNode = GlobalAddressSDNode;
2906 
2907 namespace ISD {
2908 
2909   /// Returns true if the specified node is a non-extending and unindexed load.
2910   inline bool isNormalLoad(const SDNode *N) {
2911     const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2912     return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2913       Ld->getAddressingMode() == ISD::UNINDEXED;
2914   }
2915 
2916   /// Returns true if the specified node is a non-extending load.
2917   inline bool isNON_EXTLoad(const SDNode *N) {
2918     return isa<LoadSDNode>(N) &&
2919       cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2920   }
2921 
2922   /// Returns true if the specified node is a EXTLOAD.
2923   inline bool isEXTLoad(const SDNode *N) {
2924     return isa<LoadSDNode>(N) &&
2925       cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2926   }
2927 
2928   /// Returns true if the specified node is a SEXTLOAD.
2929   inline bool isSEXTLoad(const SDNode *N) {
2930     return isa<LoadSDNode>(N) &&
2931       cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2932   }
2933 
2934   /// Returns true if the specified node is a ZEXTLOAD.
2935   inline bool isZEXTLoad(const SDNode *N) {
2936     return isa<LoadSDNode>(N) &&
2937       cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2938   }
2939 
2940   /// Returns true if the specified node is an unindexed load.
2941   inline bool isUNINDEXEDLoad(const SDNode *N) {
2942     return isa<LoadSDNode>(N) &&
2943       cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2944   }
2945 
2946   /// Returns true if the specified node is a non-truncating
2947   /// and unindexed store.
2948   inline bool isNormalStore(const SDNode *N) {
2949     const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2950     return St && !St->isTruncatingStore() &&
2951       St->getAddressingMode() == ISD::UNINDEXED;
2952   }
2953 
2954   /// Returns true if the specified node is an unindexed store.
2955   inline bool isUNINDEXEDStore(const SDNode *N) {
2956     return isa<StoreSDNode>(N) &&
2957       cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2958   }
2959 
2960   /// Attempt to match a unary predicate against a scalar/splat constant or
2961   /// every element of a constant BUILD_VECTOR.
2962   /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
2963   bool matchUnaryPredicate(SDValue Op,
2964                            std::function<bool(ConstantSDNode *)> Match,
2965                            bool AllowUndefs = false);
2966 
2967   /// Attempt to match a binary predicate against a pair of scalar/splat
2968   /// constants or every element of a pair of constant BUILD_VECTORs.
2969   /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
2970   /// If AllowTypeMismatch is true then RetType + ArgTypes don't need to match.
2971   bool matchBinaryPredicate(
2972       SDValue LHS, SDValue RHS,
2973       std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
2974       bool AllowUndefs = false, bool AllowTypeMismatch = false);
2975 
2976   /// Returns true if the specified value is the overflow result from one
2977   /// of the overflow intrinsic nodes.
2978   inline bool isOverflowIntrOpRes(SDValue Op) {
2979     unsigned Opc = Op.getOpcode();
2980     return (Op.getResNo() == 1 &&
2981             (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
2982              Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO));
2983   }
2984 
2985 } // end namespace ISD
2986 
2987 } // end namespace llvm
2988 
2989 #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H
2990