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 auto &P = lookupPass(PassID);
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());
73 return *RI->second->second;
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;
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())
91 dbgs() << "Invalidating CGSCC analysis: " << lookupPass(PassID).name()
93 CGSCCAnalysisResultLists[&C].erase(RI->second);
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())
103 dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName()
106 // Clear all the invalidated results associated specifically with this
108 SmallVector<void *, 8> InvalidatedPassIDs;
109 CGSCCAnalysisResultListT &ResultsList = CGSCCAnalysisResultLists[&C];
110 for (CGSCCAnalysisResultListT::iterator I = ResultsList.begin(),
111 E = ResultsList.end();
113 if (I->second->invalidate(C, PA)) {
115 dbgs() << "Invalidating CGSCC analysis: " << lookupPass(I->first).name()
118 InvalidatedPassIDs.push_back(I->first);
119 I = ResultsList.erase(I);
123 while (!InvalidatedPassIDs.empty())
124 CGSCCAnalysisResults.erase(
125 std::make_pair(InvalidatedPassIDs.pop_back_val(), &C));
126 CGSCCAnalysisResultLists.erase(&C);
129 char CGSCCAnalysisManagerModuleProxy::PassID;
131 CGSCCAnalysisManagerModuleProxy::Result
132 CGSCCAnalysisManagerModuleProxy::run(Module &M) {
133 assert(CGAM->empty() && "CGSCC analyses ran prior to the module proxy!");
134 return Result(*CGAM);
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.
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()))
152 // Return false to indicate that this result is still a valid proxy.
156 char ModuleAnalysisManagerCGSCCProxy::PassID;
158 char FunctionAnalysisManagerCGSCCProxy::PassID;
160 FunctionAnalysisManagerCGSCCProxy::Result
161 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C) {
162 assert(FAM->empty() && "Function analyses ran prior to the CGSCC proxy!");
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.
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()))
181 // Return false to indicate that this result is still a valid proxy.
185 char CGSCCAnalysisManagerFunctionProxy::PassID;