1 //===- PassManager.h - Infrastructure for managing & running IR passes ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/IR/PassManager.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/Debug.h"
18 DebugPM("debug-pass-manager", cl::Hidden,
19 cl::desc("Print pass management debugging information"));
21 PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
22 PreservedAnalyses PA = PreservedAnalyses::all();
25 dbgs() << "Starting module pass manager run.\n";
27 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
29 dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
31 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
33 AM->invalidate(M, PassPA);
34 PA.intersect(llvm_move(PassPA));
38 dbgs() << "Finished module pass manager run.\n";
43 ModuleAnalysisManager::ResultConceptT &
44 ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
45 ModuleAnalysisResultMapT::iterator RI;
47 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
48 PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module *> >()));
50 // If we don't have a cached result for this module, look up the pass and run
51 // it to produce a result, which we then add to the cache.
53 RI->second = lookupPass(PassID).run(M, this);
58 ModuleAnalysisManager::ResultConceptT *
59 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
60 ModuleAnalysisResultMapT::const_iterator RI = ModuleAnalysisResults.find(PassID);
61 return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second;
64 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
65 ModuleAnalysisResults.erase(PassID);
68 void ModuleAnalysisManager::invalidateImpl(Module *M,
69 const PreservedAnalyses &PA) {
70 // FIXME: This is a total hack based on the fact that erasure doesn't
71 // invalidate iteration for DenseMap.
72 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
73 E = ModuleAnalysisResults.end();
75 if (I->second->invalidate(M, PA))
76 ModuleAnalysisResults.erase(I);
79 PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) {
80 PreservedAnalyses PA = PreservedAnalyses::all();
83 dbgs() << "Starting function pass manager run.\n";
85 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
87 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
89 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
91 AM->invalidate(F, PassPA);
92 PA.intersect(llvm_move(PassPA));
96 dbgs() << "Finished function pass manager run.\n";
101 bool FunctionAnalysisManager::empty() const {
102 assert(FunctionAnalysisResults.empty() ==
103 FunctionAnalysisResultLists.empty() &&
104 "The storage and index of analysis results disagree on how many there "
106 return FunctionAnalysisResults.empty();
109 void FunctionAnalysisManager::clear() {
110 FunctionAnalysisResults.clear();
111 FunctionAnalysisResultLists.clear();
114 FunctionAnalysisManager::ResultConceptT &
115 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
116 FunctionAnalysisResultMapT::iterator RI;
118 llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
119 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
121 // If we don't have a cached result for this function, look up the pass and
122 // run it to produce a result, which we then add to the cache.
124 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
125 ResultList.push_back(std::make_pair(PassID, lookupPass(PassID).run(F, this)));
126 RI->second = llvm::prior(ResultList.end());
129 return *RI->second->second;
132 FunctionAnalysisManager::ResultConceptT *
133 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
134 FunctionAnalysisResultMapT::const_iterator RI =
135 FunctionAnalysisResults.find(std::make_pair(PassID, F));
136 return RI == FunctionAnalysisResults.end() ? 0 : &*RI->second->second;
139 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
140 FunctionAnalysisResultMapT::iterator RI =
141 FunctionAnalysisResults.find(std::make_pair(PassID, F));
142 if (RI == FunctionAnalysisResults.end())
145 FunctionAnalysisResultLists[F].erase(RI->second);
148 void FunctionAnalysisManager::invalidateImpl(Function *F,
149 const PreservedAnalyses &PA) {
150 // Clear all the invalidated results associated specifically with this
152 SmallVector<void *, 8> InvalidatedPassIDs;
153 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
154 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
155 E = ResultsList.end();
157 if (I->second->invalidate(F, PA)) {
158 InvalidatedPassIDs.push_back(I->first);
159 I = ResultsList.erase(I);
163 while (!InvalidatedPassIDs.empty())
164 FunctionAnalysisResults.erase(
165 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
168 char FunctionAnalysisManagerModuleProxy::PassID;
170 FunctionAnalysisManagerModuleProxy::Result
171 FunctionAnalysisManagerModuleProxy::run(Module *M) {
172 assert(FAM.empty() && "Function analyses ran prior to the module proxy!");
176 FunctionAnalysisManagerModuleProxy::Result::~Result() {
177 // Clear out the analysis manager if we're being destroyed -- it means we
178 // didn't even see an invalidate call when we got invalidated.
182 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
183 Module *M, const PreservedAnalyses &PA) {
184 // If this proxy isn't marked as preserved, then we can't even invalidate
185 // individual function analyses, there may be an invalid set of Function
186 // objects in the cache making it impossible to incrementally preserve them.
187 // Just clear the entire manager.
188 if (!PA.preserved(ID()))
191 // Return false to indicate that this result is still a valid proxy.
195 char ModuleAnalysisManagerFunctionProxy::PassID;