1*9880d681SAndroid Build Coastguard Worker //===- LoopDeletion.cpp - Dead Loop Deletion 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 file implements the Dead Loop Deletion Pass. This pass is responsible
11*9880d681SAndroid Build Coastguard Worker // for eliminating loops with non-infinite computable trip counts that have no
12*9880d681SAndroid Build Coastguard Worker // side effects or volatile instructions, and do not contribute to the
13*9880d681SAndroid Build Coastguard Worker // computation of the function's return value.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar/LoopDeletion.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPass.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPassManager.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/LoopUtils.h"
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "loop-delete"
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker STATISTIC(NumDeleted, "Number of loops deleted");
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker /// isLoopDead - Determined if a loop is dead. This assumes that we've already
33*9880d681SAndroid Build Coastguard Worker /// checked for unique exit and exiting blocks, and that the code is in LCSSA
34*9880d681SAndroid Build Coastguard Worker /// form.
isLoopDead(Loop * L,ScalarEvolution & SE,SmallVectorImpl<BasicBlock * > & exitingBlocks,SmallVectorImpl<BasicBlock * > & exitBlocks,bool & Changed,BasicBlock * Preheader)35*9880d681SAndroid Build Coastguard Worker bool LoopDeletionPass::isLoopDead(Loop *L, ScalarEvolution &SE,
36*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<BasicBlock *> &exitingBlocks,
37*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<BasicBlock *> &exitBlocks,
38*9880d681SAndroid Build Coastguard Worker bool &Changed, BasicBlock *Preheader) {
39*9880d681SAndroid Build Coastguard Worker BasicBlock *exitBlock = exitBlocks[0];
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker // Make sure that all PHI entries coming from the loop are loop invariant.
42*9880d681SAndroid Build Coastguard Worker // Because the code is in LCSSA form, any values used outside of the loop
43*9880d681SAndroid Build Coastguard Worker // must pass through a PHI in the exit block, meaning that this check is
44*9880d681SAndroid Build Coastguard Worker // sufficient to guarantee that no loop-variant values are used outside
45*9880d681SAndroid Build Coastguard Worker // of the loop.
46*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator BI = exitBlock->begin();
47*9880d681SAndroid Build Coastguard Worker bool AllEntriesInvariant = true;
48*9880d681SAndroid Build Coastguard Worker bool AllOutgoingValuesSame = true;
49*9880d681SAndroid Build Coastguard Worker while (PHINode *P = dyn_cast<PHINode>(BI)) {
50*9880d681SAndroid Build Coastguard Worker Value *incoming = P->getIncomingValueForBlock(exitingBlocks[0]);
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker // Make sure all exiting blocks produce the same incoming value for the exit
53*9880d681SAndroid Build Coastguard Worker // block. If there are different incoming values for different exiting
54*9880d681SAndroid Build Coastguard Worker // blocks, then it is impossible to statically determine which value should
55*9880d681SAndroid Build Coastguard Worker // be used.
56*9880d681SAndroid Build Coastguard Worker AllOutgoingValuesSame =
57*9880d681SAndroid Build Coastguard Worker all_of(makeArrayRef(exitingBlocks).slice(1), [&](BasicBlock *BB) {
58*9880d681SAndroid Build Coastguard Worker return incoming == P->getIncomingValueForBlock(BB);
59*9880d681SAndroid Build Coastguard Worker });
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker if (!AllOutgoingValuesSame)
62*9880d681SAndroid Build Coastguard Worker break;
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker if (Instruction *I = dyn_cast<Instruction>(incoming))
65*9880d681SAndroid Build Coastguard Worker if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) {
66*9880d681SAndroid Build Coastguard Worker AllEntriesInvariant = false;
67*9880d681SAndroid Build Coastguard Worker break;
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker ++BI;
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker if (Changed)
74*9880d681SAndroid Build Coastguard Worker SE.forgetLoopDispositions(L);
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker if (!AllEntriesInvariant || !AllOutgoingValuesSame)
77*9880d681SAndroid Build Coastguard Worker return false;
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker // Make sure that no instructions in the block have potential side-effects.
80*9880d681SAndroid Build Coastguard Worker // This includes instructions that could write to memory, and loads that are
81*9880d681SAndroid Build Coastguard Worker // marked volatile. This could be made more aggressive by using aliasing
82*9880d681SAndroid Build Coastguard Worker // information to identify readonly and readnone calls.
83*9880d681SAndroid Build Coastguard Worker for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
84*9880d681SAndroid Build Coastguard Worker LI != LE; ++LI) {
85*9880d681SAndroid Build Coastguard Worker for (Instruction &I : **LI) {
86*9880d681SAndroid Build Coastguard Worker if (I.mayHaveSideEffects())
87*9880d681SAndroid Build Coastguard Worker return false;
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker }
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker return true;
92*9880d681SAndroid Build Coastguard Worker }
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker /// Remove dead loops, by which we mean loops that do not impact the observable
95*9880d681SAndroid Build Coastguard Worker /// behavior of the program other than finite running time. Note we do ensure
96*9880d681SAndroid Build Coastguard Worker /// that this never remove a loop that might be infinite, as doing so could
97*9880d681SAndroid Build Coastguard Worker /// change the halting/non-halting nature of a program. NOTE: This entire
98*9880d681SAndroid Build Coastguard Worker /// process relies pretty heavily on LoopSimplify and LCSSA in order to make
99*9880d681SAndroid Build Coastguard Worker /// various safety checks work.
runImpl(Loop * L,DominatorTree & DT,ScalarEvolution & SE,LoopInfo & loopInfo)100*9880d681SAndroid Build Coastguard Worker bool LoopDeletionPass::runImpl(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
101*9880d681SAndroid Build Coastguard Worker LoopInfo &loopInfo) {
102*9880d681SAndroid Build Coastguard Worker assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker // We can only remove the loop if there is a preheader that we can
105*9880d681SAndroid Build Coastguard Worker // branch from after removing it.
106*9880d681SAndroid Build Coastguard Worker BasicBlock *preheader = L->getLoopPreheader();
107*9880d681SAndroid Build Coastguard Worker if (!preheader)
108*9880d681SAndroid Build Coastguard Worker return false;
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker // If LoopSimplify form is not available, stay out of trouble.
111*9880d681SAndroid Build Coastguard Worker if (!L->hasDedicatedExits())
112*9880d681SAndroid Build Coastguard Worker return false;
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker // We can't remove loops that contain subloops. If the subloops were dead,
115*9880d681SAndroid Build Coastguard Worker // they would already have been removed in earlier executions of this pass.
116*9880d681SAndroid Build Coastguard Worker if (L->begin() != L->end())
117*9880d681SAndroid Build Coastguard Worker return false;
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock *, 4> exitingBlocks;
120*9880d681SAndroid Build Coastguard Worker L->getExitingBlocks(exitingBlocks);
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock *, 4> exitBlocks;
123*9880d681SAndroid Build Coastguard Worker L->getUniqueExitBlocks(exitBlocks);
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker // We require that the loop only have a single exit block. Otherwise, we'd
126*9880d681SAndroid Build Coastguard Worker // be in the situation of needing to be able to solve statically which exit
127*9880d681SAndroid Build Coastguard Worker // block will be branched to, or trying to preserve the branching logic in
128*9880d681SAndroid Build Coastguard Worker // a loop invariant manner.
129*9880d681SAndroid Build Coastguard Worker if (exitBlocks.size() != 1)
130*9880d681SAndroid Build Coastguard Worker return false;
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker // Finally, we have to check that the loop really is dead.
133*9880d681SAndroid Build Coastguard Worker bool Changed = false;
134*9880d681SAndroid Build Coastguard Worker if (!isLoopDead(L, SE, exitingBlocks, exitBlocks, Changed, preheader))
135*9880d681SAndroid Build Coastguard Worker return Changed;
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker // Don't remove loops for which we can't solve the trip count.
138*9880d681SAndroid Build Coastguard Worker // They could be infinite, in which case we'd be changing program behavior.
139*9880d681SAndroid Build Coastguard Worker const SCEV *S = SE.getMaxBackedgeTakenCount(L);
140*9880d681SAndroid Build Coastguard Worker if (isa<SCEVCouldNotCompute>(S))
141*9880d681SAndroid Build Coastguard Worker return Changed;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker // Now that we know the removal is safe, remove the loop by changing the
144*9880d681SAndroid Build Coastguard Worker // branch from the preheader to go to the single exit block.
145*9880d681SAndroid Build Coastguard Worker BasicBlock *exitBlock = exitBlocks[0];
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker // Because we're deleting a large chunk of code at once, the sequence in which
148*9880d681SAndroid Build Coastguard Worker // we remove things is very important to avoid invalidation issues. Don't
149*9880d681SAndroid Build Coastguard Worker // mess with this unless you have good reason and know what you're doing.
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker // Tell ScalarEvolution that the loop is deleted. Do this before
152*9880d681SAndroid Build Coastguard Worker // deleting the loop so that ScalarEvolution can look at the loop
153*9880d681SAndroid Build Coastguard Worker // to determine what it needs to clean up.
154*9880d681SAndroid Build Coastguard Worker SE.forgetLoop(L);
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker // Connect the preheader directly to the exit block.
157*9880d681SAndroid Build Coastguard Worker TerminatorInst *TI = preheader->getTerminator();
158*9880d681SAndroid Build Coastguard Worker TI->replaceUsesOfWith(L->getHeader(), exitBlock);
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker // Rewrite phis in the exit block to get their inputs from
161*9880d681SAndroid Build Coastguard Worker // the preheader instead of the exiting block.
162*9880d681SAndroid Build Coastguard Worker BasicBlock *exitingBlock = exitingBlocks[0];
163*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator BI = exitBlock->begin();
164*9880d681SAndroid Build Coastguard Worker while (PHINode *P = dyn_cast<PHINode>(BI)) {
165*9880d681SAndroid Build Coastguard Worker int j = P->getBasicBlockIndex(exitingBlock);
166*9880d681SAndroid Build Coastguard Worker assert(j >= 0 && "Can't find exiting block in exit block's phi node!");
167*9880d681SAndroid Build Coastguard Worker P->setIncomingBlock(j, preheader);
168*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i < exitingBlocks.size(); ++i)
169*9880d681SAndroid Build Coastguard Worker P->removeIncomingValue(exitingBlocks[i]);
170*9880d681SAndroid Build Coastguard Worker ++BI;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker // Update the dominator tree and remove the instructions and blocks that will
174*9880d681SAndroid Build Coastguard Worker // be deleted from the reference counting scheme.
175*9880d681SAndroid Build Coastguard Worker SmallVector<DomTreeNode*, 8> ChildNodes;
176*9880d681SAndroid Build Coastguard Worker for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
177*9880d681SAndroid Build Coastguard Worker LI != LE; ++LI) {
178*9880d681SAndroid Build Coastguard Worker // Move all of the block's children to be children of the preheader, which
179*9880d681SAndroid Build Coastguard Worker // allows us to remove the domtree entry for the block.
180*9880d681SAndroid Build Coastguard Worker ChildNodes.insert(ChildNodes.begin(), DT[*LI]->begin(), DT[*LI]->end());
181*9880d681SAndroid Build Coastguard Worker for (DomTreeNode *ChildNode : ChildNodes) {
182*9880d681SAndroid Build Coastguard Worker DT.changeImmediateDominator(ChildNode, DT[preheader]);
183*9880d681SAndroid Build Coastguard Worker }
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker ChildNodes.clear();
186*9880d681SAndroid Build Coastguard Worker DT.eraseNode(*LI);
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker // Remove the block from the reference counting scheme, so that we can
189*9880d681SAndroid Build Coastguard Worker // delete it freely later.
190*9880d681SAndroid Build Coastguard Worker (*LI)->dropAllReferences();
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker // Erase the instructions and the blocks without having to worry
194*9880d681SAndroid Build Coastguard Worker // about ordering because we already dropped the references.
195*9880d681SAndroid Build Coastguard Worker // NOTE: This iteration is safe because erasing the block does not remove its
196*9880d681SAndroid Build Coastguard Worker // entry from the loop's block list. We do that in the next section.
197*9880d681SAndroid Build Coastguard Worker for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
198*9880d681SAndroid Build Coastguard Worker LI != LE; ++LI)
199*9880d681SAndroid Build Coastguard Worker (*LI)->eraseFromParent();
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker // Finally, the blocks from loopinfo. This has to happen late because
202*9880d681SAndroid Build Coastguard Worker // otherwise our loop iterators won't work.
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker SmallPtrSet<BasicBlock *, 8> blocks;
205*9880d681SAndroid Build Coastguard Worker blocks.insert(L->block_begin(), L->block_end());
206*9880d681SAndroid Build Coastguard Worker for (BasicBlock *BB : blocks)
207*9880d681SAndroid Build Coastguard Worker loopInfo.removeBlock(BB);
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker // The last step is to update LoopInfo now that we've eliminated this loop.
210*9880d681SAndroid Build Coastguard Worker loopInfo.markAsRemoved(L);
211*9880d681SAndroid Build Coastguard Worker Changed = true;
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker ++NumDeleted;
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker return Changed;
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
run(Loop & L,AnalysisManager<Loop> & AM)218*9880d681SAndroid Build Coastguard Worker PreservedAnalyses LoopDeletionPass::run(Loop &L, AnalysisManager<Loop> &AM) {
219*9880d681SAndroid Build Coastguard Worker auto &FAM = AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager();
220*9880d681SAndroid Build Coastguard Worker Function *F = L.getHeader()->getParent();
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker auto &DT = *FAM.getCachedResult<DominatorTreeAnalysis>(*F);
223*9880d681SAndroid Build Coastguard Worker auto &SE = *FAM.getCachedResult<ScalarEvolutionAnalysis>(*F);
224*9880d681SAndroid Build Coastguard Worker auto &LI = *FAM.getCachedResult<LoopAnalysis>(*F);
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker bool Changed = runImpl(&L, DT, SE, LI);
227*9880d681SAndroid Build Coastguard Worker if (!Changed)
228*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker return getLoopPassPreservedAnalyses();
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker namespace {
234*9880d681SAndroid Build Coastguard Worker class LoopDeletionLegacyPass : public LoopPass {
235*9880d681SAndroid Build Coastguard Worker public:
236*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid
LoopDeletionLegacyPass()237*9880d681SAndroid Build Coastguard Worker LoopDeletionLegacyPass() : LoopPass(ID) {
238*9880d681SAndroid Build Coastguard Worker initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry());
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker // Possibly eliminate loop L if it is dead.
242*9880d681SAndroid Build Coastguard Worker bool runOnLoop(Loop *L, LPPassManager &) override;
243*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const244*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
245*9880d681SAndroid Build Coastguard Worker getLoopAnalysisUsage(AU);
246*9880d681SAndroid Build Coastguard Worker }
247*9880d681SAndroid Build Coastguard Worker };
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker
250*9880d681SAndroid Build Coastguard Worker char LoopDeletionLegacyPass::ID = 0;
251*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion",
252*9880d681SAndroid Build Coastguard Worker "Delete dead loops", false, false)
INITIALIZE_PASS_DEPENDENCY(LoopPass)253*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(LoopPass)
254*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion",
255*9880d681SAndroid Build Coastguard Worker "Delete dead loops", false, false)
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); }
258*9880d681SAndroid Build Coastguard Worker
runOnLoop(Loop * L,LPPassManager &)259*9880d681SAndroid Build Coastguard Worker bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) {
260*9880d681SAndroid Build Coastguard Worker if (skipLoop(L))
261*9880d681SAndroid Build Coastguard Worker return false;
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
264*9880d681SAndroid Build Coastguard Worker ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
265*9880d681SAndroid Build Coastguard Worker LoopInfo &loopInfo = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker LoopDeletionPass Impl;
268*9880d681SAndroid Build Coastguard Worker return Impl.runImpl(L, DT, SE, loopInfo);
269*9880d681SAndroid Build Coastguard Worker }
270