blob: af43c9b08340e59d806c3086e042486aee08a8ff [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the SelectionDAG class, and transitively defines the
11// SDNode class and subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_SELECTIONDAG_H
16#define LLVM_CODEGEN_SELECTIONDAG_H
17
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/DenseSet.h"
23#include "llvm/ADT/FoldingSet.h"
24#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/ADT/ilist.h"
28#include "llvm/ADT/iterator.h"
29#include "llvm/ADT/iterator_range.h"
30#include "llvm/Analysis/AliasAnalysis.h"
31#include "llvm/Analysis/DivergenceAnalysis.h"
32#include "llvm/CodeGen/DAGCombine.h"
33#include "llvm/CodeGen/FunctionLoweringInfo.h"
34#include "llvm/CodeGen/ISDOpcodes.h"
35#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineMemOperand.h"
37#include "llvm/CodeGen/SelectionDAGNodes.h"
38#include "llvm/CodeGen/ValueTypes.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/Instructions.h"
41#include "llvm/IR/Metadata.h"
42#include "llvm/Support/Allocator.h"
43#include "llvm/Support/ArrayRecycler.h"
44#include "llvm/Support/AtomicOrdering.h"
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/CodeGen.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/MachineValueType.h"
49#include "llvm/Support/RecyclingAllocator.h"
50#include <algorithm>
51#include <cassert>
52#include <cstdint>
53#include <functional>
54#include <map>
55#include <string>
56#include <tuple>
57#include <utility>
58#include <vector>
59
60namespace llvm {
61
62class BlockAddress;
63class Constant;
64class ConstantFP;
65class ConstantInt;
66class DataLayout;
67struct fltSemantics;
68class GlobalValue;
69struct KnownBits;
70class LLVMContext;
71class MachineBasicBlock;
72class MachineConstantPoolValue;
73class MCSymbol;
74class OptimizationRemarkEmitter;
75class SDDbgValue;
76class SelectionDAG;
77class SelectionDAGTargetInfo;
78class TargetLibraryInfo;
79class TargetLowering;
80class TargetMachine;
81class TargetSubtargetInfo;
82class Value;
83
84class SDVTListNode : public FoldingSetNode {
85 friend struct FoldingSetTrait<SDVTListNode>;
86
87 /// A reference to an Interned FoldingSetNodeID for this node.
88 /// The Allocator in SelectionDAG holds the data.
89 /// SDVTList contains all types which are frequently accessed in SelectionDAG.
90 /// The size of this list is not expected to be big so it won't introduce
91 /// a memory penalty.
92 FoldingSetNodeIDRef FastID;
93 const EVT *VTs;
94 unsigned int NumVTs;
95 /// The hash value for SDVTList is fixed, so cache it to avoid
96 /// hash calculation.
97 unsigned HashValue;
98
99public:
100 SDVTListNode(const FoldingSetNodeIDRef ID, const EVT *VT, unsigned int Num) :
101 FastID(ID), VTs(VT), NumVTs(Num) {
102 HashValue = ID.ComputeHash();
103 }
104
105 SDVTList getSDVTList() {
106 SDVTList result = {VTs, NumVTs};
107 return result;
108 }
109};
110
111/// Specialize FoldingSetTrait for SDVTListNode
112/// to avoid computing temp FoldingSetNodeID and hash value.
113template<> struct FoldingSetTrait<SDVTListNode> : DefaultFoldingSetTrait<SDVTListNode> {
114 static void Profile(const SDVTListNode &X, FoldingSetNodeID& ID) {
115 ID = X.FastID;
116 }
117
118 static bool Equals(const SDVTListNode &X, const FoldingSetNodeID &ID,
119 unsigned IDHash, FoldingSetNodeID &TempID) {
120 if (X.HashValue != IDHash)
121 return false;
122 return ID == X.FastID;
123 }
124
125 static unsigned ComputeHash(const SDVTListNode &X, FoldingSetNodeID &TempID) {
126 return X.HashValue;
127 }
128};
129
130template <> struct ilist_alloc_traits<SDNode> {
131 static void deleteNode(SDNode *) {
132 llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!");
133 }
134};
135
136/// Keeps track of dbg_value information through SDISel. We do
137/// not build SDNodes for these so as not to perturb the generated code;
138/// instead the info is kept off to the side in this structure. Each SDNode may
139/// have one or more associated dbg_value entries. This information is kept in
140/// DbgValMap.
141/// Byval parameters are handled separately because they don't use alloca's,
142/// which busts the normal mechanism. There is good reason for handling all
143/// parameters separately: they may not have code generated for them, they
144/// should always go at the beginning of the function regardless of other code
145/// motion, and debug info for them is potentially useful even if the parameter
146/// is unused. Right now only byval parameters are handled separately.
147class SDDbgInfo {
148 BumpPtrAllocator Alloc;
149 SmallVector<SDDbgValue*, 32> DbgValues;
150 SmallVector<SDDbgValue*, 32> ByvalParmDbgValues;
151 using DbgValMapType = DenseMap<const SDNode *, SmallVector<SDDbgValue *, 2>>;
152 DbgValMapType DbgValMap;
153
154public:
155 SDDbgInfo() = default;
156 SDDbgInfo(const SDDbgInfo &) = delete;
157 SDDbgInfo &operator=(const SDDbgInfo &) = delete;
158
159 void add(SDDbgValue *V, const SDNode *Node, bool isParameter) {
160 if (isParameter) {
161 ByvalParmDbgValues.push_back(V);
162 } else DbgValues.push_back(V);
163 if (Node)
164 DbgValMap[Node].push_back(V);
165 }
166
167 /// \brief 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 Alloc.Reset();
176 }
177
178 BumpPtrAllocator &getAlloc() { return Alloc; }
179
180 bool empty() const {
181 return DbgValues.empty() && ByvalParmDbgValues.empty();
182 }
183
184 ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) {
185 DbgValMapType::iterator I = DbgValMap.find(Node);
186 if (I != DbgValMap.end())
187 return I->second;
188 return ArrayRef<SDDbgValue*>();
189 }
190
191 using DbgIterator = SmallVectorImpl<SDDbgValue*>::iterator;
192
193 DbgIterator DbgBegin() { return DbgValues.begin(); }
194 DbgIterator DbgEnd() { return DbgValues.end(); }
195 DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); }
196 DbgIterator ByvalParmDbgEnd() { return ByvalParmDbgValues.end(); }
197};
198
199void checkForCycles(const SelectionDAG *DAG, bool force = false);
200
201/// This is used to represent a portion of an LLVM function in a low-level
202/// Data Dependence DAG representation suitable for instruction selection.
203/// This DAG is constructed as the first step of instruction selection in order
204/// to allow implementation of machine specific optimizations
205/// and code simplifications.
206///
207/// The representation used by the SelectionDAG is a target-independent
208/// representation, which has some similarities to the GCC RTL representation,
209/// but is significantly more simple, powerful, and is a graph form instead of a
210/// linear form.
211///
212class SelectionDAG {
213 const TargetMachine &TM;
214 const SelectionDAGTargetInfo *TSI = nullptr;
215 const TargetLowering *TLI = nullptr;
216 const TargetLibraryInfo *LibInfo = nullptr;
217 MachineFunction *MF;
218 Pass *SDAGISelPass = nullptr;
219 LLVMContext *Context;
220 CodeGenOpt::Level OptLevel;
221
222 DivergenceAnalysis * DA = nullptr;
223 FunctionLoweringInfo * FLI = nullptr;
224
225 /// The function-level optimization remark emitter. Used to emit remarks
226 /// whenever manipulating the DAG.
227 OptimizationRemarkEmitter *ORE;
228
229 /// The starting token.
230 SDNode EntryNode;
231
232 /// The root of the entire DAG.
233 SDValue Root;
234
235 /// A linked list of nodes in the current DAG.
236 ilist<SDNode> AllNodes;
237
238 /// The AllocatorType for allocating SDNodes. We use
239 /// pool allocation with recycling.
240 using NodeAllocatorType = RecyclingAllocator<BumpPtrAllocator, SDNode,
241 sizeof(LargestSDNode),
242 alignof(MostAlignedSDNode)>;
243
244 /// Pool allocation for nodes.
245 NodeAllocatorType NodeAllocator;
246
247 /// This structure is used to memoize nodes, automatically performing
248 /// CSE with existing nodes when a duplicate is requested.
249 FoldingSet<SDNode> CSEMap;
250
251 /// Pool allocation for machine-opcode SDNode operands.
252 BumpPtrAllocator OperandAllocator;
253 ArrayRecycler<SDUse> OperandRecycler;
254
255 /// Pool allocation for misc. objects that are created once per SelectionDAG.
256 BumpPtrAllocator Allocator;
257
258 /// Tracks dbg_value information through SDISel.
259 SDDbgInfo *DbgInfo;
260
261 uint16_t NextPersistentId = 0;
262
263public:
264 /// Clients of various APIs that cause global effects on
265 /// the DAG can optionally implement this interface. This allows the clients
266 /// to handle the various sorts of updates that happen.
267 ///
268 /// A DAGUpdateListener automatically registers itself with DAG when it is
269 /// constructed, and removes itself when destroyed in RAII fashion.
270 struct DAGUpdateListener {
271 DAGUpdateListener *const Next;
272 SelectionDAG &DAG;
273
274 explicit DAGUpdateListener(SelectionDAG &D)
275 : Next(D.UpdateListeners), DAG(D) {
276 DAG.UpdateListeners = this;
277 }
278
279 virtual ~DAGUpdateListener() {
280 assert(DAG.UpdateListeners == this &&
281 "DAGUpdateListeners must be destroyed in LIFO order");
282 DAG.UpdateListeners = Next;
283 }
284
285 /// The node N that was deleted and, if E is not null, an
286 /// equivalent node E that replaced it.
287 virtual void NodeDeleted(SDNode *N, SDNode *E);
288
289 /// The node N that was updated.
290 virtual void NodeUpdated(SDNode *N);
291 };
292
293 struct DAGNodeDeletedListener : public DAGUpdateListener {
294 std::function<void(SDNode *, SDNode *)> Callback;
295
296 DAGNodeDeletedListener(SelectionDAG &DAG,
297 std::function<void(SDNode *, SDNode *)> Callback)
298 : DAGUpdateListener(DAG), Callback(std::move(Callback)) {}
299
300 void NodeDeleted(SDNode *N, SDNode *E) override { Callback(N, E); }
301 };
302
303 /// When true, additional steps are taken to
304 /// ensure that getConstant() and similar functions return DAG nodes that
305 /// have legal types. This is important after type legalization since
306 /// any illegally typed nodes generated after this point will not experience
307 /// type legalization.
308 bool NewNodesMustHaveLegalTypes = false;
309
310private:
311 /// DAGUpdateListener is a friend so it can manipulate the listener stack.
312 friend struct DAGUpdateListener;
313
314 /// Linked list of registered DAGUpdateListener instances.
315 /// This stack is maintained by DAGUpdateListener RAII.
316 DAGUpdateListener *UpdateListeners = nullptr;
317
318 /// Implementation of setSubgraphColor.
319 /// Return whether we had to truncate the search.
320 bool setSubgraphColorHelper(SDNode *N, const char *Color,
321 DenseSet<SDNode *> &visited,
322 int level, bool &printed);
323
324 template <typename SDNodeT, typename... ArgTypes>
325 SDNodeT *newSDNode(ArgTypes &&... Args) {
326 return new (NodeAllocator.template Allocate<SDNodeT>())
327 SDNodeT(std::forward<ArgTypes>(Args)...);
328 }
329
330 /// Build a synthetic SDNodeT with the given args and extract its subclass
331 /// data as an integer (e.g. for use in a folding set).
332 ///
333 /// The args to this function are the same as the args to SDNodeT's
334 /// constructor, except the second arg (assumed to be a const DebugLoc&) is
335 /// omitted.
336 template <typename SDNodeT, typename... ArgTypes>
337 static uint16_t getSyntheticNodeSubclassData(unsigned IROrder,
338 ArgTypes &&... Args) {
339 // The compiler can reduce this expression to a constant iff we pass an
340 // empty DebugLoc. Thankfully, the debug location doesn't have any bearing
341 // on the subclass data.
342 return SDNodeT(IROrder, DebugLoc(), std::forward<ArgTypes>(Args)...)
343 .getRawSubclassData();
344 }
345
346 template <typename SDNodeTy>
347 static uint16_t getSyntheticNodeSubclassData(unsigned Opc, unsigned Order,
348 SDVTList VTs, EVT MemoryVT,
349 MachineMemOperand *MMO) {
350 return SDNodeTy(Opc, Order, DebugLoc(), VTs, MemoryVT, MMO)
351 .getRawSubclassData();
352 }
353
354 void createOperands(SDNode *Node, ArrayRef<SDValue> Vals);
355
356 void removeOperands(SDNode *Node) {
357 if (!Node->OperandList)
358 return;
359 OperandRecycler.deallocate(
360 ArrayRecycler<SDUse>::Capacity::get(Node->NumOperands),
361 Node->OperandList);
362 Node->NumOperands = 0;
363 Node->OperandList = nullptr;
364 }
365 void CreateTopologicalOrder(std::vector<SDNode*>& Order);
366public:
367 explicit SelectionDAG(const TargetMachine &TM, CodeGenOpt::Level);
368 SelectionDAG(const SelectionDAG &) = delete;
369 SelectionDAG &operator=(const SelectionDAG &) = delete;
370 ~SelectionDAG();
371
372 /// Prepare this SelectionDAG to process code in the given MachineFunction.
373 void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE,
374 Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
375 DivergenceAnalysis * DA);
376
377 void setFunctionLoweringInfo(FunctionLoweringInfo * FuncInfo) {
378 FLI = FuncInfo;
379 }
380
381 /// Clear state and free memory necessary to make this
382 /// SelectionDAG ready to process a new block.
383 void clear();
384
385 MachineFunction &getMachineFunction() const { return *MF; }
386 const Pass *getPass() const { return SDAGISelPass; }
387
388 const DataLayout &getDataLayout() const { return MF->getDataLayout(); }
389 const TargetMachine &getTarget() const { return TM; }
390 const TargetSubtargetInfo &getSubtarget() const { return MF->getSubtarget(); }
391 const TargetLowering &getTargetLoweringInfo() const { return *TLI; }
392 const TargetLibraryInfo &getLibInfo() const { return *LibInfo; }
393 const SelectionDAGTargetInfo &getSelectionDAGInfo() const { return *TSI; }
394 LLVMContext *getContext() const {return Context; }
395 OptimizationRemarkEmitter &getORE() const { return *ORE; }
396
397 /// Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
398 void viewGraph(const std::string &Title);
399 void viewGraph();
400
401#ifndef NDEBUG
402 std::map<const SDNode *, std::string> NodeGraphAttrs;
403#endif
404
405 /// Clear all previously defined node graph attributes.
406 /// Intended to be used from a debugging tool (eg. gdb).
407 void clearGraphAttrs();
408
409 /// Set graph attributes for a node. (eg. "color=red".)
410 void setGraphAttrs(const SDNode *N, const char *Attrs);
411
412 /// Get graph attributes for a node. (eg. "color=red".)
413 /// Used from getNodeAttributes.
414 const std::string getGraphAttrs(const SDNode *N) const;
415
416 /// Convenience for setting node color attribute.
417 void setGraphColor(const SDNode *N, const char *Color);
418
419 /// Convenience for setting subgraph color attribute.
420 void setSubgraphColor(SDNode *N, const char *Color);
421
422 using allnodes_const_iterator = ilist<SDNode>::const_iterator;
423
424 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
425 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
426
427 using allnodes_iterator = ilist<SDNode>::iterator;
428
429 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
430 allnodes_iterator allnodes_end() { return AllNodes.end(); }
431
432 ilist<SDNode>::size_type allnodes_size() const {
433 return AllNodes.size();
434 }
435
436 iterator_range<allnodes_iterator> allnodes() {
437 return make_range(allnodes_begin(), allnodes_end());
438 }
439 iterator_range<allnodes_const_iterator> allnodes() const {
440 return make_range(allnodes_begin(), allnodes_end());
441 }
442
443 /// Return the root tag of the SelectionDAG.
444 const SDValue &getRoot() const { return Root; }
445
446 /// Return the token chain corresponding to the entry of the function.
447 SDValue getEntryNode() const {
448 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
449 }
450
451 /// Set the current root tag of the SelectionDAG.
452 ///
453 const SDValue &setRoot(SDValue N) {
454 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
455 "DAG root value is not a chain!");
456 if (N.getNode())
457 checkForCycles(N.getNode(), this);
458 Root = N;
459 if (N.getNode())
460 checkForCycles(this);
461 return Root;
462 }
463
464 void VerifyDAGDiverence();
465
466 /// This iterates over the nodes in the SelectionDAG, folding
467 /// certain types of nodes together, or eliminating superfluous nodes. The
468 /// Level argument controls whether Combine is allowed to produce nodes and
469 /// types that are illegal on the target.
470 void Combine(CombineLevel Level, AliasAnalysis *AA,
471 CodeGenOpt::Level OptLevel);
472
473 /// This transforms the SelectionDAG into a SelectionDAG that
474 /// only uses types natively supported by the target.
475 /// Returns "true" if it made any changes.
476 ///
477 /// Note that this is an involved process that may invalidate pointers into
478 /// the graph.
479 bool LegalizeTypes();
480
481 /// This transforms the SelectionDAG into a SelectionDAG that is
482 /// compatible with the target instruction selector, as indicated by the
483 /// TargetLowering object.
484 ///
485 /// Note that this is an involved process that may invalidate pointers into
486 /// the graph.
487 void Legalize();
488
489 /// \brief Transforms a SelectionDAG node and any operands to it into a node
490 /// that is compatible with the target instruction selector, as indicated by
491 /// the TargetLowering object.
492 ///
493 /// \returns true if \c N is a valid, legal node after calling this.
494 ///
495 /// This essentially runs a single recursive walk of the \c Legalize process
496 /// over the given node (and its operands). This can be used to incrementally
497 /// legalize the DAG. All of the nodes which are directly replaced,
498 /// potentially including N, are added to the output parameter \c
499 /// UpdatedNodes so that the delta to the DAG can be understood by the
500 /// caller.
501 ///
502 /// When this returns false, N has been legalized in a way that make the
503 /// pointer passed in no longer valid. It may have even been deleted from the
504 /// DAG, and so it shouldn't be used further. When this returns true, the
505 /// N passed in is a legal node, and can be immediately processed as such.
506 /// This may still have done some work on the DAG, and will still populate
507 /// UpdatedNodes with any new nodes replacing those originally in the DAG.
508 bool LegalizeOp(SDNode *N, SmallSetVector<SDNode *, 16> &UpdatedNodes);
509
510 /// This transforms the SelectionDAG into a SelectionDAG
511 /// that only uses vector math operations supported by the target. This is
512 /// necessary as a separate step from Legalize because unrolling a vector
513 /// operation can introduce illegal types, which requires running
514 /// LegalizeTypes again.
515 ///
516 /// This returns true if it made any changes; in that case, LegalizeTypes
517 /// is called again before Legalize.
518 ///
519 /// Note that this is an involved process that may invalidate pointers into
520 /// the graph.
521 bool LegalizeVectors();
522
523 /// This method deletes all unreachable nodes in the SelectionDAG.
524 void RemoveDeadNodes();
525
526 /// Remove the specified node from the system. This node must
527 /// have no referrers.
528 void DeleteNode(SDNode *N);
529
530 /// Return an SDVTList that represents the list of values specified.
531 SDVTList getVTList(EVT VT);
532 SDVTList getVTList(EVT VT1, EVT VT2);
533 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
534 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
535 SDVTList getVTList(ArrayRef<EVT> VTs);
536
537 //===--------------------------------------------------------------------===//
538 // Node creation methods.
539
540 /// \brief Create a ConstantSDNode wrapping a constant value.
541 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
542 ///
543 /// If only legal types can be produced, this does the necessary
544 /// transformations (e.g., if the vector element type is illegal).
545 /// @{
546 SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
547 bool isTarget = false, bool isOpaque = false);
548 SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
549 bool isTarget = false, bool isOpaque = false);
550
551 SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false,
552 bool IsOpaque = false) {
553 return getConstant(APInt::getAllOnesValue(VT.getScalarSizeInBits()), DL,
554 VT, IsTarget, IsOpaque);
555 }
556
557 SDValue getConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
558 bool isTarget = false, bool isOpaque = false);
559 SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL,
560 bool isTarget = false);
561 SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT,
562 bool isOpaque = false) {
563 return getConstant(Val, DL, VT, true, isOpaque);
564 }
565 SDValue getTargetConstant(const APInt &Val, const SDLoc &DL, EVT VT,
566 bool isOpaque = false) {
567 return getConstant(Val, DL, VT, true, isOpaque);
568 }
569 SDValue getTargetConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
570 bool isOpaque = false) {
571 return getConstant(Val, DL, VT, true, isOpaque);
572 }
573
574 /// \brief Create a true or false constant of type \p VT using the target's
575 /// BooleanContent for type \p OpVT.
576 SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT);
577 /// @}
578
579 /// \brief Create a ConstantFPSDNode wrapping a constant value.
580 /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
581 ///
582 /// If only legal types can be produced, this does the necessary
583 /// transformations (e.g., if the vector element type is illegal).
584 /// The forms that take a double should only be used for simple constants
585 /// that can be exactly represented in VT. No checks are made.
586 /// @{
587 SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT,
588 bool isTarget = false);
589 SDValue getConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT,
590 bool isTarget = false);
591 SDValue getConstantFP(const ConstantFP &CF, const SDLoc &DL, EVT VT,
592 bool isTarget = false);
593 SDValue getTargetConstantFP(double Val, const SDLoc &DL, EVT VT) {
594 return getConstantFP(Val, DL, VT, true);
595 }
596 SDValue getTargetConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT) {
597 return getConstantFP(Val, DL, VT, true);
598 }
599 SDValue getTargetConstantFP(const ConstantFP &Val, const SDLoc &DL, EVT VT) {
600 return getConstantFP(Val, DL, VT, true);
601 }
602 /// @}
603
604 SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
605 int64_t offset = 0, bool isTargetGA = false,
606 unsigned char TargetFlags = 0);
607 SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
608 int64_t offset = 0,
609 unsigned char TargetFlags = 0) {
610 return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
611 }
612 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
613 SDValue getTargetFrameIndex(int FI, EVT VT) {
614 return getFrameIndex(FI, VT, true);
615 }
616 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
617 unsigned char TargetFlags = 0);
618 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
619 return getJumpTable(JTI, VT, true, TargetFlags);
620 }
621 SDValue getConstantPool(const Constant *C, EVT VT,
622 unsigned Align = 0, int Offs = 0, bool isT=false,
623 unsigned char TargetFlags = 0);
624 SDValue getTargetConstantPool(const Constant *C, EVT VT,
625 unsigned Align = 0, int Offset = 0,
626 unsigned char TargetFlags = 0) {
627 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
628 }
629 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
630 unsigned Align = 0, int Offs = 0, bool isT=false,
631 unsigned char TargetFlags = 0);
632 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
633 EVT VT, unsigned Align = 0,
634 int Offset = 0, unsigned char TargetFlags=0) {
635 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
636 }
637 SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0,
638 unsigned char TargetFlags = 0);
639 // When generating a branch to a BB, we don't in general know enough
640 // to provide debug info for the BB at that time, so keep this one around.
641 SDValue getBasicBlock(MachineBasicBlock *MBB);
642 SDValue getBasicBlock(MachineBasicBlock *MBB, SDLoc dl);
643 SDValue getExternalSymbol(const char *Sym, EVT VT);
644 SDValue getExternalSymbol(const char *Sym, const SDLoc &dl, EVT VT);
645 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
646 unsigned char TargetFlags = 0);
647 SDValue getMCSymbol(MCSymbol *Sym, EVT VT);
648
649 SDValue getValueType(EVT);
650 SDValue getRegister(unsigned Reg, EVT VT);
651 SDValue getRegisterMask(const uint32_t *RegMask);
652 SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label);
653 SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root,
654 MCSymbol *Label);
655 SDValue getBlockAddress(const BlockAddress *BA, EVT VT,
656 int64_t Offset = 0, bool isTarget = false,
657 unsigned char TargetFlags = 0);
658 SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT,
659 int64_t Offset = 0,
660 unsigned char TargetFlags = 0) {
661 return getBlockAddress(BA, VT, Offset, true, TargetFlags);
662 }
663
664 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg,
665 SDValue N) {
666 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
667 getRegister(Reg, N.getValueType()), N);
668 }
669
670 // This version of the getCopyToReg method takes an extra operand, which
671 // indicates that there is potentially an incoming glue value (if Glue is not
672 // null) and that there should be a glue result.
673 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N,
674 SDValue Glue) {
675 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
676 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue };
677 return getNode(ISD::CopyToReg, dl, VTs,
678 makeArrayRef(Ops, Glue.getNode() ? 4 : 3));
679 }
680
681 // Similar to last getCopyToReg() except parameter Reg is a SDValue
682 SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, SDValue Reg, SDValue N,
683 SDValue Glue) {
684 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
685 SDValue Ops[] = { Chain, Reg, N, Glue };
686 return getNode(ISD::CopyToReg, dl, VTs,
687 makeArrayRef(Ops, Glue.getNode() ? 4 : 3));
688 }
689
690 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT) {
691 SDVTList VTs = getVTList(VT, MVT::Other);
692 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
693 return getNode(ISD::CopyFromReg, dl, VTs, Ops);
694 }
695
696 // This version of the getCopyFromReg method takes an extra operand, which
697 // indicates that there is potentially an incoming glue value (if Glue is not
698 // null) and that there should be a glue result.
699 SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT,
700 SDValue Glue) {
701 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue);
702 SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue };
703 return getNode(ISD::CopyFromReg, dl, VTs,
704 makeArrayRef(Ops, Glue.getNode() ? 3 : 2));
705 }
706
707 SDValue getCondCode(ISD::CondCode Cond);
708
709 /// Return an ISD::VECTOR_SHUFFLE node. The number of elements in VT,
710 /// which must be a vector type, must match the number of mask elements
711 /// NumElts. An integer mask element equal to -1 is treated as undefined.
712 SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
713 ArrayRef<int> Mask);
714
715 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
716 /// which must be a vector type, must match the number of operands in Ops.
717 /// The operands must have the same type as (or, for integers, a type wider
718 /// than) VT's element type.
719 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDValue> Ops) {
720 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
721 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
722 }
723
724 /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
725 /// which must be a vector type, must match the number of operands in Ops.
726 /// The operands must have the same type as (or, for integers, a type wider
727 /// than) VT's element type.
728 SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDUse> Ops) {
729 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
730 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
731 }
732
733 /// Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all
734 /// elements. VT must be a vector type. Op's type must be the same as (or,
735 /// for integers, a type wider than) VT's element type.
736 SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op) {
737 // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
738 if (Op.getOpcode() == ISD::UNDEF) {
739 assert((VT.getVectorElementType() == Op.getValueType() ||
740 (VT.isInteger() &&
741 VT.getVectorElementType().bitsLE(Op.getValueType()))) &&
742 "A splatted value must have a width equal or (for integers) "
743 "greater than the vector element type!");
744 return getNode(ISD::UNDEF, SDLoc(), VT);
745 }
746
747 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Op);
748 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
749 }
750
751 /// \brief Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to
752 /// the shuffle node in input but with swapped operands.
753 ///
754 /// Example: shuffle A, B, <0,5,2,7> -> shuffle B, A, <4,1,6,3>
755 SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV);
756
757 /// Convert Op, which must be of float type, to the
758 /// float type VT, by either extending or rounding (by truncation).
759 SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT);
760
761 /// Convert Op, which must be of integer type, to the
762 /// integer type VT, by either any-extending or truncating it.
763 SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
764
765 /// Convert Op, which must be of integer type, to the
766 /// integer type VT, by either sign-extending or truncating it.
767 SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
768
769 /// Convert Op, which must be of integer type, to the
770 /// integer type VT, by either zero-extending or truncating it.
771 SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
772
773 /// Return the expression required to zero extend the Op
774 /// value assuming it was the smaller SrcTy value.
775 SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT SrcTy);
776
777 /// Return an operation which will any-extend the low lanes of the operand
778 /// into the specified vector type. For example,
779 /// this can convert a v16i8 into a v4i32 by any-extending the low four
780 /// lanes of the operand from i8 to i32.
781 SDValue getAnyExtendVectorInReg(SDValue Op, const SDLoc &DL, EVT VT);
782
783 /// Return an operation which will sign extend the low lanes of the operand
784 /// into the specified vector type. For example,
785 /// this can convert a v16i8 into a v4i32 by sign extending the low four
786 /// lanes of the operand from i8 to i32.
787 SDValue getSignExtendVectorInReg(SDValue Op, const SDLoc &DL, EVT VT);
788
789 /// Return an operation which will zero extend the low lanes of the operand
790 /// into the specified vector type. For example,
791 /// this can convert a v16i8 into a v4i32 by zero extending the low four
792 /// lanes of the operand from i8 to i32.
793 SDValue getZeroExtendVectorInReg(SDValue Op, const SDLoc &DL, EVT VT);
794
795 /// Convert Op, which must be of integer type, to the integer type VT,
796 /// by using an extension appropriate for the target's
797 /// BooleanContent for type OpVT or truncating it.
798 SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT);
799
800 /// Create a bitwise NOT operation as (XOR Val, -1).
801 SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT);
802
803 /// \brief Create a logical NOT operation as (XOR Val, BooleanOne).
804 SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT);
805
806 /// \brief Create an add instruction with appropriate flags when used for
807 /// addressing some offset of an object. i.e. if a load is split into multiple
808 /// components, create an add nuw from the base pointer to the offset.
809 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Op, int64_t Offset) {
810 EVT VT = Op.getValueType();
811 return getObjectPtrOffset(SL, Op, getConstant(Offset, SL, VT));
812 }
813
814 SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Op, SDValue Offset) {
815 EVT VT = Op.getValueType();
816
817 // The object itself can't wrap around the address space, so it shouldn't be
818 // possible for the adds of the offsets to the split parts to overflow.
819 SDNodeFlags Flags;
820 Flags.setNoUnsignedWrap(true);
821 return getNode(ISD::ADD, SL, VT, Op, Offset, Flags);
822 }
823
824 /// Return a new CALLSEQ_START node, that starts new call frame, in which
825 /// InSize bytes are set up inside CALLSEQ_START..CALLSEQ_END sequence and
826 /// OutSize specifies part of the frame set up prior to the sequence.
827 SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize,
828 const SDLoc &DL) {
829 SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
830 SDValue Ops[] = { Chain,
831 getIntPtrConstant(InSize, DL, true),
832 getIntPtrConstant(OutSize, DL, true) };
833 return getNode(ISD::CALLSEQ_START, DL, VTs, Ops);
834 }
835
836 /// Return a new CALLSEQ_END node, which always must have a
837 /// glue result (to ensure it's not CSE'd).
838 /// CALLSEQ_END does not have a useful SDLoc.
839 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
840 SDValue InGlue, const SDLoc &DL) {
841 SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue);
842 SmallVector<SDValue, 4> Ops;
843 Ops.push_back(Chain);
844 Ops.push_back(Op1);
845 Ops.push_back(Op2);
846 if (InGlue.getNode())
847 Ops.push_back(InGlue);
848 return getNode(ISD::CALLSEQ_END, DL, NodeTys, Ops);
849 }
850
851 /// Return true if the result of this operation is always undefined.
852 bool isUndef(unsigned Opcode, ArrayRef<SDValue> Ops);
853
854 /// Return an UNDEF node. UNDEF does not have a useful SDLoc.
855 SDValue getUNDEF(EVT VT) {
856 return getNode(ISD::UNDEF, SDLoc(), VT);
857 }
858
859 /// Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
860 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
861 return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT);
862 }
863
864 /// Gets or creates the specified node.
865 ///
866 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
867 ArrayRef<SDUse> Ops);
868 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
869 ArrayRef<SDValue> Ops, const SDNodeFlags Flags = SDNodeFlags());
870 SDValue getNode(unsigned Opcode, const SDLoc &DL, ArrayRef<EVT> ResultTys,
871 ArrayRef<SDValue> Ops);
872 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs,
873 ArrayRef<SDValue> Ops);
874
875 // Specialize based on number of operands.
876 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT);
877 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N,
878 const SDNodeFlags Flags = SDNodeFlags());
879 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
880 SDValue N2, const SDNodeFlags Flags = SDNodeFlags());
881 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
882 SDValue N2, SDValue N3);
883 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
884 SDValue N2, SDValue N3, SDValue N4);
885 SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
886 SDValue N2, SDValue N3, SDValue N4, SDValue N5);
887
888 // Specialize again based on number of operands for nodes with a VTList
889 // rather than a single VT.
890 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs);
891 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs, SDValue N);
892 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs, SDValue N1,
893 SDValue N2);
894 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs, SDValue N1,
895 SDValue N2, SDValue N3);
896 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs, SDValue N1,
897 SDValue N2, SDValue N3, SDValue N4);
898 SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTs, SDValue N1,
899 SDValue N2, SDValue N3, SDValue N4, SDValue N5);
900
901 /// Compute a TokenFactor to force all the incoming stack arguments to be
902 /// loaded from the stack. This is used in tail call lowering to protect
903 /// stack arguments from being clobbered.
904 SDValue getStackArgumentTokenFactor(SDValue Chain);
905
906 SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
907 SDValue Size, unsigned Align, bool isVol, bool AlwaysInline,
908 bool isTailCall, MachinePointerInfo DstPtrInfo,
909 MachinePointerInfo SrcPtrInfo);
910
911 SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
912 SDValue Size, unsigned Align, bool isVol, bool isTailCall,
913 MachinePointerInfo DstPtrInfo,
914 MachinePointerInfo SrcPtrInfo);
915
916 SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
917 SDValue Size, unsigned Align, bool isVol, bool isTailCall,
918 MachinePointerInfo DstPtrInfo);
919
920 /// Helper function to make it easier to build SetCC's if you just
921 /// have an ISD::CondCode instead of an SDValue.
922 ///
923 SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS,
924 ISD::CondCode Cond) {
925 assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
926 "Cannot compare scalars to vectors");
927 assert(LHS.getValueType().isVector() == VT.isVector() &&
928 "Cannot compare scalars to vectors");
929 assert(Cond != ISD::SETCC_INVALID &&
930 "Cannot create a setCC of an invalid node.");
931 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
932 }
933
934 /// Helper function to make it easier to build Select's if you just
935 /// have operands and don't want to check for vector.
936 SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS,
937 SDValue RHS) {
938 assert(LHS.getValueType() == RHS.getValueType() &&
939 "Cannot use select on differing types");
940 assert(VT.isVector() == LHS.getValueType().isVector() &&
941 "Cannot mix vectors and scalars");
942 return getNode(Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT, DL, VT,
943 Cond, LHS, RHS);
944 }
945
946 /// Helper function to make it easier to build SelectCC's if you
947 /// just have an ISD::CondCode instead of an SDValue.
948 ///
949 SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True,
950 SDValue False, ISD::CondCode Cond) {
951 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
952 LHS, RHS, True, False, getCondCode(Cond));
953 }
954
955 /// VAArg produces a result and token chain, and takes a pointer
956 /// and a source value as input.
957 SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
958 SDValue SV, unsigned Align);
959
960 /// Gets a node for an atomic cmpxchg op. There are two
961 /// valid Opcodes. ISD::ATOMIC_CMO_SWAP produces the value loaded and a
962 /// chain result. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS produces the value loaded,
963 /// a success flag (initially i1), and a chain.
964 SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT,
965 SDVTList VTs, SDValue Chain, SDValue Ptr,
966 SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
967 unsigned Alignment, AtomicOrdering SuccessOrdering,
968 AtomicOrdering FailureOrdering,
969 SyncScope::ID SSID);
970 SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT,
971 SDVTList VTs, SDValue Chain, SDValue Ptr,
972 SDValue Cmp, SDValue Swp, MachineMemOperand *MMO);
973
974 /// Gets a node for an atomic op, produces result (if relevant)
975 /// and chain and takes 2 operands.
976 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain,
977 SDValue Ptr, SDValue Val, const Value *PtrVal,
978 unsigned Alignment, AtomicOrdering Ordering,
979 SyncScope::ID SSID);
980 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain,
981 SDValue Ptr, SDValue Val, MachineMemOperand *MMO);
982
983 /// Gets a node for an atomic op, produces result and chain and
984 /// takes 1 operand.
985 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, EVT VT,
986 SDValue Chain, SDValue Ptr, MachineMemOperand *MMO);
987
988 /// Gets a node for an atomic op, produces result and chain and takes N
989 /// operands.
990 SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
991 SDVTList VTList, ArrayRef<SDValue> Ops,
992 MachineMemOperand *MMO);
993
994 /// Creates a MemIntrinsicNode that may produce a
995 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
996 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
997 /// less than FIRST_TARGET_MEMORY_OPCODE.
998 SDValue getMemIntrinsicNode(
999 unsigned Opcode, const SDLoc &dl, SDVTList VTList,
1000 ArrayRef<SDValue> Ops, EVT MemVT,
1001 MachinePointerInfo PtrInfo,
1002 unsigned Align = 0,
1003 MachineMemOperand::Flags Flags
1004 = MachineMemOperand::MOLoad | MachineMemOperand::MOStore,
1005 unsigned Size = 0);
1006
1007 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList,
1008 ArrayRef<SDValue> Ops, EVT MemVT,
1009 MachineMemOperand *MMO);
1010
1011 /// Create a MERGE_VALUES node from the given operands.
1012 SDValue getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl);
1013
1014 /// Loads are not normal binary operators: their result type is not
1015 /// determined by their operands, and they produce a value AND a token chain.
1016 ///
1017 /// This function will set the MOLoad flag on MMOFlags, but you can set it if
1018 /// you want. The MOStore flag must not be set.
1019 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1020 MachinePointerInfo PtrInfo, unsigned Alignment = 0,
1021 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1022 const AAMDNodes &AAInfo = AAMDNodes(),
1023 const MDNode *Ranges = nullptr);
1024 SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1025 MachineMemOperand *MMO);
1026 SDValue
1027 getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain,
1028 SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT,
1029 unsigned Alignment = 0,
1030 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1031 const AAMDNodes &AAInfo = AAMDNodes());
1032 SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
1033 SDValue Chain, SDValue Ptr, EVT MemVT,
1034 MachineMemOperand *MMO);
1035 SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
1036 SDValue Offset, ISD::MemIndexedMode AM);
1037 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1038 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1039 MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment = 0,
1040 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1041 const AAMDNodes &AAInfo = AAMDNodes(),
1042 const MDNode *Ranges = nullptr);
1043 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
1044 const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
1045 EVT MemVT, MachineMemOperand *MMO);
1046
1047 /// Helper function to build ISD::STORE nodes.
1048 ///
1049 /// This function will set the MOStore flag on MMOFlags, but you can set it if
1050 /// you want. The MOLoad and MOInvariant flags must not be set.
1051 SDValue
1052 getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1053 MachinePointerInfo PtrInfo, unsigned Alignment = 0,
1054 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1055 const AAMDNodes &AAInfo = AAMDNodes());
1056 SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1057 MachineMemOperand *MMO);
1058 SDValue
1059 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1060 MachinePointerInfo PtrInfo, EVT TVT, unsigned Alignment = 0,
1061 MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1062 const AAMDNodes &AAInfo = AAMDNodes());
1063 SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
1064 SDValue Ptr, EVT TVT, MachineMemOperand *MMO);
1065 SDValue getIndexedStore(SDValue OrigStoe, const SDLoc &dl, SDValue Base,
1066 SDValue Offset, ISD::MemIndexedMode AM);
1067
1068 /// Returns sum of the base pointer and offset.
1069 SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, const SDLoc &DL);
1070
1071 SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
1072 SDValue Mask, SDValue Src0, EVT MemVT,
1073 MachineMemOperand *MMO, ISD::LoadExtType,
1074 bool IsExpanding = false);
1075 SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val,
1076 SDValue Ptr, SDValue Mask, EVT MemVT,
1077 MachineMemOperand *MMO, bool IsTruncating = false,
1078 bool IsCompressing = false);
1079 SDValue getMaskedGather(SDVTList VTs, EVT VT, const SDLoc &dl,
1080 ArrayRef<SDValue> Ops, MachineMemOperand *MMO);
1081 SDValue getMaskedScatter(SDVTList VTs, EVT VT, const SDLoc &dl,
1082 ArrayRef<SDValue> Ops, MachineMemOperand *MMO);
1083
1084 /// Return (create a new or find existing) a target-specific node.
1085 /// TargetMemSDNode should be derived class from MemSDNode.
1086 template <class TargetMemSDNode>
1087 SDValue getTargetMemSDNode(SDVTList VTs, ArrayRef<SDValue> Ops,
1088 const SDLoc &dl, EVT MemVT,
1089 MachineMemOperand *MMO);
1090
1091 /// Construct a node to track a Value* through the backend.
1092 SDValue getSrcValue(const Value *v);
1093
1094 /// Return an MDNodeSDNode which holds an MDNode.
1095 SDValue getMDNode(const MDNode *MD);
1096
1097 /// Return a bitcast using the SDLoc of the value operand, and casting to the
1098 /// provided type. Use getNode to set a custom SDLoc.
1099 SDValue getBitcast(EVT VT, SDValue V);
1100
1101 /// Return an AddrSpaceCastSDNode.
1102 SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS,
1103 unsigned DestAS);
1104
1105 /// Return the specified value casted to
1106 /// the target's desired shift amount type.
1107 SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);
1108
1109 /// Expand the specified \c ISD::VAARG node as the Legalize pass would.
1110 SDValue expandVAArg(SDNode *Node);
1111
1112 /// Expand the specified \c ISD::VACOPY node as the Legalize pass would.
1113 SDValue expandVACopy(SDNode *Node);
1114
1115 /// *Mutate* the specified node in-place to have the
1116 /// specified operands. If the resultant node already exists in the DAG,
1117 /// this does not modify the specified node, instead it returns the node that
1118 /// already exists. If the resultant node does not exist in the DAG, the
1119 /// input node is returned. As a degenerate case, if you specify the same
1120 /// input operands as the node already has, the input node is returned.
1121 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op);
1122 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2);
1123 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1124 SDValue Op3);
1125 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1126 SDValue Op3, SDValue Op4);
1127 SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
1128 SDValue Op3, SDValue Op4, SDValue Op5);
1129 SDNode *UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops);
1130
1131 // Propagates the change in divergence to users
1132 void updateDivergence(SDNode * N);
1133
1134 /// These are used for target selectors to *mutate* the
1135 /// specified node to have the specified return type, Target opcode, and
1136 /// operands. Note that target opcodes are stored as
1137 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
1138 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
1139 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
1140 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
1141 SDValue Op1, SDValue Op2);
1142 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
1143 SDValue Op1, SDValue Op2, SDValue Op3);
1144 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
1145 ArrayRef<SDValue> Ops);
1146 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
1147 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
1148 EVT VT2, ArrayRef<SDValue> Ops);
1149 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
1150 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
1151 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
1152 EVT VT2, SDValue Op1);
1153 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
1154 EVT VT2, SDValue Op1, SDValue Op2);
1155 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
1156 ArrayRef<SDValue> Ops);
1157
1158 /// This *mutates* the specified node to have the specified
1159 /// return type, opcode, and operands.
1160 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
1161 ArrayRef<SDValue> Ops);
1162
1163 /// Mutate the specified strict FP node to its non-strict equivalent,
1164 /// unlinking the node from its chain and dropping the metadata arguments.
1165 /// The node must be a strict FP node.
1166 SDNode *mutateStrictFPToFP(SDNode *Node);
1167
1168 /// These are used for target selectors to create a new node
1169 /// with specified return type(s), MachineInstr opcode, and operands.
1170 ///
1171 /// Note that getMachineNode returns the resultant node. If there is already
1172 /// a node of the specified opcode and operands, it returns that node instead
1173 /// of the current one.
1174 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT);
1175 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1176 SDValue Op1);
1177 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1178 SDValue Op1, SDValue Op2);
1179 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1180 SDValue Op1, SDValue Op2, SDValue Op3);
1181 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
1182 ArrayRef<SDValue> Ops);
1183 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1184 EVT VT2, SDValue Op1, SDValue Op2);
1185 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1186 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
1187 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1188 EVT VT2, ArrayRef<SDValue> Ops);
1189 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1190 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2);
1191 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1192 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2,
1193 SDValue Op3);
1194 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
1195 EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
1196 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl,
1197 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops);
1198 MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, SDVTList VTs,
1199 ArrayRef<SDValue> Ops);
1200
1201 /// A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
1202 SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
1203 SDValue Operand);
1204
1205 /// A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
1206 SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
1207 SDValue Operand, SDValue Subreg);
1208
1209 /// Get the specified node if it's already available, or else return NULL.
1210 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs, ArrayRef<SDValue> Ops,
1211 const SDNodeFlags Flags = SDNodeFlags());
1212
1213 /// Creates a SDDbgValue node.
1214 SDDbgValue *getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N,
1215 unsigned R, bool IsIndirect, const DebugLoc &DL,
1216 unsigned O);
1217
1218 /// Creates a constant SDDbgValue node.
1219 SDDbgValue *getConstantDbgValue(DIVariable *Var, DIExpression *Expr,
1220 const Value *C, const DebugLoc &DL,
1221 unsigned O);
1222
1223 /// Creates a FrameIndex SDDbgValue node.
1224 SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr,
1225 unsigned FI, const DebugLoc &DL,
1226 unsigned O);
1227
1228 /// Transfer debug values from one node to another, while optionally
1229 /// generating fragment expressions for split-up values. If \p InvalidateDbg
1230 /// is set, debug values are invalidated after they are transferred.
1231 void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits = 0,
1232 unsigned SizeInBits = 0, bool InvalidateDbg = true);
1233
1234 /// Remove the specified node from the system. If any of its
1235 /// operands then becomes dead, remove them as well. Inform UpdateListener
1236 /// for each node deleted.
1237 void RemoveDeadNode(SDNode *N);
1238
1239 /// This method deletes the unreachable nodes in the
1240 /// given list, and any nodes that become unreachable as a result.
1241 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes);
1242
1243 /// Modify anything using 'From' to use 'To' instead.
1244 /// This can cause recursive merging of nodes in the DAG. Use the first
1245 /// version if 'From' is known to have a single result, use the second
1246 /// if you have two nodes with identical results (or if 'To' has a superset
1247 /// of the results of 'From'), use the third otherwise.
1248 ///
1249 /// These methods all take an optional UpdateListener, which (if not null) is
1250 /// informed about nodes that are deleted and modified due to recursive
1251 /// changes in the dag.
1252 ///
1253 /// These functions only replace all existing uses. It's possible that as
1254 /// these replacements are being performed, CSE may cause the From node
1255 /// to be given new uses. These new uses of From are left in place, and
1256 /// not automatically transferred to To.
1257 ///
1258 void ReplaceAllUsesWith(SDValue From, SDValue Op);
1259 void ReplaceAllUsesWith(SDNode *From, SDNode *To);
1260 void ReplaceAllUsesWith(SDNode *From, const SDValue *To);
1261
1262 /// Replace any uses of From with To, leaving
1263 /// uses of other values produced by From.getNode() alone.
1264 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To);
1265
1266 /// Like ReplaceAllUsesOfValueWith, but for multiple values at once.
1267 /// This correctly handles the case where
1268 /// there is an overlap between the From values and the To values.
1269 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
1270 unsigned Num);
1271
1272 /// If an existing load has uses of its chain, create a token factor node with
1273 /// that chain and the new memory node's chain and update users of the old
1274 /// chain to the token factor. This ensures that the new memory node will have
1275 /// the same relative memory dependency position as the old load. Returns the
1276 /// new merged load chain.
1277 SDValue makeEquivalentMemoryOrdering(LoadSDNode *Old, SDValue New);
1278
1279 /// Topological-sort the AllNodes list and a
1280 /// assign a unique node id for each node in the DAG based on their
1281 /// topological order. Returns the number of nodes.
1282 unsigned AssignTopologicalOrder();
1283
1284 /// Move node N in the AllNodes list to be immediately
1285 /// before the given iterator Position. This may be used to update the
1286 /// topological ordering when the list of nodes is modified.
1287 void RepositionNode(allnodes_iterator Position, SDNode *N) {
1288 AllNodes.insert(Position, AllNodes.remove(N));
1289 }
1290
1291 /// Returns an APFloat semantics tag appropriate for the given type. If VT is
1292 /// a vector type, the element semantics are returned.
1293 static const fltSemantics &EVTToAPFloatSemantics(EVT VT) {
1294 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
1295 default: llvm_unreachable("Unknown FP format");
1296 case MVT::f16: return APFloat::IEEEhalf();
1297 case MVT::f32: return APFloat::IEEEsingle();
1298 case MVT::f64: return APFloat::IEEEdouble();
1299 case MVT::f80: return APFloat::x87DoubleExtended();
1300 case MVT::f128: return APFloat::IEEEquad();
1301 case MVT::ppcf128: return APFloat::PPCDoubleDouble();
1302 }
1303 }
1304
1305 /// Add a dbg_value SDNode. If SD is non-null that means the
1306 /// value is produced by SD.
1307 void AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter);
1308
1309 /// Get the debug values which reference the given SDNode.
1310 ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) {
1311 return DbgInfo->getSDDbgValues(SD);
1312 }
1313
1314public:
1315 /// Return true if there are any SDDbgValue nodes associated
1316 /// with this SelectionDAG.
1317 bool hasDebugValues() const { return !DbgInfo->empty(); }
1318
1319 SDDbgInfo::DbgIterator DbgBegin() { return DbgInfo->DbgBegin(); }
1320 SDDbgInfo::DbgIterator DbgEnd() { return DbgInfo->DbgEnd(); }
1321
1322 SDDbgInfo::DbgIterator ByvalParmDbgBegin() {
1323 return DbgInfo->ByvalParmDbgBegin();
1324 }
1325
1326 SDDbgInfo::DbgIterator ByvalParmDbgEnd() {
1327 return DbgInfo->ByvalParmDbgEnd();
1328 }
1329
1330 /// To be invoked on an SDNode that is slated to be erased. This
1331 /// function mirrors \c llvm::salvageDebugInfo.
1332 void salvageDebugInfo(SDNode &N);
1333
1334 void dump() const;
1335
1336 /// Create a stack temporary, suitable for holding the specified value type.
1337 /// If minAlign is specified, the slot size will have at least that alignment.
1338 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
1339
1340 /// Create a stack temporary suitable for holding either of the specified
1341 /// value types.
1342 SDValue CreateStackTemporary(EVT VT1, EVT VT2);
1343
1344 SDValue FoldSymbolOffset(unsigned Opcode, EVT VT,
1345 const GlobalAddressSDNode *GA,
1346 const SDNode *N2);
1347
1348 SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT,
1349 SDNode *Cst1, SDNode *Cst2);
1350
1351 SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT,
1352 const ConstantSDNode *Cst1,
1353 const ConstantSDNode *Cst2);
1354
1355 SDValue FoldConstantVectorArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT,
1356 ArrayRef<SDValue> Ops,
1357 const SDNodeFlags Flags = SDNodeFlags());
1358
1359 /// Constant fold a setcc to true or false.
1360 SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond,
1361 const SDLoc &dl);
1362
1363 /// See if the specified operand can be simplified with the knowledge that only
1364 /// the bits specified by Mask are used. If so, return the simpler operand,
1365 /// otherwise return a null SDValue.
1366 ///
1367 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can
1368 /// simplify nodes with multiple uses more aggressively.)
1369 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
1370
1371 /// Return true if the sign bit of Op is known to be zero.
1372 /// We use this predicate to simplify operations downstream.
1373 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
1374
1375 /// Return true if 'Op & Mask' is known to be zero. We
1376 /// use this predicate to simplify operations downstream. Op and Mask are
1377 /// known to be the same type.
1378 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
1379 const;
1380
1381 /// Determine which bits of Op are known to be either zero or one and return
1382 /// them in Known. For vectors, the known bits are those that are shared by
1383 /// every vector element.
1384 /// Targets can implement the computeKnownBitsForTargetNode method in the
1385 /// TargetLowering class to allow target nodes to be understood.
1386 void computeKnownBits(SDValue Op, KnownBits &Known, unsigned Depth = 0) const;
1387
1388 /// Determine which bits of Op are known to be either zero or one and return
1389 /// them in Known. The DemandedElts argument allows us to only collect the
1390 /// known bits that are shared by the requested vector elements.
1391 /// Targets can implement the computeKnownBitsForTargetNode method in the
1392 /// TargetLowering class to allow target nodes to be understood.
1393 void computeKnownBits(SDValue Op, KnownBits &Known, const APInt &DemandedElts,
1394 unsigned Depth = 0) const;
1395
1396 /// Used to represent the possible overflow behavior of an operation.
1397 /// Never: the operation cannot overflow.
1398 /// Always: the operation will always overflow.
1399 /// Sometime: the operation may or may not overflow.
1400 enum OverflowKind {
1401 OFK_Never,
1402 OFK_Sometime,
1403 OFK_Always,
1404 };
1405
1406 /// Determine if the result of the addition of 2 node can overflow.
1407 OverflowKind computeOverflowKind(SDValue N0, SDValue N1) const;
1408
1409 /// Test if the given value is known to have exactly one bit set. This differs
1410 /// from computeKnownBits in that it doesn't necessarily determine which bit
1411 /// is set.
1412 bool isKnownToBeAPowerOfTwo(SDValue Val) const;
1413
1414 /// Return the number of times the sign bit of the register is replicated into
1415 /// the other bits. We know that at least 1 bit is always equal to the sign
1416 /// bit (itself), but other cases can give us information. For example,
1417 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
1418 /// to each other, so we return 3. Targets can implement the
1419 /// ComputeNumSignBitsForTarget method in the TargetLowering class to allow
1420 /// target nodes to be understood.
1421 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
1422
1423 /// Return the number of times the sign bit of the register is replicated into
1424 /// the other bits. We know that at least 1 bit is always equal to the sign
1425 /// bit (itself), but other cases can give us information. For example,
1426 /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
1427 /// to each other, so we return 3. The DemandedElts argument allows
1428 /// us to only collect the minimum sign bits of the requested vector elements.
1429 /// Targets can implement the ComputeNumSignBitsForTarget method in the
1430 /// TargetLowering class to allow target nodes to be understood.
1431 unsigned ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
1432 unsigned Depth = 0) const;
1433
1434 /// Return true if the specified operand is an ISD::ADD with a ConstantSDNode
1435 /// on the right-hand side, or if it is an ISD::OR with a ConstantSDNode that
1436 /// is guaranteed to have the same semantics as an ADD. This handles the
1437 /// equivalence:
1438 /// X|Cst == X+Cst iff X&Cst = 0.
1439 bool isBaseWithConstantOffset(SDValue Op) const;
1440
1441 /// Test whether the given SDValue is known to never be NaN.
1442 bool isKnownNeverNaN(SDValue Op) const;
1443
1444 /// Test whether the given SDValue is known to never be positive or negative
1445 /// zero.
1446 bool isKnownNeverZero(SDValue Op) const;
1447
1448 /// Test whether two SDValues are known to compare equal. This
1449 /// is true if they are the same value, or if one is negative zero and the
1450 /// other positive zero.
1451 bool isEqualTo(SDValue A, SDValue B) const;
1452
1453 /// Return true if A and B have no common bits set. As an example, this can
1454 /// allow an 'add' to be transformed into an 'or'.
1455 bool haveNoCommonBitsSet(SDValue A, SDValue B) const;
1456
1457 /// Utility function used by legalize and lowering to
1458 /// "unroll" a vector operation by splitting out the scalars and operating
1459 /// on each element individually. If the ResNE is 0, fully unroll the vector
1460 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
1461 /// If the ResNE is greater than the width of the vector op, unroll the
1462 /// vector op and fill the end of the resulting vector with UNDEFS.
1463 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
1464
1465 /// Return true if loads are next to each other and can be
1466 /// merged. Check that both are nonvolatile and if LD is loading
1467 /// 'Bytes' bytes from a location that is 'Dist' units away from the
1468 /// location that the 'Base' load is loading from.
1469 bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base,
1470 unsigned Bytes, int Dist) const;
1471
1472 /// Infer alignment of a load / store address. Return 0 if
1473 /// it cannot be inferred.
1474 unsigned InferPtrAlignment(SDValue Ptr) const;
1475
1476 /// Compute the VTs needed for the low/hi parts of a type
1477 /// which is split (or expanded) into two not necessarily identical pieces.
1478 std::pair<EVT, EVT> GetSplitDestVTs(const EVT &VT) const;
1479
1480 /// Split the vector with EXTRACT_SUBVECTOR using the provides
1481 /// VTs and return the low/high part.
1482 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL,
1483 const EVT &LoVT, const EVT &HiVT);
1484
1485 /// Split the vector with EXTRACT_SUBVECTOR and return the low/high part.
1486 std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL) {
1487 EVT LoVT, HiVT;
1488 std::tie(LoVT, HiVT) = GetSplitDestVTs(N.getValueType());
1489 return SplitVector(N, DL, LoVT, HiVT);
1490 }
1491
1492 /// Split the node's operand with EXTRACT_SUBVECTOR and
1493 /// return the low/high part.
1494 std::pair<SDValue, SDValue> SplitVectorOperand(const SDNode *N, unsigned OpNo)
1495 {
1496 return SplitVector(N->getOperand(OpNo), SDLoc(N));
1497 }
1498
1499 /// Append the extracted elements from Start to Count out of the vector Op
1500 /// in Args. If Count is 0, all of the elements will be extracted.
1501 void ExtractVectorElements(SDValue Op, SmallVectorImpl<SDValue> &Args,
1502 unsigned Start = 0, unsigned Count = 0);
1503
1504 /// Compute the default alignment value for the given type.
1505 unsigned getEVTAlignment(EVT MemoryVT) const;
1506
1507 /// Test whether the given value is a constant int or similar node.
1508 SDNode *isConstantIntBuildVectorOrConstantInt(SDValue N);
1509
1510 /// Test whether the given value is a constant FP or similar node.
1511 SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N);
1512
1513 /// \returns true if \p N is any kind of constant or build_vector of
1514 /// constants, int or float. If a vector, it may not necessarily be a splat.
1515 inline bool isConstantValueOfAnyType(SDValue N) {
1516 return isConstantIntBuildVectorOrConstantInt(N) ||
1517 isConstantFPBuildVectorOrConstantFP(N);
1518 }
1519
1520private:
1521 void InsertNode(SDNode *N);
1522 bool RemoveNodeFromCSEMaps(SDNode *N);
1523 void AddModifiedNodeToCSEMaps(SDNode *N);
1524 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
1525 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
1526 void *&InsertPos);
1527 SDNode *FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1528 void *&InsertPos);
1529 SDNode *UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &loc);
1530
1531 void DeleteNodeNotInCSEMaps(SDNode *N);
1532 void DeallocateNode(SDNode *N);
1533
1534 void allnodes_clear();
1535
1536 /// Look up the node specified by ID in CSEMap. If it exists, return it. If
1537 /// not, return the insertion token that will make insertion faster. This
1538 /// overload is for nodes other than Constant or ConstantFP, use the other one
1539 /// for those.
1540 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
1541
1542 /// Look up the node specified by ID in CSEMap. If it exists, return it. If
1543 /// not, return the insertion token that will make insertion faster. Performs
1544 /// additional processing for constant nodes.
1545 SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, const SDLoc &DL,
1546 void *&InsertPos);
1547
1548 /// List of non-single value types.
1549 FoldingSet<SDVTListNode> VTListMap;
1550
1551 /// Maps to auto-CSE operations.
1552 std::vector<CondCodeSDNode*> CondCodeNodes;
1553
1554 std::vector<SDNode*> ValueTypeNodes;
1555 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
1556 StringMap<SDNode*> ExternalSymbols;
1557
1558 std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
1559 DenseMap<MCSymbol *, SDNode *> MCSymbols;
1560};
1561
1562template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
1563 using nodes_iterator = pointer_iterator<SelectionDAG::allnodes_iterator>;
1564
1565 static nodes_iterator nodes_begin(SelectionDAG *G) {
1566 return nodes_iterator(G->allnodes_begin());
1567 }
1568
1569 static nodes_iterator nodes_end(SelectionDAG *G) {
1570 return nodes_iterator(G->allnodes_end());
1571 }
1572};
1573
1574template <class TargetMemSDNode>
1575SDValue SelectionDAG::getTargetMemSDNode(SDVTList VTs,
1576 ArrayRef<SDValue> Ops,
1577 const SDLoc &dl, EVT MemVT,
1578 MachineMemOperand *MMO) {
1579 /// Compose node ID and try to find an existing node.
1580 FoldingSetNodeID ID;
1581 unsigned Opcode =
1582 TargetMemSDNode(dl.getIROrder(), DebugLoc(), VTs, MemVT, MMO).getOpcode();
1583 ID.AddInteger(Opcode);
1584 ID.AddPointer(VTs.VTs);
1585 for (auto& Op : Ops) {
1586 ID.AddPointer(Op.getNode());
1587 ID.AddInteger(Op.getResNo());
1588 }
1589 ID.AddInteger(MemVT.getRawBits());
1590 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
1591 ID.AddInteger(getSyntheticNodeSubclassData<TargetMemSDNode>(
1592 dl.getIROrder(), VTs, MemVT, MMO));
1593
1594 void *IP = nullptr;
1595 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
1596 cast<TargetMemSDNode>(E)->refineAlignment(MMO);
1597 return SDValue(E, 0);
1598 }
1599
1600 /// Existing node was not found. Create a new one.
1601 auto *N = newSDNode<TargetMemSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
1602 MemVT, MMO);
1603 createOperands(N, Ops);
1604 CSEMap.InsertNode(N, IP);
1605 InsertNode(N);
1606 return SDValue(N, 0);
1607}
1608
1609} // end namespace llvm
1610
1611#endif // LLVM_CODEGEN_SELECTIONDAG_H