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