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