1 //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
3 // The LLVM Compiler Infrastructure
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.
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"
17 #include "llvm/Analysis/ScalarEvolutionExpander.h"
20 //===----------------------------------------------------------------------===//
24 char LPPassManager::ID = 0;
25 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
27 LPPassManager::LPPassManager(int Depth)
28 : FunctionPass((intptr_t)&ID), PMDataManager(Depth) {
35 /// Delete loop from the loop queue and loop hierarcy (LoopInfo).
36 void LPPassManager::deleteLoopFromQueue(Loop *L) {
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();
43 if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops.
44 LI->changeLoopFor(*I, ParentLoop);
46 // Remove the loop from its parent loop.
47 for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
49 assert(I != E && "Couldn't find loop");
51 ParentLoop->removeChildLoop(I);
56 // Move all subloops into the parent loop.
57 while (L->begin() != L->end())
58 ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
60 // Reparent all of the blocks in this loop. Since BBLoop had no parent,
61 // they no longer in a loop at all.
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]);
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");
80 // Move all of the subloops to the top-level.
81 while (L->begin() != L->end())
82 LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
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) {
95 for (std::deque<Loop *>::iterator I = LQ.begin(),
96 E = LQ.end(); I != E; ++I) {
104 // Inset loop into loop nest (LoopInfo) and loop queue (LQ).
105 void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
107 assert (CurrentLoop != L && "Cannot insert CurrentLoop");
109 // Insert into loop nest
111 ParentLoop->addChildLoop(L);
113 LI->addTopLevelLoop(L);
115 // Insert L into loop queue
116 if (L == CurrentLoop)
118 else if (!ParentLoop)
119 // This is top level loop.
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.
135 // 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.
138 void LPPassManager::redoLoop(Loop *L) {
139 assert (CurrentLoop == L && "Can redo only CurrentLoop");
143 /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
145 void 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);
154 /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
155 void 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);
164 // Recurse through all subloops and all loops into LQ.
165 static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
167 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
168 addLoopIntoQueue(*I, LQ);
171 /// Pass Manager itself does not invalidate any analysis info.
172 void 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>();
176 // Used by IndVar doInitialization.
177 Info.addRequired<ScalarEvolution>();
178 Info.setPreservesAll();
181 /// 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.
183 bool LPPassManager::runOnFunction(Function &F) {
184 LI = &getAnalysis<LoopInfo>();
185 bool Changed = false;
187 // Populate Loop Queue
188 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
189 addLoopIntoQueue(*I, LQ);
192 for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
195 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
196 Pass *P = getContainedPass(Index);
197 LoopPass *LP = dynamic_cast<LoopPass *>(P);
199 Changed |= LP->doInitialization(L, *this);
204 while (!LQ.empty()) {
206 CurrentLoop = LQ.back();
207 skipThisLoop = false;
208 redoThisLoop = false;
210 // Run all passes on current SCC
211 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
213 Pass *P = getContainedPass(Index);
214 AnalysisUsage AnUsage;
215 P->getAnalysisUsage(AnUsage);
217 dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
218 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
220 initializeAnalysisImpl(P);
223 LoopPass *LP = dynamic_cast<LoopPass *>(P);
224 assert (LP && "Invalid LPPassManager member");
225 LP->runOnLoop(CurrentLoop, *this);
229 dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
230 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
232 verifyPreservedAnalysis(LP);
233 removeNotPreservedAnalysis(P);
234 recordAvailableAnalysis(P);
235 removeDeadPasses(P, "", ON_LOOP_MSG);
238 // Do not run other passes on this loop.
242 // Pop the loop from queue after running all passes.
246 LQ.push_back(CurrentLoop);
250 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
251 Pass *P = getContainedPass(Index);
252 LoopPass *LP = dynamic_cast <LoopPass *>(P);
254 Changed |= LP->doFinalization();
261 //===----------------------------------------------------------------------===//
264 // Check if this pass is suitable for the current LPPassManager, if
265 // available. This pass P is not suitable for a LPPassManager if P
266 // is not preserving higher level analysis info used by other
267 // LPPassManager passes. In such case, pop LPPassManager from the
268 // stack. This will force assignPassManager() to create new
269 // LPPassManger as expected.
270 void LoopPass::preparePassManager(PMStack &PMS) {
272 // Find LPPassManager
273 while (!PMS.empty() &&
274 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
277 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
279 // If this pass is destroying high level information that is used
280 // by other passes that are managed by LPM then do not insert
281 // this pass in current LPM. Use new LPPassManager.
282 if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
286 /// Assign pass manager to manage this pass.
287 void LoopPass::assignPassManager(PMStack &PMS,
288 PassManagerType PreferredType) {
289 // Find LPPassManager
290 while (!PMS.empty() &&
291 PMS.top()->getPassManagerType() > PMT_LoopPassManager)
294 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
296 // Create new Loop Pass Manager if it does not exist.
299 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
300 PMDataManager *PMD = PMS.top();
302 // [1] Create new Call Graph Pass Manager
303 LPPM = new LPPassManager(PMD->getDepth() + 1);
304 LPPM->populateInheritedAnalysis(PMS);
306 // [2] Set up new manager's top level manager
307 PMTopLevelManager *TPM = PMD->getTopLevelManager();
308 TPM->addIndirectPassManager(LPPM);
310 // [3] Assign manager to manage this new manager. This may create
311 // and push new managers into PMS
312 Pass *P = dynamic_cast<Pass *>(LPPM);
313 TPM->schedulePass(P);
315 // [4] Push new manager into PMS