blob: d44e41a63713b524241b8645c5bb747acf4c1ba1 [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//
5// This file was developed by Devang Patel and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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//
22/// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
23
Devang Pateld0e6e332007-02-22 23:30:07 +000024LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) {
Devang Patel8ded5852007-02-23 00:16:44 +000025 skipThisLoop = false;
26 redoThisLoop = false;
Devang Patel7a9a0692007-03-06 18:38:33 +000027 LI = NULL;
28 CurrentLoop = NULL;
Devang Pateld0e6e332007-02-22 23:30:07 +000029}
30
Devang Patel7a9a0692007-03-06 18:38:33 +000031/// Delete loop from the loop queue and loop hierarcy (LoopInfo).
Devang Patel5afdc7d2007-02-23 00:10:16 +000032void LPPassManager::deleteLoopFromQueue(Loop *L) {
Devang Patel7a9a0692007-03-06 18:38:33 +000033
34 if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
35 // Reparent all of the blocks in this loop. Since BBLoop had a parent,
36 // they are now all in it.
37 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
38 I != E; ++I)
39 if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops.
40 LI->changeLoopFor(*I, ParentLoop);
41
42 // Remove the loop from its parent loop.
43 for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
44 ++I) {
45 assert(I != E && "Couldn't find loop");
46 if (*I == L) {
47 ParentLoop->removeChildLoop(I);
48 break;
49 }
50 }
51
52 // Move all subloops into the parent loop.
53 while (L->begin() != L->end())
54 ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
55 } else {
56 // Reparent all of the blocks in this loop. Since BBLoop had no parent,
57 // they no longer in a loop at all.
58
59 for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
60 // Don't change blocks in subloops.
61 if (LI->getLoopFor(L->getBlocks()[i]) == L) {
62 LI->removeBlock(L->getBlocks()[i]);
63 --i;
64 }
65 }
66
67 // Remove the loop from the top-level LoopInfo object.
68 for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
69 assert(I != E && "Couldn't find loop");
70 if (*I == L) {
71 LI->removeLoop(I);
72 break;
73 }
74 }
75
76 // Move all of the subloops to the top-level.
77 while (L->begin() != L->end())
78 LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
79 }
80
81 delete L;
82
83 // If L is current loop then skip rest of the passes and let
84 // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
85 // and continue applying other passes on CurrentLoop.
86 if (CurrentLoop == L) {
87 skipThisLoop = true;
88 return;
89 }
90
91 for (std::deque<Loop *>::iterator I = LQ.begin(),
92 E = LQ.end(); I != E; ++I) {
93 if (*I == L) {
94 LQ.erase(I);
95 break;
96 }
97 }
Devang Patel5afdc7d2007-02-23 00:10:16 +000098}
99
Devang Patela885c062007-03-06 19:00:02 +0000100// Inset loop into loop nest (LoopInfo) and loop queue (LQ).
101void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
102
103 assert (CurrentLoop != L && "Cannot insert CurrentLoop");
104
105 // Insert into loop nest
106 if (ParentLoop)
107 ParentLoop->addChildLoop(L);
108 else
109 LI->addTopLevelLoop(L);
110
111 // Insert L into loop queue
112 if (L == CurrentLoop)
113 redoLoop(L);
114 else if (!ParentLoop)
115 // This is top level loop.
116 LQ.push_front(L);
117 else {
118 // Insert L after ParentLoop
119 for (std::deque<Loop *>::iterator I = LQ.begin(),
120 E = LQ.end(); I != E; ++I) {
121 if (*I == ParentLoop) {
122 // deque does not support insert after.
123 ++I;
124 LQ.insert(I, 1, L);
125 break;
126 }
127 }
128 }
129}
130
Devang Patel8ded5852007-02-23 00:16:44 +0000131// Reoptimize this loop. LPPassManager will re-insert this loop into the
132// queue. This allows LoopPass to change loop nest for the loop. This
133// utility may send LPPassManager into infinite loops so use caution.
134void LPPassManager::redoLoop(Loop *L) {
Devang Patela885c062007-03-06 19:00:02 +0000135 assert (CurrentLoop != L && "Can redo only CurrentLoop");
Devang Patel8ded5852007-02-23 00:16:44 +0000136 redoThisLoop = true;
137}
138
Devang Patel643a79b2007-02-22 23:45:15 +0000139// Recurse through all subloops and all loops into LQ.
Devang Patel30159722007-03-06 02:30:46 +0000140static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
Devang Patel622adea2007-03-06 19:50:49 +0000141 LQ.push_back(L);
Devang Patel643a79b2007-02-22 23:45:15 +0000142 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
143 addLoopIntoQueue(*I, LQ);
Devang Patel643a79b2007-02-22 23:45:15 +0000144}
145
Devang Patelc37177e2007-03-06 19:11:25 +0000146/// Pass Manager itself does not invalidate any analysis info.
147void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
148 // LPPassManager needs LoopInfo. In the long term LoopInfo class will
149 // become part of LPPassManager.
150 Info.addRequired<LoopInfo>();
151 Info.setPreservesAll();
152}
153
Devang Patel16a31c42007-02-22 08:56:17 +0000154/// run - Execute all of the passes scheduled for execution. Keep track of
155/// whether any of the passes modifies the function, and if so, return true.
156bool LPPassManager::runOnFunction(Function &F) {
Devang Patelc37177e2007-03-06 19:11:25 +0000157 LI = &getAnalysis<LoopInfo>();
Devang Patel16a31c42007-02-22 08:56:17 +0000158 bool Changed = false;
159
Devang Patel643a79b2007-02-22 23:45:15 +0000160 // Populate Loop Queue
Devang Patelc37177e2007-03-06 19:11:25 +0000161 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
Devang Patel643a79b2007-02-22 23:45:15 +0000162 addLoopIntoQueue(*I, LQ);
163
Devang Patela5057d02007-03-06 16:59:03 +0000164 // Initialization
165 for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
166 I != E; ++I) {
167 Loop *L = *I;
168 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
169 Pass *P = getContainedPass(Index);
170 LoopPass *LP = dynamic_cast<LoopPass *>(P);
171 if (LP)
172 Changed |= LP->doInitialization(L, *this);
173 }
174 }
175
Devang Patel16a31c42007-02-22 08:56:17 +0000176 // Walk Loops
Devang Patel30159722007-03-06 02:30:46 +0000177 while (!LQ.empty()) {
Devang Patel643a79b2007-02-22 23:45:15 +0000178
Devang Patel7a9a0692007-03-06 18:38:33 +0000179 CurrentLoop = LQ.back();
Devang Patel5afdc7d2007-02-23 00:10:16 +0000180 skipThisLoop = false;
Devang Patel8ded5852007-02-23 00:16:44 +0000181 redoThisLoop = false;
Devang Patel5afdc7d2007-02-23 00:10:16 +0000182
Devang Patel16a31c42007-02-22 08:56:17 +0000183 // Run all passes on current SCC
184 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel643a79b2007-02-22 23:45:15 +0000185
Devang Patel16a31c42007-02-22 08:56:17 +0000186 Pass *P = getContainedPass(Index);
187 AnalysisUsage AnUsage;
188 P->getAnalysisUsage(AnUsage);
189
Devang Patel7f997612007-03-05 20:01:30 +0000190 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
Devang Patel16a31c42007-02-22 08:56:17 +0000191 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
192
193 initializeAnalysisImpl(P);
194
195 StartPassTimer(P);
196 LoopPass *LP = dynamic_cast<LoopPass *>(P);
197 assert (LP && "Invalid LPPassManager member");
Devang Patel7a9a0692007-03-06 18:38:33 +0000198 LP->runOnLoop(CurrentLoop, *this);
Devang Patel16a31c42007-02-22 08:56:17 +0000199 StopPassTimer(P);
200
201 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000202 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
Devang Patel16a31c42007-02-22 08:56:17 +0000203 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
204
205 removeNotPreservedAnalysis(P);
206 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000207 removeDeadPasses(P, "", ON_LOOP_MSG);
Devang Patel5afdc7d2007-02-23 00:10:16 +0000208
209 if (skipThisLoop)
210 // Do not run other passes on this loop.
211 break;
Devang Patel16a31c42007-02-22 08:56:17 +0000212 }
Devang Patel643a79b2007-02-22 23:45:15 +0000213
214 // Pop the loop from queue after running all passes.
Devang Patel30159722007-03-06 02:30:46 +0000215 LQ.pop_back();
Devang Patel8ded5852007-02-23 00:16:44 +0000216
217 if (redoThisLoop)
Devang Patel7a9a0692007-03-06 18:38:33 +0000218 LQ.push_back(CurrentLoop);
Devang Patel16a31c42007-02-22 08:56:17 +0000219 }
Devang Patela5057d02007-03-06 16:59:03 +0000220
221 // Finalization
222 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
223 Pass *P = getContainedPass(Index);
224 LoopPass *LP = dynamic_cast <LoopPass *>(P);
225 if (LP)
226 Changed |= LP->doFinalization();
227 }
Devang Patel16a31c42007-02-22 08:56:17 +0000228
229 return Changed;
230}
231
232
Devang Patelbfd59052007-02-23 00:36:57 +0000233//===----------------------------------------------------------------------===//
234// LoopPass
235
Devang Patel22033be2007-03-06 17:59:37 +0000236// Check if this pass is suitable for the current LPPassManager, if
237// available. This pass P is not suitable for a LPPassManager if P
238// is not preserving higher level analysis info used by other
239// LPPassManager passes. In such case, pop LPPassManager from the
240// stack. This will force assignPassManager() to create new
241// LPPassManger as expected.
242void LoopPass::preparePassManager(PMStack &PMS) {
243
244 // Find LPPassManager
245 while (!PMS.empty()) {
246 if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
247 PMS.pop();
248 else;
249 break;
250 }
251
252 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
253
254 // If this pass is destroying high level information that is used
255 // by other passes that are managed by LPM then do not insert
256 // this pass in current LPM. Use new LPPassManager.
257 if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
258 PMS.pop();
259}
260
Devang Patelbfd59052007-02-23 00:36:57 +0000261/// Assign pass manager to manage this pass.
262void LoopPass::assignPassManager(PMStack &PMS,
263 PassManagerType PreferredType) {
264 // Find LPPassManager
265 while (!PMS.empty()) {
266 if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
267 PMS.pop();
268 else;
269 break;
270 }
271
272 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
273
274 // Create new Loop Pass Manager if it does not exist.
275 if (!LPPM) {
276
277 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
278 PMDataManager *PMD = PMS.top();
279
280 // [1] Create new Call Graph Pass Manager
281 LPPM = new LPPassManager(PMD->getDepth() + 1);
282
283 // [2] Set up new manager's top level manager
284 PMTopLevelManager *TPM = PMD->getTopLevelManager();
285 TPM->addIndirectPassManager(LPPM);
286
287 // [3] Assign manager to manage this new manager. This may create
288 // and push new managers into PMS
289 Pass *P = dynamic_cast<Pass *>(LPPM);
Devang Patelc37177e2007-03-06 19:11:25 +0000290 TPM->schedulePass(P);
Devang Patelbfd59052007-02-23 00:36:57 +0000291
292 // [4] Push new manager into PMS
293 PMS.push(LPPM);
294 }
295
296 LPPM->add(this);
297}
298