1*9880d681SAndroid Build Coastguard Worker //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
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 performs loop invariant code motion on machine instructions. We
11*9880d681SAndroid Build Coastguard Worker // attempt to remove as much code from the body of a loop as possible.
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 the LLVM-IR-level LICM pass. It is only designed to hoist 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/DenseMap.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/PseudoSourceValue.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetSchedule.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/TargetLowering.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
39*9880d681SAndroid Build Coastguard Worker using namespace llvm;
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "machine-licm"
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
44*9880d681SAndroid Build Coastguard Worker AvoidSpeculation("avoid-speculation",
45*9880d681SAndroid Build Coastguard Worker cl::desc("MachineLICM should avoid speculation"),
46*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
49*9880d681SAndroid Build Coastguard Worker HoistCheapInsts("hoist-cheap-insts",
50*9880d681SAndroid Build Coastguard Worker cl::desc("MachineLICM should hoist even cheap instructions"),
51*9880d681SAndroid Build Coastguard Worker cl::init(false), cl::Hidden);
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
54*9880d681SAndroid Build Coastguard Worker SinkInstsToAvoidSpills("sink-insts-to-avoid-spills",
55*9880d681SAndroid Build Coastguard Worker cl::desc("MachineLICM should sink instructions into "
56*9880d681SAndroid Build Coastguard Worker "loops to avoid register spills"),
57*9880d681SAndroid Build Coastguard Worker cl::init(false), cl::Hidden);
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker STATISTIC(NumHoisted,
60*9880d681SAndroid Build Coastguard Worker "Number of machine instructions hoisted out of loops");
61*9880d681SAndroid Build Coastguard Worker STATISTIC(NumLowRP,
62*9880d681SAndroid Build Coastguard Worker "Number of instructions hoisted in low reg pressure situation");
63*9880d681SAndroid Build Coastguard Worker STATISTIC(NumHighLatency,
64*9880d681SAndroid Build Coastguard Worker "Number of high latency instructions hoisted");
65*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCSEed,
66*9880d681SAndroid Build Coastguard Worker "Number of hoisted machine instructions CSEed");
67*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPostRAHoisted,
68*9880d681SAndroid Build Coastguard Worker "Number of machine instructions hoisted out of loops post regalloc");
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker namespace {
71*9880d681SAndroid Build Coastguard Worker class MachineLICM : public MachineFunctionPass {
72*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
73*9880d681SAndroid Build Coastguard Worker const TargetLoweringBase *TLI;
74*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
75*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI;
76*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI;
77*9880d681SAndroid Build Coastguard Worker TargetSchedModel SchedModel;
78*9880d681SAndroid Build Coastguard Worker bool PreRegAlloc;
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker // Various analyses that we use...
81*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA; // Alias analysis info.
82*9880d681SAndroid Build Coastguard Worker MachineLoopInfo *MLI; // Current MachineLoopInfo
83*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DT; // Machine dominator tree for the cur loop
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker // State that is updated as we process loops
86*9880d681SAndroid Build Coastguard Worker bool Changed; // True if a loop is changed.
87*9880d681SAndroid Build Coastguard Worker bool FirstInLoop; // True if it's the first LICM in the loop.
88*9880d681SAndroid Build Coastguard Worker MachineLoop *CurLoop; // The current loop we are working on.
89*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker // Exit blocks for CurLoop.
92*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock*, 8> ExitBlocks;
93*9880d681SAndroid Build Coastguard Worker
isExitBlock(const MachineBasicBlock * MBB) const94*9880d681SAndroid Build Coastguard Worker bool isExitBlock(const MachineBasicBlock *MBB) const {
95*9880d681SAndroid Build Coastguard Worker return std::find(ExitBlocks.begin(), ExitBlocks.end(), MBB) !=
96*9880d681SAndroid Build Coastguard Worker ExitBlocks.end();
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard Worker // Track 'estimated' register pressure.
100*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned, 32> RegSeen;
101*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> RegPressure;
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker // Register pressure "limit" per register pressure set. If the pressure
104*9880d681SAndroid Build Coastguard Worker // is higher than the limit, then it's considered high.
105*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> RegLimit;
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard Worker // Register pressure on path leading from loop preheader to current BB.
108*9880d681SAndroid Build Coastguard Worker SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker // For each opcode, keep a list of potential CSE instructions.
111*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker enum {
114*9880d681SAndroid Build Coastguard Worker SpeculateFalse = 0,
115*9880d681SAndroid Build Coastguard Worker SpeculateTrue = 1,
116*9880d681SAndroid Build Coastguard Worker SpeculateUnknown = 2
117*9880d681SAndroid Build Coastguard Worker };
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker // If a MBB does not dominate loop exiting blocks then it may not safe
120*9880d681SAndroid Build Coastguard Worker // to hoist loads from this block.
121*9880d681SAndroid Build Coastguard Worker // Tri-state: 0 - false, 1 - true, 2 - unknown
122*9880d681SAndroid Build Coastguard Worker unsigned SpeculationState;
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker public:
125*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
MachineLICM()126*9880d681SAndroid Build Coastguard Worker MachineLICM() :
127*9880d681SAndroid Build Coastguard Worker MachineFunctionPass(ID), PreRegAlloc(true) {
128*9880d681SAndroid Build Coastguard Worker initializeMachineLICMPass(*PassRegistry::getPassRegistry());
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
MachineLICM(bool PreRA)131*9880d681SAndroid Build Coastguard Worker explicit MachineLICM(bool PreRA) :
132*9880d681SAndroid Build Coastguard Worker MachineFunctionPass(ID), PreRegAlloc(PreRA) {
133*9880d681SAndroid Build Coastguard Worker initializeMachineLICMPass(*PassRegistry::getPassRegistry());
134*9880d681SAndroid Build Coastguard Worker }
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
137*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const138*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
139*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineLoopInfo>();
140*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
141*9880d681SAndroid Build Coastguard Worker AU.addRequired<AAResultsWrapperPass>();
142*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineLoopInfo>();
143*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
144*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
145*9880d681SAndroid Build Coastguard Worker }
146*9880d681SAndroid Build Coastguard Worker
releaseMemory()147*9880d681SAndroid Build Coastguard Worker void releaseMemory() override {
148*9880d681SAndroid Build Coastguard Worker RegSeen.clear();
149*9880d681SAndroid Build Coastguard Worker RegPressure.clear();
150*9880d681SAndroid Build Coastguard Worker RegLimit.clear();
151*9880d681SAndroid Build Coastguard Worker BackTrace.clear();
152*9880d681SAndroid Build Coastguard Worker CSEMap.clear();
153*9880d681SAndroid Build Coastguard Worker }
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker private:
156*9880d681SAndroid Build Coastguard Worker /// Keep track of information about hoisting candidates.
157*9880d681SAndroid Build Coastguard Worker struct CandidateInfo {
158*9880d681SAndroid Build Coastguard Worker MachineInstr *MI;
159*9880d681SAndroid Build Coastguard Worker unsigned Def;
160*9880d681SAndroid Build Coastguard Worker int FI;
CandidateInfo__anone7e161460111::MachineLICM::CandidateInfo161*9880d681SAndroid Build Coastguard Worker CandidateInfo(MachineInstr *mi, unsigned def, int fi)
162*9880d681SAndroid Build Coastguard Worker : MI(mi), Def(def), FI(fi) {}
163*9880d681SAndroid Build Coastguard Worker };
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker void HoistRegionPostRA();
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker void HoistPostRA(MachineInstr *MI, unsigned Def);
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
170*9880d681SAndroid Build Coastguard Worker BitVector &PhysRegClobbers, SmallSet<int, 32> &StoredFIs,
171*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<CandidateInfo> &Candidates);
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker void AddToLiveIns(unsigned Reg);
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker bool IsLICMCandidate(MachineInstr &I);
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker bool IsLoopInvariantInst(MachineInstr &I);
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker bool HasLoopPHIUse(const MachineInstr *MI) const;
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
182*9880d681SAndroid Build Coastguard Worker unsigned Reg) const;
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker bool IsCheapInstruction(MachineInstr &MI) const;
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
187*9880d681SAndroid Build Coastguard Worker bool Cheap);
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker void UpdateBackTraceRegPressure(const MachineInstr *MI);
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker bool IsProfitableToHoist(MachineInstr &MI);
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker bool IsGuaranteedToExecute(MachineBasicBlock *BB);
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker void EnterScope(MachineBasicBlock *MBB);
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker void ExitScope(MachineBasicBlock *MBB);
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker void ExitScopeIfDone(
200*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node,
201*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
202*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode);
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker void HoistRegion(MachineDomTreeNode *N, bool IsHeader);
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker void SinkIntoLoop();
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker void InitRegPressure(MachineBasicBlock *BB);
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
213*9880d681SAndroid Build Coastguard Worker bool ConsiderSeen,
214*9880d681SAndroid Build Coastguard Worker bool ConsiderUnseenAsDef);
215*9880d681SAndroid Build Coastguard Worker
216*9880d681SAndroid Build Coastguard Worker void UpdateRegPressure(const MachineInstr *MI,
217*9880d681SAndroid Build Coastguard Worker bool ConsiderUnseenAsDef = false);
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker const MachineInstr *
222*9880d681SAndroid Build Coastguard Worker LookForDuplicate(const MachineInstr *MI,
223*9880d681SAndroid Build Coastguard Worker std::vector<const MachineInstr *> &PrevMIs);
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker bool EliminateCSE(
226*9880d681SAndroid Build Coastguard Worker MachineInstr *MI,
227*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI);
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker bool MayCSE(MachineInstr *MI);
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker void InitCSEMap(MachineBasicBlock *BB);
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *getCurPreheader();
236*9880d681SAndroid Build Coastguard Worker };
237*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker char MachineLICM::ID = 0;
240*9880d681SAndroid Build Coastguard Worker char &llvm::MachineLICMID = MachineLICM::ID;
241*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm",
242*9880d681SAndroid Build Coastguard Worker "Machine Loop Invariant Code Motion", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)243*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
244*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
245*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
246*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MachineLICM, "machinelicm",
247*9880d681SAndroid Build Coastguard Worker "Machine Loop Invariant Code Motion", false, false)
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker /// Test if the given loop is the outer-most loop that has a unique predecessor.
250*9880d681SAndroid Build Coastguard Worker static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
251*9880d681SAndroid Build Coastguard Worker // Check whether this loop even has a unique predecessor.
252*9880d681SAndroid Build Coastguard Worker if (!CurLoop->getLoopPredecessor())
253*9880d681SAndroid Build Coastguard Worker return false;
254*9880d681SAndroid Build Coastguard Worker // Ok, now check to see if any of its outer loops do.
255*9880d681SAndroid Build Coastguard Worker for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
256*9880d681SAndroid Build Coastguard Worker if (L->getLoopPredecessor())
257*9880d681SAndroid Build Coastguard Worker return false;
258*9880d681SAndroid Build Coastguard Worker // None of them did, so this is the outermost with a unique predecessor.
259*9880d681SAndroid Build Coastguard Worker return true;
260*9880d681SAndroid Build Coastguard Worker }
261*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)262*9880d681SAndroid Build Coastguard Worker bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
263*9880d681SAndroid Build Coastguard Worker if (skipFunction(*MF.getFunction()))
264*9880d681SAndroid Build Coastguard Worker return false;
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker Changed = FirstInLoop = false;
267*9880d681SAndroid Build Coastguard Worker const TargetSubtargetInfo &ST = MF.getSubtarget();
268*9880d681SAndroid Build Coastguard Worker TII = ST.getInstrInfo();
269*9880d681SAndroid Build Coastguard Worker TLI = ST.getTargetLowering();
270*9880d681SAndroid Build Coastguard Worker TRI = ST.getRegisterInfo();
271*9880d681SAndroid Build Coastguard Worker MFI = MF.getFrameInfo();
272*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
273*9880d681SAndroid Build Coastguard Worker SchedModel.init(ST.getSchedModel(), &ST, TII);
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker PreRegAlloc = MRI->isSSA();
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker if (PreRegAlloc)
278*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
279*9880d681SAndroid Build Coastguard Worker else
280*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
281*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << MF.getName() << " ********\n");
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker if (PreRegAlloc) {
284*9880d681SAndroid Build Coastguard Worker // Estimate register pressure during pre-regalloc pass.
285*9880d681SAndroid Build Coastguard Worker unsigned NumRPS = TRI->getNumRegPressureSets();
286*9880d681SAndroid Build Coastguard Worker RegPressure.resize(NumRPS);
287*9880d681SAndroid Build Coastguard Worker std::fill(RegPressure.begin(), RegPressure.end(), 0);
288*9880d681SAndroid Build Coastguard Worker RegLimit.resize(NumRPS);
289*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = NumRPS; i != e; ++i)
290*9880d681SAndroid Build Coastguard Worker RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker // Get our Loop information...
294*9880d681SAndroid Build Coastguard Worker MLI = &getAnalysis<MachineLoopInfo>();
295*9880d681SAndroid Build Coastguard Worker DT = &getAnalysis<MachineDominatorTree>();
296*9880d681SAndroid Build Coastguard Worker AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard Worker SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
299*9880d681SAndroid Build Coastguard Worker while (!Worklist.empty()) {
300*9880d681SAndroid Build Coastguard Worker CurLoop = Worklist.pop_back_val();
301*9880d681SAndroid Build Coastguard Worker CurPreheader = nullptr;
302*9880d681SAndroid Build Coastguard Worker ExitBlocks.clear();
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker // If this is done before regalloc, only visit outer-most preheader-sporting
305*9880d681SAndroid Build Coastguard Worker // loops.
306*9880d681SAndroid Build Coastguard Worker if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
307*9880d681SAndroid Build Coastguard Worker Worklist.append(CurLoop->begin(), CurLoop->end());
308*9880d681SAndroid Build Coastguard Worker continue;
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker CurLoop->getExitBlocks(ExitBlocks);
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker if (!PreRegAlloc)
314*9880d681SAndroid Build Coastguard Worker HoistRegionPostRA();
315*9880d681SAndroid Build Coastguard Worker else {
316*9880d681SAndroid Build Coastguard Worker // CSEMap is initialized for loop header when the first instruction is
317*9880d681SAndroid Build Coastguard Worker // being hoisted.
318*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
319*9880d681SAndroid Build Coastguard Worker FirstInLoop = true;
320*9880d681SAndroid Build Coastguard Worker HoistOutOfLoop(N);
321*9880d681SAndroid Build Coastguard Worker CSEMap.clear();
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker if (SinkInstsToAvoidSpills)
324*9880d681SAndroid Build Coastguard Worker SinkIntoLoop();
325*9880d681SAndroid Build Coastguard Worker }
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard Worker return Changed;
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker /// Return true if instruction stores to the specified frame.
InstructionStoresToFI(const MachineInstr * MI,int FI)332*9880d681SAndroid Build Coastguard Worker static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
333*9880d681SAndroid Build Coastguard Worker // If we lost memory operands, conservatively assume that the instruction
334*9880d681SAndroid Build Coastguard Worker // writes to all slots.
335*9880d681SAndroid Build Coastguard Worker if (MI->memoperands_empty())
336*9880d681SAndroid Build Coastguard Worker return true;
337*9880d681SAndroid Build Coastguard Worker for (const MachineMemOperand *MemOp : MI->memoperands()) {
338*9880d681SAndroid Build Coastguard Worker if (!MemOp->isStore() || !MemOp->getPseudoValue())
339*9880d681SAndroid Build Coastguard Worker continue;
340*9880d681SAndroid Build Coastguard Worker if (const FixedStackPseudoSourceValue *Value =
341*9880d681SAndroid Build Coastguard Worker dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
342*9880d681SAndroid Build Coastguard Worker if (Value->getFrameIndex() == FI)
343*9880d681SAndroid Build Coastguard Worker return true;
344*9880d681SAndroid Build Coastguard Worker }
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker return false;
347*9880d681SAndroid Build Coastguard Worker }
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker /// Examine the instruction for potentai LICM candidate. Also
350*9880d681SAndroid Build Coastguard Worker /// gather register def and frame object update information.
ProcessMI(MachineInstr * MI,BitVector & PhysRegDefs,BitVector & PhysRegClobbers,SmallSet<int,32> & StoredFIs,SmallVectorImpl<CandidateInfo> & Candidates)351*9880d681SAndroid Build Coastguard Worker void MachineLICM::ProcessMI(MachineInstr *MI,
352*9880d681SAndroid Build Coastguard Worker BitVector &PhysRegDefs,
353*9880d681SAndroid Build Coastguard Worker BitVector &PhysRegClobbers,
354*9880d681SAndroid Build Coastguard Worker SmallSet<int, 32> &StoredFIs,
355*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<CandidateInfo> &Candidates) {
356*9880d681SAndroid Build Coastguard Worker bool RuledOut = false;
357*9880d681SAndroid Build Coastguard Worker bool HasNonInvariantUse = false;
358*9880d681SAndroid Build Coastguard Worker unsigned Def = 0;
359*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
360*9880d681SAndroid Build Coastguard Worker if (MO.isFI()) {
361*9880d681SAndroid Build Coastguard Worker // Remember if the instruction stores to the frame index.
362*9880d681SAndroid Build Coastguard Worker int FI = MO.getIndex();
363*9880d681SAndroid Build Coastguard Worker if (!StoredFIs.count(FI) &&
364*9880d681SAndroid Build Coastguard Worker MFI->isSpillSlotObjectIndex(FI) &&
365*9880d681SAndroid Build Coastguard Worker InstructionStoresToFI(MI, FI))
366*9880d681SAndroid Build Coastguard Worker StoredFIs.insert(FI);
367*9880d681SAndroid Build Coastguard Worker HasNonInvariantUse = true;
368*9880d681SAndroid Build Coastguard Worker continue;
369*9880d681SAndroid Build Coastguard Worker }
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker // We can't hoist an instruction defining a physreg that is clobbered in
372*9880d681SAndroid Build Coastguard Worker // the loop.
373*9880d681SAndroid Build Coastguard Worker if (MO.isRegMask()) {
374*9880d681SAndroid Build Coastguard Worker PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
375*9880d681SAndroid Build Coastguard Worker continue;
376*9880d681SAndroid Build Coastguard Worker }
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
379*9880d681SAndroid Build Coastguard Worker continue;
380*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
381*9880d681SAndroid Build Coastguard Worker if (!Reg)
382*9880d681SAndroid Build Coastguard Worker continue;
383*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
384*9880d681SAndroid Build Coastguard Worker "Not expecting virtual register!");
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker if (!MO.isDef()) {
387*9880d681SAndroid Build Coastguard Worker if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
388*9880d681SAndroid Build Coastguard Worker // If it's using a non-loop-invariant register, then it's obviously not
389*9880d681SAndroid Build Coastguard Worker // safe to hoist.
390*9880d681SAndroid Build Coastguard Worker HasNonInvariantUse = true;
391*9880d681SAndroid Build Coastguard Worker continue;
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard Worker if (MO.isImplicit()) {
395*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
396*9880d681SAndroid Build Coastguard Worker PhysRegClobbers.set(*AI);
397*9880d681SAndroid Build Coastguard Worker if (!MO.isDead())
398*9880d681SAndroid Build Coastguard Worker // Non-dead implicit def? This cannot be hoisted.
399*9880d681SAndroid Build Coastguard Worker RuledOut = true;
400*9880d681SAndroid Build Coastguard Worker // No need to check if a dead implicit def is also defined by
401*9880d681SAndroid Build Coastguard Worker // another instruction.
402*9880d681SAndroid Build Coastguard Worker continue;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker // FIXME: For now, avoid instructions with multiple defs, unless
406*9880d681SAndroid Build Coastguard Worker // it's a dead implicit def.
407*9880d681SAndroid Build Coastguard Worker if (Def)
408*9880d681SAndroid Build Coastguard Worker RuledOut = true;
409*9880d681SAndroid Build Coastguard Worker else
410*9880d681SAndroid Build Coastguard Worker Def = Reg;
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker // If we have already seen another instruction that defines the same
413*9880d681SAndroid Build Coastguard Worker // register, then this is not safe. Two defs is indicated by setting a
414*9880d681SAndroid Build Coastguard Worker // PhysRegClobbers bit.
415*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
416*9880d681SAndroid Build Coastguard Worker if (PhysRegDefs.test(*AS))
417*9880d681SAndroid Build Coastguard Worker PhysRegClobbers.set(*AS);
418*9880d681SAndroid Build Coastguard Worker PhysRegDefs.set(*AS);
419*9880d681SAndroid Build Coastguard Worker }
420*9880d681SAndroid Build Coastguard Worker if (PhysRegClobbers.test(Reg))
421*9880d681SAndroid Build Coastguard Worker // MI defined register is seen defined by another instruction in
422*9880d681SAndroid Build Coastguard Worker // the loop, it cannot be a LICM candidate.
423*9880d681SAndroid Build Coastguard Worker RuledOut = true;
424*9880d681SAndroid Build Coastguard Worker }
425*9880d681SAndroid Build Coastguard Worker
426*9880d681SAndroid Build Coastguard Worker // Only consider reloads for now and remats which do not have register
427*9880d681SAndroid Build Coastguard Worker // operands. FIXME: Consider unfold load folding instructions.
428*9880d681SAndroid Build Coastguard Worker if (Def && !RuledOut) {
429*9880d681SAndroid Build Coastguard Worker int FI = INT_MIN;
430*9880d681SAndroid Build Coastguard Worker if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
431*9880d681SAndroid Build Coastguard Worker (TII->isLoadFromStackSlot(*MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
432*9880d681SAndroid Build Coastguard Worker Candidates.push_back(CandidateInfo(MI, Def, FI));
433*9880d681SAndroid Build Coastguard Worker }
434*9880d681SAndroid Build Coastguard Worker }
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker /// Walk the specified region of the CFG and hoist loop invariants out to the
437*9880d681SAndroid Build Coastguard Worker /// preheader.
HoistRegionPostRA()438*9880d681SAndroid Build Coastguard Worker void MachineLICM::HoistRegionPostRA() {
439*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Preheader = getCurPreheader();
440*9880d681SAndroid Build Coastguard Worker if (!Preheader)
441*9880d681SAndroid Build Coastguard Worker return;
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard Worker unsigned NumRegs = TRI->getNumRegs();
444*9880d681SAndroid Build Coastguard Worker BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
445*9880d681SAndroid Build Coastguard Worker BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
446*9880d681SAndroid Build Coastguard Worker
447*9880d681SAndroid Build Coastguard Worker SmallVector<CandidateInfo, 32> Candidates;
448*9880d681SAndroid Build Coastguard Worker SmallSet<int, 32> StoredFIs;
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker // Walk the entire region, count number of defs for each register, and
451*9880d681SAndroid Build Coastguard Worker // collect potential LICM candidates.
452*9880d681SAndroid Build Coastguard Worker const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
453*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *BB : Blocks) {
454*9880d681SAndroid Build Coastguard Worker // If the header of the loop containing this basic block is a landing pad,
455*9880d681SAndroid Build Coastguard Worker // then don't try to hoist instructions out of this loop.
456*9880d681SAndroid Build Coastguard Worker const MachineLoop *ML = MLI->getLoopFor(BB);
457*9880d681SAndroid Build Coastguard Worker if (ML && ML->getHeader()->isEHPad()) continue;
458*9880d681SAndroid Build Coastguard Worker
459*9880d681SAndroid Build Coastguard Worker // Conservatively treat live-in's as an external def.
460*9880d681SAndroid Build Coastguard Worker // FIXME: That means a reload that're reused in successor block(s) will not
461*9880d681SAndroid Build Coastguard Worker // be LICM'ed.
462*9880d681SAndroid Build Coastguard Worker for (const auto &LI : BB->liveins()) {
463*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
464*9880d681SAndroid Build Coastguard Worker PhysRegDefs.set(*AI);
465*9880d681SAndroid Build Coastguard Worker }
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker SpeculationState = SpeculateUnknown;
468*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : *BB)
469*9880d681SAndroid Build Coastguard Worker ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
470*9880d681SAndroid Build Coastguard Worker }
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard Worker // Gather the registers read / clobbered by the terminator.
473*9880d681SAndroid Build Coastguard Worker BitVector TermRegs(NumRegs);
474*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
475*9880d681SAndroid Build Coastguard Worker if (TI != Preheader->end()) {
476*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : TI->operands()) {
477*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
478*9880d681SAndroid Build Coastguard Worker continue;
479*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
480*9880d681SAndroid Build Coastguard Worker if (!Reg)
481*9880d681SAndroid Build Coastguard Worker continue;
482*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
483*9880d681SAndroid Build Coastguard Worker TermRegs.set(*AI);
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker // Now evaluate whether the potential candidates qualify.
488*9880d681SAndroid Build Coastguard Worker // 1. Check if the candidate defined register is defined by another
489*9880d681SAndroid Build Coastguard Worker // instruction in the loop.
490*9880d681SAndroid Build Coastguard Worker // 2. If the candidate is a load from stack slot (always true for now),
491*9880d681SAndroid Build Coastguard Worker // check if the slot is stored anywhere in the loop.
492*9880d681SAndroid Build Coastguard Worker // 3. Make sure candidate def should not clobber
493*9880d681SAndroid Build Coastguard Worker // registers read by the terminator. Similarly its def should not be
494*9880d681SAndroid Build Coastguard Worker // clobbered by the terminator.
495*9880d681SAndroid Build Coastguard Worker for (CandidateInfo &Candidate : Candidates) {
496*9880d681SAndroid Build Coastguard Worker if (Candidate.FI != INT_MIN &&
497*9880d681SAndroid Build Coastguard Worker StoredFIs.count(Candidate.FI))
498*9880d681SAndroid Build Coastguard Worker continue;
499*9880d681SAndroid Build Coastguard Worker
500*9880d681SAndroid Build Coastguard Worker unsigned Def = Candidate.Def;
501*9880d681SAndroid Build Coastguard Worker if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
502*9880d681SAndroid Build Coastguard Worker bool Safe = true;
503*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = Candidate.MI;
504*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
505*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isDef() || !MO.getReg())
506*9880d681SAndroid Build Coastguard Worker continue;
507*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
508*9880d681SAndroid Build Coastguard Worker if (PhysRegDefs.test(Reg) ||
509*9880d681SAndroid Build Coastguard Worker PhysRegClobbers.test(Reg)) {
510*9880d681SAndroid Build Coastguard Worker // If it's using a non-loop-invariant register, then it's obviously
511*9880d681SAndroid Build Coastguard Worker // not safe to hoist.
512*9880d681SAndroid Build Coastguard Worker Safe = false;
513*9880d681SAndroid Build Coastguard Worker break;
514*9880d681SAndroid Build Coastguard Worker }
515*9880d681SAndroid Build Coastguard Worker }
516*9880d681SAndroid Build Coastguard Worker if (Safe)
517*9880d681SAndroid Build Coastguard Worker HoistPostRA(MI, Candidate.Def);
518*9880d681SAndroid Build Coastguard Worker }
519*9880d681SAndroid Build Coastguard Worker }
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker /// Add register 'Reg' to the livein sets of BBs in the current loop, and make
523*9880d681SAndroid Build Coastguard Worker /// sure it is not killed by any instructions in the loop.
AddToLiveIns(unsigned Reg)524*9880d681SAndroid Build Coastguard Worker void MachineLICM::AddToLiveIns(unsigned Reg) {
525*9880d681SAndroid Build Coastguard Worker const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
526*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *BB : Blocks) {
527*9880d681SAndroid Build Coastguard Worker if (!BB->isLiveIn(Reg))
528*9880d681SAndroid Build Coastguard Worker BB->addLiveIn(Reg);
529*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : *BB) {
530*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MI.operands()) {
531*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
532*9880d681SAndroid Build Coastguard Worker if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
533*9880d681SAndroid Build Coastguard Worker MO.setIsKill(false);
534*9880d681SAndroid Build Coastguard Worker }
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker }
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker /// When an instruction is found to only use loop invariant operands that is
540*9880d681SAndroid Build Coastguard Worker /// safe to hoist, this instruction is called to do the dirty work.
HoistPostRA(MachineInstr * MI,unsigned Def)541*9880d681SAndroid Build Coastguard Worker void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
542*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Preheader = getCurPreheader();
543*9880d681SAndroid Build Coastguard Worker
544*9880d681SAndroid Build Coastguard Worker // Now move the instructions to the predecessor, inserting it before any
545*9880d681SAndroid Build Coastguard Worker // terminator instructions.
546*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#"
547*9880d681SAndroid Build Coastguard Worker << MI->getParent()->getNumber() << ": " << *MI);
548*9880d681SAndroid Build Coastguard Worker
549*9880d681SAndroid Build Coastguard Worker // Splice the instruction to the preheader.
550*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
551*9880d681SAndroid Build Coastguard Worker Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
552*9880d681SAndroid Build Coastguard Worker
553*9880d681SAndroid Build Coastguard Worker // Add register to livein list to all the BBs in the current loop since a
554*9880d681SAndroid Build Coastguard Worker // loop invariant must be kept live throughout the whole loop. This is
555*9880d681SAndroid Build Coastguard Worker // important to ensure later passes do not scavenge the def register.
556*9880d681SAndroid Build Coastguard Worker AddToLiveIns(Def);
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard Worker ++NumPostRAHoisted;
559*9880d681SAndroid Build Coastguard Worker Changed = true;
560*9880d681SAndroid Build Coastguard Worker }
561*9880d681SAndroid Build Coastguard Worker
562*9880d681SAndroid Build Coastguard Worker /// Check if this mbb is guaranteed to execute. If not then a load from this mbb
563*9880d681SAndroid Build Coastguard Worker /// may not be safe to hoist.
IsGuaranteedToExecute(MachineBasicBlock * BB)564*9880d681SAndroid Build Coastguard Worker bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
565*9880d681SAndroid Build Coastguard Worker if (SpeculationState != SpeculateUnknown)
566*9880d681SAndroid Build Coastguard Worker return SpeculationState == SpeculateFalse;
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker if (BB != CurLoop->getHeader()) {
569*9880d681SAndroid Build Coastguard Worker // Check loop exiting blocks.
570*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
571*9880d681SAndroid Build Coastguard Worker CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
572*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
573*9880d681SAndroid Build Coastguard Worker if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
574*9880d681SAndroid Build Coastguard Worker SpeculationState = SpeculateTrue;
575*9880d681SAndroid Build Coastguard Worker return false;
576*9880d681SAndroid Build Coastguard Worker }
577*9880d681SAndroid Build Coastguard Worker }
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker SpeculationState = SpeculateFalse;
580*9880d681SAndroid Build Coastguard Worker return true;
581*9880d681SAndroid Build Coastguard Worker }
582*9880d681SAndroid Build Coastguard Worker
EnterScope(MachineBasicBlock * MBB)583*9880d681SAndroid Build Coastguard Worker void MachineLICM::EnterScope(MachineBasicBlock *MBB) {
584*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Entering BB#" << MBB->getNumber() << '\n');
585*9880d681SAndroid Build Coastguard Worker
586*9880d681SAndroid Build Coastguard Worker // Remember livein register pressure.
587*9880d681SAndroid Build Coastguard Worker BackTrace.push_back(RegPressure);
588*9880d681SAndroid Build Coastguard Worker }
589*9880d681SAndroid Build Coastguard Worker
ExitScope(MachineBasicBlock * MBB)590*9880d681SAndroid Build Coastguard Worker void MachineLICM::ExitScope(MachineBasicBlock *MBB) {
591*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Exiting BB#" << MBB->getNumber() << '\n');
592*9880d681SAndroid Build Coastguard Worker BackTrace.pop_back();
593*9880d681SAndroid Build Coastguard Worker }
594*9880d681SAndroid Build Coastguard Worker
595*9880d681SAndroid Build Coastguard Worker /// Destroy scope for the MBB that corresponds to the given dominator tree node
596*9880d681SAndroid Build Coastguard Worker /// if its a leaf or all of its children are done. Walk up the dominator tree to
597*9880d681SAndroid Build Coastguard Worker /// destroy ancestors which are now done.
ExitScopeIfDone(MachineDomTreeNode * Node,DenseMap<MachineDomTreeNode *,unsigned> & OpenChildren,DenseMap<MachineDomTreeNode *,MachineDomTreeNode * > & ParentMap)598*9880d681SAndroid Build Coastguard Worker void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node,
599*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
600*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
601*9880d681SAndroid Build Coastguard Worker if (OpenChildren[Node])
602*9880d681SAndroid Build Coastguard Worker return;
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard Worker // Pop scope.
605*9880d681SAndroid Build Coastguard Worker ExitScope(Node->getBlock());
606*9880d681SAndroid Build Coastguard Worker
607*9880d681SAndroid Build Coastguard Worker // Now traverse upwards to pop ancestors whose offsprings are all done.
608*9880d681SAndroid Build Coastguard Worker while (MachineDomTreeNode *Parent = ParentMap[Node]) {
609*9880d681SAndroid Build Coastguard Worker unsigned Left = --OpenChildren[Parent];
610*9880d681SAndroid Build Coastguard Worker if (Left != 0)
611*9880d681SAndroid Build Coastguard Worker break;
612*9880d681SAndroid Build Coastguard Worker ExitScope(Parent->getBlock());
613*9880d681SAndroid Build Coastguard Worker Node = Parent;
614*9880d681SAndroid Build Coastguard Worker }
615*9880d681SAndroid Build Coastguard Worker }
616*9880d681SAndroid Build Coastguard Worker
617*9880d681SAndroid Build Coastguard Worker /// Walk the specified loop in the CFG (defined by all blocks dominated by the
618*9880d681SAndroid Build Coastguard Worker /// specified header block, and that are in the current loop) in depth first
619*9880d681SAndroid Build Coastguard Worker /// order w.r.t the DominatorTree. This allows us to visit definitions before
620*9880d681SAndroid Build Coastguard Worker /// uses, allowing us to hoist a loop body in one pass without iteration.
621*9880d681SAndroid Build Coastguard Worker ///
HoistOutOfLoop(MachineDomTreeNode * HeaderN)622*9880d681SAndroid Build Coastguard Worker void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
623*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Preheader = getCurPreheader();
624*9880d681SAndroid Build Coastguard Worker if (!Preheader)
625*9880d681SAndroid Build Coastguard Worker return;
626*9880d681SAndroid Build Coastguard Worker
627*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode*, 32> Scopes;
628*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode*, 8> WorkList;
629*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
630*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
631*9880d681SAndroid Build Coastguard Worker
632*9880d681SAndroid Build Coastguard Worker // Perform a DFS walk to determine the order of visit.
633*9880d681SAndroid Build Coastguard Worker WorkList.push_back(HeaderN);
634*9880d681SAndroid Build Coastguard Worker while (!WorkList.empty()) {
635*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = WorkList.pop_back_val();
636*9880d681SAndroid Build Coastguard Worker assert(Node && "Null dominator tree node?");
637*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BB = Node->getBlock();
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker // If the header of the loop containing this basic block is a landing pad,
640*9880d681SAndroid Build Coastguard Worker // then don't try to hoist instructions out of this loop.
641*9880d681SAndroid Build Coastguard Worker const MachineLoop *ML = MLI->getLoopFor(BB);
642*9880d681SAndroid Build Coastguard Worker if (ML && ML->getHeader()->isEHPad())
643*9880d681SAndroid Build Coastguard Worker continue;
644*9880d681SAndroid Build Coastguard Worker
645*9880d681SAndroid Build Coastguard Worker // If this subregion is not in the top level loop at all, exit.
646*9880d681SAndroid Build Coastguard Worker if (!CurLoop->contains(BB))
647*9880d681SAndroid Build Coastguard Worker continue;
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker Scopes.push_back(Node);
650*9880d681SAndroid Build Coastguard Worker const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
651*9880d681SAndroid Build Coastguard Worker unsigned NumChildren = Children.size();
652*9880d681SAndroid Build Coastguard Worker
653*9880d681SAndroid Build Coastguard Worker // Don't hoist things out of a large switch statement. This often causes
654*9880d681SAndroid Build Coastguard Worker // code to be hoisted that wasn't going to be executed, and increases
655*9880d681SAndroid Build Coastguard Worker // register pressure in a situation where it's likely to matter.
656*9880d681SAndroid Build Coastguard Worker if (BB->succ_size() >= 25)
657*9880d681SAndroid Build Coastguard Worker NumChildren = 0;
658*9880d681SAndroid Build Coastguard Worker
659*9880d681SAndroid Build Coastguard Worker OpenChildren[Node] = NumChildren;
660*9880d681SAndroid Build Coastguard Worker // Add children in reverse order as then the next popped worklist node is
661*9880d681SAndroid Build Coastguard Worker // the first child of this node. This means we ultimately traverse the
662*9880d681SAndroid Build Coastguard Worker // DOM tree in exactly the same order as if we'd recursed.
663*9880d681SAndroid Build Coastguard Worker for (int i = (int)NumChildren-1; i >= 0; --i) {
664*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Child = Children[i];
665*9880d681SAndroid Build Coastguard Worker ParentMap[Child] = Node;
666*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Child);
667*9880d681SAndroid Build Coastguard Worker }
668*9880d681SAndroid Build Coastguard Worker }
669*9880d681SAndroid Build Coastguard Worker
670*9880d681SAndroid Build Coastguard Worker if (Scopes.size() == 0)
671*9880d681SAndroid Build Coastguard Worker return;
672*9880d681SAndroid Build Coastguard Worker
673*9880d681SAndroid Build Coastguard Worker // Compute registers which are livein into the loop headers.
674*9880d681SAndroid Build Coastguard Worker RegSeen.clear();
675*9880d681SAndroid Build Coastguard Worker BackTrace.clear();
676*9880d681SAndroid Build Coastguard Worker InitRegPressure(Preheader);
677*9880d681SAndroid Build Coastguard Worker
678*9880d681SAndroid Build Coastguard Worker // Now perform LICM.
679*9880d681SAndroid Build Coastguard Worker for (MachineDomTreeNode *Node : Scopes) {
680*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = Node->getBlock();
681*9880d681SAndroid Build Coastguard Worker
682*9880d681SAndroid Build Coastguard Worker EnterScope(MBB);
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker // Process the block
685*9880d681SAndroid Build Coastguard Worker SpeculationState = SpeculateUnknown;
686*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator
687*9880d681SAndroid Build Coastguard Worker MII = MBB->begin(), E = MBB->end(); MII != E; ) {
688*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator NextMII = MII; ++NextMII;
689*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = &*MII;
690*9880d681SAndroid Build Coastguard Worker if (!Hoist(MI, Preheader))
691*9880d681SAndroid Build Coastguard Worker UpdateRegPressure(MI);
692*9880d681SAndroid Build Coastguard Worker MII = NextMII;
693*9880d681SAndroid Build Coastguard Worker }
694*9880d681SAndroid Build Coastguard Worker
695*9880d681SAndroid Build Coastguard Worker // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
696*9880d681SAndroid Build Coastguard Worker ExitScopeIfDone(Node, OpenChildren, ParentMap);
697*9880d681SAndroid Build Coastguard Worker }
698*9880d681SAndroid Build Coastguard Worker }
699*9880d681SAndroid Build Coastguard Worker
700*9880d681SAndroid Build Coastguard Worker /// Sink instructions into loops if profitable. This especially tries to prevent
701*9880d681SAndroid Build Coastguard Worker /// register spills caused by register pressure if there is little to no
702*9880d681SAndroid Build Coastguard Worker /// overhead moving instructions into loops.
SinkIntoLoop()703*9880d681SAndroid Build Coastguard Worker void MachineLICM::SinkIntoLoop() {
704*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Preheader = getCurPreheader();
705*9880d681SAndroid Build Coastguard Worker if (!Preheader)
706*9880d681SAndroid Build Coastguard Worker return;
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr *, 8> Candidates;
709*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin();
710*9880d681SAndroid Build Coastguard Worker I != Preheader->instr_end(); ++I) {
711*9880d681SAndroid Build Coastguard Worker // We need to ensure that we can safely move this instruction into the loop.
712*9880d681SAndroid Build Coastguard Worker // As such, it must not have side-effects, e.g. such as a call has.
713*9880d681SAndroid Build Coastguard Worker if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I))
714*9880d681SAndroid Build Coastguard Worker Candidates.push_back(&*I);
715*9880d681SAndroid Build Coastguard Worker }
716*9880d681SAndroid Build Coastguard Worker
717*9880d681SAndroid Build Coastguard Worker for (MachineInstr *I : Candidates) {
718*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = I->getOperand(0);
719*9880d681SAndroid Build Coastguard Worker if (!MO.isDef() || !MO.isReg() || !MO.getReg())
720*9880d681SAndroid Build Coastguard Worker continue;
721*9880d681SAndroid Build Coastguard Worker if (!MRI->hasOneDef(MO.getReg()))
722*9880d681SAndroid Build Coastguard Worker continue;
723*9880d681SAndroid Build Coastguard Worker bool CanSink = true;
724*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *B = nullptr;
725*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
726*9880d681SAndroid Build Coastguard Worker // FIXME: Come up with a proper cost model that estimates whether sinking
727*9880d681SAndroid Build Coastguard Worker // the instruction (and thus possibly executing it on every loop
728*9880d681SAndroid Build Coastguard Worker // iteration) is more expensive than a register.
729*9880d681SAndroid Build Coastguard Worker // For now assumes that copies are cheap and thus almost always worth it.
730*9880d681SAndroid Build Coastguard Worker if (!MI.isCopy()) {
731*9880d681SAndroid Build Coastguard Worker CanSink = false;
732*9880d681SAndroid Build Coastguard Worker break;
733*9880d681SAndroid Build Coastguard Worker }
734*9880d681SAndroid Build Coastguard Worker if (!B) {
735*9880d681SAndroid Build Coastguard Worker B = MI.getParent();
736*9880d681SAndroid Build Coastguard Worker continue;
737*9880d681SAndroid Build Coastguard Worker }
738*9880d681SAndroid Build Coastguard Worker B = DT->findNearestCommonDominator(B, MI.getParent());
739*9880d681SAndroid Build Coastguard Worker if (!B) {
740*9880d681SAndroid Build Coastguard Worker CanSink = false;
741*9880d681SAndroid Build Coastguard Worker break;
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker }
744*9880d681SAndroid Build Coastguard Worker if (!CanSink || !B || B == Preheader)
745*9880d681SAndroid Build Coastguard Worker continue;
746*9880d681SAndroid Build Coastguard Worker B->splice(B->getFirstNonPHI(), Preheader, I);
747*9880d681SAndroid Build Coastguard Worker }
748*9880d681SAndroid Build Coastguard Worker }
749*9880d681SAndroid Build Coastguard Worker
isOperandKill(const MachineOperand & MO,MachineRegisterInfo * MRI)750*9880d681SAndroid Build Coastguard Worker static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
751*9880d681SAndroid Build Coastguard Worker return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
752*9880d681SAndroid Build Coastguard Worker }
753*9880d681SAndroid Build Coastguard Worker
754*9880d681SAndroid Build Coastguard Worker /// Find all virtual register references that are liveout of the preheader to
755*9880d681SAndroid Build Coastguard Worker /// initialize the starting "register pressure". Note this does not count live
756*9880d681SAndroid Build Coastguard Worker /// through (livein but not used) registers.
InitRegPressure(MachineBasicBlock * BB)757*9880d681SAndroid Build Coastguard Worker void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
758*9880d681SAndroid Build Coastguard Worker std::fill(RegPressure.begin(), RegPressure.end(), 0);
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard Worker // If the preheader has only a single predecessor and it ends with a
761*9880d681SAndroid Build Coastguard Worker // fallthrough or an unconditional branch, then scan its predecessor for live
762*9880d681SAndroid Build Coastguard Worker // defs as well. This happens whenever the preheader is created by splitting
763*9880d681SAndroid Build Coastguard Worker // the critical edge from the loop predecessor to the loop header.
764*9880d681SAndroid Build Coastguard Worker if (BB->pred_size() == 1) {
765*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
766*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 4> Cond;
767*9880d681SAndroid Build Coastguard Worker if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
768*9880d681SAndroid Build Coastguard Worker InitRegPressure(*BB->pred_begin());
769*9880d681SAndroid Build Coastguard Worker }
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard Worker for (const MachineInstr &MI : *BB)
772*9880d681SAndroid Build Coastguard Worker UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
773*9880d681SAndroid Build Coastguard Worker }
774*9880d681SAndroid Build Coastguard Worker
775*9880d681SAndroid Build Coastguard Worker /// Update estimate of register pressure after the specified instruction.
UpdateRegPressure(const MachineInstr * MI,bool ConsiderUnseenAsDef)776*9880d681SAndroid Build Coastguard Worker void MachineLICM::UpdateRegPressure(const MachineInstr *MI,
777*9880d681SAndroid Build Coastguard Worker bool ConsiderUnseenAsDef) {
778*9880d681SAndroid Build Coastguard Worker auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
779*9880d681SAndroid Build Coastguard Worker for (const auto &RPIdAndCost : Cost) {
780*9880d681SAndroid Build Coastguard Worker unsigned Class = RPIdAndCost.first;
781*9880d681SAndroid Build Coastguard Worker if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
782*9880d681SAndroid Build Coastguard Worker RegPressure[Class] = 0;
783*9880d681SAndroid Build Coastguard Worker else
784*9880d681SAndroid Build Coastguard Worker RegPressure[Class] += RPIdAndCost.second;
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker }
787*9880d681SAndroid Build Coastguard Worker
788*9880d681SAndroid Build Coastguard Worker /// Calculate the additional register pressure that the registers used in MI
789*9880d681SAndroid Build Coastguard Worker /// cause.
790*9880d681SAndroid Build Coastguard Worker ///
791*9880d681SAndroid Build Coastguard Worker /// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
792*9880d681SAndroid Build Coastguard Worker /// figure out which usages are live-ins.
793*9880d681SAndroid Build Coastguard Worker /// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
794*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, int>
calcRegisterCost(const MachineInstr * MI,bool ConsiderSeen,bool ConsiderUnseenAsDef)795*9880d681SAndroid Build Coastguard Worker MachineLICM::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
796*9880d681SAndroid Build Coastguard Worker bool ConsiderUnseenAsDef) {
797*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, int> Cost;
798*9880d681SAndroid Build Coastguard Worker if (MI->isImplicitDef())
799*9880d681SAndroid Build Coastguard Worker return Cost;
800*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
801*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(i);
802*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isImplicit())
803*9880d681SAndroid Build Coastguard Worker continue;
804*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
805*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
806*9880d681SAndroid Build Coastguard Worker continue;
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker // FIXME: It seems bad to use RegSeen only for some of these calculations.
809*9880d681SAndroid Build Coastguard Worker bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
810*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC = MRI->getRegClass(Reg);
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard Worker RegClassWeight W = TRI->getRegClassWeight(RC);
813*9880d681SAndroid Build Coastguard Worker int RCCost = 0;
814*9880d681SAndroid Build Coastguard Worker if (MO.isDef())
815*9880d681SAndroid Build Coastguard Worker RCCost = W.RegWeight;
816*9880d681SAndroid Build Coastguard Worker else {
817*9880d681SAndroid Build Coastguard Worker bool isKill = isOperandKill(MO, MRI);
818*9880d681SAndroid Build Coastguard Worker if (isNew && !isKill && ConsiderUnseenAsDef)
819*9880d681SAndroid Build Coastguard Worker // Haven't seen this, it must be a livein.
820*9880d681SAndroid Build Coastguard Worker RCCost = W.RegWeight;
821*9880d681SAndroid Build Coastguard Worker else if (!isNew && isKill)
822*9880d681SAndroid Build Coastguard Worker RCCost = -W.RegWeight;
823*9880d681SAndroid Build Coastguard Worker }
824*9880d681SAndroid Build Coastguard Worker if (RCCost == 0)
825*9880d681SAndroid Build Coastguard Worker continue;
826*9880d681SAndroid Build Coastguard Worker const int *PS = TRI->getRegClassPressureSets(RC);
827*9880d681SAndroid Build Coastguard Worker for (; *PS != -1; ++PS) {
828*9880d681SAndroid Build Coastguard Worker if (Cost.find(*PS) == Cost.end())
829*9880d681SAndroid Build Coastguard Worker Cost[*PS] = RCCost;
830*9880d681SAndroid Build Coastguard Worker else
831*9880d681SAndroid Build Coastguard Worker Cost[*PS] += RCCost;
832*9880d681SAndroid Build Coastguard Worker }
833*9880d681SAndroid Build Coastguard Worker }
834*9880d681SAndroid Build Coastguard Worker return Cost;
835*9880d681SAndroid Build Coastguard Worker }
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker /// Return true if this machine instruction loads from global offset table or
838*9880d681SAndroid Build Coastguard Worker /// constant pool.
mayLoadFromGOTOrConstantPool(MachineInstr & MI)839*9880d681SAndroid Build Coastguard Worker static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
840*9880d681SAndroid Build Coastguard Worker assert (MI.mayLoad() && "Expected MI that loads!");
841*9880d681SAndroid Build Coastguard Worker
842*9880d681SAndroid Build Coastguard Worker // If we lost memory operands, conservatively assume that the instruction
843*9880d681SAndroid Build Coastguard Worker // reads from everything..
844*9880d681SAndroid Build Coastguard Worker if (MI.memoperands_empty())
845*9880d681SAndroid Build Coastguard Worker return true;
846*9880d681SAndroid Build Coastguard Worker
847*9880d681SAndroid Build Coastguard Worker for (MachineMemOperand *MemOp : MI.memoperands())
848*9880d681SAndroid Build Coastguard Worker if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
849*9880d681SAndroid Build Coastguard Worker if (PSV->isGOT() || PSV->isConstantPool())
850*9880d681SAndroid Build Coastguard Worker return true;
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard Worker return false;
853*9880d681SAndroid Build Coastguard Worker }
854*9880d681SAndroid Build Coastguard Worker
855*9880d681SAndroid Build Coastguard Worker /// Returns true if the instruction may be a suitable candidate for LICM.
856*9880d681SAndroid Build Coastguard Worker /// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
IsLICMCandidate(MachineInstr & I)857*9880d681SAndroid Build Coastguard Worker bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
858*9880d681SAndroid Build Coastguard Worker // Check if it's safe to move the instruction.
859*9880d681SAndroid Build Coastguard Worker bool DontMoveAcrossStore = true;
860*9880d681SAndroid Build Coastguard Worker if (!I.isSafeToMove(AA, DontMoveAcrossStore))
861*9880d681SAndroid Build Coastguard Worker return false;
862*9880d681SAndroid Build Coastguard Worker
863*9880d681SAndroid Build Coastguard Worker // If it is load then check if it is guaranteed to execute by making sure that
864*9880d681SAndroid Build Coastguard Worker // it dominates all exiting blocks. If it doesn't, then there is a path out of
865*9880d681SAndroid Build Coastguard Worker // the loop which does not execute this load, so we can't hoist it. Loads
866*9880d681SAndroid Build Coastguard Worker // from constant memory are not safe to speculate all the time, for example
867*9880d681SAndroid Build Coastguard Worker // indexed load from a jump table.
868*9880d681SAndroid Build Coastguard Worker // Stores and side effects are already checked by isSafeToMove.
869*9880d681SAndroid Build Coastguard Worker if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
870*9880d681SAndroid Build Coastguard Worker !IsGuaranteedToExecute(I.getParent()))
871*9880d681SAndroid Build Coastguard Worker return false;
872*9880d681SAndroid Build Coastguard Worker
873*9880d681SAndroid Build Coastguard Worker return true;
874*9880d681SAndroid Build Coastguard Worker }
875*9880d681SAndroid Build Coastguard Worker
876*9880d681SAndroid Build Coastguard Worker /// Returns true if the instruction is loop invariant.
877*9880d681SAndroid Build Coastguard Worker /// I.e., all virtual register operands are defined outside of the loop,
878*9880d681SAndroid Build Coastguard Worker /// physical registers aren't accessed explicitly, and there are no side
879*9880d681SAndroid Build Coastguard Worker /// effects that aren't captured by the operands or other flags.
880*9880d681SAndroid Build Coastguard Worker ///
IsLoopInvariantInst(MachineInstr & I)881*9880d681SAndroid Build Coastguard Worker bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
882*9880d681SAndroid Build Coastguard Worker if (!IsLICMCandidate(I))
883*9880d681SAndroid Build Coastguard Worker return false;
884*9880d681SAndroid Build Coastguard Worker
885*9880d681SAndroid Build Coastguard Worker // The instruction is loop invariant if all of its operands are.
886*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : I.operands()) {
887*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
888*9880d681SAndroid Build Coastguard Worker continue;
889*9880d681SAndroid Build Coastguard Worker
890*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
891*9880d681SAndroid Build Coastguard Worker if (Reg == 0) continue;
892*9880d681SAndroid Build Coastguard Worker
893*9880d681SAndroid Build Coastguard Worker // Don't hoist an instruction that uses or defines a physical register.
894*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
895*9880d681SAndroid Build Coastguard Worker if (MO.isUse()) {
896*9880d681SAndroid Build Coastguard Worker // If the physreg has no defs anywhere, it's just an ambient register
897*9880d681SAndroid Build Coastguard Worker // and we can freely move its uses. Alternatively, if it's allocatable,
898*9880d681SAndroid Build Coastguard Worker // it could get allocated to something with a def during allocation.
899*9880d681SAndroid Build Coastguard Worker if (!MRI->isConstantPhysReg(Reg, *I.getParent()->getParent()))
900*9880d681SAndroid Build Coastguard Worker return false;
901*9880d681SAndroid Build Coastguard Worker // Otherwise it's safe to move.
902*9880d681SAndroid Build Coastguard Worker continue;
903*9880d681SAndroid Build Coastguard Worker } else if (!MO.isDead()) {
904*9880d681SAndroid Build Coastguard Worker // A def that isn't dead. We can't move it.
905*9880d681SAndroid Build Coastguard Worker return false;
906*9880d681SAndroid Build Coastguard Worker } else if (CurLoop->getHeader()->isLiveIn(Reg)) {
907*9880d681SAndroid Build Coastguard Worker // If the reg is live into the loop, we can't hoist an instruction
908*9880d681SAndroid Build Coastguard Worker // which would clobber it.
909*9880d681SAndroid Build Coastguard Worker return false;
910*9880d681SAndroid Build Coastguard Worker }
911*9880d681SAndroid Build Coastguard Worker }
912*9880d681SAndroid Build Coastguard Worker
913*9880d681SAndroid Build Coastguard Worker if (!MO.isUse())
914*9880d681SAndroid Build Coastguard Worker continue;
915*9880d681SAndroid Build Coastguard Worker
916*9880d681SAndroid Build Coastguard Worker assert(MRI->getVRegDef(Reg) &&
917*9880d681SAndroid Build Coastguard Worker "Machine instr not mapped for this vreg?!");
918*9880d681SAndroid Build Coastguard Worker
919*9880d681SAndroid Build Coastguard Worker // If the loop contains the definition of an operand, then the instruction
920*9880d681SAndroid Build Coastguard Worker // isn't loop invariant.
921*9880d681SAndroid Build Coastguard Worker if (CurLoop->contains(MRI->getVRegDef(Reg)))
922*9880d681SAndroid Build Coastguard Worker return false;
923*9880d681SAndroid Build Coastguard Worker }
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker // If we got this far, the instruction is loop invariant!
926*9880d681SAndroid Build Coastguard Worker return true;
927*9880d681SAndroid Build Coastguard Worker }
928*9880d681SAndroid Build Coastguard Worker
929*9880d681SAndroid Build Coastguard Worker
930*9880d681SAndroid Build Coastguard Worker /// Return true if the specified instruction is used by a phi node and hoisting
931*9880d681SAndroid Build Coastguard Worker /// it could cause a copy to be inserted.
HasLoopPHIUse(const MachineInstr * MI) const932*9880d681SAndroid Build Coastguard Worker bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
933*9880d681SAndroid Build Coastguard Worker SmallVector<const MachineInstr*, 8> Work(1, MI);
934*9880d681SAndroid Build Coastguard Worker do {
935*9880d681SAndroid Build Coastguard Worker MI = Work.pop_back_val();
936*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
937*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
938*9880d681SAndroid Build Coastguard Worker continue;
939*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
940*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
941*9880d681SAndroid Build Coastguard Worker continue;
942*9880d681SAndroid Build Coastguard Worker for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
943*9880d681SAndroid Build Coastguard Worker // A PHI may cause a copy to be inserted.
944*9880d681SAndroid Build Coastguard Worker if (UseMI.isPHI()) {
945*9880d681SAndroid Build Coastguard Worker // A PHI inside the loop causes a copy because the live range of Reg is
946*9880d681SAndroid Build Coastguard Worker // extended across the PHI.
947*9880d681SAndroid Build Coastguard Worker if (CurLoop->contains(&UseMI))
948*9880d681SAndroid Build Coastguard Worker return true;
949*9880d681SAndroid Build Coastguard Worker // A PHI in an exit block can cause a copy to be inserted if the PHI
950*9880d681SAndroid Build Coastguard Worker // has multiple predecessors in the loop with different values.
951*9880d681SAndroid Build Coastguard Worker // For now, approximate by rejecting all exit blocks.
952*9880d681SAndroid Build Coastguard Worker if (isExitBlock(UseMI.getParent()))
953*9880d681SAndroid Build Coastguard Worker return true;
954*9880d681SAndroid Build Coastguard Worker continue;
955*9880d681SAndroid Build Coastguard Worker }
956*9880d681SAndroid Build Coastguard Worker // Look past copies as well.
957*9880d681SAndroid Build Coastguard Worker if (UseMI.isCopy() && CurLoop->contains(&UseMI))
958*9880d681SAndroid Build Coastguard Worker Work.push_back(&UseMI);
959*9880d681SAndroid Build Coastguard Worker }
960*9880d681SAndroid Build Coastguard Worker }
961*9880d681SAndroid Build Coastguard Worker } while (!Work.empty());
962*9880d681SAndroid Build Coastguard Worker return false;
963*9880d681SAndroid Build Coastguard Worker }
964*9880d681SAndroid Build Coastguard Worker
965*9880d681SAndroid Build Coastguard Worker /// Compute operand latency between a def of 'Reg' and an use in the current
966*9880d681SAndroid Build Coastguard Worker /// loop, return true if the target considered it high.
HasHighOperandLatency(MachineInstr & MI,unsigned DefIdx,unsigned Reg) const967*9880d681SAndroid Build Coastguard Worker bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
968*9880d681SAndroid Build Coastguard Worker unsigned DefIdx, unsigned Reg) const {
969*9880d681SAndroid Build Coastguard Worker if (MRI->use_nodbg_empty(Reg))
970*9880d681SAndroid Build Coastguard Worker return false;
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard Worker for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
973*9880d681SAndroid Build Coastguard Worker if (UseMI.isCopyLike())
974*9880d681SAndroid Build Coastguard Worker continue;
975*9880d681SAndroid Build Coastguard Worker if (!CurLoop->contains(UseMI.getParent()))
976*9880d681SAndroid Build Coastguard Worker continue;
977*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
978*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = UseMI.getOperand(i);
979*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse())
980*9880d681SAndroid Build Coastguard Worker continue;
981*9880d681SAndroid Build Coastguard Worker unsigned MOReg = MO.getReg();
982*9880d681SAndroid Build Coastguard Worker if (MOReg != Reg)
983*9880d681SAndroid Build Coastguard Worker continue;
984*9880d681SAndroid Build Coastguard Worker
985*9880d681SAndroid Build Coastguard Worker if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
986*9880d681SAndroid Build Coastguard Worker return true;
987*9880d681SAndroid Build Coastguard Worker }
988*9880d681SAndroid Build Coastguard Worker
989*9880d681SAndroid Build Coastguard Worker // Only look at the first in loop use.
990*9880d681SAndroid Build Coastguard Worker break;
991*9880d681SAndroid Build Coastguard Worker }
992*9880d681SAndroid Build Coastguard Worker
993*9880d681SAndroid Build Coastguard Worker return false;
994*9880d681SAndroid Build Coastguard Worker }
995*9880d681SAndroid Build Coastguard Worker
996*9880d681SAndroid Build Coastguard Worker /// Return true if the instruction is marked "cheap" or the operand latency
997*9880d681SAndroid Build Coastguard Worker /// between its def and a use is one or less.
IsCheapInstruction(MachineInstr & MI) const998*9880d681SAndroid Build Coastguard Worker bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
999*9880d681SAndroid Build Coastguard Worker if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike())
1000*9880d681SAndroid Build Coastguard Worker return true;
1001*9880d681SAndroid Build Coastguard Worker
1002*9880d681SAndroid Build Coastguard Worker bool isCheap = false;
1003*9880d681SAndroid Build Coastguard Worker unsigned NumDefs = MI.getDesc().getNumDefs();
1004*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1005*9880d681SAndroid Build Coastguard Worker MachineOperand &DefMO = MI.getOperand(i);
1006*9880d681SAndroid Build Coastguard Worker if (!DefMO.isReg() || !DefMO.isDef())
1007*9880d681SAndroid Build Coastguard Worker continue;
1008*9880d681SAndroid Build Coastguard Worker --NumDefs;
1009*9880d681SAndroid Build Coastguard Worker unsigned Reg = DefMO.getReg();
1010*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg))
1011*9880d681SAndroid Build Coastguard Worker continue;
1012*9880d681SAndroid Build Coastguard Worker
1013*9880d681SAndroid Build Coastguard Worker if (!TII->hasLowDefLatency(SchedModel, MI, i))
1014*9880d681SAndroid Build Coastguard Worker return false;
1015*9880d681SAndroid Build Coastguard Worker isCheap = true;
1016*9880d681SAndroid Build Coastguard Worker }
1017*9880d681SAndroid Build Coastguard Worker
1018*9880d681SAndroid Build Coastguard Worker return isCheap;
1019*9880d681SAndroid Build Coastguard Worker }
1020*9880d681SAndroid Build Coastguard Worker
1021*9880d681SAndroid Build Coastguard Worker /// Visit BBs from header to current BB, check if hoisting an instruction of the
1022*9880d681SAndroid Build Coastguard Worker /// given cost matrix can cause high register pressure.
CanCauseHighRegPressure(const DenseMap<unsigned,int> & Cost,bool CheapInstr)1023*9880d681SAndroid Build Coastguard Worker bool MachineLICM::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
1024*9880d681SAndroid Build Coastguard Worker bool CheapInstr) {
1025*9880d681SAndroid Build Coastguard Worker for (const auto &RPIdAndCost : Cost) {
1026*9880d681SAndroid Build Coastguard Worker if (RPIdAndCost.second <= 0)
1027*9880d681SAndroid Build Coastguard Worker continue;
1028*9880d681SAndroid Build Coastguard Worker
1029*9880d681SAndroid Build Coastguard Worker unsigned Class = RPIdAndCost.first;
1030*9880d681SAndroid Build Coastguard Worker int Limit = RegLimit[Class];
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard Worker // Don't hoist cheap instructions if they would increase register pressure,
1033*9880d681SAndroid Build Coastguard Worker // even if we're under the limit.
1034*9880d681SAndroid Build Coastguard Worker if (CheapInstr && !HoistCheapInsts)
1035*9880d681SAndroid Build Coastguard Worker return true;
1036*9880d681SAndroid Build Coastguard Worker
1037*9880d681SAndroid Build Coastguard Worker for (const auto &RP : BackTrace)
1038*9880d681SAndroid Build Coastguard Worker if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
1039*9880d681SAndroid Build Coastguard Worker return true;
1040*9880d681SAndroid Build Coastguard Worker }
1041*9880d681SAndroid Build Coastguard Worker
1042*9880d681SAndroid Build Coastguard Worker return false;
1043*9880d681SAndroid Build Coastguard Worker }
1044*9880d681SAndroid Build Coastguard Worker
1045*9880d681SAndroid Build Coastguard Worker /// Traverse the back trace from header to the current block and update their
1046*9880d681SAndroid Build Coastguard Worker /// register pressures to reflect the effect of hoisting MI from the current
1047*9880d681SAndroid Build Coastguard Worker /// block to the preheader.
UpdateBackTraceRegPressure(const MachineInstr * MI)1048*9880d681SAndroid Build Coastguard Worker void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1049*9880d681SAndroid Build Coastguard Worker // First compute the 'cost' of the instruction, i.e. its contribution
1050*9880d681SAndroid Build Coastguard Worker // to register pressure.
1051*9880d681SAndroid Build Coastguard Worker auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1052*9880d681SAndroid Build Coastguard Worker /*ConsiderUnseenAsDef=*/false);
1053*9880d681SAndroid Build Coastguard Worker
1054*9880d681SAndroid Build Coastguard Worker // Update register pressure of blocks from loop header to current block.
1055*9880d681SAndroid Build Coastguard Worker for (auto &RP : BackTrace)
1056*9880d681SAndroid Build Coastguard Worker for (const auto &RPIdAndCost : Cost)
1057*9880d681SAndroid Build Coastguard Worker RP[RPIdAndCost.first] += RPIdAndCost.second;
1058*9880d681SAndroid Build Coastguard Worker }
1059*9880d681SAndroid Build Coastguard Worker
1060*9880d681SAndroid Build Coastguard Worker /// Return true if it is potentially profitable to hoist the given loop
1061*9880d681SAndroid Build Coastguard Worker /// invariant.
IsProfitableToHoist(MachineInstr & MI)1062*9880d681SAndroid Build Coastguard Worker bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
1063*9880d681SAndroid Build Coastguard Worker if (MI.isImplicitDef())
1064*9880d681SAndroid Build Coastguard Worker return true;
1065*9880d681SAndroid Build Coastguard Worker
1066*9880d681SAndroid Build Coastguard Worker // Besides removing computation from the loop, hoisting an instruction has
1067*9880d681SAndroid Build Coastguard Worker // these effects:
1068*9880d681SAndroid Build Coastguard Worker //
1069*9880d681SAndroid Build Coastguard Worker // - The value defined by the instruction becomes live across the entire
1070*9880d681SAndroid Build Coastguard Worker // loop. This increases register pressure in the loop.
1071*9880d681SAndroid Build Coastguard Worker //
1072*9880d681SAndroid Build Coastguard Worker // - If the value is used by a PHI in the loop, a copy will be required for
1073*9880d681SAndroid Build Coastguard Worker // lowering the PHI after extending the live range.
1074*9880d681SAndroid Build Coastguard Worker //
1075*9880d681SAndroid Build Coastguard Worker // - When hoisting the last use of a value in the loop, that value no longer
1076*9880d681SAndroid Build Coastguard Worker // needs to be live in the loop. This lowers register pressure in the loop.
1077*9880d681SAndroid Build Coastguard Worker
1078*9880d681SAndroid Build Coastguard Worker bool CheapInstr = IsCheapInstruction(MI);
1079*9880d681SAndroid Build Coastguard Worker bool CreatesCopy = HasLoopPHIUse(&MI);
1080*9880d681SAndroid Build Coastguard Worker
1081*9880d681SAndroid Build Coastguard Worker // Don't hoist a cheap instruction if it would create a copy in the loop.
1082*9880d681SAndroid Build Coastguard Worker if (CheapInstr && CreatesCopy) {
1083*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1084*9880d681SAndroid Build Coastguard Worker return false;
1085*9880d681SAndroid Build Coastguard Worker }
1086*9880d681SAndroid Build Coastguard Worker
1087*9880d681SAndroid Build Coastguard Worker // Rematerializable instructions should always be hoisted since the register
1088*9880d681SAndroid Build Coastguard Worker // allocator can just pull them down again when needed.
1089*9880d681SAndroid Build Coastguard Worker if (TII->isTriviallyReMaterializable(MI, AA))
1090*9880d681SAndroid Build Coastguard Worker return true;
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard Worker // FIXME: If there are long latency loop-invariant instructions inside the
1093*9880d681SAndroid Build Coastguard Worker // loop at this point, why didn't the optimizer's LICM hoist them?
1094*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1095*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
1096*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isImplicit())
1097*9880d681SAndroid Build Coastguard Worker continue;
1098*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
1099*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
1100*9880d681SAndroid Build Coastguard Worker continue;
1101*9880d681SAndroid Build Coastguard Worker if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
1102*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Hoist High Latency: " << MI);
1103*9880d681SAndroid Build Coastguard Worker ++NumHighLatency;
1104*9880d681SAndroid Build Coastguard Worker return true;
1105*9880d681SAndroid Build Coastguard Worker }
1106*9880d681SAndroid Build Coastguard Worker }
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard Worker // Estimate register pressure to determine whether to LICM the instruction.
1109*9880d681SAndroid Build Coastguard Worker // In low register pressure situation, we can be more aggressive about
1110*9880d681SAndroid Build Coastguard Worker // hoisting. Also, favors hoisting long latency instructions even in
1111*9880d681SAndroid Build Coastguard Worker // moderately high pressure situation.
1112*9880d681SAndroid Build Coastguard Worker // Cheap instructions will only be hoisted if they don't increase register
1113*9880d681SAndroid Build Coastguard Worker // pressure at all.
1114*9880d681SAndroid Build Coastguard Worker auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1115*9880d681SAndroid Build Coastguard Worker /*ConsiderUnseenAsDef=*/false);
1116*9880d681SAndroid Build Coastguard Worker
1117*9880d681SAndroid Build Coastguard Worker // Visit BBs from header to current BB, if hoisting this doesn't cause
1118*9880d681SAndroid Build Coastguard Worker // high register pressure, then it's safe to proceed.
1119*9880d681SAndroid Build Coastguard Worker if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1120*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1121*9880d681SAndroid Build Coastguard Worker ++NumLowRP;
1122*9880d681SAndroid Build Coastguard Worker return true;
1123*9880d681SAndroid Build Coastguard Worker }
1124*9880d681SAndroid Build Coastguard Worker
1125*9880d681SAndroid Build Coastguard Worker // Don't risk increasing register pressure if it would create copies.
1126*9880d681SAndroid Build Coastguard Worker if (CreatesCopy) {
1127*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1128*9880d681SAndroid Build Coastguard Worker return false;
1129*9880d681SAndroid Build Coastguard Worker }
1130*9880d681SAndroid Build Coastguard Worker
1131*9880d681SAndroid Build Coastguard Worker // Do not "speculate" in high register pressure situation. If an
1132*9880d681SAndroid Build Coastguard Worker // instruction is not guaranteed to be executed in the loop, it's best to be
1133*9880d681SAndroid Build Coastguard Worker // conservative.
1134*9880d681SAndroid Build Coastguard Worker if (AvoidSpeculation &&
1135*9880d681SAndroid Build Coastguard Worker (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) {
1136*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Won't speculate: " << MI);
1137*9880d681SAndroid Build Coastguard Worker return false;
1138*9880d681SAndroid Build Coastguard Worker }
1139*9880d681SAndroid Build Coastguard Worker
1140*9880d681SAndroid Build Coastguard Worker // High register pressure situation, only hoist if the instruction is going
1141*9880d681SAndroid Build Coastguard Worker // to be remat'ed.
1142*9880d681SAndroid Build Coastguard Worker if (!TII->isTriviallyReMaterializable(MI, AA) && !MI.isInvariantLoad(AA)) {
1143*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1144*9880d681SAndroid Build Coastguard Worker return false;
1145*9880d681SAndroid Build Coastguard Worker }
1146*9880d681SAndroid Build Coastguard Worker
1147*9880d681SAndroid Build Coastguard Worker return true;
1148*9880d681SAndroid Build Coastguard Worker }
1149*9880d681SAndroid Build Coastguard Worker
1150*9880d681SAndroid Build Coastguard Worker /// Unfold a load from the given machineinstr if the load itself could be
1151*9880d681SAndroid Build Coastguard Worker /// hoisted. Return the unfolded and hoistable load, or null if the load
1152*9880d681SAndroid Build Coastguard Worker /// couldn't be unfolded or if it wouldn't be hoistable.
ExtractHoistableLoad(MachineInstr * MI)1153*9880d681SAndroid Build Coastguard Worker MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
1154*9880d681SAndroid Build Coastguard Worker // Don't unfold simple loads.
1155*9880d681SAndroid Build Coastguard Worker if (MI->canFoldAsLoad())
1156*9880d681SAndroid Build Coastguard Worker return nullptr;
1157*9880d681SAndroid Build Coastguard Worker
1158*9880d681SAndroid Build Coastguard Worker // If not, we may be able to unfold a load and hoist that.
1159*9880d681SAndroid Build Coastguard Worker // First test whether the instruction is loading from an amenable
1160*9880d681SAndroid Build Coastguard Worker // memory location.
1161*9880d681SAndroid Build Coastguard Worker if (!MI->isInvariantLoad(AA))
1162*9880d681SAndroid Build Coastguard Worker return nullptr;
1163*9880d681SAndroid Build Coastguard Worker
1164*9880d681SAndroid Build Coastguard Worker // Next determine the register class for a temporary register.
1165*9880d681SAndroid Build Coastguard Worker unsigned LoadRegIndex;
1166*9880d681SAndroid Build Coastguard Worker unsigned NewOpc =
1167*9880d681SAndroid Build Coastguard Worker TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1168*9880d681SAndroid Build Coastguard Worker /*UnfoldLoad=*/true,
1169*9880d681SAndroid Build Coastguard Worker /*UnfoldStore=*/false,
1170*9880d681SAndroid Build Coastguard Worker &LoadRegIndex);
1171*9880d681SAndroid Build Coastguard Worker if (NewOpc == 0) return nullptr;
1172*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MID = TII->get(NewOpc);
1173*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MI->getParent()->getParent();
1174*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
1175*9880d681SAndroid Build Coastguard Worker // Ok, we're unfolding. Create a temporary register and do the unfold.
1176*9880d681SAndroid Build Coastguard Worker unsigned Reg = MRI->createVirtualRegister(RC);
1177*9880d681SAndroid Build Coastguard Worker
1178*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr *, 2> NewMIs;
1179*9880d681SAndroid Build Coastguard Worker bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
1180*9880d681SAndroid Build Coastguard Worker /*UnfoldLoad=*/true,
1181*9880d681SAndroid Build Coastguard Worker /*UnfoldStore=*/false, NewMIs);
1182*9880d681SAndroid Build Coastguard Worker (void)Success;
1183*9880d681SAndroid Build Coastguard Worker assert(Success &&
1184*9880d681SAndroid Build Coastguard Worker "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1185*9880d681SAndroid Build Coastguard Worker "succeeded!");
1186*9880d681SAndroid Build Coastguard Worker assert(NewMIs.size() == 2 &&
1187*9880d681SAndroid Build Coastguard Worker "Unfolded a load into multiple instructions!");
1188*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
1189*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator Pos = MI;
1190*9880d681SAndroid Build Coastguard Worker MBB->insert(Pos, NewMIs[0]);
1191*9880d681SAndroid Build Coastguard Worker MBB->insert(Pos, NewMIs[1]);
1192*9880d681SAndroid Build Coastguard Worker // If unfolding produced a load that wasn't loop-invariant or profitable to
1193*9880d681SAndroid Build Coastguard Worker // hoist, discard the new instructions and bail.
1194*9880d681SAndroid Build Coastguard Worker if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
1195*9880d681SAndroid Build Coastguard Worker NewMIs[0]->eraseFromParent();
1196*9880d681SAndroid Build Coastguard Worker NewMIs[1]->eraseFromParent();
1197*9880d681SAndroid Build Coastguard Worker return nullptr;
1198*9880d681SAndroid Build Coastguard Worker }
1199*9880d681SAndroid Build Coastguard Worker
1200*9880d681SAndroid Build Coastguard Worker // Update register pressure for the unfolded instruction.
1201*9880d681SAndroid Build Coastguard Worker UpdateRegPressure(NewMIs[1]);
1202*9880d681SAndroid Build Coastguard Worker
1203*9880d681SAndroid Build Coastguard Worker // Otherwise we successfully unfolded a load that we can hoist.
1204*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
1205*9880d681SAndroid Build Coastguard Worker return NewMIs[0];
1206*9880d681SAndroid Build Coastguard Worker }
1207*9880d681SAndroid Build Coastguard Worker
1208*9880d681SAndroid Build Coastguard Worker /// Initialize the CSE map with instructions that are in the current loop
1209*9880d681SAndroid Build Coastguard Worker /// preheader that may become duplicates of instructions that are hoisted
1210*9880d681SAndroid Build Coastguard Worker /// out of the loop.
InitCSEMap(MachineBasicBlock * BB)1211*9880d681SAndroid Build Coastguard Worker void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
1212*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : *BB)
1213*9880d681SAndroid Build Coastguard Worker CSEMap[MI.getOpcode()].push_back(&MI);
1214*9880d681SAndroid Build Coastguard Worker }
1215*9880d681SAndroid Build Coastguard Worker
1216*9880d681SAndroid Build Coastguard Worker /// Find an instruction amount PrevMIs that is a duplicate of MI.
1217*9880d681SAndroid Build Coastguard Worker /// Return this instruction if it's found.
1218*9880d681SAndroid Build Coastguard Worker const MachineInstr*
LookForDuplicate(const MachineInstr * MI,std::vector<const MachineInstr * > & PrevMIs)1219*9880d681SAndroid Build Coastguard Worker MachineLICM::LookForDuplicate(const MachineInstr *MI,
1220*9880d681SAndroid Build Coastguard Worker std::vector<const MachineInstr*> &PrevMIs) {
1221*9880d681SAndroid Build Coastguard Worker for (const MachineInstr *PrevMI : PrevMIs)
1222*9880d681SAndroid Build Coastguard Worker if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
1223*9880d681SAndroid Build Coastguard Worker return PrevMI;
1224*9880d681SAndroid Build Coastguard Worker
1225*9880d681SAndroid Build Coastguard Worker return nullptr;
1226*9880d681SAndroid Build Coastguard Worker }
1227*9880d681SAndroid Build Coastguard Worker
1228*9880d681SAndroid Build Coastguard Worker /// Given a LICM'ed instruction, look for an instruction on the preheader that
1229*9880d681SAndroid Build Coastguard Worker /// computes the same value. If it's found, do a RAU on with the definition of
1230*9880d681SAndroid Build Coastguard Worker /// the existing instruction rather than hoisting the instruction to the
1231*9880d681SAndroid Build Coastguard Worker /// preheader.
EliminateCSE(MachineInstr * MI,DenseMap<unsigned,std::vector<const MachineInstr * >>::iterator & CI)1232*9880d681SAndroid Build Coastguard Worker bool MachineLICM::EliminateCSE(MachineInstr *MI,
1233*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
1234*9880d681SAndroid Build Coastguard Worker // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1235*9880d681SAndroid Build Coastguard Worker // the undef property onto uses.
1236*9880d681SAndroid Build Coastguard Worker if (CI == CSEMap.end() || MI->isImplicitDef())
1237*9880d681SAndroid Build Coastguard Worker return false;
1238*9880d681SAndroid Build Coastguard Worker
1239*9880d681SAndroid Build Coastguard Worker if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1240*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1241*9880d681SAndroid Build Coastguard Worker
1242*9880d681SAndroid Build Coastguard Worker // Replace virtual registers defined by MI by their counterparts defined
1243*9880d681SAndroid Build Coastguard Worker // by Dup.
1244*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 2> Defs;
1245*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1246*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(i);
1247*9880d681SAndroid Build Coastguard Worker
1248*9880d681SAndroid Build Coastguard Worker // Physical registers may not differ here.
1249*9880d681SAndroid Build Coastguard Worker assert((!MO.isReg() || MO.getReg() == 0 ||
1250*9880d681SAndroid Build Coastguard Worker !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1251*9880d681SAndroid Build Coastguard Worker MO.getReg() == Dup->getOperand(i).getReg()) &&
1252*9880d681SAndroid Build Coastguard Worker "Instructions with different phys regs are not identical!");
1253*9880d681SAndroid Build Coastguard Worker
1254*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isDef() &&
1255*9880d681SAndroid Build Coastguard Worker !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1256*9880d681SAndroid Build Coastguard Worker Defs.push_back(i);
1257*9880d681SAndroid Build Coastguard Worker }
1258*9880d681SAndroid Build Coastguard Worker
1259*9880d681SAndroid Build Coastguard Worker SmallVector<const TargetRegisterClass*, 2> OrigRCs;
1260*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1261*9880d681SAndroid Build Coastguard Worker unsigned Idx = Defs[i];
1262*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI->getOperand(Idx).getReg();
1263*9880d681SAndroid Build Coastguard Worker unsigned DupReg = Dup->getOperand(Idx).getReg();
1264*9880d681SAndroid Build Coastguard Worker OrigRCs.push_back(MRI->getRegClass(DupReg));
1265*9880d681SAndroid Build Coastguard Worker
1266*9880d681SAndroid Build Coastguard Worker if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1267*9880d681SAndroid Build Coastguard Worker // Restore old RCs if more than one defs.
1268*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0; j != i; ++j)
1269*9880d681SAndroid Build Coastguard Worker MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1270*9880d681SAndroid Build Coastguard Worker return false;
1271*9880d681SAndroid Build Coastguard Worker }
1272*9880d681SAndroid Build Coastguard Worker }
1273*9880d681SAndroid Build Coastguard Worker
1274*9880d681SAndroid Build Coastguard Worker for (unsigned Idx : Defs) {
1275*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI->getOperand(Idx).getReg();
1276*9880d681SAndroid Build Coastguard Worker unsigned DupReg = Dup->getOperand(Idx).getReg();
1277*9880d681SAndroid Build Coastguard Worker MRI->replaceRegWith(Reg, DupReg);
1278*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(DupReg);
1279*9880d681SAndroid Build Coastguard Worker }
1280*9880d681SAndroid Build Coastguard Worker
1281*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
1282*9880d681SAndroid Build Coastguard Worker ++NumCSEed;
1283*9880d681SAndroid Build Coastguard Worker return true;
1284*9880d681SAndroid Build Coastguard Worker }
1285*9880d681SAndroid Build Coastguard Worker return false;
1286*9880d681SAndroid Build Coastguard Worker }
1287*9880d681SAndroid Build Coastguard Worker
1288*9880d681SAndroid Build Coastguard Worker /// Return true if the given instruction will be CSE'd if it's hoisted out of
1289*9880d681SAndroid Build Coastguard Worker /// the loop.
MayCSE(MachineInstr * MI)1290*9880d681SAndroid Build Coastguard Worker bool MachineLICM::MayCSE(MachineInstr *MI) {
1291*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI->getOpcode();
1292*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1293*9880d681SAndroid Build Coastguard Worker CI = CSEMap.find(Opcode);
1294*9880d681SAndroid Build Coastguard Worker // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1295*9880d681SAndroid Build Coastguard Worker // the undef property onto uses.
1296*9880d681SAndroid Build Coastguard Worker if (CI == CSEMap.end() || MI->isImplicitDef())
1297*9880d681SAndroid Build Coastguard Worker return false;
1298*9880d681SAndroid Build Coastguard Worker
1299*9880d681SAndroid Build Coastguard Worker return LookForDuplicate(MI, CI->second) != nullptr;
1300*9880d681SAndroid Build Coastguard Worker }
1301*9880d681SAndroid Build Coastguard Worker
1302*9880d681SAndroid Build Coastguard Worker /// When an instruction is found to use only loop invariant operands
1303*9880d681SAndroid Build Coastguard Worker /// that are safe to hoist, this instruction is called to do the dirty work.
1304*9880d681SAndroid Build Coastguard Worker /// It returns true if the instruction is hoisted.
Hoist(MachineInstr * MI,MachineBasicBlock * Preheader)1305*9880d681SAndroid Build Coastguard Worker bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
1306*9880d681SAndroid Build Coastguard Worker // First check whether we should hoist this instruction.
1307*9880d681SAndroid Build Coastguard Worker if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
1308*9880d681SAndroid Build Coastguard Worker // If not, try unfolding a hoistable load.
1309*9880d681SAndroid Build Coastguard Worker MI = ExtractHoistableLoad(MI);
1310*9880d681SAndroid Build Coastguard Worker if (!MI) return false;
1311*9880d681SAndroid Build Coastguard Worker }
1312*9880d681SAndroid Build Coastguard Worker
1313*9880d681SAndroid Build Coastguard Worker // Now move the instructions to the predecessor, inserting it before any
1314*9880d681SAndroid Build Coastguard Worker // terminator instructions.
1315*9880d681SAndroid Build Coastguard Worker DEBUG({
1316*9880d681SAndroid Build Coastguard Worker dbgs() << "Hoisting " << *MI;
1317*9880d681SAndroid Build Coastguard Worker if (MI->getParent()->getBasicBlock())
1318*9880d681SAndroid Build Coastguard Worker dbgs() << " from BB#" << MI->getParent()->getNumber();
1319*9880d681SAndroid Build Coastguard Worker if (Preheader->getBasicBlock())
1320*9880d681SAndroid Build Coastguard Worker dbgs() << " to BB#" << Preheader->getNumber();
1321*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
1322*9880d681SAndroid Build Coastguard Worker });
1323*9880d681SAndroid Build Coastguard Worker
1324*9880d681SAndroid Build Coastguard Worker // If this is the first instruction being hoisted to the preheader,
1325*9880d681SAndroid Build Coastguard Worker // initialize the CSE map with potential common expressions.
1326*9880d681SAndroid Build Coastguard Worker if (FirstInLoop) {
1327*9880d681SAndroid Build Coastguard Worker InitCSEMap(Preheader);
1328*9880d681SAndroid Build Coastguard Worker FirstInLoop = false;
1329*9880d681SAndroid Build Coastguard Worker }
1330*9880d681SAndroid Build Coastguard Worker
1331*9880d681SAndroid Build Coastguard Worker // Look for opportunity to CSE the hoisted instruction.
1332*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI->getOpcode();
1333*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1334*9880d681SAndroid Build Coastguard Worker CI = CSEMap.find(Opcode);
1335*9880d681SAndroid Build Coastguard Worker if (!EliminateCSE(MI, CI)) {
1336*9880d681SAndroid Build Coastguard Worker // Otherwise, splice the instruction to the preheader.
1337*9880d681SAndroid Build Coastguard Worker Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1338*9880d681SAndroid Build Coastguard Worker
1339*9880d681SAndroid Build Coastguard Worker // Update register pressure for BBs from header to this block.
1340*9880d681SAndroid Build Coastguard Worker UpdateBackTraceRegPressure(MI);
1341*9880d681SAndroid Build Coastguard Worker
1342*9880d681SAndroid Build Coastguard Worker // Clear the kill flags of any register this instruction defines,
1343*9880d681SAndroid Build Coastguard Worker // since they may need to be live throughout the entire loop
1344*9880d681SAndroid Build Coastguard Worker // rather than just live for part of it.
1345*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MI->operands())
1346*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isDef() && !MO.isDead())
1347*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(MO.getReg());
1348*9880d681SAndroid Build Coastguard Worker
1349*9880d681SAndroid Build Coastguard Worker // Add to the CSE map.
1350*9880d681SAndroid Build Coastguard Worker if (CI != CSEMap.end())
1351*9880d681SAndroid Build Coastguard Worker CI->second.push_back(MI);
1352*9880d681SAndroid Build Coastguard Worker else
1353*9880d681SAndroid Build Coastguard Worker CSEMap[Opcode].push_back(MI);
1354*9880d681SAndroid Build Coastguard Worker }
1355*9880d681SAndroid Build Coastguard Worker
1356*9880d681SAndroid Build Coastguard Worker ++NumHoisted;
1357*9880d681SAndroid Build Coastguard Worker Changed = true;
1358*9880d681SAndroid Build Coastguard Worker
1359*9880d681SAndroid Build Coastguard Worker return true;
1360*9880d681SAndroid Build Coastguard Worker }
1361*9880d681SAndroid Build Coastguard Worker
1362*9880d681SAndroid Build Coastguard Worker /// Get the preheader for the current loop, splitting a critical edge if needed.
getCurPreheader()1363*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MachineLICM::getCurPreheader() {
1364*9880d681SAndroid Build Coastguard Worker // Determine the block to which to hoist instructions. If we can't find a
1365*9880d681SAndroid Build Coastguard Worker // suitable loop predecessor, we can't do any hoisting.
1366*9880d681SAndroid Build Coastguard Worker
1367*9880d681SAndroid Build Coastguard Worker // If we've tried to get a preheader and failed, don't try again.
1368*9880d681SAndroid Build Coastguard Worker if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
1369*9880d681SAndroid Build Coastguard Worker return nullptr;
1370*9880d681SAndroid Build Coastguard Worker
1371*9880d681SAndroid Build Coastguard Worker if (!CurPreheader) {
1372*9880d681SAndroid Build Coastguard Worker CurPreheader = CurLoop->getLoopPreheader();
1373*9880d681SAndroid Build Coastguard Worker if (!CurPreheader) {
1374*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1375*9880d681SAndroid Build Coastguard Worker if (!Pred) {
1376*9880d681SAndroid Build Coastguard Worker CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1377*9880d681SAndroid Build Coastguard Worker return nullptr;
1378*9880d681SAndroid Build Coastguard Worker }
1379*9880d681SAndroid Build Coastguard Worker
1380*9880d681SAndroid Build Coastguard Worker CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this);
1381*9880d681SAndroid Build Coastguard Worker if (!CurPreheader) {
1382*9880d681SAndroid Build Coastguard Worker CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1383*9880d681SAndroid Build Coastguard Worker return nullptr;
1384*9880d681SAndroid Build Coastguard Worker }
1385*9880d681SAndroid Build Coastguard Worker }
1386*9880d681SAndroid Build Coastguard Worker }
1387*9880d681SAndroid Build Coastguard Worker return CurPreheader;
1388*9880d681SAndroid Build Coastguard Worker }
1389