[PM] Factor the overwhelming majority of the interface boiler plate out
[oota-llvm.git] / lib / IR / PassManager.cpp
index fe16adcaeb3db3b7f592fa6621f5887b6f7c2593..30b46b01c1def2527fb2a1bb975be58017a66800 100644 (file)
@@ -23,40 +23,42 @@ PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
   return PA;
 }
 
-void ModuleAnalysisManager::invalidate(Module *M, const PreservedAnalyses &PA) {
-  // FIXME: This is a total hack based on the fact that erasure doesn't
-  // invalidate iteration for DenseMap.
-  for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
-                                          E = ModuleAnalysisResults.end();
-       I != E; ++I)
-    if (I->second->invalidate(M, PA))
-      ModuleAnalysisResults.erase(I);
-}
-
-const detail::AnalysisResultConcept<Module *> &
+const ModuleAnalysisManager::ResultConceptT &
 ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
   ModuleAnalysisResultMapT::iterator RI;
   bool Inserted;
   llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
       PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module *> >()));
 
-  if (Inserted) {
-    // We don't have a cached result for this result. Look up the pass and run
-    // it to produce a result, which we then add to the cache.
-    ModuleAnalysisPassMapT::const_iterator PI =
-        ModuleAnalysisPasses.find(PassID);
-    assert(PI != ModuleAnalysisPasses.end() &&
-           "Analysis passes must be registered prior to being queried!");
-    RI->second = PI->second->run(M, this);
-  }
+  // If we don't have a cached result for this module, look up the pass and run
+  // it to produce a result, which we then add to the cache.
+  if (Inserted)
+    RI->second = lookupPass(PassID).run(M, this);
 
   return *RI->second;
 }
 
+const ModuleAnalysisManager::ResultConceptT *
+ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
+  ModuleAnalysisResultMapT::const_iterator RI = ModuleAnalysisResults.find(PassID);
+  return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second;
+}
+
 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
   ModuleAnalysisResults.erase(PassID);
 }
 
+void ModuleAnalysisManager::invalidateImpl(Module *M,
+                                           const PreservedAnalyses &PA) {
+  // FIXME: This is a total hack based on the fact that erasure doesn't
+  // invalidate iteration for DenseMap.
+  for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
+                                          E = ModuleAnalysisResults.end();
+       I != E; ++I)
+    if (I->second->invalidate(M, PA))
+      ModuleAnalysisResults.erase(I);
+}
+
 PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) {
   PreservedAnalyses PA = PreservedAnalyses::all();
   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
@@ -68,25 +70,6 @@ PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager
   return PA;
 }
 
-void FunctionAnalysisManager::invalidate(Function *F, const PreservedAnalyses &PA) {
-  // Clear all the invalidated results associated specifically with this
-  // function.
-  SmallVector<void *, 8> InvalidatedPassIDs;
-  FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
-  for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
-                                             E = ResultsList.end();
-       I != E;)
-    if (I->second->invalidate(F, PA)) {
-      InvalidatedPassIDs.push_back(I->first);
-      I = ResultsList.erase(I);
-    } else {
-      ++I;
-    }
-  while (!InvalidatedPassIDs.empty())
-    FunctionAnalysisResults.erase(
-        std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
-}
-
 bool FunctionAnalysisManager::empty() const {
   assert(FunctionAnalysisResults.empty() ==
              FunctionAnalysisResultLists.empty() &&
@@ -100,28 +83,31 @@ void FunctionAnalysisManager::clear() {
   FunctionAnalysisResultLists.clear();
 }
 
-const detail::AnalysisResultConcept<Function *> &
+const FunctionAnalysisManager::ResultConceptT &
 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
   FunctionAnalysisResultMapT::iterator RI;
   bool Inserted;
   llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
       std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
 
+  // If we don't have a cached result for this function, look up the pass and
+  // run it to produce a result, which we then add to the cache.
   if (Inserted) {
-    // We don't have a cached result for this result. Look up the pass and run
-    // it to produce a result, which we then add to the cache.
-    FunctionAnalysisPassMapT::const_iterator PI =
-        FunctionAnalysisPasses.find(PassID);
-    assert(PI != FunctionAnalysisPasses.end() &&
-           "Analysis passes must be registered prior to being queried!");
     FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
-    ResultList.push_back(std::make_pair(PassID, PI->second->run(F, this)));
+    ResultList.push_back(std::make_pair(PassID, lookupPass(PassID).run(F, this)));
     RI->second = llvm::prior(ResultList.end());
   }
 
   return *RI->second->second;
 }
 
+const FunctionAnalysisManager::ResultConceptT *
+FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
+  FunctionAnalysisResultMapT::const_iterator RI =
+      FunctionAnalysisResults.find(std::make_pair(PassID, F));
+  return RI == FunctionAnalysisResults.end() ? 0 : &*RI->second->second;
+}
+
 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
   FunctionAnalysisResultMapT::iterator RI =
       FunctionAnalysisResults.find(std::make_pair(PassID, F));
@@ -131,6 +117,26 @@ void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
   FunctionAnalysisResultLists[F].erase(RI->second);
 }
 
+void FunctionAnalysisManager::invalidateImpl(Function *F,
+                                             const PreservedAnalyses &PA) {
+  // Clear all the invalidated results associated specifically with this
+  // function.
+  SmallVector<void *, 8> InvalidatedPassIDs;
+  FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
+  for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
+                                             E = ResultsList.end();
+       I != E;)
+    if (I->second->invalidate(F, PA)) {
+      InvalidatedPassIDs.push_back(I->first);
+      I = ResultsList.erase(I);
+    } else {
+      ++I;
+    }
+  while (!InvalidatedPassIDs.empty())
+    FunctionAnalysisResults.erase(
+        std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
+}
+
 char FunctionAnalysisManagerModuleProxy::PassID;
 
 FunctionAnalysisManagerModuleProxy::Result
@@ -147,19 +153,15 @@ FunctionAnalysisManagerModuleProxy::Result::~Result() {
 
 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
     Module *M, const PreservedAnalyses &PA) {
-  // If this proxy isn't marked as preserved, then it is has an invalid set of
-  // Function objects in the cache making it impossible to incrementally
-  // preserve them. Just clear the entire manager.
-  if (!PA.preserved(ID())) {
+  // If this proxy isn't marked as preserved, then we can't even invalidate
+  // individual function analyses, there may be an invalid set of Function
+  // objects in the cache making it impossible to incrementally preserve them.
+  // Just clear the entire manager.
+  if (!PA.preserved(ID()))
     FAM.clear();
-    return false;
-  }
-
-  // The set of functions was preserved some how, so just directly invalidate
-  // any analysis results not preserved.
-  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
-    FAM.invalidate(I, PA);
 
   // Return false to indicate that this result is still a valid proxy.
   return false;
 }
+
+char ModuleAnalysisManagerFunctionProxy::PassID;