Add doInitialization and doFinalization methods to ModulePass's, to allow them to be re-initialized and reused on multiple Module's.

Patch by Pedro Artigas.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168008 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/Pass.h b/include/llvm/Pass.h
index cd651db..4f701d8 100644
--- a/include/llvm/Pass.h
+++ b/include/llvm/Pass.h
@@ -227,10 +227,20 @@
   /// createPrinterPass - Get a module printer pass.
   Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
 
+  /// doInitialization - Virtual method overridden by subclasses to do
+  /// any necessary initialization.
+  ///
+  virtual bool doInitialization(void)  { return false; }
+
   /// runOnModule - Virtual method overriden by subclasses to process the module
   /// being operated on.
   virtual bool runOnModule(Module &M) = 0;
 
+  /// doFinalization - Virtual method overriden by subclasses to do any post
+  /// processing needed after all passes have run.
+  ///
+  virtual bool doFinalization(void) { return false; }
+
   virtual void assignPassManager(PMStack &PMS,
                                  PassManagerType T);
 
diff --git a/include/llvm/PassManager.h b/include/llvm/PassManager.h
index ce5fda7..1d5e800 100644
--- a/include/llvm/PassManager.h
+++ b/include/llvm/PassManager.h
@@ -58,6 +58,14 @@
   /// whether any of the passes modifies the module, and if so, return true.
   bool run(Module &M);
 
+  /// doInitialization - Run all of the initializers for the module passes.
+  ///
+  bool doInitialization();
+
+  /// doFinalization - Run all of the finalizers for the module passes.
+  ///
+  bool doFinalization();
+
 private:
   /// PassManagerImpl_New is the actual class. PassManager is just the
   /// wraper to publish simple pass manager interface
diff --git a/include/llvm/PassManagers.h b/include/llvm/PassManagers.h
index 014df7d..b0450f3 100644
--- a/include/llvm/PassManagers.h
+++ b/include/llvm/PassManagers.h
@@ -420,10 +420,20 @@
   /// cleanup - After running all passes, clean up pass manager cache.
   void cleanup();
 
+  /// doInitialization - Overrides ModulePass doInitialization for global
+  /// initialization tasks
+  ///
+  using ModulePass::doInitialization;
+
   /// doInitialization - Run all of the initializers for the function passes.
   ///
   bool doInitialization(Module &M);
 
+  /// doFinalization - Overrides ModulePass doFinalization for global
+  /// finalization tasks
+  /// 
+  using ModulePass::doFinalization;
+  
   /// doFinalization - Run all of the finalizers for the function passes.
   ///
   bool doFinalization(Module &M);
diff --git a/lib/Analysis/IPA/CallGraphSCCPass.cpp b/lib/Analysis/IPA/CallGraphSCCPass.cpp
index 449b7ee..f486937 100644
--- a/lib/Analysis/IPA/CallGraphSCCPass.cpp
+++ b/lib/Analysis/IPA/CallGraphSCCPass.cpp
@@ -51,6 +51,9 @@
   /// whether any of the passes modifies the module, and if so, return true.
   bool runOnModule(Module &M);
 
+  using ModulePass::doInitialization;
+  using ModulePass::doFinalization;
+
   bool doInitialization(CallGraph &CG);
   bool doFinalization(CallGraph &CG);
 
diff --git a/lib/VMCore/PassManager.cpp b/lib/VMCore/PassManager.cpp
index 53f1149..b70a27e 100644
--- a/lib/VMCore/PassManager.cpp
+++ b/lib/VMCore/PassManager.cpp
@@ -309,6 +309,14 @@
   /// whether any of the passes modifies the module, and if so, return true.
   bool runOnModule(Module &M);
 
+  /// doInitialization - Run all of the initializers for the module passes.
+  ///
+  bool doInitialization(void);
+
+  /// doFinalization - Run all of the finalizers for the module passes.
+  ///
+  bool doFinalization(void);
+
   /// Pass Manager itself does not invalidate any analysis info.
   void getAnalysisUsage(AnalysisUsage &Info) const {
     Info.setPreservesAll();
@@ -394,6 +402,14 @@
   /// whether any of the passes modifies the module, and if so, return true.
   bool run(Module &M);
 
+  /// doInitialization - Run all of the initializers for the module passes.
+  ///
+  bool doInitialization(void);
+
+  /// doFinalization - Run all of the finalizers for the module passes.
+  ///
+  bool doFinalization(void);
+
   /// Pass Manager itself does not invalidate any analysis info.
   void getAnalysisUsage(AnalysisUsage &Info) const {
     Info.setPreservesAll();
@@ -1594,6 +1610,29 @@
     FPP->releaseMemoryOnTheFly();
     Changed |= FPP->doFinalization(M);
   }
+
+  return Changed;
+}
+
+/// Run all of the initializers for the module passes.
+///
+bool MPPassManager::doInitialization(void) {
+  bool Changed = false;
+
+  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
+    Changed |= getContainedPass(Index)->doInitialization();
+
+  return Changed;
+}
+
+/// Run all of the finalizers for the module passes.
+///
+bool MPPassManager::doFinalization(void) {
+  bool Changed = false;
+
+  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
+    Changed |= getContainedPass(Index)->doFinalization();
+
   return Changed;
 }
 
