blob: da9ac2749a03627b338ef5edd193ea936d9086f8 [file] [log] [blame]
Devang Patel16a31c42007-02-22 08:56:17 +00001//===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel16a31c42007-02-22 08:56:17 +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"
Anton Korobeynikov96fea332007-08-20 21:17:26 +000017#include "llvm/Analysis/ScalarEvolution.h"
Devang Patel16a31c42007-02-22 08:56:17 +000018using namespace llvm;
19
20//===----------------------------------------------------------------------===//
21// LPPassManager
22//
Devang Patel794fd752007-05-01 21:15:47 +000023
Devang Patel19974732007-05-03 01:11:54 +000024char LPPassManager::ID = 0;
Devang Patel16a31c42007-02-22 08:56:17 +000025/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
26
Devang Patel794fd752007-05-01 21:15:47 +000027LPPassManager::LPPassManager(int Depth)
Dan Gohmanae73dc12008-09-04 17:05:41 +000028 : FunctionPass(&ID), PMDataManager(Depth) {
Devang Patel8ded5852007-02-23 00:16:44 +000029 skipThisLoop = false;
30 redoThisLoop = false;
Devang Patel7a9a0692007-03-06 18:38:33 +000031 LI = NULL;
32 CurrentLoop = NULL;
Devang Pateld0e6e332007-02-22 23:30:07 +000033}
34
Dan Gohmane6acf362008-07-11 22:51:32 +000035/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
Devang Patel5afdc7d2007-02-23 00:10:16 +000036void LPPassManager::deleteLoopFromQueue(Loop *L) {
Devang Patel7a9a0692007-03-06 18:38:33 +000037
38 if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
39 // Reparent all of the blocks in this loop. Since BBLoop had a parent,
40 // they are now all in it.
41 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
42 I != E; ++I)
43 if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops.
44 LI->changeLoopFor(*I, ParentLoop);
45
46 // Remove the loop from its parent loop.
47 for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
48 ++I) {
49 assert(I != E && "Couldn't find loop");
50 if (*I == L) {
51 ParentLoop->removeChildLoop(I);
52 break;
53 }
54 }
55
56 // Move all subloops into the parent loop.
Dan Gohmancb406c22007-10-03 19:26:29 +000057 while (!L->empty())
Devang Patel7a9a0692007-03-06 18:38:33 +000058 ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
59 } else {
60 // Reparent all of the blocks in this loop. Since BBLoop had no parent,
61 // they no longer in a loop at all.
62
63 for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
64 // Don't change blocks in subloops.
65 if (LI->getLoopFor(L->getBlocks()[i]) == L) {
66 LI->removeBlock(L->getBlocks()[i]);
67 --i;
68 }
69 }
70
71 // Remove the loop from the top-level LoopInfo object.
72 for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
73 assert(I != E && "Couldn't find loop");
74 if (*I == L) {
75 LI->removeLoop(I);
76 break;
77 }
78 }
79
80 // Move all of the subloops to the top-level.
Dan Gohmancb406c22007-10-03 19:26:29 +000081 while (!L->empty())
Devang Patel7a9a0692007-03-06 18:38:33 +000082 LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
83 }
84
85 delete L;
86
87 // If L is current loop then skip rest of the passes and let
88 // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
89 // and continue applying other passes on CurrentLoop.
90 if (CurrentLoop == L) {
91 skipThisLoop = true;
92 return;
93 }
94
95 for (std::deque<Loop *>::iterator I = LQ.begin(),
96 E = LQ.end(); I != E; ++I) {
97 if (*I == L) {
98 LQ.erase(I);
99 break;
100 }
101 }
Devang Patel5afdc7d2007-02-23 00:10:16 +0000102}
103
Devang Patela885c062007-03-06 19:00:02 +0000104// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
105void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
106
107 assert (CurrentLoop != L && "Cannot insert CurrentLoop");
108
109 // Insert into loop nest
110 if (ParentLoop)
111 ParentLoop->addChildLoop(L);
112 else
113 LI->addTopLevelLoop(L);
114
115 // Insert L into loop queue
116 if (L == CurrentLoop)
117 redoLoop(L);
118 else if (!ParentLoop)
119 // This is top level loop.
120 LQ.push_front(L);
121 else {
122 // Insert L after ParentLoop
123 for (std::deque<Loop *>::iterator I = LQ.begin(),
124 E = LQ.end(); I != E; ++I) {
125 if (*I == ParentLoop) {
126 // deque does not support insert after.
127 ++I;
128 LQ.insert(I, 1, L);
129 break;
130 }
131 }
132 }
133}
134
Devang Patel8ded5852007-02-23 00:16:44 +0000135// Reoptimize this loop. LPPassManager will re-insert this loop into the
136// queue. This allows LoopPass to change loop nest for the loop. This
137// utility may send LPPassManager into infinite loops so use caution.
138void LPPassManager::redoLoop(Loop *L) {
Devang Patel1bc89362007-03-07 00:26:10 +0000139 assert (CurrentLoop == L && "Can redo only CurrentLoop");
Devang Patel8ded5852007-02-23 00:16:44 +0000140 redoThisLoop = true;
141}
142
Devang Patelc7e49c02007-07-31 08:00:57 +0000143/// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
144/// all loop passes.
145void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
146 BasicBlock *To, Loop *L) {
147 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
148 Pass *P = getContainedPass(Index);
149 LoopPass *LP = dynamic_cast<LoopPass *>(P);
150 LP->cloneBasicBlockAnalysis(From, To, L);
151 }
152}
153
154/// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
155void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
156 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
157 Pass *P = getContainedPass(Index);
158 LoopPass *LP = dynamic_cast<LoopPass *>(P);
159 LP->deleteAnalysisValue(V, L);
160 }
161}
162
163
Devang Patel643a79b2007-02-22 23:45:15 +0000164// Recurse through all subloops and all loops into LQ.
Devang Patel30159722007-03-06 02:30:46 +0000165static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
Devang Patel622adea2007-03-06 19:50:49 +0000166 LQ.push_back(L);
Devang Patel643a79b2007-02-22 23:45:15 +0000167 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
168 addLoopIntoQueue(*I, LQ);
Devang Patel643a79b2007-02-22 23:45:15 +0000169}
170
Devang Patelc37177e2007-03-06 19:11:25 +0000171/// Pass Manager itself does not invalidate any analysis info.
172void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
173 // LPPassManager needs LoopInfo. In the long term LoopInfo class will
174 // become part of LPPassManager.
175 Info.addRequired<LoopInfo>();
Devang Patel5ee99972007-03-07 06:39:01 +0000176 // Used by IndVar doInitialization.
177 Info.addRequired<ScalarEvolution>();
Devang Patelc37177e2007-03-06 19:11:25 +0000178 Info.setPreservesAll();
179}
180
Devang Patel16a31c42007-02-22 08:56:17 +0000181/// run - Execute all of the passes scheduled for execution. Keep track of
182/// whether any of the passes modifies the function, and if so, return true.
183bool LPPassManager::runOnFunction(Function &F) {
Devang Patelc37177e2007-03-06 19:11:25 +0000184 LI = &getAnalysis<LoopInfo>();
Devang Patel16a31c42007-02-22 08:56:17 +0000185 bool Changed = false;
186
Devang Patel70c09c52008-07-03 07:02:30 +0000187 // Collect inherited analysis from Module level pass manager.
188 populateInheritedAnalysis(TPM->activeStack);
189
Devang Patel643a79b2007-02-22 23:45:15 +0000190 // Populate Loop Queue
Devang Patelc37177e2007-03-06 19:11:25 +0000191 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
Devang Patel643a79b2007-02-22 23:45:15 +0000192 addLoopIntoQueue(*I, LQ);
193
Devang Patela5057d02007-03-06 16:59:03 +0000194 // Initialization
195 for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
196 I != E; ++I) {
197 Loop *L = *I;
198 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
199 Pass *P = getContainedPass(Index);
200 LoopPass *LP = dynamic_cast<LoopPass *>(P);
201 if (LP)
202 Changed |= LP->doInitialization(L, *this);
203 }
204 }
205
Devang Patel16a31c42007-02-22 08:56:17 +0000206 // Walk Loops
Devang Patel30159722007-03-06 02:30:46 +0000207 while (!LQ.empty()) {
Devang Patel643a79b2007-02-22 23:45:15 +0000208
Devang Patel7a9a0692007-03-06 18:38:33 +0000209 CurrentLoop = LQ.back();
Devang Patel5afdc7d2007-02-23 00:10:16 +0000210 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +0000211 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +0000212
Devang Patel16a31c42007-02-22 08:56:17 +0000213 // Run all passes on current SCC
214 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +0000215
Devang Patel16a31c42007-02-22 08:56:17 +0000216 Pass *P = getContainedPass(Index);
Devang Patel16a31c42007-02-22 08:56:17 +0000217
Devang Patel7f997612007-03-05 20:01:30 +0000218 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000219 dumpRequiredSet(P);
Devang Patel16a31c42007-02-22 08:56:17 +0000220
221 initializeAnalysisImpl(P);
222
223 StartPassTimer(P);
224 LoopPass *LP = dynamic_cast<LoopPass *>(P);
225 assert (LP && "Invalid LPPassManager member");
Devang Patel0f2fb602007-09-18 23:58:14 +0000226 Changed |= LP->runOnLoop(CurrentLoop, *this);
Devang Patel16a31c42007-02-22 08:56:17 +0000227 StopPassTimer(P);
228
229 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000230 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000231 dumpPreservedSet(P);
Devang Patel58e0ef12007-07-19 18:02:32 +0000232
233 verifyPreservedAnalysis(LP);
Devang Patel16a31c42007-02-22 08:56:17 +0000234 removeNotPreservedAnalysis(P);
235 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000236 removeDeadPasses(P, "", ON_LOOP_MSG);
Devang Patel5afdc7d2007-02-23 00:10:16 +0000237
Devang Patel844a3d12008-07-01 19:50:56 +0000238 // If dominator information is available then verify the info if requested.
Devang Patel5b57e722008-07-01 17:44:24 +0000239 verifyDomInfo(*LP, F);
240
Devang Patel5afdc7d2007-02-23 00:10:16 +0000241 if (skipThisLoop)
242 // Do not run other passes on this loop.
243 break;
Devang Patel16a31c42007-02-22 08:56:17 +0000244 }
Devang Patel643a79b2007-02-22 23:45:15 +0000245
246 // Pop the loop from queue after running all passes.
Devang Patel30159722007-03-06 02:30:46 +0000247 LQ.pop_back();
Devang Patel8ded5852007-02-23 00:16:44 +0000248
249 if (redoThisLoop)
Devang Patel7a9a0692007-03-06 18:38:33 +0000250 LQ.push_back(CurrentLoop);
Devang Patel16a31c42007-02-22 08:56:17 +0000251 }
Devang Patela5057d02007-03-06 16:59:03 +0000252
253 // Finalization
254 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
255 Pass *P = getContainedPass(Index);
256 LoopPass *LP = dynamic_cast <LoopPass *>(P);
257 if (LP)
258 Changed |= LP->doFinalization();
259 }
Devang Patel16a31c42007-02-22 08:56:17 +0000260
261 return Changed;
262}
263
Dan Gohman189c6352009-02-17 19:41:26 +0000264/// Print passes managed by this manager
265void LPPassManager::dumpPassStructure(unsigned Offset) {
266 llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
267 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
268 Pass *P = getContainedPass(Index);
269 P->dumpPassStructure(Offset + 1);
270 dumpLastUses(P, Offset+1);
271 }
272}
273
Devang Patel16a31c42007-02-22 08:56:17 +0000274
Devang Patelbfd59052007-02-23 00:36:57 +0000275//===----------------------------------------------------------------------===//
276// LoopPass
277
Devang Patel22033be2007-03-06 17:59:37 +0000278// Check if this pass is suitable for the current LPPassManager, if
279// available. This pass P is not suitable for a LPPassManager if P
280// is not preserving higher level analysis info used by other
281// LPPassManager passes. In such case, pop LPPassManager from the
282// stack. This will force assignPassManager() to create new
283// LPPassManger as expected.
284void LoopPass::preparePassManager(PMStack &PMS) {
285
286 // Find LPPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000287 while (!PMS.empty() &&
288 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
289 PMS.pop();
Devang Patel22033be2007-03-06 17:59:37 +0000290
291 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
292
293 // If this pass is destroying high level information that is used
294 // by other passes that are managed by LPM then do not insert
295 // this pass in current LPM. Use new LPPassManager.
296 if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
297 PMS.pop();
298}
299
Devang Patelbfd59052007-02-23 00:36:57 +0000300/// Assign pass manager to manage this pass.
301void LoopPass::assignPassManager(PMStack &PMS,
302 PassManagerType PreferredType) {
303 // Find LPPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000304 while (!PMS.empty() &&
305 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
306 PMS.pop();
Devang Patelbfd59052007-02-23 00:36:57 +0000307
308 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
309
310 // Create new Loop Pass Manager if it does not exist.
311 if (!LPPM) {
312
313 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
314 PMDataManager *PMD = PMS.top();
315
316 // [1] Create new Call Graph Pass Manager
317 LPPM = new LPPassManager(PMD->getDepth() + 1);
Devang Patel1bc89362007-03-07 00:26:10 +0000318 LPPM->populateInheritedAnalysis(PMS);
Devang Patelbfd59052007-02-23 00:36:57 +0000319
320 // [2] Set up new manager's top level manager
321 PMTopLevelManager *TPM = PMD->getTopLevelManager();
322 TPM->addIndirectPassManager(LPPM);
323
324 // [3] Assign manager to manage this new manager. This may create
325 // and push new managers into PMS
326 Pass *P = dynamic_cast<Pass *>(LPPM);
Devang Patelc37177e2007-03-06 19:11:25 +0000327 TPM->schedulePass(P);
Devang Patelbfd59052007-02-23 00:36:57 +0000328
329 // [4] Push new manager into PMS
330 PMS.push(LPPM);
331 }
332
333 LPPM->add(this);
334}