blob: ee03556f27412d98f3e1fafa9afcec7ec65ffeaf [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements LoopPass and LPPassManager. All loop optimization
11// and transformation passes are derived from LoopPass. LPPassManager is
12// responsible for managing LoopPasses.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/LoopPass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// LPPassManager
21//
22
23char LPPassManager::ID = 0;
24/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
25
26LPPassManager::LPPassManager(int Depth)
Dan Gohman26f8c272008-09-04 17:05:41 +000027 : FunctionPass(&ID), PMDataManager(Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 skipThisLoop = false;
29 redoThisLoop = false;
30 LI = NULL;
31 CurrentLoop = NULL;
32}
33
Dan Gohman68fa99f2008-07-11 22:51:32 +000034/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035void LPPassManager::deleteLoopFromQueue(Loop *L) {
36
37 if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
38 // Reparent all of the blocks in this loop. Since BBLoop had a parent,
39 // they are now all in it.
40 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
41 I != E; ++I)
42 if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops.
43 LI->changeLoopFor(*I, ParentLoop);
44
45 // Remove the loop from its parent loop.
46 for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
47 ++I) {
48 assert(I != E && "Couldn't find loop");
49 if (*I == L) {
50 ParentLoop->removeChildLoop(I);
51 break;
52 }
53 }
54
55 // Move all subloops into the parent loop.
Dan Gohman3f7d94b2007-10-03 19:26:29 +000056 while (!L->empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
58 } else {
59 // Reparent all of the blocks in this loop. Since BBLoop had no parent,
60 // they no longer in a loop at all.
61
62 for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
63 // Don't change blocks in subloops.
64 if (LI->getLoopFor(L->getBlocks()[i]) == L) {
65 LI->removeBlock(L->getBlocks()[i]);
66 --i;
67 }
68 }
69
70 // Remove the loop from the top-level LoopInfo object.
71 for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
72 assert(I != E && "Couldn't find loop");
73 if (*I == L) {
74 LI->removeLoop(I);
75 break;
76 }
77 }
78
79 // Move all of the subloops to the top-level.
Dan Gohman3f7d94b2007-10-03 19:26:29 +000080 while (!L->empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
82 }
83
84 delete L;
85
86 // If L is current loop then skip rest of the passes and let
87 // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
88 // and continue applying other passes on CurrentLoop.
89 if (CurrentLoop == L) {
90 skipThisLoop = true;
91 return;
92 }
93
94 for (std::deque<Loop *>::iterator I = LQ.begin(),
95 E = LQ.end(); I != E; ++I) {
96 if (*I == L) {
97 LQ.erase(I);
98 break;
99 }
100 }
101}
102
103// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
104void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
105
106 assert (CurrentLoop != L && "Cannot insert CurrentLoop");
107
108 // Insert into loop nest
109 if (ParentLoop)
110 ParentLoop->addChildLoop(L);
111 else
112 LI->addTopLevelLoop(L);
113
114 // Insert L into loop queue
115 if (L == CurrentLoop)
116 redoLoop(L);
117 else if (!ParentLoop)
118 // This is top level loop.
119 LQ.push_front(L);
120 else {
121 // Insert L after ParentLoop
122 for (std::deque<Loop *>::iterator I = LQ.begin(),
123 E = LQ.end(); I != E; ++I) {
124 if (*I == ParentLoop) {
125 // deque does not support insert after.
126 ++I;
127 LQ.insert(I, 1, L);
128 break;
129 }
130 }
131 }
132}
133
134// Reoptimize this loop. LPPassManager will re-insert this loop into the
135// queue. This allows LoopPass to change loop nest for the loop. This
136// utility may send LPPassManager into infinite loops so use caution.
137void LPPassManager::redoLoop(Loop *L) {
138 assert (CurrentLoop == L && "Can redo only CurrentLoop");
139 redoThisLoop = true;
140}
141
Devang Patel28891a02007-07-31 08:00:57 +0000142/// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
143/// all loop passes.
144void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
145 BasicBlock *To, Loop *L) {
146 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
147 Pass *P = getContainedPass(Index);
148 LoopPass *LP = dynamic_cast<LoopPass *>(P);
149 LP->cloneBasicBlockAnalysis(From, To, L);
150 }
151}
152
153/// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
154void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
Devang Patelf29fbf12009-03-25 23:57:48 +0000155 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
156 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
157 ++BI) {
158 Instruction &I = *BI;
159 deleteSimpleAnalysisValue(&I, L);
160 }
161 }
Devang Patel28891a02007-07-31 08:00:57 +0000162 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
163 Pass *P = getContainedPass(Index);
164 LoopPass *LP = dynamic_cast<LoopPass *>(P);
165 LP->deleteAnalysisValue(V, L);
166 }
167}
168
169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170// Recurse through all subloops and all loops into LQ.
171static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
172 LQ.push_back(L);
173 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
174 addLoopIntoQueue(*I, LQ);
175}
176
177/// Pass Manager itself does not invalidate any analysis info.
178void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
179 // LPPassManager needs LoopInfo. In the long term LoopInfo class will
180 // become part of LPPassManager.
181 Info.addRequired<LoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 Info.setPreservesAll();
183}
184
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185/// run - Execute all of the passes scheduled for execution. Keep track of
186/// whether any of the passes modifies the function, and if so, return true.
187bool LPPassManager::runOnFunction(Function &F) {
188 LI = &getAnalysis<LoopInfo>();
189 bool Changed = false;
190
Devang Patel340a3b52008-07-03 07:02:30 +0000191 // Collect inherited analysis from Module level pass manager.
192 populateInheritedAnalysis(TPM->activeStack);
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 // Populate Loop Queue
195 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
196 addLoopIntoQueue(*I, LQ);
197
Edwin Török0f48d6f2009-06-29 18:49:09 +0000198 if (LQ.empty()) // No loops, skip calling finalizers
199 return false;
200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 // Initialization
202 for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
203 I != E; ++I) {
204 Loop *L = *I;
205 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
206 Pass *P = getContainedPass(Index);
207 LoopPass *LP = dynamic_cast<LoopPass *>(P);
208 if (LP)
209 Changed |= LP->doInitialization(L, *this);
210 }
211 }
212
213 // Walk Loops
214 while (!LQ.empty()) {
215
216 CurrentLoop = LQ.back();
217 skipThisLoop = false;
218 redoThisLoop = false;
219
220 // Run all passes on current SCC
221 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 Pass *P = getContainedPass(Index);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223
224 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
Chris Lattnere1172482008-08-08 15:14:09 +0000225 dumpRequiredSet(P);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
227 initializeAnalysisImpl(P);
228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 LoopPass *LP = dynamic_cast<LoopPass *>(P);
Chris Lattnerd0845332009-03-06 06:45:05 +0000230 {
231 PassManagerPrettyStackEntry X(LP, *CurrentLoop->getHeader());
232 StartPassTimer(P);
233 assert(LP && "Invalid LPPassManager member");
234 Changed |= LP->runOnLoop(CurrentLoop, *this);
235 StopPassTimer(P);
236 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
238 if (Changed)
239 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
Chris Lattnere1172482008-08-08 15:14:09 +0000240 dumpPreservedSet(P);
Devang Patela5eb9a32007-07-19 18:02:32 +0000241
242 verifyPreservedAnalysis(LP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 removeNotPreservedAnalysis(P);
244 recordAvailableAnalysis(P);
245 removeDeadPasses(P, "", ON_LOOP_MSG);
246
Devang Patelb34c71b2008-07-01 19:50:56 +0000247 // If dominator information is available then verify the info if requested.
Devang Patel36a9bce2008-07-01 17:44:24 +0000248 verifyDomInfo(*LP, F);
249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 if (skipThisLoop)
251 // Do not run other passes on this loop.
252 break;
253 }
254
255 // Pop the loop from queue after running all passes.
256 LQ.pop_back();
257
258 if (redoThisLoop)
259 LQ.push_back(CurrentLoop);
260 }
261
262 // Finalization
263 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
264 Pass *P = getContainedPass(Index);
265 LoopPass *LP = dynamic_cast <LoopPass *>(P);
266 if (LP)
267 Changed |= LP->doFinalization();
268 }
269
270 return Changed;
271}
272
Dan Gohman26a00a32009-02-17 19:41:26 +0000273/// Print passes managed by this manager
274void LPPassManager::dumpPassStructure(unsigned Offset) {
275 llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
276 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
277 Pass *P = getContainedPass(Index);
278 P->dumpPassStructure(Offset + 1);
279 dumpLastUses(P, Offset+1);
280 }
281}
282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
284//===----------------------------------------------------------------------===//
285// LoopPass
286
287// Check if this pass is suitable for the current LPPassManager, if
288// available. This pass P is not suitable for a LPPassManager if P
289// is not preserving higher level analysis info used by other
290// LPPassManager passes. In such case, pop LPPassManager from the
291// stack. This will force assignPassManager() to create new
292// LPPassManger as expected.
293void LoopPass::preparePassManager(PMStack &PMS) {
294
295 // Find LPPassManager
Duncan Sands5e1f0482007-07-19 09:42:01 +0000296 while (!PMS.empty() &&
297 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
298 PMS.pop();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
300 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
301
302 // If this pass is destroying high level information that is used
303 // by other passes that are managed by LPM then do not insert
304 // this pass in current LPM. Use new LPPassManager.
305 if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
306 PMS.pop();
307}
308
309/// Assign pass manager to manage this pass.
310void LoopPass::assignPassManager(PMStack &PMS,
311 PassManagerType PreferredType) {
312 // Find LPPassManager
Duncan Sands5e1f0482007-07-19 09:42:01 +0000313 while (!PMS.empty() &&
314 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
315 PMS.pop();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
318
319 // Create new Loop Pass Manager if it does not exist.
320 if (!LPPM) {
321
322 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
323 PMDataManager *PMD = PMS.top();
324
325 // [1] Create new Call Graph Pass Manager
326 LPPM = new LPPassManager(PMD->getDepth() + 1);
327 LPPM->populateInheritedAnalysis(PMS);
328
329 // [2] Set up new manager's top level manager
330 PMTopLevelManager *TPM = PMD->getTopLevelManager();
331 TPM->addIndirectPassManager(LPPM);
332
333 // [3] Assign manager to manage this new manager. This may create
334 // and push new managers into PMS
335 Pass *P = dynamic_cast<Pass *>(LPPM);
336 TPM->schedulePass(P);
337
338 // [4] Push new manager into PMS
339 PMS.push(LPPM);
340 }
341
342 LPPM->add(this);
343}