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 RI->second = std::move(lookupPass(PassID).run(M, this));
61 ModuleAnalysisManager::ResultConceptT *
62 ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
63 ModuleAnalysisResultMapT::const_iterator RI =
64 ModuleAnalysisResults.find(PassID);
65 return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
68 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
69 ModuleAnalysisResults.erase(PassID);
72 void ModuleAnalysisManager::invalidateImpl(Module *M,
73 const PreservedAnalyses &PA) {
74 // FIXME: This is a total hack based on the fact that erasure doesn't
75 // invalidate iteration for DenseMap.
76 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
77 E = ModuleAnalysisResults.end();
79 if (I->second->invalidate(M, PA))
80 ModuleAnalysisResults.erase(I);
83 PreservedAnalyses FunctionPassManager::run(Function *F,
84 FunctionAnalysisManager *AM) {
85 PreservedAnalyses PA = PreservedAnalyses::all();
88 dbgs() << "Starting function pass manager run.\n";
90 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
92 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
94 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
96 AM->invalidate(F, PassPA);
97 PA.intersect(std::move(PassPA));
99 F->getContext().yield();
103 dbgs() << "Finished function pass manager run.\n";
108 bool FunctionAnalysisManager::empty() const {
109 assert(FunctionAnalysisResults.empty() ==
110 FunctionAnalysisResultLists.empty() &&
111 "The storage and index of analysis results disagree on how many there "
113 return FunctionAnalysisResults.empty();
116 void FunctionAnalysisManager::clear() {
117 FunctionAnalysisResults.clear();
118 FunctionAnalysisResultLists.clear();
121 FunctionAnalysisManager::ResultConceptT &
122 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
123 FunctionAnalysisResultMapT::iterator RI;
125 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
126 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
128 // If we don't have a cached result for this function, look up the pass and
129 // run it to produce a result, which we then add to the cache.
131 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
132 ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
133 RI->second = std::prev(ResultList.end());
136 return *RI->second->second;
139 FunctionAnalysisManager::ResultConceptT *
140 FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
141 FunctionAnalysisResultMapT::const_iterator RI =
142 FunctionAnalysisResults.find(std::make_pair(PassID, F));
143 return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
146 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
147 FunctionAnalysisResultMapT::iterator RI =
148 FunctionAnalysisResults.find(std::make_pair(PassID, F));
149 if (RI == FunctionAnalysisResults.end())
152 FunctionAnalysisResultLists[F].erase(RI->second);
155 void FunctionAnalysisManager::invalidateImpl(Function *F,
156 const PreservedAnalyses &PA) {
157 // Clear all the invalidated results associated specifically with this
159 SmallVector<void *, 8> InvalidatedPassIDs;
160 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
161 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
162 E = ResultsList.end();
164 if (I->second->invalidate(F, PA)) {
165 InvalidatedPassIDs.push_back(I->first);
166 I = ResultsList.erase(I);
170 while (!InvalidatedPassIDs.empty())
171 FunctionAnalysisResults.erase(
172 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
173 if (ResultsList.empty())
174 FunctionAnalysisResultLists.erase(F);
177 char FunctionAnalysisManagerModuleProxy::PassID;
179 FunctionAnalysisManagerModuleProxy::Result
180 FunctionAnalysisManagerModuleProxy::run(Module *M) {
181 assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
185 FunctionAnalysisManagerModuleProxy::Result::~Result() {
186 // Clear out the analysis manager if we're being destroyed -- it means we
187 // didn't even see an invalidate call when we got invalidated.
191 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
192 Module *M, const PreservedAnalyses &PA) {
193 // If this proxy isn't marked as preserved, then we can't even invalidate
194 // individual function analyses, there may be an invalid set of Function
195 // objects in the cache making it impossible to incrementally preserve them.
196 // Just clear the entire manager.
197 if (!PA.preserved(ID()))
200 // Return false to indicate that this result is still a valid proxy.
204 char ModuleAnalysisManagerFunctionProxy::PassID;