@@ -1640,6 +1679,25 @@
 
 //===----------------------------------------------------------------------===//
 // PassManagerImpl implementation
+
+bool PassManagerImpl::doInitialization(void) {
+  bool Changed = false;
+
+  for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
+    Changed |= getContainedManager(Index)->doInitialization();
+
+  return Changed;
+}
+
+bool PassManagerImpl::doFinalization(void) {
+  bool Changed = false;
+
+  for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
+    Changed |= getContainedManager(Index)->doFinalization();
+
+  return Changed;
+}
+
 //
 /// run - Execute all of the passes scheduled for execution.  Keep track of
 /// whether any of the passes modifies the module, and if so, return true.
@@ -1684,6 +1742,18 @@
   return PM->run(M);
 }
 
+/// doInitialization - Run all of the initializers for the module passes.
+///
+bool PassManager::doInitialization() {
+  return PM->doInitialization();
+}
+
+/// doFinalization - Run all of the finalizers for the module passes.
+///
+bool PassManager::doFinalization() {
+  return PM->doFinalization();
+}
+
 //===----------------------------------------------------------------------===//
 // TimingInfo Class - This class is used to calculate information about the
 // amount of time each pass takes to execute.  This only happens with
diff --git a/tools/bugpoint/CrashDebugger.cpp b/tools/bugpoint/CrashDebugger.cpp
index aed16f4..8836eed 100644
--- a/tools/bugpoint/CrashDebugger.cpp
+++ b/tools/bugpoint/CrashDebugger.cpp
@@ -412,7 +412,9 @@
   // Verify that this is still valid.
   PassManager Passes;
   Passes.add(createVerifierPass());
+  Passes.doInitialization();
   Passes.run(*M);
+  Passes.doFinalization();
 
   // Try running on the hacked up program...
   if (TestFn(BD, M)) {
diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp
index 4d4a74c..f3e5c20 100644
--- a/tools/llc/llc.cpp
+++ b/tools/llc/llc.cpp
@@ -359,7 +359,9 @@
     // Before executing passes, print the final values of the LLVM options.
     cl::PrintOptionValues();
 
+    PM.doInitialization();
     PM.run(*mod);
+    PM.doFinalization();
   }
 
   // Declare success.
diff --git a/tools/llvm-extract/llvm-extract.cpp b/tools/llvm-extract/llvm-extract.cpp
index ac82d98..d2caabd 100644
--- a/tools/llvm-extract/llvm-extract.cpp
+++ b/tools/llvm-extract/llvm-extract.cpp
@@ -276,7 +276,9 @@
   else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
     Passes.add(createBitcodeWriterPass(Out.os()));
 
+  Passes.doInitialization();
   Passes.run(*M.get());
+  Passes.doFinalization();
 
   // Declare success.
   Out.keep();
diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp
index 81e9503..940ac34 100644
--- a/tools/llvm-prof/llvm-prof.cpp
+++ b/tools/llvm-prof/llvm-prof.cpp
@@ -287,7 +287,9 @@
   PassManager PassMgr;
   PassMgr.add(createProfileLoaderPass(ProfileDataFile));
   PassMgr.add(new ProfileInfoPrinterPass(PIL));
+  PassMgr.doInitialization();
   PassMgr.run(*M);
+  PassMgr.doFinalization();
 
   return 0;
 }
diff --git a/tools/llvm-stress/llvm-stress.cpp b/tools/llvm-stress/llvm-stress.cpp
index 8473d94..72fdac8 100644
--- a/tools/llvm-stress/llvm-stress.cpp
+++ b/tools/llvm-stress/llvm-stress.cpp
@@ -713,7 +713,9 @@
   PassManager Passes;
   Passes.add(createVerifierPass());
   Passes.add(createPrintModulePass(&Out->os()));
+  Passes.doInitialization();
   Passes.run(*M.get());
+  Passes.doFinalization();
   Out->keep();
 
   return 0;
diff --git a/tools/lto/LTOCodeGenerator.cpp b/tools/lto/LTOCodeGenerator.cpp
index b1c4f43..d9fa218 100644
--- a/tools/lto/LTOCodeGenerator.cpp
+++ b/tools/lto/LTOCodeGenerator.cpp
@@ -342,7 +342,9 @@
   passes.add(createInternalizePass(mustPreserveList));
 
   // apply scope restrictions
+  passes.doInitialization();
   passes.run(*mergedModule);
+  passes.doFinalization();
 
   _scopeRestrictionsDone = true;
 }
@@ -397,7 +399,9 @@
   }
 
   // Run our queue of passes all at once now, efficiently.
+  passes.doInitialization();
   passes.run(*mergedModule);
+  passes.doFinalization();
 
   // Run the code generator, and write assembly file
   codeGenPasses->doInitialization();
diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp
index bac0d46..7cced98 100644
--- a/tools/opt/opt.cpp
+++ b/tools/opt/opt.cpp
@@ -820,7 +820,9 @@
   cl::PrintOptionValues();
 
   // Now that we have all of the passes ready, run them.
+  Passes.doInitialization();
   Passes.run(*M.get());
+  Passes.doFinalization();
 
   // Declare success.
   if (!NoOutput || PrintBreakpoints)