1 //===- PassManager.h - Infrastructure for managing & running IR 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/IR/PassManager.h"
11 #include "llvm/ADT/STLExtras.h"
15 PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
16 PreservedAnalyses PA = PreservedAnalyses::all();
17 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
18 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
20 AM->invalidate(M, PassPA);
21 PA.intersect(llvm_move(PassPA));
26 void ModuleAnalysisManager::invalidate(Module *M, const PreservedAnalyses &PA) {
27 // FIXME: This is a total hack based on the fact that erasure doesn't
28 // invalidate iteration for DenseMap.
29 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
30 E = ModuleAnalysisResults.end();
32 if (I->second->invalidate(M, PA))
33 ModuleAnalysisResults.erase(I);
36 const detail::AnalysisResultConcept<Module *> &
37 ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
38 ModuleAnalysisResultMapT::iterator RI;
40 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
41 PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module *> >()));
44 // We don't have a cached result for this result. Look up the pass and run
45 // it to produce a result, which we then add to the cache.
46 ModuleAnalysisPassMapT::const_iterator PI =
47 ModuleAnalysisPasses.find(PassID);
48 assert(PI != ModuleAnalysisPasses.end() &&
49 "Analysis passes must be registered prior to being queried!");
50 RI->second = PI->second->run(M, this);
56 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
57 ModuleAnalysisResults.erase(PassID);
60 PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) {
61 PreservedAnalyses PA = PreservedAnalyses::all();
62 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
63 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
65 AM->invalidate(F, PassPA);
66 PA.intersect(llvm_move(PassPA));
71 void FunctionAnalysisManager::invalidate(Function *F, const PreservedAnalyses &PA) {
72 // Clear all the invalidated results associated specifically with this
74 SmallVector<void *, 8> InvalidatedPassIDs;
75 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
76 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
77 E = ResultsList.end();
79 if (I->second->invalidate(F, PA)) {
80 InvalidatedPassIDs.push_back(I->first);
81 I = ResultsList.erase(I);
85 while (!InvalidatedPassIDs.empty())
86 FunctionAnalysisResults.erase(
87 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
90 bool FunctionAnalysisManager::empty() const {
91 assert(FunctionAnalysisResults.empty() ==
92 FunctionAnalysisResultLists.empty() &&
93 "The storage and index of analysis results disagree on how many there "
95 return FunctionAnalysisResults.empty();
98 void FunctionAnalysisManager::clear() {
99 FunctionAnalysisResults.clear();
100 FunctionAnalysisResultLists.clear();
103 const detail::AnalysisResultConcept<Function *> &
104 FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
105 FunctionAnalysisResultMapT::iterator RI;
107 llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
108 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
111 // We don't have a cached result for this result. Look up the pass and run
112 // it to produce a result, which we then add to the cache.
113 FunctionAnalysisPassMapT::const_iterator PI =
114 FunctionAnalysisPasses.find(PassID);
115 assert(PI != FunctionAnalysisPasses.end() &&
116 "Analysis passes must be registered prior to being queried!");
117 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
118 ResultList.push_back(std::make_pair(PassID, PI->second->run(F, this)));
119 RI->second = llvm::prior(ResultList.end());
122 return *RI->second->second;
125 void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
126 FunctionAnalysisResultMapT::iterator RI =
127 FunctionAnalysisResults.find(std::make_pair(PassID, F));
128 if (RI == FunctionAnalysisResults.end())
131 FunctionAnalysisResultLists[F].erase(RI->second);
134 char FunctionAnalysisManagerModuleProxy::PassID;
136 FunctionAnalysisManagerModuleProxy::Result
137 FunctionAnalysisManagerModuleProxy::run(Module *M) {
138 assert(FAM.empty() && "Function analyses ran prior to the module proxy!");
142 FunctionAnalysisManagerModuleProxy::Result::~Result() {
143 // Clear out the analysis manager if we're being destroyed -- it means we
144 // didn't even see an invalidate call when we got invalidated.
148 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
149 Module *M, const PreservedAnalyses &PA) {
150 // If this proxy isn't marked as preserved, then we can't even invalidate
151 // individual function analyses, there may be an invalid set of Function
152 // objects in the cache making it impossible to incrementally preserve them.
153 // Just clear the entire manager.
154 if (!PA.preserved(ID()))
157 // Return false to indicate that this result is still a valid proxy.