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