[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / CodeGen / SelectionDAGNodes.h
blob57fa8f269652aaa3f386b384cb94bfcc22010d69
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.
14 // Clients should include the SelectionDAG.h file instead of this file directly.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
19 #define LLVM_CODEGEN_SELECTIONDAGNODES_H
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>
55 namespace llvm {
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;
70 void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
71 bool force = false);
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;
82 namespace ISD {
84 /// Node predicates
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);
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);
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);
98 /// Return true if the specified node is a BUILD_VECTOR node of all
99 /// ConstantSDNode or undef.
100 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
102 /// Return true if the specified node is a BUILD_VECTOR node of all
103 /// ConstantFPSDNode or undef.
104 bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
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);
110 } // end namespace ISD
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).
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.
123 class SDValue {
124 friend struct DenseMapInfo<SDValue>;
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.
129 public:
130 SDValue() = default;
131 SDValue(SDNode *node, unsigned resno);
133 /// get the index which selects a specific result in the SDNode
134 unsigned getResNo() const { return ResNo; }
136 /// get the SDNode which holds the desired result
137 SDNode *getNode() const { return Node; }
139 /// set the SDNode
140 void setNode(SDNode *N) { Node = N; }
142 inline SDNode *operator->() const { return Node; }
144 bool operator==(const SDValue &O) const {
145 return Node == O.Node && ResNo == O.ResNo;
147 bool operator!=(const SDValue &O) const {
148 return !operator==(O);
150 bool operator<(const SDValue &O) const {
151 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
153 explicit operator bool() const {
154 return Node != nullptr;
157 SDValue getValue(unsigned R) const {
158 return SDValue(Node, R);
161 /// Return true if this node is an operand of N.
162 bool isOperandOf(const SDNode *N) const;
164 /// Return the ValueType of the referenced return value.
165 inline EVT getValueType() const;
167 /// Return the simple ValueType of the referenced return value.
168 MVT getSimpleValueType() const {
169 return getValueType().getSimpleVT();
172 /// Returns the size of the value in bits.
173 unsigned getValueSizeInBits() const {
174 return getValueType().getSizeInBits();
177 unsigned getScalarValueSizeInBits() const {
178 return getValueType().getScalarType().getSizeInBits();
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;
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;
206 /// Return true if there are no nodes using value ResNo of Node.
207 inline bool use_empty() const;
209 /// Return true if there is exactly one node using value ResNo of Node.
210 inline bool hasOneUse() const;
213 template<> struct DenseMapInfo<SDValue> {
214 static inline SDValue getEmptyKey() {
215 SDValue V;
216 V.ResNo = -1U;
217 return V;
220 static inline SDValue getTombstoneKey() {
221 SDValue V;
222 V.ResNo = -2U;
223 return V;
226 static unsigned getHashValue(const SDValue &Val) {
227 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
228 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
231 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
232 return LHS == RHS;
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 *;
241 static SimpleType getSimplifiedValue(SDValue &Val) {
242 return Val.getNode();
245 template<> struct simplify_type<const SDValue> {
246 using SimpleType = /*const*/ SDNode *;
248 static SimpleType getSimplifiedValue(const SDValue &Val) {
249 return Val.getNode();
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.
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;
268 public:
269 SDUse() = default;
270 SDUse(const SDUse &U) = delete;
271 SDUse &operator=(const SDUse &) = delete;
273 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
274 operator const SDValue&() const { return Val; }
276 /// If implicit conversion to SDValue doesn't work, the get() method returns
277 /// the SDValue.
278 const SDValue &get() const { return Val; }
280 /// This returns the SDNode that contains this Use.
281 SDNode *getUser() { return User; }
283 /// Get the next SDUse in the use list.
284 SDUse *getNext() const { return Next; }
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(); }
293 /// Convenience function for get().operator==
294 bool operator==(const SDValue &V) const {
295 return Val == V;
298 /// Convenience function for get().operator!=
299 bool operator!=(const SDValue &V) const {
300 return Val != V;
303 /// Convenience function for get().operator<
304 bool operator<(const SDValue &V) const {
305 return Val < V;
308 private:
309 friend class SelectionDAG;
310 friend class SDNode;
311 // TODO: unfriend HandleSDNode once we fix its operand handling.
312 friend class HandleSDNode;
314 void setUser(SDNode *p) { User = p; }
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);
326 void addToList(SDUse **List) {
327 Next = *List;
328 if (Next) Next->Prev = &Next;
329 Prev = List;
330 *List = this;
333 void removeFromList() {
334 *Prev = Next;
335 if (Next) Next->Prev = Prev;
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 *;
344 static SimpleType getSimplifiedValue(SDUse &Val) {
345 return Val.getNode();
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;
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;
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;
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) {}
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());
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; }
403 // These are mutators for each flag.
404 void setNoUnsignedWrap(bool b) {
405 setDefined();
406 NoUnsignedWrap = b;
408 void setNoSignedWrap(bool b) {
409 setDefined();
410 NoSignedWrap = b;
412 void setExact(bool b) {
413 setDefined();
414 Exact = b;
416 void setNoNaNs(bool b) {
417 setDefined();
418 NoNaNs = b;
420 void setNoInfs(bool b) {
421 setDefined();
422 NoInfs = b;
424 void setNoSignedZeros(bool b) {
425 setDefined();
426 NoSignedZeros = b;
428 void setAllowReciprocal(bool b) {
429 setDefined();
430 AllowReciprocal = b;
432 void setVectorReduction(bool b) {
433 setDefined();
434 VectorReduction = b;
436 void setAllowContract(bool b) {
437 setDefined();
438 AllowContract = b;
440 void setApproximateFuncs(bool b) {
441 setDefined();
442 ApproximateFuncs = b;
444 void setAllowReassociation(bool b) {
445 setDefined();
446 AllowReassociation = b;
448 void setFPExcept(bool b) {
449 setDefined();
450 NoFPExcept = !b;
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; }
467 bool isFast() const {
468 return NoSignedZeros && AllowReciprocal && NoNaNs && NoInfs && NoFPExcept &&
469 AllowContract && ApproximateFuncs && AllowReassociation;
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;
492 /// Represents one node in the SelectionDAG.
494 class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
495 private:
496 /// The operation that this node performs.
497 int16_t NodeType;
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.
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
514 BEGIN_TWO_BYTE_PACK()
515 class SDNodeBitfields {
516 friend class SDNode;
517 friend class MemIntrinsicSDNode;
518 friend class MemSDNode;
519 friend class SelectionDAG;
521 uint16_t HasDebugValue : 1;
522 uint16_t IsMemIntrinsic : 1;
523 uint16_t IsDivergent : 1;
525 enum { NumSDNodeBits = 3 };
527 class ConstantSDNodeBitfields {
528 friend class ConstantSDNode;
530 uint16_t : NumSDNodeBits;
532 uint16_t IsOpaque : 1;
535 class MemSDNodeBitfields {
536 friend class MemSDNode;
537 friend class MemIntrinsicSDNode;
538 friend class AtomicSDNode;
540 uint16_t : NumSDNodeBits;
542 uint16_t IsVolatile : 1;
543 uint16_t IsNonTemporal : 1;
544 uint16_t IsDereferenceable : 1;
545 uint16_t IsInvariant : 1;
547 enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
549 class LSBaseSDNodeBitfields {
550 friend class LSBaseSDNode;
551 friend class MaskedGatherScatterSDNode;
553 uint16_t : NumMemSDNodeBits;
555 // This storage is shared between disparate class hierarchies to hold an
556 // enumeration specific to the class hierarchy in use.
557 // LSBaseSDNode => enum ISD::MemIndexedMode
558 // MaskedGatherScatterSDNode => enum ISD::MemIndexType
559 uint16_t AddressingMode : 3;
561 enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
563 class LoadSDNodeBitfields {
564 friend class LoadSDNode;
565 friend class MaskedLoadSDNode;
567 uint16_t : NumLSBaseSDNodeBits;
569 uint16_t ExtTy : 2; // enum ISD::LoadExtType
570 uint16_t IsExpanding : 1;
573 class StoreSDNodeBitfields {
574 friend class StoreSDNode;
575 friend class MaskedStoreSDNode;
577 uint16_t : NumLSBaseSDNodeBits;
579 uint16_t IsTruncating : 1;
580 uint16_t IsCompressing : 1;
583 union {
584 char RawSDNodeBits[sizeof(uint16_t)];
585 SDNodeBitfields SDNodeBits;
586 ConstantSDNodeBitfields ConstantSDNodeBits;
587 MemSDNodeBitfields MemSDNodeBits;
588 LSBaseSDNodeBitfields LSBaseSDNodeBits;
589 LoadSDNodeBitfields LoadSDNodeBits;
590 StoreSDNodeBitfields StoreSDNodeBits;
592 END_TWO_BYTE_PACK()
593 #undef BEGIN_TWO_BYTE_PACK
594 #undef END_TWO_BYTE_PACK
596 // RawSDNodeBits must cover the entirety of the union. This means that all of
597 // the union's members must have size <= RawSDNodeBits. We write the RHS as
598 // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
599 static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
600 static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
601 static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
602 static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
603 static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide");
604 static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
606 private:
607 friend class SelectionDAG;
608 // TODO: unfriend HandleSDNode once we fix its operand handling.
609 friend class HandleSDNode;
611 /// Unique id per SDNode in the DAG.
612 int NodeId = -1;
614 /// The values that are used by this operation.
615 SDUse *OperandList = nullptr;
617 /// The types of the values this node defines. SDNode's may
618 /// define multiple values simultaneously.
619 const EVT *ValueList;
621 /// List of uses for this SDNode.
622 SDUse *UseList = nullptr;
624 /// The number of entries in the Operand/Value list.
625 unsigned short NumOperands = 0;
626 unsigned short NumValues;
628 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
629 // original LLVM instructions.
630 // This is used for turning off scheduling, because we'll forgo
631 // the normal scheduling algorithms and output the instructions according to
632 // this ordering.
633 unsigned IROrder;
635 /// Source line information.
636 DebugLoc debugLoc;
638 /// Return a pointer to the specified value type.
639 static const EVT *getValueTypeList(EVT VT);
641 SDNodeFlags Flags;
643 public:
644 /// Unique and persistent id per SDNode in the DAG.
645 /// Used for debug printing.
646 uint16_t PersistentId;
648 //===--------------------------------------------------------------------===//
649 // Accessors
652 /// Return the SelectionDAG opcode value for this node. For
653 /// pre-isel nodes (those for which isMachineOpcode returns false), these
654 /// are the opcode values in the ISD and <target>ISD namespaces. For
655 /// post-isel opcodes, see getMachineOpcode.
656 unsigned getOpcode() const { return (unsigned short)NodeType; }
658 /// Test if this node has a target-specific opcode (in the
659 /// \<target\>ISD namespace).
660 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
662 /// Test if this node has a target-specific
663 /// memory-referencing opcode (in the \<target\>ISD namespace and
664 /// greater than FIRST_TARGET_MEMORY_OPCODE).
665 bool isTargetMemoryOpcode() const {
666 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
669 /// Return true if the type of the node type undefined.
670 bool isUndef() const { return NodeType == ISD::UNDEF; }
672 /// Test if this node is a memory intrinsic (with valid pointer information).
673 /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
674 /// non-memory intrinsics (with chains) that are not really instances of
675 /// MemSDNode. For such nodes, we need some extra state to determine the
676 /// proper classof relationship.
677 bool isMemIntrinsic() const {
678 return (NodeType == ISD::INTRINSIC_W_CHAIN ||
679 NodeType == ISD::INTRINSIC_VOID) &&
680 SDNodeBits.IsMemIntrinsic;
683 /// Test if this node is a strict floating point pseudo-op.
684 bool isStrictFPOpcode() {
685 switch (NodeType) {
686 default:
687 return false;
688 case ISD::STRICT_FADD:
689 case ISD::STRICT_FSUB:
690 case ISD::STRICT_FMUL:
691 case ISD::STRICT_FDIV:
692 case ISD::STRICT_FREM:
693 case ISD::STRICT_FMA:
694 case ISD::STRICT_FSQRT:
695 case ISD::STRICT_FPOW:
696 case ISD::STRICT_FPOWI:
697 case ISD::STRICT_FSIN:
698 case ISD::STRICT_FCOS:
699 case ISD::STRICT_FEXP:
700 case ISD::STRICT_FEXP2:
701 case ISD::STRICT_FLOG:
702 case ISD::STRICT_FLOG10:
703 case ISD::STRICT_FLOG2:
704 case ISD::STRICT_FRINT:
705 case ISD::STRICT_FNEARBYINT:
706 case ISD::STRICT_FMAXNUM:
707 case ISD::STRICT_FMINNUM:
708 case ISD::STRICT_FCEIL:
709 case ISD::STRICT_FFLOOR:
710 case ISD::STRICT_FROUND:
711 case ISD::STRICT_FTRUNC:
712 case ISD::STRICT_FP_TO_SINT:
713 case ISD::STRICT_FP_TO_UINT:
714 case ISD::STRICT_FP_ROUND:
715 case ISD::STRICT_FP_EXTEND:
716 return true;
720 /// Test if this node has a post-isel opcode, directly
721 /// corresponding to a MachineInstr opcode.
722 bool isMachineOpcode() const { return NodeType < 0; }
724 /// This may only be called if isMachineOpcode returns
725 /// true. It returns the MachineInstr opcode value that the node's opcode
726 /// corresponds to.
727 unsigned getMachineOpcode() const {
728 assert(isMachineOpcode() && "Not a MachineInstr opcode!");
729 return ~NodeType;
732 bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
733 void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
735 bool isDivergent() const { return SDNodeBits.IsDivergent; }
737 /// Return true if there are no uses of this node.
738 bool use_empty() const { return UseList == nullptr; }
740 /// Return true if there is exactly one use of this node.
741 bool hasOneUse() const {
742 return !use_empty() && std::next(use_begin()) == use_end();
745 /// Return the number of uses of this node. This method takes
746 /// time proportional to the number of uses.
747 size_t use_size() const { return std::distance(use_begin(), use_end()); }
749 /// Return the unique node id.
750 int getNodeId() const { return NodeId; }
752 /// Set unique node id.
753 void setNodeId(int Id) { NodeId = Id; }
755 /// Return the node ordering.
756 unsigned getIROrder() const { return IROrder; }
758 /// Set the node ordering.
759 void setIROrder(unsigned Order) { IROrder = Order; }
761 /// Return the source location info.
762 const DebugLoc &getDebugLoc() const { return debugLoc; }
764 /// Set source location info. Try to avoid this, putting
765 /// it in the constructor is preferable.
766 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
768 /// This class provides iterator support for SDUse
769 /// operands that use a specific SDNode.
770 class use_iterator
771 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
772 friend class SDNode;
774 SDUse *Op = nullptr;
776 explicit use_iterator(SDUse *op) : Op(op) {}
778 public:
779 using reference = std::iterator<std::forward_iterator_tag,
780 SDUse, ptrdiff_t>::reference;
781 using pointer = std::iterator<std::forward_iterator_tag,
782 SDUse, ptrdiff_t>::pointer;
784 use_iterator() = default;
785 use_iterator(const use_iterator &I) : Op(I.Op) {}
787 bool operator==(const use_iterator &x) const {
788 return Op == x.Op;
790 bool operator!=(const use_iterator &x) const {
791 return !operator==(x);
794 /// Return true if this iterator is at the end of uses list.
795 bool atEnd() const { return Op == nullptr; }
797 // Iterator traversal: forward iteration only.
798 use_iterator &operator++() { // Preincrement
799 assert(Op && "Cannot increment end iterator!");
800 Op = Op->getNext();
801 return *this;
804 use_iterator operator++(int) { // Postincrement
805 use_iterator tmp = *this; ++*this; return tmp;
808 /// Retrieve a pointer to the current user node.
809 SDNode *operator*() const {
810 assert(Op && "Cannot dereference end iterator!");
811 return Op->getUser();
814 SDNode *operator->() const { return operator*(); }
816 SDUse &getUse() const { return *Op; }
818 /// Retrieve the operand # of this use in its user.
819 unsigned getOperandNo() const {
820 assert(Op && "Cannot dereference end iterator!");
821 return (unsigned)(Op - Op->getUser()->OperandList);
825 /// Provide iteration support to walk over all uses of an SDNode.
826 use_iterator use_begin() const {
827 return use_iterator(UseList);
830 static use_iterator use_end() { return use_iterator(nullptr); }
832 inline iterator_range<use_iterator> uses() {
833 return make_range(use_begin(), use_end());
835 inline iterator_range<use_iterator> uses() const {
836 return make_range(use_begin(), use_end());
839 /// Return true if there are exactly NUSES uses of the indicated value.
840 /// This method ignores uses of other values defined by this operation.
841 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
843 /// Return true if there are any use of the indicated value.
844 /// This method ignores uses of other values defined by this operation.
845 bool hasAnyUseOfValue(unsigned Value) const;
847 /// Return true if this node is the only use of N.
848 bool isOnlyUserOf(const SDNode *N) const;
850 /// Return true if this node is an operand of N.
851 bool isOperandOf(const SDNode *N) const;
853 /// Return true if this node is a predecessor of N.
854 /// NOTE: Implemented on top of hasPredecessor and every bit as
855 /// expensive. Use carefully.
856 bool isPredecessorOf(const SDNode *N) const {
857 return N->hasPredecessor(this);
860 /// Return true if N is a predecessor of this node.
861 /// N is either an operand of this node, or can be reached by recursively
862 /// traversing up the operands.
863 /// NOTE: This is an expensive method. Use it carefully.
864 bool hasPredecessor(const SDNode *N) const;
866 /// Returns true if N is a predecessor of any node in Worklist. This
867 /// helper keeps Visited and Worklist sets externally to allow unions
868 /// searches to be performed in parallel, caching of results across
869 /// queries and incremental addition to Worklist. Stops early if N is
870 /// found but will resume. Remember to clear Visited and Worklists
871 /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before
872 /// giving up. The TopologicalPrune flag signals that positive NodeIds are
873 /// topologically ordered (Operands have strictly smaller node id) and search
874 /// can be pruned leveraging this.
875 static bool hasPredecessorHelper(const SDNode *N,
876 SmallPtrSetImpl<const SDNode *> &Visited,
877 SmallVectorImpl<const SDNode *> &Worklist,
878 unsigned int MaxSteps = 0,
879 bool TopologicalPrune = false) {
880 SmallVector<const SDNode *, 8> DeferredNodes;
881 if (Visited.count(N))
882 return true;
884 // Node Id's are assigned in three places: As a topological
885 // ordering (> 0), during legalization (results in values set to
886 // 0), new nodes (set to -1). If N has a topolgical id then we
887 // know that all nodes with ids smaller than it cannot be
888 // successors and we need not check them. Filter out all node
889 // that can't be matches. We add them to the worklist before exit
890 // in case of multiple calls. Note that during selection the topological id
891 // may be violated if a node's predecessor is selected before it. We mark
892 // this at selection negating the id of unselected successors and
893 // restricting topological pruning to positive ids.
895 int NId = N->getNodeId();
896 // If we Invalidated the Id, reconstruct original NId.
897 if (NId < -1)
898 NId = -(NId + 1);
900 bool Found = false;
901 while (!Worklist.empty()) {
902 const SDNode *M = Worklist.pop_back_val();
903 int MId = M->getNodeId();
904 if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) &&
905 (MId > 0) && (MId < NId)) {
906 DeferredNodes.push_back(M);
907 continue;
909 for (const SDValue &OpV : M->op_values()) {
910 SDNode *Op = OpV.getNode();
911 if (Visited.insert(Op).second)
912 Worklist.push_back(Op);
913 if (Op == N)
914 Found = true;
916 if (Found)
917 break;
918 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
919 break;
921 // Push deferred nodes back on worklist.
922 Worklist.append(DeferredNodes.begin(), DeferredNodes.end());
923 // If we bailed early, conservatively return found.
924 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
925 return true;
926 return Found;
929 /// Return true if all the users of N are contained in Nodes.
930 /// NOTE: Requires at least one match, but doesn't require them all.
931 static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
933 /// Return the number of values used by this operation.
934 unsigned getNumOperands() const { return NumOperands; }
936 /// Return the maximum number of operands that a SDNode can hold.
937 static constexpr size_t getMaxNumOperands() {
938 return std::numeric_limits<decltype(SDNode::NumOperands)>::max();
941 /// Helper method returns the integer value of a ConstantSDNode operand.
942 inline uint64_t getConstantOperandVal(unsigned Num) const;
944 /// Helper method returns the APInt of a ConstantSDNode operand.
945 inline const APInt &getConstantOperandAPInt(unsigned Num) const;
947 const SDValue &getOperand(unsigned Num) const {
948 assert(Num < NumOperands && "Invalid child # of SDNode!");
949 return OperandList[Num];
952 using op_iterator = SDUse *;
954 op_iterator op_begin() const { return OperandList; }
955 op_iterator op_end() const { return OperandList+NumOperands; }
956 ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
958 /// Iterator for directly iterating over the operand SDValue's.
959 struct value_op_iterator
960 : iterator_adaptor_base<value_op_iterator, op_iterator,
961 std::random_access_iterator_tag, SDValue,
962 ptrdiff_t, value_op_iterator *,
963 value_op_iterator *> {
964 explicit value_op_iterator(SDUse *U = nullptr)
965 : iterator_adaptor_base(U) {}
967 const SDValue &operator*() const { return I->get(); }
970 iterator_range<value_op_iterator> op_values() const {
971 return make_range(value_op_iterator(op_begin()),
972 value_op_iterator(op_end()));
975 SDVTList getVTList() const {
976 SDVTList X = { ValueList, NumValues };
977 return X;
980 /// If this node has a glue operand, return the node
981 /// to which the glue operand points. Otherwise return NULL.
982 SDNode *getGluedNode() const {
983 if (getNumOperands() != 0 &&
984 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
985 return getOperand(getNumOperands()-1).getNode();
986 return nullptr;
989 /// If this node has a glue value with a user, return
990 /// the user (there is at most one). Otherwise return NULL.
991 SDNode *getGluedUser() const {
992 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
993 if (UI.getUse().get().getValueType() == MVT::Glue)
994 return *UI;
995 return nullptr;
998 const SDNodeFlags getFlags() const { return Flags; }
999 void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
1000 bool isFast() { return Flags.isFast(); }
1002 /// Clear any flags in this node that aren't also set in Flags.
1003 /// If Flags is not in a defined state then this has no effect.
1004 void intersectFlagsWith(const SDNodeFlags Flags);
1006 /// Return the number of values defined/returned by this operator.
1007 unsigned getNumValues() const { return NumValues; }
1009 /// Return the type of a specified result.
1010 EVT getValueType(unsigned ResNo) const {
1011 assert(ResNo < NumValues && "Illegal result number!");
1012 return ValueList[ResNo];
1015 /// Return the type of a specified result as a simple type.
1016 MVT getSimpleValueType(unsigned ResNo) const {
1017 return getValueType(ResNo).getSimpleVT();
1020 /// Returns MVT::getSizeInBits(getValueType(ResNo)).
1021 unsigned getValueSizeInBits(unsigned ResNo) const {
1022 return getValueType(ResNo).getSizeInBits();
1025 using value_iterator = const EVT *;
1027 value_iterator value_begin() const { return ValueList; }
1028 value_iterator value_end() const { return ValueList+NumValues; }
1030 /// Return the opcode of this operation for printing.
1031 std::string getOperationName(const SelectionDAG *G = nullptr) const;
1032 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
1033 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
1034 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
1035 void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1036 void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1038 /// Print a SelectionDAG node and all children down to
1039 /// the leaves. The given SelectionDAG allows target-specific nodes
1040 /// to be printed in human-readable form. Unlike printr, this will
1041 /// print the whole DAG, including children that appear multiple
1042 /// times.
1044 void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
1046 /// Print a SelectionDAG node and children up to
1047 /// depth "depth." The given SelectionDAG allows target-specific
1048 /// nodes to be printed in human-readable form. Unlike printr, this
1049 /// will print children that appear multiple times wherever they are
1050 /// used.
1052 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
1053 unsigned depth = 100) const;
1055 /// Dump this node, for debugging.
1056 void dump() const;
1058 /// Dump (recursively) this node and its use-def subgraph.
1059 void dumpr() const;
1061 /// Dump this node, for debugging.
1062 /// The given SelectionDAG allows target-specific nodes to be printed
1063 /// in human-readable form.
1064 void dump(const SelectionDAG *G) const;
1066 /// Dump (recursively) this node and its use-def subgraph.
1067 /// The given SelectionDAG allows target-specific nodes to be printed
1068 /// in human-readable form.
1069 void dumpr(const SelectionDAG *G) const;
1071 /// printrFull to dbgs(). The given SelectionDAG allows
1072 /// target-specific nodes to be printed in human-readable form.
1073 /// Unlike dumpr, this will print the whole DAG, including children
1074 /// that appear multiple times.
1075 void dumprFull(const SelectionDAG *G = nullptr) const;
1077 /// printrWithDepth to dbgs(). The given
1078 /// SelectionDAG allows target-specific nodes to be printed in
1079 /// human-readable form. Unlike dumpr, this will print children
1080 /// that appear multiple times wherever they are used.
1082 void dumprWithDepth(const SelectionDAG *G = nullptr,
1083 unsigned depth = 100) const;
1085 /// Gather unique data for the node.
1086 void Profile(FoldingSetNodeID &ID) const;
1088 /// This method should only be used by the SDUse class.
1089 void addUse(SDUse &U) { U.addToList(&UseList); }
1091 protected:
1092 static SDVTList getSDVTList(EVT VT) {
1093 SDVTList Ret = { getValueTypeList(VT), 1 };
1094 return Ret;
1097 /// Create an SDNode.
1099 /// SDNodes are created without any operands, and never own the operand
1100 /// storage. To add operands, see SelectionDAG::createOperands.
1101 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
1102 : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
1103 IROrder(Order), debugLoc(std::move(dl)) {
1104 memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
1105 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
1106 assert(NumValues == VTs.NumVTs &&
1107 "NumValues wasn't wide enough for its operands!");
1110 /// Release the operands and set this node to have zero operands.
1111 void DropOperands();
1114 /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
1115 /// into SDNode creation functions.
1116 /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
1117 /// from the original Instruction, and IROrder is the ordinal position of
1118 /// the instruction.
1119 /// When an SDNode is created after the DAG is being built, both DebugLoc and
1120 /// the IROrder are propagated from the original SDNode.
1121 /// So SDLoc class provides two constructors besides the default one, one to
1122 /// be used by the DAGBuilder, the other to be used by others.
1123 class SDLoc {
1124 private:
1125 DebugLoc DL;
1126 int IROrder = 0;
1128 public:
1129 SDLoc() = default;
1130 SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1131 SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1132 SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1133 assert(Order >= 0 && "bad IROrder");
1134 if (I)
1135 DL = I->getDebugLoc();
1138 unsigned getIROrder() const { return IROrder; }
1139 const DebugLoc &getDebugLoc() const { return DL; }
1142 // Define inline functions from the SDValue class.
1144 inline SDValue::SDValue(SDNode *node, unsigned resno)
1145 : Node(node), ResNo(resno) {
1146 // Explicitly check for !ResNo to avoid use-after-free, because there are
1147 // callers that use SDValue(N, 0) with a deleted N to indicate successful
1148 // combines.
1149 assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&
1150 "Invalid result number for the given node!");
1151 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
1154 inline unsigned SDValue::getOpcode() const {
1155 return Node->getOpcode();
1158 inline EVT SDValue::getValueType() const {
1159 return Node->getValueType(ResNo);
1162 inline unsigned SDValue::getNumOperands() const {
1163 return Node->getNumOperands();
1166 inline const SDValue &SDValue::getOperand(unsigned i) const {
1167 return Node->getOperand(i);
1170 inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1171 return Node->getConstantOperandVal(i);
1174 inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const {
1175 return Node->getConstantOperandAPInt(i);
1178 inline bool SDValue::isTargetOpcode() const {
1179 return Node->isTargetOpcode();
1182 inline bool SDValue::isTargetMemoryOpcode() const {
1183 return Node->isTargetMemoryOpcode();
1186 inline bool SDValue::isMachineOpcode() const {
1187 return Node->isMachineOpcode();
1190 inline unsigned SDValue::getMachineOpcode() const {
1191 return Node->getMachineOpcode();
1194 inline bool SDValue::isUndef() const {
1195 return Node->isUndef();
1198 inline bool SDValue::use_empty() const {
1199 return !Node->hasAnyUseOfValue(ResNo);
1202 inline bool SDValue::hasOneUse() const {
1203 return Node->hasNUsesOfValue(1, ResNo);
1206 inline const DebugLoc &SDValue::getDebugLoc() const {
1207 return Node->getDebugLoc();
1210 inline void SDValue::dump() const {
1211 return Node->dump();
1214 inline void SDValue::dump(const SelectionDAG *G) const {
1215 return Node->dump(G);
1218 inline void SDValue::dumpr() const {
1219 return Node->dumpr();
1222 inline void SDValue::dumpr(const SelectionDAG *G) const {
1223 return Node->dumpr(G);
1226 // Define inline functions from the SDUse class.
1228 inline void SDUse::set(const SDValue &V) {
1229 if (Val.getNode()) removeFromList();
1230 Val = V;
1231 if (V.getNode()) V.getNode()->addUse(*this);
1234 inline void SDUse::setInitial(const SDValue &V) {
1235 Val = V;
1236 V.getNode()->addUse(*this);
1239 inline void SDUse::setNode(SDNode *N) {
1240 if (Val.getNode()) removeFromList();
1241 Val.setNode(N);
1242 if (N) N->addUse(*this);
1245 /// This class is used to form a handle around another node that
1246 /// is persistent and is updated across invocations of replaceAllUsesWith on its
1247 /// operand. This node should be directly created by end-users and not added to
1248 /// the AllNodes list.
1249 class HandleSDNode : public SDNode {
1250 SDUse Op;
1252 public:
1253 explicit HandleSDNode(SDValue X)
1254 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1255 // HandleSDNodes are never inserted into the DAG, so they won't be
1256 // auto-numbered. Use ID 65535 as a sentinel.
1257 PersistentId = 0xffff;
1259 // Manually set up the operand list. This node type is special in that it's
1260 // always stack allocated and SelectionDAG does not manage its operands.
1261 // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1262 // be so special.
1263 Op.setUser(this);
1264 Op.setInitial(X);
1265 NumOperands = 1;
1266 OperandList = &Op;
1268 ~HandleSDNode();
1270 const SDValue &getValue() const { return Op; }
1273 class AddrSpaceCastSDNode : public SDNode {
1274 private:
1275 unsigned SrcAddrSpace;
1276 unsigned DestAddrSpace;
1278 public:
1279 AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1280 unsigned SrcAS, unsigned DestAS);
1282 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1283 unsigned getDestAddressSpace() const { return DestAddrSpace; }
1285 static bool classof(const SDNode *N) {
1286 return N->getOpcode() == ISD::ADDRSPACECAST;
1290 /// This is an abstract virtual class for memory operations.
1291 class MemSDNode : public SDNode {
1292 private:
1293 // VT of in-memory value.
1294 EVT MemoryVT;
1296 protected:
1297 /// Memory reference information.
1298 MachineMemOperand *MMO;
1300 public:
1301 MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1302 EVT memvt, MachineMemOperand *MMO);
1304 bool readMem() const { return MMO->isLoad(); }
1305 bool writeMem() const { return MMO->isStore(); }
1307 /// Returns alignment and volatility of the memory access
1308 unsigned getOriginalAlignment() const {
1309 return MMO->getBaseAlignment();
1311 unsigned getAlignment() const {
1312 return MMO->getAlignment();
1315 /// Return the SubclassData value, without HasDebugValue. This contains an
1316 /// encoding of the volatile flag, as well as bits used by subclasses. This
1317 /// function should only be used to compute a FoldingSetNodeID value.
1318 /// The HasDebugValue bit is masked out because CSE map needs to match
1319 /// nodes with debug info with nodes without debug info. Same is about
1320 /// isDivergent bit.
1321 unsigned getRawSubclassData() const {
1322 uint16_t Data;
1323 union {
1324 char RawSDNodeBits[sizeof(uint16_t)];
1325 SDNodeBitfields SDNodeBits;
1327 memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1328 SDNodeBits.HasDebugValue = 0;
1329 SDNodeBits.IsDivergent = false;
1330 memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1331 return Data;
1334 bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1335 bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1336 bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1337 bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1339 // Returns the offset from the location of the access.
1340 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1342 /// Returns the AA info that describes the dereference.
1343 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1345 /// Returns the Ranges that describes the dereference.
1346 const MDNode *getRanges() const { return MMO->getRanges(); }
1348 /// Returns the synchronization scope ID for this memory operation.
1349 SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); }
1351 /// Return the atomic ordering requirements for this memory operation. For
1352 /// cmpxchg atomic operations, return the atomic ordering requirements when
1353 /// store occurs.
1354 AtomicOrdering getOrdering() const { return MMO->getOrdering(); }
1356 /// Return true if the memory operation ordering is Unordered or higher.
1357 bool isAtomic() const { return MMO->isAtomic(); }
1359 /// Returns true if the memory operation doesn't imply any ordering
1360 /// constraints on surrounding memory operations beyond the normal memory
1361 /// aliasing rules.
1362 bool isUnordered() const { return MMO->isUnordered(); }
1364 /// Return the type of the in-memory value.
1365 EVT getMemoryVT() const { return MemoryVT; }
1367 /// Return a MachineMemOperand object describing the memory
1368 /// reference performed by operation.
1369 MachineMemOperand *getMemOperand() const { return MMO; }
1371 const MachinePointerInfo &getPointerInfo() const {
1372 return MMO->getPointerInfo();
1375 /// Return the address space for the associated pointer
1376 unsigned getAddressSpace() const {
1377 return getPointerInfo().getAddrSpace();
1380 /// Update this MemSDNode's MachineMemOperand information
1381 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1382 /// This must only be used when the new alignment applies to all users of
1383 /// this MachineMemOperand.
1384 void refineAlignment(const MachineMemOperand *NewMMO) {
1385 MMO->refineAlignment(NewMMO);
1388 const SDValue &getChain() const { return getOperand(0); }
1389 const SDValue &getBasePtr() const {
1390 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1393 // Methods to support isa and dyn_cast
1394 static bool classof(const SDNode *N) {
1395 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1396 // with either an intrinsic or a target opcode.
1397 return N->getOpcode() == ISD::LOAD ||
1398 N->getOpcode() == ISD::STORE ||
1399 N->getOpcode() == ISD::PREFETCH ||
1400 N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1401 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1402 N->getOpcode() == ISD::ATOMIC_SWAP ||
1403 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1404 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1405 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1406 N->getOpcode() == ISD::ATOMIC_LOAD_CLR ||
1407 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1408 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1409 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1410 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1411 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1412 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1413 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1414 N->getOpcode() == ISD::ATOMIC_LOAD_FADD ||
1415 N->getOpcode() == ISD::ATOMIC_LOAD_FSUB ||
1416 N->getOpcode() == ISD::ATOMIC_LOAD ||
1417 N->getOpcode() == ISD::ATOMIC_STORE ||
1418 N->getOpcode() == ISD::MLOAD ||
1419 N->getOpcode() == ISD::MSTORE ||
1420 N->getOpcode() == ISD::MGATHER ||
1421 N->getOpcode() == ISD::MSCATTER ||
1422 N->isMemIntrinsic() ||
1423 N->isTargetMemoryOpcode();
1427 /// This is an SDNode representing atomic operations.
1428 class AtomicSDNode : public MemSDNode {
1429 public:
1430 AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
1431 EVT MemVT, MachineMemOperand *MMO)
1432 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1433 assert(((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) ||
1434 MMO->isAtomic()) && "then why are we using an AtomicSDNode?");
1437 const SDValue &getBasePtr() const { return getOperand(1); }
1438 const SDValue &getVal() const { return getOperand(2); }
1440 /// Returns true if this SDNode represents cmpxchg atomic operation, false
1441 /// otherwise.
1442 bool isCompareAndSwap() const {
1443 unsigned Op = getOpcode();
1444 return Op == ISD::ATOMIC_CMP_SWAP ||
1445 Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1448 /// For cmpxchg atomic operations, return the atomic ordering requirements
1449 /// when store does not occur.
1450 AtomicOrdering getFailureOrdering() const {
1451 assert(isCompareAndSwap() && "Must be cmpxchg operation");
1452 return MMO->getFailureOrdering();
1455 // Methods to support isa and dyn_cast
1456 static bool classof(const SDNode *N) {
1457 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1458 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1459 N->getOpcode() == ISD::ATOMIC_SWAP ||
1460 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1461 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1462 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1463 N->getOpcode() == ISD::ATOMIC_LOAD_CLR ||
1464 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1465 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1466 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1467 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1468 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1469 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1470 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1471 N->getOpcode() == ISD::ATOMIC_LOAD_FADD ||
1472 N->getOpcode() == ISD::ATOMIC_LOAD_FSUB ||
1473 N->getOpcode() == ISD::ATOMIC_LOAD ||
1474 N->getOpcode() == ISD::ATOMIC_STORE;
1478 /// This SDNode is used for target intrinsics that touch
1479 /// memory and need an associated MachineMemOperand. Its opcode may be
1480 /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1481 /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1482 class MemIntrinsicSDNode : public MemSDNode {
1483 public:
1484 MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
1485 SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
1486 : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
1487 SDNodeBits.IsMemIntrinsic = true;
1490 // Methods to support isa and dyn_cast
1491 static bool classof(const SDNode *N) {
1492 // We lower some target intrinsics to their target opcode
1493 // early a node with a target opcode can be of this class
1494 return N->isMemIntrinsic() ||
1495 N->getOpcode() == ISD::PREFETCH ||
1496 N->isTargetMemoryOpcode();
1500 /// This SDNode is used to implement the code generator
1501 /// support for the llvm IR shufflevector instruction. It combines elements
1502 /// from two input vectors into a new input vector, with the selection and
1503 /// ordering of elements determined by an array of integers, referred to as
1504 /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1505 /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1506 /// An index of -1 is treated as undef, such that the code generator may put
1507 /// any value in the corresponding element of the result.
1508 class ShuffleVectorSDNode : public SDNode {
1509 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1510 // is freed when the SelectionDAG object is destroyed.
1511 const int *Mask;
1513 protected:
1514 friend class SelectionDAG;
1516 ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
1517 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
1519 public:
1520 ArrayRef<int> getMask() const {
1521 EVT VT = getValueType(0);
1522 return makeArrayRef(Mask, VT.getVectorNumElements());
1525 int getMaskElt(unsigned Idx) const {
1526 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
1527 return Mask[Idx];
1530 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1532 int getSplatIndex() const {
1533 assert(isSplat() && "Cannot get splat index for non-splat!");
1534 EVT VT = getValueType(0);
1535 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1536 if (Mask[i] >= 0)
1537 return Mask[i];
1539 // We can choose any index value here and be correct because all elements
1540 // are undefined. Return 0 for better potential for callers to simplify.
1541 return 0;
1544 static bool isSplatMask(const int *Mask, EVT VT);
1546 /// Change values in a shuffle permute mask assuming
1547 /// the two vector operands have swapped position.
1548 static void commuteMask(MutableArrayRef<int> Mask) {
1549 unsigned NumElems = Mask.size();
1550 for (unsigned i = 0; i != NumElems; ++i) {
1551 int idx = Mask[i];
1552 if (idx < 0)
1553 continue;
1554 else if (idx < (int)NumElems)
1555 Mask[i] = idx + NumElems;
1556 else
1557 Mask[i] = idx - NumElems;
1561 static bool classof(const SDNode *N) {
1562 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1566 class ConstantSDNode : public SDNode {
1567 friend class SelectionDAG;
1569 const ConstantInt *Value;
1571 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
1572 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(),
1573 getSDVTList(VT)),
1574 Value(val) {
1575 ConstantSDNodeBits.IsOpaque = isOpaque;
1578 public:
1579 const ConstantInt *getConstantIntValue() const { return Value; }
1580 const APInt &getAPIntValue() const { return Value->getValue(); }
1581 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1582 int64_t getSExtValue() const { return Value->getSExtValue(); }
1583 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) {
1584 return Value->getLimitedValue(Limit);
1587 bool isOne() const { return Value->isOne(); }
1588 bool isNullValue() const { return Value->isZero(); }
1589 bool isAllOnesValue() const { return Value->isMinusOne(); }
1591 bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
1593 static bool classof(const SDNode *N) {
1594 return N->getOpcode() == ISD::Constant ||
1595 N->getOpcode() == ISD::TargetConstant;
1599 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
1600 return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
1603 const APInt &SDNode::getConstantOperandAPInt(unsigned Num) const {
1604 return cast<ConstantSDNode>(getOperand(Num))->getAPIntValue();
1607 class ConstantFPSDNode : public SDNode {
1608 friend class SelectionDAG;
1610 const ConstantFP *Value;
1612 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1613 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0,
1614 DebugLoc(), getSDVTList(VT)),
1615 Value(val) {}
1617 public:
1618 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1619 const ConstantFP *getConstantFPValue() const { return Value; }
1621 /// Return true if the value is positive or negative zero.
1622 bool isZero() const { return Value->isZero(); }
1624 /// Return true if the value is a NaN.
1625 bool isNaN() const { return Value->isNaN(); }
1627 /// Return true if the value is an infinity
1628 bool isInfinity() const { return Value->isInfinity(); }
1630 /// Return true if the value is negative.
1631 bool isNegative() const { return Value->isNegative(); }
1633 /// We don't rely on operator== working on double values, as
1634 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1635 /// As such, this method can be used to do an exact bit-for-bit comparison of
1636 /// two floating point values.
1638 /// We leave the version with the double argument here because it's just so
1639 /// convenient to write "2.0" and the like. Without this function we'd
1640 /// have to duplicate its logic everywhere it's called.
1641 bool isExactlyValue(double V) const {
1642 return Value->getValueAPF().isExactlyValue(V);
1644 bool isExactlyValue(const APFloat& V) const;
1646 static bool isValueValidForType(EVT VT, const APFloat& Val);
1648 static bool classof(const SDNode *N) {
1649 return N->getOpcode() == ISD::ConstantFP ||
1650 N->getOpcode() == ISD::TargetConstantFP;
1654 /// Returns true if \p V is a constant integer zero.
1655 bool isNullConstant(SDValue V);
1657 /// Returns true if \p V is an FP constant with a value of positive zero.
1658 bool isNullFPConstant(SDValue V);
1660 /// Returns true if \p V is an integer constant with all bits set.
1661 bool isAllOnesConstant(SDValue V);
1663 /// Returns true if \p V is a constant integer one.
1664 bool isOneConstant(SDValue V);
1666 /// Return the non-bitcasted source operand of \p V if it exists.
1667 /// If \p V is not a bitcasted value, it is returned as-is.
1668 SDValue peekThroughBitcasts(SDValue V);
1670 /// Return the non-bitcasted and one-use source operand of \p V if it exists.
1671 /// If \p V is not a bitcasted one-use value, it is returned as-is.
1672 SDValue peekThroughOneUseBitcasts(SDValue V);
1674 /// Return the non-extracted vector source operand of \p V if it exists.
1675 /// If \p V is not an extracted subvector, it is returned as-is.
1676 SDValue peekThroughExtractSubvectors(SDValue V);
1678 /// Returns true if \p V is a bitwise not operation. Assumes that an all ones
1679 /// constant is canonicalized to be operand 1.
1680 bool isBitwiseNot(SDValue V, bool AllowUndefs = false);
1682 /// Returns the SDNode if it is a constant splat BuildVector or constant int.
1683 ConstantSDNode *isConstOrConstSplat(SDValue N, bool AllowUndefs = false,
1684 bool AllowTruncation = false);
1686 /// Returns the SDNode if it is a demanded constant splat BuildVector or
1687 /// constant int.
1688 ConstantSDNode *isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
1689 bool AllowUndefs = false,
1690 bool AllowTruncation = false);
1692 /// Returns the SDNode if it is a constant splat BuildVector or constant float.
1693 ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, bool AllowUndefs = false);
1695 /// Returns the SDNode if it is a demanded constant splat BuildVector or
1696 /// constant float.
1697 ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, const APInt &DemandedElts,
1698 bool AllowUndefs = false);
1700 /// Return true if the value is a constant 0 integer or a splatted vector of
1701 /// a constant 0 integer (with no undefs by default).
1702 /// Build vector implicit truncation is not an issue for null values.
1703 bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false);
1705 /// Return true if the value is a constant 1 integer or a splatted vector of a
1706 /// constant 1 integer (with no undefs).
1707 /// Does not permit build vector implicit truncation.
1708 bool isOneOrOneSplat(SDValue V);
1710 /// Return true if the value is a constant -1 integer or a splatted vector of a
1711 /// constant -1 integer (with no undefs).
1712 /// Does not permit build vector implicit truncation.
1713 bool isAllOnesOrAllOnesSplat(SDValue V);
1715 class GlobalAddressSDNode : public SDNode {
1716 friend class SelectionDAG;
1718 const GlobalValue *TheGlobal;
1719 int64_t Offset;
1720 unsigned TargetFlags;
1722 GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
1723 const GlobalValue *GA, EVT VT, int64_t o,
1724 unsigned TF);
1726 public:
1727 const GlobalValue *getGlobal() const { return TheGlobal; }
1728 int64_t getOffset() const { return Offset; }
1729 unsigned getTargetFlags() const { return TargetFlags; }
1730 // Return the address space this GlobalAddress belongs to.
1731 unsigned getAddressSpace() const;
1733 static bool classof(const SDNode *N) {
1734 return N->getOpcode() == ISD::GlobalAddress ||
1735 N->getOpcode() == ISD::TargetGlobalAddress ||
1736 N->getOpcode() == ISD::GlobalTLSAddress ||
1737 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1741 class FrameIndexSDNode : public SDNode {
1742 friend class SelectionDAG;
1744 int FI;
1746 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1747 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1748 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1751 public:
1752 int getIndex() const { return FI; }
1754 static bool classof(const SDNode *N) {
1755 return N->getOpcode() == ISD::FrameIndex ||
1756 N->getOpcode() == ISD::TargetFrameIndex;
1760 /// This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate
1761 /// the offet and size that are started/ended in the underlying FrameIndex.
1762 class LifetimeSDNode : public SDNode {
1763 friend class SelectionDAG;
1764 int64_t Size;
1765 int64_t Offset; // -1 if offset is unknown.
1767 LifetimeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl,
1768 SDVTList VTs, int64_t Size, int64_t Offset)
1769 : SDNode(Opcode, Order, dl, VTs), Size(Size), Offset(Offset) {}
1770 public:
1771 int64_t getFrameIndex() const {
1772 return cast<FrameIndexSDNode>(getOperand(1))->getIndex();
1775 bool hasOffset() const { return Offset >= 0; }
1776 int64_t getOffset() const {
1777 assert(hasOffset() && "offset is unknown");
1778 return Offset;
1780 int64_t getSize() const {
1781 assert(hasOffset() && "offset is unknown");
1782 return Size;
1785 // Methods to support isa and dyn_cast
1786 static bool classof(const SDNode *N) {
1787 return N->getOpcode() == ISD::LIFETIME_START ||
1788 N->getOpcode() == ISD::LIFETIME_END;
1792 class JumpTableSDNode : public SDNode {
1793 friend class SelectionDAG;
1795 int JTI;
1796 unsigned TargetFlags;
1798 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned TF)
1799 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1800 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1803 public:
1804 int getIndex() const { return JTI; }
1805 unsigned getTargetFlags() const { return TargetFlags; }
1807 static bool classof(const SDNode *N) {
1808 return N->getOpcode() == ISD::JumpTable ||
1809 N->getOpcode() == ISD::TargetJumpTable;
1813 class ConstantPoolSDNode : public SDNode {
1814 friend class SelectionDAG;
1816 union {
1817 const Constant *ConstVal;
1818 MachineConstantPoolValue *MachineCPVal;
1819 } Val;
1820 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1821 unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
1822 unsigned TargetFlags;
1824 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1825 unsigned Align, unsigned TF)
1826 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1827 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1828 TargetFlags(TF) {
1829 assert(Offset >= 0 && "Offset is too large");
1830 Val.ConstVal = c;
1833 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
1834 EVT VT, int o, unsigned Align, unsigned TF)
1835 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1836 DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
1837 TargetFlags(TF) {
1838 assert(Offset >= 0 && "Offset is too large");
1839 Val.MachineCPVal = v;
1840 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
1843 public:
1844 bool isMachineConstantPoolEntry() const {
1845 return Offset < 0;
1848 const Constant *getConstVal() const {
1849 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
1850 return Val.ConstVal;
1853 MachineConstantPoolValue *getMachineCPVal() const {
1854 assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
1855 return Val.MachineCPVal;
1858 int getOffset() const {
1859 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
1862 // Return the alignment of this constant pool object, which is either 0 (for
1863 // default alignment) or the desired value.
1864 unsigned getAlignment() const { return Alignment; }
1865 unsigned getTargetFlags() const { return TargetFlags; }
1867 Type *getType() const;
1869 static bool classof(const SDNode *N) {
1870 return N->getOpcode() == ISD::ConstantPool ||
1871 N->getOpcode() == ISD::TargetConstantPool;
1875 /// Completely target-dependent object reference.
1876 class TargetIndexSDNode : public SDNode {
1877 friend class SelectionDAG;
1879 unsigned TargetFlags;
1880 int Index;
1881 int64_t Offset;
1883 public:
1884 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned TF)
1885 : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1886 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1888 unsigned getTargetFlags() const { return TargetFlags; }
1889 int getIndex() const { return Index; }
1890 int64_t getOffset() const { return Offset; }
1892 static bool classof(const SDNode *N) {
1893 return N->getOpcode() == ISD::TargetIndex;
1897 class BasicBlockSDNode : public SDNode {
1898 friend class SelectionDAG;
1900 MachineBasicBlock *MBB;
1902 /// Debug info is meaningful and potentially useful here, but we create
1903 /// blocks out of order when they're jumped to, which makes it a bit
1904 /// harder. Let's see if we need it first.
1905 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1906 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1909 public:
1910 MachineBasicBlock *getBasicBlock() const { return MBB; }
1912 static bool classof(const SDNode *N) {
1913 return N->getOpcode() == ISD::BasicBlock;
1917 /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1918 class BuildVectorSDNode : public SDNode {
1919 public:
1920 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1921 explicit BuildVectorSDNode() = delete;
1923 /// Check if this is a constant splat, and if so, find the
1924 /// smallest element size that splats the vector. If MinSplatBits is
1925 /// nonzero, the element size must be at least that large. Note that the
1926 /// splat element may be the entire vector (i.e., a one element vector).
1927 /// Returns the splat element value in SplatValue. Any undefined bits in
1928 /// that value are zero, and the corresponding bits in the SplatUndef mask
1929 /// are set. The SplatBitSize value is set to the splat element size in
1930 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1931 /// undefined. isBigEndian describes the endianness of the target.
1932 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1933 unsigned &SplatBitSize, bool &HasAnyUndefs,
1934 unsigned MinSplatBits = 0,
1935 bool isBigEndian = false) const;
1937 /// Returns the demanded splatted value or a null value if this is not a
1938 /// splat.
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 SDValue getSplatValue(const APInt &DemandedElts,
1944 BitVector *UndefElements = nullptr) const;
1946 /// Returns the splatted value or a null value if this is not a splat.
1948 /// If passed a non-null UndefElements bitvector, it will resize it to match
1949 /// the vector width and set the bits where elements are undef.
1950 SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1952 /// Returns the demanded splatted constant or null if this is not a constant
1953 /// splat.
1955 /// The DemandedElts mask indicates the elements that must be in the splat.
1956 /// If passed a non-null UndefElements bitvector, it will resize it to match
1957 /// the vector width and set the bits where elements are undef.
1958 ConstantSDNode *
1959 getConstantSplatNode(const APInt &DemandedElts,
1960 BitVector *UndefElements = nullptr) const;
1962 /// Returns the splatted constant or null if this is not a constant
1963 /// splat.
1965 /// If passed a non-null UndefElements bitvector, it will resize it to match
1966 /// the vector width and set the bits where elements are undef.
1967 ConstantSDNode *
1968 getConstantSplatNode(BitVector *UndefElements = nullptr) const;
1970 /// Returns the demanded splatted constant FP or null if this is not a
1971 /// constant FP splat.
1973 /// The DemandedElts mask indicates the elements that must be in the splat.
1974 /// If passed a non-null UndefElements bitvector, it will resize it to match
1975 /// the vector width and set the bits where elements are undef.
1976 ConstantFPSDNode *
1977 getConstantFPSplatNode(const APInt &DemandedElts,
1978 BitVector *UndefElements = nullptr) const;
1980 /// Returns the splatted constant FP or null if this is not a constant
1981 /// FP splat.
1983 /// If passed a non-null UndefElements bitvector, it will resize it to match
1984 /// the vector width and set the bits where elements are undef.
1985 ConstantFPSDNode *
1986 getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
1988 /// If this is a constant FP splat and the splatted constant FP is an
1989 /// exact power or 2, return the log base 2 integer value. Otherwise,
1990 /// return -1.
1992 /// The BitWidth specifies the necessary bit precision.
1993 int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
1994 uint32_t BitWidth) const;
1996 bool isConstant() const;
1998 static bool classof(const SDNode *N) {
1999 return N->getOpcode() == ISD::BUILD_VECTOR;
2003 /// An SDNode that holds an arbitrary LLVM IR Value. This is
2004 /// used when the SelectionDAG needs to make a simple reference to something
2005 /// in the LLVM IR representation.
2007 class SrcValueSDNode : public SDNode {
2008 friend class SelectionDAG;
2010 const Value *V;
2012 /// Create a SrcValue for a general value.
2013 explicit SrcValueSDNode(const Value *v)
2014 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
2016 public:
2017 /// Return the contained Value.
2018 const Value *getValue() const { return V; }
2020 static bool classof(const SDNode *N) {
2021 return N->getOpcode() == ISD::SRCVALUE;
2025 class MDNodeSDNode : public SDNode {
2026 friend class SelectionDAG;
2028 const MDNode *MD;
2030 explicit MDNodeSDNode(const MDNode *md)
2031 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
2034 public:
2035 const MDNode *getMD() const { return MD; }
2037 static bool classof(const SDNode *N) {
2038 return N->getOpcode() == ISD::MDNODE_SDNODE;
2042 class RegisterSDNode : public SDNode {
2043 friend class SelectionDAG;
2045 unsigned Reg;
2047 RegisterSDNode(unsigned reg, EVT VT)
2048 : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
2050 public:
2051 unsigned getReg() const { return Reg; }
2053 static bool classof(const SDNode *N) {
2054 return N->getOpcode() == ISD::Register;
2058 class RegisterMaskSDNode : public SDNode {
2059 friend class SelectionDAG;
2061 // The memory for RegMask is not owned by the node.
2062 const uint32_t *RegMask;
2064 RegisterMaskSDNode(const uint32_t *mask)
2065 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
2066 RegMask(mask) {}
2068 public:
2069 const uint32_t *getRegMask() const { return RegMask; }
2071 static bool classof(const SDNode *N) {
2072 return N->getOpcode() == ISD::RegisterMask;
2076 class BlockAddressSDNode : public SDNode {
2077 friend class SelectionDAG;
2079 const BlockAddress *BA;
2080 int64_t Offset;
2081 unsigned TargetFlags;
2083 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
2084 int64_t o, unsigned Flags)
2085 : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
2086 BA(ba), Offset(o), TargetFlags(Flags) {}
2088 public:
2089 const BlockAddress *getBlockAddress() const { return BA; }
2090 int64_t getOffset() const { return Offset; }
2091 unsigned getTargetFlags() const { return TargetFlags; }
2093 static bool classof(const SDNode *N) {
2094 return N->getOpcode() == ISD::BlockAddress ||
2095 N->getOpcode() == ISD::TargetBlockAddress;
2099 class LabelSDNode : public SDNode {
2100 friend class SelectionDAG;
2102 MCSymbol *Label;
2104 LabelSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, MCSymbol *L)
2105 : SDNode(Opcode, Order, dl, getSDVTList(MVT::Other)), Label(L) {
2106 assert(LabelSDNode::classof(this) && "not a label opcode");
2109 public:
2110 MCSymbol *getLabel() const { return Label; }
2112 static bool classof(const SDNode *N) {
2113 return N->getOpcode() == ISD::EH_LABEL ||
2114 N->getOpcode() == ISD::ANNOTATION_LABEL;
2118 class ExternalSymbolSDNode : public SDNode {
2119 friend class SelectionDAG;
2121 const char *Symbol;
2122 unsigned TargetFlags;
2124 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned TF, EVT VT)
2125 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, 0,
2126 DebugLoc(), getSDVTList(VT)),
2127 Symbol(Sym), TargetFlags(TF) {}
2129 public:
2130 const char *getSymbol() const { return Symbol; }
2131 unsigned getTargetFlags() const { return TargetFlags; }
2133 static bool classof(const SDNode *N) {
2134 return N->getOpcode() == ISD::ExternalSymbol ||
2135 N->getOpcode() == ISD::TargetExternalSymbol;
2139 class MCSymbolSDNode : public SDNode {
2140 friend class SelectionDAG;
2142 MCSymbol *Symbol;
2144 MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
2145 : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
2147 public:
2148 MCSymbol *getMCSymbol() const { return Symbol; }
2150 static bool classof(const SDNode *N) {
2151 return N->getOpcode() == ISD::MCSymbol;
2155 class CondCodeSDNode : public SDNode {
2156 friend class SelectionDAG;
2158 ISD::CondCode Condition;
2160 explicit CondCodeSDNode(ISD::CondCode Cond)
2161 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2162 Condition(Cond) {}
2164 public:
2165 ISD::CondCode get() const { return Condition; }
2167 static bool classof(const SDNode *N) {
2168 return N->getOpcode() == ISD::CONDCODE;
2172 /// This class is used to represent EVT's, which are used
2173 /// to parameterize some operations.
2174 class VTSDNode : public SDNode {
2175 friend class SelectionDAG;
2177 EVT ValueType;
2179 explicit VTSDNode(EVT VT)
2180 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2181 ValueType(VT) {}
2183 public:
2184 EVT getVT() const { return ValueType; }
2186 static bool classof(const SDNode *N) {
2187 return N->getOpcode() == ISD::VALUETYPE;
2191 /// Base class for LoadSDNode and StoreSDNode
2192 class LSBaseSDNode : public MemSDNode {
2193 public:
2194 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
2195 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
2196 MachineMemOperand *MMO)
2197 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2198 LSBaseSDNodeBits.AddressingMode = AM;
2199 assert(getAddressingMode() == AM && "Value truncated");
2200 assert((!MMO->isAtomic() || MMO->isVolatile()) &&
2201 "use an AtomicSDNode instead for non-volatile atomics");
2204 const SDValue &getOffset() const {
2205 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
2208 /// Return the addressing mode for this load or store:
2209 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2210 ISD::MemIndexedMode getAddressingMode() const {
2211 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2214 /// Return true if this is a pre/post inc/dec load/store.
2215 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2217 /// Return true if this is NOT a pre/post inc/dec load/store.
2218 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2220 static bool classof(const SDNode *N) {
2221 return N->getOpcode() == ISD::LOAD ||
2222 N->getOpcode() == ISD::STORE;
2226 /// This class is used to represent ISD::LOAD nodes.
2227 class LoadSDNode : public LSBaseSDNode {
2228 friend class SelectionDAG;
2230 LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2231 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
2232 MachineMemOperand *MMO)
2233 : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
2234 LoadSDNodeBits.ExtTy = ETy;
2235 assert(readMem() && "Load MachineMemOperand is not a load!");
2236 assert(!writeMem() && "Load MachineMemOperand is a store!");
2239 public:
2240 /// Return whether this is a plain node,
2241 /// or one of the varieties of value-extending loads.
2242 ISD::LoadExtType getExtensionType() const {
2243 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2246 const SDValue &getBasePtr() const { return getOperand(1); }
2247 const SDValue &getOffset() const { return getOperand(2); }
2249 static bool classof(const SDNode *N) {
2250 return N->getOpcode() == ISD::LOAD;
2254 /// This class is used to represent ISD::STORE nodes.
2255 class StoreSDNode : public LSBaseSDNode {
2256 friend class SelectionDAG;
2258 StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2259 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
2260 MachineMemOperand *MMO)
2261 : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
2262 StoreSDNodeBits.IsTruncating = isTrunc;
2263 assert(!readMem() && "Store MachineMemOperand is a load!");
2264 assert(writeMem() && "Store MachineMemOperand is not a store!");
2267 public:
2268 /// Return true if the op does a truncation before store.
2269 /// For integers this is the same as doing a TRUNCATE and storing the result.
2270 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2271 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2272 void setTruncatingStore(bool Truncating) {
2273 StoreSDNodeBits.IsTruncating = Truncating;
2276 const SDValue &getValue() const { return getOperand(1); }
2277 const SDValue &getBasePtr() const { return getOperand(2); }
2278 const SDValue &getOffset() const { return getOperand(3); }
2280 static bool classof(const SDNode *N) {
2281 return N->getOpcode() == ISD::STORE;
2285 /// This base class is used to represent MLOAD and MSTORE nodes
2286 class MaskedLoadStoreSDNode : public MemSDNode {
2287 public:
2288 friend class SelectionDAG;
2290 MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2291 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2292 MachineMemOperand *MMO)
2293 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {}
2295 // MaskedLoadSDNode (Chain, ptr, mask, passthru)
2296 // MaskedStoreSDNode (Chain, data, ptr, mask)
2297 // Mask is a vector of i1 elements
2298 const SDValue &getBasePtr() const {
2299 return getOperand(getOpcode() == ISD::MLOAD ? 1 : 2);
2301 const SDValue &getMask() const {
2302 return getOperand(getOpcode() == ISD::MLOAD ? 2 : 3);
2305 static bool classof(const SDNode *N) {
2306 return N->getOpcode() == ISD::MLOAD ||
2307 N->getOpcode() == ISD::MSTORE;
2311 /// This class is used to represent an MLOAD node
2312 class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
2313 public:
2314 friend class SelectionDAG;
2316 MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2317 ISD::LoadExtType ETy, bool IsExpanding, EVT MemVT,
2318 MachineMemOperand *MMO)
2319 : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, MemVT, MMO) {
2320 LoadSDNodeBits.ExtTy = ETy;
2321 LoadSDNodeBits.IsExpanding = IsExpanding;
2324 ISD::LoadExtType getExtensionType() const {
2325 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2328 const SDValue &getBasePtr() const { return getOperand(1); }
2329 const SDValue &getMask() const { return getOperand(2); }
2330 const SDValue &getPassThru() const { return getOperand(3); }
2332 static bool classof(const SDNode *N) {
2333 return N->getOpcode() == ISD::MLOAD;
2336 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2339 /// This class is used to represent an MSTORE node
2340 class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2341 public:
2342 friend class SelectionDAG;
2344 MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2345 bool isTrunc, bool isCompressing, EVT MemVT,
2346 MachineMemOperand *MMO)
2347 : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, MemVT, MMO) {
2348 StoreSDNodeBits.IsTruncating = isTrunc;
2349 StoreSDNodeBits.IsCompressing = isCompressing;
2352 /// Return true if the op does a truncation before store.
2353 /// For integers this is the same as doing a TRUNCATE and storing the result.
2354 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2355 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2357 /// Returns true if the op does a compression to the vector before storing.
2358 /// The node contiguously stores the active elements (integers or floats)
2359 /// in src (those with their respective bit set in writemask k) to unaligned
2360 /// memory at base_addr.
2361 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2363 const SDValue &getValue() const { return getOperand(1); }
2364 const SDValue &getBasePtr() const { return getOperand(2); }
2365 const SDValue &getMask() const { return getOperand(3); }
2367 static bool classof(const SDNode *N) {
2368 return N->getOpcode() == ISD::MSTORE;
2372 /// This is a base class used to represent
2373 /// MGATHER and MSCATTER nodes
2375 class MaskedGatherScatterSDNode : public MemSDNode {
2376 public:
2377 friend class SelectionDAG;
2379 MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2380 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2381 MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2382 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2383 LSBaseSDNodeBits.AddressingMode = IndexType;
2384 assert(getIndexType() == IndexType && "Value truncated");
2387 /// How is Index applied to BasePtr when computing addresses.
2388 ISD::MemIndexType getIndexType() const {
2389 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
2391 bool isIndexScaled() const {
2392 return (getIndexType() == ISD::SIGNED_SCALED) ||
2393 (getIndexType() == ISD::UNSIGNED_SCALED);
2395 bool isIndexSigned() const {
2396 return (getIndexType() == ISD::SIGNED_SCALED) ||
2397 (getIndexType() == ISD::SIGNED_UNSCALED);
2400 // In the both nodes address is Op1, mask is Op2:
2401 // MaskedGatherSDNode (Chain, passthru, mask, base, index, scale)
2402 // MaskedScatterSDNode (Chain, value, mask, base, index, scale)
2403 // Mask is a vector of i1 elements
2404 const SDValue &getBasePtr() const { return getOperand(3); }
2405 const SDValue &getIndex() const { return getOperand(4); }
2406 const SDValue &getMask() const { return getOperand(2); }
2407 const SDValue &getScale() const { return getOperand(5); }
2409 static bool classof(const SDNode *N) {
2410 return N->getOpcode() == ISD::MGATHER ||
2411 N->getOpcode() == ISD::MSCATTER;
2415 /// This class is used to represent an MGATHER node
2417 class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2418 public:
2419 friend class SelectionDAG;
2421 MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2422 EVT MemVT, MachineMemOperand *MMO,
2423 ISD::MemIndexType IndexType)
2424 : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO,
2425 IndexType) {}
2427 const SDValue &getPassThru() const { return getOperand(1); }
2429 static bool classof(const SDNode *N) {
2430 return N->getOpcode() == ISD::MGATHER;
2434 /// This class is used to represent an MSCATTER node
2436 class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2437 public:
2438 friend class SelectionDAG;
2440 MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2441 EVT MemVT, MachineMemOperand *MMO,
2442 ISD::MemIndexType IndexType)
2443 : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO,
2444 IndexType) {}
2446 const SDValue &getValue() const { return getOperand(1); }
2448 static bool classof(const SDNode *N) {
2449 return N->getOpcode() == ISD::MSCATTER;
2453 /// An SDNode that represents everything that will be needed
2454 /// to construct a MachineInstr. These nodes are created during the
2455 /// instruction selection proper phase.
2457 /// Note that the only supported way to set the `memoperands` is by calling the
2458 /// `SelectionDAG::setNodeMemRefs` function as the memory management happens
2459 /// inside the DAG rather than in the node.
2460 class MachineSDNode : public SDNode {
2461 private:
2462 friend class SelectionDAG;
2464 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
2465 : SDNode(Opc, Order, DL, VTs) {}
2467 // We use a pointer union between a single `MachineMemOperand` pointer and
2468 // a pointer to an array of `MachineMemOperand` pointers. This is null when
2469 // the number of these is zero, the single pointer variant used when the
2470 // number is one, and the array is used for larger numbers.
2472 // The array is allocated via the `SelectionDAG`'s allocator and so will
2473 // always live until the DAG is cleaned up and doesn't require ownership here.
2475 // We can't use something simpler like `TinyPtrVector` here because `SDNode`
2476 // subclasses aren't managed in a conforming C++ manner. See the comments on
2477 // `SelectionDAG::MorphNodeTo` which details what all goes on, but the
2478 // constraint here is that these don't manage memory with their constructor or
2479 // destructor and can be initialized to a good state even if they start off
2480 // uninitialized.
2481 PointerUnion<MachineMemOperand *, MachineMemOperand **> MemRefs = {};
2483 // Note that this could be folded into the above `MemRefs` member if doing so
2484 // is advantageous at some point. We don't need to store this in most cases.
2485 // However, at the moment this doesn't appear to make the allocation any
2486 // smaller and makes the code somewhat simpler to read.
2487 int NumMemRefs = 0;
2489 public:
2490 using mmo_iterator = ArrayRef<MachineMemOperand *>::const_iterator;
2492 ArrayRef<MachineMemOperand *> memoperands() const {
2493 // Special case the common cases.
2494 if (NumMemRefs == 0)
2495 return {};
2496 if (NumMemRefs == 1)
2497 return makeArrayRef(MemRefs.getAddrOfPtr1(), 1);
2499 // Otherwise we have an actual array.
2500 return makeArrayRef(MemRefs.get<MachineMemOperand **>(), NumMemRefs);
2502 mmo_iterator memoperands_begin() const { return memoperands().begin(); }
2503 mmo_iterator memoperands_end() const { return memoperands().end(); }
2504 bool memoperands_empty() const { return memoperands().empty(); }
2506 /// Clear out the memory reference descriptor list.
2507 void clearMemRefs() {
2508 MemRefs = nullptr;
2509 NumMemRefs = 0;
2512 static bool classof(const SDNode *N) {
2513 return N->isMachineOpcode();
2517 class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
2518 SDNode, ptrdiff_t> {
2519 const SDNode *Node;
2520 unsigned Operand;
2522 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2524 public:
2525 bool operator==(const SDNodeIterator& x) const {
2526 return Operand == x.Operand;
2528 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2530 pointer operator*() const {
2531 return Node->getOperand(Operand).getNode();
2533 pointer operator->() const { return operator*(); }
2535 SDNodeIterator& operator++() { // Preincrement
2536 ++Operand;
2537 return *this;
2539 SDNodeIterator operator++(int) { // Postincrement
2540 SDNodeIterator tmp = *this; ++*this; return tmp;
2542 size_t operator-(SDNodeIterator Other) const {
2543 assert(Node == Other.Node &&
2544 "Cannot compare iterators of two different nodes!");
2545 return Operand - Other.Operand;
2548 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2549 static SDNodeIterator end (const SDNode *N) {
2550 return SDNodeIterator(N, N->getNumOperands());
2553 unsigned getOperand() const { return Operand; }
2554 const SDNode *getNode() const { return Node; }
2557 template <> struct GraphTraits<SDNode*> {
2558 using NodeRef = SDNode *;
2559 using ChildIteratorType = SDNodeIterator;
2561 static NodeRef getEntryNode(SDNode *N) { return N; }
2563 static ChildIteratorType child_begin(NodeRef N) {
2564 return SDNodeIterator::begin(N);
2567 static ChildIteratorType child_end(NodeRef N) {
2568 return SDNodeIterator::end(N);
2572 /// A representation of the largest SDNode, for use in sizeof().
2574 /// This needs to be a union because the largest node differs on 32 bit systems
2575 /// with 4 and 8 byte pointer alignment, respectively.
2576 using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
2577 BlockAddressSDNode,
2578 GlobalAddressSDNode>;
2580 /// The SDNode class with the greatest alignment requirement.
2581 using MostAlignedSDNode = GlobalAddressSDNode;
2583 namespace ISD {
2585 /// Returns true if the specified node is a non-extending and unindexed load.
2586 inline bool isNormalLoad(const SDNode *N) {
2587 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
2588 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
2589 Ld->getAddressingMode() == ISD::UNINDEXED;
2592 /// Returns true if the specified node is a non-extending load.
2593 inline bool isNON_EXTLoad(const SDNode *N) {
2594 return isa<LoadSDNode>(N) &&
2595 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
2598 /// Returns true if the specified node is a EXTLOAD.
2599 inline bool isEXTLoad(const SDNode *N) {
2600 return isa<LoadSDNode>(N) &&
2601 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
2604 /// Returns true if the specified node is a SEXTLOAD.
2605 inline bool isSEXTLoad(const SDNode *N) {
2606 return isa<LoadSDNode>(N) &&
2607 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
2610 /// Returns true if the specified node is a ZEXTLOAD.
2611 inline bool isZEXTLoad(const SDNode *N) {
2612 return isa<LoadSDNode>(N) &&
2613 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
2616 /// Returns true if the specified node is an unindexed load.
2617 inline bool isUNINDEXEDLoad(const SDNode *N) {
2618 return isa<LoadSDNode>(N) &&
2619 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2622 /// Returns true if the specified node is a non-truncating
2623 /// and unindexed store.
2624 inline bool isNormalStore(const SDNode *N) {
2625 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
2626 return St && !St->isTruncatingStore() &&
2627 St->getAddressingMode() == ISD::UNINDEXED;
2630 /// Returns true if the specified node is a non-truncating store.
2631 inline bool isNON_TRUNCStore(const SDNode *N) {
2632 return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
2635 /// Returns true if the specified node is a truncating store.
2636 inline bool isTRUNCStore(const SDNode *N) {
2637 return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
2640 /// Returns true if the specified node is an unindexed store.
2641 inline bool isUNINDEXEDStore(const SDNode *N) {
2642 return isa<StoreSDNode>(N) &&
2643 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
2646 /// Attempt to match a unary predicate against a scalar/splat constant or
2647 /// every element of a constant BUILD_VECTOR.
2648 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
2649 bool matchUnaryPredicate(SDValue Op,
2650 std::function<bool(ConstantSDNode *)> Match,
2651 bool AllowUndefs = false);
2653 /// Attempt to match a binary predicate against a pair of scalar/splat
2654 /// constants or every element of a pair of constant BUILD_VECTORs.
2655 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
2656 /// If AllowTypeMismatch is true then RetType + ArgTypes don't need to match.
2657 bool matchBinaryPredicate(
2658 SDValue LHS, SDValue RHS,
2659 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
2660 bool AllowUndefs = false, bool AllowTypeMismatch = false);
2661 } // end namespace ISD
2663 } // end namespace llvm
2665 #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H