LLVM API Documentation

PassManager.cpp
Go to the documentation of this file.
00001 //===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/ADT/STLExtras.h"
00011 #include "llvm/IR/PassManager.h"
00012 #include "llvm/Support/CommandLine.h"
00013 #include "llvm/Support/Debug.h"
00014 
00015 using namespace llvm;
00016 
00017 static cl::opt<bool>
00018 DebugPM("debug-pass-manager", cl::Hidden,
00019         cl::desc("Print pass management debugging information"));
00020 
00021 PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
00022   PreservedAnalyses PA = PreservedAnalyses::all();
00023 
00024   if (DebugPM)
00025     dbgs() << "Starting module pass manager run.\n";
00026 
00027   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
00028     if (DebugPM)
00029       dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
00030 
00031     PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
00032     if (AM)
00033       AM->invalidate(M, PassPA);
00034     PA.intersect(std::move(PassPA));
00035   }
00036 
00037   if (DebugPM)
00038     dbgs() << "Finished module pass manager run.\n";
00039 
00040   return PA;
00041 }
00042 
00043 ModuleAnalysisManager::ResultConceptT &
00044 ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
00045   ModuleAnalysisResultMapT::iterator RI;
00046   bool Inserted;
00047   std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
00048       PassID, std::unique_ptr<detail::AnalysisResultConcept<Module *>>()));
00049 
00050   // If we don't have a cached result for this module, look up the pass and run
00051   // it to produce a result, which we then add to the cache.
00052   if (Inserted)
00053     RI->second = std::move(lookupPass(PassID).run(M, this));
00054 
00055   return *RI->second;
00056 }
00057 
00058 ModuleAnalysisManager::ResultConceptT *
00059 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
00060   ModuleAnalysisResultMapT::const_iterator RI =
00061       ModuleAnalysisResults.find(PassID);
00062   return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
00063 }
00064 
00065 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
00066   ModuleAnalysisResults.erase(PassID);
00067 }
00068 
00069 void ModuleAnalysisManager::invalidateImpl(Module *M,
00070                                            const PreservedAnalyses &PA) {
00071   // FIXME: This is a total hack based on the fact that erasure doesn't
00072   // invalidate iteration for DenseMap.
00073   for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
00074                                           E = ModuleAnalysisResults.end();
00075        I != E; ++I)
00076     if (I->second->invalidate(M, PA))
00077       ModuleAnalysisResults.erase(I);
00078 }
00079 
00080 PreservedAnalyses FunctionPassManager::run(Function *F,
00081                                            FunctionAnalysisManager *AM) {
00082   PreservedAnalyses PA = PreservedAnalyses::all();
00083 
00084   if (DebugPM)
00085     dbgs() << "Starting function pass manager run.\n";
00086 
00087   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
00088     if (DebugPM)
00089       dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
00090 
00091     PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
00092     if (AM)
00093       AM->invalidate(F, PassPA);
00094     PA.intersect(std::move(PassPA));
00095   }
00096 
00097   if (DebugPM)
00098     dbgs() << "Finished function pass manager run.\n";
00099 
00100   return PA;
00101 }
00102 
00103 bool FunctionAnalysisManager::empty() const {
00104   assert(FunctionAnalysisResults.empty() ==
00105              FunctionAnalysisResultLists.empty() &&
00106          "The storage and index of analysis results disagree on how many there "
00107          "are!");
00108   return FunctionAnalysisResults.empty();
00109 }
00110 
00111 void FunctionAnalysisManager::clear() {
00112   FunctionAnalysisResults.clear();
00113   FunctionAnalysisResultLists.clear();
00114 }
00115 
00116 FunctionAnalysisManager::ResultConceptT &
00117 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
00118   FunctionAnalysisResultMapT::iterator RI;
00119   bool Inserted;
00120   std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
00121       std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
00122 
00123   // If we don't have a cached result for this function, look up the pass and
00124   // run it to produce a result, which we then add to the cache.
00125   if (Inserted) {
00126     FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
00127     ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
00128     RI->second = std::prev(ResultList.end());
00129   }
00130 
00131   return *RI->second->second;
00132 }
00133 
00134 FunctionAnalysisManager::ResultConceptT *
00135 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
00136   FunctionAnalysisResultMapT::const_iterator RI =
00137       FunctionAnalysisResults.find(std::make_pair(PassID, F));
00138   return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
00139 }
00140 
00141 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
00142   FunctionAnalysisResultMapT::iterator RI =
00143       FunctionAnalysisResults.find(std::make_pair(PassID, F));
00144   if (RI == FunctionAnalysisResults.end())
00145     return;
00146 
00147   FunctionAnalysisResultLists[F].erase(RI->second);
00148 }
00149 
00150 void FunctionAnalysisManager::invalidateImpl(Function *F,
00151                                              const PreservedAnalyses &PA) {
00152   // Clear all the invalidated results associated specifically with this
00153   // function.
00154   SmallVector<void *, 8> InvalidatedPassIDs;
00155   FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
00156   for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
00157                                              E = ResultsList.end();
00158        I != E;)
00159     if (I->second->invalidate(F, PA)) {
00160       InvalidatedPassIDs.push_back(I->first);
00161       I = ResultsList.erase(I);
00162     } else {
00163       ++I;
00164     }
00165   while (!InvalidatedPassIDs.empty())
00166     FunctionAnalysisResults.erase(
00167         std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
00168 }
00169 
00170 char FunctionAnalysisManagerModuleProxy::PassID;
00171 
00172 FunctionAnalysisManagerModuleProxy::Result
00173 FunctionAnalysisManagerModuleProxy::run(Module *M) {
00174   assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
00175   return Result(*FAM);
00176 }
00177 
00178 FunctionAnalysisManagerModuleProxy::Result::~Result() {
00179   // Clear out the analysis manager if we're being destroyed -- it means we
00180   // didn't even see an invalidate call when we got invalidated.
00181   FAM->clear();
00182 }
00183 
00184 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
00185     Module *M, const PreservedAnalyses &PA) {
00186   // If this proxy isn't marked as preserved, then we can't even invalidate
00187   // individual function analyses, there may be an invalid set of Function
00188   // objects in the cache making it impossible to incrementally preserve them.
00189   // Just clear the entire manager.
00190   if (!PA.preserved(ID()))
00191     FAM->clear();
00192 
00193   // Return false to indicate that this result is still a valid proxy.
00194   return false;
00195 }
00196 
00197 char ModuleAnalysisManagerFunctionProxy::PassID;