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())
88 dbgs() << "Invalidating CGSCC analysis: " << lookupPass(PassID).name()
90 CGSCCAnalysisResultLists[&C].erase(RI->second);
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())
100 dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName()
103 // Clear all the invalidated results associated specifically with this
105 SmallVector<void *, 8> InvalidatedPassIDs;
106 CGSCCAnalysisResultListT &ResultsList = CGSCCAnalysisResultLists[&C];
107 for (CGSCCAnalysisResultListT::iterator I = ResultsList.begin(),
108 E = ResultsList.end();
110 if (I->second->invalidate(C, PA)) {
112 dbgs() << "Invalidating CGSCC analysis: " << lookupPass(I->first).name()
115 InvalidatedPassIDs.push_back(I->first);
116 I = ResultsList.erase(I);
120 while (!InvalidatedPassIDs.empty())
121 CGSCCAnalysisResults.erase(
122 std::make_pair(InvalidatedPassIDs.pop_back_val(), &C));
123 CGSCCAnalysisResultLists.erase(&C);
126 char CGSCCAnalysisManagerModuleProxy::PassID;
128 CGSCCAnalysisManagerModuleProxy::Result
129 CGSCCAnalysisManagerModuleProxy::run(Module &M) {
130 assert(CGAM->empty() && "CGSCC analyses ran prior to the module proxy!");
131 return Result(*CGAM);
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.
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()))
149 // Return false to indicate that this result is still a valid proxy.
153 char ModuleAnalysisManagerCGSCCProxy::PassID;
155 char FunctionAnalysisManagerCGSCCProxy::PassID;
157 FunctionAnalysisManagerCGSCCProxy::Result
158 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C) {
159 assert(FAM->empty() && "Function analyses ran prior to the CGSCC proxy!");
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.
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()))
178 // Return false to indicate that this result is still a valid proxy.
182 char CGSCCAnalysisManagerFunctionProxy::PassID;