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"
17 #include "llvm/ModuleProvider.h"
25 /// CommonPassManagerImpl helps pass manager analysis required by
26 /// the managed passes. It provides methods to add/remove analysis
27 /// available and query if certain analysis is available or not.
28 class CommonPassManagerImpl {
32 /// Return true IFF pass P's required analysis set does not required new
34 bool manageablePass(Pass *P);
36 Pass *getAnalysisPass(AnalysisID AID) const {
38 std::map<AnalysisID, Pass*>::const_iterator I =
39 AvailableAnalysis.find(AID);
41 if (I != AvailableAnalysis.end())
47 /// Augment RequiredAnalysis by adding analysis required by pass P.
48 void noteDownRequiredAnalysis(Pass *P);
50 /// Augment AvailableAnalysis by adding analysis made available by pass P.
51 void noteDownAvailableAnalysis(Pass *P);
53 /// Remove Analysis that is not preserved by the pass
54 void removeNotPreservedAnalysis(Pass *P);
56 /// Remove dead passes
57 void removeDeadPasses(Pass *P);
59 /// Add pass P into the PassVector. Update RequiredAnalysis and
60 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
61 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
63 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
64 /// This is used before running passes managed by the manager.
65 void clearAnalysis() {
66 RequiredAnalysis.clear();
67 AvailableAnalysis.clear();
71 // All Required analyses should be available to the pass as it runs! Here
72 // we fill in the AnalysisImpls member of the pass so that it can
73 // successfully use the getAnalysis() method to retrieve the
74 // implementations it needs.
76 void initializeAnalysisImpl(Pass *P);
78 inline std::vector<Pass *>::iterator passVectorBegin() {
79 return PassVector.begin();
82 inline std::vector<Pass *>::iterator passVectorEnd() {
83 return PassVector.end();
86 inline void setLastUser(Pass *P, Pass *LU) {
88 // TODO : Check if pass P is available.
90 // Prolong live range of analyses that are needed after an analysis pass
91 // is destroyed, for querying by subsequent passes
92 AnalysisUsage AnUsage;
93 P->getAnalysisUsage(AnUsage);
94 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
95 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
96 E = IDs.end(); I != E; ++I) {
97 Pass *AnalysisPass = getAnalysisPass(*I); // getAnalysisPassFromManager(*I);
98 assert (AnalysisPass && "Analysis pass is not available");
99 setLastUser(AnalysisPass, LU);
105 // Analysis required by the passes managed by this manager. This information
106 // used while selecting pass manager during addPass. If a pass does not
107 // preserve any analysis required by other passes managed by current
108 // pass manager then new pass manager is used.
109 std::vector<AnalysisID> RequiredAnalysis;
111 // Set of available Analysis. This information is used while scheduling
112 // pass. If a pass requires an analysis which is not not available then
113 // equired analysis pass is scheduled to run before the pass itself is
115 std::map<AnalysisID, Pass*> AvailableAnalysis;
117 // Map to keep track of last user of the analysis pass.
118 // LastUser->second is the last user of Lastuser->first.
119 std::map<Pass *, Pass *> LastUser;
121 // Collection of pass that are managed by this manager
122 std::vector<Pass *> PassVector;
125 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
126 /// pass together and sequence them to process one basic block before
127 /// processing next basic block.
128 class BasicBlockPassManager_New : public CommonPassManagerImpl,
129 public FunctionPass {
132 BasicBlockPassManager_New() { }
134 /// Add a pass into a passmanager queue.
135 bool addPass(Pass *p);
137 /// Execute all of the passes scheduled for execution. Keep track of
138 /// whether any of the passes modifies the function, and if so, return true.
139 bool runOnFunction(Function &F);
141 /// Return true IFF AnalysisID AID is currently available.
142 Pass *getAnalysisPassFromManager(AnalysisID AID);
147 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
148 /// It batches all function passes and basic block pass managers together and
149 /// sequence them to process one function at a time before processing next
151 class FunctionPassManagerImpl_New : public CommonPassManagerImpl,
154 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
155 FunctionPassManagerImpl_New() {
156 activeBBPassManager = NULL;
158 ~FunctionPassManagerImpl_New() { /* TODO */ };
160 /// add - Add a pass to the queue of passes to run. This passes
161 /// ownership of the Pass to the PassManager. When the
162 /// PassManager_X is destroyed, the pass will be destroyed as well, so
163 /// there is no need to delete the pass. (TODO delete passes.)
164 /// This implies that all passes MUST be allocated with 'new'.
165 void add(Pass *P) { /* TODO*/ }
167 /// Add pass into the pass manager queue.
168 bool addPass(Pass *P);
170 /// Execute all of the passes scheduled for execution. Keep
171 /// track of whether any of the passes modifies the function, and if
173 bool runOnModule(Module &M);
175 /// Return true IFF AnalysisID AID is currently available.
176 Pass *getAnalysisPassFromManager(AnalysisID AID);
178 /// doInitialization - Run all of the initializers for the function passes.
180 bool doInitialization(Module &M);
182 /// doFinalization - Run all of the initializers for the function passes.
184 bool doFinalization(Module &M);
186 // Active Pass Managers
187 BasicBlockPassManager_New *activeBBPassManager;
190 /// ModulePassManager_New manages ModulePasses and function pass managers.
191 /// It batches all Module passes passes and function pass managers together and
192 /// sequence them to process one module.
193 class ModulePassManager_New : public CommonPassManagerImpl {
196 ModulePassManager_New() { activeFunctionPassManager = NULL; }
198 /// Add a pass into a passmanager queue.
199 bool addPass(Pass *p);
201 /// run - Execute all of the passes scheduled for execution. Keep track of
202 /// whether any of the passes modifies the module, and if so, return true.
203 bool runOnModule(Module &M);
205 /// Return true IFF AnalysisID AID is currently available.
206 Pass *getAnalysisPassFromManager(AnalysisID AID);
209 // Active Pass Manager
210 FunctionPassManagerImpl_New *activeFunctionPassManager;
213 /// PassManager_New manages ModulePassManagers
214 class PassManagerImpl_New : public CommonPassManagerImpl {
218 /// add - Add a pass to the queue of passes to run. This passes ownership of
219 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
220 /// will be destroyed as well, so there is no need to delete the pass. This
221 /// implies that all passes MUST be allocated with 'new'.
224 /// run - Execute all of the passes scheduled for execution. Keep track of
225 /// whether any of the passes modifies the module, and if so, return true.
228 /// Return true IFF AnalysisID AID is currently available.
229 Pass *getAnalysisPassFromManager(AnalysisID AID);
233 /// Add a pass into a passmanager queue. This is used by schedulePasses
234 bool addPass(Pass *p);
236 /// Schedule pass P for execution. Make sure that passes required by
237 /// P are run before P is run. Update analysis info maintained by
238 /// the manager. Remove dead passes. This is a recursive function.
239 void schedulePass(Pass *P);
241 /// Schedule all passes collected in pass queue using add(). Add all the
242 /// schedule passes into various manager's queue using addPass().
243 void schedulePasses();
245 // Collection of pass managers
246 std::vector<ModulePassManager_New *> PassManagers;
248 // Active Pass Manager
249 ModulePassManager_New *activeManager;
252 } // End of llvm namespace
254 // CommonPassManagerImpl implementation
256 /// Return true IFF pass P's required analysis set does not required new
258 bool CommonPassManagerImpl::manageablePass(Pass *P) {
260 AnalysisUsage AnUsage;
261 P->getAnalysisUsage(AnUsage);
263 // If this pass is not preserving information that is required by the other
264 // passes managed by this manager then use new manager
265 if (!AnUsage.getPreservesAll()) {
266 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
267 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
268 E = RequiredAnalysis.end(); I != E; ++I) {
269 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
271 // This analysis is not preserved. Need new manager.
278 /// Augment RequiredAnalysis by adding analysis required by pass P.
279 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
280 AnalysisUsage AnUsage;
281 P->getAnalysisUsage(AnUsage);
282 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
284 // FIXME: What about duplicates ?
285 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
288 initializeAnalysisImpl(P);
291 /// Augement AvailableAnalysis by adding analysis made available by pass P.
292 void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
294 if (const PassInfo *PI = P->getPassInfo()) {
295 AvailableAnalysis[PI] = P;
297 //TODO This pass is the current implementation of all of the interfaces it
298 //TODO implements as well.
300 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
301 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
302 //TODO CurrentAnalyses[II[i]] = P;
306 /// Remove Analyss not preserved by Pass P
307 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
308 AnalysisUsage AnUsage;
309 P->getAnalysisUsage(AnUsage);
310 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
312 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
313 E = AvailableAnalysis.end(); I != E; ++I ) {
314 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
315 PreservedSet.end()) {
316 // Remove this analysis
317 std::map<AnalysisID, Pass*>::iterator J = I++;
318 AvailableAnalysis.erase(J);
323 /// Remove analysis passes that are not used any longer
324 void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
326 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
327 E = LastUser.end(); I !=E; ++I) {
328 if (I->second == P) {
329 Pass *deadPass = I->first;
330 deadPass->releaseMemory();
332 std::map<AnalysisID, Pass*>::iterator Pos =
333 AvailableAnalysis.find(deadPass->getPassInfo());
335 assert (Pos != AvailableAnalysis.end() &&
336 "Pass is not available");
337 AvailableAnalysis.erase(Pos);
342 /// Add pass P into the PassVector. Update RequiredAnalysis and
343 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
344 void CommonPassManagerImpl::addPassToManager (Pass *P,
345 bool ProcessAnalysis) {
347 if (ProcessAnalysis) {
348 // Take a note of analysis required and made available by this pass
349 noteDownRequiredAnalysis(P);
350 noteDownAvailableAnalysis(P);
352 // Remove the analysis not preserved by this pass
353 removeNotPreservedAnalysis(P);
357 PassVector.push_back(P);
360 // All Required analyses should be available to the pass as it runs! Here
361 // we fill in the AnalysisImpls member of the pass so that it can
362 // successfully use the getAnalysis() method to retrieve the
363 // implementations it needs.
365 void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
366 AnalysisUsage AnUsage;
367 P->getAnalysisUsage(AnUsage);
369 for (std::vector<const PassInfo *>::const_iterator
370 I = AnUsage.getRequiredSet().begin(),
371 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
372 Pass *Impl = getAnalysisPass(*I);
374 assert(0 && "Analysis used but not available!");
375 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
379 /// BasicBlockPassManager implementation
381 /// Add pass P into PassVector and return true. If this pass is not
382 /// manageable by this manager then return false.
384 BasicBlockPassManager_New::addPass(Pass *P) {
386 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
390 // If this pass does not preserve anlysis that is used by other passes
391 // managed by this manager than it is not a suiable pass for this manager.
392 if (!manageablePass(P))
395 addPassToManager (BP);
400 /// Execute all of the passes scheduled for execution by invoking
401 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
402 /// the function, and if so, return true.
404 BasicBlockPassManager_New::runOnFunction(Function &F) {
406 bool Changed = false;
409 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
410 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
411 e = passVectorEnd(); itr != e; ++itr) {
414 noteDownAvailableAnalysis(P);
415 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
416 Changed |= BP->runOnBasicBlock(*I);
417 removeNotPreservedAnalysis(P);
423 /// Return true IFF AnalysisID AID is currently available.
424 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
425 return getAnalysisPass(AID);
428 // FunctionPassManager_New implementation
429 /// Create new Function pass manager
430 FunctionPassManager_New::FunctionPassManager_New() {
431 FPM = new FunctionPassManagerImpl_New();
434 /// add - Add a pass to the queue of passes to run. This passes
435 /// ownership of the Pass to the PassManager. When the
436 /// PassManager_X is destroyed, the pass will be destroyed as well, so
437 /// there is no need to delete the pass. (TODO delete passes.)
438 /// This implies that all passes MUST be allocated with 'new'.
440 FunctionPassManager_New::add(Pass *P) {
444 /// Execute all of the passes scheduled for execution. Keep
445 /// track of whether any of the passes modifies the function, and if
448 FunctionPassManager_New::runOnModule(Module &M) {
449 return FPM->runOnModule(M);
452 /// doInitialization - Run all of the initializers for the function passes.
454 bool FunctionPassManager_New::doInitialization() {
455 return FPM->doInitialization(*MP->getModule());
458 /// doFinalization - Run all of the initializers for the function passes.
460 bool FunctionPassManager_New::doFinalization() {
461 return FPM->doFinalization(*MP->getModule());
464 // FunctionPassManagerImpl_New implementation
466 // FunctionPassManager
468 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
469 /// either use it into active basic block pass manager or create new basic
470 /// block pass manager to handle pass P.
472 FunctionPassManagerImpl_New::addPass(Pass *P) {
474 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
475 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
477 if (!activeBBPassManager
478 || !activeBBPassManager->addPass(BP)) {
480 activeBBPassManager = new BasicBlockPassManager_New();
481 addPassToManager(activeBBPassManager, false);
482 if (!activeBBPassManager->addPass(BP))
483 assert(0 && "Unable to add Pass");
488 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
492 // If this pass does not preserve anlysis that is used by other passes
493 // managed by this manager than it is not a suiable pass for this manager.
494 if (!manageablePass(P))
497 addPassToManager (FP);
498 activeBBPassManager = NULL;
502 /// Execute all of the passes scheduled for execution by invoking
503 /// runOnFunction method. Keep track of whether any of the passes modifies
504 /// the function, and if so, return true.
506 FunctionPassManagerImpl_New::runOnModule(Module &M) {
508 bool Changed = false;
511 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
512 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
513 e = passVectorEnd(); itr != e; ++itr) {
516 noteDownAvailableAnalysis(P);
517 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
518 Changed |= FP->runOnFunction(*I);
519 removeNotPreservedAnalysis(P);
525 /// Return true IFF AnalysisID AID is currently available.
526 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
528 Pass *P = getAnalysisPass(AID);
532 if (activeBBPassManager &&
533 activeBBPassManager->getAnalysisPass(AID) != 0)
534 return activeBBPassManager->getAnalysisPass(AID);
536 // TODO : Check inactive managers
540 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
541 bool Changed = false;
543 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
544 e = passVectorEnd(); itr != e; ++itr) {
547 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
548 Changed |= FP->doInitialization(M);
554 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
555 bool Changed = false;
557 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
558 e = passVectorEnd(); itr != e; ++itr) {
561 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
562 Changed |= FP->doFinalization(M);
570 // ModulePassManager implementation
572 /// Add P into pass vector if it is manageble. If P is a FunctionPass
573 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
574 /// is not manageable by this manager.
576 ModulePassManager_New::addPass(Pass *P) {
578 // If P is FunctionPass then use function pass maanager.
579 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
581 activeFunctionPassManager = NULL;
583 if (!activeFunctionPassManager
584 || !activeFunctionPassManager->addPass(P)) {
586 activeFunctionPassManager = new FunctionPassManagerImpl_New();
587 addPassToManager(activeFunctionPassManager, false);
588 if (!activeFunctionPassManager->addPass(FP))
589 assert(0 && "Unable to add pass");
594 ModulePass *MP = dynamic_cast<ModulePass *>(P);
598 // If this pass does not preserve anlysis that is used by other passes
599 // managed by this manager than it is not a suiable pass for this manager.
600 if (!manageablePass(P))
603 addPassToManager(MP);
604 activeFunctionPassManager = NULL;
609 /// Execute all of the passes scheduled for execution by invoking
610 /// runOnModule method. Keep track of whether any of the passes modifies
611 /// the module, and if so, return true.
613 ModulePassManager_New::runOnModule(Module &M) {
614 bool Changed = false;
617 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
618 e = passVectorEnd(); itr != e; ++itr) {
621 noteDownAvailableAnalysis(P);
622 ModulePass *MP = dynamic_cast<ModulePass*>(P);
623 Changed |= MP->runOnModule(M);
624 removeNotPreservedAnalysis(P);
630 /// Return true IFF AnalysisID AID is currently available.
631 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
634 Pass *P = getAnalysisPass(AID);
638 if (activeFunctionPassManager &&
639 activeFunctionPassManager->getAnalysisPass(AID) != 0)
640 return activeFunctionPassManager->getAnalysisPass(AID);
642 // TODO : Check inactive managers
646 /// Return true IFF AnalysisID AID is currently available.
647 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
650 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
651 e = PassManagers.end(); !P && itr != e; ++itr)
652 P = (*itr)->getAnalysisPassFromManager(AID);
656 /// Schedule pass P for execution. Make sure that passes required by
657 /// P are run before P is run. Update analysis info maintained by
658 /// the manager. Remove dead passes. This is a recursive function.
659 void PassManagerImpl_New::schedulePass(Pass *P) {
661 AnalysisUsage AnUsage;
662 P->getAnalysisUsage(AnUsage);
663 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
664 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
665 E = RequiredSet.end(); I != E; ++I) {
667 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
669 // Schedule this analysis run first.
670 AnalysisPass = (*I)->createPass();
671 schedulePass(AnalysisPass);
673 setLastUser (AnalysisPass, P);
679 /// Schedule all passes from the queue by adding them in their
680 /// respective manager's queue.
681 void PassManagerImpl_New::schedulePasses() {
682 for (std::vector<Pass *>::iterator I = passVectorBegin(),
683 E = passVectorEnd(); I != E; ++I)
687 /// Add pass P to the queue of passes to run.
688 void PassManagerImpl_New::add(Pass *P) {
689 // Do not process Analysis now. Analysis is process while scheduling
691 addPassToManager(P, false);
694 // PassManager_New implementation
695 /// Add P into active pass manager or use new module pass manager to
697 bool PassManagerImpl_New::addPass(Pass *P) {
699 if (!activeManager || !activeManager->addPass(P)) {
700 activeManager = new ModulePassManager_New();
701 PassManagers.push_back(activeManager);
704 return activeManager->addPass(P);
707 /// run - Execute all of the passes scheduled for execution. Keep track of
708 /// whether any of the passes modifies the module, and if so, return true.
709 bool PassManagerImpl_New::run(Module &M) {
712 bool Changed = false;
713 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
714 e = PassManagers.end(); itr != e; ++itr) {
715 ModulePassManager_New *pm = *itr;
716 Changed |= pm->runOnModule(M);
721 /// Create new pass manager
722 PassManager_New::PassManager_New() {
723 PM = new PassManagerImpl_New();
726 /// add - Add a pass to the queue of passes to run. This passes ownership of
727 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
728 /// will be destroyed as well, so there is no need to delete the pass. This
729 /// implies that all passes MUST be allocated with 'new'.
731 PassManager_New::add(Pass *P) {
735 /// run - Execute all of the passes scheduled for execution. Keep track of
736 /// whether any of the passes modifies the module, and if so, return true.
738 PassManager_New::run(Module &M) {