blob: 88464e0ef0e5a30d7ad20529d54a43c57549eabc [file] [log] [blame]
Chandler Carruth74015a72013-11-13 01:12:08 +00001//===- PassManager.h - Infrastructure for managing & running IR 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
Chandler Carruth74015a72013-11-13 01:12:08 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carrutha13f27c2014-01-11 11:52:05 +000011#include "llvm/IR/PassManager.h"
12#include "llvm/Support/CommandLine.h"
13#include "llvm/Support/Debug.h"
Chandler Carruth74015a72013-11-13 01:12:08 +000014
15using namespace llvm;
16
Chandler Carrutha13f27c2014-01-11 11:52:05 +000017static cl::opt<bool>
18DebugPM("debug-pass-manager", cl::Hidden,
19 cl::desc("Print pass management debugging information"));
20
Chandler Carruthb3e72192013-11-22 00:43:29 +000021PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000022 PreservedAnalyses PA = PreservedAnalyses::all();
Chandler Carrutha13f27c2014-01-11 11:52:05 +000023
24 if (DebugPM)
25 dbgs() << "Starting module pass manager run.\n";
26
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000027 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carrutha13f27c2014-01-11 11:52:05 +000028 if (DebugPM)
29 dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
30
Chandler Carruthb3e72192013-11-22 00:43:29 +000031 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000032 if (AM)
33 AM->invalidate(M, PassPA);
Chandler Carruth002da5d2014-03-02 04:08:41 +000034 PA.intersect(std::move(PassPA));
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000035 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000036
37 if (DebugPM)
38 dbgs() << "Finished module pass manager run.\n";
39
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000040 return PA;
Chandler Carruthed1ffe02013-11-20 04:01:38 +000041}
42
Chandler Carrutheedf9fc2014-02-05 21:41:42 +000043ModuleAnalysisManager::ResultConceptT &
Chandler Carruthed1ffe02013-11-20 04:01:38 +000044ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
45 ModuleAnalysisResultMapT::iterator RI;
46 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +000047 std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
Chandler Carruthc3f3da32014-03-09 11:49:53 +000048 PassID, std::unique_ptr<detail::AnalysisResultConcept<Module *>>()));
Chandler Carruthed1ffe02013-11-20 04:01:38 +000049
Chandler Carruth16ea68e2013-11-26 11:24:37 +000050 // If we don't have a cached result for this module, look up the pass and run
51 // it to produce a result, which we then add to the cache.
52 if (Inserted)
Chandler Carruthc3f3da32014-03-09 11:49:53 +000053 RI->second = std::move(lookupPass(PassID).run(M, this));
Chandler Carruthed1ffe02013-11-20 04:01:38 +000054
55 return *RI->second;
56}
57
Chandler Carrutheedf9fc2014-02-05 21:41:42 +000058ModuleAnalysisManager::ResultConceptT *
Chandler Carruthde9afd82013-11-23 00:38:42 +000059ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
Chandler Carruth5ae74a62014-03-10 02:12:14 +000060 ModuleAnalysisResultMapT::const_iterator RI =
61 ModuleAnalysisResults.find(PassID);
Chandler Carruthde9afd82013-11-23 00:38:42 +000062 return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second;
63}
64
Chandler Carruthed1ffe02013-11-20 04:01:38 +000065void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
66 ModuleAnalysisResults.erase(PassID);
Chandler Carruth74015a72013-11-13 01:12:08 +000067}
68
Chandler Carruth16ea68e2013-11-26 11:24:37 +000069void ModuleAnalysisManager::invalidateImpl(Module *M,
70 const PreservedAnalyses &PA) {
71 // FIXME: This is a total hack based on the fact that erasure doesn't
72 // invalidate iteration for DenseMap.
73 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
74 E = ModuleAnalysisResults.end();
75 I != E; ++I)
76 if (I->second->invalidate(M, PA))
77 ModuleAnalysisResults.erase(I);
78}
79
Chandler Carruth5ae74a62014-03-10 02:12:14 +000080PreservedAnalyses FunctionPassManager::run(Function *F,
81 FunctionAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000082 PreservedAnalyses PA = PreservedAnalyses::all();
Chandler Carrutha13f27c2014-01-11 11:52:05 +000083
84 if (DebugPM)
85 dbgs() << "Starting function pass manager run.\n";
86
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000087 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carrutha13f27c2014-01-11 11:52:05 +000088 if (DebugPM)
89 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
90
Chandler Carruthb3e72192013-11-22 00:43:29 +000091 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000092 if (AM)
93 AM->invalidate(F, PassPA);
Chandler Carruth002da5d2014-03-02 04:08:41 +000094 PA.intersect(std::move(PassPA));
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000095 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000096
97 if (DebugPM)
98 dbgs() << "Finished function pass manager run.\n";
99
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +0000100 return PA;
Chandler Carruth74015a72013-11-13 01:12:08 +0000101}
102
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000103bool FunctionAnalysisManager::empty() const {
104 assert(FunctionAnalysisResults.empty() ==
105 FunctionAnalysisResultLists.empty() &&
106 "The storage and index of analysis results disagree on how many there "
107 "are!");
108 return FunctionAnalysisResults.empty();
109}
110
111void FunctionAnalysisManager::clear() {
112 FunctionAnalysisResults.clear();
113 FunctionAnalysisResultLists.clear();
114}
115
Chandler Carrutheedf9fc2014-02-05 21:41:42 +0000116FunctionAnalysisManager::ResultConceptT &
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000117FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
118 FunctionAnalysisResultMapT::iterator RI;
119 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000120 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000121 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
122
123 // If we don't have a cached result for this function, look up the pass and
124 // run it to produce a result, which we then add to the cache.
125 if (Inserted) {
126 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000127 ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000128 RI->second = std::prev(ResultList.end());
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000129 }
130
131 return *RI->second->second;
132}
133
Chandler Carrutheedf9fc2014-02-05 21:41:42 +0000134FunctionAnalysisManager::ResultConceptT *
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000135FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
136 FunctionAnalysisResultMapT::const_iterator RI =
137 FunctionAnalysisResults.find(std::make_pair(PassID, F));
138 return RI == FunctionAnalysisResults.end() ? 0 : &*RI->second->second;
139}
140
141void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
142 FunctionAnalysisResultMapT::iterator RI =
143 FunctionAnalysisResults.find(std::make_pair(PassID, F));
144 if (RI == FunctionAnalysisResults.end())
145 return;
146
147 FunctionAnalysisResultLists[F].erase(RI->second);
148}
149
150void FunctionAnalysisManager::invalidateImpl(Function *F,
151 const PreservedAnalyses &PA) {
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000152 // Clear all the invalidated results associated specifically with this
Chandler Carruth74015a72013-11-13 01:12:08 +0000153 // function.
154 SmallVector<void *, 8> InvalidatedPassIDs;
155 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
156 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
157 E = ResultsList.end();
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000158 I != E;)
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000159 if (I->second->invalidate(F, PA)) {
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000160 InvalidatedPassIDs.push_back(I->first);
161 I = ResultsList.erase(I);
162 } else {
163 ++I;
Chandler Carruth74015a72013-11-13 01:12:08 +0000164 }
165 while (!InvalidatedPassIDs.empty())
166 FunctionAnalysisResults.erase(
167 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
168}
169
Chandler Carruthb3e72192013-11-22 00:43:29 +0000170char FunctionAnalysisManagerModuleProxy::PassID;
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000171
Chandler Carruthb3e72192013-11-22 00:43:29 +0000172FunctionAnalysisManagerModuleProxy::Result
173FunctionAnalysisManagerModuleProxy::run(Module *M) {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000174 assert(FAM.empty() && "Function analyses ran prior to the module proxy!");
175 return Result(FAM);
176}
177
Chandler Carruthb3e72192013-11-22 00:43:29 +0000178FunctionAnalysisManagerModuleProxy::Result::~Result() {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000179 // Clear out the analysis manager if we're being destroyed -- it means we
180 // didn't even see an invalidate call when we got invalidated.
181 FAM.clear();
182}
183
Chandler Carruthb3e72192013-11-22 00:43:29 +0000184bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
185 Module *M, const PreservedAnalyses &PA) {
Chandler Carruthbceeb222013-11-22 23:38:07 +0000186 // If this proxy isn't marked as preserved, then we can't even invalidate
187 // individual function analyses, there may be an invalid set of Function
188 // objects in the cache making it impossible to incrementally preserve them.
189 // Just clear the entire manager.
190 if (!PA.preserved(ID()))
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000191 FAM.clear();
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000192
193 // Return false to indicate that this result is still a valid proxy.
194 return false;
195}
Chandler Carruthc1ff9ed2013-11-23 01:25:07 +0000196
197char ModuleAnalysisManagerFunctionProxy::PassID;