1*9880d681SAndroid Build Coastguard Worker //===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
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 pass moves instructions into successor blocks when possible, so that
11*9880d681SAndroid Build Coastguard Worker // they aren't executed on paths where their results aren't needed.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // This pass is not intended to be a replacement or a complete alternative
14*9880d681SAndroid Build Coastguard Worker // for an LLVM-IR-level sinking pass. It is only designed to sink simple
15*9880d681SAndroid Build Coastguard Worker // constructs that are not exposed before lowering and instruction selection.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SparseBitVector.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachinePostDominators.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
37*9880d681SAndroid Build Coastguard Worker using namespace llvm;
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "machine-sink"
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
42*9880d681SAndroid Build Coastguard Worker SplitEdges("machine-sink-split",
43*9880d681SAndroid Build Coastguard Worker cl::desc("Split critical edges during machine sinking"),
44*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
47*9880d681SAndroid Build Coastguard Worker UseBlockFreqInfo("machine-sink-bfi",
48*9880d681SAndroid Build Coastguard Worker cl::desc("Use block frequency info to find successors to sink"),
49*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSunk, "Number of machine instructions sunk");
53*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSplit, "Number of critical edges split");
54*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCoalesces, "Number of copies coalesced");
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker namespace {
57*9880d681SAndroid Build Coastguard Worker class MachineSinking : public MachineFunctionPass {
58*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
59*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
60*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI; // Machine register information
61*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DT; // Machine dominator tree
62*9880d681SAndroid Build Coastguard Worker MachinePostDominatorTree *PDT; // Machine post dominator tree
63*9880d681SAndroid Build Coastguard Worker MachineLoopInfo *LI;
64*9880d681SAndroid Build Coastguard Worker const MachineBlockFrequencyInfo *MBFI;
65*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA;
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker // Remember which edges have been considered for breaking.
68*9880d681SAndroid Build Coastguard Worker SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
69*9880d681SAndroid Build Coastguard Worker CEBCandidates;
70*9880d681SAndroid Build Coastguard Worker // Remember which edges we are about to split.
71*9880d681SAndroid Build Coastguard Worker // This is different from CEBCandidates since those edges
72*9880d681SAndroid Build Coastguard Worker // will be split.
73*9880d681SAndroid Build Coastguard Worker SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit;
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker SparseBitVector<> RegsToClearKillFlags;
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker typedef std::map<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>
78*9880d681SAndroid Build Coastguard Worker AllSuccsCache;
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker public:
81*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification
MachineSinking()82*9880d681SAndroid Build Coastguard Worker MachineSinking() : MachineFunctionPass(ID) {
83*9880d681SAndroid Build Coastguard Worker initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
87*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const88*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
89*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
90*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
91*9880d681SAndroid Build Coastguard Worker AU.addRequired<AAResultsWrapperPass>();
92*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
93*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachinePostDominatorTree>();
94*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineLoopInfo>();
95*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
96*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachinePostDominatorTree>();
97*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineLoopInfo>();
98*9880d681SAndroid Build Coastguard Worker if (UseBlockFreqInfo)
99*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineBlockFrequencyInfo>();
100*9880d681SAndroid Build Coastguard Worker }
101*9880d681SAndroid Build Coastguard Worker
releaseMemory()102*9880d681SAndroid Build Coastguard Worker void releaseMemory() override {
103*9880d681SAndroid Build Coastguard Worker CEBCandidates.clear();
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker private:
107*9880d681SAndroid Build Coastguard Worker bool ProcessBlock(MachineBasicBlock &MBB);
108*9880d681SAndroid Build Coastguard Worker bool isWorthBreakingCriticalEdge(MachineInstr &MI,
109*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *From,
110*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *To);
111*9880d681SAndroid Build Coastguard Worker /// \brief Postpone the splitting of the given critical
112*9880d681SAndroid Build Coastguard Worker /// edge (\p From, \p To).
113*9880d681SAndroid Build Coastguard Worker ///
114*9880d681SAndroid Build Coastguard Worker /// We do not split the edges on the fly. Indeed, this invalidates
115*9880d681SAndroid Build Coastguard Worker /// the dominance information and thus triggers a lot of updates
116*9880d681SAndroid Build Coastguard Worker /// of that information underneath.
117*9880d681SAndroid Build Coastguard Worker /// Instead, we postpone all the splits after each iteration of
118*9880d681SAndroid Build Coastguard Worker /// the main loop. That way, the information is at least valid
119*9880d681SAndroid Build Coastguard Worker /// for the lifetime of an iteration.
120*9880d681SAndroid Build Coastguard Worker ///
121*9880d681SAndroid Build Coastguard Worker /// \return True if the edge is marked as toSplit, false otherwise.
122*9880d681SAndroid Build Coastguard Worker /// False can be returned if, for instance, this is not profitable.
123*9880d681SAndroid Build Coastguard Worker bool PostponeSplitCriticalEdge(MachineInstr &MI,
124*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *From,
125*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *To,
126*9880d681SAndroid Build Coastguard Worker bool BreakPHIEdge);
127*9880d681SAndroid Build Coastguard Worker bool SinkInstruction(MachineInstr &MI, bool &SawStore,
128*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors);
129*9880d681SAndroid Build Coastguard Worker bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
130*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *DefMBB,
131*9880d681SAndroid Build Coastguard Worker bool &BreakPHIEdge, bool &LocalUse) const;
132*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
133*9880d681SAndroid Build Coastguard Worker bool &BreakPHIEdge, AllSuccsCache &AllSuccessors);
134*9880d681SAndroid Build Coastguard Worker bool isProfitableToSinkTo(unsigned Reg, MachineInstr &MI,
135*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB,
136*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccToSinkTo,
137*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors);
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard Worker bool PerformTrivialForwardCoalescing(MachineInstr &MI,
140*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB);
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock *, 4> &
143*9880d681SAndroid Build Coastguard Worker GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
144*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors) const;
145*9880d681SAndroid Build Coastguard Worker };
146*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker char MachineSinking::ID = 0;
149*9880d681SAndroid Build Coastguard Worker char &llvm::MachineSinkingID = MachineSinking::ID;
150*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
151*9880d681SAndroid Build Coastguard Worker "Machine code sinking", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)152*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
153*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
154*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
155*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MachineSinking, "machine-sink",
156*9880d681SAndroid Build Coastguard Worker "Machine code sinking", false, false)
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
159*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB) {
160*9880d681SAndroid Build Coastguard Worker if (!MI.isCopy())
161*9880d681SAndroid Build Coastguard Worker return false;
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = MI.getOperand(1).getReg();
164*9880d681SAndroid Build Coastguard Worker unsigned DstReg = MI.getOperand(0).getReg();
165*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
166*9880d681SAndroid Build Coastguard Worker !TargetRegisterInfo::isVirtualRegister(DstReg) ||
167*9880d681SAndroid Build Coastguard Worker !MRI->hasOneNonDBGUse(SrcReg))
168*9880d681SAndroid Build Coastguard Worker return false;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
171*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
172*9880d681SAndroid Build Coastguard Worker if (SRC != DRC)
173*9880d681SAndroid Build Coastguard Worker return false;
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
176*9880d681SAndroid Build Coastguard Worker if (DefMI->isCopyLike())
177*9880d681SAndroid Build Coastguard Worker return false;
178*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Coalescing: " << *DefMI);
179*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "*** to: " << MI);
180*9880d681SAndroid Build Coastguard Worker MRI->replaceRegWith(DstReg, SrcReg);
181*9880d681SAndroid Build Coastguard Worker MI.eraseFromParent();
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker // Conservatively, clear any kill flags, since it's possible that they are no
184*9880d681SAndroid Build Coastguard Worker // longer correct.
185*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(SrcReg);
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker ++NumCoalesces;
188*9880d681SAndroid Build Coastguard Worker return true;
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker /// AllUsesDominatedByBlock - Return true if all uses of the specified register
192*9880d681SAndroid Build Coastguard Worker /// occur in blocks dominated by the specified block. If any use is in the
193*9880d681SAndroid Build Coastguard Worker /// definition block, then return false since it is never legal to move def
194*9880d681SAndroid Build Coastguard Worker /// after uses.
195*9880d681SAndroid Build Coastguard Worker bool
AllUsesDominatedByBlock(unsigned Reg,MachineBasicBlock * MBB,MachineBasicBlock * DefMBB,bool & BreakPHIEdge,bool & LocalUse) const196*9880d681SAndroid Build Coastguard Worker MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
197*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB,
198*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *DefMBB,
199*9880d681SAndroid Build Coastguard Worker bool &BreakPHIEdge,
200*9880d681SAndroid Build Coastguard Worker bool &LocalUse) const {
201*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
202*9880d681SAndroid Build Coastguard Worker "Only makes sense for vregs");
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker // Ignore debug uses because debug info doesn't affect the code.
205*9880d681SAndroid Build Coastguard Worker if (MRI->use_nodbg_empty(Reg))
206*9880d681SAndroid Build Coastguard Worker return true;
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
209*9880d681SAndroid Build Coastguard Worker // into and they are all PHI nodes. In this case, machine-sink must break
210*9880d681SAndroid Build Coastguard Worker // the critical edge first. e.g.
211*9880d681SAndroid Build Coastguard Worker //
212*9880d681SAndroid Build Coastguard Worker // BB#1: derived from LLVM BB %bb4.preheader
213*9880d681SAndroid Build Coastguard Worker // Predecessors according to CFG: BB#0
214*9880d681SAndroid Build Coastguard Worker // ...
215*9880d681SAndroid Build Coastguard Worker // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
216*9880d681SAndroid Build Coastguard Worker // ...
217*9880d681SAndroid Build Coastguard Worker // JE_4 <BB#37>, %EFLAGS<imp-use>
218*9880d681SAndroid Build Coastguard Worker // Successors according to CFG: BB#37 BB#2
219*9880d681SAndroid Build Coastguard Worker //
220*9880d681SAndroid Build Coastguard Worker // BB#2: derived from LLVM BB %bb.nph
221*9880d681SAndroid Build Coastguard Worker // Predecessors according to CFG: BB#0 BB#1
222*9880d681SAndroid Build Coastguard Worker // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
223*9880d681SAndroid Build Coastguard Worker BreakPHIEdge = true;
224*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
225*9880d681SAndroid Build Coastguard Worker MachineInstr *UseInst = MO.getParent();
226*9880d681SAndroid Build Coastguard Worker unsigned OpNo = &MO - &UseInst->getOperand(0);
227*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *UseBlock = UseInst->getParent();
228*9880d681SAndroid Build Coastguard Worker if (!(UseBlock == MBB && UseInst->isPHI() &&
229*9880d681SAndroid Build Coastguard Worker UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) {
230*9880d681SAndroid Build Coastguard Worker BreakPHIEdge = false;
231*9880d681SAndroid Build Coastguard Worker break;
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker if (BreakPHIEdge)
235*9880d681SAndroid Build Coastguard Worker return true;
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
238*9880d681SAndroid Build Coastguard Worker // Determine the block of the use.
239*9880d681SAndroid Build Coastguard Worker MachineInstr *UseInst = MO.getParent();
240*9880d681SAndroid Build Coastguard Worker unsigned OpNo = &MO - &UseInst->getOperand(0);
241*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *UseBlock = UseInst->getParent();
242*9880d681SAndroid Build Coastguard Worker if (UseInst->isPHI()) {
243*9880d681SAndroid Build Coastguard Worker // PHI nodes use the operand in the predecessor block, not the block with
244*9880d681SAndroid Build Coastguard Worker // the PHI.
245*9880d681SAndroid Build Coastguard Worker UseBlock = UseInst->getOperand(OpNo+1).getMBB();
246*9880d681SAndroid Build Coastguard Worker } else if (UseBlock == DefMBB) {
247*9880d681SAndroid Build Coastguard Worker LocalUse = true;
248*9880d681SAndroid Build Coastguard Worker return false;
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker // Check that it dominates.
252*9880d681SAndroid Build Coastguard Worker if (!DT->dominates(MBB, UseBlock))
253*9880d681SAndroid Build Coastguard Worker return false;
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker return true;
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)259*9880d681SAndroid Build Coastguard Worker bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
260*9880d681SAndroid Build Coastguard Worker if (skipFunction(*MF.getFunction()))
261*9880d681SAndroid Build Coastguard Worker return false;
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "******** Machine Sinking ********\n");
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker TII = MF.getSubtarget().getInstrInfo();
266*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
267*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
268*9880d681SAndroid Build Coastguard Worker DT = &getAnalysis<MachineDominatorTree>();
269*9880d681SAndroid Build Coastguard Worker PDT = &getAnalysis<MachinePostDominatorTree>();
270*9880d681SAndroid Build Coastguard Worker LI = &getAnalysis<MachineLoopInfo>();
271*9880d681SAndroid Build Coastguard Worker MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
272*9880d681SAndroid Build Coastguard Worker AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
273*9880d681SAndroid Build Coastguard Worker
274*9880d681SAndroid Build Coastguard Worker bool EverMadeChange = false;
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker while (1) {
277*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard Worker // Process all basic blocks.
280*9880d681SAndroid Build Coastguard Worker CEBCandidates.clear();
281*9880d681SAndroid Build Coastguard Worker ToSplit.clear();
282*9880d681SAndroid Build Coastguard Worker for (auto &MBB: MF)
283*9880d681SAndroid Build Coastguard Worker MadeChange |= ProcessBlock(MBB);
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker // If we have anything we marked as toSplit, split it now.
286*9880d681SAndroid Build Coastguard Worker for (auto &Pair : ToSplit) {
287*9880d681SAndroid Build Coastguard Worker auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this);
288*9880d681SAndroid Build Coastguard Worker if (NewSucc != nullptr) {
289*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** Splitting critical edge:"
290*9880d681SAndroid Build Coastguard Worker " BB#" << Pair.first->getNumber()
291*9880d681SAndroid Build Coastguard Worker << " -- BB#" << NewSucc->getNumber()
292*9880d681SAndroid Build Coastguard Worker << " -- BB#" << Pair.second->getNumber() << '\n');
293*9880d681SAndroid Build Coastguard Worker MadeChange = true;
294*9880d681SAndroid Build Coastguard Worker ++NumSplit;
295*9880d681SAndroid Build Coastguard Worker } else
296*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** Not legal to break critical edge\n");
297*9880d681SAndroid Build Coastguard Worker }
298*9880d681SAndroid Build Coastguard Worker // If this iteration over the code changed anything, keep iterating.
299*9880d681SAndroid Build Coastguard Worker if (!MadeChange) break;
300*9880d681SAndroid Build Coastguard Worker EverMadeChange = true;
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker // Now clear any kill flags for recorded registers.
304*9880d681SAndroid Build Coastguard Worker for (auto I : RegsToClearKillFlags)
305*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(I);
306*9880d681SAndroid Build Coastguard Worker RegsToClearKillFlags.clear();
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker return EverMadeChange;
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
ProcessBlock(MachineBasicBlock & MBB)311*9880d681SAndroid Build Coastguard Worker bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
312*9880d681SAndroid Build Coastguard Worker // Can't sink anything out of a block that has less than two successors.
313*9880d681SAndroid Build Coastguard Worker if (MBB.succ_size() <= 1 || MBB.empty()) return false;
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard Worker // Don't bother sinking code out of unreachable blocks. In addition to being
316*9880d681SAndroid Build Coastguard Worker // unprofitable, it can also lead to infinite looping, because in an
317*9880d681SAndroid Build Coastguard Worker // unreachable loop there may be nowhere to stop.
318*9880d681SAndroid Build Coastguard Worker if (!DT->isReachableFromEntry(&MBB)) return false;
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker // Cache all successors, sorted by frequency info and loop depth.
323*9880d681SAndroid Build Coastguard Worker AllSuccsCache AllSuccessors;
324*9880d681SAndroid Build Coastguard Worker
325*9880d681SAndroid Build Coastguard Worker // Walk the basic block bottom-up. Remember if we saw a store.
326*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.end();
327*9880d681SAndroid Build Coastguard Worker --I;
328*9880d681SAndroid Build Coastguard Worker bool ProcessedBegin, SawStore = false;
329*9880d681SAndroid Build Coastguard Worker do {
330*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *I; // The instruction to sink.
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker // Predecrement I (if it's not begin) so that it isn't invalidated by
333*9880d681SAndroid Build Coastguard Worker // sinking.
334*9880d681SAndroid Build Coastguard Worker ProcessedBegin = I == MBB.begin();
335*9880d681SAndroid Build Coastguard Worker if (!ProcessedBegin)
336*9880d681SAndroid Build Coastguard Worker --I;
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard Worker if (MI.isDebugValue())
339*9880d681SAndroid Build Coastguard Worker continue;
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
342*9880d681SAndroid Build Coastguard Worker if (Joined) {
343*9880d681SAndroid Build Coastguard Worker MadeChange = true;
344*9880d681SAndroid Build Coastguard Worker continue;
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker if (SinkInstruction(MI, SawStore, AllSuccessors)) {
348*9880d681SAndroid Build Coastguard Worker ++NumSunk;
349*9880d681SAndroid Build Coastguard Worker MadeChange = true;
350*9880d681SAndroid Build Coastguard Worker }
351*9880d681SAndroid Build Coastguard Worker
352*9880d681SAndroid Build Coastguard Worker // If we just processed the first instruction in the block, we're done.
353*9880d681SAndroid Build Coastguard Worker } while (!ProcessedBegin);
354*9880d681SAndroid Build Coastguard Worker
355*9880d681SAndroid Build Coastguard Worker return MadeChange;
356*9880d681SAndroid Build Coastguard Worker }
357*9880d681SAndroid Build Coastguard Worker
isWorthBreakingCriticalEdge(MachineInstr & MI,MachineBasicBlock * From,MachineBasicBlock * To)358*9880d681SAndroid Build Coastguard Worker bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr &MI,
359*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *From,
360*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *To) {
361*9880d681SAndroid Build Coastguard Worker // FIXME: Need much better heuristics.
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker // If the pass has already considered breaking this edge (during this pass
364*9880d681SAndroid Build Coastguard Worker // through the function), then let's go ahead and break it. This means
365*9880d681SAndroid Build Coastguard Worker // sinking multiple "cheap" instructions into the same block.
366*9880d681SAndroid Build Coastguard Worker if (!CEBCandidates.insert(std::make_pair(From, To)).second)
367*9880d681SAndroid Build Coastguard Worker return true;
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
370*9880d681SAndroid Build Coastguard Worker return true;
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker // MI is cheap, we probably don't want to break the critical edge for it.
373*9880d681SAndroid Build Coastguard Worker // However, if this would allow some definitions of its source operands
374*9880d681SAndroid Build Coastguard Worker // to be sunk then it's probably worth it.
375*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
376*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
377*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse())
378*9880d681SAndroid Build Coastguard Worker continue;
379*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
380*9880d681SAndroid Build Coastguard Worker if (Reg == 0)
381*9880d681SAndroid Build Coastguard Worker continue;
382*9880d681SAndroid Build Coastguard Worker
383*9880d681SAndroid Build Coastguard Worker // We don't move live definitions of physical registers,
384*9880d681SAndroid Build Coastguard Worker // so sinking their uses won't enable any opportunities.
385*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg))
386*9880d681SAndroid Build Coastguard Worker continue;
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker // If this instruction is the only user of a virtual register,
389*9880d681SAndroid Build Coastguard Worker // check if breaking the edge will enable sinking
390*9880d681SAndroid Build Coastguard Worker // both this instruction and the defining instruction.
391*9880d681SAndroid Build Coastguard Worker if (MRI->hasOneNonDBGUse(Reg)) {
392*9880d681SAndroid Build Coastguard Worker // If the definition resides in same MBB,
393*9880d681SAndroid Build Coastguard Worker // claim it's likely we can sink these together.
394*9880d681SAndroid Build Coastguard Worker // If definition resides elsewhere, we aren't
395*9880d681SAndroid Build Coastguard Worker // blocking it from being sunk so don't break the edge.
396*9880d681SAndroid Build Coastguard Worker MachineInstr *DefMI = MRI->getVRegDef(Reg);
397*9880d681SAndroid Build Coastguard Worker if (DefMI->getParent() == MI.getParent())
398*9880d681SAndroid Build Coastguard Worker return true;
399*9880d681SAndroid Build Coastguard Worker }
400*9880d681SAndroid Build Coastguard Worker }
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard Worker return false;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker
PostponeSplitCriticalEdge(MachineInstr & MI,MachineBasicBlock * FromBB,MachineBasicBlock * ToBB,bool BreakPHIEdge)405*9880d681SAndroid Build Coastguard Worker bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
406*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *FromBB,
407*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *ToBB,
408*9880d681SAndroid Build Coastguard Worker bool BreakPHIEdge) {
409*9880d681SAndroid Build Coastguard Worker if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
410*9880d681SAndroid Build Coastguard Worker return false;
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker // Avoid breaking back edge. From == To means backedge for single BB loop.
413*9880d681SAndroid Build Coastguard Worker if (!SplitEdges || FromBB == ToBB)
414*9880d681SAndroid Build Coastguard Worker return false;
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard Worker // Check for backedges of more "complex" loops.
417*9880d681SAndroid Build Coastguard Worker if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
418*9880d681SAndroid Build Coastguard Worker LI->isLoopHeader(ToBB))
419*9880d681SAndroid Build Coastguard Worker return false;
420*9880d681SAndroid Build Coastguard Worker
421*9880d681SAndroid Build Coastguard Worker // It's not always legal to break critical edges and sink the computation
422*9880d681SAndroid Build Coastguard Worker // to the edge.
423*9880d681SAndroid Build Coastguard Worker //
424*9880d681SAndroid Build Coastguard Worker // BB#1:
425*9880d681SAndroid Build Coastguard Worker // v1024
426*9880d681SAndroid Build Coastguard Worker // Beq BB#3
427*9880d681SAndroid Build Coastguard Worker // <fallthrough>
428*9880d681SAndroid Build Coastguard Worker // BB#2:
429*9880d681SAndroid Build Coastguard Worker // ... no uses of v1024
430*9880d681SAndroid Build Coastguard Worker // <fallthrough>
431*9880d681SAndroid Build Coastguard Worker // BB#3:
432*9880d681SAndroid Build Coastguard Worker // ...
433*9880d681SAndroid Build Coastguard Worker // = v1024
434*9880d681SAndroid Build Coastguard Worker //
435*9880d681SAndroid Build Coastguard Worker // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
436*9880d681SAndroid Build Coastguard Worker //
437*9880d681SAndroid Build Coastguard Worker // BB#1:
438*9880d681SAndroid Build Coastguard Worker // ...
439*9880d681SAndroid Build Coastguard Worker // Bne BB#2
440*9880d681SAndroid Build Coastguard Worker // BB#4:
441*9880d681SAndroid Build Coastguard Worker // v1024 =
442*9880d681SAndroid Build Coastguard Worker // B BB#3
443*9880d681SAndroid Build Coastguard Worker // BB#2:
444*9880d681SAndroid Build Coastguard Worker // ... no uses of v1024
445*9880d681SAndroid Build Coastguard Worker // <fallthrough>
446*9880d681SAndroid Build Coastguard Worker // BB#3:
447*9880d681SAndroid Build Coastguard Worker // ...
448*9880d681SAndroid Build Coastguard Worker // = v1024
449*9880d681SAndroid Build Coastguard Worker //
450*9880d681SAndroid Build Coastguard Worker // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
451*9880d681SAndroid Build Coastguard Worker // flow. We need to ensure the new basic block where the computation is
452*9880d681SAndroid Build Coastguard Worker // sunk to dominates all the uses.
453*9880d681SAndroid Build Coastguard Worker // It's only legal to break critical edge and sink the computation to the
454*9880d681SAndroid Build Coastguard Worker // new block if all the predecessors of "To", except for "From", are
455*9880d681SAndroid Build Coastguard Worker // not dominated by "From". Given SSA property, this means these
456*9880d681SAndroid Build Coastguard Worker // predecessors are dominated by "To".
457*9880d681SAndroid Build Coastguard Worker //
458*9880d681SAndroid Build Coastguard Worker // There is no need to do this check if all the uses are PHI nodes. PHI
459*9880d681SAndroid Build Coastguard Worker // sources are only defined on the specific predecessor edges.
460*9880d681SAndroid Build Coastguard Worker if (!BreakPHIEdge) {
461*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
462*9880d681SAndroid Build Coastguard Worker E = ToBB->pred_end(); PI != E; ++PI) {
463*9880d681SAndroid Build Coastguard Worker if (*PI == FromBB)
464*9880d681SAndroid Build Coastguard Worker continue;
465*9880d681SAndroid Build Coastguard Worker if (!DT->dominates(ToBB, *PI))
466*9880d681SAndroid Build Coastguard Worker return false;
467*9880d681SAndroid Build Coastguard Worker }
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker ToSplit.insert(std::make_pair(FromBB, ToBB));
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard Worker return true;
473*9880d681SAndroid Build Coastguard Worker }
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard Worker /// collectDebgValues - Scan instructions following MI and collect any
476*9880d681SAndroid Build Coastguard Worker /// matching DBG_VALUEs.
collectDebugValues(MachineInstr & MI,SmallVectorImpl<MachineInstr * > & DbgValues)477*9880d681SAndroid Build Coastguard Worker static void collectDebugValues(MachineInstr &MI,
478*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &DbgValues) {
479*9880d681SAndroid Build Coastguard Worker DbgValues.clear();
480*9880d681SAndroid Build Coastguard Worker if (!MI.getOperand(0).isReg())
481*9880d681SAndroid Build Coastguard Worker return;
482*9880d681SAndroid Build Coastguard Worker
483*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator DI = MI; ++DI;
484*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator DE = MI.getParent()->end();
485*9880d681SAndroid Build Coastguard Worker DI != DE; ++DI) {
486*9880d681SAndroid Build Coastguard Worker if (!DI->isDebugValue())
487*9880d681SAndroid Build Coastguard Worker return;
488*9880d681SAndroid Build Coastguard Worker if (DI->getOperand(0).isReg() &&
489*9880d681SAndroid Build Coastguard Worker DI->getOperand(0).getReg() == MI.getOperand(0).getReg())
490*9880d681SAndroid Build Coastguard Worker DbgValues.push_back(&*DI);
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker }
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Worker /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
isProfitableToSinkTo(unsigned Reg,MachineInstr & MI,MachineBasicBlock * MBB,MachineBasicBlock * SuccToSinkTo,AllSuccsCache & AllSuccessors)495*9880d681SAndroid Build Coastguard Worker bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr &MI,
496*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB,
497*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccToSinkTo,
498*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors) {
499*9880d681SAndroid Build Coastguard Worker assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker if (MBB == SuccToSinkTo)
502*9880d681SAndroid Build Coastguard Worker return false;
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker // It is profitable if SuccToSinkTo does not post dominate current block.
505*9880d681SAndroid Build Coastguard Worker if (!PDT->dominates(SuccToSinkTo, MBB))
506*9880d681SAndroid Build Coastguard Worker return true;
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker // It is profitable to sink an instruction from a deeper loop to a shallower
509*9880d681SAndroid Build Coastguard Worker // loop, even if the latter post-dominates the former (PR21115).
510*9880d681SAndroid Build Coastguard Worker if (LI->getLoopDepth(MBB) > LI->getLoopDepth(SuccToSinkTo))
511*9880d681SAndroid Build Coastguard Worker return true;
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard Worker // Check if only use in post dominated block is PHI instruction.
514*9880d681SAndroid Build Coastguard Worker bool NonPHIUse = false;
515*9880d681SAndroid Build Coastguard Worker for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
516*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *UseBlock = UseInst.getParent();
517*9880d681SAndroid Build Coastguard Worker if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
518*9880d681SAndroid Build Coastguard Worker NonPHIUse = true;
519*9880d681SAndroid Build Coastguard Worker }
520*9880d681SAndroid Build Coastguard Worker if (!NonPHIUse)
521*9880d681SAndroid Build Coastguard Worker return true;
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker // If SuccToSinkTo post dominates then also it may be profitable if MI
524*9880d681SAndroid Build Coastguard Worker // can further profitably sinked into another block in next round.
525*9880d681SAndroid Build Coastguard Worker bool BreakPHIEdge = false;
526*9880d681SAndroid Build Coastguard Worker // FIXME - If finding successor is compile time expensive then cache results.
527*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB2 =
528*9880d681SAndroid Build Coastguard Worker FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
529*9880d681SAndroid Build Coastguard Worker return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker // If SuccToSinkTo is final destination and it is a post dominator of current
532*9880d681SAndroid Build Coastguard Worker // block then it is not profitable to sink MI into SuccToSinkTo block.
533*9880d681SAndroid Build Coastguard Worker return false;
534*9880d681SAndroid Build Coastguard Worker }
535*9880d681SAndroid Build Coastguard Worker
536*9880d681SAndroid Build Coastguard Worker /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
537*9880d681SAndroid Build Coastguard Worker /// computing it if it was not already cached.
538*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock *, 4> &
GetAllSortedSuccessors(MachineInstr & MI,MachineBasicBlock * MBB,AllSuccsCache & AllSuccessors) const539*9880d681SAndroid Build Coastguard Worker MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
540*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors) const {
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker // Do we have the sorted successors in cache ?
543*9880d681SAndroid Build Coastguard Worker auto Succs = AllSuccessors.find(MBB);
544*9880d681SAndroid Build Coastguard Worker if (Succs != AllSuccessors.end())
545*9880d681SAndroid Build Coastguard Worker return Succs->second;
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->succ_begin(),
548*9880d681SAndroid Build Coastguard Worker MBB->succ_end());
549*9880d681SAndroid Build Coastguard Worker
550*9880d681SAndroid Build Coastguard Worker // Handle cases where sinking can happen but where the sink point isn't a
551*9880d681SAndroid Build Coastguard Worker // successor. For example:
552*9880d681SAndroid Build Coastguard Worker //
553*9880d681SAndroid Build Coastguard Worker // x = computation
554*9880d681SAndroid Build Coastguard Worker // if () {} else {}
555*9880d681SAndroid Build Coastguard Worker // use x
556*9880d681SAndroid Build Coastguard Worker //
557*9880d681SAndroid Build Coastguard Worker const std::vector<MachineDomTreeNode *> &Children =
558*9880d681SAndroid Build Coastguard Worker DT->getNode(MBB)->getChildren();
559*9880d681SAndroid Build Coastguard Worker for (const auto &DTChild : Children)
560*9880d681SAndroid Build Coastguard Worker // DomTree children of MBB that have MBB as immediate dominator are added.
561*9880d681SAndroid Build Coastguard Worker if (DTChild->getIDom()->getBlock() == MI.getParent() &&
562*9880d681SAndroid Build Coastguard Worker // Skip MBBs already added to the AllSuccs vector above.
563*9880d681SAndroid Build Coastguard Worker !MBB->isSuccessor(DTChild->getBlock()))
564*9880d681SAndroid Build Coastguard Worker AllSuccs.push_back(DTChild->getBlock());
565*9880d681SAndroid Build Coastguard Worker
566*9880d681SAndroid Build Coastguard Worker // Sort Successors according to their loop depth or block frequency info.
567*9880d681SAndroid Build Coastguard Worker std::stable_sort(
568*9880d681SAndroid Build Coastguard Worker AllSuccs.begin(), AllSuccs.end(),
569*9880d681SAndroid Build Coastguard Worker [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
570*9880d681SAndroid Build Coastguard Worker uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
571*9880d681SAndroid Build Coastguard Worker uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
572*9880d681SAndroid Build Coastguard Worker bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
573*9880d681SAndroid Build Coastguard Worker return HasBlockFreq ? LHSFreq < RHSFreq
574*9880d681SAndroid Build Coastguard Worker : LI->getLoopDepth(L) < LI->getLoopDepth(R);
575*9880d681SAndroid Build Coastguard Worker });
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard Worker auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker return it.first->second;
580*9880d681SAndroid Build Coastguard Worker }
581*9880d681SAndroid Build Coastguard Worker
582*9880d681SAndroid Build Coastguard Worker /// FindSuccToSinkTo - Find a successor to sink this instruction to.
583*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *
FindSuccToSinkTo(MachineInstr & MI,MachineBasicBlock * MBB,bool & BreakPHIEdge,AllSuccsCache & AllSuccessors)584*9880d681SAndroid Build Coastguard Worker MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
585*9880d681SAndroid Build Coastguard Worker bool &BreakPHIEdge,
586*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors) {
587*9880d681SAndroid Build Coastguard Worker assert (MBB && "Invalid MachineBasicBlock!");
588*9880d681SAndroid Build Coastguard Worker
589*9880d681SAndroid Build Coastguard Worker // Loop over all the operands of the specified instruction. If there is
590*9880d681SAndroid Build Coastguard Worker // anything we can't handle, bail out.
591*9880d681SAndroid Build Coastguard Worker
592*9880d681SAndroid Build Coastguard Worker // SuccToSinkTo - This is the successor to sink this instruction to, once we
593*9880d681SAndroid Build Coastguard Worker // decide.
594*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccToSinkTo = nullptr;
595*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
596*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
597*9880d681SAndroid Build Coastguard Worker if (!MO.isReg()) continue; // Ignore non-register operands.
598*9880d681SAndroid Build Coastguard Worker
599*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
600*9880d681SAndroid Build Coastguard Worker if (Reg == 0) continue;
601*9880d681SAndroid Build Coastguard Worker
602*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
603*9880d681SAndroid Build Coastguard Worker if (MO.isUse()) {
604*9880d681SAndroid Build Coastguard Worker // If the physreg has no defs anywhere, it's just an ambient register
605*9880d681SAndroid Build Coastguard Worker // and we can freely move its uses. Alternatively, if it's allocatable,
606*9880d681SAndroid Build Coastguard Worker // it could get allocated to something with a def during allocation.
607*9880d681SAndroid Build Coastguard Worker if (!MRI->isConstantPhysReg(Reg, *MBB->getParent()))
608*9880d681SAndroid Build Coastguard Worker return nullptr;
609*9880d681SAndroid Build Coastguard Worker } else if (!MO.isDead()) {
610*9880d681SAndroid Build Coastguard Worker // A def that isn't dead. We can't move it.
611*9880d681SAndroid Build Coastguard Worker return nullptr;
612*9880d681SAndroid Build Coastguard Worker }
613*9880d681SAndroid Build Coastguard Worker } else {
614*9880d681SAndroid Build Coastguard Worker // Virtual register uses are always safe to sink.
615*9880d681SAndroid Build Coastguard Worker if (MO.isUse()) continue;
616*9880d681SAndroid Build Coastguard Worker
617*9880d681SAndroid Build Coastguard Worker // If it's not safe to move defs of the register class, then abort.
618*9880d681SAndroid Build Coastguard Worker if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
619*9880d681SAndroid Build Coastguard Worker return nullptr;
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard Worker // Virtual register defs can only be sunk if all their uses are in blocks
622*9880d681SAndroid Build Coastguard Worker // dominated by one of the successors.
623*9880d681SAndroid Build Coastguard Worker if (SuccToSinkTo) {
624*9880d681SAndroid Build Coastguard Worker // If a previous operand picked a block to sink to, then this operand
625*9880d681SAndroid Build Coastguard Worker // must be sinkable to the same block.
626*9880d681SAndroid Build Coastguard Worker bool LocalUse = false;
627*9880d681SAndroid Build Coastguard Worker if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
628*9880d681SAndroid Build Coastguard Worker BreakPHIEdge, LocalUse))
629*9880d681SAndroid Build Coastguard Worker return nullptr;
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker continue;
632*9880d681SAndroid Build Coastguard Worker }
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker // Otherwise, we should look at all the successors and decide which one
635*9880d681SAndroid Build Coastguard Worker // we should sink to. If we have reliable block frequency information
636*9880d681SAndroid Build Coastguard Worker // (frequency != 0) available, give successors with smaller frequencies
637*9880d681SAndroid Build Coastguard Worker // higher priority, otherwise prioritize smaller loop depths.
638*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *SuccBlock :
639*9880d681SAndroid Build Coastguard Worker GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
640*9880d681SAndroid Build Coastguard Worker bool LocalUse = false;
641*9880d681SAndroid Build Coastguard Worker if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
642*9880d681SAndroid Build Coastguard Worker BreakPHIEdge, LocalUse)) {
643*9880d681SAndroid Build Coastguard Worker SuccToSinkTo = SuccBlock;
644*9880d681SAndroid Build Coastguard Worker break;
645*9880d681SAndroid Build Coastguard Worker }
646*9880d681SAndroid Build Coastguard Worker if (LocalUse)
647*9880d681SAndroid Build Coastguard Worker // Def is used locally, it's never safe to move this def.
648*9880d681SAndroid Build Coastguard Worker return nullptr;
649*9880d681SAndroid Build Coastguard Worker }
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard Worker // If we couldn't find a block to sink to, ignore this instruction.
652*9880d681SAndroid Build Coastguard Worker if (!SuccToSinkTo)
653*9880d681SAndroid Build Coastguard Worker return nullptr;
654*9880d681SAndroid Build Coastguard Worker if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
655*9880d681SAndroid Build Coastguard Worker return nullptr;
656*9880d681SAndroid Build Coastguard Worker }
657*9880d681SAndroid Build Coastguard Worker }
658*9880d681SAndroid Build Coastguard Worker
659*9880d681SAndroid Build Coastguard Worker // It is not possible to sink an instruction into its own block. This can
660*9880d681SAndroid Build Coastguard Worker // happen with loops.
661*9880d681SAndroid Build Coastguard Worker if (MBB == SuccToSinkTo)
662*9880d681SAndroid Build Coastguard Worker return nullptr;
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker // It's not safe to sink instructions to EH landing pad. Control flow into
665*9880d681SAndroid Build Coastguard Worker // landing pad is implicitly defined.
666*9880d681SAndroid Build Coastguard Worker if (SuccToSinkTo && SuccToSinkTo->isEHPad())
667*9880d681SAndroid Build Coastguard Worker return nullptr;
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker return SuccToSinkTo;
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker
672*9880d681SAndroid Build Coastguard Worker /// \brief Return true if MI is likely to be usable as a memory operation by the
673*9880d681SAndroid Build Coastguard Worker /// implicit null check optimization.
674*9880d681SAndroid Build Coastguard Worker ///
675*9880d681SAndroid Build Coastguard Worker /// This is a "best effort" heuristic, and should not be relied upon for
676*9880d681SAndroid Build Coastguard Worker /// correctness. This returning true does not guarantee that the implicit null
677*9880d681SAndroid Build Coastguard Worker /// check optimization is legal over MI, and this returning false does not
678*9880d681SAndroid Build Coastguard Worker /// guarantee MI cannot possibly be used to do a null check.
SinkingPreventsImplicitNullCheck(MachineInstr & MI,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)679*9880d681SAndroid Build Coastguard Worker static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
680*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII,
681*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) {
682*9880d681SAndroid Build Coastguard Worker typedef TargetInstrInfo::MachineBranchPredicate MachineBranchPredicate;
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker auto *MBB = MI.getParent();
685*9880d681SAndroid Build Coastguard Worker if (MBB->pred_size() != 1)
686*9880d681SAndroid Build Coastguard Worker return false;
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker auto *PredMBB = *MBB->pred_begin();
689*9880d681SAndroid Build Coastguard Worker auto *PredBB = PredMBB->getBasicBlock();
690*9880d681SAndroid Build Coastguard Worker
691*9880d681SAndroid Build Coastguard Worker // Frontends that don't use implicit null checks have no reason to emit
692*9880d681SAndroid Build Coastguard Worker // branches with make.implicit metadata, and this function should always
693*9880d681SAndroid Build Coastguard Worker // return false for them.
694*9880d681SAndroid Build Coastguard Worker if (!PredBB ||
695*9880d681SAndroid Build Coastguard Worker !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
696*9880d681SAndroid Build Coastguard Worker return false;
697*9880d681SAndroid Build Coastguard Worker
698*9880d681SAndroid Build Coastguard Worker unsigned BaseReg;
699*9880d681SAndroid Build Coastguard Worker int64_t Offset;
700*9880d681SAndroid Build Coastguard Worker if (!TII->getMemOpBaseRegImmOfs(MI, BaseReg, Offset, TRI))
701*9880d681SAndroid Build Coastguard Worker return false;
702*9880d681SAndroid Build Coastguard Worker
703*9880d681SAndroid Build Coastguard Worker if (!(MI.mayLoad() && !MI.isPredicable()))
704*9880d681SAndroid Build Coastguard Worker return false;
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker MachineBranchPredicate MBP;
707*9880d681SAndroid Build Coastguard Worker if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
708*9880d681SAndroid Build Coastguard Worker return false;
709*9880d681SAndroid Build Coastguard Worker
710*9880d681SAndroid Build Coastguard Worker return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
711*9880d681SAndroid Build Coastguard Worker (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
712*9880d681SAndroid Build Coastguard Worker MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
713*9880d681SAndroid Build Coastguard Worker MBP.LHS.getReg() == BaseReg;
714*9880d681SAndroid Build Coastguard Worker }
715*9880d681SAndroid Build Coastguard Worker
716*9880d681SAndroid Build Coastguard Worker /// SinkInstruction - Determine whether it is safe to sink the specified machine
717*9880d681SAndroid Build Coastguard Worker /// instruction out of its current block into a successor.
SinkInstruction(MachineInstr & MI,bool & SawStore,AllSuccsCache & AllSuccessors)718*9880d681SAndroid Build Coastguard Worker bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
719*9880d681SAndroid Build Coastguard Worker AllSuccsCache &AllSuccessors) {
720*9880d681SAndroid Build Coastguard Worker // Don't sink instructions that the target prefers not to sink.
721*9880d681SAndroid Build Coastguard Worker if (!TII->shouldSink(MI))
722*9880d681SAndroid Build Coastguard Worker return false;
723*9880d681SAndroid Build Coastguard Worker
724*9880d681SAndroid Build Coastguard Worker // Check if it's safe to move the instruction.
725*9880d681SAndroid Build Coastguard Worker if (!MI.isSafeToMove(AA, SawStore))
726*9880d681SAndroid Build Coastguard Worker return false;
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker // Convergent operations may not be made control-dependent on additional
729*9880d681SAndroid Build Coastguard Worker // values.
730*9880d681SAndroid Build Coastguard Worker if (MI.isConvergent())
731*9880d681SAndroid Build Coastguard Worker return false;
732*9880d681SAndroid Build Coastguard Worker
733*9880d681SAndroid Build Coastguard Worker // Don't break implicit null checks. This is a performance heuristic, and not
734*9880d681SAndroid Build Coastguard Worker // required for correctness.
735*9880d681SAndroid Build Coastguard Worker if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
736*9880d681SAndroid Build Coastguard Worker return false;
737*9880d681SAndroid Build Coastguard Worker
738*9880d681SAndroid Build Coastguard Worker // FIXME: This should include support for sinking instructions within the
739*9880d681SAndroid Build Coastguard Worker // block they are currently in to shorten the live ranges. We often get
740*9880d681SAndroid Build Coastguard Worker // instructions sunk into the top of a large block, but it would be better to
741*9880d681SAndroid Build Coastguard Worker // also sink them down before their first use in the block. This xform has to
742*9880d681SAndroid Build Coastguard Worker // be careful not to *increase* register pressure though, e.g. sinking
743*9880d681SAndroid Build Coastguard Worker // "x = y + z" down if it kills y and z would increase the live ranges of y
744*9880d681SAndroid Build Coastguard Worker // and z and only shrink the live range of x.
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker bool BreakPHIEdge = false;
747*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *ParentBlock = MI.getParent();
748*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccToSinkTo =
749*9880d681SAndroid Build Coastguard Worker FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
750*9880d681SAndroid Build Coastguard Worker
751*9880d681SAndroid Build Coastguard Worker // If there are no outputs, it must have side-effects.
752*9880d681SAndroid Build Coastguard Worker if (!SuccToSinkTo)
753*9880d681SAndroid Build Coastguard Worker return false;
754*9880d681SAndroid Build Coastguard Worker
755*9880d681SAndroid Build Coastguard Worker
756*9880d681SAndroid Build Coastguard Worker // If the instruction to move defines a dead physical register which is live
757*9880d681SAndroid Build Coastguard Worker // when leaving the basic block, don't move it because it could turn into a
758*9880d681SAndroid Build Coastguard Worker // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
759*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
760*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(I);
761*9880d681SAndroid Build Coastguard Worker if (!MO.isReg()) continue;
762*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
763*9880d681SAndroid Build Coastguard Worker if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
764*9880d681SAndroid Build Coastguard Worker if (SuccToSinkTo->isLiveIn(Reg))
765*9880d681SAndroid Build Coastguard Worker return false;
766*9880d681SAndroid Build Coastguard Worker }
767*9880d681SAndroid Build Coastguard Worker
768*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
769*9880d681SAndroid Build Coastguard Worker
770*9880d681SAndroid Build Coastguard Worker // If the block has multiple predecessors, this is a critical edge.
771*9880d681SAndroid Build Coastguard Worker // Decide if we can sink along it or need to break the edge.
772*9880d681SAndroid Build Coastguard Worker if (SuccToSinkTo->pred_size() > 1) {
773*9880d681SAndroid Build Coastguard Worker // We cannot sink a load across a critical edge - there may be stores in
774*9880d681SAndroid Build Coastguard Worker // other code paths.
775*9880d681SAndroid Build Coastguard Worker bool TryBreak = false;
776*9880d681SAndroid Build Coastguard Worker bool store = true;
777*9880d681SAndroid Build Coastguard Worker if (!MI.isSafeToMove(AA, store)) {
778*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
779*9880d681SAndroid Build Coastguard Worker TryBreak = true;
780*9880d681SAndroid Build Coastguard Worker }
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard Worker // We don't want to sink across a critical edge if we don't dominate the
783*9880d681SAndroid Build Coastguard Worker // successor. We could be introducing calculations to new code paths.
784*9880d681SAndroid Build Coastguard Worker if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
785*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
786*9880d681SAndroid Build Coastguard Worker TryBreak = true;
787*9880d681SAndroid Build Coastguard Worker }
788*9880d681SAndroid Build Coastguard Worker
789*9880d681SAndroid Build Coastguard Worker // Don't sink instructions into a loop.
790*9880d681SAndroid Build Coastguard Worker if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
791*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** NOTE: Loop header found\n");
792*9880d681SAndroid Build Coastguard Worker TryBreak = true;
793*9880d681SAndroid Build Coastguard Worker }
794*9880d681SAndroid Build Coastguard Worker
795*9880d681SAndroid Build Coastguard Worker // Otherwise we are OK with sinking along a critical edge.
796*9880d681SAndroid Build Coastguard Worker if (!TryBreak)
797*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Sinking along critical edge.\n");
798*9880d681SAndroid Build Coastguard Worker else {
799*9880d681SAndroid Build Coastguard Worker // Mark this edge as to be split.
800*9880d681SAndroid Build Coastguard Worker // If the edge can actually be split, the next iteration of the main loop
801*9880d681SAndroid Build Coastguard Worker // will sink MI in the newly created block.
802*9880d681SAndroid Build Coastguard Worker bool Status =
803*9880d681SAndroid Build Coastguard Worker PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
804*9880d681SAndroid Build Coastguard Worker if (!Status)
805*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
806*9880d681SAndroid Build Coastguard Worker "break critical edge\n");
807*9880d681SAndroid Build Coastguard Worker // The instruction will not be sunk this time.
808*9880d681SAndroid Build Coastguard Worker return false;
809*9880d681SAndroid Build Coastguard Worker }
810*9880d681SAndroid Build Coastguard Worker }
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard Worker if (BreakPHIEdge) {
813*9880d681SAndroid Build Coastguard Worker // BreakPHIEdge is true if all the uses are in the successor MBB being
814*9880d681SAndroid Build Coastguard Worker // sunken into and they are all PHI nodes. In this case, machine-sink must
815*9880d681SAndroid Build Coastguard Worker // break the critical edge first.
816*9880d681SAndroid Build Coastguard Worker bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
817*9880d681SAndroid Build Coastguard Worker SuccToSinkTo, BreakPHIEdge);
818*9880d681SAndroid Build Coastguard Worker if (!Status)
819*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
820*9880d681SAndroid Build Coastguard Worker "break critical edge\n");
821*9880d681SAndroid Build Coastguard Worker // The instruction will not be sunk this time.
822*9880d681SAndroid Build Coastguard Worker return false;
823*9880d681SAndroid Build Coastguard Worker }
824*9880d681SAndroid Build Coastguard Worker
825*9880d681SAndroid Build Coastguard Worker // Determine where to insert into. Skip phi nodes.
826*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
827*9880d681SAndroid Build Coastguard Worker while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
828*9880d681SAndroid Build Coastguard Worker ++InsertPos;
829*9880d681SAndroid Build Coastguard Worker
830*9880d681SAndroid Build Coastguard Worker // collect matching debug values.
831*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr *, 2> DbgValuesToSink;
832*9880d681SAndroid Build Coastguard Worker collectDebugValues(MI, DbgValuesToSink);
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker // Move the instruction.
835*9880d681SAndroid Build Coastguard Worker SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
836*9880d681SAndroid Build Coastguard Worker ++MachineBasicBlock::iterator(MI));
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard Worker // Move debug values.
839*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
840*9880d681SAndroid Build Coastguard Worker DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
841*9880d681SAndroid Build Coastguard Worker MachineInstr *DbgMI = *DBI;
842*9880d681SAndroid Build Coastguard Worker SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI,
843*9880d681SAndroid Build Coastguard Worker ++MachineBasicBlock::iterator(DbgMI));
844*9880d681SAndroid Build Coastguard Worker }
845*9880d681SAndroid Build Coastguard Worker
846*9880d681SAndroid Build Coastguard Worker // Conservatively, clear any kill flags, since it's possible that they are no
847*9880d681SAndroid Build Coastguard Worker // longer correct.
848*9880d681SAndroid Build Coastguard Worker // Note that we have to clear the kill flags for any register this instruction
849*9880d681SAndroid Build Coastguard Worker // uses as we may sink over another instruction which currently kills the
850*9880d681SAndroid Build Coastguard Worker // used registers.
851*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MI.operands()) {
852*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse())
853*9880d681SAndroid Build Coastguard Worker RegsToClearKillFlags.set(MO.getReg()); // Remember to clear kill flags.
854*9880d681SAndroid Build Coastguard Worker }
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard Worker return true;
857*9880d681SAndroid Build Coastguard Worker }
858