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