blob: 70533fef587c39442fd303c218d498cd2c082c38 [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);
34 PA.intersect(llvm_move(PassPA));
35 }
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 Carruth16ea68e2013-11-26 11:24:37 +000043const ModuleAnalysisManager::ResultConceptT &
Chandler Carruthed1ffe02013-11-20 04:01:38 +000044ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
45 ModuleAnalysisResultMapT::iterator RI;
46 bool Inserted;
47 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
Chandler Carruth5bf5e312013-11-22 11:34:43 +000048 PassID, polymorphic_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)
53 RI->second = lookupPass(PassID).run(M, this);
Chandler Carruthed1ffe02013-11-20 04:01:38 +000054
55 return *RI->second;
56}
57
Chandler Carruth16ea68e2013-11-26 11:24:37 +000058const ModuleAnalysisManager::ResultConceptT *
Chandler Carruthde9afd82013-11-23 00:38:42 +000059ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
60 ModuleAnalysisResultMapT::const_iterator RI = ModuleAnalysisResults.find(PassID);
61 return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second;
62}
63
Chandler Carruthed1ffe02013-11-20 04:01:38 +000064void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
65 ModuleAnalysisResults.erase(PassID);
Chandler Carruth74015a72013-11-13 01:12:08 +000066}
67
Chandler Carruth16ea68e2013-11-26 11:24:37 +000068void ModuleAnalysisManager::invalidateImpl(Module *M,
69 const PreservedAnalyses &PA) {
70 // FIXME: This is a total hack based on the fact that erasure doesn't
71 // invalidate iteration for DenseMap.
72 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
73 E = ModuleAnalysisResults.end();
74 I != E; ++I)
75 if (I->second->invalidate(M, PA))
76 ModuleAnalysisResults.erase(I);
77}
78
Chandler Carruthb3e72192013-11-22 00:43:29 +000079PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000080 PreservedAnalyses PA = PreservedAnalyses::all();
Chandler Carrutha13f27c2014-01-11 11:52:05 +000081
82 if (DebugPM)
83 dbgs() << "Starting function pass manager run.\n";
84
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000085 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carrutha13f27c2014-01-11 11:52:05 +000086 if (DebugPM)
87 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
88
Chandler Carruthb3e72192013-11-22 00:43:29 +000089 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000090 if (AM)
91 AM->invalidate(F, PassPA);
92 PA.intersect(llvm_move(PassPA));
93 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000094
95 if (DebugPM)
96 dbgs() << "Finished function pass manager run.\n";
97
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000098 return PA;
Chandler Carruth74015a72013-11-13 01:12:08 +000099}
100
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000101bool FunctionAnalysisManager::empty() const {
102 assert(FunctionAnalysisResults.empty() ==
103 FunctionAnalysisResultLists.empty() &&
104 "The storage and index of analysis results disagree on how many there "
105 "are!");
106 return FunctionAnalysisResults.empty();
107}
108
109void FunctionAnalysisManager::clear() {
110 FunctionAnalysisResults.clear();
111 FunctionAnalysisResultLists.clear();
112}
113
114const FunctionAnalysisManager::ResultConceptT &
115FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
116 FunctionAnalysisResultMapT::iterator RI;
117 bool Inserted;
118 llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
119 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
120
121 // If we don't have a cached result for this function, look up the pass and
122 // run it to produce a result, which we then add to the cache.
123 if (Inserted) {
124 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
125 ResultList.push_back(std::make_pair(PassID, lookupPass(PassID).run(F, this)));
126 RI->second = llvm::prior(ResultList.end());
127 }
128
129 return *RI->second->second;
130}
131
132const FunctionAnalysisManager::ResultConceptT *
133FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
134 FunctionAnalysisResultMapT::const_iterator RI =
135 FunctionAnalysisResults.find(std::make_pair(PassID, F));
136 return RI == FunctionAnalysisResults.end() ? 0 : &*RI->second->second;
137}
138
139void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
140 FunctionAnalysisResultMapT::iterator RI =
141 FunctionAnalysisResults.find(std::make_pair(PassID, F));
142 if (RI == FunctionAnalysisResults.end())
143 return;
144
145 FunctionAnalysisResultLists[F].erase(RI->second);
146}
147
148void FunctionAnalysisManager::invalidateImpl(Function *F,
149 const PreservedAnalyses &PA) {
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000150 // Clear all the invalidated results associated specifically with this
Chandler Carruth74015a72013-11-13 01:12:08 +0000151 // function.
152 SmallVector<void *, 8> InvalidatedPassIDs;
153 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
154 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
155 E = ResultsList.end();
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000156 I != E;)
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000157 if (I->second->invalidate(F, PA)) {
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000158 InvalidatedPassIDs.push_back(I->first);
159 I = ResultsList.erase(I);
160 } else {
161 ++I;
Chandler Carruth74015a72013-11-13 01:12:08 +0000162 }
163 while (!InvalidatedPassIDs.empty())
164 FunctionAnalysisResults.erase(
165 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
166}
167
Chandler Carruthb3e72192013-11-22 00:43:29 +0000168char FunctionAnalysisManagerModuleProxy::PassID;
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000169
Chandler Carruthb3e72192013-11-22 00:43:29 +0000170FunctionAnalysisManagerModuleProxy::Result
171FunctionAnalysisManagerModuleProxy::run(Module *M) {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000172 assert(FAM.empty() && "Function analyses ran prior to the module proxy!");
173 return Result(FAM);
174}
175
Chandler Carruthb3e72192013-11-22 00:43:29 +0000176FunctionAnalysisManagerModuleProxy::Result::~Result() {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000177 // Clear out the analysis manager if we're being destroyed -- it means we
178 // didn't even see an invalidate call when we got invalidated.
179 FAM.clear();
180}
181
Chandler Carruthb3e72192013-11-22 00:43:29 +0000182bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
183 Module *M, const PreservedAnalyses &PA) {
Chandler Carruthbceeb222013-11-22 23:38:07 +0000184 // If this proxy isn't marked as preserved, then we can't even invalidate
185 // individual function analyses, there may be an invalid set of Function
186 // objects in the cache making it impossible to incrementally preserve them.
187 // Just clear the entire manager.
188 if (!PA.preserved(ID()))
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000189 FAM.clear();
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000190
191 // Return false to indicate that this result is still a valid proxy.
192 return false;
193}
Chandler Carruthc1ff9ed2013-11-23 01:25:07 +0000194
195char ModuleAnalysisManagerFunctionProxy::PassID;