1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
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 the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManager.h"
16 #include "llvm/Module.h"
24 /// CommonPassManagerImpl helps pass manager analysis required by
25 /// the managed passes. It provides methods to add/remove analysis
26 /// available and query if certain analysis is available or not.
27 class CommonPassManagerImpl : public Pass {
31 /// Return true IFF pass P's required analysis set does not required new
33 bool manageablePass(Pass *P);
35 /// Return true IFF AnalysisID AID is currently available.
36 bool analysisCurrentlyAvailable(AnalysisID AID);
38 /// Augment RequiredAnalysis by adding analysis required by pass P.
39 void noteDownRequiredAnalysis(Pass *P);
41 /// Augment AvailableAnalysis by adding analysis made available by pass P.
42 void noteDownAvailableAnalysis(Pass *P);
44 /// Remove AnalysisID from the RequiredSet
45 void removeAnalysis(AnalysisID AID);
47 /// Remove Analysis that is not preserved by the pass
48 void removeNotPreservedAnalysis(Pass *P);
50 /// Remove dead passes
51 void removeDeadPasses() { /* TODO : Implement */ }
54 // Analysis required by the passes managed by this manager
55 std::vector<AnalysisID> RequiredAnalysis;
57 // set of available Analysis
58 std::set<AnalysisID> AvailableAnalysis;
61 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
62 /// pass together and sequence them to process one basic block before
63 /// processing next basic block.
64 class BasicBlockPassManager_New : public CommonPassManagerImpl {
67 BasicBlockPassManager_New() { }
69 /// Add a pass into a passmanager queue.
70 bool addPass(Pass *p);
72 /// Execute all of the passes scheduled for execution. Keep track of
73 /// whether any of the passes modifies the function, and if so, return true.
74 bool runOnFunction(Function &F);
77 // Collection of pass that are managed by this manager
78 std::vector<Pass *> PassVector;
81 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
82 /// It batches all function passes and basic block pass managers together and
83 /// sequence them to process one function at a time before processing next
85 class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
87 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
88 FunctionPassManagerImpl_New() {
89 activeBBPassManager = NULL;
91 ~FunctionPassManagerImpl_New() { /* TODO */ };
93 /// add - Add a pass to the queue of passes to run. This passes
94 /// ownership of the Pass to the PassManager. When the
95 /// PassManager_X is destroyed, the pass will be destroyed as well, so
96 /// there is no need to delete the pass. (TODO delete passes.)
97 /// This implies that all passes MUST be allocated with 'new'.
98 void add(Pass *P) { /* TODO*/ }
100 /// Add pass into the pass manager queue.
101 bool addPass(Pass *P);
103 /// Execute all of the passes scheduled for execution. Keep
104 /// track of whether any of the passes modifies the function, and if
106 bool runOnModule(Module &M);
109 // Collection of pass that are manged by this manager
110 std::vector<Pass *> PassVector;
112 // Active Pass Managers
113 BasicBlockPassManager_New *activeBBPassManager;
116 /// ModulePassManager_New manages ModulePasses and function pass managers.
117 /// It batches all Module passes passes and function pass managers together and
118 /// sequence them to process one module.
119 class ModulePassManager_New : public CommonPassManagerImpl {
122 ModulePassManager_New() { activeFunctionPassManager = NULL; }
124 /// Add a pass into a passmanager queue.
125 bool addPass(Pass *p);
127 /// run - Execute all of the passes scheduled for execution. Keep track of
128 /// whether any of the passes modifies the module, and if so, return true.
129 bool runOnModule(Module &M);
132 // Collection of pass that are managed by this manager
133 std::vector<Pass *> PassVector;
135 // Active Pass Manager
136 FunctionPassManagerImpl_New *activeFunctionPassManager;
139 /// PassManager_New manages ModulePassManagers
140 class PassManagerImpl_New : public CommonPassManagerImpl {
144 /// add - Add a pass to the queue of passes to run. This passes ownership of
145 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
146 /// will be destroyed as well, so there is no need to delete the pass. This
147 /// implies that all passes MUST be allocated with 'new'.
150 /// run - Execute all of the passes scheduled for execution. Keep track of
151 /// whether any of the passes modifies the module, and if so, return true.
156 /// Add a pass into a passmanager queue. This is used by schedulePasses
157 bool addPass(Pass *p);
159 /// Schedule all passes collected in pass queue using add(). Add all the
160 /// schedule passes into various manager's queue using addPass().
161 void schedulePasses();
163 // Collection of pass managers
164 std::vector<ModulePassManager_New *> PassManagers;
166 // Collection of pass that are not yet scheduled
167 std::vector<Pass *> PassVector;
169 // Active Pass Manager
170 ModulePassManager_New *activeManager;
173 } // End of llvm namespace
175 // CommonPassManagerImpl implementation
177 /// Return true IFF pass P's required analysis set does not required new
179 bool CommonPassManagerImpl::manageablePass(Pass *P) {
181 AnalysisUsage AnUsage;
182 P->getAnalysisUsage(AnUsage);
184 // If this pass is not preserving information that is required by the other
185 // passes managed by this manager then use new manager
186 if (!AnUsage.getPreservesAll()) {
187 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
188 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
189 E = RequiredAnalysis.end(); I != E; ++I) {
190 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
192 // This analysis is not preserved. Need new manager.
199 /// Return true IFF AnalysisID AID is currently available.
200 bool CommonPassManagerImpl::analysisCurrentlyAvailable(AnalysisID AID) {
206 /// Augment RequiredAnalysis by adding analysis required by pass P.
207 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
208 AnalysisUsage AnUsage;
209 P->getAnalysisUsage(AnUsage);
210 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
212 // FIXME: What about duplicates ?
213 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
217 /// Augement AvailableAnalysis by adding analysis made available by pass P.
218 void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
220 if (const PassInfo *PI = P->getPassInfo()) {
221 AvailableAnalysis.insert(PI);
223 //TODO This pass is the current implementation of all of the interfaces it
224 //TODO implements as well.
226 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
227 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
228 //TODO CurrentAnalyses[II[i]] = P;
232 /// Remove AnalysisID from the RequiredSet
233 void CommonPassManagerImpl::removeAnalysis(AnalysisID AID) {
238 /// Remove Analyss not preserved by Pass P
239 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
240 AnalysisUsage AnUsage;
241 P->getAnalysisUsage(AnUsage);
242 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
244 for (std::set<AnalysisID>::iterator I = AvailableAnalysis.begin(),
245 E = AvailableAnalysis.end(); I != E; ++I ) {
247 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
248 PreservedSet.end()) {
249 // Remove this analysis
250 std::set<AnalysisID>::iterator J = I++;
251 AvailableAnalysis.erase(J);
256 /// BasicBlockPassManager implementation
258 /// Add pass P into PassVector and return true. If this pass is not
259 /// manageable by this manager then return false.
261 BasicBlockPassManager_New::addPass(Pass *P) {
263 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
267 // If this pass does not preserve anlysis that is used by other passes
268 // managed by this manager than it is not a suiable pass for this manager.
269 if (!manageablePass(P))
272 // Take a note of analysis required and made available by this pass
273 noteDownRequiredAnalysis(P);
274 noteDownAvailableAnalysis(P);
277 PassVector.push_back(BP);
279 // Remove the analysis not preserved by this pass
280 removeNotPreservedAnalysis(P);
285 /// Execute all of the passes scheduled for execution by invoking
286 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
287 /// the function, and if so, return true.
289 BasicBlockPassManager_New::runOnFunction(Function &F) {
291 bool Changed = false;
292 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
293 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
294 e = PassVector.end(); itr != e; ++itr) {
296 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
297 Changed |= BP->runOnBasicBlock(*I);
302 // FunctionPassManager_New implementation
303 /// Create new Function pass manager
304 FunctionPassManager_New::FunctionPassManager_New() {
305 FPM = new FunctionPassManagerImpl_New();
308 /// add - Add a pass to the queue of passes to run. This passes
309 /// ownership of the Pass to the PassManager. When the
310 /// PassManager_X is destroyed, the pass will be destroyed as well, so
311 /// there is no need to delete the pass. (TODO delete passes.)
312 /// This implies that all passes MUST be allocated with 'new'.
314 FunctionPassManager_New::add(Pass *P) {
318 /// Execute all of the passes scheduled for execution. Keep
319 /// track of whether any of the passes modifies the function, and if
322 FunctionPassManager_New::runOnModule(Module &M) {
323 return FPM->runOnModule(M);
326 // FunctionPassManagerImpl_New implementation
328 // FunctionPassManager
330 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
331 /// either use it into active basic block pass manager or create new basic
332 /// block pass manager to handle pass P.
334 FunctionPassManagerImpl_New::addPass(Pass *P) {
336 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
337 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
339 if (!activeBBPassManager
340 || !activeBBPassManager->addPass(BP)) {
342 activeBBPassManager = new BasicBlockPassManager_New();
344 PassVector.push_back(activeBBPassManager);
345 if (!activeBBPassManager->addPass(BP))
346 assert(0 && "Unable to add Pass");
351 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
355 // If this pass does not preserve anlysis that is used by other passes
356 // managed by this manager than it is not a suiable pass for this manager.
357 if (!manageablePass(P))
360 // Take a note of analysis required and made available by this pass
361 noteDownRequiredAnalysis(P);
362 noteDownAvailableAnalysis(P);
364 PassVector.push_back(FP);
366 // Remove the analysis not preserved by this pass
367 removeNotPreservedAnalysis(P);
369 activeBBPassManager = NULL;
373 /// Execute all of the passes scheduled for execution by invoking
374 /// runOnFunction method. Keep track of whether any of the passes modifies
375 /// the function, and if so, return true.
377 FunctionPassManagerImpl_New::runOnModule(Module &M) {
379 bool Changed = false;
380 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
381 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
382 e = PassVector.end(); itr != e; ++itr) {
384 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
385 Changed |= FP->runOnFunction(*I);
391 // ModulePassManager implementation
393 /// Add P into pass vector if it is manageble. If P is a FunctionPass
394 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
395 /// is not manageable by this manager.
397 ModulePassManager_New::addPass(Pass *P) {
399 // If P is FunctionPass then use function pass maanager.
400 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
402 activeFunctionPassManager = NULL;
404 if (!activeFunctionPassManager
405 || !activeFunctionPassManager->addPass(P)) {
407 activeFunctionPassManager = new FunctionPassManagerImpl_New();
409 PassVector.push_back(activeFunctionPassManager);
410 if (!activeFunctionPassManager->addPass(FP))
411 assert(0 && "Unable to add pass");
416 ModulePass *MP = dynamic_cast<ModulePass *>(P);
420 // If this pass does not preserve anlysis that is used by other passes
421 // managed by this manager than it is not a suiable pass for this manager.
422 if (!manageablePass(P))
425 // Take a note of analysis required and made available by this pass
426 noteDownRequiredAnalysis(P);
427 noteDownAvailableAnalysis(P);
429 PassVector.push_back(MP);
431 // Remove the analysis not preserved by this pass
432 removeNotPreservedAnalysis(P);
434 activeFunctionPassManager = NULL;
439 /// Execute all of the passes scheduled for execution by invoking
440 /// runOnModule method. Keep track of whether any of the passes modifies
441 /// the module, and if so, return true.
443 ModulePassManager_New::runOnModule(Module &M) {
444 bool Changed = false;
445 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
446 e = PassVector.end(); itr != e; ++itr) {
448 ModulePass *MP = dynamic_cast<ModulePass*>(P);
449 Changed |= MP->runOnModule(M);
454 /// Schedule all passes from the queue by adding them in their
455 /// respective manager's queue.
457 PassManagerImpl_New::schedulePasses() {
461 /// Add pass P to the queue of passes to run.
463 PassManagerImpl_New::add(Pass *P) {
467 // PassManager_New implementation
468 /// Add P into active pass manager or use new module pass manager to
471 PassManagerImpl_New::addPass(Pass *P) {
473 if (!activeManager || !activeManager->addPass(P)) {
474 activeManager = new ModulePassManager_New();
475 PassManagers.push_back(activeManager);
478 return activeManager->addPass(P);
481 /// run - Execute all of the passes scheduled for execution. Keep track of
482 /// whether any of the passes modifies the module, and if so, return true.
484 PassManagerImpl_New::run(Module &M) {
487 bool Changed = false;
488 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
489 e = PassManagers.end(); itr != e; ++itr) {
490 ModulePassManager_New *pm = *itr;
491 Changed |= pm->runOnModule(M);
496 /// Create new pass manager
497 PassManager_New::PassManager_New() {
498 PM = new PassManagerImpl_New();
501 /// add - Add a pass to the queue of passes to run. This passes ownership of
502 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
503 /// will be destroyed as well, so there is no need to delete the pass. This
504 /// implies that all passes MUST be allocated with 'new'.
506 PassManager_New::add(Pass *P) {
510 /// run - Execute all of the passes scheduled for execution. Keep track of
511 /// whether any of the passes modifies the module, and if so, return true.
513 PassManager_New::run(Module &M) {