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 isAnalysisAvailable(AnalysisID AID) {
37 return (AvailableAnalysis.count(AID) != 0);
40 /// Augment RequiredAnalysis by adding analysis required by pass P.
41 void noteDownRequiredAnalysis(Pass *P);
43 /// Augment AvailableAnalysis by adding analysis made available by pass P.
44 void noteDownAvailableAnalysis(Pass *P);
46 /// Remove Analysis that is not preserved by the pass
47 void removeNotPreservedAnalysis(Pass *P);
49 /// Remove dead passes
50 void removeDeadPasses() { /* TODO : Implement */ }
52 /// Add pass P into the PassVector. Update RequiredAnalysis and
53 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
54 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
56 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
57 /// This is used before running passes managed by the manager.
58 void clearAnalysis() {
59 RequiredAnalysis.clear();
60 AvailableAnalysis.clear();
63 inline std::vector<Pass *>::iterator passVectorBegin() {
64 return PassVector.begin();
67 inline std::vector<Pass *>::iterator passVectorEnd() {
68 return PassVector.end();
72 // Analysis required by the passes managed by this manager. This information
73 // used while selecting pass manager during addPass. If a pass does not
74 // preserve any analysis required by other passes managed by current
75 // pass manager then new pass manager is used.
76 std::vector<AnalysisID> RequiredAnalysis;
78 // Set of available Analysis. This information is used while scheduling
79 // pass. If a pass requires an analysis which is not not available then
80 // equired analysis pass is scheduled to run before the pass itself is
82 std::set<AnalysisID> AvailableAnalysis;
84 // Collection of pass that are managed by this manager
85 std::vector<Pass *> PassVector;
88 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
89 /// pass together and sequence them to process one basic block before
90 /// processing next basic block.
91 class BasicBlockPassManager_New : public CommonPassManagerImpl {
94 BasicBlockPassManager_New() { }
96 /// Add a pass into a passmanager queue.
97 bool addPass(Pass *p);
99 /// Execute all of the passes scheduled for execution. Keep track of
100 /// whether any of the passes modifies the function, and if so, return true.
101 bool runOnFunction(Function &F);
103 /// Return true IFF AnalysisID AID is currently available.
104 bool analysisCurrentlyAvailable(AnalysisID AID);
109 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
110 /// It batches all function passes and basic block pass managers together and
111 /// sequence them to process one function at a time before processing next
113 class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
115 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
116 FunctionPassManagerImpl_New() {
117 activeBBPassManager = NULL;
119 ~FunctionPassManagerImpl_New() { /* TODO */ };
121 /// add - Add a pass to the queue of passes to run. This passes
122 /// ownership of the Pass to the PassManager. When the
123 /// PassManager_X is destroyed, the pass will be destroyed as well, so
124 /// there is no need to delete the pass. (TODO delete passes.)
125 /// This implies that all passes MUST be allocated with 'new'.
126 void add(Pass *P) { /* TODO*/ }
128 /// Add pass into the pass manager queue.
129 bool addPass(Pass *P);
131 /// Execute all of the passes scheduled for execution. Keep
132 /// track of whether any of the passes modifies the function, and if
134 bool runOnModule(Module &M);
136 /// Return true IFF AnalysisID AID is currently available.
137 bool analysisCurrentlyAvailable(AnalysisID AID);
140 // Active Pass Managers
141 BasicBlockPassManager_New *activeBBPassManager;
144 /// ModulePassManager_New manages ModulePasses and function pass managers.
145 /// It batches all Module passes passes and function pass managers together and
146 /// sequence them to process one module.
147 class ModulePassManager_New : public CommonPassManagerImpl {
150 ModulePassManager_New() { activeFunctionPassManager = NULL; }
152 /// Add a pass into a passmanager queue.
153 bool addPass(Pass *p);
155 /// run - Execute all of the passes scheduled for execution. Keep track of
156 /// whether any of the passes modifies the module, and if so, return true.
157 bool runOnModule(Module &M);
159 /// Return true IFF AnalysisID AID is currently available.
160 bool analysisCurrentlyAvailable(AnalysisID AID);
163 // Active Pass Manager
164 FunctionPassManagerImpl_New *activeFunctionPassManager;
167 /// PassManager_New manages ModulePassManagers
168 class PassManagerImpl_New : public CommonPassManagerImpl {
172 /// add - Add a pass to the queue of passes to run. This passes ownership of
173 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
174 /// will be destroyed as well, so there is no need to delete the pass. This
175 /// implies that all passes MUST be allocated with 'new'.
178 /// run - Execute all of the passes scheduled for execution. Keep track of
179 /// whether any of the passes modifies the module, and if so, return true.
182 /// Return true IFF AnalysisID AID is currently available.
183 bool analysisCurrentlyAvailable(AnalysisID AID);
187 /// Add a pass into a passmanager queue. This is used by schedulePasses
188 bool addPass(Pass *p);
190 /// Schedule pass P for execution. Make sure that passes required by
191 /// P are run before P is run. Update analysis info maintained by
192 /// the manager. Remove dead passes. This is a recursive function.
193 void schedulePass(Pass *P);
195 /// Schedule all passes collected in pass queue using add(). Add all the
196 /// schedule passes into various manager's queue using addPass().
197 void schedulePasses();
199 // Collection of pass managers
200 std::vector<ModulePassManager_New *> PassManagers;
202 // Active Pass Manager
203 ModulePassManager_New *activeManager;
206 } // End of llvm namespace
208 // CommonPassManagerImpl implementation
210 /// Return true IFF pass P's required analysis set does not required new
212 bool CommonPassManagerImpl::manageablePass(Pass *P) {
214 AnalysisUsage AnUsage;
215 P->getAnalysisUsage(AnUsage);
217 // If this pass is not preserving information that is required by the other
218 // passes managed by this manager then use new manager
219 if (!AnUsage.getPreservesAll()) {
220 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
221 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
222 E = RequiredAnalysis.end(); I != E; ++I) {
223 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
225 // This analysis is not preserved. Need new manager.
232 /// Augment RequiredAnalysis by adding analysis required by pass P.
233 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
234 AnalysisUsage AnUsage;
235 P->getAnalysisUsage(AnUsage);
236 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
238 // FIXME: What about duplicates ?
239 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
243 /// Augement AvailableAnalysis by adding analysis made available by pass P.
244 void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
246 if (const PassInfo *PI = P->getPassInfo()) {
247 AvailableAnalysis.insert(PI);
249 //TODO This pass is the current implementation of all of the interfaces it
250 //TODO implements as well.
252 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
253 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
254 //TODO CurrentAnalyses[II[i]] = P;
258 /// Remove Analyss not preserved by Pass P
259 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
260 AnalysisUsage AnUsage;
261 P->getAnalysisUsage(AnUsage);
262 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
264 for (std::set<AnalysisID>::iterator I = AvailableAnalysis.begin(),
265 E = AvailableAnalysis.end(); I != E; ++I ) {
266 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
267 PreservedSet.end()) {
268 // Remove this analysis
269 std::set<AnalysisID>::iterator J = I++;
270 AvailableAnalysis.erase(J);
275 /// Add pass P into the PassVector. Update RequiredAnalysis and
276 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
277 void CommonPassManagerImpl::addPassToManager (Pass *P,
278 bool ProcessAnalysis) {
280 if (ProcessAnalysis) {
281 // Take a note of analysis required and made available by this pass
282 noteDownRequiredAnalysis(P);
283 noteDownAvailableAnalysis(P);
285 // Remove the analysis not preserved by this pass
286 removeNotPreservedAnalysis(P);
290 PassVector.push_back(P);
293 /// BasicBlockPassManager implementation
295 /// Add pass P into PassVector and return true. If this pass is not
296 /// manageable by this manager then return false.
298 BasicBlockPassManager_New::addPass(Pass *P) {
300 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
304 // If this pass does not preserve anlysis that is used by other passes
305 // managed by this manager than it is not a suiable pass for this manager.
306 if (!manageablePass(P))
309 addPassToManager (BP);
314 /// Execute all of the passes scheduled for execution by invoking
315 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
316 /// the function, and if so, return true.
318 BasicBlockPassManager_New::runOnFunction(Function &F) {
320 bool Changed = false;
323 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
324 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
325 e = passVectorEnd(); itr != e; ++itr) {
328 noteDownAvailableAnalysis(P);
329 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
330 Changed |= BP->runOnBasicBlock(*I);
331 removeNotPreservedAnalysis(P);
337 /// Return true IFF AnalysisID AID is currently available.
338 bool BasicBlockPassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
339 return isAnalysisAvailable(AID);
342 // FunctionPassManager_New implementation
343 /// Create new Function pass manager
344 FunctionPassManager_New::FunctionPassManager_New() {
345 FPM = new FunctionPassManagerImpl_New();
348 /// add - Add a pass to the queue of passes to run. This passes
349 /// ownership of the Pass to the PassManager. When the
350 /// PassManager_X is destroyed, the pass will be destroyed as well, so
351 /// there is no need to delete the pass. (TODO delete passes.)
352 /// This implies that all passes MUST be allocated with 'new'.
354 FunctionPassManager_New::add(Pass *P) {
358 /// Execute all of the passes scheduled for execution. Keep
359 /// track of whether any of the passes modifies the function, and if
362 FunctionPassManager_New::runOnModule(Module &M) {
363 return FPM->runOnModule(M);
366 // FunctionPassManagerImpl_New implementation
368 // FunctionPassManager
370 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
371 /// either use it into active basic block pass manager or create new basic
372 /// block pass manager to handle pass P.
374 FunctionPassManagerImpl_New::addPass(Pass *P) {
376 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
377 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
379 if (!activeBBPassManager
380 || !activeBBPassManager->addPass(BP)) {
382 activeBBPassManager = new BasicBlockPassManager_New();
383 addPassToManager(activeBBPassManager, false);
384 if (!activeBBPassManager->addPass(BP))
385 assert(0 && "Unable to add Pass");
390 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
394 // If this pass does not preserve anlysis that is used by other passes
395 // managed by this manager than it is not a suiable pass for this manager.
396 if (!manageablePass(P))
399 addPassToManager (FP);
400 activeBBPassManager = NULL;
404 /// Execute all of the passes scheduled for execution by invoking
405 /// runOnFunction method. Keep track of whether any of the passes modifies
406 /// the function, and if so, return true.
408 FunctionPassManagerImpl_New::runOnModule(Module &M) {
410 bool Changed = false;
413 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
414 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
415 e = passVectorEnd(); itr != e; ++itr) {
418 noteDownAvailableAnalysis(P);
419 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
420 Changed |= FP->runOnFunction(*I);
421 removeNotPreservedAnalysis(P);
427 /// Return true IFF AnalysisID AID is currently available.
428 bool FunctionPassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
430 if (isAnalysisAvailable(AID))
433 if (activeBBPassManager &&
434 activeBBPassManager->isAnalysisAvailable(AID))
437 // TODO : Check inactive managers
441 // ModulePassManager implementation
443 /// Add P into pass vector if it is manageble. If P is a FunctionPass
444 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
445 /// is not manageable by this manager.
447 ModulePassManager_New::addPass(Pass *P) {
449 // If P is FunctionPass then use function pass maanager.
450 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
452 activeFunctionPassManager = NULL;
454 if (!activeFunctionPassManager
455 || !activeFunctionPassManager->addPass(P)) {
457 activeFunctionPassManager = new FunctionPassManagerImpl_New();
458 addPassToManager(activeFunctionPassManager, false);
459 if (!activeFunctionPassManager->addPass(FP))
460 assert(0 && "Unable to add pass");
465 ModulePass *MP = dynamic_cast<ModulePass *>(P);
469 // If this pass does not preserve anlysis that is used by other passes
470 // managed by this manager than it is not a suiable pass for this manager.
471 if (!manageablePass(P))
474 addPassToManager(MP);
475 activeFunctionPassManager = NULL;
480 /// Execute all of the passes scheduled for execution by invoking
481 /// runOnModule method. Keep track of whether any of the passes modifies
482 /// the module, and if so, return true.
484 ModulePassManager_New::runOnModule(Module &M) {
485 bool Changed = false;
488 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
489 e = passVectorEnd(); itr != e; ++itr) {
492 noteDownAvailableAnalysis(P);
493 ModulePass *MP = dynamic_cast<ModulePass*>(P);
494 Changed |= MP->runOnModule(M);
495 removeNotPreservedAnalysis(P);
501 /// Return true IFF AnalysisID AID is currently available.
502 bool ModulePassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
504 if (isAnalysisAvailable(AID))
507 if (activeFunctionPassManager &&
508 activeFunctionPassManager->isAnalysisAvailable(AID))
511 // TODO : Check inactive managers
515 /// Return true IFF AnalysisID AID is currently available.
516 bool PassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
518 bool available = false;
519 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
520 e = PassManagers.end(); !available && itr != e; ++itr)
521 available = (*itr)->analysisCurrentlyAvailable(AID);
525 /// Schedule pass P for execution. Make sure that passes required by
526 /// P are run before P is run. Update analysis info maintained by
527 /// the manager. Remove dead passes. This is a recursive function.
528 void PassManagerImpl_New::schedulePass(Pass *P) {
530 AnalysisUsage AnUsage;
531 P->getAnalysisUsage(AnUsage);
532 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
533 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
534 E = RequiredSet.end(); I != E; ++I) {
536 if (!analysisCurrentlyAvailable(*I)) {
537 // Schedule this analysis run first.
538 Pass *AP = (*I)->createPass();
546 /// Schedule all passes from the queue by adding them in their
547 /// respective manager's queue.
548 void PassManagerImpl_New::schedulePasses() {
549 for (std::vector<Pass *>::iterator I = passVectorBegin(),
550 E = passVectorEnd(); I != E; ++I)
554 /// Add pass P to the queue of passes to run.
555 void PassManagerImpl_New::add(Pass *P) {
556 // Do not process Analysis now. Analysis is process while scheduling
558 addPassToManager(P, false);
561 // PassManager_New implementation
562 /// Add P into active pass manager or use new module pass manager to
564 bool PassManagerImpl_New::addPass(Pass *P) {
566 if (!activeManager || !activeManager->addPass(P)) {
567 activeManager = new ModulePassManager_New();
568 PassManagers.push_back(activeManager);
571 return activeManager->addPass(P);
574 /// run - Execute all of the passes scheduled for execution. Keep track of
575 /// whether any of the passes modifies the module, and if so, return true.
576 bool PassManagerImpl_New::run(Module &M) {
579 bool Changed = false;
580 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
581 e = PassManagers.end(); itr != e; ++itr) {
582 ModulePassManager_New *pm = *itr;
583 Changed |= pm->runOnModule(M);
588 /// Create new pass manager
589 PassManager_New::PassManager_New() {
590 PM = new PassManagerImpl_New();
593 /// add - Add a pass to the queue of passes to run. This passes ownership of
594 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
595 /// will be destroyed as well, so there is no need to delete the pass. This
596 /// implies that all passes MUST be allocated with 'new'.
598 PassManager_New::add(Pass *P) {
602 /// run - Execute all of the passes scheduled for execution. Keep track of
603 /// whether any of the passes modifies the module, and if so, return true.
605 PassManager_New::run(Module &M) {