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