[PM] Don't run the machinery of invalidating all the analysis passes
[oota-llvm.git] / lib / IR / PassManager.cpp
1 //===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
15
16 using namespace llvm;
17
18 static cl::opt<bool>
19     DebugPM("debug-pass-manager", cl::Hidden,
20             cl::desc("Print pass management debugging information"));
21
22 PreservedAnalyses ModulePassManager::run(Module &M, ModuleAnalysisManager *AM) {
23   PreservedAnalyses PA = PreservedAnalyses::all();
24
25   if (DebugPM)
26     dbgs() << "Starting module pass manager run.\n";
27
28   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
29     if (DebugPM)
30       dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
31
32     PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
33     if (AM)
34       AM->invalidate(M, PassPA);
35     PA.intersect(std::move(PassPA));
36
37     M.getContext().yield();
38   }
39
40   if (DebugPM)
41     dbgs() << "Finished module pass manager run.\n";
42
43   return PA;
44 }
45
46 ModuleAnalysisManager::ResultConceptT &
47 ModuleAnalysisManager::getResultImpl(void *PassID, Module &M) {
48   ModuleAnalysisResultMapT::iterator RI;
49   bool Inserted;
50   std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
51       PassID, std::unique_ptr<detail::AnalysisResultConcept<Module &>>()));
52
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.
55   if (Inserted) {
56     auto &P = lookupPass(PassID);
57     if (DebugPM)
58       dbgs() << "Running module analysis: " << P.name() << "\n";
59     RI->second = P.run(M, this);
60   }
61
62   return *RI->second;
63 }
64
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;
70 }
71
72 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module &M) {
73   if (DebugPM)
74     dbgs() << "Invalidating module analysis: " << lookupPass(PassID).name()
75            << "\n";
76   ModuleAnalysisResults.erase(PassID);
77 }
78
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())
83     return;
84
85   if (DebugPM)
86     dbgs() << "Invalidating all non-preserved analyses for module: "
87            << M.getModuleIdentifier() << "\n";
88
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();
93        I != E; ++I)
94     if (I->second->invalidate(M, PA)) {
95       if (DebugPM)
96         dbgs() << "Invalidating module analysis: "
97                << lookupPass(I->first).name() << "\n";
98
99       ModuleAnalysisResults.erase(I);
100     }
101 }
102
103 PreservedAnalyses FunctionPassManager::run(Function &F,
104                                            FunctionAnalysisManager *AM) {
105   PreservedAnalyses PA = PreservedAnalyses::all();
106
107   if (DebugPM)
108     dbgs() << "Starting function pass manager run.\n";
109
110   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
111     if (DebugPM)
112       dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
113
114     PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
115     if (AM)
116       AM->invalidate(F, PassPA);
117     PA.intersect(std::move(PassPA));
118
119     F.getContext().yield();
120   }
121
122   if (DebugPM)
123     dbgs() << "Finished function pass manager run.\n";
124
125   return PA;
126 }
127
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 "
132          "are!");
133   return FunctionAnalysisResults.empty();
134 }
135
136 void FunctionAnalysisManager::clear() {
137   FunctionAnalysisResults.clear();
138   FunctionAnalysisResultLists.clear();
139 }
140
141 FunctionAnalysisManager::ResultConceptT &
142 FunctionAnalysisManager::getResultImpl(void *PassID, Function &F) {
143   FunctionAnalysisResultMapT::iterator RI;
144   bool Inserted;
145   std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
146       std::make_pair(PassID, &F), FunctionAnalysisResultListT::iterator()));
147
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.
150   if (Inserted) {
151     auto &P = lookupPass(PassID);
152     if (DebugPM)
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());
157   }
158
159   return *RI->second->second;
160 }
161
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;
167 }
168
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())
173     return;
174
175   if (DebugPM)
176     dbgs() << "Invalidating function analysis: " << lookupPass(PassID).name()
177            << "\n";
178   FunctionAnalysisResultLists[&F].erase(RI->second);
179 }
180
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())
185     return;
186
187   if (DebugPM)
188     dbgs() << "Invalidating all non-preserved analyses for function: "
189            << F.getName() << "\n";
190
191   // Clear all the invalidated results associated specifically with this
192   // function.
193   SmallVector<void *, 8> InvalidatedPassIDs;
194   FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[&F];
195   for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
196                                              E = ResultsList.end();
197        I != E;)
198     if (I->second->invalidate(F, PA)) {
199       if (DebugPM)
200         dbgs() << "Invalidating function analysis: "
201                << lookupPass(I->first).name() << "\n";
202
203       InvalidatedPassIDs.push_back(I->first);
204       I = ResultsList.erase(I);
205     } else {
206       ++I;
207     }
208   while (!InvalidatedPassIDs.empty())
209     FunctionAnalysisResults.erase(
210         std::make_pair(InvalidatedPassIDs.pop_back_val(), &F));
211   if (ResultsList.empty())
212     FunctionAnalysisResultLists.erase(&F);
213 }
214
215 char FunctionAnalysisManagerModuleProxy::PassID;
216
217 FunctionAnalysisManagerModuleProxy::Result
218 FunctionAnalysisManagerModuleProxy::run(Module &M) {
219   assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
220   return Result(*FAM);
221 }
222
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.
226   FAM->clear();
227 }
228
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()))
236     FAM->clear();
237
238   // Return false to indicate that this result is still a valid proxy.
239   return false;
240 }
241
242 char ModuleAnalysisManagerFunctionProxy::PassID;