LPPassManager::deleteLoopFromQueue() add meat. Cut-n-paste code from
[oota-llvm.git] / lib / Analysis / LoopPass.cpp
1 //===- 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"
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // LPPassManager
21 //
22 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
23
24 LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) { 
25   skipThisLoop = false;
26   redoThisLoop = false;
27   LI = NULL;
28   CurrentLoop = NULL;
29 }
30
31 /// Delete loop from the loop queue and loop hierarcy (LoopInfo). 
32 void LPPassManager::deleteLoopFromQueue(Loop *L) {
33
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   }
98 }
99
100 // Reoptimize this loop. LPPassManager will re-insert this loop into the
101 // queue. This allows LoopPass to change loop nest for the loop. This
102 // utility may send LPPassManager into infinite loops so use caution.
103 void LPPassManager::redoLoop(Loop *L) {
104   redoThisLoop = true;
105 }
106
107 // Recurse through all subloops and all loops  into LQ.
108 static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
109   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
110     addLoopIntoQueue(*I, LQ);
111   LQ.push_back(L);
112 }
113
114 /// run - Execute all of the passes scheduled for execution.  Keep track of
115 /// whether any of the passes modifies the function, and if so, return true.
116 bool LPPassManager::runOnFunction(Function &F) {
117   LoopInfo &LI = getAnalysis<LoopInfo>();
118   bool Changed = false;
119
120   // Populate Loop Queue
121   for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
122     addLoopIntoQueue(*I, LQ);
123
124   // Initialization
125   for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
126        I != E; ++I) {
127     Loop *L = *I;
128     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
129       Pass *P = getContainedPass(Index);
130       LoopPass *LP = dynamic_cast<LoopPass *>(P);
131       if (LP)
132         Changed |= LP->doInitialization(L, *this);
133     }
134   }
135
136   // Walk Loops
137   while (!LQ.empty()) {
138       
139     CurrentLoop  = LQ.back();
140     skipThisLoop = false;
141     redoThisLoop = false;
142
143     // Run all passes on current SCC
144     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
145         
146       Pass *P = getContainedPass(Index);
147       AnalysisUsage AnUsage;
148       P->getAnalysisUsage(AnUsage);
149
150       dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
151       dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
152
153       initializeAnalysisImpl(P);
154
155       StartPassTimer(P);
156       LoopPass *LP = dynamic_cast<LoopPass *>(P);
157       assert (LP && "Invalid LPPassManager member");
158       LP->runOnLoop(CurrentLoop, *this);
159       StopPassTimer(P);
160
161       if (Changed)
162         dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
163       dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
164       
165       removeNotPreservedAnalysis(P);
166       recordAvailableAnalysis(P);
167       removeDeadPasses(P, "", ON_LOOP_MSG);
168
169       if (skipThisLoop)
170         // Do not run other passes on this loop.
171         break;
172     }
173     
174     // Pop the loop from queue after running all passes.
175     LQ.pop_back();
176     
177     if (redoThisLoop)
178       LQ.push_back(CurrentLoop);
179   }
180   
181   // Finalization
182   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
183     Pass *P = getContainedPass(Index);
184     LoopPass *LP = dynamic_cast <LoopPass *>(P);
185     if (LP)
186       Changed |= LP->doFinalization();
187   }
188
189   return Changed;
190 }
191
192
193 //===----------------------------------------------------------------------===//
194 // LoopPass
195
196 // Check if this pass is suitable for the current LPPassManager, if
197 // available. This pass P is not suitable for a LPPassManager if P
198 // is not preserving higher level analysis info used by other
199 // LPPassManager passes. In such case, pop LPPassManager from the
200 // stack. This will force assignPassManager() to create new
201 // LPPassManger as expected.
202 void LoopPass::preparePassManager(PMStack &PMS) {
203
204   // Find LPPassManager 
205   while (!PMS.empty()) {
206     if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
207       PMS.pop();
208     else;
209     break;
210   }
211
212   LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
213
214   // If this pass is destroying high level information that is used
215   // by other passes that are managed by LPM then do not insert
216   // this pass in current LPM. Use new LPPassManager.
217   if (LPPM && !LPPM->preserveHigherLevelAnalysis(this)) 
218     PMS.pop();
219 }
220
221 /// Assign pass manager to manage this pass.
222 void LoopPass::assignPassManager(PMStack &PMS,
223                                  PassManagerType PreferredType) {
224   // Find LPPassManager 
225   while (!PMS.empty()) {
226     if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
227       PMS.pop();
228     else;
229     break;
230   }
231
232   LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
233
234   // Create new Loop Pass Manager if it does not exist. 
235   if (!LPPM) {
236
237     assert (!PMS.empty() && "Unable to create Loop Pass Manager");
238     PMDataManager *PMD = PMS.top();
239
240     // [1] Create new Call Graph Pass Manager
241     LPPM = new LPPassManager(PMD->getDepth() + 1);
242
243     // [2] Set up new manager's top level manager
244     PMTopLevelManager *TPM = PMD->getTopLevelManager();
245     TPM->addIndirectPassManager(LPPM);
246
247     // [3] Assign manager to manage this new manager. This may create
248     // and push new managers into PMS
249     Pass *P = dynamic_cast<Pass *>(LPPM);
250     P->assignPassManager(PMS);
251
252     // [4] Push new manager into PMS
253     PMS.push(LPPM);
254   }
255
256   LPPM->add(this);
257 }
258