1 //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/CGSCCPassManager.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/Debug.h"
17 DebugPM("debug-cgscc-pass-manager", cl::Hidden,
18 cl::desc("Print CGSCC pass management debugging information"));
20 PreservedAnalyses CGSCCPassManager::run(LazyCallGraph::SCC *C,
21 CGSCCAnalysisManager *AM) {
22 PreservedAnalyses PA = PreservedAnalyses::all();
25 dbgs() << "Starting CGSCC pass manager run.\n";
27 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
29 dbgs() << "Running CGSCC pass: " << Passes[Idx]->name() << "\n";
31 PreservedAnalyses PassPA = Passes[Idx]->run(C, AM);
33 AM->invalidate(C, PassPA);
34 PA.intersect(std::move(PassPA));
38 dbgs() << "Finished CGSCC pass manager run.\n";
43 bool CGSCCAnalysisManager::empty() const {
44 assert(CGSCCAnalysisResults.empty() == CGSCCAnalysisResultLists.empty() &&
45 "The storage and index of analysis results disagree on how many there "
47 return CGSCCAnalysisResults.empty();
50 void CGSCCAnalysisManager::clear() {
51 CGSCCAnalysisResults.clear();
52 CGSCCAnalysisResultLists.clear();
55 CGSCCAnalysisManager::ResultConceptT &
56 CGSCCAnalysisManager::getResultImpl(void *PassID, LazyCallGraph::SCC *C) {
57 CGSCCAnalysisResultMapT::iterator RI;
59 std::tie(RI, Inserted) = CGSCCAnalysisResults.insert(std::make_pair(
60 std::make_pair(PassID, C), CGSCCAnalysisResultListT::iterator()));
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.
65 CGSCCAnalysisResultListT &ResultList = CGSCCAnalysisResultLists[C];
66 ResultList.emplace_back(PassID, lookupPass(PassID).run(C, this));
67 RI->second = std::prev(ResultList.end());
70 return *RI->second->second;
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;
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())
87 CGSCCAnalysisResultLists[C].erase(RI->second);
90 void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC *C,
91 const PreservedAnalyses &PA) {
92 // Clear all the invalidated results associated specifically with this
94 SmallVector<void *, 8> InvalidatedPassIDs;
95 CGSCCAnalysisResultListT &ResultsList = CGSCCAnalysisResultLists[C];
96 for (CGSCCAnalysisResultListT::iterator I = ResultsList.begin(),
97 E = ResultsList.end();
99 if (I->second->invalidate(C, PA)) {
100 InvalidatedPassIDs.push_back(I->first);
101 I = ResultsList.erase(I);
105 while (!InvalidatedPassIDs.empty())
106 CGSCCAnalysisResults.erase(
107 std::make_pair(InvalidatedPassIDs.pop_back_val(), C));
108 CGSCCAnalysisResultLists.erase(C);
111 char CGSCCAnalysisManagerModuleProxy::PassID;
113 CGSCCAnalysisManagerModuleProxy::Result
114 CGSCCAnalysisManagerModuleProxy::run(Module *M) {
115 assert(CGAM->empty() && "CGSCC analyses ran prior to the module proxy!");
116 return Result(*CGAM);
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.
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()))
134 // Return false to indicate that this result is still a valid proxy.
138 char ModuleAnalysisManagerCGSCCProxy::PassID;
140 char FunctionAnalysisManagerCGSCCProxy::PassID;
142 FunctionAnalysisManagerCGSCCProxy::Result
143 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC *C) {
144 assert(FAM->empty() && "Function analyses ran prior to the CGSCC proxy!");
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.
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()))
163 // Return false to indicate that this result is still a valid proxy.
167 char CGSCCAnalysisManagerFunctionProxy::PassID;