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