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"
18 #include "llvm/Support/Streams.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.
92 // Analysis required by the passes managed by this manager. This information
93 // used while selecting pass manager during addPass. If a pass does not
94 // preserve any analysis required by other passes managed by current
95 // pass manager then new pass manager is used.
96 std::vector<AnalysisID> RequiredAnalysis;
98 // Set of available Analysis. This information is used while scheduling
99 // pass. If a pass requires an analysis which is not not available then
100 // equired analysis pass is scheduled to run before the pass itself is
102 std::map<AnalysisID, Pass*> AvailableAnalysis;
104 // Map to keep track of last user of the analysis pass.
105 // LastUser->second is the last user of Lastuser->first.
106 std::map<Pass *, Pass *> LastUser;
108 // Collection of pass that are managed by this manager
109 std::vector<Pass *> PassVector;
112 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
113 /// pass together and sequence them to process one basic block before
114 /// processing next basic block.
115 class BasicBlockPassManager_New : public CommonPassManagerImpl,
116 public FunctionPass {
119 BasicBlockPassManager_New() { }
121 /// Add a pass into a passmanager queue.
122 bool addPass(Pass *p);
124 /// Execute all of the passes scheduled for execution. Keep track of
125 /// whether any of the passes modifies the function, and if so, return true.
126 bool runOnFunction(Function &F);
128 /// Return true IFF AnalysisID AID is currently available.
129 Pass *getAnalysisPassFromManager(AnalysisID AID);
134 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
135 /// It batches all function passes and basic block pass managers together and
136 /// sequence them to process one function at a time before processing next
138 class FunctionPassManagerImpl_New : public CommonPassManagerImpl,
141 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
142 FunctionPassManagerImpl_New() {
143 activeBBPassManager = NULL;
145 ~FunctionPassManagerImpl_New() { /* TODO */ };
147 /// add - Add a pass to the queue of passes to run. This passes
148 /// ownership of the Pass to the PassManager. When the
149 /// PassManager_X is destroyed, the pass will be destroyed as well, so
150 /// there is no need to delete the pass. (TODO delete passes.)
151 /// This implies that all passes MUST be allocated with 'new'.
152 void add(Pass *P) { /* TODO*/ }
154 /// Add pass into the pass manager queue.
155 bool addPass(Pass *P);
157 /// Execute all of the passes scheduled for execution. Keep
158 /// track of whether any of the passes modifies the function, and if
160 bool runOnModule(Module &M);
161 bool runOnFunction(Function &F);
163 /// Return true IFF AnalysisID AID is currently available.
164 Pass *getAnalysisPassFromManager(AnalysisID AID);
166 /// doInitialization - Run all of the initializers for the function passes.
168 bool doInitialization(Module &M);
170 /// doFinalization - Run all of the initializers for the function passes.
172 bool doFinalization(Module &M);
174 // Active Pass Managers
175 BasicBlockPassManager_New *activeBBPassManager;
178 /// ModulePassManager_New manages ModulePasses and function pass managers.
179 /// It batches all Module passes passes and function pass managers together and
180 /// sequence them to process one module.
181 class ModulePassManager_New : public CommonPassManagerImpl {
184 ModulePassManager_New() { activeFunctionPassManager = NULL; }
186 /// Add a pass into a passmanager queue.
187 bool addPass(Pass *p);
189 /// run - Execute all of the passes scheduled for execution. Keep track of
190 /// whether any of the passes modifies the module, and if so, return true.
191 bool runOnModule(Module &M);
193 /// Return true IFF AnalysisID AID is currently available.
194 Pass *getAnalysisPassFromManager(AnalysisID AID);
197 // Active Pass Manager
198 FunctionPassManagerImpl_New *activeFunctionPassManager;
201 /// PassManager_New manages ModulePassManagers
202 class PassManagerImpl_New : public CommonPassManagerImpl {
206 /// add - Add a pass to the queue of passes to run. This passes ownership of
207 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
208 /// will be destroyed as well, so there is no need to delete the pass. This
209 /// implies that all passes MUST be allocated with 'new'.
212 /// run - Execute all of the passes scheduled for execution. Keep track of
213 /// whether any of the passes modifies the module, and if so, return true.
216 /// Return true IFF AnalysisID AID is currently available.
217 Pass *getAnalysisPassFromManager(AnalysisID AID);
221 /// Add a pass into a passmanager queue. This is used by schedulePasses
222 bool addPass(Pass *p);
224 /// Schedule pass P for execution. Make sure that passes required by
225 /// P are run before P is run. Update analysis info maintained by
226 /// the manager. Remove dead passes. This is a recursive function.
227 void schedulePass(Pass *P);
229 /// Schedule all passes collected in pass queue using add(). Add all the
230 /// schedule passes into various manager's queue using addPass().
231 void schedulePasses();
233 // Collection of pass managers
234 std::vector<ModulePassManager_New *> PassManagers;
236 // Active Pass Manager
237 ModulePassManager_New *activeManager;
240 } // End of llvm namespace
242 // CommonPassManagerImpl implementation
244 /// Return true IFF pass P's required analysis set does not required new
246 bool CommonPassManagerImpl::manageablePass(Pass *P) {
248 AnalysisUsage AnUsage;
249 P->getAnalysisUsage(AnUsage);
251 // If this pass is not preserving information that is required by the other
252 // passes managed by this manager then use new manager
253 if (!AnUsage.getPreservesAll()) {
254 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
255 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
256 E = RequiredAnalysis.end(); I != E; ++I) {
257 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
259 // This analysis is not preserved. Need new manager.
266 /// Augment RequiredAnalysis by adding analysis required by pass P.
267 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
268 AnalysisUsage AnUsage;
269 P->getAnalysisUsage(AnUsage);
270 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
272 // FIXME: What about duplicates ?
273 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
276 initializeAnalysisImpl(P);
279 /// Augement AvailableAnalysis by adding analysis made available by pass P.
280 void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
282 if (const PassInfo *PI = P->getPassInfo()) {
283 AvailableAnalysis[PI] = P;
285 //TODO This pass is the current implementation of all of the interfaces it
286 //TODO implements as well.
288 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
289 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
290 //TODO CurrentAnalyses[II[i]] = P;
294 /// Remove Analyss not preserved by Pass P
295 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
296 AnalysisUsage AnUsage;
297 P->getAnalysisUsage(AnUsage);
298 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
300 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
301 E = AvailableAnalysis.end(); I != E; ++I ) {
302 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
303 PreservedSet.end()) {
304 // Remove this analysis
305 std::map<AnalysisID, Pass*>::iterator J = I++;
306 AvailableAnalysis.erase(J);
311 /// Remove analysis passes that are not used any longer
312 void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
314 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
315 E = LastUser.end(); I !=E; ++I) {
316 if (I->second == P) {
317 Pass *deadPass = I->first;
318 deadPass->releaseMemory();
320 std::map<AnalysisID, Pass*>::iterator Pos =
321 AvailableAnalysis.find(deadPass->getPassInfo());
323 assert (Pos != AvailableAnalysis.end() &&
324 "Pass is not available");
325 AvailableAnalysis.erase(Pos);
330 /// Add pass P into the PassVector. Update RequiredAnalysis and
331 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
332 void CommonPassManagerImpl::addPassToManager (Pass *P,
333 bool ProcessAnalysis) {
335 if (ProcessAnalysis) {
336 // Take a note of analysis required and made available by this pass
337 noteDownRequiredAnalysis(P);
338 noteDownAvailableAnalysis(P);
340 // Remove the analysis not preserved by this pass
341 removeNotPreservedAnalysis(P);
345 PassVector.push_back(P);
348 // All Required analyses should be available to the pass as it runs! Here
349 // we fill in the AnalysisImpls member of the pass so that it can
350 // successfully use the getAnalysis() method to retrieve the
351 // implementations it needs.
353 void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
354 AnalysisUsage AnUsage;
355 P->getAnalysisUsage(AnUsage);
357 for (std::vector<const PassInfo *>::const_iterator
358 I = AnUsage.getRequiredSet().begin(),
359 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
360 Pass *Impl = getAnalysisPass(*I);
362 assert(0 && "Analysis used but not available!");
363 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
367 /// BasicBlockPassManager implementation
369 /// Add pass P into PassVector and return true. If this pass is not
370 /// manageable by this manager then return false.
372 BasicBlockPassManager_New::addPass(Pass *P) {
374 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
378 // If this pass does not preserve anlysis that is used by other passes
379 // managed by this manager than it is not a suiable pass for this manager.
380 if (!manageablePass(P))
383 addPassToManager (BP);
388 /// Execute all of the passes scheduled for execution by invoking
389 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
390 /// the function, and if so, return true.
392 BasicBlockPassManager_New::runOnFunction(Function &F) {
394 bool Changed = false;
397 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
398 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
399 e = passVectorEnd(); itr != e; ++itr) {
402 noteDownAvailableAnalysis(P);
403 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
404 Changed |= BP->runOnBasicBlock(*I);
405 removeNotPreservedAnalysis(P);
411 /// Return true IFF AnalysisID AID is currently available.
412 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
413 return getAnalysisPass(AID);
416 // FunctionPassManager_New implementation
417 /// Create new Function pass manager
418 FunctionPassManager_New::FunctionPassManager_New() {
419 FPM = new FunctionPassManagerImpl_New();
422 /// add - Add a pass to the queue of passes to run. This passes
423 /// ownership of the Pass to the PassManager. When the
424 /// PassManager_X is destroyed, the pass will be destroyed as well, so
425 /// there is no need to delete the pass. (TODO delete passes.)
426 /// This implies that all passes MUST be allocated with 'new'.
427 void FunctionPassManager_New::add(Pass *P) {
431 /// Execute all of the passes scheduled for execution. Keep
432 /// track of whether any of the passes modifies the function, and if
434 bool FunctionPassManager_New::runOnModule(Module &M) {
435 return FPM->runOnModule(M);
438 /// run - Execute all of the passes scheduled for execution. Keep
439 /// track of whether any of the passes modifies the function, and if
442 bool FunctionPassManager_New::run(Function &F) {
444 if (MP->materializeFunction(&F, &errstr)) {
445 llvm_cerr << "Error reading bytecode file: " << errstr << "\n";
448 return FPM->runOnFunction(F);
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.
505 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
507 bool Changed = false;
510 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
511 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
512 e = passVectorEnd(); itr != e; ++itr) {
515 noteDownAvailableAnalysis(P);
516 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
517 Changed |= FP->runOnFunction(*I);
518 removeNotPreservedAnalysis(P);
524 /// Execute all of the passes scheduled for execution by invoking
525 /// runOnFunction method. Keep track of whether any of the passes modifies
526 /// the function, and if so, return true.
527 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
529 bool Changed = false;
532 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
533 e = passVectorEnd(); itr != e; ++itr) {
536 noteDownAvailableAnalysis(P);
537 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
538 Changed |= FP->runOnFunction(F);
539 removeNotPreservedAnalysis(P);
546 /// Return true IFF AnalysisID AID is currently available.
547 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
549 Pass *P = getAnalysisPass(AID);
553 if (activeBBPassManager &&
554 activeBBPassManager->getAnalysisPass(AID) != 0)
555 return activeBBPassManager->getAnalysisPass(AID);
557 // TODO : Check inactive managers
561 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
562 bool Changed = false;
564 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
565 e = passVectorEnd(); itr != e; ++itr) {
568 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
569 Changed |= FP->doInitialization(M);
575 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
576 bool Changed = false;
578 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
579 e = passVectorEnd(); itr != e; ++itr) {
582 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
583 Changed |= FP->doFinalization(M);
591 // ModulePassManager implementation
593 /// Add P into pass vector if it is manageble. If P is a FunctionPass
594 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
595 /// is not manageable by this manager.
597 ModulePassManager_New::addPass(Pass *P) {
599 // If P is FunctionPass then use function pass maanager.
600 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
602 activeFunctionPassManager = NULL;
604 if (!activeFunctionPassManager
605 || !activeFunctionPassManager->addPass(P)) {
607 activeFunctionPassManager = new FunctionPassManagerImpl_New();
608 addPassToManager(activeFunctionPassManager, false);
609 if (!activeFunctionPassManager->addPass(FP))
610 assert(0 && "Unable to add pass");
615 ModulePass *MP = dynamic_cast<ModulePass *>(P);
619 // If this pass does not preserve anlysis that is used by other passes
620 // managed by this manager than it is not a suiable pass for this manager.
621 if (!manageablePass(P))
624 addPassToManager(MP);
625 activeFunctionPassManager = NULL;
630 /// Execute all of the passes scheduled for execution by invoking
631 /// runOnModule method. Keep track of whether any of the passes modifies
632 /// the module, and if so, return true.
634 ModulePassManager_New::runOnModule(Module &M) {
635 bool Changed = false;
638 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
639 e = passVectorEnd(); itr != e; ++itr) {
642 noteDownAvailableAnalysis(P);
643 ModulePass *MP = dynamic_cast<ModulePass*>(P);
644 Changed |= MP->runOnModule(M);
645 removeNotPreservedAnalysis(P);
651 /// Return true IFF AnalysisID AID is currently available.
652 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
655 Pass *P = getAnalysisPass(AID);
659 if (activeFunctionPassManager &&
660 activeFunctionPassManager->getAnalysisPass(AID) != 0)
661 return activeFunctionPassManager->getAnalysisPass(AID);
663 // TODO : Check inactive managers
667 /// Return true IFF AnalysisID AID is currently available.
668 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
671 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
672 e = PassManagers.end(); !P && itr != e; ++itr)
673 P = (*itr)->getAnalysisPassFromManager(AID);
677 /// Schedule pass P for execution. Make sure that passes required by
678 /// P are run before P is run. Update analysis info maintained by
679 /// the manager. Remove dead passes. This is a recursive function.
680 void PassManagerImpl_New::schedulePass(Pass *P) {
682 AnalysisUsage AnUsage;
683 P->getAnalysisUsage(AnUsage);
684 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
685 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
686 E = RequiredSet.end(); I != E; ++I) {
688 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
690 // Schedule this analysis run first.
691 AnalysisPass = (*I)->createPass();
692 schedulePass(AnalysisPass);
694 setLastUser (AnalysisPass, P);
696 // Prolong live range of analyses that are needed after an analysis pass
697 // is destroyed, for querying by subsequent passes
698 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
699 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
700 E = IDs.end(); I != E; ++I) {
701 Pass *AP = getAnalysisPassFromManager(*I);
702 assert (AP && "Analysis pass is not available");
709 /// Schedule all passes from the queue by adding them in their
710 /// respective manager's queue.
711 void PassManagerImpl_New::schedulePasses() {
712 for (std::vector<Pass *>::iterator I = passVectorBegin(),
713 E = passVectorEnd(); I != E; ++I)
717 /// Add pass P to the queue of passes to run.
718 void PassManagerImpl_New::add(Pass *P) {
719 // Do not process Analysis now. Analysis is process while scheduling
721 addPassToManager(P, false);
724 // PassManager_New implementation
725 /// Add P into active pass manager or use new module pass manager to
727 bool PassManagerImpl_New::addPass(Pass *P) {
729 if (!activeManager || !activeManager->addPass(P)) {
730 activeManager = new ModulePassManager_New();
731 PassManagers.push_back(activeManager);
734 return activeManager->addPass(P);
737 /// run - Execute all of the passes scheduled for execution. Keep track of
738 /// whether any of the passes modifies the module, and if so, return true.
739 bool PassManagerImpl_New::run(Module &M) {
742 bool Changed = false;
743 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
744 e = PassManagers.end(); itr != e; ++itr) {
745 ModulePassManager_New *pm = *itr;
746 Changed |= pm->runOnModule(M);
751 /// Create new pass manager
752 PassManager_New::PassManager_New() {
753 PM = new PassManagerImpl_New();
756 /// add - Add a pass to the queue of passes to run. This passes ownership of
757 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
758 /// will be destroyed as well, so there is no need to delete the pass. This
759 /// implies that all passes MUST be allocated with 'new'.
761 PassManager_New::add(Pass *P) {
765 /// run - Execute all of the passes scheduled for execution. Keep track of
766 /// whether any of the passes modifies the module, and if so, return true.
768 PassManager_New::run(Module &M) {