blob: c4ff9889446da81dc5bdeb2b32e277b83e0916fa [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"
17using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// LPPassManager
21//
Devang Patel794fd752007-05-01 21:15:47 +000022
Devang Patel19974732007-05-03 01:11:54 +000023char LPPassManager::ID = 0;
Devang Patel16a31c42007-02-22 08:56:17 +000024/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
25
Devang Patel794fd752007-05-01 21:15:47 +000026LPPassManager::LPPassManager(int Depth)
Dan Gohmanae73dc12008-09-04 17:05:41 +000027 : FunctionPass(&ID), PMDataManager(Depth) {
Devang Patel8ded5852007-02-23 00:16:44 +000028 skipThisLoop = false;
29 redoThisLoop = false;
Devang Patel7a9a0692007-03-06 18:38:33 +000030 LI = NULL;
31 CurrentLoop = NULL;
Devang Pateld0e6e332007-02-22 23:30:07 +000032}
33
Dan Gohmane6acf362008-07-11 22:51:32 +000034/// Delete loop from the loop queue and loop hierarchy (LoopInfo).
Devang Patel5afdc7d2007-02-23 00:10:16 +000035void LPPassManager::deleteLoopFromQueue(Loop *L) {
Devang Patel7a9a0692007-03-06 18:38:33 +000036
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 Gohmancb406c22007-10-03 19:26:29 +000056 while (!L->empty())
Devang Patel7a9a0692007-03-06 18:38:33 +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 Gohmancb406c22007-10-03 19:26:29 +000080 while (!L->empty())
Devang Patel7a9a0692007-03-06 18:38:33 +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 }
Devang Patel5afdc7d2007-02-23 00:10:16 +0000101}
102
Devang Patela885c062007-03-06 19:00:02 +0000103// 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
Devang Patel8ded5852007-02-23 00:16:44 +0000134// 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) {
Devang Patel1bc89362007-03-07 00:26:10 +0000138 assert (CurrentLoop == L && "Can redo only CurrentLoop");
Devang Patel8ded5852007-02-23 00:16:44 +0000139 redoThisLoop = true;
140}
141
Devang Patelc7e49c02007-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) {
155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
156 Pass *P = getContainedPass(Index);
157 LoopPass *LP = dynamic_cast<LoopPass *>(P);
158 LP->deleteAnalysisValue(V, L);
159 }
160}
161
162
Devang Patel643a79b2007-02-22 23:45:15 +0000163// Recurse through all subloops and all loops into LQ.
Devang Patel30159722007-03-06 02:30:46 +0000164static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
Devang Patel622adea2007-03-06 19:50:49 +0000165 LQ.push_back(L);
Devang Patel643a79b2007-02-22 23:45:15 +0000166 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
167 addLoopIntoQueue(*I, LQ);
Devang Patel643a79b2007-02-22 23:45:15 +0000168}
169
Devang Patelc37177e2007-03-06 19:11:25 +0000170/// Pass Manager itself does not invalidate any analysis info.
171void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
172 // LPPassManager needs LoopInfo. In the long term LoopInfo class will
173 // become part of LPPassManager.
174 Info.addRequired<LoopInfo>();
175 Info.setPreservesAll();
176}
177
Devang Patel16a31c42007-02-22 08:56:17 +0000178/// run - Execute all of the passes scheduled for execution. Keep track of
179/// whether any of the passes modifies the function, and if so, return true.
180bool LPPassManager::runOnFunction(Function &F) {
Devang Patelc37177e2007-03-06 19:11:25 +0000181 LI = &getAnalysis<LoopInfo>();
Devang Patel16a31c42007-02-22 08:56:17 +0000182 bool Changed = false;
183
Devang Patel70c09c52008-07-03 07:02:30 +0000184 // Collect inherited analysis from Module level pass manager.
185 populateInheritedAnalysis(TPM->activeStack);
186
Devang Patel643a79b2007-02-22 23:45:15 +0000187 // Populate Loop Queue
Devang Patelc37177e2007-03-06 19:11:25 +0000188 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
Devang Patel643a79b2007-02-22 23:45:15 +0000189 addLoopIntoQueue(*I, LQ);
190
Devang Patela5057d02007-03-06 16:59:03 +0000191 // Initialization
192 for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
193 I != E; ++I) {
194 Loop *L = *I;
195 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
196 Pass *P = getContainedPass(Index);
197 LoopPass *LP = dynamic_cast<LoopPass *>(P);
198 if (LP)
199 Changed |= LP->doInitialization(L, *this);
200 }
201 }
202
Devang Patel16a31c42007-02-22 08:56:17 +0000203 // Walk Loops
Devang Patel30159722007-03-06 02:30:46 +0000204 while (!LQ.empty()) {
Devang Patel643a79b2007-02-22 23:45:15 +0000205
Devang Patel7a9a0692007-03-06 18:38:33 +0000206 CurrentLoop = LQ.back();
Devang Patel5afdc7d2007-02-23 00:10:16 +0000207 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +0000208 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +0000209
Devang Patel16a31c42007-02-22 08:56:17 +0000210 // Run all passes on current SCC
211 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel16a31c42007-02-22 08:56:17 +0000212 Pass *P = getContainedPass(Index);
Devang Patel16a31c42007-02-22 08:56:17 +0000213
Devang Patel7f997612007-03-05 20:01:30 +0000214 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000215 dumpRequiredSet(P);
Devang Patel16a31c42007-02-22 08:56:17 +0000216
217 initializeAnalysisImpl(P);
218
Devang Patel16a31c42007-02-22 08:56:17 +0000219 LoopPass *LP = dynamic_cast<LoopPass *>(P);
Chris Lattnerd6f16582009-03-06 06:45:05 +0000220 {
221 PassManagerPrettyStackEntry X(LP, *CurrentLoop->getHeader());
222 StartPassTimer(P);
223 assert(LP && "Invalid LPPassManager member");
224 Changed |= LP->runOnLoop(CurrentLoop, *this);
225 StopPassTimer(P);
226 }
Devang Patel16a31c42007-02-22 08:56:17 +0000227
228 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000229 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000230 dumpPreservedSet(P);
Devang Patel58e0ef12007-07-19 18:02:32 +0000231
232 verifyPreservedAnalysis(LP);
Devang Patel16a31c42007-02-22 08:56:17 +0000233 removeNotPreservedAnalysis(P);
234 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000235 removeDeadPasses(P, "", ON_LOOP_MSG);
Devang Patel5afdc7d2007-02-23 00:10:16 +0000236
Devang Patel844a3d12008-07-01 19:50:56 +0000237 // If dominator information is available then verify the info if requested.
Devang Patel5b57e722008-07-01 17:44:24 +0000238 verifyDomInfo(*LP, F);
239
Devang Patel5afdc7d2007-02-23 00:10:16 +0000240 if (skipThisLoop)
241 // Do not run other passes on this loop.
242 break;
Devang Patel16a31c42007-02-22 08:56:17 +0000243 }
Devang Patel643a79b2007-02-22 23:45:15 +0000244
245 // Pop the loop from queue after running all passes.
Devang Patel30159722007-03-06 02:30:46 +0000246 LQ.pop_back();
Devang Patel8ded5852007-02-23 00:16:44 +0000247
248 if (redoThisLoop)
Devang Patel7a9a0692007-03-06 18:38:33 +0000249 LQ.push_back(CurrentLoop);
Devang Patel16a31c42007-02-22 08:56:17 +0000250 }
Devang Patela5057d02007-03-06 16:59:03 +0000251
252 // Finalization
253 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
254 Pass *P = getContainedPass(Index);
255 LoopPass *LP = dynamic_cast <LoopPass *>(P);
256 if (LP)
257 Changed |= LP->doFinalization();
258 }
Devang Patel16a31c42007-02-22 08:56:17 +0000259
260 return Changed;
261}
262
Dan Gohman189c6352009-02-17 19:41:26 +0000263/// Print passes managed by this manager
264void LPPassManager::dumpPassStructure(unsigned Offset) {
265 llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
266 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
267 Pass *P = getContainedPass(Index);
268 P->dumpPassStructure(Offset + 1);
269 dumpLastUses(P, Offset+1);
270 }
271}
272
Devang Patel16a31c42007-02-22 08:56:17 +0000273
Devang Patelbfd59052007-02-23 00:36:57 +0000274//===----------------------------------------------------------------------===//
275// LoopPass
276
Devang Patel22033be2007-03-06 17:59:37 +0000277// Check if this pass is suitable for the current LPPassManager, if
278// available. This pass P is not suitable for a LPPassManager if P
279// is not preserving higher level analysis info used by other
280// LPPassManager passes. In such case, pop LPPassManager from the
281// stack. This will force assignPassManager() to create new
282// LPPassManger as expected.
283void LoopPass::preparePassManager(PMStack &PMS) {
284
285 // Find LPPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000286 while (!PMS.empty() &&
287 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
288 PMS.pop();
Devang Patel22033be2007-03-06 17:59:37 +0000289
290 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
291
292 // If this pass is destroying high level information that is used
293 // by other passes that are managed by LPM then do not insert
294 // this pass in current LPM. Use new LPPassManager.
295 if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
296 PMS.pop();
297}
298
Devang Patelbfd59052007-02-23 00:36:57 +0000299/// Assign pass manager to manage this pass.
300void LoopPass::assignPassManager(PMStack &PMS,
301 PassManagerType PreferredType) {
302 // Find LPPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000303 while (!PMS.empty() &&
304 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
305 PMS.pop();
Devang Patelbfd59052007-02-23 00:36:57 +0000306
307 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
308
309 // Create new Loop Pass Manager if it does not exist.
310 if (!LPPM) {
311
312 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
313 PMDataManager *PMD = PMS.top();
314
315 // [1] Create new Call Graph Pass Manager
316 LPPM = new LPPassManager(PMD->getDepth() + 1);
Devang Patel1bc89362007-03-07 00:26:10 +0000317 LPPM->populateInheritedAnalysis(PMS);
Devang Patelbfd59052007-02-23 00:36:57 +0000318
319 // [2] Set up new manager's top level manager
320 PMTopLevelManager *TPM = PMD->getTopLevelManager();
321 TPM->addIndirectPassManager(LPPM);
322
323 // [3] Assign manager to manage this new manager. This may create
324 // and push new managers into PMS
325 Pass *P = dynamic_cast<Pass *>(LPPM);
Devang Patelc37177e2007-03-06 19:11:25 +0000326 TPM->schedulePass(P);
Devang Patelbfd59052007-02-23 00:36:57 +0000327
328 // [4] Push new manager into PMS
329 PMS.push(LPPM);
330 }
331
332 LPPM->add(this);
333}