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/Support/CommandLine.h"
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/Support/Streams.h"
24 //===----------------------------------------------------------------------===//
26 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
28 // o Manage optimization pass execution order
29 // o Make required Analysis information available before pass P is run
30 // o Release memory occupied by dead passes
31 // o If Analysis information is dirtied by a pass then regenerate Analysis
32 // information before it is consumed by another pass.
34 // Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
35 // FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class
36 // hierarcy uses multiple inheritance but pass managers do not derive from
37 // another pass manager.
39 // PassManager and FunctionPassManager are two top level pass manager that
40 // represents the external interface of this entire pass manager infrastucture.
42 // Important classes :
44 // [o] class PMTopLevelManager;
46 // Two top level managers, PassManager and FunctionPassManager, derive from
47 // PMTopLevelManager. PMTopLevelManager manages information used by top level
48 // managers such as last user info.
50 // [o] class PMDataManager;
52 // PMDataManager manages information, e.g. list of available analysis info,
53 // used by a pass manager to manage execution order of passes. It also provides
54 // a place to implement common pass manager APIs. All pass managers derive from
57 // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
59 // BasicBlockPassManager manages BasicBlockPasses.
61 // [o] class FunctionPassManager;
63 // This is a external interface used by JIT to manage FunctionPasses. This
64 // interface relies on FunctionPassManagerImpl to do all the tasks.
66 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
67 // public PMTopLevelManager;
69 // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
70 // and BasicBlockPassManagers.
72 // [o] class ModulePassManager : public Pass, public PMDataManager;
74 // ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
76 // [o] class PassManager;
78 // This is a external interface used by various tools to manages passes. It
79 // relies on PassManagerImpl to do all the tasks.
81 // [o] class PassManagerImpl : public Pass, public PMDataManager,
82 // public PMDTopLevelManager
84 // PassManagerImpl is a top level pass manager responsible for managing
85 // ModulePassManagers.
86 //===----------------------------------------------------------------------===//
90 //===----------------------------------------------------------------------===//
91 // Pass debugging information. Often it is useful to find out what pass is
92 // running when a crash occurs in a utility. When this library is compiled with
93 // debugging on, a command line option (--debug-pass) is enabled that causes the
94 // pass name to be printed before it executes.
97 // Different debug levels that can be enabled...
99 None, Arguments, Structure, Executions, Details
102 static cl::opt<enum PassDebugLevel>
103 PassDebugging_New("debug-pass", cl::Hidden,
104 cl::desc("Print PassManager debugging information"),
106 clEnumVal(None , "disable debug output"),
107 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
108 clEnumVal(Structure , "print pass structure before run()"),
109 clEnumVal(Executions, "print pass name before it is executed"),
110 clEnumVal(Details , "print pass details when it is executed"),
112 } // End of llvm namespace
114 #ifndef USE_OLD_PASSMANAGER
119 //===----------------------------------------------------------------------===//
122 /// PMTopLevelManager manages LastUser info and collects common APIs used by
123 /// top level pass managers.
124 class PMTopLevelManager {
128 inline std::vector<Pass *>::iterator passManagersBegin() {
129 return PassManagers.begin();
132 inline std::vector<Pass *>::iterator passManagersEnd() {
133 return PassManagers.end();
136 /// Schedule pass P for execution. Make sure that passes required by
137 /// P are run before P is run. Update analysis info maintained by
138 /// the manager. Remove dead passes. This is a recursive function.
139 void schedulePass(Pass *P);
141 /// This is implemented by top level pass manager and used by
142 /// schedulePass() to add analysis info passes that are not available.
143 virtual void addTopLevelPass(Pass *P) = 0;
145 /// Set pass P as the last user of the given analysis passes.
146 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
148 /// Collect passes whose last user is P
149 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
151 /// Find the pass that implements Analysis AID. Search immutable
152 /// passes and all pass managers. If desired pass is not found
153 /// then return NULL.
154 Pass *findAnalysisPass(AnalysisID AID);
156 inline void clearManagers() {
157 PassManagers.clear();
160 virtual ~PMTopLevelManager() {
162 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
163 E = PassManagers.end(); I != E; ++I)
166 for (std::vector<ImmutablePass *>::iterator
167 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
170 PassManagers.clear();
173 /// Add immutable pass and initialize it.
174 inline void addImmutablePass(ImmutablePass *P) {
176 ImmutablePasses.push_back(P);
179 inline std::vector<ImmutablePass *>& getImmutablePasses() {
180 return ImmutablePasses;
183 void addPassManager(Pass *Manager) {
184 PassManagers.push_back(Manager);
187 // Add Manager into the list of managers that are not directly
188 // maintained by this top level pass manager
189 inline void addIndirectPassManager(PMDataManager *Manager) {
190 IndirectPassManagers.push_back(Manager);
193 // Print passes managed by this top level manager.
195 void dumpArguments();
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
202 /// Collection of pass managers that are not directly maintained
203 /// by this pass manager
204 std::vector<PMDataManager *> IndirectPassManagers;
206 // Map to keep track of last user of the analysis pass.
207 // LastUser->second is the last user of Lastuser->first.
208 std::map<Pass *, Pass *> LastUser;
210 /// Immutable passes are managed by top level manager.
211 std::vector<ImmutablePass *> ImmutablePasses;
214 //===----------------------------------------------------------------------===//
217 /// PMDataManager provides the common place to manage the analysis data
218 /// used by pass managers.
219 class PMDataManager {
223 PMDataManager(int D) : TPM(NULL), Depth(D) {
224 initializeAnalysisInfo();
227 virtual ~PMDataManager() {
229 for (std::vector<Pass *>::iterator I = PassVector.begin(),
230 E = PassVector.end(); I != E; ++I)
236 /// Return true IFF pass P's required analysis set does not required new
238 bool manageablePass(Pass *P);
240 /// Augment AvailableAnalysis by adding analysis made available by pass P.
241 void recordAvailableAnalysis(Pass *P);
243 /// Remove Analysis that is not preserved by the pass
244 void removeNotPreservedAnalysis(Pass *P);
246 /// Remove dead passes
247 void removeDeadPasses(Pass *P, std::string &Msg);
249 /// Add pass P into the PassVector. Update
250 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
251 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
253 /// Initialize available analysis information.
254 void initializeAnalysisInfo() {
255 ForcedLastUses.clear();
256 AvailableAnalysis.clear();
259 /// Populate RequiredPasses with the analysis pass that are required by
261 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
264 /// All Required analyses should be available to the pass as it runs! Here
265 /// we fill in the AnalysisImpls member of the pass so that it can
266 /// successfully use the getAnalysis() method to retrieve the
267 /// implementations it needs.
268 void initializeAnalysisImpl(Pass *P);
270 /// Find the pass that implements Analysis AID. If desired pass is not found
271 /// then return NULL.
272 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
274 inline std::vector<Pass *>::iterator passVectorBegin() {
275 return PassVector.begin();
278 inline std::vector<Pass *>::iterator passVectorEnd() {
279 return PassVector.end();
282 // Access toplevel manager
283 PMTopLevelManager *getTopLevelManager() { return TPM; }
284 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
286 unsigned getDepth() { return Depth; }
288 // Print list of passes that are last used by P.
289 void dumpLastUses(Pass *P, unsigned Offset) {
291 std::vector<Pass *> LUses;
293 assert (TPM && "Top Level Manager is missing");
294 TPM->collectLastUses(LUses, P);
296 for (std::vector<Pass *>::iterator I = LUses.begin(),
297 E = LUses.end(); I != E; ++I) {
298 llvm::cerr << "--" << std::string(Offset*2, ' ');
299 (*I)->dumpPassStructure(0);
303 void dumpPassArguments() {
304 for(std::vector<Pass *>::iterator I = PassVector.begin(),
305 E = PassVector.end(); I != E; ++I) {
306 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
307 PMD->dumpPassArguments();
309 if (const PassInfo *PI = (*I)->getPassInfo())
310 if (!PI->isAnalysisGroup())
311 cerr << " -" << PI->getPassArgument();
315 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) {
316 if (PassDebugging_New < Executions)
318 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
320 cerr << P->getPassName();
324 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
325 const std::vector<AnalysisID> &Set) {
326 if (PassDebugging_New >= Details && !Set.empty()) {
327 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
328 for (unsigned i = 0; i != Set.size(); ++i) {
330 cerr << " " << Set[i]->getPassName();
337 // Collection of pass whose last user asked this manager to claim
338 // last use. If a FunctionPass F is the last user of ModulePass info M
339 // then the F's manager, not F, records itself as a last user of M.
340 std::vector<Pass *> ForcedLastUses;
342 // Top level manager.
343 PMTopLevelManager *TPM;
346 // Set of available Analysis. This information is used while scheduling
347 // pass. If a pass requires an analysis which is not not available then
348 // equired analysis pass is scheduled to run before the pass itself is
350 std::map<AnalysisID, Pass*> AvailableAnalysis;
352 // Collection of pass that are managed by this manager
353 std::vector<Pass *> PassVector;
358 //===----------------------------------------------------------------------===//
359 // BasicBlockPassManager
361 /// BasicBlockPassManager manages BasicBlockPass. It batches all the
362 /// pass together and sequence them to process one basic block before
363 /// processing next basic block.
364 class BasicBlockPassManager : public PMDataManager,
365 public FunctionPass {
368 BasicBlockPassManager(int D) : PMDataManager(D) { }
370 /// Add a pass into a passmanager queue.
371 bool addPass(Pass *p);
373 /// Execute all of the passes scheduled for execution. Keep track of
374 /// whether any of the passes modifies the function, and if so, return true.
375 bool runOnFunction(Function &F);
377 /// Pass Manager itself does not invalidate any analysis info.
378 void getAnalysisUsage(AnalysisUsage &Info) const {
379 Info.setPreservesAll();
382 bool doInitialization(Module &M);
383 bool doInitialization(Function &F);
384 bool doFinalization(Module &M);
385 bool doFinalization(Function &F);
387 // Print passes managed by this manager
388 void dumpPassStructure(unsigned Offset) {
389 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
390 for (std::vector<Pass *>::iterator I = passVectorBegin(),
391 E = passVectorEnd(); I != E; ++I) {
392 (*I)->dumpPassStructure(Offset + 1);
393 dumpLastUses(*I, Offset+1);
398 //===----------------------------------------------------------------------===//
399 // FunctionPassManagerImpl_New
401 /// FunctionPassManagerImpl_New manages FunctionPasses and
402 /// BasicBlockPassManagers. It batches all function passes and basic block pass
403 /// managers together and sequence them to process one function at a time before
404 /// processing next function.
405 class FunctionPassManagerImpl_New : public ModulePass,
406 public PMDataManager,
407 public PMTopLevelManager {
409 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
410 activeBBPassManager = NULL;
412 ~FunctionPassManagerImpl_New() { /* TODO */ };
414 inline void addTopLevelPass(Pass *P) {
416 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
418 // P is a immutable pass then it will be managed by this
419 // top level manager. Set up analysis resolver to connect them.
420 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
422 initializeAnalysisImpl(P);
423 addImmutablePass(IP);
424 recordAvailableAnalysis(IP);
430 /// add - Add a pass to the queue of passes to run. This passes
431 /// ownership of the Pass to the PassManager. When the
432 /// PassManager_X is destroyed, the pass will be destroyed as well, so
433 /// there is no need to delete the pass. (TODO delete passes.)
434 /// This implies that all passes MUST be allocated with 'new'.
439 /// Add pass into the pass manager queue.
440 bool addPass(Pass *P);
442 /// Execute all of the passes scheduled for execution. Keep
443 /// track of whether any of the passes modifies the function, and if
445 bool runOnModule(Module &M);
446 bool runOnFunction(Function &F);
447 bool run(Function &F);
449 /// doInitialization - Run all of the initializers for the function passes.
451 bool doInitialization(Module &M);
453 /// doFinalization - Run all of the initializers for the function passes.
455 bool doFinalization(Module &M);
457 /// Pass Manager itself does not invalidate any analysis info.
458 void getAnalysisUsage(AnalysisUsage &Info) const {
459 Info.setPreservesAll();
462 // Print passes managed by this manager
463 void dumpPassStructure(unsigned Offset) {
464 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
465 for (std::vector<Pass *>::iterator I = passVectorBegin(),
466 E = passVectorEnd(); I != E; ++I) {
467 (*I)->dumpPassStructure(Offset + 1);
468 dumpLastUses(*I, Offset+1);
473 // Active Pass Managers
474 BasicBlockPassManager *activeBBPassManager;
477 //===----------------------------------------------------------------------===//
480 /// ModulePassManager manages ModulePasses and function pass managers.
481 /// It batches all Module passes passes and function pass managers together and
482 /// sequence them to process one module.
483 class ModulePassManager : public Pass,
484 public PMDataManager {
487 ModulePassManager(int D) : PMDataManager(D) {
488 activeFunctionPassManager = NULL;
491 /// Add a pass into a passmanager queue.
492 bool addPass(Pass *p);
494 /// run - Execute all of the passes scheduled for execution. Keep track of
495 /// whether any of the passes modifies the module, and if so, return true.
496 bool runOnModule(Module &M);
498 /// Pass Manager itself does not invalidate any analysis info.
499 void getAnalysisUsage(AnalysisUsage &Info) const {
500 Info.setPreservesAll();
503 // Print passes managed by this manager
504 void dumpPassStructure(unsigned Offset) {
505 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
506 for (std::vector<Pass *>::iterator I = passVectorBegin(),
507 E = passVectorEnd(); I != E; ++I) {
508 (*I)->dumpPassStructure(Offset + 1);
509 dumpLastUses(*I, Offset+1);
514 // Active Pass Manager
515 FunctionPassManagerImpl_New *activeFunctionPassManager;
518 //===----------------------------------------------------------------------===//
519 // PassManagerImpl_New
521 /// PassManagerImpl_New manages ModulePassManagers
522 class PassManagerImpl_New : public Pass,
523 public PMDataManager,
524 public PMTopLevelManager {
528 PassManagerImpl_New(int D) : PMDataManager(D) {
529 activeManager = NULL;
532 /// add - Add a pass to the queue of passes to run. This passes ownership of
533 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
534 /// will be destroyed as well, so there is no need to delete the pass. This
535 /// implies that all passes MUST be allocated with 'new'.
540 /// run - Execute all of the passes scheduled for execution. Keep track of
541 /// whether any of the passes modifies the module, and if so, return true.
544 /// Pass Manager itself does not invalidate any analysis info.
545 void getAnalysisUsage(AnalysisUsage &Info) const {
546 Info.setPreservesAll();
549 inline void addTopLevelPass(Pass *P) {
551 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
553 // P is a immutable pass and it will be managed by this
554 // top level manager. Set up analysis resolver to connect them.
555 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
557 initializeAnalysisImpl(P);
558 addImmutablePass(IP);
559 recordAvailableAnalysis(IP);
567 /// Add a pass into a passmanager queue.
568 bool addPass(Pass *p);
570 // Active Pass Manager
571 ModulePassManager *activeManager;
574 } // End of llvm namespace
576 //===----------------------------------------------------------------------===//
577 // PMTopLevelManager implementation
579 /// Set pass P as the last user of the given analysis passes.
580 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
583 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
584 E = AnalysisPasses.end(); I != E; ++I) {
587 // If AP is the last user of other passes then make P last user of
589 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
590 LUE = LastUser.end(); LUI != LUE; ++LUI) {
591 if (LUI->second == AP)
592 LastUser[LUI->first] = P;
597 /// Collect passes whose last user is P
598 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
600 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
601 LUE = LastUser.end(); LUI != LUE; ++LUI)
602 if (LUI->second == P)
603 LastUses.push_back(LUI->first);
606 /// Schedule pass P for execution. Make sure that passes required by
607 /// P are run before P is run. Update analysis info maintained by
608 /// the manager. Remove dead passes. This is a recursive function.
609 void PMTopLevelManager::schedulePass(Pass *P) {
611 // TODO : Allocate function manager for this pass, other wise required set
612 // may be inserted into previous function manager
614 AnalysisUsage AnUsage;
615 P->getAnalysisUsage(AnUsage);
616 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
617 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
618 E = RequiredSet.end(); I != E; ++I) {
620 Pass *AnalysisPass = findAnalysisPass(*I);
622 // Schedule this analysis run first.
623 AnalysisPass = (*I)->createPass();
624 schedulePass(AnalysisPass);
628 // Now all required passes are available.
632 /// Find the pass that implements Analysis AID. Search immutable
633 /// passes and all pass managers. If desired pass is not found
634 /// then return NULL.
635 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
638 // Check pass managers
639 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
640 E = PassManagers.end(); P == NULL && I != E; ++I) {
641 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
642 assert(PMD && "This is not a PassManager");
643 P = PMD->findAnalysisPass(AID, false);
646 // Check other pass managers
647 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
648 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
649 P = (*I)->findAnalysisPass(AID, false);
651 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
652 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
653 const PassInfo *PI = (*I)->getPassInfo();
657 // If Pass not found then check the interfaces implemented by Immutable Pass
659 const std::vector<const PassInfo*> &ImmPI =
660 PI->getInterfacesImplemented();
661 for (unsigned Index = 0, End = ImmPI.size();
662 P == NULL && Index != End; ++Index)
663 if (ImmPI[Index] == AID)
671 // Print passes managed by this top level manager.
672 void PMTopLevelManager::dumpPasses() {
674 // Print out the immutable passes
675 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
676 ImmutablePasses[i]->dumpPassStructure(0);
679 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
680 E = PassManagers.end(); I != E; ++I)
681 (*I)->dumpPassStructure(1);
685 void PMTopLevelManager::dumpArguments() {
687 if (PassDebugging_New < Arguments)
690 cerr << "Pass Arguments: ";
691 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
692 E = PassManagers.end(); I != E; ++I) {
693 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
694 assert(PMD && "This is not a PassManager");
695 PMD->dumpPassArguments();
700 //===----------------------------------------------------------------------===//
701 // PMDataManager implementation
703 /// Return true IFF pass P's required analysis set does not required new
705 bool PMDataManager::manageablePass(Pass *P) {
708 // If this pass is not preserving information that is required by a
709 // pass maintained by higher level pass manager then do not insert
710 // this pass into current manager. Use new manager. For example,
711 // For example, If FunctionPass F is not preserving ModulePass Info M1
712 // that is used by another ModulePass M2 then do not insert F in
713 // current function pass manager.
717 /// Augement AvailableAnalysis by adding analysis made available by pass P.
718 void PMDataManager::recordAvailableAnalysis(Pass *P) {
720 if (const PassInfo *PI = P->getPassInfo()) {
721 AvailableAnalysis[PI] = P;
723 //This pass is the current implementation of all of the interfaces it
724 //implements as well.
725 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
726 for (unsigned i = 0, e = II.size(); i != e; ++i)
727 AvailableAnalysis[II[i]] = P;
731 /// Remove Analyss not preserved by Pass P
732 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
733 AnalysisUsage AnUsage;
734 P->getAnalysisUsage(AnUsage);
736 if (AnUsage.getPreservesAll())
739 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
740 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
741 E = AvailableAnalysis.end(); I != E; ) {
742 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
743 PreservedSet.end()) {
744 // Remove this analysis
745 if (!dynamic_cast<ImmutablePass*>(I->second)) {
746 std::map<AnalysisID, Pass*>::iterator J = I++;
747 AvailableAnalysis.erase(J);
755 /// Remove analysis passes that are not used any longer
756 void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
758 std::vector<Pass *> DeadPasses;
759 TPM->collectLastUses(DeadPasses, P);
761 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
762 E = DeadPasses.end(); I != E; ++I) {
764 std::string Msg1 = " Freeing Pass '";
765 dumpPassInfo(*I, Msg1, Msg);
767 (*I)->releaseMemory();
769 std::map<AnalysisID, Pass*>::iterator Pos =
770 AvailableAnalysis.find((*I)->getPassInfo());
772 // It is possible that pass is already removed from the AvailableAnalysis
773 if (Pos != AvailableAnalysis.end())
774 AvailableAnalysis.erase(Pos);
778 /// Add pass P into the PassVector. Update
779 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
780 void PMDataManager::addPassToManager(Pass *P,
781 bool ProcessAnalysis) {
783 // This manager is going to manage pass P. Set up analysis resolver
785 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
788 if (ProcessAnalysis) {
790 // At the moment, this pass is the last user of all required passes.
791 std::vector<Pass *> LastUses;
792 std::vector<Pass *> RequiredPasses;
793 unsigned PDepth = this->getDepth();
795 collectRequiredAnalysisPasses(RequiredPasses, P);
796 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
797 E = RequiredPasses.end(); I != E; ++I) {
798 Pass *PRequired = *I;
801 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
802 RDepth = DM.getDepth();
804 if (PDepth == RDepth)
805 LastUses.push_back(PRequired);
806 else if (PDepth > RDepth) {
807 // Let the parent claim responsibility of last use
808 ForcedLastUses.push_back(PRequired);
810 // Note : This feature is not yet implemented
812 "Unable to handle Pass that requires lower level Analysis pass");
816 if (!LastUses.empty())
817 TPM->setLastUser(LastUses, P);
819 // Take a note of analysis required and made available by this pass.
820 // Remove the analysis not preserved by this pass
821 removeNotPreservedAnalysis(P);
822 recordAvailableAnalysis(P);
826 PassVector.push_back(P);
829 /// Populate RequiredPasses with the analysis pass that are required by
831 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
833 AnalysisUsage AnUsage;
834 P->getAnalysisUsage(AnUsage);
835 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
836 for (std::vector<AnalysisID>::const_iterator
837 I = RequiredSet.begin(), E = RequiredSet.end();
839 Pass *AnalysisPass = findAnalysisPass(*I, true);
840 assert (AnalysisPass && "Analysis pass is not available");
841 RP.push_back(AnalysisPass);
844 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
845 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
846 E = IDs.end(); I != E; ++I) {
847 Pass *AnalysisPass = findAnalysisPass(*I, true);
848 assert (AnalysisPass && "Analysis pass is not available");
849 RP.push_back(AnalysisPass);
853 // All Required analyses should be available to the pass as it runs! Here
854 // we fill in the AnalysisImpls member of the pass so that it can
855 // successfully use the getAnalysis() method to retrieve the
856 // implementations it needs.
858 void PMDataManager::initializeAnalysisImpl(Pass *P) {
859 AnalysisUsage AnUsage;
860 P->getAnalysisUsage(AnUsage);
862 for (std::vector<const PassInfo *>::const_iterator
863 I = AnUsage.getRequiredSet().begin(),
864 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
865 Pass *Impl = findAnalysisPass(*I, true);
867 assert(0 && "Analysis used but not available!");
868 AnalysisResolver_New *AR = P->getResolver();
869 AR->addAnalysisImplsPair(*I, Impl);
873 /// Find the pass that implements Analysis AID. If desired pass is not found
874 /// then return NULL.
875 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
877 // Check if AvailableAnalysis map has one entry.
878 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
880 if (I != AvailableAnalysis.end())
883 // Search Parents through TopLevelManager
885 return TPM->findAnalysisPass(AID);
891 //===----------------------------------------------------------------------===//
892 // NOTE: Is this the right place to define this method ?
893 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
894 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
895 return PM.findAnalysisPass(ID, dir);
898 //===----------------------------------------------------------------------===//
899 // BasicBlockPassManager implementation
901 /// Add pass P into PassVector and return true. If this pass is not
902 /// manageable by this manager then return false.
904 BasicBlockPassManager::addPass(Pass *P) {
906 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
910 // If this pass does not preserve anlysis that is used by other passes
911 // managed by this manager than it is not a suiable pass for this manager.
912 if (!manageablePass(P))
915 addPassToManager (BP);
920 /// Execute all of the passes scheduled for execution by invoking
921 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
922 /// the function, and if so, return true.
924 BasicBlockPassManager::runOnFunction(Function &F) {
929 bool Changed = doInitialization(F);
930 initializeAnalysisInfo();
932 std::string Msg1 = "Executing Pass '";
933 std::string Msg3 = "' Made Modification '";
935 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
936 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
937 e = passVectorEnd(); itr != e; ++itr) {
939 AnalysisUsage AnUsage;
940 P->getAnalysisUsage(AnUsage);
942 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
943 dumpPassInfo(P, Msg1, Msg2);
944 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
946 initializeAnalysisImpl(P);
948 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
949 Changed |= BP->runOnBasicBlock(*I);
952 dumpPassInfo(P, Msg3, Msg2);
953 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
955 removeNotPreservedAnalysis(P);
956 recordAvailableAnalysis(P);
957 removeDeadPasses(P, Msg2);
959 return Changed | doFinalization(F);
962 // Implement doInitialization and doFinalization
963 inline bool BasicBlockPassManager::doInitialization(Module &M) {
964 bool Changed = false;
966 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
967 e = passVectorEnd(); itr != e; ++itr) {
969 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
970 Changed |= BP->doInitialization(M);
976 inline bool BasicBlockPassManager::doFinalization(Module &M) {
977 bool Changed = false;
979 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
980 e = passVectorEnd(); itr != e; ++itr) {
982 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
983 Changed |= BP->doFinalization(M);
989 inline bool BasicBlockPassManager::doInitialization(Function &F) {
990 bool Changed = false;
992 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
993 e = passVectorEnd(); itr != e; ++itr) {
995 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
996 Changed |= BP->doInitialization(F);
1002 inline bool BasicBlockPassManager::doFinalization(Function &F) {
1003 bool Changed = false;
1005 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1006 e = passVectorEnd(); itr != e; ++itr) {
1008 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1009 Changed |= BP->doFinalization(F);
1016 //===----------------------------------------------------------------------===//
1017 // FunctionPassManager implementation
1019 /// Create new Function pass manager
1020 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1021 FPM = new FunctionPassManagerImpl_New(0);
1022 // FPM is the top level manager.
1023 FPM->setTopLevelManager(FPM);
1025 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1026 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
1027 FPM->setResolver(AR);
1029 FPM->addPassManager(FPM);
1033 FunctionPassManager::~FunctionPassManager() {
1034 // Note : FPM maintains one entry in PassManagers vector.
1035 // This one entry is FPM itself. This is not ideal. One
1036 // alternative is have one additional layer between
1037 // FunctionPassManager and FunctionPassManagerImpl.
1038 // Meanwhile, to avoid going into infinte loop, first
1039 // remove FPM from its PassMangers vector.
1040 FPM->clearManagers();
1044 /// add - Add a pass to the queue of passes to run. This passes
1045 /// ownership of the Pass to the PassManager. When the
1046 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1047 /// there is no need to delete the pass. (TODO delete passes.)
1048 /// This implies that all passes MUST be allocated with 'new'.
1049 void FunctionPassManager::add(Pass *P) {
1053 /// run - Execute all of the passes scheduled for execution. Keep
1054 /// track of whether any of the passes modifies the function, and if
1055 /// so, return true.
1057 bool FunctionPassManager::run(Function &F) {
1059 if (MP->materializeFunction(&F, &errstr)) {
1060 cerr << "Error reading bytecode file: " << errstr << "\n";
1067 /// doInitialization - Run all of the initializers for the function passes.
1069 bool FunctionPassManager::doInitialization() {
1070 return FPM->doInitialization(*MP->getModule());
1073 /// doFinalization - Run all of the initializers for the function passes.
1075 bool FunctionPassManager::doFinalization() {
1076 return FPM->doFinalization(*MP->getModule());
1079 //===----------------------------------------------------------------------===//
1080 // FunctionPassManagerImpl_New implementation
1082 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1083 /// either use it into active basic block pass manager or create new basic
1084 /// block pass manager to handle pass P.
1086 FunctionPassManagerImpl_New::addPass(Pass *P) {
1088 // If P is a BasicBlockPass then use BasicBlockPassManager.
1089 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1091 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
1093 // If active manager exists then clear its analysis info.
1094 if (activeBBPassManager)
1095 activeBBPassManager->initializeAnalysisInfo();
1097 // Create and add new manager
1098 activeBBPassManager =
1099 new BasicBlockPassManager(getDepth() + 1);
1100 // Inherit top level manager
1101 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
1103 // Add new manager into current manager's list.
1104 addPassToManager(activeBBPassManager, false);
1106 // Add new manager into top level manager's indirect passes list
1107 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1108 assert (PMD && "Manager is not Pass Manager");
1109 TPM->addIndirectPassManager(PMD);
1111 // Add pass into new manager. This time it must succeed.
1112 if (!activeBBPassManager->addPass(BP))
1113 assert(0 && "Unable to add Pass");
1116 if (!ForcedLastUses.empty())
1117 TPM->setLastUser(ForcedLastUses, this);
1122 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1126 // If this pass does not preserve anlysis that is used by other passes
1127 // managed by this manager than it is not a suiable pass for this manager.
1128 if (!manageablePass(P))
1131 addPassToManager (FP);
1133 // If active manager exists then clear its analysis info.
1134 if (activeBBPassManager) {
1135 activeBBPassManager->initializeAnalysisInfo();
1136 activeBBPassManager = NULL;
1142 /// Execute all of the passes scheduled for execution by invoking
1143 /// runOnFunction method. Keep track of whether any of the passes modifies
1144 /// the function, and if so, return true.
1145 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
1147 bool Changed = doInitialization(M);
1148 initializeAnalysisInfo();
1150 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1151 this->runOnFunction(*I);
1153 return Changed | doFinalization(M);
1156 /// Execute all of the passes scheduled for execution by invoking
1157 /// runOnFunction method. Keep track of whether any of the passes modifies
1158 /// the function, and if so, return true.
1159 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1161 bool Changed = false;
1166 initializeAnalysisInfo();
1168 std::string Msg1 = "Executing Pass '";
1169 std::string Msg3 = "' Made Modification '";
1171 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1172 e = passVectorEnd(); itr != e; ++itr) {
1174 AnalysisUsage AnUsage;
1175 P->getAnalysisUsage(AnUsage);
1177 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1178 dumpPassInfo(P, Msg1, Msg2);
1179 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
1181 initializeAnalysisImpl(P);
1182 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1183 Changed |= FP->runOnFunction(F);
1186 dumpPassInfo(P, Msg3, Msg2);
1187 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
1189 removeNotPreservedAnalysis(P);
1190 recordAvailableAnalysis(P);
1191 removeDeadPasses(P, Msg2);
1197 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1198 bool Changed = false;
1200 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1201 e = passVectorEnd(); itr != e; ++itr) {
1204 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1205 Changed |= FP->doInitialization(M);
1211 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1212 bool Changed = false;
1214 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1215 e = passVectorEnd(); itr != e; ++itr) {
1218 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1219 Changed |= FP->doFinalization(M);
1225 // Execute all the passes managed by this top level manager.
1226 // Return true if any function is modified by a pass.
1227 bool FunctionPassManagerImpl_New::run(Function &F) {
1229 bool Changed = false;
1230 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1231 E = passManagersEnd(); I != E; ++I) {
1232 FunctionPassManagerImpl_New *FP =
1233 dynamic_cast<FunctionPassManagerImpl_New *>(*I);
1234 Changed |= FP->runOnFunction(F);
1239 //===----------------------------------------------------------------------===//
1240 // ModulePassManager implementation
1242 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1243 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
1244 /// is not manageable by this manager.
1246 ModulePassManager::addPass(Pass *P) {
1248 // If P is FunctionPass then use function pass maanager.
1249 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1251 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1253 // If active manager exists then clear its analysis info.
1254 if (activeFunctionPassManager)
1255 activeFunctionPassManager->initializeAnalysisInfo();
1257 // Create and add new manager
1258 activeFunctionPassManager =
1259 new FunctionPassManagerImpl_New(getDepth() + 1);
1261 // Add new manager into current manager's list
1262 addPassToManager(activeFunctionPassManager, false);
1264 // Inherit top level manager
1265 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1267 // Add new manager into top level manager's indirect passes list
1268 PMDataManager *PMD =
1269 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1270 assert(PMD && "Manager is not Pass Manager");
1271 TPM->addIndirectPassManager(PMD);
1273 // Add pass into new manager. This time it must succeed.
1274 if (!activeFunctionPassManager->addPass(FP))
1275 assert(0 && "Unable to add pass");
1278 if (!ForcedLastUses.empty())
1279 TPM->setLastUser(ForcedLastUses, this);
1284 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1288 // If this pass does not preserve anlysis that is used by other passes
1289 // managed by this manager than it is not a suiable pass for this manager.
1290 if (!manageablePass(P))
1293 addPassToManager(MP);
1294 // If active manager exists then clear its analysis info.
1295 if (activeFunctionPassManager) {
1296 activeFunctionPassManager->initializeAnalysisInfo();
1297 activeFunctionPassManager = NULL;
1304 /// Execute all of the passes scheduled for execution by invoking
1305 /// runOnModule method. Keep track of whether any of the passes modifies
1306 /// the module, and if so, return true.
1308 ModulePassManager::runOnModule(Module &M) {
1309 bool Changed = false;
1310 initializeAnalysisInfo();
1312 std::string Msg1 = "Executing Pass '";
1313 std::string Msg3 = "' Made Modification '";
1315 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1316 e = passVectorEnd(); itr != e; ++itr) {
1318 AnalysisUsage AnUsage;
1319 P->getAnalysisUsage(AnUsage);
1321 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1322 dumpPassInfo(P, Msg1, Msg2);
1323 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
1325 initializeAnalysisImpl(P);
1326 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1327 Changed |= MP->runOnModule(M);
1330 dumpPassInfo(P, Msg3, Msg2);
1331 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
1333 removeNotPreservedAnalysis(P);
1334 recordAvailableAnalysis(P);
1335 removeDeadPasses(P, Msg2);
1340 //===----------------------------------------------------------------------===//
1341 // PassManagerImpl implementation
1343 /// Add P into active pass manager or use new module pass manager to
1345 bool PassManagerImpl_New::addPass(Pass *P) {
1347 if (!activeManager || !activeManager->addPass(P)) {
1348 activeManager = new ModulePassManager(getDepth() + 1);
1349 // Inherit top level manager
1350 activeManager->setTopLevelManager(this->getTopLevelManager());
1352 // This top level manager is going to manage activeManager.
1353 // Set up analysis resolver to connect them.
1354 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1355 activeManager->setResolver(AR);
1357 addPassManager(activeManager);
1358 return activeManager->addPass(P);
1363 /// run - Execute all of the passes scheduled for execution. Keep track of
1364 /// whether any of the passes modifies the module, and if so, return true.
1365 bool PassManagerImpl_New::run(Module &M) {
1367 bool Changed = false;
1370 if (PassDebugging_New >= Structure)
1373 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1374 E = passManagersEnd(); I != E; ++I) {
1375 ModulePassManager *MP = dynamic_cast<ModulePassManager *>(*I);
1376 Changed |= MP->runOnModule(M);
1381 //===----------------------------------------------------------------------===//
1382 // PassManager implementation
1384 /// Create new pass manager
1385 PassManager::PassManager() {
1386 PM = new PassManagerImpl_New(0);
1387 // PM is the top level manager
1388 PM->setTopLevelManager(PM);
1391 PassManager::~PassManager() {
1395 /// add - Add a pass to the queue of passes to run. This passes ownership of
1396 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1397 /// will be destroyed as well, so there is no need to delete the pass. This
1398 /// implies that all passes MUST be allocated with 'new'.
1400 PassManager::add(Pass *P) {
1404 /// run - Execute all of the passes scheduled for execution. Keep track of
1405 /// whether any of the passes modifies the module, and if so, return true.
1407 PassManager::run(Module &M) {