blob: 6d1d344a9296537888c7028d7e2ecff2c15033fd [file] [log] [blame]
Owen Anderson0396cd32008-04-29 20:06:54 +00001//===- LoopDeletion.cpp - Dead Loop Deletion Pass ---------------===//
Owen Anderson0ff77082008-04-29 00:38:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksena8a118b2008-05-08 17:46:35 +000010// This file implements the Dead Loop Deletion Pass. This pass is responsible
11// for eliminating loops with non-infinite computable trip counts that have no
12// side effects or volatile instructions, and do not contribute to the
13// computation of the function's return value.
Owen Anderson0ff77082008-04-29 00:38:34 +000014//
15//===----------------------------------------------------------------------===//
16
Owen Anderson0396cd32008-04-29 20:06:54 +000017#define DEBUG_TYPE "loop-delete"
Owen Anderson0ff77082008-04-29 00:38:34 +000018#include "llvm/Transforms/Scalar.h"
Owen Anderson0ff77082008-04-29 00:38:34 +000019#include "llvm/Analysis/LoopPass.h"
Cameron Zwarich30127872011-01-18 04:11:31 +000020#include "llvm/Analysis/Dominators.h"
Owen Anderson0db198d2008-05-16 04:32:45 +000021#include "llvm/Analysis/ScalarEvolution.h"
Owen Anderson0ff77082008-04-29 00:38:34 +000022#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/SmallVector.h"
Owen Anderson0ff77082008-04-29 00:38:34 +000024using namespace llvm;
25
26STATISTIC(NumDeleted, "Number of loops deleted");
27
28namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000029 class LoopDeletion : public LoopPass {
Owen Anderson0ff77082008-04-29 00:38:34 +000030 public:
31 static char ID; // Pass ID, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000032 LoopDeletion() : LoopPass(ID) {
33 initializeLoopDeletionPass(*PassRegistry::getPassRegistry());
34 }
Owen Anderson0ff77082008-04-29 00:38:34 +000035
36 // Possibly eliminate loop L if it is dead.
37 bool runOnLoop(Loop* L, LPPassManager& LPM);
38
Owen Anderson9862f312008-04-29 20:59:33 +000039 bool IsLoopDead(Loop* L, SmallVector<BasicBlock*, 4>& exitingBlocks,
Dan Gohmanbdc017e2009-07-15 01:25:43 +000040 SmallVector<BasicBlock*, 4>& exitBlocks,
41 bool &Changed, BasicBlock *Preheader);
42
Owen Anderson0ff77082008-04-29 00:38:34 +000043 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
44 AU.addRequired<DominatorTree>();
45 AU.addRequired<LoopInfo>();
Dan Gohman052f0002010-07-26 18:11:16 +000046 AU.addRequired<ScalarEvolution>();
Owen Anderson0ff77082008-04-29 00:38:34 +000047 AU.addRequiredID(LoopSimplifyID);
48 AU.addRequiredID(LCSSAID);
49
Owen Anderson0db198d2008-05-16 04:32:45 +000050 AU.addPreserved<ScalarEvolution>();
Owen Anderson0ff77082008-04-29 00:38:34 +000051 AU.addPreserved<DominatorTree>();
52 AU.addPreserved<LoopInfo>();
53 AU.addPreservedID(LoopSimplifyID);
54 AU.addPreservedID(LCSSAID);
55 }
56 };
Owen Anderson0ff77082008-04-29 00:38:34 +000057}
Dan Gohman844731a2008-05-13 00:00:25 +000058
59char LoopDeletion::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000060INITIALIZE_PASS_BEGIN(LoopDeletion, "loop-deletion",
61 "Delete dead loops", false, false)
62INITIALIZE_PASS_DEPENDENCY(DominatorTree)
63INITIALIZE_PASS_DEPENDENCY(LoopInfo)
64INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
65INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
66INITIALIZE_PASS_DEPENDENCY(LCSSA)
Owen Anderson2ab36d32010-10-12 19:48:12 +000067INITIALIZE_PASS_END(LoopDeletion, "loop-deletion",
Owen Andersonce665bd2010-10-07 22:25:06 +000068 "Delete dead loops", false, false)
Owen Anderson0ff77082008-04-29 00:38:34 +000069
Daniel Dunbar394f0442008-10-22 23:32:42 +000070Pass* llvm::createLoopDeletionPass() {
Owen Anderson0396cd32008-04-29 20:06:54 +000071 return new LoopDeletion();
Owen Anderson0ff77082008-04-29 00:38:34 +000072}
73
Owen Anderson9862f312008-04-29 20:59:33 +000074/// IsLoopDead - Determined if a loop is dead. This assumes that we've already
75/// checked for unique exit and exiting blocks, and that the code is in LCSSA
76/// form.
77bool LoopDeletion::IsLoopDead(Loop* L,
78 SmallVector<BasicBlock*, 4>& exitingBlocks,
Dan Gohmanbdc017e2009-07-15 01:25:43 +000079 SmallVector<BasicBlock*, 4>& exitBlocks,
80 bool &Changed, BasicBlock *Preheader) {
Owen Anderson0ff77082008-04-29 00:38:34 +000081 BasicBlock* exitingBlock = exitingBlocks[0];
Owen Anderson0ff77082008-04-29 00:38:34 +000082 BasicBlock* exitBlock = exitBlocks[0];
83
84 // Make sure that all PHI entries coming from the loop are loop invariant.
Owen Anderson9862f312008-04-29 20:59:33 +000085 // Because the code is in LCSSA form, any values used outside of the loop
86 // must pass through a PHI in the exit block, meaning that this check is
87 // sufficient to guarantee that no loop-variant values are used outside
88 // of the loop.
Owen Anderson0ff77082008-04-29 00:38:34 +000089 BasicBlock::iterator BI = exitBlock->begin();
90 while (PHINode* P = dyn_cast<PHINode>(BI)) {
91 Value* incoming = P->getIncomingValueForBlock(exitingBlock);
92 if (Instruction* I = dyn_cast<Instruction>(incoming))
Dan Gohmanbdc017e2009-07-15 01:25:43 +000093 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator()))
Owen Anderson0ff77082008-04-29 00:38:34 +000094 return false;
95
Dan Gohmanfe601042010-06-22 15:08:57 +000096 ++BI;
Owen Anderson0ff77082008-04-29 00:38:34 +000097 }
98
99 // Make sure that no instructions in the block have potential side-effects.
Owen Anderson9862f312008-04-29 20:59:33 +0000100 // This includes instructions that could write to memory, and loads that are
101 // marked volatile. This could be made more aggressive by using aliasing
102 // information to identify readonly and readnone calls.
Owen Anderson0ff77082008-04-29 00:38:34 +0000103 for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
104 LI != LE; ++LI) {
105 for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end();
106 BI != BE; ++BI) {
Duncan Sands7af1c782009-05-06 06:49:50 +0000107 if (BI->mayHaveSideEffects())
Owen Anderson0ff77082008-04-29 00:38:34 +0000108 return false;
109 }
110 }
111
112 return true;
113}
114
Owen Anderson5f8b3442008-04-29 06:34:55 +0000115/// runOnLoop - Remove dead loops, by which we mean loops that do not impact the
116/// observable behavior of the program other than finite running time. Note
117/// we do ensure that this never remove a loop that might be infinite, as doing
118/// so could change the halting/non-halting nature of a program.
Owen Anderson9862f312008-04-29 20:59:33 +0000119/// NOTE: This entire process relies pretty heavily on LoopSimplify and LCSSA
120/// in order to make various safety checks work.
Owen Anderson0396cd32008-04-29 20:06:54 +0000121bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
Owen Anderson0ff77082008-04-29 00:38:34 +0000122 // We can only remove the loop if there is a preheader that we can
123 // branch from after removing it.
124 BasicBlock* preheader = L->getLoopPreheader();
125 if (!preheader)
126 return false;
127
Dan Gohman32cc5f42009-11-05 21:47:04 +0000128 // If LoopSimplify form is not available, stay out of trouble.
129 if (!L->hasDedicatedExits())
130 return false;
131
Owen Anderson0ff77082008-04-29 00:38:34 +0000132 // We can't remove loops that contain subloops. If the subloops were dead,
133 // they would already have been removed in earlier executions of this pass.
134 if (L->begin() != L->end())
135 return false;
136
Owen Anderson0db198d2008-05-16 04:32:45 +0000137 SmallVector<BasicBlock*, 4> exitingBlocks;
138 L->getExitingBlocks(exitingBlocks);
139
140 SmallVector<BasicBlock*, 4> exitBlocks;
141 L->getUniqueExitBlocks(exitBlocks);
142
143 // We require that the loop only have a single exit block. Otherwise, we'd
144 // be in the situation of needing to be able to solve statically which exit
145 // block will be branched to, or trying to preserve the branching logic in
146 // a loop invariant manner.
147 if (exitBlocks.size() != 1)
Owen Anderson9862f312008-04-29 20:59:33 +0000148 return false;
149
Dan Gohman06d4033a2009-10-26 22:18:58 +0000150 // Loops with multiple exits are too complicated to handle correctly.
151 if (exitingBlocks.size() != 1)
Owen Anderson0ff77082008-04-29 00:38:34 +0000152 return false;
153
154 // Finally, we have to check that the loop really is dead.
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000155 bool Changed = false;
156 if (!IsLoopDead(L, exitingBlocks, exitBlocks, Changed, preheader))
157 return Changed;
Owen Anderson0ff77082008-04-29 00:38:34 +0000158
Owen Anderson0db198d2008-05-16 04:32:45 +0000159 // Don't remove loops for which we can't solve the trip count.
160 // They could be infinite, in which case we'd be changing program behavior.
161 ScalarEvolution& SE = getAnalysis<ScalarEvolution>();
Dan Gohman934af9c2009-10-23 17:10:01 +0000162 const SCEV *S = SE.getMaxBackedgeTakenCount(L);
Owen Anderson0db198d2008-05-16 04:32:45 +0000163 if (isa<SCEVCouldNotCompute>(S))
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000164 return Changed;
Owen Anderson0db198d2008-05-16 04:32:45 +0000165
Owen Anderson9862f312008-04-29 20:59:33 +0000166 // Now that we know the removal is safe, remove the loop by changing the
Owen Anderson8b23bb72008-05-06 20:55:16 +0000167 // branch from the preheader to go to the single exit block.
Owen Anderson0ff77082008-04-29 00:38:34 +0000168 BasicBlock* exitBlock = exitBlocks[0];
Owen Anderson8b23bb72008-05-06 20:55:16 +0000169 BasicBlock* exitingBlock = exitingBlocks[0];
Owen Anderson0ff77082008-04-29 00:38:34 +0000170
Owen Andersone54cfdb2008-04-29 00:45:15 +0000171 // Because we're deleting a large chunk of code at once, the sequence in which
172 // we remove things is very important to avoid invalidation issues. Don't
173 // mess with this unless you have good reason and know what you're doing.
Dan Gohman85ebb0f2009-07-08 19:14:29 +0000174
175 // Tell ScalarEvolution that the loop is deleted. Do this before
176 // deleting the loop so that ScalarEvolution can look at the loop
177 // to determine what it needs to clean up.
Dan Gohman4c7279a2009-10-31 15:04:55 +0000178 SE.forgetLoop(L);
Dan Gohman85ebb0f2009-07-08 19:14:29 +0000179
Owen Andersone54cfdb2008-04-29 00:45:15 +0000180 // Connect the preheader directly to the exit block.
Owen Anderson0ff77082008-04-29 00:38:34 +0000181 TerminatorInst* TI = preheader->getTerminator();
Owen Anderson9862f312008-04-29 20:59:33 +0000182 TI->replaceUsesOfWith(L->getHeader(), exitBlock);
183
Owen Andersone54cfdb2008-04-29 00:45:15 +0000184 // Rewrite phis in the exit block to get their inputs from
185 // the preheader instead of the exiting block.
Owen Anderson0ff77082008-04-29 00:38:34 +0000186 BasicBlock::iterator BI = exitBlock->begin();
187 while (PHINode* P = dyn_cast<PHINode>(BI)) {
Owen Anderson8b23bb72008-05-06 20:55:16 +0000188 P->replaceUsesOfWith(exitingBlock, preheader);
Dan Gohmanfe601042010-06-22 15:08:57 +0000189 ++BI;
Owen Anderson0ff77082008-04-29 00:38:34 +0000190 }
191
Owen Anderson9862f312008-04-29 20:59:33 +0000192 // Update the dominator tree and remove the instructions and blocks that will
193 // be deleted from the reference counting scheme.
Owen Anderson0ff77082008-04-29 00:38:34 +0000194 DominatorTree& DT = getAnalysis<DominatorTree>();
Devang Patel85bbd572011-01-12 19:12:45 +0000195 SmallVector<DomTreeNode*, 8> ChildNodes;
Owen Anderson0ff77082008-04-29 00:38:34 +0000196 for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
197 LI != LE; ++LI) {
Owen Andersone54cfdb2008-04-29 00:45:15 +0000198 // Move all of the block's children to be children of the preheader, which
199 // allows us to remove the domtree entry for the block.
Devang Patel85bbd572011-01-12 19:12:45 +0000200 ChildNodes.insert(ChildNodes.begin(), DT[*LI]->begin(), DT[*LI]->end());
201 for (SmallVector<DomTreeNode*, 8>::iterator DI = ChildNodes.begin(),
Dan Gohmand0a90b92009-02-24 01:21:53 +0000202 DE = ChildNodes.end(); DI != DE; ++DI) {
Owen Anderson0ff77082008-04-29 00:38:34 +0000203 DT.changeImmediateDominator(*DI, DT[preheader]);
Dan Gohmand0a90b92009-02-24 01:21:53 +0000204 }
Owen Anderson0ff77082008-04-29 00:38:34 +0000205
Owen Anderson9862f312008-04-29 20:59:33 +0000206 ChildNodes.clear();
Owen Anderson0ff77082008-04-29 00:38:34 +0000207 DT.eraseNode(*LI);
Dan Gohmand0a90b92009-02-24 01:21:53 +0000208
Owen Anderson0db198d2008-05-16 04:32:45 +0000209 // Remove the block from the reference counting scheme, so that we can
210 // delete it freely later.
Owen Anderson0ff77082008-04-29 00:38:34 +0000211 (*LI)->dropAllReferences();
212 }
213
Owen Andersone54cfdb2008-04-29 00:45:15 +0000214 // Erase the instructions and the blocks without having to worry
215 // about ordering because we already dropped the references.
Owen Anderson9862f312008-04-29 20:59:33 +0000216 // NOTE: This iteration is safe because erasing the block does not remove its
217 // entry from the loop's block list. We do that in the next section.
Owen Anderson0ff77082008-04-29 00:38:34 +0000218 for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
Owen Andersoncd5e6dd2008-05-29 08:15:48 +0000219 LI != LE; ++LI)
Owen Anderson0ff77082008-04-29 00:38:34 +0000220 (*LI)->eraseFromParent();
Dan Gohmane2abdd32009-02-23 17:10:29 +0000221
Owen Andersone54cfdb2008-04-29 00:45:15 +0000222 // Finally, the blocks from loopinfo. This has to happen late because
223 // otherwise our loop iterators won't work.
Owen Anderson0ff77082008-04-29 00:38:34 +0000224 LoopInfo& loopInfo = getAnalysis<LoopInfo>();
225 SmallPtrSet<BasicBlock*, 8> blocks;
226 blocks.insert(L->block_begin(), L->block_end());
227 for (SmallPtrSet<BasicBlock*,8>::iterator I = blocks.begin(),
228 E = blocks.end(); I != E; ++I)
229 loopInfo.removeBlock(*I);
230
Owen Andersone54cfdb2008-04-29 00:45:15 +0000231 // The last step is to inform the loop pass manager that we've
232 // eliminated this loop.
Owen Anderson0ff77082008-04-29 00:38:34 +0000233 LPM.deleteLoopFromQueue(L);
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000234 Changed = true;
Owen Anderson0ff77082008-04-29 00:38:34 +0000235
Dan Gohmanfe601042010-06-22 15:08:57 +0000236 ++NumDeleted;
Owen Anderson0ff77082008-04-29 00:38:34 +0000237
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000238 return Changed;
Owen Anderson0ff77082008-04-29 00:38:34 +0000239}