1 //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/LoopPass.h"
19 //===----------------------------------------------------------------------===//
23 char LPPassManager::ID
= 0;
24 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
26 LPPassManager::LPPassManager(int Depth
)
27 : FunctionPass(&ID
), PMDataManager(Depth
) {
34 /// Delete loop from the loop queue and loop hierarchy (LoopInfo).
35 void LPPassManager::deleteLoopFromQueue(Loop
*L
) {
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();
42 if (LI
->getLoopFor(*I
) == L
) // Don't change blocks in subloops.
43 LI
->changeLoopFor(*I
, ParentLoop
);
45 // Remove the loop from its parent loop.
46 for (Loop::iterator I
= ParentLoop
->begin(), E
= ParentLoop
->end();;
48 assert(I
!= E
&& "Couldn't find loop");
50 ParentLoop
->removeChildLoop(I
);
55 // Move all subloops into the parent loop.
57 ParentLoop
->addChildLoop(L
->removeChildLoop(L
->end()-1));
59 // Reparent all of the blocks in this loop. Since BBLoop had no parent,
60 // they no longer in a loop at all.
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
]);
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");
79 // Move all of the subloops to the top-level.
81 LI
->addTopLevelLoop(L
->removeChildLoop(L
->end()-1));
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
) {
94 for (std::deque
<Loop
*>::iterator I
= LQ
.begin(),
95 E
= LQ
.end(); I
!= E
; ++I
) {
103 // Inset loop into loop nest (LoopInfo) and loop queue (LQ).
104 void LPPassManager::insertLoop(Loop
*L
, Loop
*ParentLoop
) {
106 assert (CurrentLoop
!= L
&& "Cannot insert CurrentLoop");
108 // Insert into loop nest
110 ParentLoop
->addChildLoop(L
);
112 LI
->addTopLevelLoop(L
);
114 // Insert L into loop queue
115 if (L
== CurrentLoop
)
117 else if (!ParentLoop
)
118 // This is top level loop.
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.
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.
137 void LPPassManager::redoLoop(Loop
*L
) {
138 assert (CurrentLoop
== L
&& "Can redo only CurrentLoop");
142 /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
144 void 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
);
153 /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
154 void LPPassManager::deleteSimpleAnalysisValue(Value
*V
, Loop
*L
) {
155 if (BasicBlock
*BB
= dyn_cast
<BasicBlock
>(V
)) {
156 for (BasicBlock::iterator BI
= BB
->begin(), BE
= BB
->end(); BI
!= BE
;
158 Instruction
&I
= *BI
;
159 deleteSimpleAnalysisValue(&I
, L
);
162 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
163 Pass
*P
= getContainedPass(Index
);
164 LoopPass
*LP
= dynamic_cast<LoopPass
*>(P
);
165 LP
->deleteAnalysisValue(V
, L
);
170 // Recurse through all subloops and all loops into LQ.
171 static void addLoopIntoQueue(Loop
*L
, std::deque
<Loop
*> &LQ
) {
173 for (Loop::iterator I
= L
->begin(), E
= L
->end(); I
!= E
; ++I
)
174 addLoopIntoQueue(*I
, LQ
);
177 /// Pass Manager itself does not invalidate any analysis info.
178 void 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
>();
182 Info
.setPreservesAll();
185 /// 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.
187 bool LPPassManager::runOnFunction(Function
&F
) {
188 LI
= &getAnalysis
<LoopInfo
>();
189 bool Changed
= false;
191 // Collect inherited analysis from Module level pass manager.
192 populateInheritedAnalysis(TPM
->activeStack
);
194 // Populate Loop Queue
195 for (LoopInfo::iterator I
= LI
->begin(), E
= LI
->end(); I
!= E
; ++I
)
196 addLoopIntoQueue(*I
, LQ
);
198 if (LQ
.empty()) // No loops, skip calling finalizers
202 for (std::deque
<Loop
*>::const_iterator I
= LQ
.begin(), E
= LQ
.end();
205 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
206 Pass
*P
= getContainedPass(Index
);
207 LoopPass
*LP
= dynamic_cast<LoopPass
*>(P
);
209 Changed
|= LP
->doInitialization(L
, *this);
214 while (!LQ
.empty()) {
216 CurrentLoop
= LQ
.back();
217 skipThisLoop
= false;
218 redoThisLoop
= false;
220 // Run all passes on current SCC
221 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
222 Pass
*P
= getContainedPass(Index
);
224 dumpPassInfo(P
, EXECUTION_MSG
, ON_LOOP_MSG
, "");
227 initializeAnalysisImpl(P
);
229 LoopPass
*LP
= dynamic_cast<LoopPass
*>(P
);
231 PassManagerPrettyStackEntry
X(LP
, *CurrentLoop
->getHeader());
233 assert(LP
&& "Invalid LPPassManager member");
234 Changed
|= LP
->runOnLoop(CurrentLoop
, *this);
239 dumpPassInfo(P
, MODIFICATION_MSG
, ON_LOOP_MSG
, "");
243 verifyPreservedAnalysis(LP
);
245 removeNotPreservedAnalysis(P
);
246 recordAvailableAnalysis(P
);
247 removeDeadPasses(P
, "", ON_LOOP_MSG
);
249 // If dominator information is available then verify the info if requested.
250 verifyDomInfo(*LP
, F
);
253 // Do not run other passes on this loop.
257 // Pop the loop from queue after running all passes.
261 LQ
.push_back(CurrentLoop
);
265 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
266 Pass
*P
= getContainedPass(Index
);
267 LoopPass
*LP
= dynamic_cast <LoopPass
*>(P
);
269 Changed
|= LP
->doFinalization();
275 /// Print passes managed by this manager
276 void LPPassManager::dumpPassStructure(unsigned Offset
) {
277 errs().indent(Offset
*2) << "Loop Pass Manager\n";
278 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
279 Pass
*P
= getContainedPass(Index
);
280 P
->dumpPassStructure(Offset
+ 1);
281 dumpLastUses(P
, Offset
+1);
286 //===----------------------------------------------------------------------===//
289 // Check if this pass is suitable for the current LPPassManager, if
290 // available. This pass P is not suitable for a LPPassManager if P
291 // is not preserving higher level analysis info used by other
292 // LPPassManager passes. In such case, pop LPPassManager from the
293 // stack. This will force assignPassManager() to create new
294 // LPPassManger as expected.
295 void LoopPass::preparePassManager(PMStack
&PMS
) {
297 // Find LPPassManager
298 while (!PMS
.empty() &&
299 PMS
.top()->getPassManagerType() > PMT_LoopPassManager
)
302 LPPassManager
*LPPM
= dynamic_cast<LPPassManager
*>(PMS
.top());
304 // If this pass is destroying high level information that is used
305 // by other passes that are managed by LPM then do not insert
306 // this pass in current LPM. Use new LPPassManager.
307 if (LPPM
&& !LPPM
->preserveHigherLevelAnalysis(this))
311 /// Assign pass manager to manage this pass.
312 void LoopPass::assignPassManager(PMStack
&PMS
,
313 PassManagerType PreferredType
) {
314 // Find LPPassManager
315 while (!PMS
.empty() &&
316 PMS
.top()->getPassManagerType() > PMT_LoopPassManager
)
319 LPPassManager
*LPPM
= dynamic_cast<LPPassManager
*>(PMS
.top());
321 // Create new Loop Pass Manager if it does not exist.
324 assert (!PMS
.empty() && "Unable to create Loop Pass Manager");
325 PMDataManager
*PMD
= PMS
.top();
327 // [1] Create new Call Graph Pass Manager
328 LPPM
= new LPPassManager(PMD
->getDepth() + 1);
329 LPPM
->populateInheritedAnalysis(PMS
);
331 // [2] Set up new manager's top level manager
332 PMTopLevelManager
*TPM
= PMD
->getTopLevelManager();
333 TPM
->addIndirectPassManager(LPPM
);
335 // [3] Assign manager to manage this new manager. This may create
336 // and push new managers into PMS
337 Pass
*P
= dynamic_cast<Pass
*>(LPPM
);
338 TPM
->schedulePass(P
);
340 // [4] Push new manager into PMS