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