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 Pass *getAnalysisPass(AnalysisID AID) const {
37 std::map<AnalysisID, Pass*>::const_iterator I =
38 AvailableAnalysis.find(AID);
40 if (I != AvailableAnalysis.end())
46 /// Augment RequiredAnalysis by adding analysis required by pass P.
47 void noteDownRequiredAnalysis(Pass *P);
49 /// Augment AvailableAnalysis by adding analysis made available by pass P.
50 void noteDownAvailableAnalysis(Pass *P);
52 /// Remove Analysis that is not preserved by the pass
53 void removeNotPreservedAnalysis(Pass *P);
55 /// Remove dead passes
56 void removeDeadPasses(Pass *P);
58 /// Add pass P into the PassVector. Update RequiredAnalysis and
59 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
60 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
62 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
63 /// This is used before running passes managed by the manager.
64 void clearAnalysis() {
65 RequiredAnalysis.clear();
66 AvailableAnalysis.clear();
70 // All Required analyses should be available to the pass as it runs! Here
71 // we fill in the AnalysisImpls member of the pass so that it can
72 // successfully use the getAnalysis() method to retrieve the
73 // implementations it needs.
75 void initializeAnalysisImpl(Pass *P);
77 inline std::vector<Pass *>::iterator passVectorBegin() {
78 return PassVector.begin();
81 inline std::vector<Pass *>::iterator passVectorEnd() {
82 return PassVector.end();
85 inline void setLastUser(Pass *P, Pass *LU) {
87 // TODO : Check if pass P is available.
89 // Prolong live range of analyses that are needed after an analysis pass
90 // is destroyed, for querying by subsequent passes
91 AnalysisUsage AnUsage;
92 P->getAnalysisUsage(AnUsage);
93 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
94 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
95 E = IDs.end(); I != E; ++I) {
96 Pass *AnalysisPass = getAnalysisPass(*I); // getAnalysisPassFromManager(*I);
97 assert (AnalysisPass && "Analysis pass is not available");
98 setLastUser(AnalysisPass, LU);
104 // Analysis required by the passes managed by this manager. This information
105 // used while selecting pass manager during addPass. If a pass does not
106 // preserve any analysis required by other passes managed by current
107 // pass manager then new pass manager is used.
108 std::vector<AnalysisID> RequiredAnalysis;
110 // Set of available Analysis. This information is used while scheduling
111 // pass. If a pass requires an analysis which is not not available then
112 // equired analysis pass is scheduled to run before the pass itself is
114 std::map<AnalysisID, Pass*> AvailableAnalysis;
116 // Map to keep track of last user of the analysis pass.
117 // LastUser->second is the last user of Lastuser->first.
118 std::map<Pass *, Pass *> LastUser;
120 // Collection of pass that are managed by this manager
121 std::vector<Pass *> PassVector;
124 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
125 /// pass together and sequence them to process one basic block before
126 /// processing next basic block.
127 class BasicBlockPassManager_New : public CommonPassManagerImpl {
130 BasicBlockPassManager_New() { }
132 /// Add a pass into a passmanager queue.
133 bool addPass(Pass *p);
135 /// Execute all of the passes scheduled for execution. Keep track of
136 /// whether any of the passes modifies the function, and if so, return true.
137 bool runOnFunction(Function &F);
139 /// Return true IFF AnalysisID AID is currently available.
140 Pass *getAnalysisPassFromManager(AnalysisID AID);
145 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
146 /// It batches all function passes and basic block pass managers together and
147 /// sequence them to process one function at a time before processing next
149 class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
151 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
152 FunctionPassManagerImpl_New() {
153 activeBBPassManager = NULL;
155 ~FunctionPassManagerImpl_New() { /* TODO */ };
157 /// add - Add a pass to the queue of passes to run. This passes
158 /// ownership of the Pass to the PassManager. When the
159 /// PassManager_X is destroyed, the pass will be destroyed as well, so
160 /// there is no need to delete the pass. (TODO delete passes.)
161 /// This implies that all passes MUST be allocated with 'new'.
162 void add(Pass *P) { /* TODO*/ }
164 /// Add pass into the pass manager queue.
165 bool addPass(Pass *P);
167 /// Execute all of the passes scheduled for execution. Keep
168 /// track of whether any of the passes modifies the function, and if
170 bool runOnModule(Module &M);
172 /// Return true IFF AnalysisID AID is currently available.
173 Pass *getAnalysisPassFromManager(AnalysisID AID);
176 // Active Pass Managers
177 BasicBlockPassManager_New *activeBBPassManager;
180 /// ModulePassManager_New manages ModulePasses and function pass managers.
181 /// It batches all Module passes passes and function pass managers together and
182 /// sequence them to process one module.
183 class ModulePassManager_New : public CommonPassManagerImpl {
186 ModulePassManager_New() { activeFunctionPassManager = NULL; }
188 /// Add a pass into a passmanager queue.
189 bool addPass(Pass *p);
191 /// run - Execute all of the passes scheduled for execution. Keep track of
192 /// whether any of the passes modifies the module, and if so, return true.
193 bool runOnModule(Module &M);
195 /// Return true IFF AnalysisID AID is currently available.
196 Pass *getAnalysisPassFromManager(AnalysisID AID);
199 // Active Pass Manager
200 FunctionPassManagerImpl_New *activeFunctionPassManager;
203 /// PassManager_New manages ModulePassManagers
204 class PassManagerImpl_New : public CommonPassManagerImpl {
208 /// add - Add a pass to the queue of passes to run. This passes ownership of
209 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
210 /// will be destroyed as well, so there is no need to delete the pass. This
211 /// implies that all passes MUST be allocated with 'new'.
214 /// run - Execute all of the passes scheduled for execution. Keep track of
215 /// whether any of the passes modifies the module, and if so, return true.
218 /// Return true IFF AnalysisID AID is currently available.
219 Pass *getAnalysisPassFromManager(AnalysisID AID);
223 /// Add a pass into a passmanager queue. This is used by schedulePasses
224 bool addPass(Pass *p);
226 /// Schedule pass P for execution. Make sure that passes required by
227 /// P are run before P is run. Update analysis info maintained by
228 /// the manager. Remove dead passes. This is a recursive function.
229 void schedulePass(Pass *P);
231 /// Schedule all passes collected in pass queue using add(). Add all the
232 /// schedule passes into various manager's queue using addPass().
233 void schedulePasses();
235 // Collection of pass managers
236 std::vector<ModulePassManager_New *> PassManagers;
238 // Active Pass Manager
239 ModulePassManager_New *activeManager;
242 } // End of llvm namespace
244 // CommonPassManagerImpl implementation
246 /// Return true IFF pass P's required analysis set does not required new
248 bool CommonPassManagerImpl::manageablePass(Pass *P) {
250 AnalysisUsage AnUsage;
251 P->getAnalysisUsage(AnUsage);
253 // If this pass is not preserving information that is required by the other
254 // passes managed by this manager then use new manager
255 if (!AnUsage.getPreservesAll()) {
256 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
257 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
258 E = RequiredAnalysis.end(); I != E; ++I) {
259 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
261 // This analysis is not preserved. Need new manager.
268 /// Augment RequiredAnalysis by adding analysis required by pass P.
269 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
270 AnalysisUsage AnUsage;
271 P->getAnalysisUsage(AnUsage);
272 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
274 // FIXME: What about duplicates ?
275 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
278 initializeAnalysisImpl(P);
281 /// Augement AvailableAnalysis by adding analysis made available by pass P.
282 void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
284 if (const PassInfo *PI = P->getPassInfo()) {
285 AvailableAnalysis[PI] = P;
287 //TODO This pass is the current implementation of all of the interfaces it
288 //TODO implements as well.
290 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
291 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
292 //TODO CurrentAnalyses[II[i]] = P;
296 /// Remove Analyss not preserved by Pass P
297 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
298 AnalysisUsage AnUsage;
299 P->getAnalysisUsage(AnUsage);
300 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
302 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
303 E = AvailableAnalysis.end(); I != E; ++I ) {
304 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
305 PreservedSet.end()) {
306 // Remove this analysis
307 std::map<AnalysisID, Pass*>::iterator J = I++;
308 AvailableAnalysis.erase(J);
313 /// Remove analysis passes that are not used any longer
314 void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
316 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
317 E = LastUser.end(); I !=E; ++I) {
318 if (I->second == P) {
319 Pass *deadPass = I->first;
320 deadPass->releaseMemory();
322 std::map<AnalysisID, Pass*>::iterator Pos =
323 AvailableAnalysis.find(deadPass->getPassInfo());
325 assert (Pos != AvailableAnalysis.end() &&
326 "Pass is not available");
327 AvailableAnalysis.erase(Pos);
332 /// Add pass P into the PassVector. Update RequiredAnalysis and
333 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
334 void CommonPassManagerImpl::addPassToManager (Pass *P,
335 bool ProcessAnalysis) {
337 if (ProcessAnalysis) {
338 // Take a note of analysis required and made available by this pass
339 noteDownRequiredAnalysis(P);
340 noteDownAvailableAnalysis(P);
342 // Remove the analysis not preserved by this pass
343 removeNotPreservedAnalysis(P);
347 PassVector.push_back(P);
350 // All Required analyses should be available to the pass as it runs! Here
351 // we fill in the AnalysisImpls member of the pass so that it can
352 // successfully use the getAnalysis() method to retrieve the
353 // implementations it needs.
355 void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
356 AnalysisUsage AnUsage;
357 P->getAnalysisUsage(AnUsage);
359 for (std::vector<const PassInfo *>::const_iterator
360 I = AnUsage.getRequiredSet().begin(),
361 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
362 Pass *Impl = getAnalysisPass(*I);
364 assert(0 && "Analysis used but not available!");
365 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
369 /// BasicBlockPassManager implementation
371 /// Add pass P into PassVector and return true. If this pass is not
372 /// manageable by this manager then return false.
374 BasicBlockPassManager_New::addPass(Pass *P) {
376 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
380 // If this pass does not preserve anlysis that is used by other passes
381 // managed by this manager than it is not a suiable pass for this manager.
382 if (!manageablePass(P))
385 addPassToManager (BP);
390 /// Execute all of the passes scheduled for execution by invoking
391 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
392 /// the function, and if so, return true.
394 BasicBlockPassManager_New::runOnFunction(Function &F) {
396 bool Changed = false;
399 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
400 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
401 e = passVectorEnd(); itr != e; ++itr) {
404 noteDownAvailableAnalysis(P);
405 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
406 Changed |= BP->runOnBasicBlock(*I);
407 removeNotPreservedAnalysis(P);
413 /// Return true IFF AnalysisID AID is currently available.
414 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
415 return getAnalysisPass(AID);
418 // FunctionPassManager_New implementation
419 /// Create new Function pass manager
420 FunctionPassManager_New::FunctionPassManager_New() {
421 FPM = new FunctionPassManagerImpl_New();
424 /// add - Add a pass to the queue of passes to run. This passes
425 /// ownership of the Pass to the PassManager. When the
426 /// PassManager_X is destroyed, the pass will be destroyed as well, so
427 /// there is no need to delete the pass. (TODO delete passes.)
428 /// This implies that all passes MUST be allocated with 'new'.
430 FunctionPassManager_New::add(Pass *P) {
434 /// Execute all of the passes scheduled for execution. Keep
435 /// track of whether any of the passes modifies the function, and if
438 FunctionPassManager_New::runOnModule(Module &M) {
439 return FPM->runOnModule(M);
442 // FunctionPassManagerImpl_New implementation
444 // FunctionPassManager
446 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
447 /// either use it into active basic block pass manager or create new basic
448 /// block pass manager to handle pass P.
450 FunctionPassManagerImpl_New::addPass(Pass *P) {
452 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
453 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
455 if (!activeBBPassManager
456 || !activeBBPassManager->addPass(BP)) {
458 activeBBPassManager = new BasicBlockPassManager_New();
459 addPassToManager(activeBBPassManager, false);
460 if (!activeBBPassManager->addPass(BP))
461 assert(0 && "Unable to add Pass");
466 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
470 // If this pass does not preserve anlysis that is used by other passes
471 // managed by this manager than it is not a suiable pass for this manager.
472 if (!manageablePass(P))
475 addPassToManager (FP);
476 activeBBPassManager = NULL;
480 /// Execute all of the passes scheduled for execution by invoking
481 /// runOnFunction method. Keep track of whether any of the passes modifies
482 /// the function, and if so, return true.
484 FunctionPassManagerImpl_New::runOnModule(Module &M) {
486 bool Changed = false;
489 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
490 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
491 e = passVectorEnd(); itr != e; ++itr) {
494 noteDownAvailableAnalysis(P);
495 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
496 Changed |= FP->runOnFunction(*I);
497 removeNotPreservedAnalysis(P);
503 /// Return true IFF AnalysisID AID is currently available.
504 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
506 Pass *P = getAnalysisPass(AID);
510 if (activeBBPassManager &&
511 activeBBPassManager->getAnalysisPass(AID) != 0)
512 return activeBBPassManager->getAnalysisPass(AID);
514 // TODO : Check inactive managers
518 // ModulePassManager implementation
520 /// Add P into pass vector if it is manageble. If P is a FunctionPass
521 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
522 /// is not manageable by this manager.
524 ModulePassManager_New::addPass(Pass *P) {
526 // If P is FunctionPass then use function pass maanager.
527 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
529 activeFunctionPassManager = NULL;
531 if (!activeFunctionPassManager
532 || !activeFunctionPassManager->addPass(P)) {
534 activeFunctionPassManager = new FunctionPassManagerImpl_New();
535 addPassToManager(activeFunctionPassManager, false);
536 if (!activeFunctionPassManager->addPass(FP))
537 assert(0 && "Unable to add pass");
542 ModulePass *MP = dynamic_cast<ModulePass *>(P);
546 // If this pass does not preserve anlysis that is used by other passes
547 // managed by this manager than it is not a suiable pass for this manager.
548 if (!manageablePass(P))
551 addPassToManager(MP);
552 activeFunctionPassManager = NULL;
557 /// Execute all of the passes scheduled for execution by invoking
558 /// runOnModule method. Keep track of whether any of the passes modifies
559 /// the module, and if so, return true.
561 ModulePassManager_New::runOnModule(Module &M) {
562 bool Changed = false;
565 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
566 e = passVectorEnd(); itr != e; ++itr) {
569 noteDownAvailableAnalysis(P);
570 ModulePass *MP = dynamic_cast<ModulePass*>(P);
571 Changed |= MP->runOnModule(M);
572 removeNotPreservedAnalysis(P);
578 /// Return true IFF AnalysisID AID is currently available.
579 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
582 Pass *P = getAnalysisPass(AID);
586 if (activeFunctionPassManager &&
587 activeFunctionPassManager->getAnalysisPass(AID) != 0)
588 return activeFunctionPassManager->getAnalysisPass(AID);
590 // TODO : Check inactive managers
594 /// Return true IFF AnalysisID AID is currently available.
595 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
598 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
599 e = PassManagers.end(); !P && itr != e; ++itr)
600 P = (*itr)->getAnalysisPassFromManager(AID);
604 /// Schedule pass P for execution. Make sure that passes required by
605 /// P are run before P is run. Update analysis info maintained by
606 /// the manager. Remove dead passes. This is a recursive function.
607 void PassManagerImpl_New::schedulePass(Pass *P) {
609 AnalysisUsage AnUsage;
610 P->getAnalysisUsage(AnUsage);
611 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
612 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
613 E = RequiredSet.end(); I != E; ++I) {
615 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
617 // Schedule this analysis run first.
618 AnalysisPass = (*I)->createPass();
619 schedulePass(AnalysisPass);
621 setLastUser (AnalysisPass, P);
627 /// Schedule all passes from the queue by adding them in their
628 /// respective manager's queue.
629 void PassManagerImpl_New::schedulePasses() {
630 for (std::vector<Pass *>::iterator I = passVectorBegin(),
631 E = passVectorEnd(); I != E; ++I)
635 /// Add pass P to the queue of passes to run.
636 void PassManagerImpl_New::add(Pass *P) {
637 // Do not process Analysis now. Analysis is process while scheduling
639 addPassToManager(P, false);
642 // PassManager_New implementation
643 /// Add P into active pass manager or use new module pass manager to
645 bool PassManagerImpl_New::addPass(Pass *P) {
647 if (!activeManager || !activeManager->addPass(P)) {
648 activeManager = new ModulePassManager_New();
649 PassManagers.push_back(activeManager);
652 return activeManager->addPass(P);
655 /// run - Execute all of the passes scheduled for execution. Keep track of
656 /// whether any of the passes modifies the module, and if so, return true.
657 bool PassManagerImpl_New::run(Module &M) {
660 bool Changed = false;
661 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
662 e = PassManagers.end(); itr != e; ++itr) {
663 ModulePassManager_New *pm = *itr;
664 Changed |= pm->runOnModule(M);
669 /// Create new pass manager
670 PassManager_New::PassManager_New() {
671 PM = new PassManagerImpl_New();
674 /// add - Add a pass to the queue of passes to run. This passes ownership of
675 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
676 /// will be destroyed as well, so there is no need to delete the pass. This
677 /// implies that all passes MUST be allocated with 'new'.
679 PassManager_New::add(Pass *P) {
683 /// run - Execute all of the passes scheduled for execution. Keep track of
684 /// whether any of the passes modifies the module, and if so, return true.
686 PassManager_New::run(Module &M) {