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/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(std::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 std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
48 PassID, std::unique_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 = std::move(lookupPass(PassID).run(M, this));
58 ModuleAnalysisManager::ResultConceptT *
59 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
60 ModuleAnalysisResultMapT::const_iterator RI =
61 ModuleAnalysisResults.find(PassID);
62 return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
65 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
66 ModuleAnalysisResults.erase(PassID);
69 void ModuleAnalysisManager::invalidateImpl(Module *M,
70 const PreservedAnalyses &PA) {
71 // FIXME: This is a total hack based on the fact that erasure doesn't
72 // invalidate iteration for DenseMap.
73 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
74 E = ModuleAnalysisResults.end();
76 if (I->second->invalidate(M, PA))
77 ModuleAnalysisResults.erase(I);
80 PreservedAnalyses FunctionPassManager::run(Function *F,
81 FunctionAnalysisManager *AM) {
82 PreservedAnalyses PA = PreservedAnalyses::all();
85 dbgs() << "Starting function pass manager run.\n";
87 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
89 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
91 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
93 AM->invalidate(F, PassPA);
94 PA.intersect(std::move(PassPA));
98 dbgs() << "Finished function pass manager run.\n";
103 bool FunctionAnalysisManager::empty() const {
104 assert(FunctionAnalysisResults.empty() ==
105 FunctionAnalysisResultLists.empty() &&
106 "The storage and index of analysis results disagree on how many there "
108 return FunctionAnalysisResults.empty();
111 void FunctionAnalysisManager::clear() {
112 FunctionAnalysisResults.clear();
113 FunctionAnalysisResultLists.clear();
116 FunctionAnalysisManager::ResultConceptT &
117 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
118 FunctionAnalysisResultMapT::iterator RI;
120 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
121 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
123 // If we don't have a cached result for this function, look up the pass and
124 // run it to produce a result, which we then add to the cache.
126 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
127 ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
128 RI->second = std::prev(ResultList.end());
131 return *RI->second->second;
134 FunctionAnalysisManager::ResultConceptT *
135 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
136 FunctionAnalysisResultMapT::const_iterator RI =
137 FunctionAnalysisResults.find(std::make_pair(PassID, F));
138 return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
141 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
142 FunctionAnalysisResultMapT::iterator RI =
143 FunctionAnalysisResults.find(std::make_pair(PassID, F));
144 if (RI == FunctionAnalysisResults.end())
147 FunctionAnalysisResultLists[F].erase(RI->second);
150 void FunctionAnalysisManager::invalidateImpl(Function *F,
151 const PreservedAnalyses &PA) {
152 // Clear all the invalidated results associated specifically with this
154 SmallVector<void *, 8> InvalidatedPassIDs;
155 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
156 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
157 E = ResultsList.end();
159 if (I->second->invalidate(F, PA)) {
160 InvalidatedPassIDs.push_back(I->first);
161 I = ResultsList.erase(I);
165 while (!InvalidatedPassIDs.empty())
166 FunctionAnalysisResults.erase(
167 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
170 char FunctionAnalysisManagerModuleProxy::PassID;
172 FunctionAnalysisManagerModuleProxy::Result
173 FunctionAnalysisManagerModuleProxy::run(Module *M) {
174 assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
178 FunctionAnalysisManagerModuleProxy::Result::~Result() {
179 // Clear out the analysis manager if we're being destroyed -- it means we
180 // didn't even see an invalidate call when we got invalidated.
184 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
185 Module *M, const PreservedAnalyses &PA) {
186 // If this proxy isn't marked as preserved, then we can't even invalidate
187 // individual function analyses, there may be an invalid set of Function
188 // objects in the cache making it impossible to incrementally preserve them.
189 // Just clear the entire manager.
190 if (!PA.preserved(ID()))
193 // Return false to indicate that this result is still a valid proxy.
197 char ModuleAnalysisManagerFunctionProxy::PassID;