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 AM->invalidate(M, PassPA);
35 PA.intersect(std::move(PassPA));
37 M.getContext().yield();
41 dbgs() << "Finished module pass manager run.\n";
46 ModuleAnalysisManager::ResultConceptT &
47 ModuleAnalysisManager::getResultImpl(void *PassID, Module &M) {
48 ModuleAnalysisResultMapT::iterator RI;
50 std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
51 PassID, std::unique_ptr<detail::AnalysisResultConcept<Module &>>()));
53 // If we don't have a cached result for this module, look up the pass and run
54 // it to produce a result, which we then add to the cache.
56 auto &P = lookupPass(PassID);
58 dbgs() << "Running module analysis: " << P.name() << "\n";
59 RI->second = P.run(M, this);
65 ModuleAnalysisManager::ResultConceptT *
66 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module &M) const {
67 ModuleAnalysisResultMapT::const_iterator RI =
68 ModuleAnalysisResults.find(PassID);
69 return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
72 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module &M) {
74 dbgs() << "Invalidating module analysis: " << lookupPass(PassID).name()
76 ModuleAnalysisResults.erase(PassID);
79 void ModuleAnalysisManager::invalidateImpl(Module &M,
80 const PreservedAnalyses &PA) {
81 // Short circuit for a common case of all analyses being preserved.
82 if (PA.areAllPreserved())
86 dbgs() << "Invalidating all non-preserved analyses for module: "
87 << M.getModuleIdentifier() << "\n";
89 // FIXME: This is a total hack based on the fact that erasure doesn't
90 // invalidate iteration for DenseMap.
91 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
92 E = ModuleAnalysisResults.end();
94 if (I->second->invalidate(M, PA)) {
96 dbgs() << "Invalidating module analysis: "
97 << lookupPass(I->first).name() << "\n";
99 ModuleAnalysisResults.erase(I);
103 PreservedAnalyses FunctionPassManager::run(Function &F,
104 FunctionAnalysisManager *AM) {
105 PreservedAnalyses PA = PreservedAnalyses::all();
108 dbgs() << "Starting function pass manager run.\n";
110 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
112 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
114 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
116 AM->invalidate(F, PassPA);
117 PA.intersect(std::move(PassPA));
119 F.getContext().yield();
123 dbgs() << "Finished function pass manager run.\n";
128 bool FunctionAnalysisManager::empty() const {
129 assert(FunctionAnalysisResults.empty() ==
130 FunctionAnalysisResultLists.empty() &&
131 "The storage and index of analysis results disagree on how many there "
133 return FunctionAnalysisResults.empty();
136 void FunctionAnalysisManager::clear() {
137 FunctionAnalysisResults.clear();
138 FunctionAnalysisResultLists.clear();
141 FunctionAnalysisManager::ResultConceptT &
142 FunctionAnalysisManager::getResultImpl(void *PassID, Function &F) {
143 FunctionAnalysisResultMapT::iterator RI;
145 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
146 std::make_pair(PassID, &F), FunctionAnalysisResultListT::iterator()));
148 // If we don't have a cached result for this function, look up the pass and
149 // run it to produce a result, which we then add to the cache.
151 auto &P = lookupPass(PassID);
153 dbgs() << "Running function analysis: " << P.name() << "\n";
154 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[&F];
155 ResultList.emplace_back(PassID, P.run(F, this));
156 RI->second = std::prev(ResultList.end());
159 return *RI->second->second;
162 FunctionAnalysisManager::ResultConceptT *
163 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function &F) const {
164 FunctionAnalysisResultMapT::const_iterator RI =
165 FunctionAnalysisResults.find(std::make_pair(PassID, &F));
166 return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
169 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function &F) {
170 FunctionAnalysisResultMapT::iterator RI =
171 FunctionAnalysisResults.find(std::make_pair(PassID, &F));
172 if (RI == FunctionAnalysisResults.end())
176 dbgs() << "Invalidating function analysis: " << lookupPass(PassID).name()
178 FunctionAnalysisResultLists[&F].erase(RI->second);
181 void FunctionAnalysisManager::invalidateImpl(Function &F,
182 const PreservedAnalyses &PA) {
183 // Short circuit for a common case of all analyses being preserved.
184 if (PA.areAllPreserved())
188 dbgs() << "Invalidating all non-preserved analyses for function: "
189 << F.getName() << "\n";
191 // Clear all the invalidated results associated specifically with this
193 SmallVector<void *, 8> InvalidatedPassIDs;
194 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[&F];
195 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
196 E = ResultsList.end();
198 if (I->second->invalidate(F, PA)) {
200 dbgs() << "Invalidating function analysis: "
201 << lookupPass(I->first).name() << "\n";
203 InvalidatedPassIDs.push_back(I->first);
204 I = ResultsList.erase(I);
208 while (!InvalidatedPassIDs.empty())
209 FunctionAnalysisResults.erase(
210 std::make_pair(InvalidatedPassIDs.pop_back_val(), &F));
211 if (ResultsList.empty())
212 FunctionAnalysisResultLists.erase(&F);
215 char FunctionAnalysisManagerModuleProxy::PassID;
217 FunctionAnalysisManagerModuleProxy::Result
218 FunctionAnalysisManagerModuleProxy::run(Module &M) {
219 assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
223 FunctionAnalysisManagerModuleProxy::Result::~Result() {
224 // Clear out the analysis manager if we're being destroyed -- it means we
225 // didn't even see an invalidate call when we got invalidated.
229 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
230 Module &M, const PreservedAnalyses &PA) {
231 // If this proxy isn't marked as preserved, then we can't even invalidate
232 // individual function analyses, there may be an invalid set of Function
233 // objects in the cache making it impossible to incrementally preserve them.
234 // Just clear the entire manager.
235 if (!PA.preserved(ID()))
238 // Return false to indicate that this result is still a valid proxy.
242 char ModuleAnalysisManagerFunctionProxy::PassID;