blob: 0defb6ab3e42c1bb22efc5ffc84f13494dae7d82 [file] [log] [blame]
Hans Wennborgf88287f2014-03-19 18:41:38 +00001//===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
Chandler Carruth74015a72013-11-13 01:12:08 +00002//
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"
Juergen Ributzka34390c72014-05-16 02:33:15 +000011#include "llvm/IR/LLVMContext.h"
Chandler Carrutha13f27c2014-01-11 11:52:05 +000012#include "llvm/IR/PassManager.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/Debug.h"
Chandler Carruth74015a72013-11-13 01:12:08 +000015
16using namespace llvm;
17
Chandler Carrutha13f27c2014-01-11 11:52:05 +000018static cl::opt<bool>
19DebugPM("debug-pass-manager", cl::Hidden,
20 cl::desc("Print pass management debugging information"));
21
Chandler Carruthb3e72192013-11-22 00:43:29 +000022PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000023 PreservedAnalyses PA = PreservedAnalyses::all();
Chandler Carrutha13f27c2014-01-11 11:52:05 +000024
25 if (DebugPM)
26 dbgs() << "Starting module pass manager run.\n";
27
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000028 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carrutha13f27c2014-01-11 11:52:05 +000029 if (DebugPM)
30 dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
31
Chandler Carruthb3e72192013-11-22 00:43:29 +000032 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000033 if (AM)
34 AM->invalidate(M, PassPA);
Chandler Carruth002da5d2014-03-02 04:08:41 +000035 PA.intersect(std::move(PassPA));
Juergen Ributzka34390c72014-05-16 02:33:15 +000036
37 M->getContext().yield();
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000038 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000039
40 if (DebugPM)
41 dbgs() << "Finished module pass manager run.\n";
42
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000043 return PA;
Chandler Carruthed1ffe02013-11-20 04:01:38 +000044}
45
Chandler Carrutheedf9fc2014-02-05 21:41:42 +000046ModuleAnalysisManager::ResultConceptT &
Chandler Carruthed1ffe02013-11-20 04:01:38 +000047ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
48 ModuleAnalysisResultMapT::iterator RI;
49 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +000050 std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
Chandler Carruthc3f3da32014-03-09 11:49:53 +000051 PassID, std::unique_ptr<detail::AnalysisResultConcept<Module *>>()));
Chandler Carruthed1ffe02013-11-20 04:01:38 +000052
Chandler Carruth16ea68e2013-11-26 11:24:37 +000053 // If we don't have a cached result for this module, look up the pass and run
54 // it to produce a result, which we then add to the cache.
55 if (Inserted)
Chandler Carruthc3f3da32014-03-09 11:49:53 +000056 RI->second = std::move(lookupPass(PassID).run(M, this));
Chandler Carruthed1ffe02013-11-20 04:01:38 +000057
58 return *RI->second;
59}
60
Chandler Carrutheedf9fc2014-02-05 21:41:42 +000061ModuleAnalysisManager::ResultConceptT *
Chandler Carruthde9afd82013-11-23 00:38:42 +000062ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const {
Chandler Carruth5ae74a62014-03-10 02:12:14 +000063 ModuleAnalysisResultMapT::const_iterator RI =
64 ModuleAnalysisResults.find(PassID);
Craig Topperc6207612014-04-09 06:08:46 +000065 return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second;
Chandler Carruthde9afd82013-11-23 00:38:42 +000066}
67
Chandler Carruthed1ffe02013-11-20 04:01:38 +000068void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
69 ModuleAnalysisResults.erase(PassID);
Chandler Carruth74015a72013-11-13 01:12:08 +000070}
71
Chandler Carruth16ea68e2013-11-26 11:24:37 +000072void ModuleAnalysisManager::invalidateImpl(Module *M,
73 const PreservedAnalyses &PA) {
74 // FIXME: This is a total hack based on the fact that erasure doesn't
75 // invalidate iteration for DenseMap.
76 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
77 E = ModuleAnalysisResults.end();
78 I != E; ++I)
79 if (I->second->invalidate(M, PA))
80 ModuleAnalysisResults.erase(I);
81}
82
Chandler Carruth5ae74a62014-03-10 02:12:14 +000083PreservedAnalyses FunctionPassManager::run(Function *F,
84 FunctionAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000085 PreservedAnalyses PA = PreservedAnalyses::all();
Chandler Carrutha13f27c2014-01-11 11:52:05 +000086
87 if (DebugPM)
88 dbgs() << "Starting function pass manager run.\n";
89
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000090 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carrutha13f27c2014-01-11 11:52:05 +000091 if (DebugPM)
92 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
93
Chandler Carruthb3e72192013-11-22 00:43:29 +000094 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000095 if (AM)
96 AM->invalidate(F, PassPA);
Chandler Carruth002da5d2014-03-02 04:08:41 +000097 PA.intersect(std::move(PassPA));
Juergen Ributzka34390c72014-05-16 02:33:15 +000098
99 F->getContext().yield();
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +0000100 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000101
102 if (DebugPM)
103 dbgs() << "Finished function pass manager run.\n";
104
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +0000105 return PA;
Chandler Carruth74015a72013-11-13 01:12:08 +0000106}
107
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000108bool FunctionAnalysisManager::empty() const {
109 assert(FunctionAnalysisResults.empty() ==
110 FunctionAnalysisResultLists.empty() &&
111 "The storage and index of analysis results disagree on how many there "
112 "are!");
113 return FunctionAnalysisResults.empty();
114}
115
116void FunctionAnalysisManager::clear() {
117 FunctionAnalysisResults.clear();
118 FunctionAnalysisResultLists.clear();
119}
120
Chandler Carrutheedf9fc2014-02-05 21:41:42 +0000121FunctionAnalysisManager::ResultConceptT &
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000122FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
123 FunctionAnalysisResultMapT::iterator RI;
124 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000125 std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000126 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
127
128 // If we don't have a cached result for this function, look up the pass and
129 // run it to produce a result, which we then add to the cache.
130 if (Inserted) {
131 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000132 ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this));
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000133 RI->second = std::prev(ResultList.end());
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000134 }
135
136 return *RI->second->second;
137}
138
Chandler Carrutheedf9fc2014-02-05 21:41:42 +0000139FunctionAnalysisManager::ResultConceptT *
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000140FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const {
141 FunctionAnalysisResultMapT::const_iterator RI =
142 FunctionAnalysisResults.find(std::make_pair(PassID, F));
Craig Topperc6207612014-04-09 06:08:46 +0000143 return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second;
Chandler Carruth16ea68e2013-11-26 11:24:37 +0000144}
145
146void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
147 FunctionAnalysisResultMapT::iterator RI =
148 FunctionAnalysisResults.find(std::make_pair(PassID, F));
149 if (RI == FunctionAnalysisResults.end())
150 return;
151
152 FunctionAnalysisResultLists[F].erase(RI->second);
153}
154
155void FunctionAnalysisManager::invalidateImpl(Function *F,
156 const PreservedAnalyses &PA) {
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000157 // Clear all the invalidated results associated specifically with this
Chandler Carruth74015a72013-11-13 01:12:08 +0000158 // function.
159 SmallVector<void *, 8> InvalidatedPassIDs;
160 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
161 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
162 E = ResultsList.end();
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000163 I != E;)
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000164 if (I->second->invalidate(F, PA)) {
Chandler Carruth8c60bc92013-11-15 21:56:44 +0000165 InvalidatedPassIDs.push_back(I->first);
166 I = ResultsList.erase(I);
167 } else {
168 ++I;
Chandler Carruth74015a72013-11-13 01:12:08 +0000169 }
170 while (!InvalidatedPassIDs.empty())
171 FunctionAnalysisResults.erase(
172 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
Chandler Carruthde37c462014-04-21 11:11:54 +0000173 if (ResultsList.empty())
174 FunctionAnalysisResultLists.erase(F);
Chandler Carruth74015a72013-11-13 01:12:08 +0000175}
176
Chandler Carruthb3e72192013-11-22 00:43:29 +0000177char FunctionAnalysisManagerModuleProxy::PassID;
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000178
Chandler Carruthb3e72192013-11-22 00:43:29 +0000179FunctionAnalysisManagerModuleProxy::Result
180FunctionAnalysisManagerModuleProxy::run(Module *M) {
Chandler Carruthb07f3782014-03-13 09:50:31 +0000181 assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
182 return Result(*FAM);
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000183}
184
Chandler Carruthb3e72192013-11-22 00:43:29 +0000185FunctionAnalysisManagerModuleProxy::Result::~Result() {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000186 // Clear out the analysis manager if we're being destroyed -- it means we
187 // didn't even see an invalidate call when we got invalidated.
Chandler Carruthb07f3782014-03-13 09:50:31 +0000188 FAM->clear();
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000189}
190
Chandler Carruthb3e72192013-11-22 00:43:29 +0000191bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
192 Module *M, const PreservedAnalyses &PA) {
Chandler Carruthbceeb222013-11-22 23:38:07 +0000193 // If this proxy isn't marked as preserved, then we can't even invalidate
194 // individual function analyses, there may be an invalid set of Function
195 // objects in the cache making it impossible to incrementally preserve them.
196 // Just clear the entire manager.
197 if (!PA.preserved(ID()))
Chandler Carruthb07f3782014-03-13 09:50:31 +0000198 FAM->clear();
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000199
200 // Return false to indicate that this result is still a valid proxy.
201 return false;
202}
Chandler Carruthc1ff9ed2013-11-23 01:25:07 +0000203
204char ModuleAnalysisManagerFunctionProxy::PassID;