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