SelectionDAGBuilder.h revision 353358
1327952Sdim//===- SelectionDAGBuilder.h - Selection-DAG building -----------*- C++ -*-===//
2199989Srdivacky//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6199989Srdivacky//
7199989Srdivacky//===----------------------------------------------------------------------===//
8199989Srdivacky//
9199989Srdivacky// This implements routines for translating from LLVM IR into SelectionDAG IR.
10199989Srdivacky//
11199989Srdivacky//===----------------------------------------------------------------------===//
12199989Srdivacky
13280031Sdim#ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
14280031Sdim#define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
15199989Srdivacky
16280031Sdim#include "StatepointLowering.h"
17199989Srdivacky#include "llvm/ADT/APInt.h"
18327952Sdim#include "llvm/ADT/ArrayRef.h"
19199989Srdivacky#include "llvm/ADT/DenseMap.h"
20353358Sdim#include "llvm/ADT/MapVector.h"
21327952Sdim#include "llvm/ADT/SmallVector.h"
22296417Sdim#include "llvm/Analysis/AliasAnalysis.h"
23327952Sdim#include "llvm/CodeGen/ISDOpcodes.h"
24249423Sdim#include "llvm/CodeGen/SelectionDAG.h"
25199989Srdivacky#include "llvm/CodeGen/SelectionDAGNodes.h"
26353358Sdim#include "llvm/CodeGen/SwitchLoweringUtils.h"
27327952Sdim#include "llvm/CodeGen/TargetLowering.h"
28327952Sdim#include "llvm/CodeGen/ValueTypes.h"
29276479Sdim#include "llvm/IR/CallSite.h"
30327952Sdim#include "llvm/IR/DebugLoc.h"
31327952Sdim#include "llvm/IR/Instruction.h"
32288943Sdim#include "llvm/IR/Statepoint.h"
33327952Sdim#include "llvm/Support/BranchProbability.h"
34327952Sdim#include "llvm/Support/CodeGen.h"
35199989Srdivacky#include "llvm/Support/ErrorHandling.h"
36341825Sdim#include "llvm/Support/MachineValueType.h"
37327952Sdim#include <algorithm>
38327952Sdim#include <cassert>
39327952Sdim#include <cstdint>
40309124Sdim#include <utility>
41199989Srdivacky#include <vector>
42199989Srdivacky
43199989Srdivackynamespace llvm {
44199989Srdivacky
45199989Srdivackyclass AllocaInst;
46327952Sdimclass AtomicCmpXchgInst;
47327952Sdimclass AtomicRMWInst;
48199989Srdivackyclass BasicBlock;
49199989Srdivackyclass BranchInst;
50199989Srdivackyclass CallInst;
51353358Sdimclass CallBrInst;
52327952Sdimclass CatchPadInst;
53327952Sdimclass CatchReturnInst;
54327952Sdimclass CatchSwitchInst;
55327952Sdimclass CleanupPadInst;
56327952Sdimclass CleanupReturnInst;
57327952Sdimclass Constant;
58327952Sdimclass ConstantInt;
59327952Sdimclass ConstrainedFPIntrinsic;
60207618Srdivackyclass DbgValueInst;
61327952Sdimclass DataLayout;
62327952Sdimclass DIExpression;
63327952Sdimclass DILocalVariable;
64327952Sdimclass DILocation;
65327952Sdimclass FenceInst;
66199989Srdivackyclass FunctionLoweringInfo;
67199989Srdivackyclass GCFunctionInfo;
68327952Sdimclass GCRelocateInst;
69327952Sdimclass GCResultInst;
70199989Srdivackyclass IndirectBrInst;
71199989Srdivackyclass InvokeInst;
72327952Sdimclass LandingPadInst;
73327952Sdimclass LLVMContext;
74199989Srdivackyclass LoadInst;
75199989Srdivackyclass MachineBasicBlock;
76199989Srdivackyclass PHINode;
77327952Sdimclass ResumeInst;
78199989Srdivackyclass ReturnInst;
79212904Sdimclass SDDbgValue;
80199989Srdivackyclass StoreInst;
81353358Sdimclass SwiftErrorValueTracking;
82199989Srdivackyclass SwitchInst;
83234353Sdimclass TargetLibraryInfo;
84327952Sdimclass TargetMachine;
85327952Sdimclass Type;
86327952Sdimclass VAArgInst;
87199989Srdivackyclass UnreachableInst;
88327952Sdimclass Use;
89327952Sdimclass User;
90327952Sdimclass Value;
91199989Srdivacky
92199989Srdivacky//===----------------------------------------------------------------------===//
93199989Srdivacky/// SelectionDAGBuilder - This is the common target-independent lowering
94199989Srdivacky/// implementation that is parameterized by a TargetLowering object.
95199989Srdivacky///
96199989Srdivackyclass SelectionDAGBuilder {
97353358Sdim  /// The current instruction being visited.
98327952Sdim  const Instruction *CurInst = nullptr;
99199989Srdivacky
100199989Srdivacky  DenseMap<const Value*, SDValue> NodeMap;
101261991Sdim
102353358Sdim  /// Maps argument value for unused arguments. This is used
103210299Sed  /// to preserve debug information for incoming arguments.
104210299Sed  DenseMap<const Value*, SDValue> UnusedArgNodeMap;
105199989Srdivacky
106353358Sdim  /// Helper type for DanglingDebugInfoMap.
107212904Sdim  class DanglingDebugInfo {
108327952Sdim    const DbgValueInst* DI = nullptr;
109212904Sdim    DebugLoc dl;
110327952Sdim    unsigned SDNodeOrder = 0;
111327952Sdim
112212904Sdim  public:
113327952Sdim    DanglingDebugInfo() = default;
114309124Sdim    DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO)
115309124Sdim        : DI(di), dl(std::move(DL)), SDNodeOrder(SDNO) {}
116327952Sdim
117212904Sdim    const DbgValueInst* getDI() { return DI; }
118212904Sdim    DebugLoc getdl() { return dl; }
119212904Sdim    unsigned getSDNodeOrder() { return SDNodeOrder; }
120212904Sdim  };
121212904Sdim
122353358Sdim  /// Helper type for DanglingDebugInfoMap.
123341825Sdim  typedef std::vector<DanglingDebugInfo> DanglingDebugInfoVector;
124341825Sdim
125353358Sdim  /// Keeps track of dbg_values for which we have not yet seen the referent.
126353358Sdim  /// We defer handling these until we do see it.
127353358Sdim  MapVector<const Value*, DanglingDebugInfoVector> DanglingDebugInfoMap;
128212904Sdim
129201360Srdivackypublic:
130353358Sdim  /// Loads are not emitted to the program immediately.  We bunch them up and
131353358Sdim  /// then emit token factor nodes when possible.  This allows us to get simple
132353358Sdim  /// disambiguation between loads without worrying about alias analysis.
133199989Srdivacky  SmallVector<SDValue, 8> PendingLoads;
134280031Sdim
135280031Sdim  /// State used while lowering a statepoint sequence (gc_statepoint,
136280031Sdim  /// gc_relocate, and gc_result).  See StatepointLowering.hpp/cpp for details.
137280031Sdim  StatepointLoweringState StatepointLowering;
138327952Sdim
139201360Srdivackyprivate:
140353358Sdim  /// CopyToReg nodes that copy values to virtual registers for export to other
141353358Sdim  /// blocks need to be emitted before any terminator instruction, but they have
142353358Sdim  /// no other ordering requirements. We bunch them up and the emit a single
143353358Sdim  /// tokenfactor for them just before terminator instructions.
144199989Srdivacky  SmallVector<SDValue, 8> PendingExports;
145199989Srdivacky
146353358Sdim  /// A unique monotonically increasing number used to order the SDNodes we
147353358Sdim  /// create.
148201360Srdivacky  unsigned SDNodeOrder;
149201360Srdivacky
150288943Sdim  /// Determine the rank by weight of CC in [First,Last]. If CC has more weight
151288943Sdim  /// than each cluster in the range, its rank is 0.
152353358Sdim  unsigned caseClusterRank(const SwitchCG::CaseCluster &CC,
153353358Sdim                           SwitchCG::CaseClusterIt First,
154353358Sdim                           SwitchCG::CaseClusterIt Last);
155288943Sdim
156288943Sdim  /// Emit comparison and split W into two subtrees.
157353358Sdim  void splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
158353358Sdim                     const SwitchCG::SwitchWorkListItem &W, Value *Cond,
159353358Sdim                     MachineBasicBlock *SwitchMBB);
160288943Sdim
161288943Sdim  /// Lower W.
162353358Sdim  void lowerWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond,
163288943Sdim                     MachineBasicBlock *SwitchMBB,
164288943Sdim                     MachineBasicBlock *DefaultMBB);
165288943Sdim
166327952Sdim  /// Peel the top probability case if it exceeds the threshold
167353358Sdim  MachineBasicBlock *
168353358Sdim  peelDominantCaseCluster(const SwitchInst &SI,
169353358Sdim                          SwitchCG::CaseClusterVector &Clusters,
170353358Sdim                          BranchProbability &PeeledCaseProb);
171288943Sdim
172261991Sdim  /// A class which encapsulates all of the information needed to generate a
173261991Sdim  /// stack protector check and signals to isel via its state being initialized
174261991Sdim  /// that a stack protector needs to be generated.
175261991Sdim  ///
176261991Sdim  /// *NOTE* The following is a high level documentation of SelectionDAG Stack
177261991Sdim  /// Protector Generation. The reason that it is placed here is for a lack of
178261991Sdim  /// other good places to stick it.
179261991Sdim  ///
180261991Sdim  /// High Level Overview of SelectionDAG Stack Protector Generation:
181261991Sdim  ///
182261991Sdim  /// Previously, generation of stack protectors was done exclusively in the
183261991Sdim  /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
184261991Sdim  /// splitting basic blocks at the IR level to create the success/failure basic
185261991Sdim  /// blocks in the tail of the basic block in question. As a result of this,
186261991Sdim  /// calls that would have qualified for the sibling call optimization were no
187261991Sdim  /// longer eligible for optimization since said calls were no longer right in
188261991Sdim  /// the "tail position" (i.e. the immediate predecessor of a ReturnInst
189261991Sdim  /// instruction).
190261991Sdim  ///
191261991Sdim  /// Then it was noticed that since the sibling call optimization causes the
192261991Sdim  /// callee to reuse the caller's stack, if we could delay the generation of
193261991Sdim  /// the stack protector check until later in CodeGen after the sibling call
194261991Sdim  /// decision was made, we get both the tail call optimization and the stack
195261991Sdim  /// protector check!
196261991Sdim  ///
197261991Sdim  /// A few goals in solving this problem were:
198261991Sdim  ///
199261991Sdim  ///   1. Preserve the architecture independence of stack protector generation.
200261991Sdim  ///
201261991Sdim  ///   2. Preserve the normal IR level stack protector check for platforms like
202276479Sdim  ///      OpenBSD for which we support platform-specific stack protector
203261991Sdim  ///      generation.
204261991Sdim  ///
205261991Sdim  /// The main problem that guided the present solution is that one can not
206261991Sdim  /// solve this problem in an architecture independent manner at the IR level
207261991Sdim  /// only. This is because:
208261991Sdim  ///
209261991Sdim  ///   1. The decision on whether or not to perform a sibling call on certain
210261991Sdim  ///      platforms (for instance i386) requires lower level information
211261991Sdim  ///      related to available registers that can not be known at the IR level.
212261991Sdim  ///
213261991Sdim  ///   2. Even if the previous point were not true, the decision on whether to
214261991Sdim  ///      perform a tail call is done in LowerCallTo in SelectionDAG which
215261991Sdim  ///      occurs after the Stack Protector Pass. As a result, one would need to
216261991Sdim  ///      put the relevant callinst into the stack protector check success
217261991Sdim  ///      basic block (where the return inst is placed) and then move it back
218261991Sdim  ///      later at SelectionDAG/MI time before the stack protector check if the
219261991Sdim  ///      tail call optimization failed. The MI level option was nixed
220276479Sdim  ///      immediately since it would require platform-specific pattern
221261991Sdim  ///      matching. The SelectionDAG level option was nixed because
222261991Sdim  ///      SelectionDAG only processes one IR level basic block at a time
223261991Sdim  ///      implying one could not create a DAG Combine to move the callinst.
224261991Sdim  ///
225261991Sdim  /// To get around this problem a few things were realized:
226261991Sdim  ///
227261991Sdim  ///   1. While one can not handle multiple IR level basic blocks at the
228261991Sdim  ///      SelectionDAG Level, one can generate multiple machine basic blocks
229261991Sdim  ///      for one IR level basic block. This is how we handle bit tests and
230261991Sdim  ///      switches.
231261991Sdim  ///
232261991Sdim  ///   2. At the MI level, tail calls are represented via a special return
233261991Sdim  ///      MIInst called "tcreturn". Thus if we know the basic block in which we
234261991Sdim  ///      wish to insert the stack protector check, we get the correct behavior
235261991Sdim  ///      by always inserting the stack protector check right before the return
236261991Sdim  ///      statement. This is a "magical transformation" since no matter where
237261991Sdim  ///      the stack protector check intrinsic is, we always insert the stack
238261991Sdim  ///      protector check code at the end of the BB.
239261991Sdim  ///
240261991Sdim  /// Given the aforementioned constraints, the following solution was devised:
241261991Sdim  ///
242261991Sdim  ///   1. On platforms that do not support SelectionDAG stack protector check
243261991Sdim  ///      generation, allow for the normal IR level stack protector check
244261991Sdim  ///      generation to continue.
245261991Sdim  ///
246261991Sdim  ///   2. On platforms that do support SelectionDAG stack protector check
247261991Sdim  ///      generation:
248261991Sdim  ///
249261991Sdim  ///     a. Use the IR level stack protector pass to decide if a stack
250261991Sdim  ///        protector is required/which BB we insert the stack protector check
251261991Sdim  ///        in by reusing the logic already therein. If we wish to generate a
252261991Sdim  ///        stack protector check in a basic block, we place a special IR
253261991Sdim  ///        intrinsic called llvm.stackprotectorcheck right before the BB's
254261991Sdim  ///        returninst or if there is a callinst that could potentially be
255261991Sdim  ///        sibling call optimized, before the call inst.
256261991Sdim  ///
257261991Sdim  ///     b. Then when a BB with said intrinsic is processed, we codegen the BB
258261991Sdim  ///        normally via SelectBasicBlock. In said process, when we visit the
259261991Sdim  ///        stack protector check, we do not actually emit anything into the
260261991Sdim  ///        BB. Instead, we just initialize the stack protector descriptor
261261991Sdim  ///        class (which involves stashing information/creating the success
262261991Sdim  ///        mbbb and the failure mbb if we have not created one for this
263261991Sdim  ///        function yet) and export the guard variable that we are going to
264261991Sdim  ///        compare.
265261991Sdim  ///
266261991Sdim  ///     c. After we finish selecting the basic block, in FinishBasicBlock if
267261991Sdim  ///        the StackProtectorDescriptor attached to the SelectionDAGBuilder is
268309124Sdim  ///        initialized, we produce the validation code with one of these
269309124Sdim  ///        techniques:
270309124Sdim  ///          1) with a call to a guard check function
271309124Sdim  ///          2) with inlined instrumentation
272309124Sdim  ///
273309124Sdim  ///        1) We insert a call to the check function before the terminator.
274309124Sdim  ///
275309124Sdim  ///        2) We first find a splice point in the parent basic block
276261991Sdim  ///        before the terminator and then splice the terminator of said basic
277261991Sdim  ///        block into the success basic block. Then we code-gen a new tail for
278261991Sdim  ///        the parent basic block consisting of the two loads, the comparison,
279261991Sdim  ///        and finally two branches to the success/failure basic blocks. We
280261991Sdim  ///        conclude by code-gening the failure basic block if we have not
281261991Sdim  ///        code-gened it already (all stack protector checks we generate in
282261991Sdim  ///        the same function, use the same failure basic block).
283261991Sdim  class StackProtectorDescriptor {
284261991Sdim  public:
285327952Sdim    StackProtectorDescriptor() = default;
286261991Sdim
287261991Sdim    /// Returns true if all fields of the stack protector descriptor are
288261991Sdim    /// initialized implying that we should/are ready to emit a stack protector.
289261991Sdim    bool shouldEmitStackProtector() const {
290309124Sdim      return ParentMBB && SuccessMBB && FailureMBB;
291261991Sdim    }
292261991Sdim
293309124Sdim    bool shouldEmitFunctionBasedCheckStackProtector() const {
294309124Sdim      return ParentMBB && !SuccessMBB && !FailureMBB;
295309124Sdim    }
296309124Sdim
297261991Sdim    /// Initialize the stack protector descriptor structure for a new basic
298261991Sdim    /// block.
299309124Sdim    void initialize(const BasicBlock *BB, MachineBasicBlock *MBB,
300309124Sdim                    bool FunctionBasedInstrumentation) {
301261991Sdim      // Make sure we are not initialized yet.
302261991Sdim      assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
303261991Sdim             "already initialized!");
304261991Sdim      ParentMBB = MBB;
305309124Sdim      if (!FunctionBasedInstrumentation) {
306309124Sdim        SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true);
307309124Sdim        FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB);
308309124Sdim      }
309261991Sdim    }
310261991Sdim
311261991Sdim    /// Reset state that changes when we handle different basic blocks.
312261991Sdim    ///
313261991Sdim    /// This currently includes:
314261991Sdim    ///
315261991Sdim    /// 1. The specific basic block we are generating a
316261991Sdim    /// stack protector for (ParentMBB).
317261991Sdim    ///
318261991Sdim    /// 2. The successor machine basic block that will contain the tail of
319261991Sdim    /// parent mbb after we create the stack protector check (SuccessMBB). This
320261991Sdim    /// BB is visited only on stack protector check success.
321261991Sdim    void resetPerBBState() {
322276479Sdim      ParentMBB = nullptr;
323276479Sdim      SuccessMBB = nullptr;
324261991Sdim    }
325261991Sdim
326261991Sdim    /// Reset state that only changes when we switch functions.
327261991Sdim    ///
328261991Sdim    /// This currently includes:
329261991Sdim    ///
330261991Sdim    /// 1. FailureMBB since we reuse the failure code path for all stack
331261991Sdim    /// protector checks created in an individual function.
332261991Sdim    ///
333261991Sdim    /// 2.The guard variable since the guard variable we are checking against is
334261991Sdim    /// always the same.
335261991Sdim    void resetPerFunctionState() {
336276479Sdim      FailureMBB = nullptr;
337261991Sdim    }
338261991Sdim
339261991Sdim    MachineBasicBlock *getParentMBB() { return ParentMBB; }
340261991Sdim    MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
341261991Sdim    MachineBasicBlock *getFailureMBB() { return FailureMBB; }
342261991Sdim
343261991Sdim  private:
344261991Sdim    /// The basic block for which we are generating the stack protector.
345261991Sdim    ///
346261991Sdim    /// As a result of stack protector generation, we will splice the
347261991Sdim    /// terminators of this basic block into the successor mbb SuccessMBB and
348261991Sdim    /// replace it with a compare/branch to the successor mbbs
349261991Sdim    /// SuccessMBB/FailureMBB depending on whether or not the stack protector
350261991Sdim    /// was violated.
351327952Sdim    MachineBasicBlock *ParentMBB = nullptr;
352261991Sdim
353261991Sdim    /// A basic block visited on stack protector check success that contains the
354261991Sdim    /// terminators of ParentMBB.
355327952Sdim    MachineBasicBlock *SuccessMBB = nullptr;
356261991Sdim
357261991Sdim    /// This basic block visited on stack protector check failure that will
358261991Sdim    /// contain a call to __stack_chk_fail().
359327952Sdim    MachineBasicBlock *FailureMBB = nullptr;
360261991Sdim
361261991Sdim    /// Add a successor machine basic block to ParentMBB. If the successor mbb
362261991Sdim    /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic
363280031Sdim    /// block will be created. Assign a large weight if IsLikely is true.
364261991Sdim    MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB,
365261991Sdim                                       MachineBasicBlock *ParentMBB,
366280031Sdim                                       bool IsLikely,
367276479Sdim                                       MachineBasicBlock *SuccMBB = nullptr);
368261991Sdim  };
369261991Sdim
370261991Sdimprivate:
371261991Sdim  const TargetMachine &TM;
372327952Sdim
373199989Srdivackypublic:
374276479Sdim  /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling
375276479Sdim  /// nodes without a corresponding SDNode.
376276479Sdim  static const unsigned LowestSDNodeOrder = 1;
377276479Sdim
378199989Srdivacky  SelectionDAG &DAG;
379327952Sdim  const DataLayout *DL = nullptr;
380327952Sdim  AliasAnalysis *AA = nullptr;
381234353Sdim  const TargetLibraryInfo *LibInfo;
382199989Srdivacky
383353358Sdim  class SDAGSwitchLowering : public SwitchCG::SwitchLowering {
384353358Sdim  public:
385353358Sdim    SDAGSwitchLowering(SelectionDAGBuilder *sdb, FunctionLoweringInfo &funcinfo)
386353358Sdim        : SwitchCG::SwitchLowering(funcinfo), SDB(sdb) {}
387327952Sdim
388353358Sdim    virtual void addSuccessorWithProb(
389353358Sdim        MachineBasicBlock *Src, MachineBasicBlock *Dst,
390353358Sdim        BranchProbability Prob = BranchProbability::getUnknown()) override {
391353358Sdim      SDB->addSuccessorWithProb(Src, Dst, Prob);
392353358Sdim    }
393327952Sdim
394353358Sdim  private:
395353358Sdim    SelectionDAGBuilder *SDB;
396353358Sdim  };
397327952Sdim
398353358Sdim  std::unique_ptr<SDAGSwitchLowering> SL;
399353358Sdim
400261991Sdim  /// A StackProtectorDescriptor structure used to communicate stack protector
401261991Sdim  /// information in between SelectBasicBlock and FinishBasicBlock.
402261991Sdim  StackProtectorDescriptor SPDescriptor;
403199989Srdivacky
404199989Srdivacky  // Emit PHI-node-operand constants only once even if used by multiple
405199989Srdivacky  // PHI nodes.
406207618Srdivacky  DenseMap<const Constant *, unsigned> ConstantsOut;
407199989Srdivacky
408353358Sdim  /// Information about the function as a whole.
409199989Srdivacky  FunctionLoweringInfo &FuncInfo;
410199989Srdivacky
411353358Sdim  /// Information about the swifterror values used throughout the function.
412353358Sdim  SwiftErrorValueTracking &SwiftError;
413353358Sdim
414353358Sdim  /// Garbage collection metadata for the function.
415199989Srdivacky  GCFunctionInfo *GFI;
416199989Srdivacky
417353358Sdim  /// Map a landing pad to the call site indexes.
418327952Sdim  DenseMap<MachineBasicBlock *, SmallVector<unsigned, 4>> LPadToCallSiteMap;
419226633Sdim
420353358Sdim  /// This is set to true if a call in the current block has been translated as
421353358Sdim  /// a tail call. In this case, no subsequent DAG nodes should be created.
422327952Sdim  bool HasTailCall = false;
423199989Srdivacky
424199989Srdivacky  LLVMContext *Context;
425199989Srdivacky
426207618Srdivacky  SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
427353358Sdim                      SwiftErrorValueTracking &swifterror, CodeGenOpt::Level ol)
428353358Sdim      : SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), DAG(dag),
429353358Sdim        SL(make_unique<SDAGSwitchLowering>(this, funcinfo)), FuncInfo(funcinfo),
430353358Sdim        SwiftError(swifterror) {}
431199989Srdivacky
432321369Sdim  void init(GCFunctionInfo *gfi, AliasAnalysis *AA,
433234353Sdim            const TargetLibraryInfo *li);
434199989Srdivacky
435321369Sdim  /// Clear out the current SelectionDAG and the associated state and prepare
436321369Sdim  /// this SelectionDAGBuilder object to be used for a new block. This doesn't
437321369Sdim  /// clear out information about additional blocks that are needed to complete
438321369Sdim  /// switch lowering or PHI node updating; that information is cleared out as
439321369Sdim  /// it is consumed.
440199989Srdivacky  void clear();
441199989Srdivacky
442321369Sdim  /// Clear the dangling debug information map. This function is separated from
443321369Sdim  /// the clear so that debug information that is dangling in a basic block can
444321369Sdim  /// be properly resolved in a different basic block. This allows the
445321369Sdim  /// SelectionDAG to resolve dangling debug information attached to PHI nodes.
446223017Sdim  void clearDanglingDebugInfo();
447223017Sdim
448321369Sdim  /// Return the current virtual root of the Selection DAG, flushing any
449321369Sdim  /// PendingLoad items. This must be done before emitting a store or any other
450321369Sdim  /// node that may need to be ordered after any prior load instructions.
451199989Srdivacky  SDValue getRoot();
452199989Srdivacky
453321369Sdim  /// Similar to getRoot, but instead of flushing all the PendingLoad items,
454321369Sdim  /// flush all the PendingExports items. It is necessary to do this before
455321369Sdim  /// emitting a terminator instruction.
456199989Srdivacky  SDValue getControlRoot();
457199989Srdivacky
458261991Sdim  SDLoc getCurSDLoc() const {
459261991Sdim    return SDLoc(CurInst, SDNodeOrder);
460261991Sdim  }
461219077Sdim
462261991Sdim  DebugLoc getCurDebugLoc() const {
463261991Sdim    return CurInst ? CurInst->getDebugLoc() : DebugLoc();
464261991Sdim  }
465261991Sdim
466207618Srdivacky  void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
467199989Srdivacky
468207618Srdivacky  void visit(const Instruction &I);
469199989Srdivacky
470207618Srdivacky  void visit(unsigned Opcode, const User &I);
471199989Srdivacky
472353358Sdim  /// If there was virtual register allocated for the value V emit CopyFromReg
473353358Sdim  /// of the specified type Ty. Return empty SDValue() otherwise.
474288943Sdim  SDValue getCopyFromRegs(const Value *V, Type *Ty);
475288943Sdim
476341825Sdim  /// If we have dangling debug info that describes \p Variable, or an
477341825Sdim  /// overlapping part of variable considering the \p Expr, then this method
478353358Sdim  /// will drop that debug info as it isn't valid any longer.
479341825Sdim  void dropDanglingDebugInfo(const DILocalVariable *Variable,
480341825Sdim                             const DIExpression *Expr);
481341825Sdim
482353358Sdim  /// If we saw an earlier dbg_value referring to V, generate the debug data
483353358Sdim  /// structures now that we've seen its definition.
484212904Sdim  void resolveDanglingDebugInfo(const Value *V, SDValue Val);
485327952Sdim
486353358Sdim  /// For the given dangling debuginfo record, perform last-ditch efforts to
487353358Sdim  /// resolve the debuginfo to something that is represented in this DAG. If
488353358Sdim  /// this cannot be done, produce an Undef debug value record.
489353358Sdim  void salvageUnresolvedDbgValue(DanglingDebugInfo &DDI);
490353358Sdim
491353358Sdim  /// For a given Value, attempt to create and record a SDDbgValue in the
492353358Sdim  /// SelectionDAG.
493353358Sdim  bool handleDebugValue(const Value *V, DILocalVariable *Var,
494353358Sdim                        DIExpression *Expr, DebugLoc CurDL,
495353358Sdim                        DebugLoc InstDL, unsigned Order);
496353358Sdim
497353358Sdim  /// Evict any dangling debug information, attempting to salvage it first.
498353358Sdim  void resolveOrClearDbgInfo();
499353358Sdim
500199989Srdivacky  SDValue getValue(const Value *V);
501288943Sdim  bool findValue(const Value *V) const;
502288943Sdim
503341825Sdim  /// Return the SDNode for the specified IR value if it exists.
504341825Sdim  SDNode *getNodeForIRValue(const Value *V) {
505341825Sdim    if (NodeMap.find(V) == NodeMap.end())
506341825Sdim      return nullptr;
507341825Sdim    return NodeMap[V].getNode();
508341825Sdim  }
509341825Sdim
510210299Sed  SDValue getNonRegisterValue(const Value *V);
511210299Sed  SDValue getValueImpl(const Value *V);
512199989Srdivacky
513199989Srdivacky  void setValue(const Value *V, SDValue NewN) {
514199989Srdivacky    SDValue &N = NodeMap[V];
515276479Sdim    assert(!N.getNode() && "Already set a value for this node!");
516199989Srdivacky    N = NewN;
517199989Srdivacky  }
518261991Sdim
519210299Sed  void setUnusedArgValue(const Value *V, SDValue NewN) {
520210299Sed    SDValue &N = UnusedArgNodeMap[V];
521276479Sdim    assert(!N.getNode() && "Already set a value for this node!");
522210299Sed    N = NewN;
523210299Sed  }
524199989Srdivacky
525207618Srdivacky  void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
526199989Srdivacky                            MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
527296417Sdim                            MachineBasicBlock *SwitchBB,
528341825Sdim                            Instruction::BinaryOps Opc, BranchProbability TProb,
529341825Sdim                            BranchProbability FProb, bool InvertCond);
530207618Srdivacky  void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
531199989Srdivacky                                    MachineBasicBlock *FBB,
532207618Srdivacky                                    MachineBasicBlock *CurBB,
533276479Sdim                                    MachineBasicBlock *SwitchBB,
534341825Sdim                                    BranchProbability TProb, BranchProbability FProb,
535321369Sdim                                    bool InvertCond);
536353358Sdim  bool ShouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases);
537207618Srdivacky  bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
538207618Srdivacky  void CopyToExportRegsIfNeeded(const Value *V);
539207618Srdivacky  void ExportFromCurrentBlock(const Value *V);
540207618Srdivacky  void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
541296417Sdim                   const BasicBlock *EHPadBB = nullptr);
542199989Srdivacky
543309124Sdim  // Lower range metadata from 0 to N to assert zext to an integer of nearest
544309124Sdim  // floor power of two.
545309124Sdim  SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I,
546309124Sdim                                 SDValue Op);
547261991Sdim
548309124Sdim  void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI,
549353358Sdim                                const CallBase *Call, unsigned ArgIdx,
550309124Sdim                                unsigned NumArgs, SDValue Callee,
551309124Sdim                                Type *ReturnTy, bool IsPatchPoint);
552309124Sdim
553309124Sdim  std::pair<SDValue, SDValue>
554309124Sdim  lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
555309124Sdim                 const BasicBlock *EHPadBB = nullptr);
556309124Sdim
557353358Sdim  /// When an MBB was split during scheduling, update the
558276479Sdim  /// references that need to refer to the last resulting block.
559218893Sdim  void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
560218893Sdim
561309124Sdim  /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes
562309124Sdim  /// of lowering into a STATEPOINT node.
563309124Sdim  struct StatepointLoweringInfo {
564309124Sdim    /// Bases[i] is the base pointer for Ptrs[i].  Together they denote the set
565309124Sdim    /// of gc pointers this STATEPOINT has to relocate.
566309124Sdim    SmallVector<const Value *, 16> Bases;
567309124Sdim    SmallVector<const Value *, 16> Ptrs;
568309124Sdim
569309124Sdim    /// The set of gc.relocate calls associated with this gc.statepoint.
570309124Sdim    SmallVector<const GCRelocateInst *, 16> GCRelocates;
571309124Sdim
572309124Sdim    /// The full list of gc arguments to the gc.statepoint being lowered.
573309124Sdim    ArrayRef<const Use> GCArgs;
574309124Sdim
575309124Sdim    /// The gc.statepoint instruction.
576309124Sdim    const Instruction *StatepointInstr = nullptr;
577309124Sdim
578309124Sdim    /// The list of gc transition arguments present in the gc.statepoint being
579309124Sdim    /// lowered.
580309124Sdim    ArrayRef<const Use> GCTransitionArgs;
581309124Sdim
582309124Sdim    /// The ID that the resulting STATEPOINT instruction has to report.
583309124Sdim    unsigned ID = -1;
584309124Sdim
585309124Sdim    /// Information regarding the underlying call instruction.
586309124Sdim    TargetLowering::CallLoweringInfo CLI;
587309124Sdim
588309124Sdim    /// The deoptimization state associated with this gc.statepoint call, if
589309124Sdim    /// any.
590309124Sdim    ArrayRef<const Use> DeoptState;
591309124Sdim
592309124Sdim    /// Flags associated with the meta arguments being lowered.
593309124Sdim    uint64_t StatepointFlags = -1;
594309124Sdim
595309124Sdim    /// The number of patchable bytes the call needs to get lowered into.
596309124Sdim    unsigned NumPatchBytes = -1;
597309124Sdim
598309124Sdim    /// The exception handling unwind destination, in case this represents an
599309124Sdim    /// invoke of gc.statepoint.
600309124Sdim    const BasicBlock *EHPadBB = nullptr;
601309124Sdim
602309124Sdim    explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {}
603309124Sdim  };
604309124Sdim
605309124Sdim  /// Lower \p SLI into a STATEPOINT instruction.
606341825Sdim  SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SI);
607309124Sdim
608288943Sdim  // This function is responsible for the whole statepoint lowering process.
609288943Sdim  // It uniformly handles invoke and call statepoints.
610341825Sdim  void LowerStatepoint(ImmutableStatepoint ISP,
611296417Sdim                       const BasicBlock *EHPadBB = nullptr);
612309124Sdim
613353358Sdim  void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee,
614309124Sdim                                    const BasicBlock *EHPadBB);
615309124Sdim
616309124Sdim  void LowerDeoptimizeCall(const CallInst *CI);
617309124Sdim  void LowerDeoptimizingReturn();
618309124Sdim
619353358Sdim  void LowerCallSiteWithDeoptBundleImpl(const CallBase *Call, SDValue Callee,
620309124Sdim                                        const BasicBlock *EHPadBB,
621309124Sdim                                        bool VarArgDisallowed,
622309124Sdim                                        bool ForceVoidReturnTy);
623309124Sdim
624321369Sdim  /// Returns the type of FrameIndex and TargetFrameIndex nodes.
625321369Sdim  MVT getFrameIndexTy() {
626321369Sdim    return DAG.getTargetLoweringInfo().getFrameIndexTy(DAG.getDataLayout());
627321369Sdim  }
628321369Sdim
629199989Srdivackyprivate:
630199989Srdivacky  // Terminator instructions.
631207618Srdivacky  void visitRet(const ReturnInst &I);
632207618Srdivacky  void visitBr(const BranchInst &I);
633207618Srdivacky  void visitSwitch(const SwitchInst &I);
634207618Srdivacky  void visitIndirectBr(const IndirectBrInst &I);
635276479Sdim  void visitUnreachable(const UnreachableInst &I);
636296417Sdim  void visitCleanupRet(const CleanupReturnInst &I);
637296417Sdim  void visitCatchSwitch(const CatchSwitchInst &I);
638296417Sdim  void visitCatchRet(const CatchReturnInst &I);
639296417Sdim  void visitCatchPad(const CatchPadInst &I);
640296417Sdim  void visitCleanupPad(const CleanupPadInst &CPI);
641199989Srdivacky
642296417Sdim  BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
643296417Sdim                                       const MachineBasicBlock *Dst) const;
644296417Sdim  void addSuccessorWithProb(
645296417Sdim      MachineBasicBlock *Src, MachineBasicBlock *Dst,
646296417Sdim      BranchProbability Prob = BranchProbability::getUnknown());
647296417Sdim
648199989Srdivackypublic:
649353358Sdim  void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB);
650261991Sdim  void visitSPDescriptorParent(StackProtectorDescriptor &SPD,
651261991Sdim                               MachineBasicBlock *ParentBB);
652261991Sdim  void visitSPDescriptorFailure(StackProtectorDescriptor &SPD);
653353358Sdim  void visitBitTestHeader(SwitchCG::BitTestBlock &B,
654353358Sdim                          MachineBasicBlock *SwitchBB);
655353358Sdim  void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB,
656353358Sdim                        BranchProbability BranchProbToNext, unsigned Reg,
657353358Sdim                        SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB);
658353358Sdim  void visitJumpTable(SwitchCG::JumpTable &JT);
659353358Sdim  void visitJumpTableHeader(SwitchCG::JumpTable &JT,
660353358Sdim                            SwitchCG::JumpTableHeader &JTH,
661207618Srdivacky                            MachineBasicBlock *SwitchBB);
662261991Sdim
663199989Srdivackyprivate:
664199989Srdivacky  // These all get lowered before this pass.
665207618Srdivacky  void visitInvoke(const InvokeInst &I);
666353358Sdim  void visitCallBr(const CallBrInst &I);
667226633Sdim  void visitResume(const ResumeInst &I);
668199989Srdivacky
669344779Sdim  void visitUnary(const User &I, unsigned Opcode);
670344779Sdim  void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); }
671344779Sdim
672341825Sdim  void visitBinary(const User &I, unsigned Opcode);
673207618Srdivacky  void visitShift(const User &I, unsigned Opcode);
674207618Srdivacky  void visitAdd(const User &I)  { visitBinary(I, ISD::ADD); }
675207618Srdivacky  void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
676207618Srdivacky  void visitSub(const User &I)  { visitBinary(I, ISD::SUB); }
677207618Srdivacky  void visitFSub(const User &I);
678207618Srdivacky  void visitMul(const User &I)  { visitBinary(I, ISD::MUL); }
679207618Srdivacky  void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
680207618Srdivacky  void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
681207618Srdivacky  void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
682207618Srdivacky  void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
683207618Srdivacky  void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
684224145Sdim  void visitSDiv(const User &I);
685207618Srdivacky  void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
686207618Srdivacky  void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
687207618Srdivacky  void visitOr  (const User &I) { visitBinary(I, ISD::OR); }
688207618Srdivacky  void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
689207618Srdivacky  void visitShl (const User &I) { visitShift(I, ISD::SHL); }
690207618Srdivacky  void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
691207618Srdivacky  void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
692207618Srdivacky  void visitICmp(const User &I);
693207618Srdivacky  void visitFCmp(const User &I);
694199989Srdivacky  // Visit the conversion instructions
695207618Srdivacky  void visitTrunc(const User &I);
696207618Srdivacky  void visitZExt(const User &I);
697207618Srdivacky  void visitSExt(const User &I);
698207618Srdivacky  void visitFPTrunc(const User &I);
699207618Srdivacky  void visitFPExt(const User &I);
700207618Srdivacky  void visitFPToUI(const User &I);
701207618Srdivacky  void visitFPToSI(const User &I);
702207618Srdivacky  void visitUIToFP(const User &I);
703207618Srdivacky  void visitSIToFP(const User &I);
704207618Srdivacky  void visitPtrToInt(const User &I);
705207618Srdivacky  void visitIntToPtr(const User &I);
706207618Srdivacky  void visitBitCast(const User &I);
707261991Sdim  void visitAddrSpaceCast(const User &I);
708199989Srdivacky
709207618Srdivacky  void visitExtractElement(const User &I);
710207618Srdivacky  void visitInsertElement(const User &I);
711207618Srdivacky  void visitShuffleVector(const User &I);
712199989Srdivacky
713321369Sdim  void visitExtractValue(const User &I);
714321369Sdim  void visitInsertValue(const User &I);
715341825Sdim  void visitLandingPad(const LandingPadInst &LP);
716199989Srdivacky
717207618Srdivacky  void visitGetElementPtr(const User &I);
718207618Srdivacky  void visitSelect(const User &I);
719199989Srdivacky
720207618Srdivacky  void visitAlloca(const AllocaInst &I);
721207618Srdivacky  void visitLoad(const LoadInst &I);
722207618Srdivacky  void visitStore(const StoreInst &I);
723314564Sdim  void visitMaskedLoad(const CallInst &I, bool IsExpanding = false);
724314564Sdim  void visitMaskedStore(const CallInst &I, bool IsCompressing = false);
725288943Sdim  void visitMaskedGather(const CallInst &I);
726288943Sdim  void visitMaskedScatter(const CallInst &I);
727226633Sdim  void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
728226633Sdim  void visitAtomicRMW(const AtomicRMWInst &I);
729226633Sdim  void visitFence(const FenceInst &I);
730207618Srdivacky  void visitPHI(const PHINode &I);
731207618Srdivacky  void visitCall(const CallInst &I);
732207618Srdivacky  bool visitMemCmpCall(const CallInst &I);
733314564Sdim  bool visitMemPCpyCall(const CallInst &I);
734261991Sdim  bool visitMemChrCall(const CallInst &I);
735261991Sdim  bool visitStrCpyCall(const CallInst &I, bool isStpcpy);
736261991Sdim  bool visitStrCmpCall(const CallInst &I);
737261991Sdim  bool visitStrLenCall(const CallInst &I);
738261991Sdim  bool visitStrNLenCall(const CallInst &I);
739239462Sdim  bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
740280031Sdim  bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode);
741226633Sdim  void visitAtomicLoad(const LoadInst &I);
742226633Sdim  void visitAtomicStore(const StoreInst &I);
743309124Sdim  void visitLoadFromSwiftError(const LoadInst &I);
744309124Sdim  void visitStoreToSwiftError(const StoreInst &I);
745226633Sdim
746207618Srdivacky  void visitInlineAsm(ImmutableCallSite CS);
747353358Sdim  void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
748207618Srdivacky  void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
749321369Sdim  void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI);
750199989Srdivacky
751207618Srdivacky  void visitVAStart(const CallInst &I);
752207618Srdivacky  void visitVAArg(const VAArgInst &I);
753207618Srdivacky  void visitVAEnd(const CallInst &I);
754207618Srdivacky  void visitVACopy(const CallInst &I);
755261991Sdim  void visitStackmap(const CallInst &I);
756280031Sdim  void visitPatchpoint(ImmutableCallSite CS,
757296417Sdim                       const BasicBlock *EHPadBB = nullptr);
758199989Srdivacky
759309124Sdim  // These two are implemented in StatepointLowering.cpp
760341825Sdim  void visitGCRelocate(const GCRelocateInst &Relocate);
761309124Sdim  void visitGCResult(const GCResultInst &I);
762280031Sdim
763321369Sdim  void visitVectorReduce(const CallInst &I, unsigned Intrinsic);
764321369Sdim
765207618Srdivacky  void visitUserOp1(const Instruction &I) {
766199989Srdivacky    llvm_unreachable("UserOp1 should not exist at instruction selection time!");
767199989Srdivacky  }
768207618Srdivacky  void visitUserOp2(const Instruction &I) {
769199989Srdivacky    llvm_unreachable("UserOp2 should not exist at instruction selection time!");
770199989Srdivacky  }
771207618Srdivacky
772261991Sdim  void processIntegerCallValue(const Instruction &I,
773261991Sdim                               SDValue Value, bool IsSigned);
774261991Sdim
775207618Srdivacky  void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
776207618Srdivacky
777309124Sdim  void emitInlineAsmError(ImmutableCallSite CS, const Twine &Message);
778309124Sdim
779327952Sdim  /// If V is an function argument then create corresponding DBG_VALUE machine
780327952Sdim  /// instruction for it now. At the end of instruction selection, they will be
781327952Sdim  /// inserted to the entry BB.
782288943Sdim  bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable,
783288943Sdim                                DIExpression *Expr, DILocation *DL,
784327952Sdim                                bool IsDbgDeclare, const SDValue &N);
785288943Sdim
786288943Sdim  /// Return the next block after MBB, or nullptr if there is none.
787288943Sdim  MachineBasicBlock *NextBlock(MachineBasicBlock *MBB);
788288943Sdim
789288943Sdim  /// Update the DAG and DAG builder with the relevant information after
790288943Sdim  /// a new root node has been created which could be a tail call.
791288943Sdim  void updateDAGForMaybeTailCall(SDValue MaybeTC);
792314564Sdim
793314564Sdim  /// Return the appropriate SDDbgValue based on N.
794314564Sdim  SDDbgValue *getDbgValue(SDValue N, DILocalVariable *Variable,
795327952Sdim                          DIExpression *Expr, const DebugLoc &dl,
796327952Sdim                          unsigned DbgSDNodeOrder);
797353358Sdim
798353358Sdim  /// Lowers CallInst to an external symbol.
799353358Sdim  void lowerCallToExternalSymbol(const CallInst &I, const char *FunctionName);
800199989Srdivacky};
801199989Srdivacky
802353358Sdim/// This struct represents the registers (physical or virtual)
803288943Sdim/// that a particular set of values is assigned, and the type information about
804288943Sdim/// the value. The most common situation is to represent one value at a time,
805288943Sdim/// but struct or array values are handled element-wise as multiple values.  The
806288943Sdim/// splitting of aggregates is performed recursively, so that we never have
807288943Sdim/// aggregate-typed registers. The values at this point do not necessarily have
808288943Sdim/// legal types, so each value may require one or more registers of some legal
809288943Sdim/// type.
810288943Sdim///
811288943Sdimstruct RegsForValue {
812321369Sdim  /// The value types of the values, which may not be legal, and
813288943Sdim  /// may need be promoted or synthesized from one or more registers.
814288943Sdim  SmallVector<EVT, 4> ValueVTs;
815288943Sdim
816321369Sdim  /// The value types of the registers. This is the same size as ValueVTs and it
817321369Sdim  /// records, for each value, what the type of the assigned register or
818321369Sdim  /// registers are. (Individual values are never synthesized from more than one
819321369Sdim  /// type of register.)
820288943Sdim  ///
821288943Sdim  /// With virtual registers, the contents of RegVTs is redundant with TLI's
822288943Sdim  /// getRegisterType member function, however when with physical registers
823288943Sdim  /// it is necessary to have a separate record of the types.
824288943Sdim  SmallVector<MVT, 4> RegVTs;
825288943Sdim
826321369Sdim  /// This list holds the registers assigned to the values.
827288943Sdim  /// Each legal or promoted value requires one register, and each
828288943Sdim  /// expanded value requires multiple registers.
829288943Sdim  SmallVector<unsigned, 4> Regs;
830288943Sdim
831321369Sdim  /// This list holds the number of registers for each value.
832321369Sdim  SmallVector<unsigned, 4> RegCount;
833321369Sdim
834321369Sdim  /// Records if this value needs to be treated in an ABI dependant manner,
835321369Sdim  /// different to normal type legalization.
836341825Sdim  Optional<CallingConv::ID> CallConv;
837321369Sdim
838327952Sdim  RegsForValue() = default;
839321369Sdim  RegsForValue(const SmallVector<unsigned, 4> &regs, MVT regvt, EVT valuevt,
840341825Sdim               Optional<CallingConv::ID> CC = None);
841288943Sdim  RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
842321369Sdim               const DataLayout &DL, unsigned Reg, Type *Ty,
843341825Sdim               Optional<CallingConv::ID> CC);
844288943Sdim
845341825Sdim  bool isABIMangled() const {
846341825Sdim    return CallConv.hasValue();
847341825Sdim  }
848341825Sdim
849321369Sdim  /// Add the specified values to this one.
850288943Sdim  void append(const RegsForValue &RHS) {
851288943Sdim    ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
852288943Sdim    RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
853288943Sdim    Regs.append(RHS.Regs.begin(), RHS.Regs.end());
854321369Sdim    RegCount.push_back(RHS.Regs.size());
855288943Sdim  }
856288943Sdim
857321369Sdim  /// Emit a series of CopyFromReg nodes that copies from this value and returns
858321369Sdim  /// the result as a ValueVTs value. This uses Chain/Flag as the input and
859321369Sdim  /// updates them for the output Chain/Flag. If the Flag pointer is NULL, no
860321369Sdim  /// flag is used.
861288943Sdim  SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
862309124Sdim                          const SDLoc &dl, SDValue &Chain, SDValue *Flag,
863288943Sdim                          const Value *V = nullptr) const;
864288943Sdim
865321369Sdim  /// Emit a series of CopyToReg nodes that copies the specified value into the
866321369Sdim  /// registers specified by this object. This uses Chain/Flag as the input and
867321369Sdim  /// updates them for the output Chain/Flag. If the Flag pointer is nullptr, no
868321369Sdim  /// flag is used. If V is not nullptr, then it is used in printing better
869321369Sdim  /// diagnostic messages on error.
870309124Sdim  void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl,
871309124Sdim                     SDValue &Chain, SDValue *Flag, const Value *V = nullptr,
872309124Sdim                     ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const;
873288943Sdim
874321369Sdim  /// Add this value to the specified inlineasm node operand list. This adds the
875321369Sdim  /// code marker, matching input operand index (if applicable), and includes
876321369Sdim  /// the number of values added into it.
877341825Sdim  void AddInlineAsmOperands(unsigned Code, bool HasMatching,
878309124Sdim                            unsigned MatchingIdx, const SDLoc &dl,
879309124Sdim                            SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
880341825Sdim
881341825Sdim  /// Check if the total RegCount is greater than one.
882341825Sdim  bool occupiesMultipleRegs() const {
883341825Sdim    return std::accumulate(RegCount.begin(), RegCount.end(), 0) > 1;
884341825Sdim  }
885341825Sdim
886341825Sdim  /// Return a list of registers and their sizes.
887341825Sdim  SmallVector<std::pair<unsigned, unsigned>, 4> getRegsAndSizes() const;
888288943Sdim};
889288943Sdim
890199989Srdivacky} // end namespace llvm
891199989Srdivacky
892327952Sdim#endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
893