[PM] Make the function pass manager more regular.
authorChandler Carruth <chandlerc@gmail.com>
Wed, 20 Nov 2013 04:39:16 +0000 (04:39 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 20 Nov 2013 04:39:16 +0000 (04:39 +0000)
The FunctionPassManager is now itself a function pass. When run over
a function, it runs all N of its passes over that function. This is the
1:N mapping in the pass dimension only. This allows it to be used in
either a ModulePassManager or potentially some other manager that
works on IR units which are supersets of Functions.

This commit also adds the obvious adaptor to map from a module pass to
a function pass, running the function pass across every function in the
module.

The test has been updated to use this new pattern.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@195192 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/PassManager.h
lib/IR/PassManager.cpp
unittests/IR/PassManagerTest.cpp

index 4a64f20c49a9a12de399339bbcba2a0d854cd79a..2664657694fffd3414cb9ccd714e8b1761d80bdf 100644 (file)
@@ -189,7 +189,7 @@ public:
     Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
   }
 
     Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
   }
 
-  bool run(Module *M);
+  bool run(Function *F);
 
 private:
   // Pull in the concept type and model template specialized for functions.
 
 private:
   // Pull in the concept type and model template specialized for functions.
@@ -204,6 +204,36 @@ private:
   std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
 };
 
   std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
 };
 
+/// \brief Trivial adaptor that maps from a module to its functions.
+///
+/// Designed to allow composition of a FunctionPass(Manager) and a
+/// ModulePassManager.
+template <typename FunctionPassT>
+class ModuleToFunctionPassAdaptor {
+public:
+  explicit ModuleToFunctionPassAdaptor(FunctionPassT Pass)
+      : Pass(llvm_move(Pass)) {}
+
+  /// \brief Runs the function pass across every function in the module.
+  bool run(Module *M) {
+    bool Changed = false;
+    for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+      Changed |= Pass.run(I);
+    return Changed;
+  }
+
+private:
+  FunctionPassT Pass;
+};
+
+/// \brief A function to deduce a function pass type and wrap it in the
+/// templated adaptor.
+template <typename FunctionPassT>
+ModuleToFunctionPassAdaptor<FunctionPassT>
+createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
+  return ModuleToFunctionPassAdaptor<FunctionPassT>(llvm_move(Pass));
+}
+
 /// \brief A module analysis pass manager with lazy running and caching of
 /// results.
 class ModuleAnalysisManager {
 /// \brief A module analysis pass manager with lazy running and caching of
 /// results.
 class ModuleAnalysisManager {
index b53a2b9671d93bc7c9244f0f0afa1aa4bc4a52c3..35fc534151a343116efbb4e12a764f505b28251f 100644 (file)
@@ -53,15 +53,14 @@ void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
   ModuleAnalysisResults.erase(PassID);
 }
 
   ModuleAnalysisResults.erase(PassID);
 }
 
-bool FunctionPassManager::run(Module *M) {
+bool FunctionPassManager::run(Function *F) {
   bool Changed = false;
   bool Changed = false;
-  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
-    for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
-      if (Passes[Idx]->run(I)) {
-        Changed = true;
-        if (AM)
-          AM->invalidateAll(I);
-      }
+  for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
+    if (Passes[Idx]->run(F)) {
+      Changed = true;
+      if (AM)
+        AM->invalidateAll(F);
+    }
   return Changed;
 }
 
   return Changed;
 }
 
index cf7b6e47488f0eeb7e7229762eeff1530e74a0d5..1e02d6ba02bfe194bc82cccc78ae45fcfd55e9b8 100644 (file)
@@ -120,7 +120,7 @@ TEST_F(PassManagerTest, Basic) {
   int FunctionPassRunCount = 0;
   int AnalyzedInstrCount = 0;
   FPM.addPass(TestFunctionPass(AM, FunctionPassRunCount, AnalyzedInstrCount));
   int FunctionPassRunCount = 0;
   int AnalyzedInstrCount = 0;
   FPM.addPass(TestFunctionPass(AM, FunctionPassRunCount, AnalyzedInstrCount));
-  MPM.addPass(FPM);
+  MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
 
   MPM.run(M.get());
   EXPECT_EQ(1, ModulePassRunCount);
 
   MPM.run(M.get());
   EXPECT_EQ(1, ModulePassRunCount);