[PM] Add a utility pass template that synthesizes the invalidation of
[oota-llvm.git] / lib / Analysis / CGSCCPassManager.cpp
1 //===- CGSCCPassManager.cpp - Managing & running CGSCC 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/Analysis/CGSCCPassManager.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/Debug.h"
13
14 using namespace llvm;
15
16 static cl::opt<bool>
17 DebugPM("debug-cgscc-pass-manager", cl::Hidden,
18         cl::desc("Print CGSCC pass management debugging information"));
19
20 PreservedAnalyses CGSCCPassManager::run(LazyCallGraph::SCC &C,
21                                         CGSCCAnalysisManager *AM) {
22   PreservedAnalyses PA = PreservedAnalyses::all();
23
24   if (DebugPM)
25     dbgs() << "Starting CGSCC pass manager run.\n";
26
27   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
28     if (DebugPM)
29       dbgs() << "Running CGSCC pass: " << Passes[Idx]->name() << "\n";
30
31     PreservedAnalyses PassPA = Passes[Idx]->run(C, AM);
32     if (AM)
33       AM->invalidate(C, PassPA);
34     PA.intersect(std::move(PassPA));
35   }
36
37   if (DebugPM)
38     dbgs() << "Finished CGSCC pass manager run.\n";
39
40   return PA;
41 }
42
43 bool CGSCCAnalysisManager::empty() const {
44   assert(CGSCCAnalysisResults.empty() == CGSCCAnalysisResultLists.empty() &&
45          "The storage and index of analysis results disagree on how many there "
46          "are!");
47   return CGSCCAnalysisResults.empty();
48 }
49
50 void CGSCCAnalysisManager::clear() {
51   CGSCCAnalysisResults.clear();
52   CGSCCAnalysisResultLists.clear();
53 }
54
55 CGSCCAnalysisManager::ResultConceptT &
56 CGSCCAnalysisManager::getResultImpl(void *PassID, LazyCallGraph::SCC &C) {
57   CGSCCAnalysisResultMapT::iterator RI;
58   bool Inserted;
59   std::tie(RI, Inserted) = CGSCCAnalysisResults.insert(std::make_pair(
60       std::make_pair(PassID, &C), CGSCCAnalysisResultListT::iterator()));
61
62   // If we don't have a cached result for this function, look up the pass and
63   // run it to produce a result, which we then add to the cache.
64   if (Inserted) {
65     auto &P = lookupPass(PassID);
66     if (DebugPM)
67       dbgs() << "Running CGSCC analysis: " << P.name() << "\n";
68     CGSCCAnalysisResultListT &ResultList = CGSCCAnalysisResultLists[&C];
69     ResultList.emplace_back(PassID, P.run(C, this));
70     RI->second = std::prev(ResultList.end());
71   }
72
73   return *RI->second->second;
74 }
75
76 CGSCCAnalysisManager::ResultConceptT *
77 CGSCCAnalysisManager::getCachedResultImpl(void *PassID,
78                                           LazyCallGraph::SCC &C) const {
79   CGSCCAnalysisResultMapT::const_iterator RI =
80       CGSCCAnalysisResults.find(std::make_pair(PassID, &C));
81   return RI == CGSCCAnalysisResults.end() ? nullptr : &*RI->second->second;
82 }
83
84 void CGSCCAnalysisManager::invalidateImpl(void *PassID, LazyCallGraph::SCC &C) {
85   CGSCCAnalysisResultMapT::iterator RI =
86       CGSCCAnalysisResults.find(std::make_pair(PassID, &C));
87   if (RI == CGSCCAnalysisResults.end())
88     return;
89
90   if (DebugPM)
91     dbgs() << "Invalidating CGSCC analysis: " << lookupPass(PassID).name()
92            << "\n";
93   CGSCCAnalysisResultLists[&C].erase(RI->second);
94   CGSCCAnalysisResults.erase(RI);
95 }
96
97 void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC &C,
98                                           const PreservedAnalyses &PA) {
99   // Short circuit for a common case of all analyses being preserved.
100   if (PA.areAllPreserved())
101     return;
102
103   if (DebugPM)
104     dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName()
105            << "\n";
106
107   // Clear all the invalidated results associated specifically with this
108   // function.
109   SmallVector<void *, 8> InvalidatedPassIDs;
110   CGSCCAnalysisResultListT &ResultsList = CGSCCAnalysisResultLists[&C];
111   for (CGSCCAnalysisResultListT::iterator I = ResultsList.begin(),
112                                           E = ResultsList.end();
113        I != E;)
114     if (I->second->invalidate(C, PA)) {
115       if (DebugPM)
116         dbgs() << "Invalidating CGSCC analysis: " << lookupPass(I->first).name()
117                << "\n";
118
119       InvalidatedPassIDs.push_back(I->first);
120       I = ResultsList.erase(I);
121     } else {
122       ++I;
123     }
124   while (!InvalidatedPassIDs.empty())
125     CGSCCAnalysisResults.erase(
126         std::make_pair(InvalidatedPassIDs.pop_back_val(), &C));
127   CGSCCAnalysisResultLists.erase(&C);
128 }
129
130 char CGSCCAnalysisManagerModuleProxy::PassID;
131
132 CGSCCAnalysisManagerModuleProxy::Result
133 CGSCCAnalysisManagerModuleProxy::run(Module &M) {
134   assert(CGAM->empty() && "CGSCC analyses ran prior to the module proxy!");
135   return Result(*CGAM);
136 }
137
138 CGSCCAnalysisManagerModuleProxy::Result::~Result() {
139   // Clear out the analysis manager if we're being destroyed -- it means we
140   // didn't even see an invalidate call when we got invalidated.
141   CGAM->clear();
142 }
143
144 bool CGSCCAnalysisManagerModuleProxy::Result::invalidate(
145     Module &M, const PreservedAnalyses &PA) {
146   // If this proxy isn't marked as preserved, then we can't even invalidate
147   // individual CGSCC analyses, there may be an invalid set of SCC objects in
148   // the cache making it impossible to incrementally preserve them.
149   // Just clear the entire manager.
150   if (!PA.preserved(ID()))
151     CGAM->clear();
152
153   // Return false to indicate that this result is still a valid proxy.
154   return false;
155 }
156
157 char ModuleAnalysisManagerCGSCCProxy::PassID;
158
159 char FunctionAnalysisManagerCGSCCProxy::PassID;
160
161 FunctionAnalysisManagerCGSCCProxy::Result
162 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C) {
163   assert(FAM->empty() && "Function analyses ran prior to the CGSCC proxy!");
164   return Result(*FAM);
165 }
166
167 FunctionAnalysisManagerCGSCCProxy::Result::~Result() {
168   // Clear out the analysis manager if we're being destroyed -- it means we
169   // didn't even see an invalidate call when we got invalidated.
170   FAM->clear();
171 }
172
173 bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate(
174     LazyCallGraph::SCC &C, const PreservedAnalyses &PA) {
175   // If this proxy isn't marked as preserved, then we can't even invalidate
176   // individual function analyses, there may be an invalid set of Function
177   // objects in the cache making it impossible to incrementally preserve them.
178   // Just clear the entire manager.
179   if (!PA.preserved(ID()))
180     FAM->clear();
181
182   // Return false to indicate that this result is still a valid proxy.
183   return false;
184 }
185
186 char CGSCCAnalysisManagerFunctionProxy::PassID;