1 //===- PassManager.cpp - 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/LLVMContext.h"
12 #include "llvm/IR/PassManager.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/Debug.h"
19 DebugPM("debug-pass-manager", cl::Hidden,
20 cl::desc("Print pass management debugging information"));
22 PreservedAnalyses ModulePassManager::run(Module &M, ModuleAnalysisManager *AM) {
23 PreservedAnalyses PA = PreservedAnalyses::all();
26 dbgs() << "Starting module pass manager run.\n";
28 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
30 dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
32 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
34 // If we have an active analysis manager at this level we want to ensure we
35 // update it as each pass runs and potentially invalidates analyses. We
36 // also update the preserved set of analyses based on what analyses we have
37 // already handled the invalidation for here and don't need to invalidate
40 PassPA = AM->invalidate(M, std::move(PassPA));
42 // Finally, we intersect the final preserved analyses to compute the
43 // aggregate preserved set for this pass manager.
44 PA.intersect(std::move(PassPA));
46 M.getContext().yield();
50 dbgs() << "Finished module pass manager run.\n";
55 ModuleAnalysisManager::ResultConceptT &
56 ModuleAnalysisManager::getResultImpl(void *PassID, Module &M) {
57 ModuleAnalysisResultMapT::iterator RI;
59 std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
60 PassID, std::unique_ptr<detail::AnalysisResultConcept<Module &>>()));
62 // If we don't have a cached result for this module, look up the pass and run
63 // it to produce a result, which we then add to the cache.
65 auto &P = lookupPass(PassID);
67 dbgs() << "Running module analysis: " << P.name() << "\n";
68 RI->second = P.run(M, this);
74 ModuleAnalysisManager::ResultConceptT *
75 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module &M) const {
76 ModuleAnalysisResultMapT::const_iterator RI =
77 ModuleAnalysisResults.find(PassID);
78 return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
81 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module &M) {
83 dbgs() << "Invalidating module analysis: " << lookupPass(PassID).name()
85 ModuleAnalysisResults.erase(PassID);
88 PreservedAnalyses ModuleAnalysisManager::invalidateImpl(Module &M,
89 PreservedAnalyses PA) {
90 // Short circuit for a common case of all analyses being preserved.
91 if (PA.areAllPreserved())
95 dbgs() << "Invalidating all non-preserved analyses for module: "
96 << M.getModuleIdentifier() << "\n";
98 // FIXME: This is a total hack based on the fact that erasure doesn't
99 // invalidate iteration for DenseMap.
100 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
101 E = ModuleAnalysisResults.end();
103 void *PassID = I->first;
105 // Pass the invalidation down to the pass itself to see if it thinks it is
106 // necessary. The analysis pass can return false if no action on the part
107 // of the analysis manager is required for this invalidation event.
108 if (I->second->invalidate(M, PA)) {
110 dbgs() << "Invalidating module analysis: "
111 << lookupPass(PassID).name() << "\n";
113 ModuleAnalysisResults.erase(I);
116 // After handling each pass, we mark it as preserved. Once we've
117 // invalidated any stale results, the rest of the system is allowed to
118 // start preserving this analysis again.
122 return std::move(PA);
125 PreservedAnalyses FunctionPassManager::run(Function &F,
126 FunctionAnalysisManager *AM) {
127 PreservedAnalyses PA = PreservedAnalyses::all();
130 dbgs() << "Starting function pass manager run.\n";
132 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
134 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
136 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
138 // If we have an active analysis manager at this level we want to ensure we
139 // update it as each pass runs and potentially invalidates analyses. We
140 // also update the preserved set of analyses based on what analyses we have
141 // already handled the invalidation for here and don't need to invalidate
144 PassPA = AM->invalidate(F, std::move(PassPA));
146 // Finally, we intersect the final preserved analyses to compute the
147 // aggregate preserved set for this pass manager.
148 PA.intersect(std::move(PassPA));
150 F.getContext().yield();
154 dbgs() << "Finished function pass manager run.\n";
159 bool FunctionAnalysisManager::empty() const {
160 assert(FunctionAnalysisResults.empty() ==
161 FunctionAnalysisResultLists.empty() &&
162 "The storage and index of analysis results disagree on how many there "
164 return FunctionAnalysisResults.empty();
167 void FunctionAnalysisManager::clear() {
168 FunctionAnalysisResults.clear();
169 FunctionAnalysisResultLists.clear();
172 FunctionAnalysisManager::ResultConceptT &
173 FunctionAnalysisManager::getResultImpl(void *PassID, Function &F) {
174 FunctionAnalysisResultMapT::iterator RI;
176 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
177 std::make_pair(PassID, &F), FunctionAnalysisResultListT::iterator()));
179 // If we don't have a cached result for this function, look up the pass and
180 // run it to produce a result, which we then add to the cache.
182 auto &P = lookupPass(PassID);
184 dbgs() << "Running function analysis: " << P.name() << "\n";
185 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[&F];
186 ResultList.emplace_back(PassID, P.run(F, this));
187 RI->second = std::prev(ResultList.end());
190 return *RI->second->second;
193 FunctionAnalysisManager::ResultConceptT *
194 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function &F) const {
195 FunctionAnalysisResultMapT::const_iterator RI =
196 FunctionAnalysisResults.find(std::make_pair(PassID, &F));
197 return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
200 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function &F) {
201 FunctionAnalysisResultMapT::iterator RI =
202 FunctionAnalysisResults.find(std::make_pair(PassID, &F));
203 if (RI == FunctionAnalysisResults.end())
207 dbgs() << "Invalidating function analysis: " << lookupPass(PassID).name()
209 FunctionAnalysisResultLists[&F].erase(RI->second);
210 FunctionAnalysisResults.erase(RI);
214 FunctionAnalysisManager::invalidateImpl(Function &F, PreservedAnalyses PA) {
215 // Short circuit for a common case of all analyses being preserved.
216 if (PA.areAllPreserved())
217 return std::move(PA);
220 dbgs() << "Invalidating all non-preserved analyses for function: "
221 << F.getName() << "\n";
223 // Clear all the invalidated results associated specifically with this
225 SmallVector<void *, 8> InvalidatedPassIDs;
226 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[&F];
227 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
228 E = ResultsList.end();
230 void *PassID = I->first;
232 // Pass the invalidation down to the pass itself to see if it thinks it is
233 // necessary. The analysis pass can return false if no action on the part
234 // of the analysis manager is required for this invalidation event.
235 if (I->second->invalidate(F, PA)) {
237 dbgs() << "Invalidating function analysis: "
238 << lookupPass(PassID).name() << "\n";
240 InvalidatedPassIDs.push_back(I->first);
241 I = ResultsList.erase(I);
246 // After handling each pass, we mark it as preserved. Once we've
247 // invalidated any stale results, the rest of the system is allowed to
248 // start preserving this analysis again.
251 while (!InvalidatedPassIDs.empty())
252 FunctionAnalysisResults.erase(
253 std::make_pair(InvalidatedPassIDs.pop_back_val(), &F));
254 if (ResultsList.empty())
255 FunctionAnalysisResultLists.erase(&F);
257 return std::move(PA);
260 char FunctionAnalysisManagerModuleProxy::PassID;
262 FunctionAnalysisManagerModuleProxy::Result
263 FunctionAnalysisManagerModuleProxy::run(Module &M) {
264 assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
268 FunctionAnalysisManagerModuleProxy::Result::~Result() {
269 // Clear out the analysis manager if we're being destroyed -- it means we
270 // didn't even see an invalidate call when we got invalidated.
274 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
275 Module &M, const PreservedAnalyses &PA) {
276 // If this proxy isn't marked as preserved, then we can't even invalidate
277 // individual function analyses, there may be an invalid set of Function
278 // objects in the cache making it impossible to incrementally preserve them.
279 // Just clear the entire manager.
280 if (!PA.preserved(ID()))
283 // Return false to indicate that this result is still a valid proxy.
287 char ModuleAnalysisManagerFunctionProxy::PassID;