[PM] Add a collection of no-op analysis passes and switch the new pass
[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 }
95
96 void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC &C,
97                                           const PreservedAnalyses &PA) {
98   // Short circuit for a common case of all analyses being preserved.
99   if (PA.areAllPreserved())
100     return;
101
102   if (DebugPM)
103     dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName()
104            << "\n";
105
106   // Clear all the invalidated results associated specifically with this
107   // function.
108   SmallVector<void *, 8> InvalidatedPassIDs;
109   CGSCCAnalysisResultListT &ResultsList = CGSCCAnalysisResultLists[&C];
110   for (CGSCCAnalysisResultListT::iterator I = ResultsList.begin(),
111                                           E = ResultsList.end();
112        I != E;)
113     if (I->second->invalidate(C, PA)) {
114       if (DebugPM)
115         dbgs() << "Invalidating CGSCC analysis: " << lookupPass(I->first).name()
116                << "\n";
117
118       InvalidatedPassIDs.push_back(I->first);
119       I = ResultsList.erase(I);
120     } else {
121       ++I;
122     }
123   while (!InvalidatedPassIDs.empty())
124     CGSCCAnalysisResults.erase(
125         std::make_pair(InvalidatedPassIDs.pop_back_val(), &C));
126   CGSCCAnalysisResultLists.erase(&C);
127 }
128
129 char CGSCCAnalysisManagerModuleProxy::PassID;
130
131 CGSCCAnalysisManagerModuleProxy::Result
132 CGSCCAnalysisManagerModuleProxy::run(Module &M) {
133   assert(CGAM->empty() && "CGSCC analyses ran prior to the module proxy!");
134   return Result(*CGAM);
135 }
136
137 CGSCCAnalysisManagerModuleProxy::Result::~Result() {
138   // Clear out the analysis manager if we're being destroyed -- it means we
139   // didn't even see an invalidate call when we got invalidated.
140   CGAM->clear();
141 }
142
143 bool CGSCCAnalysisManagerModuleProxy::Result::invalidate(
144     Module &M, const PreservedAnalyses &PA) {
145   // If this proxy isn't marked as preserved, then we can't even invalidate
146   // individual CGSCC analyses, there may be an invalid set of SCC objects in
147   // the cache making it impossible to incrementally preserve them.
148   // Just clear the entire manager.
149   if (!PA.preserved(ID()))
150     CGAM->clear();
151
152   // Return false to indicate that this result is still a valid proxy.
153   return false;
154 }
155
156 char ModuleAnalysisManagerCGSCCProxy::PassID;
157
158 char FunctionAnalysisManagerCGSCCProxy::PassID;
159
160 FunctionAnalysisManagerCGSCCProxy::Result
161 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C) {
162   assert(FAM->empty() && "Function analyses ran prior to the CGSCC proxy!");
163   return Result(*FAM);
164 }
165
166 FunctionAnalysisManagerCGSCCProxy::Result::~Result() {
167   // Clear out the analysis manager if we're being destroyed -- it means we
168   // didn't even see an invalidate call when we got invalidated.
169   FAM->clear();
170 }
171
172 bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate(
173     LazyCallGraph::SCC &C, const PreservedAnalyses &PA) {
174   // If this proxy isn't marked as preserved, then we can't even invalidate
175   // individual function analyses, there may be an invalid set of Function
176   // objects in the cache making it impossible to incrementally preserve them.
177   // Just clear the entire manager.
178   if (!PA.preserved(ID()))
179     FAM->clear();
180
181   // Return false to indicate that this result is still a valid proxy.
182   return false;
183 }
184
185 char CGSCCAnalysisManagerFunctionProxy::PassID;