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