Debian Codegraph @

suite/synctools/tablegen/include/llvm/CodeGen/SelectionDAG.h

raw symbols SCIP
1 //===- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the SelectionDAG class, and transitively defines the
10 // SDNode class and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
15 #define LLVM_CODEGEN_SELECTIONDAG_H
16
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/ilist.h"
27 #include "llvm/ADT/iterator.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/CodeGen/DAGCombine.h"
30 #include "llvm/CodeGen/ISDOpcodes.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/SelectionDAGNodes.h"
34 #include "llvm/CodeGen/ValueTypes.h"
35 #include "llvm/IR/DebugLoc.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/Metadata.h"
38 #include "llvm/Support/Allocator.h"
39 #include "llvm/Support/ArrayRecycler.h"
40 #include "llvm/Support/AtomicOrdering.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CodeGen.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MachineValueType.h"
45 #include "llvm/Support/RecyclingAllocator.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstdint>
49 #include <functional>
50 #include <map>
51 #include <string>
52 #include <tuple>
53 #include <utility>
54 #include <vector>
55
56 namespace llvm {
57
58 class AAResults;
59 class BlockAddress;
60 class BlockFrequencyInfo;
61 class Constant;
62 class ConstantFP;
63 class ConstantInt;
64 class DataLayout;
65 struct fltSemantics;
66 class FunctionLoweringInfo;
67 class GlobalValue;
68 struct KnownBits;
69 class LegacyDivergenceAnalysis;
70 class LLVMContext;
71 class MachineBasicBlock;
72 class MachineConstantPoolValue;
73 class MCSymbol;
74 class OptimizationRemarkEmitter;
75 class ProfileSummaryInfo;
76 class SDDbgValue;
77 class SDDbgOperand;
78 class SDDbgLabel;
79 class SelectionDAG;
80 class SelectionDAGTargetInfo;
81 class TargetLibraryInfo;
82 class TargetLowering;
83 class TargetMachine;
84 class TargetSubtargetInfo;
85 class Value;
86
87 class SDVTListNode : public FoldingSetNode {
88 friend struct FoldingSetTrait<SDVTListNode>;
89
90 /// A reference to an Interned FoldingSetNodeID for this node.
91 /// The Allocator in SelectionDAG holds the data.
92 /// SDVTList contains all types which are frequently accessed in SelectionDAG.
93 /// The size of this list is not expected to be big so it won't introduce
94 /// a memory penalty.
95 FoldingSetNodeIDRef FastID;
96 const EVT *VTs;
97 unsigned int NumVTs;
98 /// The hash value for SDVTList is fixed, so cache it to avoid
99 /// hash calculation.
100 unsigned HashValue;
101
102 public:
103 SDVTListNode(const FoldingSetNodeIDRef ID, const EVT *VT, unsigned int Num) :
104 FastID(ID), VTs(VT), NumVTs(Num) {
105 HashValue = ID.ComputeHash();
106 }
107
108 SDVTList getSDVTList() {
109 SDVTList result = {VTs, NumVTs};
110 return result;
111 }
112 };
113
114 /// Specialize FoldingSetTrait for SDVTListNode
115 /// to avoid computing temp FoldingSetNodeID and hash value.
116 template<> struct FoldingSetTrait<SDVTListNode> : DefaultFoldingSetTrait<SDVTListNode> {
117 static void Profile(const SDVTListNode &X, FoldingSetNodeID& ID) {
118 ID = X.FastID;
119 }
120
121 static bool Equals(const SDVTListNode &X, const FoldingSetNodeID &ID,
122 unsigned IDHash, FoldingSetNodeID &TempID) {
123 if (X.HashValue != IDHash)
124 return false;
125 return ID == X.FastID;
126 }
127
128 static unsigned ComputeHash(const SDVTListNode &X, FoldingSetNodeID &TempID) {
129 return X.HashValue;
130 }
131 };
132
133 template <> struct ilist_alloc_traits<SDNode> {
134 static void deleteNode(SDNode *) {
135 llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!");
136 }
137 };
138
139 /// Keeps track of dbg_value information through SDISel. We do
140 /// not build SDNodes for these so as not to perturb the generated code;
141 /// instead the info is kept off to the side in this structure. Each SDNode may
142 /// have one or more associated dbg_value entries. This information is kept in
143 /// DbgValMap.
144 /// Byval parameters are handled separately because they don't use alloca's,
145 /// which busts the normal mechanism. There is good reason for handling all
146 /// parameters separately: they may not have code generated for them, they
147 /// should always go at the beginning of the function regardless of other code
148 /// motion, and debug info for them is potentially useful even if the parameter
149 /// is unused. Right now only byval parameters are handled separately.
150 class SDDbgInfo {
151 BumpPtrAllocator Alloc;
152 SmallVector<SDDbgValue*, 32> DbgValues;
153 SmallVector<SDDbgValue*, 32> ByvalParmDbgValues;
154 SmallVector<SDDbgLabel*, 4> DbgLabels;
155 using DbgValMapType = DenseMap<const SDNode *, SmallVector<SDDbgValue *, 2>>;
156 DbgValMapType DbgValMap;
157
158 public:
159 SDDbgInfo() = default;
160 SDDbgInfo(const SDDbgInfo &) = delete;
161 SDDbgInfo &operator=(const SDDbgInfo &) = delete;
162
163 void add(SDDbgValue *V, bool isParameter);
164
165 void add(SDDbgLabel *L) { DbgLabels.push_back(L); }
166
167 /// Invalidate all DbgValues attached to the node and remove
168 /// it from the Node-to-DbgValues map.
169 void erase(const SDNode *Node);
170
171 void clear() {
172 DbgValMap.clear();
173 DbgValues.clear();
174 ByvalParmDbgValues.clear();
175 DbgLabels.clear();
176 Alloc.Reset();
177 }
178
179 BumpPtrAllocator &getAlloc() { return Alloc; }
180
181 bool empty() const {
182 return DbgValues.empty() && ByvalParmDbgValues.empty() && DbgLabels.empty();
183 }
184
185 ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) const {
186 auto I = DbgValMap.find(Node);
187 if (I != DbgValMap.end())
188 return I->second;
189 return ArrayRef<SDDbgValue*>();
190 }
191
192 using DbgIterator = SmallVectorImpl<SDDbgValue*>::iterator;
193 using DbgLabelIterator = SmallVectorImpl<SDDbgLabel*>::iterator;
194
195 DbgIterator DbgBegin() { return DbgValues.begin(); }
196 DbgIterator DbgEnd() { return DbgValues.end(); }
197 DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); }
198 DbgIterator ByvalParmDbgEnd() { return ByvalParmDbgValues.end(); }
199 DbgLabelIterator DbgLabelBegin() { return DbgLabels.begin(); }
200 DbgLabelIterator DbgLabelEnd() { return DbgLabels.end(); }
201 };
202
203 void checkForCycles(const SelectionDAG *DAG, bool force = false);
204
205 /// This is used to represent a portion of an LLVM function in a low-level
206 /// Data Dependence DAG representation suitable for instruction selection.
207 /// This DAG is constructed as the first step of instruction selection in order
208 /// to allow implementation of machine specific optimizations
209 /// and code simplifications.
210 ///
211 /// The representation used by the SelectionDAG is a target-independent
212 /// representation, which has some similarities to the GCC RTL representation,
213 /// but is significantly more simple, powerful, and is a graph form instead of a
214 /// linear form.
215 ///
216 class SelectionDAG {
217 const TargetMachine &TM;
218 const SelectionDAGTargetInfo *TSI = nullptr;
219 const TargetLowering *TLI = nullptr;
220 const TargetLibraryInfo *LibInfo = nullptr;
221 MachineFunction *MF;
222 Pass *SDAGISelPass = nullptr;
223 LLVMContext *Context;
224 CodeGenOpt::Level OptLevel;
225
226 LegacyDivergenceAnalysis * DA = nullptr;
227 FunctionLoweringInfo * FLI = nullptr;
228
229 /// The function-level optimization remark emitter. Used to emit remarks
230 /// whenever manipulating the DAG.
231 OptimizationRemarkEmitter *ORE;
232
233 ProfileSummaryInfo *PSI = nullptr;
234 BlockFrequencyInfo *BFI = nullptr;
235
236 /// The starting token.
237 SDNode EntryNode;
238
239 /// The root of the entire DAG.
240 SDValue Root;
241
242 /// A linked list of nodes in the current DAG.
243 ilist<SDNode> AllNodes;
244
245 /// The AllocatorType for allocating SDNodes. We use
246 /// pool allocation with recycling.
247 using NodeAllocatorType = RecyclingAllocator<BumpPtrAllocator, SDNode,
248 sizeof(LargestSDNode),
249 alignof(MostAlignedSDNode)>;
250
251 /// Pool allocation for nodes.
252 NodeAllocatorType NodeAllocator;
253
254 /// This structure is used to memoize nodes, automatically performing
255 /// CSE with existing nodes when a duplicate is requested.
256 FoldingSet<SDNode> CSEMap;
257
258 /// Pool allocation for machine-opcode SDNode operands.
259 BumpPtrAllocator OperandAllocator;
260 ArrayRecycler<SDUse> OperandRecycler;
261
262 /// Pool allocation for misc. objects that are created once per SelectionDAG.
263 BumpPtrAllocator Allocator;
264
265 /// Tracks dbg_value and dbg_label information through SDISel.
266 SDDbgInfo *DbgInfo;
267
268 using CallSiteInfo = MachineFunction::CallSiteInfo;
269 using CallSiteInfoImpl = MachineFunction::CallSiteInfoImpl;
270
271 struct CallSiteDbgInfo {
272 CallSiteInfo CSInfo;
273 MDNode *HeapAllocSite = nullptr;
274 bool NoMerge = false;
275 };
276
277 DenseMap<const SDNode *, CallSiteDbgInfo> SDCallSiteDbgInfo;
278
279 uint16_t NextPersistentId = 0;
280
281 /// Are instruction referencing variable locations desired for this function?
282 bool UseInstrRefDebugInfo = false;
283
284 public:
285 /// Clients of various APIs that cause global effects on
286 /// the DAG can optionally implement this interface. This allows the clients
287 /// to handle the various sorts of updates that happen.
288 ///
289 /// A DAGUpdateListener automatically registers itself with DAG when it is
290 /// constructed, and removes itself when destroyed in RAII fashion.
291 struct DAGUpdateListener {
292 DAGUpdateListener *const Next;
293 SelectionDAG &DAG;
294
295 explicit DAGUpdateListener(SelectionDAG &D)
296 : Next(D.UpdateListeners), DAG(D) {
297 DAG.UpdateListeners = this;
298 }
299
300 virtual ~DAGUpdateListener() {
301 assert(DAG.UpdateListeners == this &&
302 "DAGUpdateListeners must be destroyed in LIFO order");
303 DAG.UpdateListeners = Next;
304 }
305
306 /// The node N that was deleted and, if E is not null, an
307 /// equivalent node E that replaced it.
308 virtual void NodeDeleted(SDNode *N, SDNode *E);
309
310 /// The node N that was updated.
311 virtual void NodeUpdated(SDNode *N);
312
313 /// The node N that was inserted.
314 virtual void NodeInserted(SDNode *N);
315 };
316
317 struct DAGNodeDeletedListener : public DAGUpdateListener {
318 std::function<void(SDNode *, SDNode *)> Callback;
319
320 DAGNodeDeletedListener(SelectionDAG &DAG,
321 std::function<void(SDNode *, SDNode *)> Callback)
322 : DAGUpdateListener(DAG), Callback(std::move(Callback)) {}
323
324 void NodeDeleted(SDNode *N, SDNode *E) override { Callback(N, E); }
325
326 private:
327 virtual void anchor();
328 };
329
330 /// Help to insert SDNodeFlags automatically in transforming. Use
331 /// RAII to save and resume flags in current scope.
332 class FlagInserter {
333 SelectionDAG &DAG;
334 SDNodeFlags Flags;
335 FlagInserter *LastInserter;
336
337 public:
338 FlagInserter(SelectionDAG &SDAG, SDNodeFlags Flags)
339 : DAG(SDAG), Flags(Flags),
340 LastInserter(SDAG.getFlagInserter()) {
341 SDAG.setFlagInserter(this);
342 }
343 FlagInserter(SelectionDAG &SDAG, SDNode *N)
344 : FlagInserter(SDAG, N->getFlags()) {}
345
346 FlagInserter(const FlagInserter &) = delete;
347 FlagInserter &operator=(const FlagInserter &) = delete;
348 ~FlagInserter() { DAG.setFlagInserter(LastInserter); }
349
350 SDNodeFlags getFlags() const { return Flags; }
351 };
352
353 /// When true, additional steps are taken to
354 /// ensure that getConstant() and similar functions return DAG nodes that
355 /// have legal types. This is important after type legalization since
356 /// any illegally typed nodes generated after this point will not experience
357 /// type legalization.
358 bool NewNodesMustHaveLegalTypes = false;
359
360 private:
361 /// DAGUpdateListener is a friend so it can manipulate the listener stack.
362 friend struct DAGUpdateListener;
363
364 /// Linked list of registered DAGUpdateListener instances.
365 /// This stack is maintained by DAGUpdateListener RAII.
366 DAGUpdateListener *UpdateListeners = nullptr;
367
368 /// Implementation of setSubgraphColor.
369 /// Return whether we had to truncate the search.
370 bool setSubgraphColorHelper(SDNode *N, const char *Color,
371 DenseSet<SDNode *> &visited,
372 int level, bool &printed);
373
374 template <typename SDNodeT, typename... ArgTypes>
375 SDNodeT *newSDNode(ArgTypes &&... Args) {
376 return new (NodeAllocator.template Allocate<SDNodeT>())
377 SDNodeT(std::forward<ArgTypes>(Args)...);
378 }
379
380 /// Build a synthetic SDNodeT with the given args and extract its subclass
381 /// data as an integer (e.g. for use in a folding set).
382 ///
383 /// The args to this function are the same as the args to SDNodeT's
384 /// constructor, except the second arg (assumed to be a const DebugLoc&) is
385 /// omitted.
386 template <typename SDNodeT, typename... ArgTypes>
387 static uint16_t getSyntheticNodeSubclassData(unsigned IROrder,
388 ArgTypes &&... Args) {
389 // The compiler can reduce this expression to a constant iff we pass an
390 // empty DebugLoc. Thankfully, the debug location doesn't have any bearing
391 // on the subclass data.
392 return SDNodeT(IROrder, DebugLoc(), std::forward<ArgTypes>(Args)...)
393 .getRawSubclassData();
394 }
395
396 template <typename SDNodeTy>
397 static uint16_t getSyntheticNodeSubclassData(unsigned Opc, unsigned Order,
398 SDVTList VTs, EVT MemoryVT,
399 MachineMemOperand *MMO) {
400 return SDNodeTy(Opc, Order, DebugLoc(), VTs, MemoryVT, MMO)
401 .getRawSubclassData();
402 }
403
404 void createOperands(SDNode *Node, ArrayRef<SDValue> Vals);
405
406 void removeOperands(SDNode *Node) {
407 if (!Node->OperandList)
408 return;
409 OperandRecycler.deallocate(
410 ArrayRecycler<SDUse>::Capacity::get(Node->NumOperands),
411 Node->OperandList);
412 Node->NumOperands = 0;
413 Node->OperandList = nullptr;
414 }
415 void CreateTopologicalOrder(std::vector<SDNode*>& Order);
416
417 public:
418 // Maximum depth for recursive analysis such as computeKnownBits, etc.
419 static constexpr unsigned MaxRecursionDepth = 6;
420
421 explicit SelectionDAG(const TargetMachine &TM, CodeGenOpt::Level);
422 SelectionDAG(const SelectionDAG &) = delete;
423 SelectionDAG &operator=(const SelectionDAG &) = delete;
424 ~SelectionDAG();
425
426 /// Prepare this SelectionDAG to process code in the given MachineFunction.
427 void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE,
428 Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
429 LegacyDivergenceAnalysis * Divergence,
430 ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin);
431
432 void setFunctionLoweringInfo(FunctionLoweringInfo * FuncInfo) {
433 FLI = FuncInfo;
434 }
435
436 /// Clear state and free memory necessary to make this
437 /// SelectionDAG ready to process a new block.
438 void clear();
439
440 MachineFunction &getMachineFunction() const { return *MF; }
441 const Pass *getPass() const { return SDAGISelPass; }
442
443 const DataLayout &getDataLayout() const { return MF->getDataLayout(); }
444 const TargetMachine &getTarget() const { return TM; }
445 const TargetSubtargetInfo &getSubtarget() const { return MF->getSubtarget(); }
446 const TargetLowering &getTargetLoweringInfo() const { return *TLI; }
447 const TargetLibraryInfo &getLibInfo() const { return *LibInfo; }
448 const SelectionDAGTargetInfo &getSelectionDAGInfo() const { return *TSI; }
449 const LegacyDivergenceAnalysis *getDivergenceAnalysis() const { return DA; }
450 LLVMContext *getContext() const { return Context; }
451 OptimizationRemarkEmitter &getORE() const { return *ORE; }
452 ProfileSummaryInfo *getPSI() const { return PSI; }
453 BlockFrequencyInfo *getBFI() const { return BFI; }
454
455 FlagInserter *getFlagInserter() { return Inserter; }
456 void setFlagInserter(FlagInserter *FI) { Inserter = FI; }
457
458 /// Just dump dot graph to a user-provided path and title.
459 /// This doesn't open the dot viewer program and
460 /// helps visualization when outside debugging session.
461 /// FileName expects absolute path. If provided
462 /// without any path separators then the file
463 /// will be created in the current directory.
464 /// Error will be emitted if the path is insane.
465 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
466 LLVM_DUMP_METHOD void dumpDotGraph(const Twine &FileName, const Twine &Title);
467 #endif
468
469 /// Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
470 void viewGraph(const std::string &Title);
471 void viewGraph();
472
473 #ifndef NDEBUG
474 std::map<const SDNode *, std::string> NodeGraphAttrs;
475 #endif
476
477 /// Clear all previously defined node graph attributes.
478 /// Intended to be used from a debugging tool (eg. gdb).
479 void clearGraphAttrs();
480
481 /// Set graph attributes for a node. (eg. "color=red".)
482 void setGraphAttrs(const SDNode *N, const char *Attrs);
483
484 /// Get graph attributes for a node. (eg. "color=red".)
485 /// Used from getNodeAttributes.
486 std::string getGraphAttrs(const SDNode *N) const;
487
488 /// Convenience for setting node color attribute.
489 void setGraphColor(const SDNode *N, const char *Color);
490
491 /// Convenience for setting subgraph color attribute.
492 void setSubgraphColor(SDNode *N, const char *Color);
493
494 using allnodes_const_iterator = ilist<SDNode>::const_iterator;
495
496 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
497 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
498
499 using allnodes_iterator = ilist<SDNode>::iterator;
500
501 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
502 allnodes_iterator allnodes_end() { return AllNodes.end(); }
503
504 ilist<SDNode>::size_type allnodes_size() const {
505 return AllNodes.size();
506 }
507
508 iterator_range<allnodes_iterator> allnodes() {
509 return make_range(allnodes_begin(), allnodes_end());
510 }
511 iterator_range<allnodes_const_iterator> allnodes() const {
512 return make_range(allnodes_begin(), allnodes_end());
513 }
514
515 /// Return the root tag of the SelectionDAG.
516 const SDValue &getRoot() const { return Root; }
517
518 /// Return the token chain corresponding to the entry of the function.
519 SDValue getEntryNode() const {
520 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
521 }
522
523 /// Set the current root tag of the SelectionDAG.
524 ///
525 const SDValue &setRoot(SDValue N) {
526 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
527 "DAG root value is not a chain!");
528 if (N.getNode())
529 checkForCycles(N.getNode(), this);
530 Root = N;
531 if (N.getNode())
532 checkForCycles(this);
533 return Root;
534 }
535
536 #ifndef NDEBUG
537 void VerifyDAGDivergence();
538 #endif
539
540 /// This iterates over the nodes in the SelectionDAG, folding
541 /// certain types of nodes together, or eliminating superfluous nodes. The
542 /// Level argument controls whether Combine is allowed to produce nodes and
543 /// types that are illegal on the target.
544 void Combine(CombineLevel Level, AAResults *AA,
545 CodeGenOpt::Level OptLevel);
546
547 /// This transforms the SelectionDAG into a SelectionDAG that
548 /// only uses types natively supported by the target.
549 /// Returns "true" if it made any changes.
550 ///
551 /// Note that this is an involved process that may invalidate pointers into
552 /// the graph.
553 bool LegalizeTypes();
554
555 /// This transforms the SelectionDAG into a SelectionDAG that is
556 /// compatible with the target instruction selector, as indicated by the
557 /// TargetLowering object.
558 ///
559 /// Note that this is an involved process that may invalidate pointers into
560 /// the graph.
561 void Legalize();
562
563 /// Transforms a SelectionDAG node and any operands to it into a node
564 /// that is compatible with the target instruction selector, as indicated by
565 /// the TargetLowering object.
566 ///
567 /// \returns true if \c N is a valid, legal node after calling this.
568 ///
569 /// This essentially runs a single recursive walk of the \c Legalize process
570 /// over the given node (and its operands). This can be used to incrementally
571 /// legalize the DAG. All of the nodes which are directly replaced,
572 /// potentially including N, are added to the output parameter \c
573 /// UpdatedNodes so that the delta to the DAG can be understood by the
574 /// caller.
575 ///
576 /// When this returns false, N has been legalized in a way that make the
577 /// pointer passed in no longer valid. It may have even been deleted from the
578 /// DAG, and so it shouldn't be used further. When this returns true, the
579 /// N passed in is a legal node, and can be immediately processed as such.
580 /// This may still have done some work on the DAG, and will still populate
581 /// UpdatedNodes with any new nodes replacing those originally in the DAG.
582 bool LegalizeOp(SDNode *N, SmallSetVector<SDNode *, 16> &UpdatedNodes);
583
584 /// This transforms the SelectionDAG into a SelectionDAG
585 /// that only uses vector math operations supported by the target. This is
586 /// necessary as a separate step from Legalize because unrolling a vector
587 /// operation can introduce illegal types, which requires running
588 /// LegalizeTypes again.
589 ///
590 /// This returns true if it made any changes; in that case, LegalizeTypes
591 /// is called again before Legalize.
592 ///
593 /// Note that this is an involved process that may invalidate pointers into
594 /// the graph.
595 bool LegalizeVectors();
596
597 /// This method deletes all unreachable nodes in the SelectionDAG.
598 void RemoveDeadNodes();
599
600 /// Remove the specified node from the system. This node must
601 /// have no referrers.
602 void DeleteNode(SDNode *N);
603
604 /// Return an SDVTList that represents the list of values specified.
605 SDVTList getVTList(EVT VT);
606 SDVTList getVTList(EVT VT1, EVT VT2);
607 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
608 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
609 SDVTList getVTList(ArrayRef<EVT> VTs);
610
611 //===--------------------------------------------------------------------===//
612 // Node creation methods.
613
614 /// Create a ConstantSDNode wrapping a constant value.
615 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
616 ///
617 /// If only legal types can be produced, this does the necessary
618 /// transformations (e.g., if the vector element type is illegal).
619 /// @{
620 SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
621 bool isTarget = false, bool isOpaque = false);
622 SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
623 bool isTarget = false, bool isOpaque = false);
624
625 SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false,
626 bool IsOpaque = false) {
627 return getConstant(APInt::getAllOnes(VT.getScalarSizeInBits()), DL, VT,
628 IsTarget, IsOpaque);
629 }
630
631 SDValue getConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
632 bool isTarget = false, bool isOpaque = false);
633 SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL,
634 bool isTarget = false);
635 SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL,
636 bool LegalTypes = true);
637 SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL,
638 bool isTarget = false);
639
640 SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT,
641 bool isOpaque = false) {
642 return getConstant(Val, DL, VT, true, isOpaque);
643 }
644 SDValue getTargetConstant(const APInt &Val, const SDLoc &DL, EVT VT,
645 bool isOpaque = false) {
646 return getConstant(Val, DL, VT, true, isOpaque);
647 }
648 SDValue getTargetConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
649 bool isOpaque = false) {
650 return getConstant(Val, DL, VT, true, isOpaque);
651 }
652
653 /// Create a true or false constant of type \p VT using the target's
654 /// BooleanContent for type \p OpVT.
655 SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT);
656 /// @}
657
658 /// Create a ConstantFPSDNode wrapping a constant value.
659 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
660 ///
661 /// If only legal types can be produced, this does the necessary
662 /// transformations (e.g., if the vector element type is illegal).
663 /// The forms that take a double should only be used for simple constants
664 /// that can be exactly represented in VT. No checks are made.
665 /// @{
666 SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT,
667 bool isTarget = false);
668 SDValue getConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT,
669 bool isTarget = false);
670 SDValue getConstantFP(const ConstantFP &V, const SDLoc &DL, EVT VT,
671 bool isTarget = false);
672 SDValue getTargetConstantFP(double Val, const SDLoc &DL, EVT VT) {
673 return getConstantFP(Val, DL, VT, true);
674 }
675 SDValue getTargetConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT) {
676 return getConstantFP(Val, DL, VT, true);
677 }
678 SDValue getTargetConstantFP(const ConstantFP &Val, const SDLoc &DL, EVT VT) {
679 return getConstantFP(Val, DL, VT, true);
680 }
681 /// @}
682
683 SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
684 int64_t offset = 0, bool isTargetGA = false,
685 unsigned TargetFlags = 0);
686 SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
687 int64_t offset = 0, unsigned TargetFlags = 0) {
688 return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
689 }
690 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
691 SDValue getTargetFrameIndex(int FI, EVT VT) {
692 return getFrameIndex(FI, VT, true);
693 }
694 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
695 unsigned TargetFlags = 0);
696 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags = 0) {
697 return getJumpTable(JTI, VT, true, TargetFlags);
698 }
699 SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align = None,
700 int Offs = 0, bool isT = false,
701 unsigned TargetFlags = 0);
702 SDValue getTargetConstantPool(const Constant *C, EVT VT,
703 MaybeAlign Align = None, int Offset = 0,
704 unsigned TargetFlags = 0) {
705 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
706 }
707 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
708 MaybeAlign Align = None, int Offs = 0,
709 bool isT = false, unsigned TargetFlags = 0);
710 SDValue getTargetConstantPool(MachineConstantPoolValue *C, EVT VT,
711 MaybeAlign Align = None, int Offset = 0,
712 unsigned TargetFlags = 0) {
713 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
714 }
715 SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0,
716 unsigned TargetFlags = 0);
717 // When generating a branch to a BB, we don't in general know enough
718 // to provide debug info for the BB at that time, so keep this one around.
719 SDValue getBasicBlock(MachineBasicBlock *MBB);
720 SDValue getExternalSymbol(const char *Sym, EVT VT);
721 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
722 unsigned TargetFlags = 0);
723 SDValue getMCSymbol(MCSymbol *Sym, EVT VT);
724
725 SDValue getValueType(EVT);
726 SDValue getRegister(unsigned Reg, EVT VT);
727 SDValue getRegisterMask(const uint32_t *RegMask);
728 SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label);
729 SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root,
730 MCSymbol *Label);
731 SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset = 0,
732 bool isTarget = false, unsigned TargetFlags = 0);
733 SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT,
734 int64_t Offset = 0, unsigned TargetFlags = 0) {
735 return getBlockAddress(BA, VT, Offset, true, TargetFlags);
736 }
737
738 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg,
739 SDValue N) {
740 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
741 getRegister(Reg, N.getValueType()), N);
742 }
743
744 // This version of the getCopyToReg method takes an extra operand, which
745 // indicates that there is potentially an incoming glue value (if Glue is not
746 // null) and that there should be a glue result.
747 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N,
748 SDValue Glue) {
749 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
750 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue };
751 return getNode(ISD::CopyToReg, dl, VTs,
752 makeArrayRef(Ops, Glue.getNode() ? 4 : 3));
753 }
754
755 // Similar to last getCopyToReg() except parameter Reg is a SDValue
756 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, SDValue Reg, SDValue N,
757 SDValue Glue) {
758 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
759 SDValue Ops[] = { Chain, Reg, N, Glue };
760 return getNode(ISD::CopyToReg, dl, VTs,
761 makeArrayRef(Ops, Glue.getNode() ? 4 : 3));
762 }
763
764 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT) {
765 SDVTList VTs = getVTList(VT, MVT::Other);
766 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
767 return getNode(ISD::CopyFromReg, dl, VTs, Ops);
768 }
769
770 // This version of the getCopyFromReg method takes an extra operand, which
771 // indicates that there is potentially an incoming glue value (if Glue is not
772 // null) and that there should be a glue result.
773 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT,
774 SDValue Glue) {
775 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue);
776 SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue };
777 return getNode(ISD::CopyFromReg, dl, VTs,
778 makeArrayRef(Ops, Glue.getNode() ? 3 : 2));
779 }
780
781 SDValue getCondCode(ISD::CondCode Cond);
782
783 /// Return an ISD::VECTOR_SHUFFLE node. The number of elements in VT,
784 /// which must be a vector type, must match the number of mask elements
785 /// NumElts. An integer mask element equal to -1 is treated as undefined.
786 SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
787 ArrayRef<int> Mask);
788
789 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
790 /// which must be a vector type, must match the number of operands in Ops.
791 /// The operands must have the same type as (or, for integers, a type wider
792 /// than) VT's element type.
793 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDValue> Ops) {
794 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
795 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
796 }
797
798 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
799 /// which must be a vector type, must match the number of operands in Ops.
800 /// The operands must have the same type as (or, for integers, a type wider
801 /// than) VT's element type.
802 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDUse> Ops) {
803 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
804 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
805 }
806
807 /// Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all
808 /// elements. VT must be a vector type. Op's type must be the same as (or,
809 /// for integers, a type wider than) VT's element type.
810 SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op) {
811 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
812 if (Op.getOpcode() == ISD::UNDEF) {
813 assert((VT.getVectorElementType() == Op.getValueType() ||
814 (VT.isInteger() &&
815 VT.getVectorElementType().bitsLE(Op.getValueType()))) &&
816 "A splatted value must have a width equal or (for integers) "
817 "greater than the vector element type!");
818 return getNode(ISD::UNDEF, SDLoc(), VT);
819 }
820
821 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Op);
822 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
823 }
824
825 // Return a splat ISD::SPLAT_VECTOR node, consisting of Op splatted to all
826 // elements.
827 SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op) {
828 if (Op.getOpcode() == ISD::UNDEF) {
829 assert((VT.getVectorElementType() == Op.getValueType() ||
830 (VT.isInteger() &&
831 VT.getVectorElementType().bitsLE(Op.getValueType()))) &&
832 "A splatted value must have a width equal or (for integers) "
833 "greater than the vector element type!");
834 return getNode(ISD::UNDEF, SDLoc(), VT);
835 }
836 return getNode(ISD::SPLAT_VECTOR, DL, VT, Op);
837 }
838
839 /// Returns a vector of type ResVT whose elements contain the linear sequence
840 /// <0, Step, Step * 2, Step * 3, ...>
841 SDValue getStepVector(const SDLoc &DL, EVT ResVT, APInt StepVal);
842
843 /// Returns a vector of type ResVT whose elements contain the linear sequence
844 /// <0, 1, 2, 3, ...>
845 SDValue getStepVector(const SDLoc &DL, EVT ResVT);
846
847 /// Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to
848 /// the shuffle node in input but with swapped operands.
849 ///
850 /// Example: shuffle A, B, <0,5,2,7> -> shuffle B, A, <4,1,6,3>
851 SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV);
852
853 /// Convert Op, which must be of float type, to the
854 /// float type VT, by either extending or rounding (by truncation).
855 SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT);
856
857 /// Convert Op, which must be a STRICT operation of float type, to the
858 /// float type VT, by either extending or rounding (by truncation).
859 std::pair<SDValue, SDValue>
860 getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT);
861
862 /// Convert Op, which must be of integer type, to the
863 /// integer type VT, by either any-extending or truncating it.
864 SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
865
866 /// Convert Op, which must be of integer type, to the
867 /// integer type VT, by either sign-extending or truncating it.
868 SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
869
870 /// Convert Op, which must be of integer type, to the
871 /// integer type VT, by either zero-extending or truncating it.
872 SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
873
874 /// Return the expression required to zero extend the Op
875 /// value assuming it was the smaller SrcTy value.
876 SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT);
877
878 /// Convert Op, which must be of integer type, to the integer type VT, by
879 /// either truncating it or performing either zero or sign extension as
880 /// appropriate extension for the pointer's semantics.
881 SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
882
883 /// Return the expression required to extend the Op as a pointer value
884 /// assuming it was the smaller SrcTy value. This may be either a zero extend
885 /// or a sign extend.
886 SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT);
887
888 /// Convert Op, which must be of integer type, to the integer type VT,
889 /// by using an extension appropriate for the target's
890 /// BooleanContent for type OpVT or truncating it.
891 SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT);
892
893 /// Create a bitwise NOT operation as (XOR Val, -1).
894 SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT);
895
896 /// Create a logical NOT operation as (XOR Val, BooleanOne).
897 SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT);
898
899 /// Returns sum of the base pointer and offset.
900 /// Unlike getObjectPtrOffset this does not set NoUnsignedWrap by default.
901 SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL,
902 const SDNodeFlags Flags = SDNodeFlags());
903 SDValue getMemBasePlusOffset(SDValue Base, SDValue Offset, const SDLoc &DL,
904 const SDNodeFlags Flags = SDNodeFlags());
905
906 /// Create an add instruction with appropriate flags when used for
907 /// addressing some offset of an object. i.e. if a load is split into multiple
908 /// components, create an add nuw from the base pointer to the offset.
909 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset) {
910 SDNodeFlags Flags;
911 Flags.setNoUnsignedWrap(true);
912 return getMemBasePlusOffset(Ptr, Offset, SL, Flags);
913 }
914
915 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, SDValue Offset) {
916 // The object itself can't wrap around the address space, so it shouldn't be
917 // possible for the adds of the offsets to the split parts to overflow.
918 SDNodeFlags Flags;
919 Flags.setNoUnsignedWrap(true);
920 return getMemBasePlusOffset(Ptr, Offset, SL, Flags);
921 }
922
923 /// Return a new CALLSEQ_START node, that starts new call frame, in which
924 /// InSize bytes are set up inside CALLSEQ_START..CALLSEQ_END sequence and
925 /// OutSize specifies part of the frame set up prior to the sequence.
926 SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize,
927 const SDLoc &DL) {
928 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
929 SDValue Ops[] = { Chain,
930 getIntPtrConstant(InSize, DL, true),
931 getIntPtrConstant(OutSize, DL, true) };
932 return getNode(ISD::CALLSEQ_START, DL, VTs, Ops);
933 }
934
935 /// Return a new CALLSEQ_END node, which always must have a
936 /// glue result (to ensure it's not CSE'd).
937 /// CALLSEQ_END does not have a useful SDLoc.
938 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
939 SDValue InGlue, const SDLoc &DL) {
940 SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue);
941 SmallVector<SDValue, 4> Ops;
942 Ops.push_back(Chain);
943 Ops.push_back(Op1);
944 Ops.push_back(Op2);
945 if (InGlue.getNode())
946 Ops.push_back(InGlue);
947 return getNode(ISD::CALLSEQ_END, DL, NodeTys, Ops);
948 }
949
950 /// Return true if the result of this operation is always undefined.
951 bool isUndef(unsigned Opcode, ArrayRef<SDValue> Ops);
952
953 /// Return an UNDEF node. UNDEF does not have a useful SDLoc.
954 SDValue getUNDEF(EVT VT) {
955 return getNode(ISD::UNDEF, SDLoc(), VT);
956 }
957
958 /// Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
959 SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm) {
960 assert(MulImm.getMinSignedBits() <= VT.getSizeInBits() &&
961 "Immediate does not fit VT");
962 return getNode(ISD::VSCALE, DL, VT,
963 getConstant(MulImm.sextOrTrunc(VT.getSizeInBits()), DL, VT));
964 }
965
966 /// Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
967 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
968 return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT);
969 }
970
971 /// Gets or creates the specified node.
972 ///
973 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
974 ArrayRef<SDUse> Ops);
975 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
976 ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
977 SDValue getNode(unsigned Opcode, const SDLoc &DL, ArrayRef<EVT> ResultTys,
978 ArrayRef<SDValue> Ops);
979 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
980 ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
981
982 // Use flags from current flag inserter.
983 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
984 ArrayRef<SDValue> Ops);
985 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
986 ArrayRef<SDValue> Ops);
987 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand);
988 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
989 SDValue N2);
990 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
991 SDValue N2, SDValue N3);
992
993 // Specialize based on number of operands.
994 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT);
995 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand,
996 const SDNodeFlags Flags);
997 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
998 SDValue N2, const SDNodeFlags Flags);
999 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
1000 SDValue N2, SDValue N3, const SDNodeFlags Flags);
1001 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
1002 SDValue N2, SDValue N3, SDValue N4);
1003 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
1004 SDValue N2, SDValue N3, SDValue N4, SDValue N5);
1005
1006 // Specialize again based on number of operands for nodes with a VTList
1007 // rather than a single VT.
1008 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList);
1009 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N);
1010 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
1011 SDValue N2);
1012 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
1013 SDValue N2, SDValue N3);
1014 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
1015 SDValue N2, SDValue N3, SDValue N4);
1016 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
1017 SDValue N2, SDValue N3, SDValue N4, SDValue N5);
1018
1019 /// Compute a TokenFactor to force all the incoming stack arguments to be
1020 /// loaded from the stack. This is used in tail call lowering to protect
1021 /// stack arguments from being clobbered.
1022 SDValue getStackArgumentTokenFactor(SDValue Chain);
1023
1024 SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
1025 SDValue Size, Align Alignment, bool isVol,
1026 bool AlwaysInline, bool isTailCall,
1027 MachinePointerInfo DstPtrInfo,
1028 MachinePointerInfo SrcPtrInfo,
1029 const AAMDNodes &AAInfo = AAMDNodes());
1030
1031 SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
1032 SDValue Size, Align Alignment, bool isVol, bool isTailCall,
1033 MachinePointerInfo DstPtrInfo,
1034 MachinePointerInfo SrcPtrInfo,
1035 const AAMDNodes &AAInfo = AAMDNodes());
1036
1037 SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
1038 SDValue Size, Align Alignment, bool isVol, bool isTailCall,
1039 MachinePointerInfo DstPtrInfo,
1040 const AAMDNodes &AAInfo = AAMDNodes());
1041
1042 SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst,
1043 unsigned DstAlign, SDValue Src, unsigned SrcAlign,
1044 SDValue Size, Type *SizeTy, unsigned ElemSz,
1045 bool isTailCall, MachinePointerInfo DstPtrInfo,
1046 MachinePointerInfo SrcPtrInfo);
1047
1048 SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
1049 unsigned DstAlign, SDValue Src, unsigned SrcAlign,
1050 SDValue Size, Type *SizeTy, unsigned ElemSz,
1051 bool isTailCall, MachinePointerInfo DstPtrInfo,
1052 MachinePointerInfo SrcPtrInfo);
1053
1054 SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst,
1055 unsigned DstAlign, SDValue Value, SDValue Size,
1056 Type *SizeTy, unsigned ElemSz, bool isTailCall,
1057 MachinePointerInfo DstPtrInfo);
1058
1059 /// Helper function to make it easier to build SetCC's if you just have an
1060 /// ISD::CondCode instead of an SDValue.
1061 SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS,
1062 ISD::CondCode Cond, SDValue Chain = SDValue(),
1063 bool IsSignaling = false) {
1064 assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
1065 "Cannot compare scalars to vectors");
1066 assert(LHS.getValueType().isVector() == VT.isVector() &&
1067 "Cannot compare scalars to vectors");
1068 assert(Cond != ISD::SETCC_INVALID &&
1069 "Cannot create a setCC of an invalid node.");
1070 if (Chain)
1071 return getNode(IsSignaling ? ISD::STRICT_FSETCCS : ISD::STRICT_FSETCC, DL,
1072 {VT, MVT::Other}, {Chain, LHS, RHS, getCondCode(Cond)});
1073 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
1074 }
1075
1076 /// Helper function to make it easier to build Select's if you just have
1077 /// operands and don't want to check for vector.
1078 SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS,
1079 SDValue RHS) {
1080 assert(LHS.getValueType() == RHS.getValueType() &&
1081 "Cannot use select on differing types");
1082 assert(VT.isVector() == LHS.getValueType().isVector() &&
1083 "Cannot mix vectors and scalars");
1084 auto Opcode = Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
1085 return getNode(Opcode, DL, VT, Cond, LHS, RHS);
1086 }
1087
1088 /// Helper function to make it easier to build SelectCC's if you just have an
1089 /// ISD::CondCode instead of an SDValue.
1090 SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True,
1091 SDValue False, ISD::CondCode Cond) {
1092 return getNode(ISD::SELECT_CC, DL, True.getValueType(), LHS, RHS, True,
1093 False, getCondCode(Cond));
1094 }
1095
1096 /// Try to simplify a select/vselect into 1 of its operands or a constant.
1097 SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal);
1098
1099 /// Try to simplify a shift into 1 of its operands or a constant.
1100 SDValue simplifyShift(SDValue X, SDValue Y);
1101
1102 /// Try to simplify a floating-point binary operation into 1 of its operands
1103 /// or a constant.
1104 SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y,
1105 SDNodeFlags Flags);
1106
1107 /// VAArg produces a result and token chain, and takes a pointer
1108 /// and a source value as input.
1109 SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1110 SDValue SV, unsigned Align);
1111
1112 /// Gets a node for an atomic cmpxchg op. There are two
1113 /// valid Opcodes. ISD::ATOMIC_CMO_SWAP produces the value loaded and a
1114 /// chain result. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS produces the value loaded,
1115 /// a success flag (initially i1), and a chain.
1116 SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT,
1117 SDVTList VTs, SDValue Chain, SDValue Ptr,
1118 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO);
1119
1120 /// Gets a node for an atomic op, produces result (if relevant)
1121 /// and chain and takes 2 operands.
1122 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain,
1123 SDValue Ptr, SDValue Val, MachineMemOperand *MMO);
1124
1125 /// Gets a node for an atomic op, produces result and chain and
1126 /// takes 1 operand.
1127 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, EVT VT,
1128 SDValue Chain, SDValue Ptr, MachineMemOperand *MMO);
1129
1130 /// Gets a node for an atomic op, produces result and chain and takes N
1131 /// operands.
1132 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
1133 SDVTList VTList, ArrayRef<SDValue> Ops,
1134 MachineMemOperand *MMO);
1135
1136 /// Creates a MemIntrinsicNode that may produce a
1137 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
1138 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
1139 /// less than FIRST_TARGET_MEMORY_OPCODE.
1140 SDValue getMemIntrinsicNode(
1141 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
1142 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
1143 MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad |
1144 MachineMemOperand::MOStore,
1145 uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes());
1146
1147 inline SDValue getMemIntrinsicNode(
1148 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
1149 EVT MemVT, MachinePointerInfo PtrInfo, MaybeAlign Alignment = None,
1150 MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad |
1151 MachineMemOperand::MOStore,
1152 uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes()) {
1153 // Ensure that codegen never sees alignment 0
1154 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, PtrInfo,
1155 Alignment.getValueOr(getEVTAlign(MemVT)), Flags,
1156 Size, AAInfo);
1157 }
1158
1159 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList,
1160 ArrayRef<SDValue> Ops, EVT MemVT,
1161 MachineMemOperand *MMO);
1162
1163 /// Creates a LifetimeSDNode that starts (`IsStart==true`) or ends
1164 /// (`IsStart==false`) the lifetime of the portion of `FrameIndex` between
1165 /// offsets `Offset` and `Offset + Size`.
1166 SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain,
1167 int FrameIndex, int64_t Size, int64_t Offset = -1);
1168
1169 /// Creates a PseudoProbeSDNode with function GUID `Guid` and
1170 /// the index of the block `Index` it is probing, as well as the attributes
1171 /// `attr` of the probe.
1172 SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid,
1173 uint64_t Index, uint32_t Attr);
1174
1175 /// Create a MERGE_VALUES node from the given operands.
1176 SDValue getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl);
1177
1178 /// Loads are not normal binary operators: their result type is not
1179 /// determined by their operands, and they produce a value AND a token chain.
1180 ///
1181 /// This function will set the MOLoad flag on MMOFlags, but you can set it if
1182 /// you want. The MOStore flag must not be set.
1183 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1184 MachinePointerInfo PtrInfo,
1185 MaybeAlign Alignment = MaybeAlign(),
1186 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1187 const AAMDNodes &AAInfo = AAMDNodes(),
1188 const MDNode *Ranges = nullptr);
1189 /// FIXME: Remove once transition to Align is over.
1190 inline SDValue
1191 getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1192 MachinePointerInfo PtrInfo, unsigned Alignment,
1193 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1194 const AAMDNodes &AAInfo = AAMDNodes(),
1195 const MDNode *Ranges = nullptr) {
1196 return getLoad(VT, dl, Chain, Ptr, PtrInfo, MaybeAlign(Alignment), MMOFlags,
1197 AAInfo, Ranges);
1198 }
1199 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1200 MachineMemOperand *MMO);
1201 SDValue
1202 getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain,
1203 SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT,
1204 MaybeAlign Alignment = MaybeAlign(),
1205 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1206 const AAMDNodes &AAInfo = AAMDNodes());
1207 /// FIXME: Remove once transition to Align is over.
1208 inline SDValue
1209 getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain,
1210 SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT,
1211 unsigned Alignment,
1212 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1213 const AAMDNodes &AAInfo = AAMDNodes()) {
1214 return getExtLoad(ExtType, dl, VT, Chain, Ptr, PtrInfo, MemVT,
1215 MaybeAlign(Alignment), MMOFlags, AAInfo);
1216 }
1217 SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
1218 SDValue Chain, SDValue Ptr, EVT MemVT,
1219 MachineMemOperand *MMO);
1220 SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
1221 SDValue Offset, ISD::MemIndexedMode AM);
1222 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1223 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1224 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
1225 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1226 const AAMDNodes &AAInfo = AAMDNodes(),
1227 const MDNode *Ranges = nullptr);
1228 inline SDValue getLoad(
1229 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
1230 SDValue Chain, SDValue Ptr, SDValue Offset, MachinePointerInfo PtrInfo,
1231 EVT MemVT, MaybeAlign Alignment = MaybeAlign(),
1232 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1233 const AAMDNodes &AAInfo = AAMDNodes(), const MDNode *Ranges = nullptr) {
1234 // Ensures that codegen never sees a None Alignment.
1235 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT,
1236 Alignment.getValueOr(getEVTAlign(MemVT)), MMOFlags, AAInfo,
1237 Ranges);
1238 }
1239 /// FIXME: Remove once transition to Align is over.
1240 inline SDValue
1241 getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1242 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1243 MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment,
1244 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1245 const AAMDNodes &AAInfo = AAMDNodes(),
1246 const MDNode *Ranges = nullptr) {
1247 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT,
1248 MaybeAlign(Alignment), MMOFlags, AAInfo, Ranges);
1249 }
1250 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1251 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1252 EVT MemVT, MachineMemOperand *MMO);
1253
1254 /// Helper function to build ISD::STORE nodes.
1255 ///
1256 /// This function will set the MOStore flag on MMOFlags, but you can set it if
1257 /// you want. The MOLoad and MOInvariant flags must not be set.
1258
1259 SDValue
1260 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1261 MachinePointerInfo PtrInfo, Align Alignment,
1262 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1263 const AAMDNodes &AAInfo = AAMDNodes());
1264 inline SDValue
1265 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1266 MachinePointerInfo PtrInfo, MaybeAlign Alignment = MaybeAlign(),
1267 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1268 const AAMDNodes &AAInfo = AAMDNodes()) {
1269 return getStore(Chain, dl, Val, Ptr, PtrInfo,
1270 Alignment.getValueOr(getEVTAlign(Val.getValueType())),
1271 MMOFlags, AAInfo);
1272 }
1273 /// FIXME: Remove once transition to Align is over.
1274 inline SDValue
1275 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1276 MachinePointerInfo PtrInfo, unsigned Alignment,
1277 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1278 const AAMDNodes &AAInfo = AAMDNodes()) {
1279 return getStore(Chain, dl, Val, Ptr, PtrInfo, MaybeAlign(Alignment),
1280 MMOFlags, AAInfo);
1281 }
1282 SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1283 MachineMemOperand *MMO);
1284 SDValue
1285 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1286 MachinePointerInfo PtrInfo, EVT SVT, Align Alignment,
1287 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1288 const AAMDNodes &AAInfo = AAMDNodes());
1289 inline SDValue
1290 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1291 MachinePointerInfo PtrInfo, EVT SVT,
1292 MaybeAlign Alignment = MaybeAlign(),
1293 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1294 const AAMDNodes &AAInfo = AAMDNodes()) {
1295 return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT,
1296 Alignment.getValueOr(getEVTAlign(SVT)), MMOFlags,
1297 AAInfo);
1298 }
1299 /// FIXME: Remove once transition to Align is over.
1300 inline SDValue
1301 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1302 MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment,
1303 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1304 const AAMDNodes &AAInfo = AAMDNodes()) {
1305 return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT,
1306 MaybeAlign(Alignment), MMOFlags, AAInfo);
1307 }
1308 SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
1309 SDValue Ptr, EVT SVT, MachineMemOperand *MMO);
1310 SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base,
1311 SDValue Offset, ISD::MemIndexedMode AM);
1312
1313 SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1314 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1315 SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo,
1316 EVT MemVT, Align Alignment,
1317 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
1318 const MDNode *Ranges = nullptr, bool IsExpanding = false);
1319 inline SDValue
1320 getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1321 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1322 SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT,
1323 MaybeAlign Alignment = MaybeAlign(),
1324 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1325 const AAMDNodes &AAInfo = AAMDNodes(),
1326 const MDNode *Ranges = nullptr, bool IsExpanding = false) {
1327 // Ensures that codegen never sees a None Alignment.
1328 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL,
1329 PtrInfo, MemVT, Alignment.getValueOr(getEVTAlign(MemVT)),
1330 MMOFlags, AAInfo, Ranges, IsExpanding);
1331 }
1332 SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1333 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1334 SDValue Mask, SDValue EVL, EVT MemVT,
1335 MachineMemOperand *MMO, bool IsExpanding = false);
1336 SDValue getLoadVP(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1337 SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo,
1338 MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags,
1339 const AAMDNodes &AAInfo, const MDNode *Ranges = nullptr,
1340 bool IsExpanding = false);
1341 SDValue getLoadVP(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1342 SDValue Mask, SDValue EVL, MachineMemOperand *MMO,
1343 bool IsExpanding = false);
1344 SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
1345 SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL,
1346 MachinePointerInfo PtrInfo, EVT MemVT,
1347 MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags,
1348 const AAMDNodes &AAInfo, bool IsExpanding = false);
1349 SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
1350 SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL,
1351 EVT MemVT, MachineMemOperand *MMO,
1352 bool IsExpanding = false);
1353 SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
1354 SDValue Offset, ISD::MemIndexedMode AM);
1355 SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1356 SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT,
1357 MachineMemOperand *MMO, ISD::MemIndexedMode AM,
1358 bool IsTruncating = false, bool IsCompressing = false);
1359 SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val,
1360 SDValue Ptr, SDValue Mask, SDValue EVL,
1361 MachinePointerInfo PtrInfo, EVT SVT, Align Alignment,
1362 MachineMemOperand::Flags MMOFlags,
1363 const AAMDNodes &AAInfo, bool IsCompressing = false);
1364 SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val,
1365 SDValue Ptr, SDValue Mask, SDValue EVL, EVT SVT,
1366 MachineMemOperand *MMO, bool IsCompressing = false);
1367 SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base,
1368 SDValue Offset, ISD::MemIndexedMode AM);
1369
1370 SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl,
1371 ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
1372 ISD::MemIndexType IndexType);
1373 SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl,
1374 ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
1375 ISD::MemIndexType IndexType);
1376
1377 SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base,
1378 SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT,
1379 MachineMemOperand *MMO, ISD::MemIndexedMode AM,
1380 ISD::LoadExtType, bool IsExpanding = false);
1381 SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
1382 SDValue Offset, ISD::MemIndexedMode AM);
1383 SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val,
1384 SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT,
1385 MachineMemOperand *MMO, ISD::MemIndexedMode AM,
1386 bool IsTruncating = false, bool IsCompressing = false);
1387 SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl,
1388 SDValue Base, SDValue Offset,
1389 ISD::MemIndexedMode AM);
1390 SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl,
1391 ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
1392 ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy);
1393 SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl,
1394 ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
1395 ISD::MemIndexType IndexType,
1396 bool IsTruncating = false);
1397
1398 /// Construct a node to track a Value* through the backend.
1399 SDValue getSrcValue(const Value *v);
1400
1401 /// Return an MDNodeSDNode which holds an MDNode.
1402 SDValue getMDNode(const MDNode *MD);
1403
1404 /// Return a bitcast using the SDLoc of the value operand, and casting to the
1405 /// provided type. Use getNode to set a custom SDLoc.
1406 SDValue getBitcast(EVT VT, SDValue V);
1407
1408 /// Return an AddrSpaceCastSDNode.
1409 SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS,
1410 unsigned DestAS);
1411
1412 /// Return a freeze using the SDLoc of the value operand.
1413 SDValue getFreeze(SDValue V);
1414
1415 /// Return an AssertAlignSDNode.
1416 SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A);
1417
1418 /// Return the specified value casted to
1419 /// the target's desired shift amount type.
1420 SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);
1421
1422 /// Expand the specified \c ISD::VAARG node as the Legalize pass would.
1423 SDValue expandVAArg(SDNode *Node);
1424
1425 /// Expand the specified \c ISD::VACOPY node as the Legalize pass would.
1426 SDValue expandVACopy(SDNode *Node);
1427
1428 /// Returs an GlobalAddress of the function from the current module with
1429 /// name matching the given ExternalSymbol. Additionally can provide the
1430 /// matched function.
1431 /// Panics the function doesn't exists.
1432 SDValue getSymbolFunctionGlobalAddress(SDValue Op,
1433 Function **TargetFunction = nullptr);
1434
1435 /// *Mutate* the specified node in-place to have the
1436 /// specified operands. If the resultant node already exists in the DAG,
1437 /// this does not modify the specified node, instead it returns the node that
1438 /// already exists. If the resultant node does not exist in the DAG, the
1439 /// input node is returned. As a degenerate case, if you specify the same
1440 /// input operands as the node already has, the input node is returned.
1441 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op);
1442 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2);
1443 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1444 SDValue Op3);
1445 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1446 SDValue Op3, SDValue Op4);
1447 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1448 SDValue Op3, SDValue Op4, SDValue Op5);
1449 SDNode *UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops);
1450
1451 /// Creates a new TokenFactor containing \p Vals. If \p Vals contains 64k
1452 /// values or more, move values into new TokenFactors in 64k-1 blocks, until
1453 /// the final TokenFactor has less than 64k operands.
1454 SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl<SDValue> &Vals);
1455
1456 /// *Mutate* the specified machine node's memory references to the provided
1457 /// list.
1458 void setNodeMemRefs(MachineSDNode *N,
1459 ArrayRef<MachineMemOperand *> NewMemRefs);
1460
1461 // Calculate divergence of node \p N based on its operands.
1462 bool calculateDivergence(SDNode *N);
1463
1464 // Propagates the change in divergence to users
1465 void updateDivergence(SDNode * N);
1466
1467 /// These are used for target selectors to *mutate* the
1468 /// specified node to have the specified return type, Target opcode, and
1469 /// operands. Note that target opcodes are stored as
1470 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
1471 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT);
1472 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, SDValue Op1);
1473 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
1474 SDValue Op1, SDValue Op2);
1475 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
1476 SDValue Op1, SDValue Op2, SDValue Op3);
1477 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
1478 ArrayRef<SDValue> Ops);
1479 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, EVT VT2);
1480 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
1481 EVT VT2, ArrayRef<SDValue> Ops);
1482 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
1483 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
1484 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
1485 EVT VT2, SDValue Op1, SDValue Op2);
1486 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, SDVTList VTs,
1487 ArrayRef<SDValue> Ops);
1488
1489 /// This *mutates* the specified node to have the specified
1490 /// return type, opcode, and operands.
1491 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
1492 ArrayRef<SDValue> Ops);
1493
1494 /// Mutate the specified strict FP node to its non-strict equivalent,
1495 /// unlinking the node from its chain and dropping the metadata arguments.
1496 /// The node must be a strict FP node.
1497 SDNode *mutateStrictFPToFP(SDNode *Node);
1498
1499 /// These are used for target selectors to create a new node
1500 /// with specified return type(s), MachineInstr opcode, and operands.
1501 ///
1502 /// Note that getMachineNode returns the resultant node. If there is already
1503 /// a node of the specified opcode and operands, it returns that node instead
1504 /// of the current one.
1505 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT);
1506 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1507 SDValue Op1);
1508 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1509 SDValue Op1, SDValue Op2);
1510 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1511 SDValue Op1, SDValue Op2, SDValue Op3);
1512 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1513 ArrayRef<SDValue> Ops);
1514 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1515 EVT VT2, SDValue Op1, SDValue Op2);
1516 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1517 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
1518 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1519 EVT VT2, ArrayRef<SDValue> Ops);
1520 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1521 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2);
1522 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1523 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2,
1524 SDValue Op3);
1525 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1526 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
1527 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl,
1528 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops);
1529 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, SDVTList VTs,
1530 ArrayRef<SDValue> Ops);
1531
1532 /// A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
1533 SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
1534 SDValue Operand);
1535
1536 /// A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
1537 SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
1538 SDValue Operand, SDValue Subreg);
1539
1540 /// Get the specified node if it's already available, or else return NULL.
1541 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList,
1542 ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
1543 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList,
1544 ArrayRef<SDValue> Ops);
1545
1546 /// Check if a node exists without modifying its flags.
1547 bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef<SDValue> Ops);
1548
1549 /// Creates a SDDbgValue node.
1550 SDDbgValue *getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N,
1551 unsigned R, bool IsIndirect, const DebugLoc &DL,
1552 unsigned O);
1553
1554 /// Creates a constant SDDbgValue node.
1555 SDDbgValue *getConstantDbgValue(DIVariable *Var, DIExpression *Expr,
1556 const Value *C, const DebugLoc &DL,
1557 unsigned O);
1558
1559 /// Creates a FrameIndex SDDbgValue node.
1560 SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr,
1561 unsigned FI, bool IsIndirect,
1562 const DebugLoc &DL, unsigned O);
1563
1564 /// Creates a FrameIndex SDDbgValue node.
1565 SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr,
1566 unsigned FI,
1567 ArrayRef<SDNode *> Dependencies,
1568 bool IsIndirect, const DebugLoc &DL,
1569 unsigned O);
1570
1571 /// Creates a VReg SDDbgValue node.
1572 SDDbgValue *getVRegDbgValue(DIVariable *Var, DIExpression *Expr,
1573 unsigned VReg, bool IsIndirect,
1574 const DebugLoc &DL, unsigned O);
1575
1576 /// Creates a SDDbgValue node from a list of locations.
1577 SDDbgValue *getDbgValueList(DIVariable *Var, DIExpression *Expr,
1578 ArrayRef<SDDbgOperand> Locs,
1579 ArrayRef<SDNode *> Dependencies, bool IsIndirect,
1580 const DebugLoc &DL, unsigned O, bool IsVariadic);
1581
1582 /// Creates a SDDbgLabel node.
1583 SDDbgLabel *getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O);
1584
1585 /// Transfer debug values from one node to another, while optionally
1586 /// generating fragment expressions for split-up values. If \p InvalidateDbg
1587 /// is set, debug values are invalidated after they are transferred.
1588 void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits = 0,
1589 unsigned SizeInBits = 0, bool InvalidateDbg = true);
1590
1591 /// Remove the specified node from the system. If any of its
1592 /// operands then becomes dead, remove them as well. Inform UpdateListener
1593 /// for each node deleted.
1594 void RemoveDeadNode(SDNode *N);
1595
1596 /// This method deletes the unreachable nodes in the
1597 /// given list, and any nodes that become unreachable as a result.
1598 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes);
1599
1600 /// Modify anything using 'From' to use 'To' instead.
1601 /// This can cause recursive merging of nodes in the DAG. Use the first
1602 /// version if 'From' is known to have a single result, use the second
1603 /// if you have two nodes with identical results (or if 'To' has a superset
1604 /// of the results of 'From'), use the third otherwise.
1605 ///
1606 /// These methods all take an optional UpdateListener, which (if not null) is
1607 /// informed about nodes that are deleted and modified due to recursive
1608 /// changes in the dag.
1609 ///
1610 /// These functions only replace all existing uses. It's possible that as
1611 /// these replacements are being performed, CSE may cause the From node
1612 /// to be given new uses. These new uses of From are left in place, and
1613 /// not automatically transferred to To.
1614 ///
1615 void ReplaceAllUsesWith(SDValue From, SDValue To);
1616 void ReplaceAllUsesWith(SDNode *From, SDNode *To);
1617 void ReplaceAllUsesWith(SDNode *From, const SDValue *To);
1618
1619 /// Replace any uses of From with To, leaving
1620 /// uses of other values produced by From.getNode() alone.
1621 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To);
1622
1623 /// Like ReplaceAllUsesOfValueWith, but for multiple values at once.
1624 /// This correctly handles the case where
1625 /// there is an overlap between the From values and the To values.
1626 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
1627 unsigned Num);
1628
1629 /// If an existing load has uses of its chain, create a token factor node with
1630 /// that chain and the new memory node's chain and update users of the old
1631 /// chain to the token factor. This ensures that the new memory node will have
1632 /// the same relative memory dependency position as the old load. Returns the
1633 /// new merged load chain.
1634 SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain);
1635
1636 /// If an existing load has uses of its chain, create a token factor node with
1637 /// that chain and the new memory node's chain and update users of the old
1638 /// chain to the token factor. This ensures that the new memory node will have
1639 /// the same relative memory dependency position as the old load. Returns the
1640 /// new merged load chain.
1641 SDValue makeEquivalentMemoryOrdering(LoadSDNode *OldLoad, SDValue NewMemOp);
1642
1643 /// Topological-sort the AllNodes list and a
1644 /// assign a unique node id for each node in the DAG based on their
1645 /// topological order. Returns the number of nodes.
1646 unsigned AssignTopologicalOrder();
1647
1648 /// Move node N in the AllNodes list to be immediately
1649 /// before the given iterator Position. This may be used to update the
1650 /// topological ordering when the list of nodes is modified.
1651 void RepositionNode(allnodes_iterator Position, SDNode *N) {
1652 AllNodes.insert(Position, AllNodes.remove(N));
1653 }
1654
1655 /// Returns an APFloat semantics tag appropriate for the given type. If VT is
1656 /// a vector type, the element semantics are returned.
1657 static const fltSemantics &EVTToAPFloatSemantics(EVT VT) {
1658 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
1659 default: llvm_unreachable("Unknown FP format");
1660 case MVT::f16: return APFloat::IEEEhalf();
1661 case MVT::bf16: return APFloat::BFloat();
1662 case MVT::f32: return APFloat::IEEEsingle();
1663 case MVT::f64: return APFloat::IEEEdouble();
1664 case MVT::f80: return APFloat::x87DoubleExtended();
1665 case MVT::f128: return APFloat::IEEEquad();
1666 case MVT::ppcf128: return APFloat::PPCDoubleDouble();
1667 }
1668 }
1669
1670 /// Add a dbg_value SDNode. If SD is non-null that means the
1671 /// value is produced by SD.
1672 void AddDbgValue(SDDbgValue *DB, bool isParameter);
1673
1674 /// Add a dbg_label SDNode.
1675 void AddDbgLabel(SDDbgLabel *DB);
1676
1677 /// Get the debug values which reference the given SDNode.
1678 ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) const {
1679 return DbgInfo->getSDDbgValues(SD);
1680 }
1681
1682 public:
1683 /// Return true if there are any SDDbgValue nodes associated
1684 /// with this SelectionDAG.
1685 bool hasDebugValues() const { return !DbgInfo->empty(); }
1686
1687 SDDbgInfo::DbgIterator DbgBegin() const { return DbgInfo->DbgBegin(); }
1688 SDDbgInfo::DbgIterator DbgEnd() const { return DbgInfo->DbgEnd(); }
1689
1690 SDDbgInfo::DbgIterator ByvalParmDbgBegin() const {
1691 return DbgInfo->ByvalParmDbgBegin();
1692 }
1693 SDDbgInfo::DbgIterator ByvalParmDbgEnd() const {
1694 return DbgInfo->ByvalParmDbgEnd();
1695 }
1696
1697 SDDbgInfo::DbgLabelIterator DbgLabelBegin() const {
1698 return DbgInfo->DbgLabelBegin();
1699 }
1700 SDDbgInfo::DbgLabelIterator DbgLabelEnd() const {
1701 return DbgInfo->DbgLabelEnd();
1702 }
1703
1704 /// To be invoked on an SDNode that is slated to be erased. This
1705 /// function mirrors \c llvm::salvageDebugInfo.
1706 void salvageDebugInfo(SDNode &N);
1707
1708 /// Signal whether instruction referencing variable locations are desired for
1709 /// this function's debug-info.
1710 void useInstrRefDebugInfo(bool Flag) {
1711 UseInstrRefDebugInfo = Flag;
1712 }
1713
1714 bool getUseInstrRefDebugInfo() const {
1715 return UseInstrRefDebugInfo;
1716 }
1717
1718 void dump() const;
1719
1720 /// In most cases this function returns the ABI alignment for a given type,
1721 /// except for illegal vector types where the alignment exceeds that of the
1722 /// stack. In such cases we attempt to break the vector down to a legal type
1723 /// and return the ABI alignment for that instead.
1724 Align getReducedAlign(EVT VT, bool UseABI);
1725
1726 /// Create a stack temporary based on the size in bytes and the alignment
1727 SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment);
1728
1729 /// Create a stack temporary, suitable for holding the specified value type.
1730 /// If minAlign is specified, the slot size will have at least that alignment.
1731 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
1732
1733 /// Create a stack temporary suitable for holding either of the specified
1734 /// value types.
1735 SDValue CreateStackTemporary(EVT VT1, EVT VT2);
1736
1737 SDValue FoldSymbolOffset(unsigned Opcode, EVT VT,
1738 const GlobalAddressSDNode *GA,
1739 const SDNode *N2);
1740
1741 SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT,
1742 ArrayRef<SDValue> Ops);
1743
1744 /// Fold floating-point operations with 2 operands when both operands are
1745 /// constants and/or undefined.
1746 SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT,
1747 SDValue N1, SDValue N2);
1748
1749 /// Constant fold a setcc to true or false.
1750 SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond,
1751 const SDLoc &dl);
1752
1753 /// See if the specified operand can be simplified with the knowledge that
1754 /// only the bits specified by DemandedBits are used. If so, return the
1755 /// simpler operand, otherwise return a null SDValue.
1756 ///
1757 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can
1758 /// simplify nodes with multiple uses more aggressively.)
1759 SDValue GetDemandedBits(SDValue V, const APInt &DemandedBits);
1760
1761 /// See if the specified operand can be simplified with the knowledge that
1762 /// only the bits specified by DemandedBits are used in the elements specified
1763 /// by DemandedElts. If so, return the simpler operand, otherwise return a
1764 /// null SDValue.
1765 ///
1766 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can
1767 /// simplify nodes with multiple uses more aggressively.)
1768 SDValue GetDemandedBits(SDValue V, const APInt &DemandedBits,
1769 const APInt &DemandedElts);
1770
1771 /// Return true if the sign bit of Op is known to be zero.
1772 /// We use this predicate to simplify operations downstream.
1773 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
1774
1775 /// Return true if 'Op & Mask' is known to be zero. We
1776 /// use this predicate to simplify operations downstream. Op and Mask are
1777 /// known to be the same type.
1778 bool MaskedValueIsZero(SDValue Op, const APInt &Mask,
1779 unsigned Depth = 0) const;
1780
1781 /// Return true if 'Op & Mask' is known to be zero in DemandedElts. We
1782 /// use this predicate to simplify operations downstream. Op and Mask are
1783 /// known to be the same type.
1784 bool MaskedValueIsZero(SDValue Op, const APInt &Mask,
1785 const APInt &DemandedElts, unsigned Depth = 0) const;
1786
1787 /// Return true if '(Op & Mask) == Mask'.
1788 /// Op and Mask are known to be the same type.
1789 bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask,
1790 unsigned Depth = 0) const;
1791
1792 /// Determine which bits of Op are known to be either zero or one and return
1793 /// them in Known. For vectors, the known bits are those that are shared by
1794 /// every vector element.
1795 /// Targets can implement the computeKnownBitsForTargetNode method in the
1796 /// TargetLowering class to allow target nodes to be understood.
1797 KnownBits computeKnownBits(SDValue Op, unsigned Depth = 0) const;
1798
1799 /// Determine which bits of Op are known to be either zero or one and return
1800 /// them in Known. The DemandedElts argument allows us to only collect the
1801 /// known bits that are shared by the requested vector elements.
1802 /// Targets can implement the computeKnownBitsForTargetNode method in the
1803 /// TargetLowering class to allow target nodes to be understood.
1804 KnownBits computeKnownBits(SDValue Op, const APInt &DemandedElts,
1805 unsigned Depth = 0) const;
1806
1807 /// Used to represent the possible overflow behavior of an operation.
1808 /// Never: the operation cannot overflow.
1809 /// Always: the operation will always overflow.
1810 /// Sometime: the operation may or may not overflow.
1811 enum OverflowKind {
1812 OFK_Never,
1813 OFK_Sometime,
1814 OFK_Always,
1815 };
1816
1817 /// Determine if the result of the addition of 2 node can overflow.
1818 OverflowKind computeOverflowKind(SDValue N0, SDValue N1) const;
1819
1820 /// Test if the given value is known to have exactly one bit set. This differs
1821 /// from computeKnownBits in that it doesn't necessarily determine which bit
1822 /// is set.
1823 bool isKnownToBeAPowerOfTwo(SDValue Val) const;
1824
1825 /// Return the number of times the sign bit of the register is replicated into
1826 /// the other bits. We know that at least 1 bit is always equal to the sign
1827 /// bit (itself), but other cases can give us information. For example,
1828 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
1829 /// to each other, so we return 3. Targets can implement the
1830 /// ComputeNumSignBitsForTarget method in the TargetLowering class to allow
1831 /// target nodes to be understood.
1832 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
1833
1834 /// Return the number of times the sign bit of the register is replicated into
1835 /// the other bits. We know that at least 1 bit is always equal to the sign
1836 /// bit (itself), but other cases can give us information. For example,
1837 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
1838 /// to each other, so we return 3. The DemandedElts argument allows
1839 /// us to only collect the minimum sign bits of the requested vector elements.
1840 /// Targets can implement the ComputeNumSignBitsForTarget method in the
1841 /// TargetLowering class to allow target nodes to be understood.
1842 unsigned ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
1843 unsigned Depth = 0) const;
1844
1845 /// Get the upper bound on bit size for this Value \p Op as a signed integer.
1846 /// i.e. x == sext(trunc(x to MaxSignedBits) to bitwidth(x)).
1847 /// Similar to the APInt::getSignificantBits function.
1848 /// Helper wrapper to ComputeNumSignBits.
1849 unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth = 0) const;
1850
1851 /// Get the upper bound on bit size for this Value \p Op as a signed integer.
1852 /// i.e. x == sext(trunc(x to MaxSignedBits) to bitwidth(x)).
1853 /// Similar to the APInt::getSignificantBits function.
1854 /// Helper wrapper to ComputeNumSignBits.
1855 unsigned ComputeMaxSignificantBits(SDValue Op, const APInt &DemandedElts,
1856 unsigned Depth = 0) const;
1857
1858 /// Return true if this function can prove that \p Op is never poison
1859 /// and, if \p PoisonOnly is false, does not have undef bits.
1860 bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly = false,
1861 unsigned Depth = 0) const;
1862
1863 /// Return true if this function can prove that \p Op is never poison
1864 /// and, if \p PoisonOnly is false, does not have undef bits. The DemandedElts
1865 /// argument limits the check to the requested vector elements.
1866 bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, const APInt &DemandedElts,
1867 bool PoisonOnly = false,
1868 unsigned Depth = 0) const;
1869
1870 /// Return true if this function can prove that \p Op is never poison.
1871 bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth = 0) const {
1872 return isGuaranteedNotToBeUndefOrPoison(Op, /*PoisonOnly*/ true, Depth);
1873 }
1874
1875 /// Return true if this function can prove that \p Op is never poison. The
1876 /// DemandedElts argument limits the check to the requested vector elements.
1877 bool isGuaranteedNotToBePoison(SDValue Op, const APInt &DemandedElts,
1878 unsigned Depth = 0) const {
1879 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts,
1880 /*PoisonOnly*/ true, Depth);
1881 }
1882
1883 /// Return true if the specified operand is an ISD::ADD with a ConstantSDNode
1884 /// on the right-hand side, or if it is an ISD::OR with a ConstantSDNode that
1885 /// is guaranteed to have the same semantics as an ADD. This handles the
1886 /// equivalence:
1887 /// X|Cst == X+Cst iff X&Cst = 0.
1888 bool isBaseWithConstantOffset(SDValue Op) const;
1889
1890 /// Test whether the given SDValue is known to never be NaN. If \p SNaN is
1891 /// true, returns if \p Op is known to never be a signaling NaN (it may still
1892 /// be a qNaN).
1893 bool isKnownNeverNaN(SDValue Op, bool SNaN = false, unsigned Depth = 0) const;
1894
1895 /// \returns true if \p Op is known to never be a signaling NaN.
1896 bool isKnownNeverSNaN(SDValue Op, unsigned Depth = 0) const {
1897 return isKnownNeverNaN(Op, true, Depth);
1898 }
1899
1900 /// Test whether the given floating point SDValue is known to never be
1901 /// positive or negative zero.
1902 bool isKnownNeverZeroFloat(SDValue Op) const;
1903
1904 /// Test whether the given SDValue is known to contain non-zero value(s).
1905 bool isKnownNeverZero(SDValue Op) const;
1906
1907 /// Test whether two SDValues are known to compare equal. This
1908 /// is true if they are the same value, or if one is negative zero and the
1909 /// other positive zero.
1910 bool isEqualTo(SDValue A, SDValue B) const;
1911
1912 /// Return true if A and B have no common bits set. As an example, this can
1913 /// allow an 'add' to be transformed into an 'or'.
1914 bool haveNoCommonBitsSet(SDValue A, SDValue B) const;
1915
1916 /// Test whether \p V has a splatted value for all the demanded elements.
1917 ///
1918 /// On success \p UndefElts will indicate the elements that have UNDEF
1919 /// values instead of the splat value, this is only guaranteed to be correct
1920 /// for \p DemandedElts.
1921 ///
1922 /// NOTE: The function will return true for a demanded splat of UNDEF values.
1923 bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts,
1924 unsigned Depth = 0) const;
1925
1926 /// Test whether \p V has a splatted value.
1927 bool isSplatValue(SDValue V, bool AllowUndefs = false) const;
1928
1929 /// If V is a splatted value, return the source vector and its splat index.
1930 SDValue getSplatSourceVector(SDValue V, int &SplatIndex);
1931
1932 /// If V is a splat vector, return its scalar source operand by extracting
1933 /// that element from the source vector. If LegalTypes is true, this method
1934 /// may only return a legally-typed splat value. If it cannot legalize the
1935 /// splatted value it will return SDValue().
1936 SDValue getSplatValue(SDValue V, bool LegalTypes = false);
1937
1938 /// If a SHL/SRA/SRL node \p V has a constant or splat constant shift amount
1939 /// that is less than the element bit-width of the shift node, return it.
1940 const APInt *getValidShiftAmountConstant(SDValue V,
1941 const APInt &DemandedElts) const;
1942
1943 /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less
1944 /// than the element bit-width of the shift node, return the minimum value.
1945 const APInt *
1946 getValidMinimumShiftAmountConstant(SDValue V,
1947 const APInt &DemandedElts) const;
1948
1949 /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less
1950 /// than the element bit-width of the shift node, return the maximum value.
1951 const APInt *
1952 getValidMaximumShiftAmountConstant(SDValue V,
1953 const APInt &DemandedElts) const;
1954
1955 /// Match a binop + shuffle pyramid that represents a horizontal reduction
1956 /// over the elements of a vector starting from the EXTRACT_VECTOR_ELT node /p
1957 /// Extract. The reduction must use one of the opcodes listed in /p
1958 /// CandidateBinOps and on success /p BinOp will contain the matching opcode.
1959 /// Returns the vector that is being reduced on, or SDValue() if a reduction
1960 /// was not matched. If \p AllowPartials is set then in the case of a
1961 /// reduction pattern that only matches the first few stages, the extracted
1962 /// subvector of the start of the reduction is returned.
1963 SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp,
1964 ArrayRef<ISD::NodeType> CandidateBinOps,
1965 bool AllowPartials = false);
1966
1967 /// Utility function used by legalize and lowering to
1968 /// "unroll" a vector operation by splitting out the scalars and operating
1969 /// on each element individually. If the ResNE is 0, fully unroll the vector
1970 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
1971 /// If the ResNE is greater than the width of the vector op, unroll the
1972 /// vector op and fill the end of the resulting vector with UNDEFS.
1973 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
1974
1975 /// Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
1976 /// This is a separate function because those opcodes have two results.
1977 std::pair<SDValue, SDValue> UnrollVectorOverflowOp(SDNode *N,
1978 unsigned ResNE = 0);
1979
1980 /// Return true if loads are next to each other and can be
1981 /// merged. Check that both are nonvolatile and if LD is loading
1982 /// 'Bytes' bytes from a location that is 'Dist' units away from the
1983 /// location that the 'Base' load is loading from.
1984 bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base,
1985 unsigned Bytes, int Dist) const;
1986
1987 /// Infer alignment of a load / store address. Return None if it cannot be
1988 /// inferred.
1989 MaybeAlign InferPtrAlign(SDValue Ptr) const;
1990
1991 /// Compute the VTs needed for the low/hi parts of a type
1992 /// which is split (or expanded) into two not necessarily identical pieces.
1993 std::pair<EVT, EVT> GetSplitDestVTs(const EVT &VT) const;
1994
1995 /// Compute the VTs needed for the low/hi parts of a type, dependent on an
1996 /// enveloping VT that has been split into two identical pieces. Sets the
1997 /// HisIsEmpty flag when hi type has zero storage size.
1998 std::pair<EVT, EVT> GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT,
1999 bool *HiIsEmpty) const;
2000
2001 /// Split the vector with EXTRACT_SUBVECTOR using the provides
2002 /// VTs and return the low/high part.
2003 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL,
2004 const EVT &LoVT, const EVT &HiVT);
2005
2006 /// Split the vector with EXTRACT_SUBVECTOR and return the low/high part.
2007 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL) {
2008 EVT LoVT, HiVT;
2009 std::tie(LoVT, HiVT) = GetSplitDestVTs(N.getValueType());
2010 return SplitVector(N, DL, LoVT, HiVT);
2011 }
2012
2013 /// Split the explicit vector length parameter of a VP operation.
2014 std::pair<SDValue, SDValue> SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL);
2015
2016 /// Split the node's operand with EXTRACT_SUBVECTOR and
2017 /// return the low/high part.
2018 std::pair<SDValue, SDValue> SplitVectorOperand(const SDNode *N, unsigned OpNo)
2019 {
2020 return SplitVector(N->getOperand(OpNo), SDLoc(N));
2021 }
2022
2023 /// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
2024 SDValue WidenVector(const SDValue &N, const SDLoc &DL);
2025
2026 /// Append the extracted elements from Start to Count out of the vector Op in
2027 /// Args. If Count is 0, all of the elements will be extracted. The extracted
2028 /// elements will have type EVT if it is provided, and otherwise their type
2029 /// will be Op's element type.
2030 void ExtractVectorElements(SDValue Op, SmallVectorImpl<SDValue> &Args,
2031 unsigned Start = 0, unsigned Count = 0,
2032 EVT EltVT = EVT());
2033
2034 /// Compute the default alignment value for the given type.
2035 Align getEVTAlign(EVT MemoryVT) const;
2036 /// Compute the default alignment value for the given type.
2037 /// FIXME: Remove once transition to Align is over.
2038 inline unsigned getEVTAlignment(EVT MemoryVT) const {
2039 return getEVTAlign(MemoryVT).value();
2040 }
2041
2042 /// Test whether the given value is a constant int or similar node.
2043 SDNode *isConstantIntBuildVectorOrConstantInt(SDValue N) const;
2044
2045 /// Test whether the given value is a constant FP or similar node.
2046 SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N) const ;
2047
2048 /// \returns true if \p N is any kind of constant or build_vector of
2049 /// constants, int or float. If a vector, it may not necessarily be a splat.
2050 inline bool isConstantValueOfAnyType(SDValue N) const {
2051 return isConstantIntBuildVectorOrConstantInt(N) ||
2052 isConstantFPBuildVectorOrConstantFP(N);
2053 }
2054
2055 void addCallSiteInfo(const SDNode *CallNode, CallSiteInfoImpl &&CallInfo) {
2056 SDCallSiteDbgInfo[CallNode].CSInfo = std::move(CallInfo);
2057 }
2058
2059 CallSiteInfo getSDCallSiteInfo(const SDNode *CallNode) {
2060 auto I = SDCallSiteDbgInfo.find(CallNode);
2061 if (I != SDCallSiteDbgInfo.end())
2062 return std::move(I->second).CSInfo;
2063 return CallSiteInfo();
2064 }
2065
2066 void addHeapAllocSite(const SDNode *Node, MDNode *MD) {
2067 SDCallSiteDbgInfo[Node].HeapAllocSite = MD;
2068 }
2069
2070 /// Return the HeapAllocSite type associated with the SDNode, if it exists.
2071 MDNode *getHeapAllocSite(const SDNode *Node) {
2072 auto It = SDCallSiteDbgInfo.find(Node);
2073 if (It == SDCallSiteDbgInfo.end())
2074 return nullptr;
2075 return It->second.HeapAllocSite;
2076 }
2077
2078 void addNoMergeSiteInfo(const SDNode *Node, bool NoMerge) {
2079 if (NoMerge)
2080 SDCallSiteDbgInfo[Node].NoMerge = NoMerge;
2081 }
2082
2083 bool getNoMergeSiteInfo(const SDNode *Node) {
2084 auto I = SDCallSiteDbgInfo.find(Node);
2085 if (I == SDCallSiteDbgInfo.end())
2086 return false;
2087 return I->second.NoMerge;
2088 }
2089
2090 /// Return the current function's default denormal handling kind for the given
2091 /// floating point type.
2092 DenormalMode getDenormalMode(EVT VT) const {
2093 return MF->getDenormalMode(EVTToAPFloatSemantics(VT));
2094 }
2095
2096 bool shouldOptForSize() const;
2097
2098 /// Get the (commutative) neutral element for the given opcode, if it exists.
2099 SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT,
2100 SDNodeFlags Flags);
2101
2102 private:
2103 void InsertNode(SDNode *N);
2104 bool RemoveNodeFromCSEMaps(SDNode *N);
2105 void AddModifiedNodeToCSEMaps(SDNode *N);
2106 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
2107 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
2108 void *&InsertPos);
2109 SDNode *FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
2110 void *&InsertPos);
2111 SDNode *UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &loc);
2112
2113 void DeleteNodeNotInCSEMaps(SDNode *N);
2114 void DeallocateNode(SDNode *N);
2115
2116 void allnodes_clear();
2117
2118 /// Look up the node specified by ID in CSEMap. If it exists, return it. If
2119 /// not, return the insertion token that will make insertion faster. This
2120 /// overload is for nodes other than Constant or ConstantFP, use the other one
2121 /// for those.
2122 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
2123
2124 /// Look up the node specified by ID in CSEMap. If it exists, return it. If
2125 /// not, return the insertion token that will make insertion faster. Performs
2126 /// additional processing for constant nodes.
2127 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, const SDLoc &DL,
2128 void *&InsertPos);
2129
2130 /// List of non-single value types.
2131 FoldingSet<SDVTListNode> VTListMap;
2132
2133 /// Maps to auto-CSE operations.
2134 std::vector<CondCodeSDNode*> CondCodeNodes;
2135
2136 std::vector<SDNode*> ValueTypeNodes;
2137 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
2138 StringMap<SDNode*> ExternalSymbols;
2139
2140 std::map<std::pair<std::string, unsigned>, SDNode *> TargetExternalSymbols;
2141 DenseMap<MCSymbol *, SDNode *> MCSymbols;
2142
2143 FlagInserter *Inserter = nullptr;
2144 };
2145
2146 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
2147 using nodes_iterator = pointer_iterator<SelectionDAG::allnodes_iterator>;
2148
2149 static nodes_iterator nodes_begin(SelectionDAG *G) {
2150 return nodes_iterator(G->allnodes_begin());
2151 }
2152
2153 static nodes_iterator nodes_end(SelectionDAG *G) {
2154 return nodes_iterator(G->allnodes_end());
2155 }
2156 };
2157
2158 } // end namespace llvm
2159
2160 #endif // LLVM_CODEGEN_SELECTIONDAG_H