Split FunctionPassManager_New into FunctionPassManager_New and FunctionPassManagerImp...
[oota-llvm.git] / lib / VMCore / PassManager.cpp
1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
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 the LLVM Pass Manager infrastructure. 
11 //
12 //===----------------------------------------------------------------------===//
13
14
15 #include "llvm/PassManager.h"
16 #include "llvm/Module.h"
17
18 using namespace llvm;
19
20 namespace llvm {
21
22 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
23 /// pass together and sequence them to process one basic block before
24 /// processing next basic block.
25 class BasicBlockPassManager_New : public Pass,
26                                   public PassManagerAnalysisHelper {
27
28 public:
29   BasicBlockPassManager_New() { }
30
31   /// Add a pass into a passmanager queue. 
32   bool addPass(Pass *p);
33   
34   /// Execute all of the passes scheduled for execution.  Keep track of
35   /// whether any of the passes modifies the function, and if so, return true.
36   bool runOnFunction(Function &F);
37
38 private:
39   // Collection of pass that are managed by this manager
40   std::vector<Pass *> PassVector;
41 };
42
43 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
44 /// It batches all function passes and basic block pass managers together and
45 /// sequence them to process one function at a time before processing next
46 /// function.
47 class FunctionPassManagerImpl_New : public Pass,
48                                 public PassManagerAnalysisHelper {
49 public:
50   FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
51   FunctionPassManagerImpl_New() { 
52     activeBBPassManager = NULL;
53   }
54   ~FunctionPassManagerImpl_New() { /* TODO */ };
55  
56   /// add - Add a pass to the queue of passes to run.  This passes
57   /// ownership of the Pass to the PassManager.  When the
58   /// PassManager_X is destroyed, the pass will be destroyed as well, so
59   /// there is no need to delete the pass. (TODO delete passes.)
60   /// This implies that all passes MUST be allocated with 'new'.
61   void add(Pass *P) { /* TODO*/  }
62
63   /// Add pass into the pass manager queue.
64   bool addPass(Pass *P);
65
66   /// Execute all of the passes scheduled for execution.  Keep
67   /// track of whether any of the passes modifies the function, and if
68   /// so, return true.
69   bool runOnModule(Module &M);
70
71 private:
72   // Collection of pass that are manged by this manager
73   std::vector<Pass *> PassVector;
74  
75   // Active Pass Managers
76   BasicBlockPassManager_New *activeBBPassManager;
77 };
78
79 /// ModulePassManager_New manages ModulePasses and function pass managers.
80 /// It batches all Module passes  passes and function pass managers together and
81 /// sequence them to process one module.
82 class ModulePassManager_New : public Pass,
83                               public PassManagerAnalysisHelper {
84  
85 public:
86   ModulePassManager_New() { activeFunctionPassManager = NULL; }
87   
88   /// Add a pass into a passmanager queue. 
89   bool addPass(Pass *p);
90   
91   /// run - Execute all of the passes scheduled for execution.  Keep track of
92   /// whether any of the passes modifies the module, and if so, return true.
93   bool runOnModule(Module &M);
94   
95 private:
96   // Collection of pass that are managed by this manager
97   std::vector<Pass *> PassVector;
98   
99   // Active Pass Manager
100   FunctionPassManagerImpl_New *activeFunctionPassManager;
101 };
102
103 /// PassManager_New manages ModulePassManagers
104 class PassManagerImpl_New : public Pass,
105                             public PassManagerAnalysisHelper {
106
107 public:
108
109   /// add - Add a pass to the queue of passes to run.  This passes ownership of
110   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
111   /// will be destroyed as well, so there is no need to delete the pass.  This
112   /// implies that all passes MUST be allocated with 'new'.
113   void add(Pass *P);
114  
115   /// run - Execute all of the passes scheduled for execution.  Keep track of
116   /// whether any of the passes modifies the module, and if so, return true.
117   bool run(Module &M);
118
119 private:
120
121   /// Add a pass into a passmanager queue. This is used by schedulePasses
122   bool addPass(Pass *p);
123
124   /// Schedule all passes collected in pass queue using add(). Add all the
125   /// schedule passes into various manager's queue using addPass().
126   void schedulePasses();
127
128   // Collection of pass managers
129   std::vector<ModulePassManager_New *> PassManagers;
130
131   // Collection of pass that are not yet scheduled
132   std::vector<Pass *> PassVector;
133   
134   // Active Pass Manager
135   ModulePassManager_New *activeManager;
136 };
137
138 } // End of llvm namespace
139
140 // PassManagerAnalysisHelper implementation
141
142 /// Return true IFF pass P's required analysis set does not required new
143 /// manager.
144 bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
145
146   AnalysisUsage AnUsage;
147   P->getAnalysisUsage(AnUsage);
148
149   // If this pass is not preserving information that is required by the other passes
150   // managed by this manager then use new manager
151   // TODO
152   return true;
153 }
154
155 /// Return true IFF AnalysisID AID is currently available.
156 bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
157
158   // TODO
159   return false;
160 }
161
162 /// Augment RequiredSet by adding analysis required by pass P.
163 void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
164
165   // TODO
166 }
167
168 /// Remove AnalysisID from the RequiredSet
169 void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
170
171   // TODO
172 }
173
174 /// Remove Analyss not preserved by Pass P
175 void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
176
177   // TODO
178 }
179
180 /// BasicBlockPassManager implementation
181
182 /// Add pass P into PassVector and return true. If this pass is not
183 /// manageable by this manager then return false.
184 bool
185 BasicBlockPassManager_New::addPass(Pass *P) {
186
187   BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
188   if (!BP)
189     return false;
190
191   // If this pass does not preserve anlysis that is used by other passes
192   // managed by this manager than it is not a suiable pass for this manager.
193   if (!manageablePass(P))
194     return false;
195
196   // Take a note of analysis required by this pass.
197   noteDownRequiredAnalysis(P);
198
199   // Add pass
200   PassVector.push_back(BP);
201   return true;
202 }
203
204 /// Execute all of the passes scheduled for execution by invoking 
205 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
206 /// the function, and if so, return true.
207 bool
208 BasicBlockPassManager_New::runOnFunction(Function &F) {
209
210   bool Changed = false;
211   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
212     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
213            e = PassVector.end(); itr != e; ++itr) {
214       Pass *P = *itr;
215       BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
216       Changed |= BP->runOnBasicBlock(*I);
217     }
218   return Changed;
219 }
220
221 // FunctionPassManager_New implementation
222 /// Create new Function pass manager
223 FunctionPassManager_New::FunctionPassManager_New() {
224   FPM = new FunctionPassManagerImpl_New();
225 }
226
227 /// add - Add a pass to the queue of passes to run.  This passes
228 /// ownership of the Pass to the PassManager.  When the
229 /// PassManager_X is destroyed, the pass will be destroyed as well, so
230 /// there is no need to delete the pass. (TODO delete passes.)
231 /// This implies that all passes MUST be allocated with 'new'.
232 void 
233 FunctionPassManager_New::add(Pass *P) { 
234   FPM->add(P);
235 }
236
237 /// Execute all of the passes scheduled for execution.  Keep
238 /// track of whether any of the passes modifies the function, and if
239 /// so, return true.
240 bool 
241 FunctionPassManager_New::runOnModule(Module &M) {
242   return FPM->runOnModule(M);
243 }
244
245 // FunctionPassManagerImpl_New implementation
246
247 // FunctionPassManager
248
249 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
250 /// either use it into active basic block pass manager or create new basic
251 /// block pass manager to handle pass P.
252 bool
253 FunctionPassManagerImpl_New::addPass(Pass *P) {
254
255   // If P is a BasicBlockPass then use BasicBlockPassManager_New.
256   if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
257
258     if (!activeBBPassManager
259         || !activeBBPassManager->addPass(BP)) {
260
261       activeBBPassManager = new BasicBlockPassManager_New();
262
263       PassVector.push_back(activeBBPassManager);
264       if (!activeBBPassManager->addPass(BP))
265         assert(0 && "Unable to add Pass");
266     }
267     return true;
268   }
269
270   FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
271   if (!FP)
272     return false;
273
274   // If this pass does not preserve anlysis that is used by other passes
275   // managed by this manager than it is not a suiable pass for this manager.
276   if (!manageablePass(P))
277     return false;
278
279   // Take a note of analysis required by this pass.
280   noteDownRequiredAnalysis(P);
281
282   PassVector.push_back(FP);
283   activeBBPassManager = NULL;
284   return true;
285 }
286
287 /// Execute all of the passes scheduled for execution by invoking 
288 /// runOnFunction method.  Keep track of whether any of the passes modifies 
289 /// the function, and if so, return true.
290 bool
291 FunctionPassManagerImpl_New::runOnModule(Module &M) {
292
293   bool Changed = false;
294   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
295     for (std::vector<Pass *>::iterator itr = PassVector.begin(),
296            e = PassVector.end(); itr != e; ++itr) {
297       Pass *P = *itr;
298       FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
299       Changed |= FP->runOnFunction(*I);
300     }
301   return Changed;
302 }
303
304
305 // ModulePassManager implementation
306
307 /// Add P into pass vector if it is manageble. If P is a FunctionPass
308 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
309 /// is not manageable by this manager.
310 bool
311 ModulePassManager_New::addPass(Pass *P) {
312
313   // If P is FunctionPass then use function pass maanager.
314   if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
315
316     activeFunctionPassManager = NULL;
317
318     if (!activeFunctionPassManager
319         || !activeFunctionPassManager->addPass(P)) {
320
321       activeFunctionPassManager = new FunctionPassManagerImpl_New();
322
323       PassVector.push_back(activeFunctionPassManager);
324       if (!activeFunctionPassManager->addPass(FP))
325         assert(0 && "Unable to add pass");
326     }
327     return true;
328   }
329
330   ModulePass *MP = dynamic_cast<ModulePass *>(P);
331   if (!MP)
332     return false;
333
334   // If this pass does not preserve anlysis that is used by other passes
335   // managed by this manager than it is not a suiable pass for this manager.
336   if (!manageablePass(P))
337     return false;
338
339   // Take a note of analysis required by this pass.
340   noteDownRequiredAnalysis(P);
341
342   PassVector.push_back(MP);
343   activeFunctionPassManager = NULL;
344   return true;
345 }
346
347
348 /// Execute all of the passes scheduled for execution by invoking 
349 /// runOnModule method.  Keep track of whether any of the passes modifies 
350 /// the module, and if so, return true.
351 bool
352 ModulePassManager_New::runOnModule(Module &M) {
353   bool Changed = false;
354   for (std::vector<Pass *>::iterator itr = PassVector.begin(),
355          e = PassVector.end(); itr != e; ++itr) {
356     Pass *P = *itr;
357     ModulePass *MP = dynamic_cast<ModulePass*>(P);
358     Changed |= MP->runOnModule(M);
359   }
360   return Changed;
361 }
362
363 /// Schedule all passes from the queue by adding them in their
364 /// respective manager's queue. 
365 void
366 PassManagerImpl_New::schedulePasses() {
367   /* TODO */
368 }
369
370 /// Add pass P to the queue of passes to run.
371 void
372 PassManagerImpl_New::add(Pass *P) {
373   /* TODO */
374 }
375
376 // PassManager_New implementation
377 /// Add P into active pass manager or use new module pass manager to
378 /// manage it.
379 bool
380 PassManagerImpl_New::addPass(Pass *P) {
381
382   if (!activeManager) {
383     activeManager = new ModulePassManager_New();
384     PassManagers.push_back(activeManager);
385   }
386
387   return activeManager->addPass(P);
388 }
389
390 /// run - Execute all of the passes scheduled for execution.  Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
392 bool
393 PassManagerImpl_New::run(Module &M) {
394
395   schedulePasses();
396   bool Changed = false;
397   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
398          e = PassManagers.end(); itr != e; ++itr) {
399     ModulePassManager_New *pm = *itr;
400     Changed |= pm->runOnModule(M);
401   }
402   return Changed;
403 }
404
405 /// Create new pass manager
406 PassManager_New::PassManager_New() {
407   PM = new PassManagerImpl_New();
408 }
409
410 /// add - Add a pass to the queue of passes to run.  This passes ownership of
411 /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
412 /// will be destroyed as well, so there is no need to delete the pass.  This
413 /// implies that all passes MUST be allocated with 'new'.
414 void 
415 PassManager_New::add(Pass *P) {
416   PM->add(P);
417 }
418
419 /// run - Execute all of the passes scheduled for execution.  Keep track of
420 /// whether any of the passes modifies the module, and if so, return true.
421 bool
422 PassManager_New::run(Module &M) {
423   return PM->run(M);
424 }
425