xref: /aosp_15_r20/external/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This implements routines for translating from LLVM IR into SelectionDAG IR.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
15*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "StatepointLowering.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/APInt.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseMap.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAG.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAGNodes.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Statepoint.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
28*9880d681SAndroid Build Coastguard Worker #include <utility>
29*9880d681SAndroid Build Coastguard Worker #include <vector>
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker namespace llvm {
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker class AddrSpaceCastInst;
34*9880d681SAndroid Build Coastguard Worker class AllocaInst;
35*9880d681SAndroid Build Coastguard Worker class BasicBlock;
36*9880d681SAndroid Build Coastguard Worker class BitCastInst;
37*9880d681SAndroid Build Coastguard Worker class BranchInst;
38*9880d681SAndroid Build Coastguard Worker class CallInst;
39*9880d681SAndroid Build Coastguard Worker class DbgValueInst;
40*9880d681SAndroid Build Coastguard Worker class ExtractElementInst;
41*9880d681SAndroid Build Coastguard Worker class ExtractValueInst;
42*9880d681SAndroid Build Coastguard Worker class FCmpInst;
43*9880d681SAndroid Build Coastguard Worker class FPExtInst;
44*9880d681SAndroid Build Coastguard Worker class FPToSIInst;
45*9880d681SAndroid Build Coastguard Worker class FPToUIInst;
46*9880d681SAndroid Build Coastguard Worker class FPTruncInst;
47*9880d681SAndroid Build Coastguard Worker class Function;
48*9880d681SAndroid Build Coastguard Worker class FunctionLoweringInfo;
49*9880d681SAndroid Build Coastguard Worker class GetElementPtrInst;
50*9880d681SAndroid Build Coastguard Worker class GCFunctionInfo;
51*9880d681SAndroid Build Coastguard Worker class ICmpInst;
52*9880d681SAndroid Build Coastguard Worker class IntToPtrInst;
53*9880d681SAndroid Build Coastguard Worker class IndirectBrInst;
54*9880d681SAndroid Build Coastguard Worker class InvokeInst;
55*9880d681SAndroid Build Coastguard Worker class InsertElementInst;
56*9880d681SAndroid Build Coastguard Worker class InsertValueInst;
57*9880d681SAndroid Build Coastguard Worker class Instruction;
58*9880d681SAndroid Build Coastguard Worker class LoadInst;
59*9880d681SAndroid Build Coastguard Worker class MachineBasicBlock;
60*9880d681SAndroid Build Coastguard Worker class MachineInstr;
61*9880d681SAndroid Build Coastguard Worker class MachineRegisterInfo;
62*9880d681SAndroid Build Coastguard Worker class MDNode;
63*9880d681SAndroid Build Coastguard Worker class MVT;
64*9880d681SAndroid Build Coastguard Worker class PHINode;
65*9880d681SAndroid Build Coastguard Worker class PtrToIntInst;
66*9880d681SAndroid Build Coastguard Worker class ReturnInst;
67*9880d681SAndroid Build Coastguard Worker class SDDbgValue;
68*9880d681SAndroid Build Coastguard Worker class SExtInst;
69*9880d681SAndroid Build Coastguard Worker class SelectInst;
70*9880d681SAndroid Build Coastguard Worker class ShuffleVectorInst;
71*9880d681SAndroid Build Coastguard Worker class SIToFPInst;
72*9880d681SAndroid Build Coastguard Worker class StoreInst;
73*9880d681SAndroid Build Coastguard Worker class SwitchInst;
74*9880d681SAndroid Build Coastguard Worker class DataLayout;
75*9880d681SAndroid Build Coastguard Worker class TargetLibraryInfo;
76*9880d681SAndroid Build Coastguard Worker class TargetLowering;
77*9880d681SAndroid Build Coastguard Worker class TruncInst;
78*9880d681SAndroid Build Coastguard Worker class UIToFPInst;
79*9880d681SAndroid Build Coastguard Worker class UnreachableInst;
80*9880d681SAndroid Build Coastguard Worker class VAArgInst;
81*9880d681SAndroid Build Coastguard Worker class ZExtInst;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
84*9880d681SAndroid Build Coastguard Worker /// SelectionDAGBuilder - This is the common target-independent lowering
85*9880d681SAndroid Build Coastguard Worker /// implementation that is parameterized by a TargetLowering object.
86*9880d681SAndroid Build Coastguard Worker ///
87*9880d681SAndroid Build Coastguard Worker class SelectionDAGBuilder {
88*9880d681SAndroid Build Coastguard Worker   /// CurInst - The current instruction being visited
89*9880d681SAndroid Build Coastguard Worker   const Instruction *CurInst;
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker   DenseMap<const Value*, SDValue> NodeMap;
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used
94*9880d681SAndroid Build Coastguard Worker   /// to preserve debug information for incoming arguments.
95*9880d681SAndroid Build Coastguard Worker   DenseMap<const Value*, SDValue> UnusedArgNodeMap;
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker   /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap.
98*9880d681SAndroid Build Coastguard Worker   class DanglingDebugInfo {
99*9880d681SAndroid Build Coastguard Worker     const DbgValueInst* DI;
100*9880d681SAndroid Build Coastguard Worker     DebugLoc dl;
101*9880d681SAndroid Build Coastguard Worker     unsigned SDNodeOrder;
102*9880d681SAndroid Build Coastguard Worker   public:
DanglingDebugInfo()103*9880d681SAndroid Build Coastguard Worker     DanglingDebugInfo() : DI(nullptr), dl(DebugLoc()), SDNodeOrder(0) { }
DanglingDebugInfo(const DbgValueInst * di,DebugLoc DL,unsigned SDNO)104*9880d681SAndroid Build Coastguard Worker     DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO)
105*9880d681SAndroid Build Coastguard Worker         : DI(di), dl(std::move(DL)), SDNodeOrder(SDNO) {}
getDI()106*9880d681SAndroid Build Coastguard Worker     const DbgValueInst* getDI() { return DI; }
getdl()107*9880d681SAndroid Build Coastguard Worker     DebugLoc getdl() { return dl; }
getSDNodeOrder()108*9880d681SAndroid Build Coastguard Worker     unsigned getSDNodeOrder() { return SDNodeOrder; }
109*9880d681SAndroid Build Coastguard Worker   };
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not
112*9880d681SAndroid Build Coastguard Worker   /// yet seen the referent.  We defer handling these until we do see it.
113*9880d681SAndroid Build Coastguard Worker   DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap;
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker public:
116*9880d681SAndroid Build Coastguard Worker   /// PendingLoads - Loads are not emitted to the program immediately.  We bunch
117*9880d681SAndroid Build Coastguard Worker   /// them up and then emit token factor nodes when possible.  This allows us to
118*9880d681SAndroid Build Coastguard Worker   /// get simple disambiguation between loads without worrying about alias
119*9880d681SAndroid Build Coastguard Worker   /// analysis.
120*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> PendingLoads;
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   /// State used while lowering a statepoint sequence (gc_statepoint,
123*9880d681SAndroid Build Coastguard Worker   /// gc_relocate, and gc_result).  See StatepointLowering.hpp/cpp for details.
124*9880d681SAndroid Build Coastguard Worker   StatepointLoweringState StatepointLowering;
125*9880d681SAndroid Build Coastguard Worker private:
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker   /// PendingExports - CopyToReg nodes that copy values to virtual registers
128*9880d681SAndroid Build Coastguard Worker   /// for export to other blocks need to be emitted before any terminator
129*9880d681SAndroid Build Coastguard Worker   /// instruction, but they have no other ordering requirements. We bunch them
130*9880d681SAndroid Build Coastguard Worker   /// up and the emit a single tokenfactor for them just before terminator
131*9880d681SAndroid Build Coastguard Worker   /// instructions.
132*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> PendingExports;
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker   /// SDNodeOrder - A unique monotonically increasing number used to order the
135*9880d681SAndroid Build Coastguard Worker   /// SDNodes we create.
136*9880d681SAndroid Build Coastguard Worker   unsigned SDNodeOrder;
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker   enum CaseClusterKind {
139*9880d681SAndroid Build Coastguard Worker     /// A cluster of adjacent case labels with the same destination, or just one
140*9880d681SAndroid Build Coastguard Worker     /// case.
141*9880d681SAndroid Build Coastguard Worker     CC_Range,
142*9880d681SAndroid Build Coastguard Worker     /// A cluster of cases suitable for jump table lowering.
143*9880d681SAndroid Build Coastguard Worker     CC_JumpTable,
144*9880d681SAndroid Build Coastguard Worker     /// A cluster of cases suitable for bit test lowering.
145*9880d681SAndroid Build Coastguard Worker     CC_BitTests
146*9880d681SAndroid Build Coastguard Worker   };
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker   /// A cluster of case labels.
149*9880d681SAndroid Build Coastguard Worker   struct CaseCluster {
150*9880d681SAndroid Build Coastguard Worker     CaseClusterKind Kind;
151*9880d681SAndroid Build Coastguard Worker     const ConstantInt *Low, *High;
152*9880d681SAndroid Build Coastguard Worker     union {
153*9880d681SAndroid Build Coastguard Worker       MachineBasicBlock *MBB;
154*9880d681SAndroid Build Coastguard Worker       unsigned JTCasesIndex;
155*9880d681SAndroid Build Coastguard Worker       unsigned BTCasesIndex;
156*9880d681SAndroid Build Coastguard Worker     };
157*9880d681SAndroid Build Coastguard Worker     BranchProbability Prob;
158*9880d681SAndroid Build Coastguard Worker 
rangeCaseCluster159*9880d681SAndroid Build Coastguard Worker     static CaseCluster range(const ConstantInt *Low, const ConstantInt *High,
160*9880d681SAndroid Build Coastguard Worker                              MachineBasicBlock *MBB, BranchProbability Prob) {
161*9880d681SAndroid Build Coastguard Worker       CaseCluster C;
162*9880d681SAndroid Build Coastguard Worker       C.Kind = CC_Range;
163*9880d681SAndroid Build Coastguard Worker       C.Low = Low;
164*9880d681SAndroid Build Coastguard Worker       C.High = High;
165*9880d681SAndroid Build Coastguard Worker       C.MBB = MBB;
166*9880d681SAndroid Build Coastguard Worker       C.Prob = Prob;
167*9880d681SAndroid Build Coastguard Worker       return C;
168*9880d681SAndroid Build Coastguard Worker     }
169*9880d681SAndroid Build Coastguard Worker 
jumpTableCaseCluster170*9880d681SAndroid Build Coastguard Worker     static CaseCluster jumpTable(const ConstantInt *Low,
171*9880d681SAndroid Build Coastguard Worker                                  const ConstantInt *High, unsigned JTCasesIndex,
172*9880d681SAndroid Build Coastguard Worker                                  BranchProbability Prob) {
173*9880d681SAndroid Build Coastguard Worker       CaseCluster C;
174*9880d681SAndroid Build Coastguard Worker       C.Kind = CC_JumpTable;
175*9880d681SAndroid Build Coastguard Worker       C.Low = Low;
176*9880d681SAndroid Build Coastguard Worker       C.High = High;
177*9880d681SAndroid Build Coastguard Worker       C.JTCasesIndex = JTCasesIndex;
178*9880d681SAndroid Build Coastguard Worker       C.Prob = Prob;
179*9880d681SAndroid Build Coastguard Worker       return C;
180*9880d681SAndroid Build Coastguard Worker     }
181*9880d681SAndroid Build Coastguard Worker 
bitTestsCaseCluster182*9880d681SAndroid Build Coastguard Worker     static CaseCluster bitTests(const ConstantInt *Low, const ConstantInt *High,
183*9880d681SAndroid Build Coastguard Worker                                 unsigned BTCasesIndex, BranchProbability Prob) {
184*9880d681SAndroid Build Coastguard Worker       CaseCluster C;
185*9880d681SAndroid Build Coastguard Worker       C.Kind = CC_BitTests;
186*9880d681SAndroid Build Coastguard Worker       C.Low = Low;
187*9880d681SAndroid Build Coastguard Worker       C.High = High;
188*9880d681SAndroid Build Coastguard Worker       C.BTCasesIndex = BTCasesIndex;
189*9880d681SAndroid Build Coastguard Worker       C.Prob = Prob;
190*9880d681SAndroid Build Coastguard Worker       return C;
191*9880d681SAndroid Build Coastguard Worker     }
192*9880d681SAndroid Build Coastguard Worker   };
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker   typedef std::vector<CaseCluster> CaseClusterVector;
195*9880d681SAndroid Build Coastguard Worker   typedef CaseClusterVector::iterator CaseClusterIt;
196*9880d681SAndroid Build Coastguard Worker 
197*9880d681SAndroid Build Coastguard Worker   struct CaseBits {
198*9880d681SAndroid Build Coastguard Worker     uint64_t Mask;
199*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock* BB;
200*9880d681SAndroid Build Coastguard Worker     unsigned Bits;
201*9880d681SAndroid Build Coastguard Worker     BranchProbability ExtraProb;
202*9880d681SAndroid Build Coastguard Worker 
CaseBitsCaseBits203*9880d681SAndroid Build Coastguard Worker     CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits,
204*9880d681SAndroid Build Coastguard Worker              BranchProbability Prob):
205*9880d681SAndroid Build Coastguard Worker       Mask(mask), BB(bb), Bits(bits), ExtraProb(Prob) { }
206*9880d681SAndroid Build Coastguard Worker 
CaseBitsCaseBits207*9880d681SAndroid Build Coastguard Worker     CaseBits() : Mask(0), BB(nullptr), Bits(0) {}
208*9880d681SAndroid Build Coastguard Worker   };
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker   typedef std::vector<CaseBits> CaseBitsVector;
211*9880d681SAndroid Build Coastguard Worker 
212*9880d681SAndroid Build Coastguard Worker   /// Sort Clusters and merge adjacent cases.
213*9880d681SAndroid Build Coastguard Worker   void sortAndRangeify(CaseClusterVector &Clusters);
214*9880d681SAndroid Build Coastguard Worker 
215*9880d681SAndroid Build Coastguard Worker   /// CaseBlock - This structure is used to communicate between
216*9880d681SAndroid Build Coastguard Worker   /// SelectionDAGBuilder and SDISel for the code generation of additional basic
217*9880d681SAndroid Build Coastguard Worker   /// blocks needed by multi-case switch statements.
218*9880d681SAndroid Build Coastguard Worker   struct CaseBlock {
219*9880d681SAndroid Build Coastguard Worker     CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
220*9880d681SAndroid Build Coastguard Worker               const Value *cmpmiddle, MachineBasicBlock *truebb,
221*9880d681SAndroid Build Coastguard Worker               MachineBasicBlock *falsebb, MachineBasicBlock *me,
222*9880d681SAndroid Build Coastguard Worker               BranchProbability trueprob = BranchProbability::getUnknown(),
223*9880d681SAndroid Build Coastguard Worker               BranchProbability falseprob = BranchProbability::getUnknown())
CCCaseBlock224*9880d681SAndroid Build Coastguard Worker         : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
225*9880d681SAndroid Build Coastguard Worker           TrueBB(truebb), FalseBB(falsebb), ThisBB(me), TrueProb(trueprob),
226*9880d681SAndroid Build Coastguard Worker           FalseProb(falseprob) {}
227*9880d681SAndroid Build Coastguard Worker 
228*9880d681SAndroid Build Coastguard Worker     // CC - the condition code to use for the case block's setcc node
229*9880d681SAndroid Build Coastguard Worker     ISD::CondCode CC;
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker     // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
232*9880d681SAndroid Build Coastguard Worker     // Emit by default LHS op RHS. MHS is used for range comparisons:
233*9880d681SAndroid Build Coastguard Worker     // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
234*9880d681SAndroid Build Coastguard Worker     const Value *CmpLHS, *CmpMHS, *CmpRHS;
235*9880d681SAndroid Build Coastguard Worker 
236*9880d681SAndroid Build Coastguard Worker     // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
237*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *TrueBB, *FalseBB;
238*9880d681SAndroid Build Coastguard Worker 
239*9880d681SAndroid Build Coastguard Worker     // ThisBB - the block into which to emit the code for the setcc and branches
240*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *ThisBB;
241*9880d681SAndroid Build Coastguard Worker 
242*9880d681SAndroid Build Coastguard Worker     // TrueProb/FalseProb - branch weights.
243*9880d681SAndroid Build Coastguard Worker     BranchProbability TrueProb, FalseProb;
244*9880d681SAndroid Build Coastguard Worker   };
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker   struct JumpTable {
JumpTableJumpTable247*9880d681SAndroid Build Coastguard Worker     JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
248*9880d681SAndroid Build Coastguard Worker               MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
249*9880d681SAndroid Build Coastguard Worker 
250*9880d681SAndroid Build Coastguard Worker     /// Reg - the virtual register containing the index of the jump table entry
251*9880d681SAndroid Build Coastguard Worker     //. to jump to.
252*9880d681SAndroid Build Coastguard Worker     unsigned Reg;
253*9880d681SAndroid Build Coastguard Worker     /// JTI - the JumpTableIndex for this jump table in the function.
254*9880d681SAndroid Build Coastguard Worker     unsigned JTI;
255*9880d681SAndroid Build Coastguard Worker     /// MBB - the MBB into which to emit the code for the indirect jump.
256*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *MBB;
257*9880d681SAndroid Build Coastguard Worker     /// Default - the MBB of the default bb, which is a successor of the range
258*9880d681SAndroid Build Coastguard Worker     /// check MBB.  This is when updating PHI nodes in successors.
259*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *Default;
260*9880d681SAndroid Build Coastguard Worker   };
261*9880d681SAndroid Build Coastguard Worker   struct JumpTableHeader {
262*9880d681SAndroid Build Coastguard Worker     JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H,
263*9880d681SAndroid Build Coastguard Worker                     bool E = false)
FirstJumpTableHeader264*9880d681SAndroid Build Coastguard Worker         : First(std::move(F)), Last(std::move(L)), SValue(SV), HeaderBB(H),
265*9880d681SAndroid Build Coastguard Worker           Emitted(E) {}
266*9880d681SAndroid Build Coastguard Worker     APInt First;
267*9880d681SAndroid Build Coastguard Worker     APInt Last;
268*9880d681SAndroid Build Coastguard Worker     const Value *SValue;
269*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *HeaderBB;
270*9880d681SAndroid Build Coastguard Worker     bool Emitted;
271*9880d681SAndroid Build Coastguard Worker   };
272*9880d681SAndroid Build Coastguard Worker   typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
273*9880d681SAndroid Build Coastguard Worker 
274*9880d681SAndroid Build Coastguard Worker   struct BitTestCase {
BitTestCaseBitTestCase275*9880d681SAndroid Build Coastguard Worker     BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr,
276*9880d681SAndroid Build Coastguard Worker                 BranchProbability Prob):
277*9880d681SAndroid Build Coastguard Worker       Mask(M), ThisBB(T), TargetBB(Tr), ExtraProb(Prob) { }
278*9880d681SAndroid Build Coastguard Worker     uint64_t Mask;
279*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *ThisBB;
280*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *TargetBB;
281*9880d681SAndroid Build Coastguard Worker     BranchProbability ExtraProb;
282*9880d681SAndroid Build Coastguard Worker   };
283*9880d681SAndroid Build Coastguard Worker 
284*9880d681SAndroid Build Coastguard Worker   typedef SmallVector<BitTestCase, 3> BitTestInfo;
285*9880d681SAndroid Build Coastguard Worker 
286*9880d681SAndroid Build Coastguard Worker   struct BitTestBlock {
BitTestBlockBitTestBlock287*9880d681SAndroid Build Coastguard Worker     BitTestBlock(APInt F, APInt R, const Value *SV, unsigned Rg, MVT RgVT,
288*9880d681SAndroid Build Coastguard Worker                  bool E, bool CR, MachineBasicBlock *P, MachineBasicBlock *D,
289*9880d681SAndroid Build Coastguard Worker                  BitTestInfo C, BranchProbability Pr)
290*9880d681SAndroid Build Coastguard Worker         : First(std::move(F)), Range(std::move(R)), SValue(SV), Reg(Rg),
291*9880d681SAndroid Build Coastguard Worker           RegVT(RgVT), Emitted(E), ContiguousRange(CR), Parent(P), Default(D),
292*9880d681SAndroid Build Coastguard Worker           Cases(std::move(C)), Prob(Pr) {}
293*9880d681SAndroid Build Coastguard Worker     APInt First;
294*9880d681SAndroid Build Coastguard Worker     APInt Range;
295*9880d681SAndroid Build Coastguard Worker     const Value *SValue;
296*9880d681SAndroid Build Coastguard Worker     unsigned Reg;
297*9880d681SAndroid Build Coastguard Worker     MVT RegVT;
298*9880d681SAndroid Build Coastguard Worker     bool Emitted;
299*9880d681SAndroid Build Coastguard Worker     bool ContiguousRange;
300*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *Parent;
301*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *Default;
302*9880d681SAndroid Build Coastguard Worker     BitTestInfo Cases;
303*9880d681SAndroid Build Coastguard Worker     BranchProbability Prob;
304*9880d681SAndroid Build Coastguard Worker     BranchProbability DefaultProb;
305*9880d681SAndroid Build Coastguard Worker   };
306*9880d681SAndroid Build Coastguard Worker 
307*9880d681SAndroid Build Coastguard Worker   /// Check whether a range of clusters is dense enough for a jump table.
308*9880d681SAndroid Build Coastguard Worker   bool isDense(const CaseClusterVector &Clusters, unsigned *TotalCases,
309*9880d681SAndroid Build Coastguard Worker                unsigned First, unsigned Last, unsigned MinDensity);
310*9880d681SAndroid Build Coastguard Worker 
311*9880d681SAndroid Build Coastguard Worker   /// Build a jump table cluster from Clusters[First..Last]. Returns false if it
312*9880d681SAndroid Build Coastguard Worker   /// decides it's not a good idea.
313*9880d681SAndroid Build Coastguard Worker   bool buildJumpTable(CaseClusterVector &Clusters, unsigned First,
314*9880d681SAndroid Build Coastguard Worker                       unsigned Last, const SwitchInst *SI,
315*9880d681SAndroid Build Coastguard Worker                       MachineBasicBlock *DefaultMBB, CaseCluster &JTCluster);
316*9880d681SAndroid Build Coastguard Worker 
317*9880d681SAndroid Build Coastguard Worker   /// Find clusters of cases suitable for jump table lowering.
318*9880d681SAndroid Build Coastguard Worker   void findJumpTables(CaseClusterVector &Clusters, const SwitchInst *SI,
319*9880d681SAndroid Build Coastguard Worker                       MachineBasicBlock *DefaultMBB);
320*9880d681SAndroid Build Coastguard Worker 
321*9880d681SAndroid Build Coastguard Worker   /// Check whether the range [Low,High] fits in a machine word.
322*9880d681SAndroid Build Coastguard Worker   bool rangeFitsInWord(const APInt &Low, const APInt &High);
323*9880d681SAndroid Build Coastguard Worker 
324*9880d681SAndroid Build Coastguard Worker   /// Check whether these clusters are suitable for lowering with bit tests based
325*9880d681SAndroid Build Coastguard Worker   /// on the number of destinations, comparison metric, and range.
326*9880d681SAndroid Build Coastguard Worker   bool isSuitableForBitTests(unsigned NumDests, unsigned NumCmps,
327*9880d681SAndroid Build Coastguard Worker                              const APInt &Low, const APInt &High);
328*9880d681SAndroid Build Coastguard Worker 
329*9880d681SAndroid Build Coastguard Worker   /// Build a bit test cluster from Clusters[First..Last]. Returns false if it
330*9880d681SAndroid Build Coastguard Worker   /// decides it's not a good idea.
331*9880d681SAndroid Build Coastguard Worker   bool buildBitTests(CaseClusterVector &Clusters, unsigned First, unsigned Last,
332*9880d681SAndroid Build Coastguard Worker                      const SwitchInst *SI, CaseCluster &BTCluster);
333*9880d681SAndroid Build Coastguard Worker 
334*9880d681SAndroid Build Coastguard Worker   /// Find clusters of cases suitable for bit test lowering.
335*9880d681SAndroid Build Coastguard Worker   void findBitTestClusters(CaseClusterVector &Clusters, const SwitchInst *SI);
336*9880d681SAndroid Build Coastguard Worker 
337*9880d681SAndroid Build Coastguard Worker   struct SwitchWorkListItem {
338*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *MBB;
339*9880d681SAndroid Build Coastguard Worker     CaseClusterIt FirstCluster;
340*9880d681SAndroid Build Coastguard Worker     CaseClusterIt LastCluster;
341*9880d681SAndroid Build Coastguard Worker     const ConstantInt *GE;
342*9880d681SAndroid Build Coastguard Worker     const ConstantInt *LT;
343*9880d681SAndroid Build Coastguard Worker     BranchProbability DefaultProb;
344*9880d681SAndroid Build Coastguard Worker   };
345*9880d681SAndroid Build Coastguard Worker   typedef SmallVector<SwitchWorkListItem, 4> SwitchWorkList;
346*9880d681SAndroid Build Coastguard Worker 
347*9880d681SAndroid Build Coastguard Worker   /// Determine the rank by weight of CC in [First,Last]. If CC has more weight
348*9880d681SAndroid Build Coastguard Worker   /// than each cluster in the range, its rank is 0.
349*9880d681SAndroid Build Coastguard Worker   static unsigned caseClusterRank(const CaseCluster &CC, CaseClusterIt First,
350*9880d681SAndroid Build Coastguard Worker                                   CaseClusterIt Last);
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker   /// Emit comparison and split W into two subtrees.
353*9880d681SAndroid Build Coastguard Worker   void splitWorkItem(SwitchWorkList &WorkList, const SwitchWorkListItem &W,
354*9880d681SAndroid Build Coastguard Worker                      Value *Cond, MachineBasicBlock *SwitchMBB);
355*9880d681SAndroid Build Coastguard Worker 
356*9880d681SAndroid Build Coastguard Worker   /// Lower W.
357*9880d681SAndroid Build Coastguard Worker   void lowerWorkItem(SwitchWorkListItem W, Value *Cond,
358*9880d681SAndroid Build Coastguard Worker                      MachineBasicBlock *SwitchMBB,
359*9880d681SAndroid Build Coastguard Worker                      MachineBasicBlock *DefaultMBB);
360*9880d681SAndroid Build Coastguard Worker 
361*9880d681SAndroid Build Coastguard Worker 
362*9880d681SAndroid Build Coastguard Worker   /// A class which encapsulates all of the information needed to generate a
363*9880d681SAndroid Build Coastguard Worker   /// stack protector check and signals to isel via its state being initialized
364*9880d681SAndroid Build Coastguard Worker   /// that a stack protector needs to be generated.
365*9880d681SAndroid Build Coastguard Worker   ///
366*9880d681SAndroid Build Coastguard Worker   /// *NOTE* The following is a high level documentation of SelectionDAG Stack
367*9880d681SAndroid Build Coastguard Worker   /// Protector Generation. The reason that it is placed here is for a lack of
368*9880d681SAndroid Build Coastguard Worker   /// other good places to stick it.
369*9880d681SAndroid Build Coastguard Worker   ///
370*9880d681SAndroid Build Coastguard Worker   /// High Level Overview of SelectionDAG Stack Protector Generation:
371*9880d681SAndroid Build Coastguard Worker   ///
372*9880d681SAndroid Build Coastguard Worker   /// Previously, generation of stack protectors was done exclusively in the
373*9880d681SAndroid Build Coastguard Worker   /// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
374*9880d681SAndroid Build Coastguard Worker   /// splitting basic blocks at the IR level to create the success/failure basic
375*9880d681SAndroid Build Coastguard Worker   /// blocks in the tail of the basic block in question. As a result of this,
376*9880d681SAndroid Build Coastguard Worker   /// calls that would have qualified for the sibling call optimization were no
377*9880d681SAndroid Build Coastguard Worker   /// longer eligible for optimization since said calls were no longer right in
378*9880d681SAndroid Build Coastguard Worker   /// the "tail position" (i.e. the immediate predecessor of a ReturnInst
379*9880d681SAndroid Build Coastguard Worker   /// instruction).
380*9880d681SAndroid Build Coastguard Worker   ///
381*9880d681SAndroid Build Coastguard Worker   /// Then it was noticed that since the sibling call optimization causes the
382*9880d681SAndroid Build Coastguard Worker   /// callee to reuse the caller's stack, if we could delay the generation of
383*9880d681SAndroid Build Coastguard Worker   /// the stack protector check until later in CodeGen after the sibling call
384*9880d681SAndroid Build Coastguard Worker   /// decision was made, we get both the tail call optimization and the stack
385*9880d681SAndroid Build Coastguard Worker   /// protector check!
386*9880d681SAndroid Build Coastguard Worker   ///
387*9880d681SAndroid Build Coastguard Worker   /// A few goals in solving this problem were:
388*9880d681SAndroid Build Coastguard Worker   ///
389*9880d681SAndroid Build Coastguard Worker   ///   1. Preserve the architecture independence of stack protector generation.
390*9880d681SAndroid Build Coastguard Worker   ///
391*9880d681SAndroid Build Coastguard Worker   ///   2. Preserve the normal IR level stack protector check for platforms like
392*9880d681SAndroid Build Coastguard Worker   ///      OpenBSD for which we support platform-specific stack protector
393*9880d681SAndroid Build Coastguard Worker   ///      generation.
394*9880d681SAndroid Build Coastguard Worker   ///
395*9880d681SAndroid Build Coastguard Worker   /// The main problem that guided the present solution is that one can not
396*9880d681SAndroid Build Coastguard Worker   /// solve this problem in an architecture independent manner at the IR level
397*9880d681SAndroid Build Coastguard Worker   /// only. This is because:
398*9880d681SAndroid Build Coastguard Worker   ///
399*9880d681SAndroid Build Coastguard Worker   ///   1. The decision on whether or not to perform a sibling call on certain
400*9880d681SAndroid Build Coastguard Worker   ///      platforms (for instance i386) requires lower level information
401*9880d681SAndroid Build Coastguard Worker   ///      related to available registers that can not be known at the IR level.
402*9880d681SAndroid Build Coastguard Worker   ///
403*9880d681SAndroid Build Coastguard Worker   ///   2. Even if the previous point were not true, the decision on whether to
404*9880d681SAndroid Build Coastguard Worker   ///      perform a tail call is done in LowerCallTo in SelectionDAG which
405*9880d681SAndroid Build Coastguard Worker   ///      occurs after the Stack Protector Pass. As a result, one would need to
406*9880d681SAndroid Build Coastguard Worker   ///      put the relevant callinst into the stack protector check success
407*9880d681SAndroid Build Coastguard Worker   ///      basic block (where the return inst is placed) and then move it back
408*9880d681SAndroid Build Coastguard Worker   ///      later at SelectionDAG/MI time before the stack protector check if the
409*9880d681SAndroid Build Coastguard Worker   ///      tail call optimization failed. The MI level option was nixed
410*9880d681SAndroid Build Coastguard Worker   ///      immediately since it would require platform-specific pattern
411*9880d681SAndroid Build Coastguard Worker   ///      matching. The SelectionDAG level option was nixed because
412*9880d681SAndroid Build Coastguard Worker   ///      SelectionDAG only processes one IR level basic block at a time
413*9880d681SAndroid Build Coastguard Worker   ///      implying one could not create a DAG Combine to move the callinst.
414*9880d681SAndroid Build Coastguard Worker   ///
415*9880d681SAndroid Build Coastguard Worker   /// To get around this problem a few things were realized:
416*9880d681SAndroid Build Coastguard Worker   ///
417*9880d681SAndroid Build Coastguard Worker   ///   1. While one can not handle multiple IR level basic blocks at the
418*9880d681SAndroid Build Coastguard Worker   ///      SelectionDAG Level, one can generate multiple machine basic blocks
419*9880d681SAndroid Build Coastguard Worker   ///      for one IR level basic block. This is how we handle bit tests and
420*9880d681SAndroid Build Coastguard Worker   ///      switches.
421*9880d681SAndroid Build Coastguard Worker   ///
422*9880d681SAndroid Build Coastguard Worker   ///   2. At the MI level, tail calls are represented via a special return
423*9880d681SAndroid Build Coastguard Worker   ///      MIInst called "tcreturn". Thus if we know the basic block in which we
424*9880d681SAndroid Build Coastguard Worker   ///      wish to insert the stack protector check, we get the correct behavior
425*9880d681SAndroid Build Coastguard Worker   ///      by always inserting the stack protector check right before the return
426*9880d681SAndroid Build Coastguard Worker   ///      statement. This is a "magical transformation" since no matter where
427*9880d681SAndroid Build Coastguard Worker   ///      the stack protector check intrinsic is, we always insert the stack
428*9880d681SAndroid Build Coastguard Worker   ///      protector check code at the end of the BB.
429*9880d681SAndroid Build Coastguard Worker   ///
430*9880d681SAndroid Build Coastguard Worker   /// Given the aforementioned constraints, the following solution was devised:
431*9880d681SAndroid Build Coastguard Worker   ///
432*9880d681SAndroid Build Coastguard Worker   ///   1. On platforms that do not support SelectionDAG stack protector check
433*9880d681SAndroid Build Coastguard Worker   ///      generation, allow for the normal IR level stack protector check
434*9880d681SAndroid Build Coastguard Worker   ///      generation to continue.
435*9880d681SAndroid Build Coastguard Worker   ///
436*9880d681SAndroid Build Coastguard Worker   ///   2. On platforms that do support SelectionDAG stack protector check
437*9880d681SAndroid Build Coastguard Worker   ///      generation:
438*9880d681SAndroid Build Coastguard Worker   ///
439*9880d681SAndroid Build Coastguard Worker   ///     a. Use the IR level stack protector pass to decide if a stack
440*9880d681SAndroid Build Coastguard Worker   ///        protector is required/which BB we insert the stack protector check
441*9880d681SAndroid Build Coastguard Worker   ///        in by reusing the logic already therein. If we wish to generate a
442*9880d681SAndroid Build Coastguard Worker   ///        stack protector check in a basic block, we place a special IR
443*9880d681SAndroid Build Coastguard Worker   ///        intrinsic called llvm.stackprotectorcheck right before the BB's
444*9880d681SAndroid Build Coastguard Worker   ///        returninst or if there is a callinst that could potentially be
445*9880d681SAndroid Build Coastguard Worker   ///        sibling call optimized, before the call inst.
446*9880d681SAndroid Build Coastguard Worker   ///
447*9880d681SAndroid Build Coastguard Worker   ///     b. Then when a BB with said intrinsic is processed, we codegen the BB
448*9880d681SAndroid Build Coastguard Worker   ///        normally via SelectBasicBlock. In said process, when we visit the
449*9880d681SAndroid Build Coastguard Worker   ///        stack protector check, we do not actually emit anything into the
450*9880d681SAndroid Build Coastguard Worker   ///        BB. Instead, we just initialize the stack protector descriptor
451*9880d681SAndroid Build Coastguard Worker   ///        class (which involves stashing information/creating the success
452*9880d681SAndroid Build Coastguard Worker   ///        mbbb and the failure mbb if we have not created one for this
453*9880d681SAndroid Build Coastguard Worker   ///        function yet) and export the guard variable that we are going to
454*9880d681SAndroid Build Coastguard Worker   ///        compare.
455*9880d681SAndroid Build Coastguard Worker   ///
456*9880d681SAndroid Build Coastguard Worker   ///     c. After we finish selecting the basic block, in FinishBasicBlock if
457*9880d681SAndroid Build Coastguard Worker   ///        the StackProtectorDescriptor attached to the SelectionDAGBuilder is
458*9880d681SAndroid Build Coastguard Worker   ///        initialized, we produce the validation code with one of these
459*9880d681SAndroid Build Coastguard Worker   ///        techniques:
460*9880d681SAndroid Build Coastguard Worker   ///          1) with a call to a guard check function
461*9880d681SAndroid Build Coastguard Worker   ///          2) with inlined instrumentation
462*9880d681SAndroid Build Coastguard Worker   ///
463*9880d681SAndroid Build Coastguard Worker   ///        1) We insert a call to the check function before the terminator.
464*9880d681SAndroid Build Coastguard Worker   ///
465*9880d681SAndroid Build Coastguard Worker   ///        2) We first find a splice point in the parent basic block
466*9880d681SAndroid Build Coastguard Worker   ///        before the terminator and then splice the terminator of said basic
467*9880d681SAndroid Build Coastguard Worker   ///        block into the success basic block. Then we code-gen a new tail for
468*9880d681SAndroid Build Coastguard Worker   ///        the parent basic block consisting of the two loads, the comparison,
469*9880d681SAndroid Build Coastguard Worker   ///        and finally two branches to the success/failure basic blocks. We
470*9880d681SAndroid Build Coastguard Worker   ///        conclude by code-gening the failure basic block if we have not
471*9880d681SAndroid Build Coastguard Worker   ///        code-gened it already (all stack protector checks we generate in
472*9880d681SAndroid Build Coastguard Worker   ///        the same function, use the same failure basic block).
473*9880d681SAndroid Build Coastguard Worker   class StackProtectorDescriptor {
474*9880d681SAndroid Build Coastguard Worker   public:
StackProtectorDescriptor()475*9880d681SAndroid Build Coastguard Worker     StackProtectorDescriptor()
476*9880d681SAndroid Build Coastguard Worker         : ParentMBB(nullptr), SuccessMBB(nullptr), FailureMBB(nullptr) {}
477*9880d681SAndroid Build Coastguard Worker 
478*9880d681SAndroid Build Coastguard Worker     /// Returns true if all fields of the stack protector descriptor are
479*9880d681SAndroid Build Coastguard Worker     /// initialized implying that we should/are ready to emit a stack protector.
shouldEmitStackProtector()480*9880d681SAndroid Build Coastguard Worker     bool shouldEmitStackProtector() const {
481*9880d681SAndroid Build Coastguard Worker       return ParentMBB && SuccessMBB && FailureMBB;
482*9880d681SAndroid Build Coastguard Worker     }
483*9880d681SAndroid Build Coastguard Worker 
shouldEmitFunctionBasedCheckStackProtector()484*9880d681SAndroid Build Coastguard Worker     bool shouldEmitFunctionBasedCheckStackProtector() const {
485*9880d681SAndroid Build Coastguard Worker       return ParentMBB && !SuccessMBB && !FailureMBB;
486*9880d681SAndroid Build Coastguard Worker     }
487*9880d681SAndroid Build Coastguard Worker 
488*9880d681SAndroid Build Coastguard Worker     /// Initialize the stack protector descriptor structure for a new basic
489*9880d681SAndroid Build Coastguard Worker     /// block.
initialize(const BasicBlock * BB,MachineBasicBlock * MBB,bool FunctionBasedInstrumentation)490*9880d681SAndroid Build Coastguard Worker     void initialize(const BasicBlock *BB, MachineBasicBlock *MBB,
491*9880d681SAndroid Build Coastguard Worker                     bool FunctionBasedInstrumentation) {
492*9880d681SAndroid Build Coastguard Worker       // Make sure we are not initialized yet.
493*9880d681SAndroid Build Coastguard Worker       assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
494*9880d681SAndroid Build Coastguard Worker              "already initialized!");
495*9880d681SAndroid Build Coastguard Worker       ParentMBB = MBB;
496*9880d681SAndroid Build Coastguard Worker       if (!FunctionBasedInstrumentation) {
497*9880d681SAndroid Build Coastguard Worker         SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true);
498*9880d681SAndroid Build Coastguard Worker         FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB);
499*9880d681SAndroid Build Coastguard Worker       }
500*9880d681SAndroid Build Coastguard Worker     }
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker     /// Reset state that changes when we handle different basic blocks.
503*9880d681SAndroid Build Coastguard Worker     ///
504*9880d681SAndroid Build Coastguard Worker     /// This currently includes:
505*9880d681SAndroid Build Coastguard Worker     ///
506*9880d681SAndroid Build Coastguard Worker     /// 1. The specific basic block we are generating a
507*9880d681SAndroid Build Coastguard Worker     /// stack protector for (ParentMBB).
508*9880d681SAndroid Build Coastguard Worker     ///
509*9880d681SAndroid Build Coastguard Worker     /// 2. The successor machine basic block that will contain the tail of
510*9880d681SAndroid Build Coastguard Worker     /// parent mbb after we create the stack protector check (SuccessMBB). This
511*9880d681SAndroid Build Coastguard Worker     /// BB is visited only on stack protector check success.
resetPerBBState()512*9880d681SAndroid Build Coastguard Worker     void resetPerBBState() {
513*9880d681SAndroid Build Coastguard Worker       ParentMBB = nullptr;
514*9880d681SAndroid Build Coastguard Worker       SuccessMBB = nullptr;
515*9880d681SAndroid Build Coastguard Worker     }
516*9880d681SAndroid Build Coastguard Worker 
517*9880d681SAndroid Build Coastguard Worker     /// Reset state that only changes when we switch functions.
518*9880d681SAndroid Build Coastguard Worker     ///
519*9880d681SAndroid Build Coastguard Worker     /// This currently includes:
520*9880d681SAndroid Build Coastguard Worker     ///
521*9880d681SAndroid Build Coastguard Worker     /// 1. FailureMBB since we reuse the failure code path for all stack
522*9880d681SAndroid Build Coastguard Worker     /// protector checks created in an individual function.
523*9880d681SAndroid Build Coastguard Worker     ///
524*9880d681SAndroid Build Coastguard Worker     /// 2.The guard variable since the guard variable we are checking against is
525*9880d681SAndroid Build Coastguard Worker     /// always the same.
resetPerFunctionState()526*9880d681SAndroid Build Coastguard Worker     void resetPerFunctionState() {
527*9880d681SAndroid Build Coastguard Worker       FailureMBB = nullptr;
528*9880d681SAndroid Build Coastguard Worker     }
529*9880d681SAndroid Build Coastguard Worker 
getParentMBB()530*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getParentMBB() { return ParentMBB; }
getSuccessMBB()531*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
getFailureMBB()532*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getFailureMBB() { return FailureMBB; }
533*9880d681SAndroid Build Coastguard Worker 
534*9880d681SAndroid Build Coastguard Worker   private:
535*9880d681SAndroid Build Coastguard Worker     /// The basic block for which we are generating the stack protector.
536*9880d681SAndroid Build Coastguard Worker     ///
537*9880d681SAndroid Build Coastguard Worker     /// As a result of stack protector generation, we will splice the
538*9880d681SAndroid Build Coastguard Worker     /// terminators of this basic block into the successor mbb SuccessMBB and
539*9880d681SAndroid Build Coastguard Worker     /// replace it with a compare/branch to the successor mbbs
540*9880d681SAndroid Build Coastguard Worker     /// SuccessMBB/FailureMBB depending on whether or not the stack protector
541*9880d681SAndroid Build Coastguard Worker     /// was violated.
542*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *ParentMBB;
543*9880d681SAndroid Build Coastguard Worker 
544*9880d681SAndroid Build Coastguard Worker     /// A basic block visited on stack protector check success that contains the
545*9880d681SAndroid Build Coastguard Worker     /// terminators of ParentMBB.
546*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *SuccessMBB;
547*9880d681SAndroid Build Coastguard Worker 
548*9880d681SAndroid Build Coastguard Worker     /// This basic block visited on stack protector check failure that will
549*9880d681SAndroid Build Coastguard Worker     /// contain a call to __stack_chk_fail().
550*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *FailureMBB;
551*9880d681SAndroid Build Coastguard Worker 
552*9880d681SAndroid Build Coastguard Worker     /// Add a successor machine basic block to ParentMBB. If the successor mbb
553*9880d681SAndroid Build Coastguard Worker     /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic
554*9880d681SAndroid Build Coastguard Worker     /// block will be created. Assign a large weight if IsLikely is true.
555*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB,
556*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock *ParentMBB,
557*9880d681SAndroid Build Coastguard Worker                                        bool IsLikely,
558*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock *SuccMBB = nullptr);
559*9880d681SAndroid Build Coastguard Worker   };
560*9880d681SAndroid Build Coastguard Worker 
561*9880d681SAndroid Build Coastguard Worker private:
562*9880d681SAndroid Build Coastguard Worker   const TargetMachine &TM;
563*9880d681SAndroid Build Coastguard Worker public:
564*9880d681SAndroid Build Coastguard Worker   /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling
565*9880d681SAndroid Build Coastguard Worker   /// nodes without a corresponding SDNode.
566*9880d681SAndroid Build Coastguard Worker   static const unsigned LowestSDNodeOrder = 1;
567*9880d681SAndroid Build Coastguard Worker 
568*9880d681SAndroid Build Coastguard Worker   SelectionDAG &DAG;
569*9880d681SAndroid Build Coastguard Worker   const DataLayout *DL;
570*9880d681SAndroid Build Coastguard Worker   AliasAnalysis *AA;
571*9880d681SAndroid Build Coastguard Worker   const TargetLibraryInfo *LibInfo;
572*9880d681SAndroid Build Coastguard Worker 
573*9880d681SAndroid Build Coastguard Worker   /// SwitchCases - Vector of CaseBlock structures used to communicate
574*9880d681SAndroid Build Coastguard Worker   /// SwitchInst code generation information.
575*9880d681SAndroid Build Coastguard Worker   std::vector<CaseBlock> SwitchCases;
576*9880d681SAndroid Build Coastguard Worker   /// JTCases - Vector of JumpTable structures used to communicate
577*9880d681SAndroid Build Coastguard Worker   /// SwitchInst code generation information.
578*9880d681SAndroid Build Coastguard Worker   std::vector<JumpTableBlock> JTCases;
579*9880d681SAndroid Build Coastguard Worker   /// BitTestCases - Vector of BitTestBlock structures used to communicate
580*9880d681SAndroid Build Coastguard Worker   /// SwitchInst code generation information.
581*9880d681SAndroid Build Coastguard Worker   std::vector<BitTestBlock> BitTestCases;
582*9880d681SAndroid Build Coastguard Worker   /// A StackProtectorDescriptor structure used to communicate stack protector
583*9880d681SAndroid Build Coastguard Worker   /// information in between SelectBasicBlock and FinishBasicBlock.
584*9880d681SAndroid Build Coastguard Worker   StackProtectorDescriptor SPDescriptor;
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker   // Emit PHI-node-operand constants only once even if used by multiple
587*9880d681SAndroid Build Coastguard Worker   // PHI nodes.
588*9880d681SAndroid Build Coastguard Worker   DenseMap<const Constant *, unsigned> ConstantsOut;
589*9880d681SAndroid Build Coastguard Worker 
590*9880d681SAndroid Build Coastguard Worker   /// FuncInfo - Information about the function as a whole.
591*9880d681SAndroid Build Coastguard Worker   ///
592*9880d681SAndroid Build Coastguard Worker   FunctionLoweringInfo &FuncInfo;
593*9880d681SAndroid Build Coastguard Worker 
594*9880d681SAndroid Build Coastguard Worker   /// GFI - Garbage collection metadata for the function.
595*9880d681SAndroid Build Coastguard Worker   GCFunctionInfo *GFI;
596*9880d681SAndroid Build Coastguard Worker 
597*9880d681SAndroid Build Coastguard Worker   /// LPadToCallSiteMap - Map a landing pad to the call site indexes.
598*9880d681SAndroid Build Coastguard Worker   DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   /// HasTailCall - This is set to true if a call in the current
601*9880d681SAndroid Build Coastguard Worker   /// block has been translated as a tail call. In this case,
602*9880d681SAndroid Build Coastguard Worker   /// no subsequent DAG nodes should be created.
603*9880d681SAndroid Build Coastguard Worker   ///
604*9880d681SAndroid Build Coastguard Worker   bool HasTailCall;
605*9880d681SAndroid Build Coastguard Worker 
606*9880d681SAndroid Build Coastguard Worker   LLVMContext *Context;
607*9880d681SAndroid Build Coastguard Worker 
SelectionDAGBuilder(SelectionDAG & dag,FunctionLoweringInfo & funcinfo,CodeGenOpt::Level ol)608*9880d681SAndroid Build Coastguard Worker   SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
609*9880d681SAndroid Build Coastguard Worker                       CodeGenOpt::Level ol)
610*9880d681SAndroid Build Coastguard Worker     : CurInst(nullptr), SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()),
611*9880d681SAndroid Build Coastguard Worker       DAG(dag), FuncInfo(funcinfo),
612*9880d681SAndroid Build Coastguard Worker       HasTailCall(false) {
613*9880d681SAndroid Build Coastguard Worker   }
614*9880d681SAndroid Build Coastguard Worker 
615*9880d681SAndroid Build Coastguard Worker   void init(GCFunctionInfo *gfi, AliasAnalysis &aa,
616*9880d681SAndroid Build Coastguard Worker             const TargetLibraryInfo *li);
617*9880d681SAndroid Build Coastguard Worker 
618*9880d681SAndroid Build Coastguard Worker   /// clear - Clear out the current SelectionDAG and the associated
619*9880d681SAndroid Build Coastguard Worker   /// state and prepare this SelectionDAGBuilder object to be used
620*9880d681SAndroid Build Coastguard Worker   /// for a new block. This doesn't clear out information about
621*9880d681SAndroid Build Coastguard Worker   /// additional blocks that are needed to complete switch lowering
622*9880d681SAndroid Build Coastguard Worker   /// or PHI node updating; that information is cleared out as it is
623*9880d681SAndroid Build Coastguard Worker   /// consumed.
624*9880d681SAndroid Build Coastguard Worker   void clear();
625*9880d681SAndroid Build Coastguard Worker 
626*9880d681SAndroid Build Coastguard Worker   /// clearDanglingDebugInfo - Clear the dangling debug information
627*9880d681SAndroid Build Coastguard Worker   /// map. This function is separated from the clear so that debug
628*9880d681SAndroid Build Coastguard Worker   /// information that is dangling in a basic block can be properly
629*9880d681SAndroid Build Coastguard Worker   /// resolved in a different basic block. This allows the
630*9880d681SAndroid Build Coastguard Worker   /// SelectionDAG to resolve dangling debug information attached
631*9880d681SAndroid Build Coastguard Worker   /// to PHI nodes.
632*9880d681SAndroid Build Coastguard Worker   void clearDanglingDebugInfo();
633*9880d681SAndroid Build Coastguard Worker 
634*9880d681SAndroid Build Coastguard Worker   /// getRoot - Return the current virtual root of the Selection DAG,
635*9880d681SAndroid Build Coastguard Worker   /// flushing any PendingLoad items. This must be done before emitting
636*9880d681SAndroid Build Coastguard Worker   /// a store or any other node that may need to be ordered after any
637*9880d681SAndroid Build Coastguard Worker   /// prior load instructions.
638*9880d681SAndroid Build Coastguard Worker   ///
639*9880d681SAndroid Build Coastguard Worker   SDValue getRoot();
640*9880d681SAndroid Build Coastguard Worker 
641*9880d681SAndroid Build Coastguard Worker   /// getControlRoot - Similar to getRoot, but instead of flushing all the
642*9880d681SAndroid Build Coastguard Worker   /// PendingLoad items, flush all the PendingExports items. It is necessary
643*9880d681SAndroid Build Coastguard Worker   /// to do this before emitting a terminator instruction.
644*9880d681SAndroid Build Coastguard Worker   ///
645*9880d681SAndroid Build Coastguard Worker   SDValue getControlRoot();
646*9880d681SAndroid Build Coastguard Worker 
getCurSDLoc()647*9880d681SAndroid Build Coastguard Worker   SDLoc getCurSDLoc() const {
648*9880d681SAndroid Build Coastguard Worker     return SDLoc(CurInst, SDNodeOrder);
649*9880d681SAndroid Build Coastguard Worker   }
650*9880d681SAndroid Build Coastguard Worker 
getCurDebugLoc()651*9880d681SAndroid Build Coastguard Worker   DebugLoc getCurDebugLoc() const {
652*9880d681SAndroid Build Coastguard Worker     return CurInst ? CurInst->getDebugLoc() : DebugLoc();
653*9880d681SAndroid Build Coastguard Worker   }
654*9880d681SAndroid Build Coastguard Worker 
getSDNodeOrder()655*9880d681SAndroid Build Coastguard Worker   unsigned getSDNodeOrder() const { return SDNodeOrder; }
656*9880d681SAndroid Build Coastguard Worker 
657*9880d681SAndroid Build Coastguard Worker   void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
658*9880d681SAndroid Build Coastguard Worker 
659*9880d681SAndroid Build Coastguard Worker   void visit(const Instruction &I);
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker   void visit(unsigned Opcode, const User &I);
662*9880d681SAndroid Build Coastguard Worker 
663*9880d681SAndroid Build Coastguard Worker   /// getCopyFromRegs - If there was virtual register allocated for the value V
664*9880d681SAndroid Build Coastguard Worker   /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
665*9880d681SAndroid Build Coastguard Worker   SDValue getCopyFromRegs(const Value *V, Type *Ty);
666*9880d681SAndroid Build Coastguard Worker 
667*9880d681SAndroid Build Coastguard Worker   // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
668*9880d681SAndroid Build Coastguard Worker   // generate the debug data structures now that we've seen its definition.
669*9880d681SAndroid Build Coastguard Worker   void resolveDanglingDebugInfo(const Value *V, SDValue Val);
670*9880d681SAndroid Build Coastguard Worker   SDValue getValue(const Value *V);
671*9880d681SAndroid Build Coastguard Worker   bool findValue(const Value *V) const;
672*9880d681SAndroid Build Coastguard Worker 
673*9880d681SAndroid Build Coastguard Worker   SDValue getNonRegisterValue(const Value *V);
674*9880d681SAndroid Build Coastguard Worker   SDValue getValueImpl(const Value *V);
675*9880d681SAndroid Build Coastguard Worker 
setValue(const Value * V,SDValue NewN)676*9880d681SAndroid Build Coastguard Worker   void setValue(const Value *V, SDValue NewN) {
677*9880d681SAndroid Build Coastguard Worker     SDValue &N = NodeMap[V];
678*9880d681SAndroid Build Coastguard Worker     assert(!N.getNode() && "Already set a value for this node!");
679*9880d681SAndroid Build Coastguard Worker     N = NewN;
680*9880d681SAndroid Build Coastguard Worker   }
681*9880d681SAndroid Build Coastguard Worker 
setUnusedArgValue(const Value * V,SDValue NewN)682*9880d681SAndroid Build Coastguard Worker   void setUnusedArgValue(const Value *V, SDValue NewN) {
683*9880d681SAndroid Build Coastguard Worker     SDValue &N = UnusedArgNodeMap[V];
684*9880d681SAndroid Build Coastguard Worker     assert(!N.getNode() && "Already set a value for this node!");
685*9880d681SAndroid Build Coastguard Worker     N = NewN;
686*9880d681SAndroid Build Coastguard Worker   }
687*9880d681SAndroid Build Coastguard Worker 
688*9880d681SAndroid Build Coastguard Worker   void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
689*9880d681SAndroid Build Coastguard Worker                             MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
690*9880d681SAndroid Build Coastguard Worker                             MachineBasicBlock *SwitchBB,
691*9880d681SAndroid Build Coastguard Worker                             Instruction::BinaryOps Opc, BranchProbability TW,
692*9880d681SAndroid Build Coastguard Worker                             BranchProbability FW);
693*9880d681SAndroid Build Coastguard Worker   void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
694*9880d681SAndroid Build Coastguard Worker                                     MachineBasicBlock *FBB,
695*9880d681SAndroid Build Coastguard Worker                                     MachineBasicBlock *CurBB,
696*9880d681SAndroid Build Coastguard Worker                                     MachineBasicBlock *SwitchBB,
697*9880d681SAndroid Build Coastguard Worker                                     BranchProbability TW, BranchProbability FW);
698*9880d681SAndroid Build Coastguard Worker   bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
699*9880d681SAndroid Build Coastguard Worker   bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
700*9880d681SAndroid Build Coastguard Worker   void CopyToExportRegsIfNeeded(const Value *V);
701*9880d681SAndroid Build Coastguard Worker   void ExportFromCurrentBlock(const Value *V);
702*9880d681SAndroid Build Coastguard Worker   void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
703*9880d681SAndroid Build Coastguard Worker                    const BasicBlock *EHPadBB = nullptr);
704*9880d681SAndroid Build Coastguard Worker 
705*9880d681SAndroid Build Coastguard Worker   // Lower range metadata from 0 to N to assert zext to an integer of nearest
706*9880d681SAndroid Build Coastguard Worker   // floor power of two.
707*9880d681SAndroid Build Coastguard Worker   SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I,
708*9880d681SAndroid Build Coastguard Worker                                  SDValue Op);
709*9880d681SAndroid Build Coastguard Worker 
710*9880d681SAndroid Build Coastguard Worker   void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI,
711*9880d681SAndroid Build Coastguard Worker                                 ImmutableCallSite CS, unsigned ArgIdx,
712*9880d681SAndroid Build Coastguard Worker                                 unsigned NumArgs, SDValue Callee,
713*9880d681SAndroid Build Coastguard Worker                                 Type *ReturnTy, bool IsPatchPoint);
714*9880d681SAndroid Build Coastguard Worker 
715*9880d681SAndroid Build Coastguard Worker   std::pair<SDValue, SDValue>
716*9880d681SAndroid Build Coastguard Worker   lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
717*9880d681SAndroid Build Coastguard Worker                  const BasicBlock *EHPadBB = nullptr);
718*9880d681SAndroid Build Coastguard Worker 
719*9880d681SAndroid Build Coastguard Worker   /// UpdateSplitBlock - When an MBB was split during scheduling, update the
720*9880d681SAndroid Build Coastguard Worker   /// references that need to refer to the last resulting block.
721*9880d681SAndroid Build Coastguard Worker   void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
722*9880d681SAndroid Build Coastguard Worker 
723*9880d681SAndroid Build Coastguard Worker   /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes
724*9880d681SAndroid Build Coastguard Worker   /// of lowering into a STATEPOINT node.
725*9880d681SAndroid Build Coastguard Worker   struct StatepointLoweringInfo {
726*9880d681SAndroid Build Coastguard Worker     /// Bases[i] is the base pointer for Ptrs[i].  Together they denote the set
727*9880d681SAndroid Build Coastguard Worker     /// of gc pointers this STATEPOINT has to relocate.
728*9880d681SAndroid Build Coastguard Worker     SmallVector<const Value *, 16> Bases;
729*9880d681SAndroid Build Coastguard Worker     SmallVector<const Value *, 16> Ptrs;
730*9880d681SAndroid Build Coastguard Worker 
731*9880d681SAndroid Build Coastguard Worker     /// The set of gc.relocate calls associated with this gc.statepoint.
732*9880d681SAndroid Build Coastguard Worker     SmallVector<const GCRelocateInst *, 16> GCRelocates;
733*9880d681SAndroid Build Coastguard Worker 
734*9880d681SAndroid Build Coastguard Worker     /// The full list of gc arguments to the gc.statepoint being lowered.
735*9880d681SAndroid Build Coastguard Worker     ArrayRef<const Use> GCArgs;
736*9880d681SAndroid Build Coastguard Worker 
737*9880d681SAndroid Build Coastguard Worker     /// The gc.statepoint instruction.
738*9880d681SAndroid Build Coastguard Worker     const Instruction *StatepointInstr = nullptr;
739*9880d681SAndroid Build Coastguard Worker 
740*9880d681SAndroid Build Coastguard Worker     /// The list of gc transition arguments present in the gc.statepoint being
741*9880d681SAndroid Build Coastguard Worker     /// lowered.
742*9880d681SAndroid Build Coastguard Worker     ArrayRef<const Use> GCTransitionArgs;
743*9880d681SAndroid Build Coastguard Worker 
744*9880d681SAndroid Build Coastguard Worker     /// The ID that the resulting STATEPOINT instruction has to report.
745*9880d681SAndroid Build Coastguard Worker     unsigned ID = -1;
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker     /// Information regarding the underlying call instruction.
748*9880d681SAndroid Build Coastguard Worker     TargetLowering::CallLoweringInfo CLI;
749*9880d681SAndroid Build Coastguard Worker 
750*9880d681SAndroid Build Coastguard Worker     /// The deoptimization state associated with this gc.statepoint call, if
751*9880d681SAndroid Build Coastguard Worker     /// any.
752*9880d681SAndroid Build Coastguard Worker     ArrayRef<const Use> DeoptState;
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker     /// Flags associated with the meta arguments being lowered.
755*9880d681SAndroid Build Coastguard Worker     uint64_t StatepointFlags = -1;
756*9880d681SAndroid Build Coastguard Worker 
757*9880d681SAndroid Build Coastguard Worker     /// The number of patchable bytes the call needs to get lowered into.
758*9880d681SAndroid Build Coastguard Worker     unsigned NumPatchBytes = -1;
759*9880d681SAndroid Build Coastguard Worker 
760*9880d681SAndroid Build Coastguard Worker     /// The exception handling unwind destination, in case this represents an
761*9880d681SAndroid Build Coastguard Worker     /// invoke of gc.statepoint.
762*9880d681SAndroid Build Coastguard Worker     const BasicBlock *EHPadBB = nullptr;
763*9880d681SAndroid Build Coastguard Worker 
StatepointLoweringInfoStatepointLoweringInfo764*9880d681SAndroid Build Coastguard Worker     explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {}
765*9880d681SAndroid Build Coastguard Worker   };
766*9880d681SAndroid Build Coastguard Worker 
767*9880d681SAndroid Build Coastguard Worker   /// Lower \p SLI into a STATEPOINT instruction.
768*9880d681SAndroid Build Coastguard Worker   SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SLI);
769*9880d681SAndroid Build Coastguard Worker 
770*9880d681SAndroid Build Coastguard Worker   // This function is responsible for the whole statepoint lowering process.
771*9880d681SAndroid Build Coastguard Worker   // It uniformly handles invoke and call statepoints.
772*9880d681SAndroid Build Coastguard Worker   void LowerStatepoint(ImmutableStatepoint Statepoint,
773*9880d681SAndroid Build Coastguard Worker                        const BasicBlock *EHPadBB = nullptr);
774*9880d681SAndroid Build Coastguard Worker 
775*9880d681SAndroid Build Coastguard Worker   void LowerCallSiteWithDeoptBundle(ImmutableCallSite CS, SDValue Callee,
776*9880d681SAndroid Build Coastguard Worker                                     const BasicBlock *EHPadBB);
777*9880d681SAndroid Build Coastguard Worker 
778*9880d681SAndroid Build Coastguard Worker   void LowerDeoptimizeCall(const CallInst *CI);
779*9880d681SAndroid Build Coastguard Worker   void LowerDeoptimizingReturn();
780*9880d681SAndroid Build Coastguard Worker 
781*9880d681SAndroid Build Coastguard Worker   void LowerCallSiteWithDeoptBundleImpl(ImmutableCallSite CS, SDValue Callee,
782*9880d681SAndroid Build Coastguard Worker                                         const BasicBlock *EHPadBB,
783*9880d681SAndroid Build Coastguard Worker                                         bool VarArgDisallowed,
784*9880d681SAndroid Build Coastguard Worker                                         bool ForceVoidReturnTy);
785*9880d681SAndroid Build Coastguard Worker 
786*9880d681SAndroid Build Coastguard Worker private:
787*9880d681SAndroid Build Coastguard Worker   // Terminator instructions.
788*9880d681SAndroid Build Coastguard Worker   void visitRet(const ReturnInst &I);
789*9880d681SAndroid Build Coastguard Worker   void visitBr(const BranchInst &I);
790*9880d681SAndroid Build Coastguard Worker   void visitSwitch(const SwitchInst &I);
791*9880d681SAndroid Build Coastguard Worker   void visitIndirectBr(const IndirectBrInst &I);
792*9880d681SAndroid Build Coastguard Worker   void visitUnreachable(const UnreachableInst &I);
793*9880d681SAndroid Build Coastguard Worker   void visitCleanupRet(const CleanupReturnInst &I);
794*9880d681SAndroid Build Coastguard Worker   void visitCatchSwitch(const CatchSwitchInst &I);
795*9880d681SAndroid Build Coastguard Worker   void visitCatchRet(const CatchReturnInst &I);
796*9880d681SAndroid Build Coastguard Worker   void visitCatchPad(const CatchPadInst &I);
797*9880d681SAndroid Build Coastguard Worker   void visitCleanupPad(const CleanupPadInst &CPI);
798*9880d681SAndroid Build Coastguard Worker 
799*9880d681SAndroid Build Coastguard Worker   BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
800*9880d681SAndroid Build Coastguard Worker                                        const MachineBasicBlock *Dst) const;
801*9880d681SAndroid Build Coastguard Worker   void addSuccessorWithProb(
802*9880d681SAndroid Build Coastguard Worker       MachineBasicBlock *Src, MachineBasicBlock *Dst,
803*9880d681SAndroid Build Coastguard Worker       BranchProbability Prob = BranchProbability::getUnknown());
804*9880d681SAndroid Build Coastguard Worker 
805*9880d681SAndroid Build Coastguard Worker public:
806*9880d681SAndroid Build Coastguard Worker   void visitSwitchCase(CaseBlock &CB,
807*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock *SwitchBB);
808*9880d681SAndroid Build Coastguard Worker   void visitSPDescriptorParent(StackProtectorDescriptor &SPD,
809*9880d681SAndroid Build Coastguard Worker                                MachineBasicBlock *ParentBB);
810*9880d681SAndroid Build Coastguard Worker   void visitSPDescriptorFailure(StackProtectorDescriptor &SPD);
811*9880d681SAndroid Build Coastguard Worker   void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB);
812*9880d681SAndroid Build Coastguard Worker   void visitBitTestCase(BitTestBlock &BB,
813*9880d681SAndroid Build Coastguard Worker                         MachineBasicBlock* NextMBB,
814*9880d681SAndroid Build Coastguard Worker                         BranchProbability BranchProbToNext,
815*9880d681SAndroid Build Coastguard Worker                         unsigned Reg,
816*9880d681SAndroid Build Coastguard Worker                         BitTestCase &B,
817*9880d681SAndroid Build Coastguard Worker                         MachineBasicBlock *SwitchBB);
818*9880d681SAndroid Build Coastguard Worker   void visitJumpTable(JumpTable &JT);
819*9880d681SAndroid Build Coastguard Worker   void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH,
820*9880d681SAndroid Build Coastguard Worker                             MachineBasicBlock *SwitchBB);
821*9880d681SAndroid Build Coastguard Worker 
822*9880d681SAndroid Build Coastguard Worker private:
823*9880d681SAndroid Build Coastguard Worker   // These all get lowered before this pass.
824*9880d681SAndroid Build Coastguard Worker   void visitInvoke(const InvokeInst &I);
825*9880d681SAndroid Build Coastguard Worker   void visitResume(const ResumeInst &I);
826*9880d681SAndroid Build Coastguard Worker 
827*9880d681SAndroid Build Coastguard Worker   void visitBinary(const User &I, unsigned OpCode);
828*9880d681SAndroid Build Coastguard Worker   void visitShift(const User &I, unsigned Opcode);
visitAdd(const User & I)829*9880d681SAndroid Build Coastguard Worker   void visitAdd(const User &I)  { visitBinary(I, ISD::ADD); }
visitFAdd(const User & I)830*9880d681SAndroid Build Coastguard Worker   void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
visitSub(const User & I)831*9880d681SAndroid Build Coastguard Worker   void visitSub(const User &I)  { visitBinary(I, ISD::SUB); }
832*9880d681SAndroid Build Coastguard Worker   void visitFSub(const User &I);
visitMul(const User & I)833*9880d681SAndroid Build Coastguard Worker   void visitMul(const User &I)  { visitBinary(I, ISD::MUL); }
visitFMul(const User & I)834*9880d681SAndroid Build Coastguard Worker   void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
visitURem(const User & I)835*9880d681SAndroid Build Coastguard Worker   void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
visitSRem(const User & I)836*9880d681SAndroid Build Coastguard Worker   void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
visitFRem(const User & I)837*9880d681SAndroid Build Coastguard Worker   void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
visitUDiv(const User & I)838*9880d681SAndroid Build Coastguard Worker   void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
839*9880d681SAndroid Build Coastguard Worker   void visitSDiv(const User &I);
visitFDiv(const User & I)840*9880d681SAndroid Build Coastguard Worker   void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
visitAnd(const User & I)841*9880d681SAndroid Build Coastguard Worker   void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
visitOr(const User & I)842*9880d681SAndroid Build Coastguard Worker   void visitOr  (const User &I) { visitBinary(I, ISD::OR); }
visitXor(const User & I)843*9880d681SAndroid Build Coastguard Worker   void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
visitShl(const User & I)844*9880d681SAndroid Build Coastguard Worker   void visitShl (const User &I) { visitShift(I, ISD::SHL); }
visitLShr(const User & I)845*9880d681SAndroid Build Coastguard Worker   void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
visitAShr(const User & I)846*9880d681SAndroid Build Coastguard Worker   void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
847*9880d681SAndroid Build Coastguard Worker   void visitICmp(const User &I);
848*9880d681SAndroid Build Coastguard Worker   void visitFCmp(const User &I);
849*9880d681SAndroid Build Coastguard Worker   // Visit the conversion instructions
850*9880d681SAndroid Build Coastguard Worker   void visitTrunc(const User &I);
851*9880d681SAndroid Build Coastguard Worker   void visitZExt(const User &I);
852*9880d681SAndroid Build Coastguard Worker   void visitSExt(const User &I);
853*9880d681SAndroid Build Coastguard Worker   void visitFPTrunc(const User &I);
854*9880d681SAndroid Build Coastguard Worker   void visitFPExt(const User &I);
855*9880d681SAndroid Build Coastguard Worker   void visitFPToUI(const User &I);
856*9880d681SAndroid Build Coastguard Worker   void visitFPToSI(const User &I);
857*9880d681SAndroid Build Coastguard Worker   void visitUIToFP(const User &I);
858*9880d681SAndroid Build Coastguard Worker   void visitSIToFP(const User &I);
859*9880d681SAndroid Build Coastguard Worker   void visitPtrToInt(const User &I);
860*9880d681SAndroid Build Coastguard Worker   void visitIntToPtr(const User &I);
861*9880d681SAndroid Build Coastguard Worker   void visitBitCast(const User &I);
862*9880d681SAndroid Build Coastguard Worker   void visitAddrSpaceCast(const User &I);
863*9880d681SAndroid Build Coastguard Worker 
864*9880d681SAndroid Build Coastguard Worker   void visitExtractElement(const User &I);
865*9880d681SAndroid Build Coastguard Worker   void visitInsertElement(const User &I);
866*9880d681SAndroid Build Coastguard Worker   void visitShuffleVector(const User &I);
867*9880d681SAndroid Build Coastguard Worker 
868*9880d681SAndroid Build Coastguard Worker   void visitExtractValue(const ExtractValueInst &I);
869*9880d681SAndroid Build Coastguard Worker   void visitInsertValue(const InsertValueInst &I);
870*9880d681SAndroid Build Coastguard Worker   void visitLandingPad(const LandingPadInst &I);
871*9880d681SAndroid Build Coastguard Worker 
872*9880d681SAndroid Build Coastguard Worker   void visitGetElementPtr(const User &I);
873*9880d681SAndroid Build Coastguard Worker   void visitSelect(const User &I);
874*9880d681SAndroid Build Coastguard Worker 
875*9880d681SAndroid Build Coastguard Worker   void visitAlloca(const AllocaInst &I);
876*9880d681SAndroid Build Coastguard Worker   void visitLoad(const LoadInst &I);
877*9880d681SAndroid Build Coastguard Worker   void visitStore(const StoreInst &I);
878*9880d681SAndroid Build Coastguard Worker   void visitMaskedLoad(const CallInst &I);
879*9880d681SAndroid Build Coastguard Worker   void visitMaskedStore(const CallInst &I);
880*9880d681SAndroid Build Coastguard Worker   void visitMaskedGather(const CallInst &I);
881*9880d681SAndroid Build Coastguard Worker   void visitMaskedScatter(const CallInst &I);
882*9880d681SAndroid Build Coastguard Worker   void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
883*9880d681SAndroid Build Coastguard Worker   void visitAtomicRMW(const AtomicRMWInst &I);
884*9880d681SAndroid Build Coastguard Worker   void visitFence(const FenceInst &I);
885*9880d681SAndroid Build Coastguard Worker   void visitPHI(const PHINode &I);
886*9880d681SAndroid Build Coastguard Worker   void visitCall(const CallInst &I);
887*9880d681SAndroid Build Coastguard Worker   bool visitMemCmpCall(const CallInst &I);
888*9880d681SAndroid Build Coastguard Worker   bool visitMemChrCall(const CallInst &I);
889*9880d681SAndroid Build Coastguard Worker   bool visitStrCpyCall(const CallInst &I, bool isStpcpy);
890*9880d681SAndroid Build Coastguard Worker   bool visitStrCmpCall(const CallInst &I);
891*9880d681SAndroid Build Coastguard Worker   bool visitStrLenCall(const CallInst &I);
892*9880d681SAndroid Build Coastguard Worker   bool visitStrNLenCall(const CallInst &I);
893*9880d681SAndroid Build Coastguard Worker   bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
894*9880d681SAndroid Build Coastguard Worker   bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode);
895*9880d681SAndroid Build Coastguard Worker   void visitAtomicLoad(const LoadInst &I);
896*9880d681SAndroid Build Coastguard Worker   void visitAtomicStore(const StoreInst &I);
897*9880d681SAndroid Build Coastguard Worker   void visitLoadFromSwiftError(const LoadInst &I);
898*9880d681SAndroid Build Coastguard Worker   void visitStoreToSwiftError(const StoreInst &I);
899*9880d681SAndroid Build Coastguard Worker 
900*9880d681SAndroid Build Coastguard Worker   void visitInlineAsm(ImmutableCallSite CS);
901*9880d681SAndroid Build Coastguard Worker   const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
902*9880d681SAndroid Build Coastguard Worker   void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
903*9880d681SAndroid Build Coastguard Worker 
904*9880d681SAndroid Build Coastguard Worker   void visitVAStart(const CallInst &I);
905*9880d681SAndroid Build Coastguard Worker   void visitVAArg(const VAArgInst &I);
906*9880d681SAndroid Build Coastguard Worker   void visitVAEnd(const CallInst &I);
907*9880d681SAndroid Build Coastguard Worker   void visitVACopy(const CallInst &I);
908*9880d681SAndroid Build Coastguard Worker   void visitStackmap(const CallInst &I);
909*9880d681SAndroid Build Coastguard Worker   void visitPatchpoint(ImmutableCallSite CS,
910*9880d681SAndroid Build Coastguard Worker                        const BasicBlock *EHPadBB = nullptr);
911*9880d681SAndroid Build Coastguard Worker 
912*9880d681SAndroid Build Coastguard Worker   // These two are implemented in StatepointLowering.cpp
913*9880d681SAndroid Build Coastguard Worker   void visitGCRelocate(const GCRelocateInst &I);
914*9880d681SAndroid Build Coastguard Worker   void visitGCResult(const GCResultInst &I);
915*9880d681SAndroid Build Coastguard Worker 
visitUserOp1(const Instruction & I)916*9880d681SAndroid Build Coastguard Worker   void visitUserOp1(const Instruction &I) {
917*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("UserOp1 should not exist at instruction selection time!");
918*9880d681SAndroid Build Coastguard Worker   }
visitUserOp2(const Instruction & I)919*9880d681SAndroid Build Coastguard Worker   void visitUserOp2(const Instruction &I) {
920*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("UserOp2 should not exist at instruction selection time!");
921*9880d681SAndroid Build Coastguard Worker   }
922*9880d681SAndroid Build Coastguard Worker 
923*9880d681SAndroid Build Coastguard Worker   void processIntegerCallValue(const Instruction &I,
924*9880d681SAndroid Build Coastguard Worker                                SDValue Value, bool IsSigned);
925*9880d681SAndroid Build Coastguard Worker 
926*9880d681SAndroid Build Coastguard Worker   void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
927*9880d681SAndroid Build Coastguard Worker 
928*9880d681SAndroid Build Coastguard Worker   void emitInlineAsmError(ImmutableCallSite CS, const Twine &Message);
929*9880d681SAndroid Build Coastguard Worker 
930*9880d681SAndroid Build Coastguard Worker   /// EmitFuncArgumentDbgValue - If V is an function argument then create
931*9880d681SAndroid Build Coastguard Worker   /// corresponding DBG_VALUE machine instruction for it now. At the end of
932*9880d681SAndroid Build Coastguard Worker   /// instruction selection, they will be inserted to the entry BB.
933*9880d681SAndroid Build Coastguard Worker   bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable,
934*9880d681SAndroid Build Coastguard Worker                                 DIExpression *Expr, DILocation *DL,
935*9880d681SAndroid Build Coastguard Worker                                 int64_t Offset, bool IsIndirect,
936*9880d681SAndroid Build Coastguard Worker                                 const SDValue &N);
937*9880d681SAndroid Build Coastguard Worker 
938*9880d681SAndroid Build Coastguard Worker   /// Return the next block after MBB, or nullptr if there is none.
939*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *NextBlock(MachineBasicBlock *MBB);
940*9880d681SAndroid Build Coastguard Worker 
941*9880d681SAndroid Build Coastguard Worker   /// Update the DAG and DAG builder with the relevant information after
942*9880d681SAndroid Build Coastguard Worker   /// a new root node has been created which could be a tail call.
943*9880d681SAndroid Build Coastguard Worker   void updateDAGForMaybeTailCall(SDValue MaybeTC);
944*9880d681SAndroid Build Coastguard Worker };
945*9880d681SAndroid Build Coastguard Worker 
946*9880d681SAndroid Build Coastguard Worker /// RegsForValue - This struct represents the registers (physical or virtual)
947*9880d681SAndroid Build Coastguard Worker /// that a particular set of values is assigned, and the type information about
948*9880d681SAndroid Build Coastguard Worker /// the value. The most common situation is to represent one value at a time,
949*9880d681SAndroid Build Coastguard Worker /// but struct or array values are handled element-wise as multiple values.  The
950*9880d681SAndroid Build Coastguard Worker /// splitting of aggregates is performed recursively, so that we never have
951*9880d681SAndroid Build Coastguard Worker /// aggregate-typed registers. The values at this point do not necessarily have
952*9880d681SAndroid Build Coastguard Worker /// legal types, so each value may require one or more registers of some legal
953*9880d681SAndroid Build Coastguard Worker /// type.
954*9880d681SAndroid Build Coastguard Worker ///
955*9880d681SAndroid Build Coastguard Worker struct RegsForValue {
956*9880d681SAndroid Build Coastguard Worker   /// ValueVTs - The value types of the values, which may not be legal, and
957*9880d681SAndroid Build Coastguard Worker   /// may need be promoted or synthesized from one or more registers.
958*9880d681SAndroid Build Coastguard Worker   ///
959*9880d681SAndroid Build Coastguard Worker   SmallVector<EVT, 4> ValueVTs;
960*9880d681SAndroid Build Coastguard Worker 
961*9880d681SAndroid Build Coastguard Worker   /// RegVTs - The value types of the registers. This is the same size as
962*9880d681SAndroid Build Coastguard Worker   /// ValueVTs and it records, for each value, what the type of the assigned
963*9880d681SAndroid Build Coastguard Worker   /// register or registers are. (Individual values are never synthesized
964*9880d681SAndroid Build Coastguard Worker   /// from more than one type of register.)
965*9880d681SAndroid Build Coastguard Worker   ///
966*9880d681SAndroid Build Coastguard Worker   /// With virtual registers, the contents of RegVTs is redundant with TLI's
967*9880d681SAndroid Build Coastguard Worker   /// getRegisterType member function, however when with physical registers
968*9880d681SAndroid Build Coastguard Worker   /// it is necessary to have a separate record of the types.
969*9880d681SAndroid Build Coastguard Worker   ///
970*9880d681SAndroid Build Coastguard Worker   SmallVector<MVT, 4> RegVTs;
971*9880d681SAndroid Build Coastguard Worker 
972*9880d681SAndroid Build Coastguard Worker   /// Regs - This list holds the registers assigned to the values.
973*9880d681SAndroid Build Coastguard Worker   /// Each legal or promoted value requires one register, and each
974*9880d681SAndroid Build Coastguard Worker   /// expanded value requires multiple registers.
975*9880d681SAndroid Build Coastguard Worker   ///
976*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 4> Regs;
977*9880d681SAndroid Build Coastguard Worker 
978*9880d681SAndroid Build Coastguard Worker   RegsForValue();
979*9880d681SAndroid Build Coastguard Worker 
980*9880d681SAndroid Build Coastguard Worker   RegsForValue(const SmallVector<unsigned, 4> &regs, MVT regvt, EVT valuevt);
981*9880d681SAndroid Build Coastguard Worker 
982*9880d681SAndroid Build Coastguard Worker   RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
983*9880d681SAndroid Build Coastguard Worker                const DataLayout &DL, unsigned Reg, Type *Ty);
984*9880d681SAndroid Build Coastguard Worker 
985*9880d681SAndroid Build Coastguard Worker   /// append - Add the specified values to this one.
appendRegsForValue986*9880d681SAndroid Build Coastguard Worker   void append(const RegsForValue &RHS) {
987*9880d681SAndroid Build Coastguard Worker     ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
988*9880d681SAndroid Build Coastguard Worker     RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
989*9880d681SAndroid Build Coastguard Worker     Regs.append(RHS.Regs.begin(), RHS.Regs.end());
990*9880d681SAndroid Build Coastguard Worker   }
991*9880d681SAndroid Build Coastguard Worker 
992*9880d681SAndroid Build Coastguard Worker   /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
993*9880d681SAndroid Build Coastguard Worker   /// this value and returns the result as a ValueVTs value.  This uses
994*9880d681SAndroid Build Coastguard Worker   /// Chain/Flag as the input and updates them for the output Chain/Flag.
995*9880d681SAndroid Build Coastguard Worker   /// If the Flag pointer is NULL, no flag is used.
996*9880d681SAndroid Build Coastguard Worker   SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
997*9880d681SAndroid Build Coastguard Worker                           const SDLoc &dl, SDValue &Chain, SDValue *Flag,
998*9880d681SAndroid Build Coastguard Worker                           const Value *V = nullptr) const;
999*9880d681SAndroid Build Coastguard Worker 
1000*9880d681SAndroid Build Coastguard Worker   /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the specified
1001*9880d681SAndroid Build Coastguard Worker   /// value into the registers specified by this object.  This uses Chain/Flag
1002*9880d681SAndroid Build Coastguard Worker   /// as the input and updates them for the output Chain/Flag.  If the Flag
1003*9880d681SAndroid Build Coastguard Worker   /// pointer is nullptr, no flag is used.  If V is not nullptr, then it is used
1004*9880d681SAndroid Build Coastguard Worker   /// in printing better diagnostic messages on error.
1005*9880d681SAndroid Build Coastguard Worker   void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl,
1006*9880d681SAndroid Build Coastguard Worker                      SDValue &Chain, SDValue *Flag, const Value *V = nullptr,
1007*9880d681SAndroid Build Coastguard Worker                      ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const;
1008*9880d681SAndroid Build Coastguard Worker 
1009*9880d681SAndroid Build Coastguard Worker   /// AddInlineAsmOperands - Add this value to the specified inlineasm node
1010*9880d681SAndroid Build Coastguard Worker   /// operand list.  This adds the code marker, matching input operand index
1011*9880d681SAndroid Build Coastguard Worker   /// (if applicable), and includes the number of values added into it.
1012*9880d681SAndroid Build Coastguard Worker   void AddInlineAsmOperands(unsigned Kind, bool HasMatching,
1013*9880d681SAndroid Build Coastguard Worker                             unsigned MatchingIdx, const SDLoc &dl,
1014*9880d681SAndroid Build Coastguard Worker                             SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
1015*9880d681SAndroid Build Coastguard Worker };
1016*9880d681SAndroid Build Coastguard Worker 
1017*9880d681SAndroid Build Coastguard Worker } // end namespace llvm
1018*9880d681SAndroid Build Coastguard Worker 
1019*9880d681SAndroid Build Coastguard Worker #endif
1020