blob: 01d14f17bba5a4066eccd5b7c6c96cbbdb6b680e [file] [log] [blame]
Chandler Carruth49837ef2013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel55fd43f2006-11-07 21:31:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel55fd43f2006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruth49837ef2013-11-09 12:26:54 +000010// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel55fd43f2006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth49837ef2013-11-09 12:26:54 +000014#include "llvm/IR/LegacyPassManager.h"
Florian Hahnf48234e2018-05-24 21:33:17 +000015#include "llvm/ADT/MapVector.h"
James Henderson97f38492017-05-16 09:43:21 +000016#include "llvm/ADT/Statistic.h"
Jessica Paquette849da552018-05-18 17:26:39 +000017#include "llvm/IR/DiagnosticInfo.h"
Pavel Labath515a7232016-10-25 16:20:07 +000018#include "llvm/IR/IRPrintingPasses.h"
19#include "llvm/IR/LLVMContext.h"
Chandler Carruth49837ef2013-11-09 12:26:54 +000020#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruthf7591dd2014-03-04 12:32:42 +000021#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth974a4452014-01-07 11:48:04 +000022#include "llvm/IR/Module.h"
Fedor Sergeev9f6be222018-08-28 21:06:51 +000023#include "llvm/IR/PassTimingInfo.h"
Pavel Labath515a7232016-10-25 16:20:07 +000024#include "llvm/Support/Chrono.h"
Devang Patel45dc02d2006-12-13 20:03:48 +000025#include "llvm/Support/CommandLine.h"
David Greene170c48a2010-01-05 01:30:02 +000026#include "llvm/Support/Debug.h"
Peter Collingbourne76c218e2016-11-09 17:49:19 +000027#include "llvm/Support/Error.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000028#include "llvm/Support/ErrorHandling.h"
Devang Patel8e58a1b2006-12-14 00:59:42 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/Timer.h"
32#include "llvm/Support/raw_ostream.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000033#include <algorithm>
Weiming Zhaob85410e2016-01-06 22:55:03 +000034#include <unordered_set>
Dan Gohman2bb7d062007-10-03 19:04:09 +000035using namespace llvm;
Chandler Carruth49837ef2013-11-09 12:26:54 +000036using namespace llvm::legacy;
Devang Patelc2ff9622006-12-15 19:39:30 +000037
Devang Patelab7752c2007-01-12 18:52:44 +000038// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patele77242c2006-12-07 18:23:30 +000039
Devang Patel45dc02d2006-12-13 20:03:48 +000040//===----------------------------------------------------------------------===//
41// Pass debugging information. Often it is useful to find out what pass is
42// running when a crash occurs in a utility. When this library is compiled with
43// debugging on, a command line option (--debug-pass) is enabled that causes the
44// pass name to be printed before it executes.
45//
46
Chandler Carruth49837ef2013-11-09 12:26:54 +000047namespace {
Devang Patele8ff1ce2006-12-13 21:13:31 +000048// Different debug levels that can be enabled...
49enum PassDebugLevel {
Dmitri Gribenko5c332db2013-05-05 00:40:33 +000050 Disabled, Arguments, Structure, Executions, Details
Devang Patele8ff1ce2006-12-13 21:13:31 +000051};
Chandler Carruth49837ef2013-11-09 12:26:54 +000052}
Devang Patele8ff1ce2006-12-13 21:13:31 +000053
Andrew Trickabe68f52013-09-19 06:02:43 +000054static cl::opt<enum PassDebugLevel>
55PassDebugging("debug-pass", cl::Hidden,
Devang Patel45dc02d2006-12-13 20:03:48 +000056 cl::desc("Print PassManager debugging information"),
57 cl::values(
Andrew Trickabe68f52013-09-19 06:02:43 +000058 clEnumVal(Disabled , "disable debug output"),
59 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
60 clEnumVal(Structure , "print pass structure before run()"),
61 clEnumVal(Executions, "print pass name before it is executed"),
Mehdi Amini3ffe1132016-10-08 19:41:06 +000062 clEnumVal(Details , "print pass details when it is executed")));
David Greene5c8aa952010-04-02 23:17:14 +000063
Chandler Carruth49837ef2013-11-09 12:26:54 +000064namespace {
Andrew Trickabe68f52013-09-19 06:02:43 +000065typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
66PassOptionList;
Chandler Carruth49837ef2013-11-09 12:26:54 +000067}
Andrew Trickabe68f52013-09-19 06:02:43 +000068
69// Print IR out before/after specified passes.
70static PassOptionList
71PrintBefore("print-before",
72 llvm::cl::desc("Print IR before specified passes"),
73 cl::Hidden);
74
75static PassOptionList
76PrintAfter("print-after",
77 llvm::cl::desc("Print IR after specified passes"),
78 cl::Hidden);
79
Zachary Turner9a4e15c2017-12-01 00:53:10 +000080static cl::opt<bool> PrintBeforeAll("print-before-all",
81 llvm::cl::desc("Print IR before each pass"),
82 cl::init(false), cl::Hidden);
83static cl::opt<bool> PrintAfterAll("print-after-all",
84 llvm::cl::desc("Print IR after each pass"),
85 cl::init(false), cl::Hidden);
Andrew Trickabe68f52013-09-19 06:02:43 +000086
Fedor Sergeev7d160f72017-12-01 17:42:46 +000087static cl::opt<bool>
88 PrintModuleScope("print-module-scope",
89 cl::desc("When printing IR for print-[before|after]{-all} "
90 "always print a module IR"),
Craig Toppered7a0172018-04-01 21:54:26 +000091 cl::init(false), cl::Hidden);
Fedor Sergeev7d160f72017-12-01 17:42:46 +000092
Weiming Zhaob85410e2016-01-06 22:55:03 +000093static cl::list<std::string>
94 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
95 cl::desc("Only print IR for functions whose name "
96 "match this for all print-[before|after][-all] "
97 "options"),
Zachary Turner9a4e15c2017-12-01 00:53:10 +000098 cl::CommaSeparated, cl::Hidden);
Weiming Zhaob85410e2016-01-06 22:55:03 +000099
Andrew Trickabe68f52013-09-19 06:02:43 +0000100/// This is a helper to determine whether to print IR before or
101/// after a pass.
102
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000103bool llvm::shouldPrintBeforePass() {
104 return PrintBeforeAll || !PrintBefore.empty();
105}
106
107bool llvm::shouldPrintAfterPass() {
108 return PrintAfterAll || !PrintAfter.empty();
109}
110
111static bool ShouldPrintBeforeOrAfterPass(StringRef PassID,
Andrew Trickabe68f52013-09-19 06:02:43 +0000112 PassOptionList &PassesToPrint) {
Chris Bieneman054c7962015-04-29 21:45:22 +0000113 for (auto *PassInf : PassesToPrint) {
Andrew Trickabe68f52013-09-19 06:02:43 +0000114 if (PassInf)
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000115 if (PassInf->getPassArgument() == PassID) {
Andrew Trickabe68f52013-09-19 06:02:43 +0000116 return true;
117 }
David Greene5c8aa952010-04-02 23:17:14 +0000118 }
Andrew Trickabe68f52013-09-19 06:02:43 +0000119 return false;
120}
Dan Gohman95df6192010-08-12 23:50:08 +0000121
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000122bool llvm::shouldPrintBeforePass(StringRef PassID) {
123 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
Andrew Trickabe68f52013-09-19 06:02:43 +0000124}
David Greene5c8aa952010-04-02 23:17:14 +0000125
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000126bool llvm::shouldPrintAfterPass(StringRef PassID) {
127 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene5c8aa952010-04-02 23:17:14 +0000128}
129
Fedor Sergeev7d160f72017-12-01 17:42:46 +0000130bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
131
Weiming Zhaob85410e2016-01-06 22:55:03 +0000132bool llvm::isFunctionInPrintList(StringRef FunctionName) {
133 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
134 PrintFuncsList.end());
135 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
136}
Chris Lattner9554c612009-09-15 05:03:04 +0000137/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
138/// or higher is specified.
139bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickabe68f52013-09-19 06:02:43 +0000140 return PassDebugging >= Executions;
Chris Lattner9554c612009-09-15 05:03:04 +0000141}
142
Jessica Paquette9e212802018-09-06 21:19:54 +0000143unsigned PMDataManager::initSizeRemarkInfo(
144 Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) {
Jessica Paquette849da552018-05-18 17:26:39 +0000145 // Only calculate getInstructionCount if the size-info remark is requested.
Jessica Paquette9e212802018-09-06 21:19:54 +0000146 unsigned InstrCount = 0;
147
148 // Collect instruction counts for every function. We'll use this to emit
149 // per-function size remarks later.
150 for (Function &F : M) {
151 unsigned FCount = F.getInstructionCount();
152
153 // Insert a record into FunctionToInstrCount keeping track of the current
154 // size of the function as the first member of a pair. Set the second
155 // member to 0; if the function is deleted by the pass, then when we get
156 // here, we'll be able to let the user know that F no longer contributes to
157 // the module.
158 FunctionToInstrCount[F.getName().str()] =
159 std::pair<unsigned, unsigned>(FCount, 0);
160 InstrCount += FCount;
161 }
162 return InstrCount;
Jessica Paquette849da552018-05-18 17:26:39 +0000163}
Andrew Trickabe68f52013-09-19 06:02:43 +0000164
Jessica Paquette9e212802018-09-06 21:19:54 +0000165void PMDataManager::emitInstrCountChangedRemark(
166 Pass *P, Module &M, int64_t Delta, unsigned CountBefore,
167 StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount,
168 Function *F) {
Jessica Paquettea5d3acd2018-08-31 20:51:54 +0000169 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
170 // that the only passes that return non-null with getAsPMDataManager are pass
171 // managers.) The reason we have to do this is to avoid emitting remarks for
172 // CGSCC passes.
173 if (P->getAsPMDataManager())
174 return;
175
Jessica Paquette2618a112018-09-04 21:03:43 +0000176 // Set to true if this isn't a module pass or CGSCC pass.
177 bool CouldOnlyImpactOneFunction = (F != nullptr);
178
Jessica Paquette9e212802018-09-06 21:19:54 +0000179 // Helper lambda that updates the changes to the size of some function.
180 auto UpdateFunctionChanges =
181 [&FunctionToInstrCount](Function &MaybeChangedFn) {
182 // Update the total module count.
183 unsigned FnSize = MaybeChangedFn.getInstructionCount();
184 auto It = FunctionToInstrCount.find(MaybeChangedFn.getName());
185
186 // If we created a new function, then we need to add it to the map and
187 // say that it changed from 0 instructions to FnSize.
188 if (It == FunctionToInstrCount.end()) {
189 FunctionToInstrCount[MaybeChangedFn.getName()] =
190 std::pair<unsigned, unsigned>(0, FnSize);
191 return;
192 }
193 // Insert the new function size into the second member of the pair. This
194 // tells us whether or not this function changed in size.
195 It->second.second = FnSize;
196 };
197
198 // We need to initially update all of the function sizes.
199 // If no function was passed in, then we're either a module pass or an
200 // CGSCC pass.
201 if (!CouldOnlyImpactOneFunction)
202 std::for_each(M.begin(), M.end(), UpdateFunctionChanges);
203 else
204 UpdateFunctionChanges(*F);
205
Jessica Paquette87d50cf2018-08-31 20:54:37 +0000206 // Do we have a function we can use to emit a remark?
Jessica Paquette2618a112018-09-04 21:03:43 +0000207 if (!CouldOnlyImpactOneFunction) {
Jessica Paquette87d50cf2018-08-31 20:54:37 +0000208 // We need a function containing at least one basic block in order to output
209 // remarks. Since it's possible that the first function in the module
210 // doesn't actually contain a basic block, we have to go and find one that's
211 // suitable for emitting remarks.
212 auto It = std::find_if(M.begin(), M.end(),
213 [](const Function &Fn) { return !Fn.empty(); });
Jessica Paquette849da552018-05-18 17:26:39 +0000214
Jessica Paquette87d50cf2018-08-31 20:54:37 +0000215 // Didn't find a function. Quit.
216 if (It == M.end())
217 return;
Jessica Paquette849da552018-05-18 17:26:39 +0000218
Jessica Paquette87d50cf2018-08-31 20:54:37 +0000219 // We found a function containing at least one basic block.
220 F = &*It;
221 }
Jessica Paquette4f3a2192018-08-31 20:20:57 +0000222 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
Jessica Paquette849da552018-05-18 17:26:39 +0000223 BasicBlock &BB = *F->begin();
224 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
225 DiagnosticLocation(), &BB);
226 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
227 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
228 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
229 << ": IR instruction count changed from "
230 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
231 << " to "
232 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
233 << "; Delta: "
234 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
235 F->getContext().diagnose(R); // Not using ORE for layering reasons.
Jessica Paquette9e212802018-09-06 21:19:54 +0000236
237 // Emit per-function size change remarks separately.
238 std::string PassName = P->getPassName().str();
239
240 // Helper lambda that emits a remark when the size of a function has changed.
241 auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB,
242 &PassName](const std::string &Fname) {
243 unsigned FnCountBefore, FnCountAfter;
244 std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname];
245 std::tie(FnCountBefore, FnCountAfter) = Change;
246 int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
247 static_cast<int64_t>(FnCountBefore);
248
249 if (FnDelta == 0)
250 return;
251
252 // FIXME: We shouldn't use BB for the location here. Unfortunately, because
253 // the function that we're looking at could have been deleted, we can't use
254 // it for the source location. We *want* remarks when a function is deleted
255 // though, so we're kind of stuck here as is. (This remark, along with the
256 // whole-module size change remarks really ought not to have source
257 // locations at all.)
258 OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange",
259 DiagnosticLocation(), &BB);
260 FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName)
261 << ": Function: "
262 << DiagnosticInfoOptimizationBase::Argument("Function", Fname)
263 << ": IR instruction count changed from "
264 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore",
265 FnCountBefore)
266 << " to "
267 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter",
268 FnCountAfter)
269 << "; Delta: "
270 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta);
271 F->getContext().diagnose(FR);
272
273 // Update the function size.
274 Change.first = FnCountAfter;
275 };
276
277 // Are we looking at more than one function? If so, emit remarks for all of
278 // the functions in the module. Otherwise, only emit one remark.
279 if (!CouldOnlyImpactOneFunction)
280 std::for_each(FunctionToInstrCount.keys().begin(),
281 FunctionToInstrCount.keys().end(),
282 EmitFunctionSizeChangedRemark);
283 else
284 EmitFunctionSizeChangedRemark(F->getName().str());
Jessica Paquette849da552018-05-18 17:26:39 +0000285}
Andrew Trickabe68f52013-09-19 06:02:43 +0000286
Chris Lattnerd6f16582009-03-06 06:45:05 +0000287void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000288 if (!V && !M)
Chris Lattnerd6f16582009-03-06 06:45:05 +0000289 OS << "Releasing pass '";
290 else
291 OS << "Running pass '";
Dan Gohman95df6192010-08-12 23:50:08 +0000292
Chris Lattnerd6f16582009-03-06 06:45:05 +0000293 OS << P->getPassName() << "'";
Dan Gohman95df6192010-08-12 23:50:08 +0000294
Chris Lattnerd6f16582009-03-06 06:45:05 +0000295 if (M) {
296 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
297 return;
298 }
Craig Topperec0f0bc2014-04-09 06:08:46 +0000299 if (!V) {
Chris Lattnerd6f16582009-03-06 06:45:05 +0000300 OS << '\n';
301 return;
302 }
303
Dan Gohmanac57b122009-03-10 18:47:59 +0000304 OS << " on ";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000305 if (isa<Function>(V))
Dan Gohmanac57b122009-03-10 18:47:59 +0000306 OS << "function";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000307 else if (isa<BasicBlock>(V))
Dan Gohmanac57b122009-03-10 18:47:59 +0000308 OS << "basic block";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000309 else
Dan Gohmanac57b122009-03-10 18:47:59 +0000310 OS << "value";
311
312 OS << " '";
Chandler Carruth560e3952014-01-09 02:29:41 +0000313 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohmanac57b122009-03-10 18:47:59 +0000314 OS << "'\n";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000315}
316
317
Devang Patelc2ff9622006-12-15 19:39:30 +0000318namespace {
Devang Patel3f5d2b52006-12-07 19:21:29 +0000319//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000320// BBPassManager
Devang Patel7e601a72006-12-12 22:47:13 +0000321//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000322/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelc67c9382006-11-08 10:05:38 +0000323/// pass together and sequence them to process one basic block before
324/// processing next basic block.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000325class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelc67c9382006-11-08 10:05:38 +0000326
327public:
Devang Patel19974732007-05-03 01:11:54 +0000328 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000329 explicit BBPassManager()
330 : PMDataManager(), FunctionPass(ID) {}
Devang Patelc67c9382006-11-08 10:05:38 +0000331
Devang Patelc67c9382006-11-08 10:05:38 +0000332 /// Execute all of the passes scheduled for execution. Keep track of
333 /// whether any of the passes modifies the function, and if so, return true.
Craig Topper98f54c02014-03-05 06:35:38 +0000334 bool runOnFunction(Function &F) override;
Devang Patelc67c9382006-11-08 10:05:38 +0000335
Devang Patel66d72e12006-12-07 19:57:52 +0000336 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper98f54c02014-03-05 06:35:38 +0000337 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel66d72e12006-12-07 19:57:52 +0000338 Info.setPreservesAll();
339 }
340
Craig Topper98f54c02014-03-05 06:35:38 +0000341 bool doInitialization(Module &M) override;
Devang Patel964e45e2006-12-08 00:59:05 +0000342 bool doInitialization(Function &F);
Craig Topper98f54c02014-03-05 06:35:38 +0000343 bool doFinalization(Module &M) override;
Devang Patel964e45e2006-12-08 00:59:05 +0000344 bool doFinalization(Function &F);
345
Craig Topper98f54c02014-03-05 06:35:38 +0000346 PMDataManager *getAsPMDataManager() override { return this; }
347 Pass *getAsPass() override { return this; }
Chris Lattner3660eca2010-01-22 05:24:46 +0000348
Mehdi Amini67f335d2016-10-01 02:56:57 +0000349 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele27ae7e2007-02-01 22:08:25 +0000350
Devang Patelebc09222006-12-12 23:34:33 +0000351 // Print passes managed by this manager
Craig Topper98f54c02014-03-05 06:35:38 +0000352 void dumpPassStructure(unsigned Offset) override {
Eric Christopher0115a4e2014-02-26 23:27:16 +0000353 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +0000354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
355 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +0000356 BP->dumpPassStructure(Offset + 1);
Devang Patel1554c852006-12-16 00:56:26 +0000357 dumpLastUses(BP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +0000358 }
359 }
Devang Patel1554c852006-12-16 00:56:26 +0000360
361 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng310fa652012-11-13 02:56:38 +0000362 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patel1554c852006-12-16 00:56:26 +0000363 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
364 return BP;
365 }
Devang Patel25919cb2007-01-11 01:10:25 +0000366
Craig Topper98f54c02014-03-05 06:35:38 +0000367 PassManagerType getPassManagerType() const override {
Dan Gohman95df6192010-08-12 23:50:08 +0000368 return PMT_BasicBlockPassManager;
Devang Patel25919cb2007-01-11 01:10:25 +0000369 }
Devang Patelc67c9382006-11-08 10:05:38 +0000370};
371
Devang Patel19974732007-05-03 01:11:54 +0000372char BBPassManager::ID = 0;
Chandler Carruth49837ef2013-11-09 12:26:54 +0000373} // End anonymous namespace
Devang Patel5f4ddf52006-12-19 19:46:59 +0000374
Devang Patelab7752c2007-01-12 18:52:44 +0000375namespace llvm {
Chandler Carruth49837ef2013-11-09 12:26:54 +0000376namespace legacy {
Devang Patel7e601a72006-12-12 22:47:13 +0000377//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000378// FunctionPassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000379//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000380/// FunctionPassManagerImpl manages FPPassManagers
381class FunctionPassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000382 public PMDataManager,
383 public PMTopLevelManager {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000384 virtual void anchor();
Torok Edwin1970a892009-06-29 18:49:09 +0000385private:
386 bool wasRun;
Devang Patel5f4ddf52006-12-19 19:46:59 +0000387public:
Devang Patel19974732007-05-03 01:11:54 +0000388 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000389 explicit FunctionPassManagerImpl() :
390 Pass(PT_PassManager, ID), PMDataManager(),
391 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel5f4ddf52006-12-19 19:46:59 +0000392
Matthias Braune8210372014-12-12 01:27:01 +0000393 /// \copydoc FunctionPassManager::add()
Devang Patel5f4ddf52006-12-19 19:46:59 +0000394 void add(Pass *P) {
395 schedulePass(P);
396 }
Dan Gohman95df6192010-08-12 23:50:08 +0000397
398 /// createPrinterPass - Get a function printer pass.
Craig Topper98f54c02014-03-05 06:35:38 +0000399 Pass *createPrinterPass(raw_ostream &O,
400 const std::string &Banner) const override {
Chandler Carrutha5ced5e2014-01-12 11:30:46 +0000401 return createPrintFunctionPass(O, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000402 }
403
Torok Edwin1970a892009-06-29 18:49:09 +0000404 // Prepare for running an on the fly pass, freeing memory if needed
405 // from a previous run.
406 void releaseMemoryOnTheFly();
407
Devang Patel5f4ddf52006-12-19 19:46:59 +0000408 /// run - Execute all of the passes scheduled for execution. Keep track of
409 /// whether any of the passes modifies the module, and if so, return true.
410 bool run(Function &F);
411
412 /// doInitialization - Run all of the initializers for the function passes.
413 ///
Craig Topper98f54c02014-03-05 06:35:38 +0000414 bool doInitialization(Module &M) override;
Dan Gohman95df6192010-08-12 23:50:08 +0000415
Dan Gohman209ee182007-07-30 14:51:13 +0000416 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel5f4ddf52006-12-19 19:46:59 +0000417 ///
Craig Topper98f54c02014-03-05 06:35:38 +0000418 bool doFinalization(Module &M) override;
Devang Patel5f4ddf52006-12-19 19:46:59 +0000419
Dan Gohman95df6192010-08-12 23:50:08 +0000420
Craig Topper98f54c02014-03-05 06:35:38 +0000421 PMDataManager *getAsPMDataManager() override { return this; }
422 Pass *getAsPass() override { return this; }
423 PassManagerType getTopLevelPassManagerType() override {
Andrew Trick11e43292012-02-01 07:16:20 +0000424 return PMT_FunctionPassManager;
425 }
Chris Lattner3660eca2010-01-22 05:24:46 +0000426
Devang Patel5f4ddf52006-12-19 19:46:59 +0000427 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper98f54c02014-03-05 06:35:38 +0000428 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel5f4ddf52006-12-19 19:46:59 +0000429 Info.setPreservesAll();
430 }
431
Devang Patel5f4ddf52006-12-19 19:46:59 +0000432 FPPassManager *getContainedManager(unsigned N) {
Chris Lattnerf9574362009-03-06 05:53:14 +0000433 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel5f4ddf52006-12-19 19:46:59 +0000434 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
435 return FP;
436 }
Devang Patel5f4ddf52006-12-19 19:46:59 +0000437};
438
David Blaikie2d24e2a2011-12-20 02:50:00 +0000439void FunctionPassManagerImpl::anchor() {}
440
Devang Patel19974732007-05-03 01:11:54 +0000441char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000442} // End of legacy namespace
443} // End of llvm namespace
Dan Gohman95df6192010-08-12 23:50:08 +0000444
Chandler Carruth49837ef2013-11-09 12:26:54 +0000445namespace {
Devang Patel5f4ddf52006-12-19 19:46:59 +0000446//===----------------------------------------------------------------------===//
447// MPPassManager
448//
449/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmanb4b28232008-03-11 16:18:48 +0000450/// It batches all Module passes and function pass managers together and
451/// sequences them to process one module.
Devang Patel5f4ddf52006-12-19 19:46:59 +0000452class MPPassManager : public Pass, public PMDataManager {
Devang Patelc67c9382006-11-08 10:05:38 +0000453public:
Devang Patel19974732007-05-03 01:11:54 +0000454 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000455 explicit MPPassManager() :
456 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel693941b2007-04-16 20:39:59 +0000457
458 // Delete on the fly managers.
Alexander Kornienkoc16fc542015-04-11 02:11:45 +0000459 ~MPPassManager() override {
Yaron Keren485adca2015-06-05 17:48:47 +0000460 for (auto &OnTheFlyManager : OnTheFlyManagers) {
461 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel693941b2007-04-16 20:39:59 +0000462 delete FPP;
463 }
464 }
465
Dan Gohman95df6192010-08-12 23:50:08 +0000466 /// createPrinterPass - Get a module printer pass.
Craig Topper98f54c02014-03-05 06:35:38 +0000467 Pass *createPrinterPass(raw_ostream &O,
468 const std::string &Banner) const override {
Chandler Carrutha5ced5e2014-01-12 11:30:46 +0000469 return createPrintModulePass(O, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000470 }
471
Devang Patelc67c9382006-11-08 10:05:38 +0000472 /// run - Execute all of the passes scheduled for execution. Keep track of
473 /// whether any of the passes modifies the module, and if so, return true.
474 bool runOnModule(Module &M);
Devang Patelbe6d5152006-11-13 22:40:09 +0000475
Pedro Artigas49eb6282012-12-03 21:56:57 +0000476 using llvm::Pass::doInitialization;
477 using llvm::Pass::doFinalization;
478
Devang Patel66d72e12006-12-07 19:57:52 +0000479 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper98f54c02014-03-05 06:35:38 +0000480 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel66d72e12006-12-07 19:57:52 +0000481 Info.setPreservesAll();
482 }
483
Devang Patel569a6fd2007-04-16 20:12:57 +0000484 /// Add RequiredPass into list of lower level passes required by pass P.
485 /// RequiredPass is run on the fly by Pass Manager when P requests it
486 /// through getAnalysis interface.
Craig Topper98f54c02014-03-05 06:35:38 +0000487 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patel569a6fd2007-04-16 20:12:57 +0000488
Dan Gohman95df6192010-08-12 23:50:08 +0000489 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel0ed8df32007-04-16 20:27:05 +0000490 /// required by module pass MP. Instantiate analysis pass, by using
491 /// its runOnFunction() for function F.
Craig Topper98f54c02014-03-05 06:35:38 +0000492 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel0ed8df32007-04-16 20:27:05 +0000493
Mehdi Amini67f335d2016-10-01 02:56:57 +0000494 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele27ae7e2007-02-01 22:08:25 +0000495
Craig Topper98f54c02014-03-05 06:35:38 +0000496 PMDataManager *getAsPMDataManager() override { return this; }
497 Pass *getAsPass() override { return this; }
Chris Lattner3660eca2010-01-22 05:24:46 +0000498
Devang Patelebc09222006-12-12 23:34:33 +0000499 // Print passes managed by this manager
Craig Topper98f54c02014-03-05 06:35:38 +0000500 void dumpPassStructure(unsigned Offset) override {
Eric Christopher0115a4e2014-02-26 23:27:16 +0000501 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +0000502 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
503 ModulePass *MP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +0000504 MP->dumpPassStructure(Offset + 1);
Florian Hahnf48234e2018-05-24 21:33:17 +0000505 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
506 OnTheFlyManagers.find(MP);
Dan Gohman82c32c42009-07-01 23:12:33 +0000507 if (I != OnTheFlyManagers.end())
508 I->second->dumpPassStructure(Offset + 2);
Devang Patel1554c852006-12-16 00:56:26 +0000509 dumpLastUses(MP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +0000510 }
511 }
512
Devang Patel1554c852006-12-16 00:56:26 +0000513 ModulePass *getContainedPass(unsigned N) {
Evan Cheng310fa652012-11-13 02:56:38 +0000514 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattnerf9574362009-03-06 05:53:14 +0000515 return static_cast<ModulePass *>(PassVector[N]);
Devang Patel1554c852006-12-16 00:56:26 +0000516 }
517
Craig Topper98f54c02014-03-05 06:35:38 +0000518 PassManagerType getPassManagerType() const override {
Dan Gohman95df6192010-08-12 23:50:08 +0000519 return PMT_ModulePassManager;
Devang Patel84da80d2007-02-27 15:00:39 +0000520 }
Devang Patel0ed8df32007-04-16 20:27:05 +0000521
522 private:
523 /// Collection of on the fly FPPassManagers. These managers manage
524 /// function passes that are required by module passes.
Florian Hahnf48234e2018-05-24 21:33:17 +0000525 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelc67c9382006-11-08 10:05:38 +0000526};
527
Devang Patel19974732007-05-03 01:11:54 +0000528char MPPassManager::ID = 0;
Chandler Carruth49837ef2013-11-09 12:26:54 +0000529} // End anonymous namespace
530
531namespace llvm {
532namespace legacy {
Devang Patel7e601a72006-12-12 22:47:13 +0000533//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000534// PassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000535//
Devang Patel794fd752007-05-01 21:15:47 +0000536
Devang Patel5f4ddf52006-12-19 19:46:59 +0000537/// PassManagerImpl manages MPPassManagers
538class PassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000539 public PMDataManager,
540 public PMTopLevelManager {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000541 virtual void anchor();
Devang Patel5a39b2e2006-11-08 10:29:57 +0000542
543public:
Devang Patel19974732007-05-03 01:11:54 +0000544 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000545 explicit PassManagerImpl() :
546 Pass(PT_PassManager, ID), PMDataManager(),
547 PMTopLevelManager(new MPPassManager()) {}
Devang Patelf72d29c2006-12-07 23:24:58 +0000548
Matthias Braune8210372014-12-12 01:27:01 +0000549 /// \copydoc PassManager::add()
Devang Patel877bfbb2006-12-07 21:32:57 +0000550 void add(Pass *P) {
Devang Patele61b7472006-12-08 22:34:02 +0000551 schedulePass(P);
Devang Patel877bfbb2006-12-07 21:32:57 +0000552 }
Dan Gohman95df6192010-08-12 23:50:08 +0000553
554 /// createPrinterPass - Get a module printer pass.
Craig Topper98f54c02014-03-05 06:35:38 +0000555 Pass *createPrinterPass(raw_ostream &O,
556 const std::string &Banner) const override {
Chandler Carrutha5ced5e2014-01-12 11:30:46 +0000557 return createPrintModulePass(O, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000558 }
559
Devang Patel5a39b2e2006-11-08 10:29:57 +0000560 /// run - Execute all of the passes scheduled for execution. Keep track of
561 /// whether any of the passes modifies the module, and if so, return true.
562 bool run(Module &M);
563
Pedro Artigas49eb6282012-12-03 21:56:57 +0000564 using llvm::Pass::doInitialization;
565 using llvm::Pass::doFinalization;
566
Devang Patel66d72e12006-12-07 19:57:52 +0000567 /// Pass Manager itself does not invalidate any analysis info.
Craig Topper98f54c02014-03-05 06:35:38 +0000568 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel66d72e12006-12-07 19:57:52 +0000569 Info.setPreservesAll();
570 }
571
Craig Topper98f54c02014-03-05 06:35:38 +0000572 PMDataManager *getAsPMDataManager() override { return this; }
573 Pass *getAsPass() override { return this; }
574 PassManagerType getTopLevelPassManagerType() override {
Andrew Trick11e43292012-02-01 07:16:20 +0000575 return PMT_ModulePassManager;
576 }
Chris Lattner3660eca2010-01-22 05:24:46 +0000577
Devang Patel5f4ddf52006-12-19 19:46:59 +0000578 MPPassManager *getContainedManager(unsigned N) {
Chris Lattnerf9574362009-03-06 05:53:14 +0000579 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel5f4ddf52006-12-19 19:46:59 +0000580 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
581 return MP;
582 }
Devang Patel5a39b2e2006-11-08 10:29:57 +0000583};
584
David Blaikie2d24e2a2011-12-20 02:50:00 +0000585void PassManagerImpl::anchor() {}
586
Devang Patel19974732007-05-03 01:11:54 +0000587char PassManagerImpl::ID = 0;
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000588} // End of legacy namespace
589} // End of llvm namespace
Devang Patelc874eb52007-01-29 23:10:37 +0000590
Devang Patel06e86562006-12-07 19:39:39 +0000591//===----------------------------------------------------------------------===//
Devang Patel1b8d0152006-12-12 22:35:25 +0000592// PMTopLevelManager implementation
593
Devang Patel8f3f3d12007-01-16 02:00:38 +0000594/// Initialize top level manager. Create first pass manager.
Dan Gohman7578ea82010-08-16 21:38:42 +0000595PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
596 PMDM->setTopLevelManager(this);
597 addPassManager(PMDM);
598 activeStack.push(PMDM);
Devang Patel8f3f3d12007-01-16 02:00:38 +0000599}
600
Devang Patel1b8d0152006-12-12 22:35:25 +0000601/// Set pass P as the last user of the given analysis passes.
Dan Gohman568a63d2010-10-12 00:12:29 +0000602void
Bill Wendlingc6db6b62012-05-14 07:53:40 +0000603PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grossere9069212011-01-20 21:03:22 +0000604 unsigned PDepth = 0;
605 if (P->getResolver())
606 PDepth = P->getResolver()->getPMDataManager().getDepth();
607
Yaron Keren485adca2015-06-05 17:48:47 +0000608 for (Pass *AP : AnalysisPasses) {
Devang Patel1b8d0152006-12-12 22:35:25 +0000609 LastUser[AP] = P;
Dan Gohman95df6192010-08-12 23:50:08 +0000610
Devang Pateld46825c2007-03-08 19:05:01 +0000611 if (P == AP)
612 continue;
613
Tobias Grossere9069212011-01-20 21:03:22 +0000614 // Update the last users of passes that are required transitive by AP.
615 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
616 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
617 SmallVector<Pass *, 12> LastUses;
618 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren85c12152016-04-28 14:49:44 +0000619 for (AnalysisID ID : IDs) {
620 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grossere9069212011-01-20 21:03:22 +0000621 assert(AnalysisPass && "Expected analysis pass to exist.");
622 AnalysisResolver *AR = AnalysisPass->getResolver();
623 assert(AR && "Expected analysis resolver to exist.");
624 unsigned APDepth = AR->getPMDataManager().getDepth();
625
626 if (PDepth == APDepth)
627 LastUses.push_back(AnalysisPass);
628 else if (PDepth > APDepth)
629 LastPMUses.push_back(AnalysisPass);
630 }
631
632 setLastUser(LastUses, P);
633
634 // If this pass has a corresponding pass manager, push higher level
635 // analysis to this pass manager.
636 if (P->getResolver())
637 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
638
639
Devang Patel1b8d0152006-12-12 22:35:25 +0000640 // If AP is the last user of other passes then make P last user of
641 // such passes.
Yaron Kerenc36f8002016-10-02 19:21:41 +0000642 for (auto LU : LastUser) {
643 if (LU.second == AP)
Devang Patel721e59c2008-08-12 00:26:16 +0000644 // DenseMap iterator is not invalidated here because
Tobias Grossere9069212011-01-20 21:03:22 +0000645 // this is just updating existing entries.
Yaron Kerenc36f8002016-10-02 19:21:41 +0000646 LastUser[LU.first] = P;
Devang Patel1b8d0152006-12-12 22:35:25 +0000647 }
648 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000649}
650
651/// Collect passes whose last user is P
Dan Gohmanebb18342010-10-12 00:11:18 +0000652void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patel721e59c2008-08-12 00:26:16 +0000653 Pass *P) {
Dan Gohman95df6192010-08-12 23:50:08 +0000654 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patel721e59c2008-08-12 00:26:16 +0000655 InversedLastUser.find(P);
656 if (DMI == InversedLastUser.end())
657 return;
658
659 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper273fd112014-08-24 23:23:06 +0000660 for (Pass *LUP : LU) {
661 LastUses.push_back(LUP);
Devang Patel721e59c2008-08-12 00:26:16 +0000662 }
663
Devang Patel1b8d0152006-12-12 22:35:25 +0000664}
665
Devang Patel3b8a9062008-08-11 21:13:39 +0000666AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000667 AnalysisUsage *AnUsage = nullptr;
Philip Reames697fe022015-12-04 20:05:04 +0000668 auto DMI = AnUsageMap.find(P);
Dan Gohman95df6192010-08-12 23:50:08 +0000669 if (DMI != AnUsageMap.end())
Devang Patel3b8a9062008-08-11 21:13:39 +0000670 AnUsage = DMI->second;
671 else {
Philip Reames697fe022015-12-04 20:05:04 +0000672 // Look up the analysis usage from the pass instance (different instances
673 // of the same pass can produce different results), but unique the
674 // resulting object to reduce memory usage. This helps to greatly reduce
675 // memory usage when we have many instances of only a few pass types
676 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
677 // of dependencies.
678 AnalysisUsage AU;
679 P->getAnalysisUsage(AU);
Bjorn Petterssona1ff84e2018-07-03 12:39:52 +0000680
Philip Reames697fe022015-12-04 20:05:04 +0000681 AUFoldingSetNode* Node = nullptr;
682 FoldingSetNodeID ID;
683 AUFoldingSetNode::Profile(ID, AU);
684 void *IP = nullptr;
685 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
686 Node = N;
687 else {
Philip Reamesf79b7832015-12-04 23:48:19 +0000688 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reames697fe022015-12-04 20:05:04 +0000689 UniqueAnalysisUsages.InsertNode(Node, IP);
690 }
691 assert(Node && "cached analysis usage must be non null");
692
693 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang8c3abee2017-06-06 05:08:36 +0000694 AnUsage = &Node->AU;
Devang Patel3b8a9062008-08-11 21:13:39 +0000695 }
696 return AnUsage;
697}
698
Devang Patel1b8d0152006-12-12 22:35:25 +0000699/// Schedule pass P for execution. Make sure that passes required by
700/// P are run before P is run. Update analysis info maintained by
701/// the manager. Remove dead passes. This is a recursive function.
702void PMTopLevelManager::schedulePass(Pass *P) {
703
Devang Patel9d133e12007-01-16 21:43:18 +0000704 // TODO : Allocate function manager for this pass, other wise required set
705 // may be inserted into previous function manager
Devang Patel1b8d0152006-12-12 22:35:25 +0000706
Devang Patel22a1cf92007-03-06 01:06:16 +0000707 // Give pass a chance to prepare the stage.
708 P->preparePassManager(activeStack);
709
Devang Patel1cee94f2008-03-18 00:39:19 +0000710 // If P is an analysis pass and it is available then do not
711 // generate the analysis again. Stale analysis info should not be
712 // available at this point.
Chandler Carruth69774fa2015-01-28 09:47:21 +0000713 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Anderson90c579d2010-08-06 18:33:48 +0000714 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topper6b498e02018-08-20 20:57:30 +0000715 // Remove any cached AnalysisUsage information.
716 AnUsageMap.erase(P);
Nuno Lopes641397f2008-11-04 23:03:58 +0000717 delete P;
Devang Patelc7fe32e2008-03-19 00:48:41 +0000718 return;
Nuno Lopes641397f2008-11-04 23:03:58 +0000719 }
Devang Patel1cee94f2008-03-18 00:39:19 +0000720
Devang Patel3b8a9062008-08-11 21:13:39 +0000721 AnalysisUsage *AnUsage = findAnalysisUsage(P);
722
Devang Patel488dc672008-08-14 23:07:48 +0000723 bool checkAnalysis = true;
724 while (checkAnalysis) {
725 checkAnalysis = false;
Dan Gohman95df6192010-08-12 23:50:08 +0000726
Devang Patel488dc672008-08-14 23:07:48 +0000727 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn91acfe52017-07-13 10:52:00 +0000728 for (const AnalysisID ID : RequiredSet) {
Dan Gohman95df6192010-08-12 23:50:08 +0000729
Florian Hahn91acfe52017-07-13 10:52:00 +0000730 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patel488dc672008-08-14 23:07:48 +0000731 if (!AnalysisPass) {
Florian Hahn91acfe52017-07-13 10:52:00 +0000732 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000733
Craig Topperec0f0bc2014-04-09 06:08:46 +0000734 if (!PI) {
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000735 // Pass P is not in the global PassRegistry
736 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
737 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
738 dbgs() << "Required Passes:" << "\n";
Florian Hahn91acfe52017-07-13 10:52:00 +0000739 for (const AnalysisID ID2 : RequiredSet) {
740 if (ID == ID2)
741 break;
742 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000743 if (AnalysisPass2) {
744 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper97fe3d92013-02-06 06:50:38 +0000745 } else {
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000746 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
747 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
748 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
749 }
750 }
751 }
752
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000753 assert(PI && "Expected required passes to be initialized");
Owen Anderson90c579d2010-08-06 18:33:48 +0000754 AnalysisPass = PI->createPass();
Devang Patel488dc672008-08-14 23:07:48 +0000755 if (P->getPotentialPassManagerType () ==
756 AnalysisPass->getPotentialPassManagerType())
757 // Schedule analysis pass that is managed by the same pass manager.
758 schedulePass(AnalysisPass);
759 else if (P->getPotentialPassManagerType () >
760 AnalysisPass->getPotentialPassManagerType()) {
761 // Schedule analysis pass that is managed by a new manager.
762 schedulePass(AnalysisPass);
Dan Gohman3620ff92010-08-16 22:57:28 +0000763 // Recheck analysis passes to ensure that required analyses that
Devang Patel488dc672008-08-14 23:07:48 +0000764 // are already checked are still available.
765 checkAnalysis = true;
Craig Topper97fe3d92013-02-06 06:50:38 +0000766 } else
Chad Rosierdda7fcb2015-03-20 15:45:14 +0000767 // Do not schedule this analysis. Lower level analysis
Devang Patel488dc672008-08-14 23:07:48 +0000768 // passes are run on the fly.
769 delete AnalysisPass;
770 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000771 }
772 }
773
774 // Now all required passes are available.
Andrew Trick11e43292012-02-01 07:16:20 +0000775 if (ImmutablePass *IP = P->getAsImmutablePass()) {
776 // P is a immutable pass and it will be managed by this
777 // top level manager. Set up analysis resolver to connect them.
778 PMDataManager *DM = getAsPMDataManager();
779 AnalysisResolver *AR = new AnalysisResolver(*DM);
780 P->setResolver(AR);
781 DM->initializeAnalysisImpl(P);
782 addImmutablePass(IP);
783 DM->recordAvailableAnalysis(IP);
784 return;
785 }
786
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000787 if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) {
Andrew Trick11e43292012-02-01 07:16:20 +0000788 Pass *PP = P->createPrinterPass(
Mehdi Amini67f335d2016-10-01 02:56:57 +0000789 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trick11e43292012-02-01 07:16:20 +0000790 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
791 }
792
793 // Add the requested pass to the best available pass manager.
794 P->assignPassManager(activeStack, getTopLevelPassManagerType());
795
Fedor Sergeeva7c23702018-09-24 16:08:15 +0000796 if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) {
Andrew Trick11e43292012-02-01 07:16:20 +0000797 Pass *PP = P->createPrinterPass(
Mehdi Amini67f335d2016-10-01 02:56:57 +0000798 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trick11e43292012-02-01 07:16:20 +0000799 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
800 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000801}
802
803/// Find the pass that implements Analysis AID. Search immutable
804/// passes and all pass managers. If desired pass is not found
805/// then return NULL.
806Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb29f7b82015-09-10 02:31:42 +0000807 // For immutable passes we have a direct mapping from ID to pass, so check
808 // that first.
809 if (Pass *P = ImmutablePassMap.lookup(AID))
810 return P;
Devang Patel1b8d0152006-12-12 22:35:25 +0000811
Devang Pateld0fa16c2006-12-12 22:50:05 +0000812 // Check pass managers
Yaron Keren485adca2015-06-05 17:48:47 +0000813 for (PMDataManager *PassManager : PassManagers)
814 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman7c347302010-10-11 23:19:01 +0000815 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000816
817 // Check other pass managers
Yaron Keren485adca2015-06-05 17:48:47 +0000818 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
819 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman7c347302010-10-11 23:19:01 +0000820 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000821
Craig Topperec0f0bc2014-04-09 06:08:46 +0000822 return nullptr;
Devang Patel1b8d0152006-12-12 22:35:25 +0000823}
824
Chandler Carruth69774fa2015-01-28 09:47:21 +0000825const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
826 const PassInfo *&PI = AnalysisPassInfos[AID];
827 if (!PI)
828 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
829 else
830 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
831 "The pass info pointer changed for an analysis ID!");
832
833 return PI;
834}
835
Chandler Carruthb29f7b82015-09-10 02:31:42 +0000836void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
837 P->initializePass();
838 ImmutablePasses.push_back(P);
839
840 // Add this pass to the map from its analysis ID. We clobber any prior runs
841 // of the pass in the map so that the last one added is the one found when
842 // doing lookups.
843 AnalysisID AID = P->getPassID();
844 ImmutablePassMap[AID] = P;
845
846 // Also add any interfaces implemented by the immutable pass to the map for
847 // fast lookup.
848 const PassInfo *PassInf = findAnalysisPassInfo(AID);
849 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth9c7d2782015-09-10 04:22:36 +0000850 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb29f7b82015-09-10 02:31:42 +0000851 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
852}
853
Devang Patelebc09222006-12-12 23:34:33 +0000854// Print passes managed by this top level manager.
Devang Patela52035a2006-12-15 20:13:01 +0000855void PMTopLevelManager::dumpPasses() const {
Devang Patelebc09222006-12-12 23:34:33 +0000856
Andrew Trickabe68f52013-09-19 06:02:43 +0000857 if (PassDebugging < Structure)
Devang Patel5f4ddf52006-12-19 19:46:59 +0000858 return;
859
Devang Patelebc09222006-12-12 23:34:33 +0000860 // Print out the immutable passes
861 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohman8a757ae2010-08-19 01:29:07 +0000862 ImmutablePasses[i]->dumpPassStructure(0);
Devang Patelebc09222006-12-12 23:34:33 +0000863 }
Dan Gohman95df6192010-08-12 23:50:08 +0000864
Dan Gohman8a757ae2010-08-19 01:29:07 +0000865 // Every class that derives from PMDataManager also derives from Pass
866 // (sometimes indirectly), but there's no inheritance relationship
867 // between PMDataManager and Pass, so we have to getAsPass to get
868 // from a PMDataManager* to a Pass*.
Yaron Keren485adca2015-06-05 17:48:47 +0000869 for (PMDataManager *Manager : PassManagers)
870 Manager->getAsPass()->dumpPassStructure(1);
Devang Patelebc09222006-12-12 23:34:33 +0000871}
872
Devang Patela52035a2006-12-15 20:13:01 +0000873void PMTopLevelManager::dumpArguments() const {
Devang Patelc32cf542006-12-13 22:10:00 +0000874
Andrew Trickabe68f52013-09-19 06:02:43 +0000875 if (PassDebugging < Arguments)
Devang Patelc32cf542006-12-13 22:10:00 +0000876 return;
877
David Greene170c48a2010-01-05 01:30:02 +0000878 dbgs() << "Pass Arguments: ";
Yaron Keren85c12152016-04-28 14:49:44 +0000879 for (ImmutablePass *P : ImmutablePasses)
880 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000881 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohman67a84f12010-11-11 16:32:17 +0000882 if (!PI->isAnalysisGroup())
883 dbgs() << " -" << PI->getPassArgument();
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000884 }
Yaron Keren85c12152016-04-28 14:49:44 +0000885 for (PMDataManager *PM : PassManagers)
886 PM->dumpPassArguments();
David Greene170c48a2010-01-05 01:30:02 +0000887 dbgs() << "\n";
Devang Patelc32cf542006-12-13 22:10:00 +0000888}
889
Devang Patel1336a6b2006-12-21 00:16:50 +0000890void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren85c12152016-04-28 14:49:44 +0000891 for (PMDataManager *PM : PassManagers)
892 PM->initializeAnalysisInfo();
Dan Gohman95df6192010-08-12 23:50:08 +0000893
Devang Patel1336a6b2006-12-21 00:16:50 +0000894 // Initailize other pass managers
Yaron Keren85c12152016-04-28 14:49:44 +0000895 for (PMDataManager *IPM : IndirectPassManagers)
896 IPM->initializeAnalysisInfo();
Devang Patel721e59c2008-08-12 00:26:16 +0000897
Yaron Kerenc36f8002016-10-02 19:21:41 +0000898 for (auto LU : LastUser) {
899 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
900 L.insert(LU.first);
Devang Patel721e59c2008-08-12 00:26:16 +0000901 }
Devang Patel1336a6b2006-12-21 00:16:50 +0000902}
903
Devang Patelab7752c2007-01-12 18:52:44 +0000904/// Destructor
905PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren85c12152016-04-28 14:49:44 +0000906 for (PMDataManager *PM : PassManagers)
907 delete PM;
Dan Gohman95df6192010-08-12 23:50:08 +0000908
Yaron Keren85c12152016-04-28 14:49:44 +0000909 for (ImmutablePass *P : ImmutablePasses)
910 delete P;
Devang Patelab7752c2007-01-12 18:52:44 +0000911}
912
Devang Patel1b8d0152006-12-12 22:35:25 +0000913//===----------------------------------------------------------------------===//
Devang Patel419f0e92006-12-07 18:36:24 +0000914// PMDataManager implementation
Devang Patel889739c2006-11-07 22:35:17 +0000915
Devang Patelb8526162006-11-11 01:10:19 +0000916/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patelf32b4dd2006-12-07 19:33:53 +0000917void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000918 AnalysisID PI = P->getPassID();
Dan Gohman95df6192010-08-12 23:50:08 +0000919
Chris Lattnerf9574362009-03-06 05:53:14 +0000920 AvailableAnalysis[PI] = P;
Dan Gohman95df6192010-08-12 23:50:08 +0000921
Dan Gohman9e2f6282010-08-12 23:46:28 +0000922 assert(!AvailableAnalysis.empty());
Devang Patelb8526162006-11-11 01:10:19 +0000923
Dan Gohman95df6192010-08-12 23:50:08 +0000924 // This pass is the current implementation of all of the interfaces it
925 // implements as well.
Chandler Carruth69774fa2015-01-28 09:47:21 +0000926 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperec0f0bc2014-04-09 06:08:46 +0000927 if (!PInf) return;
Owen Anderson90c579d2010-08-06 18:33:48 +0000928 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +0000929 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Anderson90c579d2010-08-06 18:33:48 +0000930 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patelb8526162006-11-11 01:10:19 +0000931}
932
Devang Patel7b65dd92007-03-06 17:52:53 +0000933// Return true if P preserves high level analysis used by other
934// passes managed by this manager
935bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000936 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel3b8a9062008-08-11 21:13:39 +0000937 if (AnUsage->getPreservesAll())
Devang Patel7b65dd92007-03-06 17:52:53 +0000938 return true;
Dan Gohman95df6192010-08-12 23:50:08 +0000939
Devang Patel3b8a9062008-08-11 21:13:39 +0000940 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenc36f8002016-10-02 19:21:41 +0000941 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000942 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer975248e2016-08-11 22:21:41 +0000943 !is_contained(PreservedSet, P1->getPassID()))
Devang Pateld46825c2007-03-08 19:05:01 +0000944 return false;
Devang Patel7b65dd92007-03-06 17:52:53 +0000945 }
Dan Gohman95df6192010-08-12 23:50:08 +0000946
Devang Patel7b65dd92007-03-06 17:52:53 +0000947 return true;
948}
949
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000950/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patel58e0ef12007-07-19 18:02:32 +0000951void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000952 // Don't do this unless assertions are enabled.
953#ifdef NDEBUG
954 return;
955#endif
Devang Patel3b8a9062008-08-11 21:13:39 +0000956 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
957 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel889739c2006-11-07 22:35:17 +0000958
Devang Patel9750b5d2007-07-19 05:36:09 +0000959 // Verify preserved analysis
Yaron Kerenc36f8002016-10-02 19:21:41 +0000960 for (AnalysisID AID : PreservedSet) {
Dan Gohman9450b0e2009-09-28 00:27:48 +0000961 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattnera782e752010-03-30 04:03:22 +0000962 TimeRegion PassTimer(getPassTimer(AP));
Devang Patel58e0ef12007-07-19 18:02:32 +0000963 AP->verifyAnalysis();
Dan Gohman9450b0e2009-09-28 00:27:48 +0000964 }
Devang Patel5b57e722008-07-01 17:44:24 +0000965 }
966}
967
Devang Patel844a3d12008-07-01 19:50:56 +0000968/// Remove Analysis not preserved by Pass P
Devang Patel58e0ef12007-07-19 18:02:32 +0000969void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000970 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
971 if (AnUsage->getPreservesAll())
Devang Patel04b4e052006-12-07 20:03:49 +0000972 return;
973
Devang Patel3b8a9062008-08-11 21:13:39 +0000974 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemance522ee2013-02-26 01:31:59 +0000975 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel54e247d2006-12-12 23:07:44 +0000976 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemance522ee2013-02-26 01:31:59 +0000977 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperec0f0bc2014-04-09 06:08:46 +0000978 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer975248e2016-08-11 22:21:41 +0000979 !is_contained(PreservedSet, Info->first)) {
Devang Patel14d65812006-11-11 01:24:55 +0000980 // Remove this analysis
Andrew Trickabe68f52013-09-19 06:02:43 +0000981 if (PassDebugging >= Details) {
Devang Patele62f7502008-06-03 01:02:16 +0000982 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +0000983 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
984 dbgs() << S->getPassName() << "'\n";
Devang Patele62f7502008-06-03 01:02:16 +0000985 }
Dan Gohmane1877262008-11-06 21:57:17 +0000986 AvailableAnalysis.erase(Info);
Devang Patele62f7502008-06-03 01:02:16 +0000987 }
Devang Patel14d65812006-11-11 01:24:55 +0000988 }
Dan Gohman95df6192010-08-12 23:50:08 +0000989
Devang Patelfe613902007-03-06 01:55:46 +0000990 // Check inherited analysis also. If P is not preserving analysis
991 // provided by parent manager then remove it here.
992 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
993
994 if (!InheritedAnalysis[Index])
995 continue;
996
Michael Ilsemance522ee2013-02-26 01:31:59 +0000997 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patelfe613902007-03-06 01:55:46 +0000998 I = InheritedAnalysis[Index]->begin(),
999 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemance522ee2013-02-26 01:31:59 +00001000 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperec0f0bc2014-04-09 06:08:46 +00001001 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer975248e2016-08-11 22:21:41 +00001002 !is_contained(PreservedSet, Info->first)) {
Devang Patelfe613902007-03-06 01:55:46 +00001003 // Remove this analysis
Andrew Trickabe68f52013-09-19 06:02:43 +00001004 if (PassDebugging >= Details) {
Andreas Neustifter1f6ae812009-12-04 06:58:24 +00001005 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +00001006 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
1007 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter1f6ae812009-12-04 06:58:24 +00001008 }
Devang Pateld46825c2007-03-08 19:05:01 +00001009 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter1f6ae812009-12-04 06:58:24 +00001010 }
Devang Patelfe613902007-03-06 01:55:46 +00001011 }
1012 }
Devang Patel889739c2006-11-07 22:35:17 +00001013}
1014
Devang Pateldf1a10e2006-11-14 03:05:08 +00001015/// Remove analysis passes that are not used any longer
Daniel Dunbar2928c832009-11-06 10:58:06 +00001016void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel7f997612007-03-05 20:01:30 +00001017 enum PassDebuggingString DBG_STR) {
Devang Patelf9a60ae2006-12-08 00:37:52 +00001018
Devang Pateledbef382007-07-20 18:04:54 +00001019 SmallVector<Pass *, 12> DeadPasses;
Devang Patel0ed8df32007-04-16 20:27:05 +00001020
Devang Patel693941b2007-04-16 20:39:59 +00001021 // If this is a on the fly manager then it does not have TPM.
Devang Patel0ed8df32007-04-16 20:27:05 +00001022 if (!TPM)
1023 return;
1024
Devang Patelf9a60ae2006-12-08 00:37:52 +00001025 TPM->collectLastUses(DeadPasses, P);
1026
Andrew Trickabe68f52013-09-19 06:02:43 +00001027 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene170c48a2010-01-05 01:30:02 +00001028 dbgs() << " -*- '" << P->getPassName();
1029 dbgs() << "' is the last user of following pass instances.";
1030 dbgs() << " Free these instances\n";
Evan Cheng7c9b6522008-06-04 09:13:31 +00001031 }
1032
Yaron Kerenc36f8002016-10-02 19:21:41 +00001033 for (Pass *P : DeadPasses)
1034 freePass(P, Msg, DBG_STR);
Dan Gohman27a8fb82009-09-27 23:38:27 +00001035}
Devang Patel4eeea772006-12-13 23:50:44 +00001036
Daniel Dunbar2928c832009-11-06 10:58:06 +00001037void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman27a8fb82009-09-27 23:38:27 +00001038 enum PassDebuggingString DBG_STR) {
1039 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel4eeea772006-12-13 23:50:44 +00001040
Dan Gohman27a8fb82009-09-27 23:38:27 +00001041 {
1042 // If the pass crashes releasing memory, remember this.
1043 PassManagerPrettyStackEntry X(P);
Chris Lattnera782e752010-03-30 04:03:22 +00001044 TimeRegion PassTimer(getPassTimer(P));
1045
Dan Gohman27a8fb82009-09-27 23:38:27 +00001046 P->releaseMemory();
Dan Gohman27a8fb82009-09-27 23:38:27 +00001047 }
1048
Owen Anderson90c579d2010-08-06 18:33:48 +00001049 AnalysisID PI = P->getPassID();
Chandler Carruth69774fa2015-01-28 09:47:21 +00001050 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman27a8fb82009-09-27 23:38:27 +00001051 // Remove the pass itself (if it is not already removed).
1052 AvailableAnalysis.erase(PI);
1053
1054 // Remove all interfaces this pass implements, for which it is also
1055 // listed as the available implementation.
Owen Anderson90c579d2010-08-06 18:33:48 +00001056 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +00001057 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemance522ee2013-02-26 01:31:59 +00001058 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Anderson90c579d2010-08-06 18:33:48 +00001059 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman27a8fb82009-09-27 23:38:27 +00001060 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patel617fddf2008-10-06 20:36:36 +00001061 AvailableAnalysis.erase(Pos);
Devang Patel617fddf2008-10-06 20:36:36 +00001062 }
Devang Patelf9a60ae2006-12-08 00:37:52 +00001063 }
Devang Pateldf1a10e2006-11-14 03:05:08 +00001064}
1065
Dan Gohman95df6192010-08-12 23:50:08 +00001066/// Add pass P into the PassVector. Update
Devang Patel893a5a62006-11-11 02:04:19 +00001067/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerf9574362009-03-06 05:53:14 +00001068void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Patel145e83d2006-12-08 23:53:00 +00001069 // This manager is going to manage pass P. Set up analysis resolver
1070 // to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +00001071 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel145e83d2006-12-08 23:53:00 +00001072 P->setResolver(AR);
1073
Devang Patelcf5fb2b2007-03-05 22:57:49 +00001074 // If a FunctionPass F is the last user of ModulePass info M
1075 // then the F's manager, not F, records itself as a last user of M.
Devang Pateledbef382007-07-20 18:04:54 +00001076 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelcf5fb2b2007-03-05 22:57:49 +00001077
Chris Lattnerf9574362009-03-06 05:53:14 +00001078 if (!ProcessAnalysis) {
1079 // Add pass
1080 PassVector.push_back(P);
1081 return;
Devang Patel893a5a62006-11-11 02:04:19 +00001082 }
Devang Patele2533852006-11-11 01:51:02 +00001083
Chris Lattnerf9574362009-03-06 05:53:14 +00001084 // At the moment, this pass is the last user of all required passes.
1085 SmallVector<Pass *, 12> LastUses;
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001086 SmallVector<Pass *, 8> UsedPasses;
Chris Lattnerf9574362009-03-06 05:53:14 +00001087 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1088
1089 unsigned PDepth = this->getDepth();
1090
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001091 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1092 for (Pass *PUsed : UsedPasses) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001093 unsigned RDepth = 0;
1094
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001095 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1096 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattnerf9574362009-03-06 05:53:14 +00001097 RDepth = DM.getDepth();
1098
1099 if (PDepth == RDepth)
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001100 LastUses.push_back(PUsed);
Chris Lattnerf9574362009-03-06 05:53:14 +00001101 else if (PDepth > RDepth) {
1102 // Let the parent claim responsibility of last use
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001103 TransferLastUses.push_back(PUsed);
Chris Lattnerf9574362009-03-06 05:53:14 +00001104 // Keep track of higher level analysis used by this manager.
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001105 HigherLevelAnalysis.push_back(PUsed);
Dan Gohman95df6192010-08-12 23:50:08 +00001106 } else
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001107 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattnerf9574362009-03-06 05:53:14 +00001108 }
1109
1110 // Set P as P's last user until someone starts using P.
1111 // However, if P is a Pass Manager then it does not need
1112 // to record its last user.
Craig Topperec0f0bc2014-04-09 06:08:46 +00001113 if (!P->getAsPMDataManager())
Chris Lattnerf9574362009-03-06 05:53:14 +00001114 LastUses.push_back(P);
1115 TPM->setLastUser(LastUses, P);
1116
1117 if (!TransferLastUses.empty()) {
Chris Lattner3660eca2010-01-22 05:24:46 +00001118 Pass *My_PM = getAsPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001119 TPM->setLastUser(TransferLastUses, My_PM);
1120 TransferLastUses.clear();
1121 }
1122
Dan Gohman3620ff92010-08-16 22:57:28 +00001123 // Now, take care of required analyses that are not available.
Chandler Carruthebb4ccb2015-08-18 18:41:53 +00001124 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1125 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Anderson90c579d2010-08-06 18:33:48 +00001126 Pass *AnalysisPass = PI->createPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001127 this->addLowerLevelRequiredPass(P, AnalysisPass);
1128 }
1129
1130 // Take a note of analysis required and made available by this pass.
1131 // Remove the analysis not preserved by this pass
1132 removeNotPreservedAnalysis(P);
1133 recordAvailableAnalysis(P);
1134
Devang Patele2533852006-11-11 01:51:02 +00001135 // Add pass
1136 PassVector.push_back(P);
Devang Patele2533852006-11-11 01:51:02 +00001137}
1138
Devang Patel569a6fd2007-04-16 20:12:57 +00001139
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001140/// Populate UP with analysis pass that are used or required by
Devang Patel569a6fd2007-04-16 20:12:57 +00001141/// pass P and are available. Populate RP_NotAvail with analysis
1142/// pass that are required by pass P but are not available.
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001143void PMDataManager::collectRequiredAndUsedAnalyses(
1144 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1145 Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001146 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001147
1148 for (const auto &UsedID : AnUsage->getUsedSet())
1149 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1150 UP.push_back(AnalysisPass);
1151
Chandler Carruthebb4ccb2015-08-18 18:41:53 +00001152 for (const auto &RequiredID : AnUsage->getRequiredSet())
1153 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001154 UP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001155 else
Chandler Carruthebb4ccb2015-08-18 18:41:53 +00001156 RP_NotAvail.push_back(RequiredID);
Devang Patel27aaab22006-12-12 23:09:32 +00001157
Chandler Carruthebb4ccb2015-08-18 18:41:53 +00001158 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1159 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001160 UP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001161 else
Chandler Carruthebb4ccb2015-08-18 18:41:53 +00001162 RP_NotAvail.push_back(RequiredID);
Devang Patelc17bbb62006-12-07 23:05:44 +00001163}
1164
Devang Patel2f42ed62006-11-14 21:49:36 +00001165// All Required analyses should be available to the pass as it runs! Here
1166// we fill in the AnalysisImpls member of the pass so that it can
1167// successfully use the getAnalysis() method to retrieve the
1168// implementations it needs.
1169//
Devang Patel419f0e92006-12-07 18:36:24 +00001170void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001171 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1172
Florian Hahn91acfe52017-07-13 10:52:00 +00001173 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1174 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperec0f0bc2014-04-09 06:08:46 +00001175 if (!Impl)
Devang Patelf4bd76a2007-04-16 20:44:16 +00001176 // This may be analysis pass that is initialized on the fly.
1177 // If that is not the case then it will raise an assert when it is used.
1178 continue;
Devang Patelcde53d32007-01-05 22:47:07 +00001179 AnalysisResolver *AR = P->getResolver();
Chris Lattnerf9574362009-03-06 05:53:14 +00001180 assert(AR && "Analysis Resolver is not set");
Florian Hahn91acfe52017-07-13 10:52:00 +00001181 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel2f42ed62006-11-14 21:49:36 +00001182 }
1183}
1184
Devang Patel69867b52006-12-08 22:30:11 +00001185/// Find the pass that implements Analysis AID. If desired pass is not found
1186/// then return NULL.
1187Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1188
1189 // Check if AvailableAnalysis map has one entry.
Michael Ilsemance522ee2013-02-26 01:31:59 +00001190 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel69867b52006-12-08 22:30:11 +00001191
1192 if (I != AvailableAnalysis.end())
1193 return I->second;
1194
1195 // Search Parents through TopLevelManager
1196 if (SearchParent)
1197 return TPM->findAnalysisPass(AID);
Dan Gohman95df6192010-08-12 23:50:08 +00001198
Craig Topperec0f0bc2014-04-09 06:08:46 +00001199 return nullptr;
Devang Patel69867b52006-12-08 22:30:11 +00001200}
1201
Devang Patela52035a2006-12-15 20:13:01 +00001202// Print list of passes that are last used by P.
1203void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1204
Devang Pateledbef382007-07-20 18:04:54 +00001205 SmallVector<Pass *, 12> LUses;
Devang Patel693941b2007-04-16 20:39:59 +00001206
1207 // If this is a on the fly manager then it does not have TPM.
1208 if (!TPM)
1209 return;
1210
Devang Patela52035a2006-12-15 20:13:01 +00001211 TPM->collectLastUses(LUses, P);
Dan Gohman95df6192010-08-12 23:50:08 +00001212
Florian Hahn91acfe52017-07-13 10:52:00 +00001213 for (Pass *P : LUses) {
Eric Christopher0115a4e2014-02-26 23:27:16 +00001214 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn91acfe52017-07-13 10:52:00 +00001215 P->dumpPassStructure(0);
Devang Patela52035a2006-12-15 20:13:01 +00001216 }
1217}
1218
1219void PMDataManager::dumpPassArguments() const {
Florian Hahn91acfe52017-07-13 10:52:00 +00001220 for (Pass *P : PassVector) {
1221 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patela52035a2006-12-15 20:13:01 +00001222 PMD->dumpPassArguments();
1223 else
Owen Anderson90c579d2010-08-06 18:33:48 +00001224 if (const PassInfo *PI =
Florian Hahn91acfe52017-07-13 10:52:00 +00001225 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patela52035a2006-12-15 20:13:01 +00001226 if (!PI->isAnalysisGroup())
David Greene170c48a2010-01-05 01:30:02 +00001227 dbgs() << " -" << PI->getPassArgument();
Devang Patela52035a2006-12-15 20:13:01 +00001228 }
1229}
1230
Chris Lattner417efc82007-08-10 06:17:04 +00001231void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1232 enum PassDebuggingString S2,
Daniel Dunbar2928c832009-11-06 10:58:06 +00001233 StringRef Msg) {
Andrew Trickabe68f52013-09-19 06:02:43 +00001234 if (PassDebugging < Executions)
Devang Patela52035a2006-12-15 20:13:01 +00001235 return;
Pavel Labath515a7232016-10-25 16:20:07 +00001236 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth7f194642014-04-27 23:59:25 +00001237 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel7f997612007-03-05 20:01:30 +00001238 switch (S1) {
1239 case EXECUTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001240 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001241 break;
1242 case MODIFICATION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001243 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001244 break;
1245 case FREEING_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001246 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001247 break;
1248 default:
1249 break;
1250 }
1251 switch (S2) {
1252 case ON_BASICBLOCK_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001253 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001254 break;
1255 case ON_FUNCTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001256 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001257 break;
1258 case ON_MODULE_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001259 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001260 break;
Tobias Grosser65513602010-10-20 01:54:44 +00001261 case ON_REGION_MSG:
1262 dbgs() << "' on Region '" << Msg << "'...\n";
1263 break;
Devang Patel7f997612007-03-05 20:01:30 +00001264 case ON_LOOP_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001265 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001266 break;
1267 case ON_CG_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001268 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001269 break;
1270 default:
1271 break;
1272 }
Devang Patela52035a2006-12-15 20:13:01 +00001273}
1274
Chris Lattnerd6f16582009-03-06 06:45:05 +00001275void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickabe68f52013-09-19 06:02:43 +00001276 if (PassDebugging < Details)
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001277 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001278
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001279 AnalysisUsage analysisUsage;
1280 P->getAnalysisUsage(analysisUsage);
1281 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1282}
1283
Chris Lattnerd6f16582009-03-06 06:45:05 +00001284void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickabe68f52013-09-19 06:02:43 +00001285 if (PassDebugging < Details)
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001286 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001287
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001288 AnalysisUsage analysisUsage;
1289 P->getAnalysisUsage(analysisUsage);
1290 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1291}
1292
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001293void PMDataManager::dumpUsedSet(const Pass *P) const {
1294 if (PassDebugging < Details)
1295 return;
1296
1297 AnalysisUsage analysisUsage;
1298 P->getAnalysisUsage(analysisUsage);
1299 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1300}
1301
Daniel Dunbar2928c832009-11-06 10:58:06 +00001302void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattnerd6f16582009-03-06 06:45:05 +00001303 const AnalysisUsage::VectorType &Set) const {
Andrew Trickabe68f52013-09-19 06:02:43 +00001304 assert(PassDebugging >= Details);
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001305 if (Set.empty())
1306 return;
Roman Divacky59324292012-09-05 22:26:57 +00001307 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerd6f16582009-03-06 06:45:05 +00001308 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene170c48a2010-01-05 01:30:02 +00001309 if (i) dbgs() << ',';
Chandler Carruth69774fa2015-01-28 09:47:21 +00001310 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trickc5d93bb2011-06-03 00:48:58 +00001311 if (!PInf) {
1312 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1313 // all drivers.
1314 dbgs() << " Uninitialized Pass";
1315 continue;
1316 }
Owen Anderson90c579d2010-08-06 18:33:48 +00001317 dbgs() << ' ' << PInf->getPassName();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001318 }
David Greene170c48a2010-01-05 01:30:02 +00001319 dbgs() << '\n';
Devang Patela52035a2006-12-15 20:13:01 +00001320}
Devang Patelf3dc6d92006-12-08 23:28:54 +00001321
Devang Patel19fe8f92007-07-27 20:06:09 +00001322/// Add RequiredPass into list of lower level passes required by pass P.
1323/// RequiredPass is run on the fly by Pass Manager when P requests it
1324/// through getAnalysis interface.
1325/// This should be handled by specific pass manager.
1326void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1327 if (TPM) {
1328 TPM->dumpArguments();
1329 TPM->dumpPasses();
1330 }
Devang Patel1cf47cb2008-02-02 01:43:30 +00001331
Dan Gohman95df6192010-08-12 23:50:08 +00001332 // Module Level pass may required Function Level analysis info
1333 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1334 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel1cf47cb2008-02-02 01:43:30 +00001335 // module level pass is requiring lower level analysis info managed by
1336 // lower level pass manager.
1337
1338 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohman95df6192010-08-12 23:50:08 +00001339 // checks whether any lower level manager will be able to provide this
Devang Patel1cf47cb2008-02-02 01:43:30 +00001340 // analysis info on demand or not.
Devang Patelc0c33f52008-06-03 01:20:02 +00001341#ifndef NDEBUG
David Greene170c48a2010-01-05 01:30:02 +00001342 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1343 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelc0c33f52008-06-03 01:20:02 +00001344#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001345 llvm_unreachable("Unable to schedule pass");
Devang Patel19fe8f92007-07-27 20:06:09 +00001346}
1347
Owen Anderson90c579d2010-08-06 18:33:48 +00001348Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topper50bee422012-02-05 22:14:15 +00001349 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmane407c1d2010-06-21 18:46:45 +00001350}
1351
Devang Patelab7752c2007-01-12 18:52:44 +00001352// Destructor
1353PMDataManager::~PMDataManager() {
Florian Hahn91acfe52017-07-13 10:52:00 +00001354 for (Pass *P : PassVector)
1355 delete P;
Devang Patelab7752c2007-01-12 18:52:44 +00001356}
1357
Devang Patelf3dc6d92006-12-08 23:28:54 +00001358//===----------------------------------------------------------------------===//
1359// NOTE: Is this the right place to define this method ?
Duncan Sands1465d612009-01-28 13:14:17 +00001360// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1361Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patelf3dc6d92006-12-08 23:28:54 +00001362 return PM.findAnalysisPass(ID, dir);
1363}
1364
Dan Gohman95df6192010-08-12 23:50:08 +00001365Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel6b1df0e2007-04-16 20:56:24 +00001366 Function &F) {
1367 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1368}
1369
Devang Patel06e86562006-12-07 19:39:39 +00001370//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001371// BBPassManager implementation
Devang Patel55fd43f2006-11-07 21:31:57 +00001372
Dan Gohman95df6192010-08-12 23:50:08 +00001373/// Execute all of the passes scheduled for execution by invoking
1374/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel55fd43f2006-11-07 21:31:57 +00001375/// the function, and if so, return true.
Chris Lattnerd6f16582009-03-06 06:45:05 +00001376bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001377 if (F.isDeclaration())
Devang Patel1fbe2c92006-12-12 23:15:28 +00001378 return false;
1379
Devang Patel3b14fbe2006-12-08 01:38:28 +00001380 bool Changed = doInitialization(F);
Jessica Paquette849da552018-05-18 17:26:39 +00001381 Module &M = *F.getParent();
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001382
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001383 unsigned InstrCount, BBSize = 0;
Jessica Paquette9e212802018-09-06 21:19:54 +00001384 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tonga747d612018-07-22 05:27:41 +00001385 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001386 if (EmitICRemark)
Jessica Paquette9e212802018-09-06 21:19:54 +00001387 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001388
1389 for (BasicBlock &BB : F) {
1390 // Collect the initial size of the basic block.
1391 if (EmitICRemark)
1392 BBSize = BB.size();
Devang Patel1554c852006-12-16 00:56:26 +00001393 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1394 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001395 bool LocalChanged = false;
Devang Patel017b5d92006-12-14 00:25:06 +00001396
Florian Hahn91acfe52017-07-13 10:52:00 +00001397 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001398 dumpRequiredSet(BP);
Devang Patel017b5d92006-12-14 00:25:06 +00001399
Devang Patel1554c852006-12-16 00:56:26 +00001400 initializeAnalysisImpl(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001401
Chris Lattnerd6f16582009-03-06 06:45:05 +00001402 {
1403 // If the pass crashes, remember this.
Florian Hahn91acfe52017-07-13 10:52:00 +00001404 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattnera782e752010-03-30 04:03:22 +00001405 TimeRegion PassTimer(getPassTimer(BP));
Florian Hahn91acfe52017-07-13 10:52:00 +00001406 LocalChanged |= BP->runOnBasicBlock(BB);
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001407 if (EmitICRemark) {
1408 unsigned NewSize = BB.size();
1409 // Update the size of the basic block, emit a remark, and update the
1410 // size of the module.
1411 if (NewSize != BBSize) {
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001412 int64_t Delta =
1413 static_cast<int64_t>(NewSize) - static_cast<int64_t>(BBSize);
Jessica Paquette9e212802018-09-06 21:19:54 +00001414 emitInstrCountChangedRemark(BP, M, Delta, InstrCount,
1415 FunctionToInstrCount, &F);
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001416 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1417 BBSize = NewSize;
1418 }
1419 }
Chris Lattnerd6f16582009-03-06 06:45:05 +00001420 }
Devang Patel693a74e2006-12-14 00:08:04 +00001421
Dan Gohman16b77212010-03-01 17:34:28 +00001422 Changed |= LocalChanged;
Dan Gohman95df6192010-08-12 23:50:08 +00001423 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001424 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn91acfe52017-07-13 10:52:00 +00001425 BB.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001426 dumpPreservedSet(BP);
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001427 dumpUsedSet(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001428
Devang Patel58e0ef12007-07-19 18:02:32 +00001429 verifyPreservedAnalysis(BP);
Devang Patel1554c852006-12-16 00:56:26 +00001430 removeNotPreservedAnalysis(BP);
1431 recordAvailableAnalysis(BP);
Florian Hahn91acfe52017-07-13 10:52:00 +00001432 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel55fd43f2006-11-07 21:31:57 +00001433 }
Jessica Paquettec1a096d2018-08-31 20:20:53 +00001434 }
Chris Lattnerfc23bc72007-08-10 06:22:25 +00001435
Bill Wendling47eb1ea2009-12-25 13:50:18 +00001436 return doFinalization(F) || Changed;
Devang Patel55fd43f2006-11-07 21:31:57 +00001437}
1438
Devang Patel964e45e2006-12-08 00:59:05 +00001439// Implement doInitialization and doFinalization
Duncan Sandse70a6832009-02-13 09:42:34 +00001440bool BBPassManager::doInitialization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001441 bool Changed = false;
1442
Chris Lattnerd6f16582009-03-06 06:45:05 +00001443 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1444 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001445
1446 return Changed;
1447}
1448
Duncan Sandse70a6832009-02-13 09:42:34 +00001449bool BBPassManager::doFinalization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001450 bool Changed = false;
1451
Pedro Artigasd1abec32012-12-05 17:12:22 +00001452 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001453 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001454
1455 return Changed;
1456}
1457
Duncan Sandse70a6832009-02-13 09:42:34 +00001458bool BBPassManager::doInitialization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001459 bool Changed = false;
1460
Devang Patel1554c852006-12-16 00:56:26 +00001461 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1462 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001463 Changed |= BP->doInitialization(F);
1464 }
1465
1466 return Changed;
1467}
1468
Duncan Sandse70a6832009-02-13 09:42:34 +00001469bool BBPassManager::doFinalization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001470 bool Changed = false;
1471
Devang Patel1554c852006-12-16 00:56:26 +00001472 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1473 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001474 Changed |= BP->doFinalization(F);
1475 }
1476
1477 return Changed;
1478}
1479
1480
Devang Patel06e86562006-12-07 19:39:39 +00001481//===----------------------------------------------------------------------===//
Devang Patel31626912006-12-13 02:36:01 +00001482// FunctionPassManager implementation
Devang Patel06e86562006-12-07 19:39:39 +00001483
Devang Patelc63592b2006-11-08 10:44:40 +00001484/// Create new Function pass manager
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001485FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001486 FPM = new FunctionPassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001487 // FPM is the top level manager.
1488 FPM->setTopLevelManager(FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001489
Dan Gohman59ef0152008-03-13 02:08:36 +00001490 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001491 FPM->setResolver(AR);
Devang Patelcc132cd2006-12-08 18:57:16 +00001492}
1493
Devang Patel31626912006-12-13 02:36:01 +00001494FunctionPassManager::~FunctionPassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001495 delete FPM;
1496}
1497
Dan Gohman95df6192010-08-12 23:50:08 +00001498void FunctionPassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001499 FPM->add(P);
Devang Patelc63592b2006-11-08 10:44:40 +00001500}
1501
Devang Patel214ca232006-11-15 19:39:54 +00001502/// run - Execute all of the passes scheduled for execution. Keep
1503/// track of whether any of the passes modifies the function, and if
1504/// so, return true.
1505///
Devang Patel31626912006-12-13 02:36:01 +00001506bool FunctionPassManager::run(Function &F) {
Peter Collingbourne76c218e2016-11-09 17:49:19 +00001507 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1508 report_fatal_error("Error reading bitcode file: " + EIB.message());
1509 });
Devang Patelc4756922006-12-08 22:57:48 +00001510 return FPM->run(F);
Devang Patel214ca232006-11-15 19:39:54 +00001511}
1512
1513
Devang Patel3799f972006-11-15 01:27:05 +00001514/// doInitialization - Run all of the initializers for the function passes.
1515///
Devang Patel31626912006-12-13 02:36:01 +00001516bool FunctionPassManager::doInitialization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001517 return FPM->doInitialization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001518}
1519
Dan Gohman209ee182007-07-30 14:51:13 +00001520/// doFinalization - Run all of the finalizers for the function passes.
Devang Patel3799f972006-11-15 01:27:05 +00001521///
Devang Patel31626912006-12-13 02:36:01 +00001522bool FunctionPassManager::doFinalization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001523 return FPM->doFinalization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001524}
1525
Devang Patel06e86562006-12-07 19:39:39 +00001526//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001527// FunctionPassManagerImpl implementation
1528//
Duncan Sandse70a6832009-02-13 09:42:34 +00001529bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001530 bool Changed = false;
1531
Dan Gohman9f9ca732009-11-23 16:24:18 +00001532 dumpArguments();
1533 dumpPasses();
1534
Yaron Keren485adca2015-06-05 17:48:47 +00001535 for (ImmutablePass *ImPass : getImmutablePasses())
1536 Changed |= ImPass->doInitialization(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001537
Chris Lattnerd6f16582009-03-06 06:45:05 +00001538 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1539 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001540
1541 return Changed;
1542}
1543
Duncan Sandse70a6832009-02-13 09:42:34 +00001544bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001545 bool Changed = false;
1546
Pedro Artigasd1abec32012-12-05 17:12:22 +00001547 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001548 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001549
Yaron Keren485adca2015-06-05 17:48:47 +00001550 for (ImmutablePass *ImPass : getImmutablePasses())
1551 Changed |= ImPass->doFinalization(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001552
Devang Patel5f4ddf52006-12-19 19:46:59 +00001553 return Changed;
1554}
1555
Devang Patel9dfa1672009-04-01 22:34:41 +00001556/// cleanup - After running all passes, clean up pass manager cache.
1557void FPPassManager::cleanup() {
1558 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1559 FunctionPass *FP = getContainedPass(Index);
1560 AnalysisResolver *AR = FP->getResolver();
1561 assert(AR && "Analysis Resolver is not set");
1562 AR->clearAnalysisImpls();
1563 }
1564}
1565
Torok Edwin1970a892009-06-29 18:49:09 +00001566void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1567 if (!wasRun)
1568 return;
1569 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1570 FPPassManager *FPPM = getContainedManager(Index);
1571 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1572 FPPM->getContainedPass(Index)->releaseMemory();
1573 }
1574 }
Torok Edwin6c839922009-06-29 21:05:10 +00001575 wasRun = false;
Torok Edwin1970a892009-06-29 18:49:09 +00001576}
1577
Devang Patel5f4ddf52006-12-19 19:46:59 +00001578// Execute all the passes managed by this top level manager.
1579// Return true if any function is modified by a pass.
1580bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001581 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001582
Devang Patel1336a6b2006-12-21 00:16:50 +00001583 initializeAllAnalysisInfo();
Juergen Ributzka9bc1b732014-05-16 02:33:15 +00001584 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavlov69a1a202017-01-15 10:23:18 +00001585 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka9bc1b732014-05-16 02:33:15 +00001586 F.getContext().yield();
1587 }
Devang Patel9dfa1672009-04-01 22:34:41 +00001588
1589 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1590 getContainedManager(Index)->cleanup();
1591
Torok Edwin1970a892009-06-29 18:49:09 +00001592 wasRun = true;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001593 return Changed;
1594}
1595
1596//===----------------------------------------------------------------------===//
1597// FPPassManager implementation
Devang Patel448d27c2006-11-07 21:49:50 +00001598
Devang Patel19974732007-05-03 01:11:54 +00001599char FPPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +00001600/// Print passes managed by this manager
1601void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramer962bad72011-10-16 16:30:34 +00001602 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patelab7752c2007-01-12 18:52:44 +00001603 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1604 FunctionPass *FP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +00001605 FP->dumpPassStructure(Offset + 1);
Devang Patelab7752c2007-01-12 18:52:44 +00001606 dumpLastUses(FP, Offset+1);
1607 }
1608}
1609
1610
Dan Gohman95df6192010-08-12 23:50:08 +00001611/// Execute all of the passes scheduled for execution by invoking
1612/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel448d27c2006-11-07 21:49:50 +00001613/// the function, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001614bool FPPassManager::runOnFunction(Function &F) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001615 if (F.isDeclaration())
1616 return false;
Devang Patel214ca232006-11-15 19:39:54 +00001617
1618 bool Changed = false;
Jessica Paquette849da552018-05-18 17:26:39 +00001619 Module &M = *F.getParent();
Devang Patelbed7e682008-03-20 01:09:53 +00001620 // Collect inherited analysis from Module level pass manager.
1621 populateInheritedAnalysis(TPM->activeStack);
Devang Patel1fbe2c92006-12-12 23:15:28 +00001622
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001623 unsigned InstrCount, FunctionSize = 0;
Jessica Paquette9e212802018-09-06 21:19:54 +00001624 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tonga747d612018-07-22 05:27:41 +00001625 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001626 // Collect the initial size of the module.
1627 if (EmitICRemark) {
Jessica Paquette9e212802018-09-06 21:19:54 +00001628 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001629 FunctionSize = F.getInstructionCount();
1630 }
1631
Devang Patel1554c852006-12-16 00:56:26 +00001632 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1633 FunctionPass *FP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001634 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001635
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001636 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001637 dumpRequiredSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001638
Devang Patel1554c852006-12-16 00:56:26 +00001639 initializeAnalysisImpl(FP);
Eric Christopher9e7e6092012-03-23 03:54:05 +00001640
Chris Lattnerd6f16582009-03-06 06:45:05 +00001641 {
1642 PassManagerPrettyStackEntry X(FP, F);
Chris Lattnera782e752010-03-30 04:03:22 +00001643 TimeRegion PassTimer(getPassTimer(FP));
Dan Gohman16b77212010-03-01 17:34:28 +00001644 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001645 if (EmitICRemark) {
1646 unsigned NewSize = F.getInstructionCount();
1647
1648 // Update the size of the function, emit a remark, and update the size
1649 // of the module.
1650 if (NewSize != FunctionSize) {
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001651 int64_t Delta = static_cast<int64_t>(NewSize) -
1652 static_cast<int64_t>(FunctionSize);
Jessica Paquette9e212802018-09-06 21:19:54 +00001653 emitInstrCountChangedRemark(FP, M, Delta, InstrCount,
1654 FunctionToInstrCount, &F);
Jessica Paquettee76b95d2018-08-31 20:19:41 +00001655 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1656 FunctionSize = NewSize;
1657 }
1658 }
Chris Lattnerd6f16582009-03-06 06:45:05 +00001659 }
Devang Patel693a74e2006-12-14 00:08:04 +00001660
Dan Gohman16b77212010-03-01 17:34:28 +00001661 Changed |= LocalChanged;
1662 if (LocalChanged)
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001663 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001664 dumpPreservedSet(FP);
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001665 dumpUsedSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001666
Devang Patel58e0ef12007-07-19 18:02:32 +00001667 verifyPreservedAnalysis(FP);
Devang Patel1554c852006-12-16 00:56:26 +00001668 removeNotPreservedAnalysis(FP);
1669 recordAvailableAnalysis(FP);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001670 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel214ca232006-11-15 19:39:54 +00001671 }
1672 return Changed;
1673}
1674
Devang Patel5f4ddf52006-12-19 19:46:59 +00001675bool FPPassManager::runOnModule(Module &M) {
Pedro Artigas6eda0812012-11-29 17:47:05 +00001676 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001677
Serge Pavlov69a1a202017-01-15 10:23:18 +00001678 for (Function &F : M)
Yaron Kerened3a7f52015-06-05 14:15:07 +00001679 Changed |= runOnFunction(F);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001680
Pedro Artigas6eda0812012-11-29 17:47:05 +00001681 return Changed;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001682}
1683
Duncan Sandse70a6832009-02-13 09:42:34 +00001684bool FPPassManager::doInitialization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001685 bool Changed = false;
1686
Chris Lattnerd6f16582009-03-06 06:45:05 +00001687 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1688 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trick2cccc622013-09-18 23:31:10 +00001689
Devang Patel3799f972006-11-15 01:27:05 +00001690 return Changed;
1691}
1692
Duncan Sandse70a6832009-02-13 09:42:34 +00001693bool FPPassManager::doFinalization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001694 bool Changed = false;
Pedro Artigasd1abec32012-12-05 17:12:22 +00001695
1696 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001697 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trick2cccc622013-09-18 23:31:10 +00001698
Devang Patel3799f972006-11-15 01:27:05 +00001699 return Changed;
1700}
1701
Devang Patel06e86562006-12-07 19:39:39 +00001702//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001703// MPPassManager implementation
Devang Patel92c45ee2006-11-07 22:03:15 +00001704
Dan Gohman95df6192010-08-12 23:50:08 +00001705/// Execute all of the passes scheduled for execution by invoking
1706/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel92c45ee2006-11-07 22:03:15 +00001707/// the module, and if so, return true.
1708bool
Devang Patel5f4ddf52006-12-19 19:46:59 +00001709MPPassManager::runOnModule(Module &M) {
Devang Patel92c45ee2006-11-07 22:03:15 +00001710 bool Changed = false;
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001711
Torok Edwin1970a892009-06-29 18:49:09 +00001712 // Initialize on-the-fly passes
Yaron Kerened3a7f52015-06-05 14:15:07 +00001713 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1714 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin1970a892009-06-29 18:49:09 +00001715 Changed |= FPP->doInitialization(M);
1716 }
1717
Pedro Artigas6eda0812012-11-29 17:47:05 +00001718 // Initialize module passes
1719 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1720 Changed |= getContainedPass(Index)->doInitialization(M);
1721
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001722 unsigned InstrCount, ModuleCount = 0;
Jessica Paquette9e212802018-09-06 21:19:54 +00001723 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tonga747d612018-07-22 05:27:41 +00001724 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001725 // Collect the initial size of the module.
1726 if (EmitICRemark) {
Jessica Paquette9e212802018-09-06 21:19:54 +00001727 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001728 ModuleCount = InstrCount;
1729 }
1730
Devang Patel1554c852006-12-16 00:56:26 +00001731 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1732 ModulePass *MP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001733 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001734
Benjamin Kramer69cee612009-12-08 13:07:38 +00001735 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001736 dumpRequiredSet(MP);
Devang Patel693a74e2006-12-14 00:08:04 +00001737
Devang Patel1554c852006-12-16 00:56:26 +00001738 initializeAnalysisImpl(MP);
Devang Patel8e58a1b2006-12-14 00:59:42 +00001739
Chris Lattnerd6f16582009-03-06 06:45:05 +00001740 {
1741 PassManagerPrettyStackEntry X(MP, M);
Chris Lattnera782e752010-03-30 04:03:22 +00001742 TimeRegion PassTimer(getPassTimer(MP));
1743
Dan Gohman16b77212010-03-01 17:34:28 +00001744 LocalChanged |= MP->runOnModule(M);
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001745 if (EmitICRemark) {
1746 // Update the size of the module.
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001747 ModuleCount = M.getInstructionCount();
1748 if (ModuleCount != InstrCount) {
Jessica Paquette4f3a2192018-08-31 20:20:57 +00001749 int64_t Delta = static_cast<int64_t>(ModuleCount) -
1750 static_cast<int64_t>(InstrCount);
Jessica Paquette9e212802018-09-06 21:19:54 +00001751 emitInstrCountChangedRemark(MP, M, Delta, InstrCount,
1752 FunctionToInstrCount);
Jessica Paquette29f24c92018-08-31 22:43:41 +00001753 InstrCount = ModuleCount;
Jessica Paquetteca1a3572018-08-31 20:20:55 +00001754 }
1755 }
Chris Lattnerd6f16582009-03-06 06:45:05 +00001756 }
Devang Patel693a74e2006-12-14 00:08:04 +00001757
Dan Gohman16b77212010-03-01 17:34:28 +00001758 Changed |= LocalChanged;
1759 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001760 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramer69cee612009-12-08 13:07:38 +00001761 M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001762 dumpPreservedSet(MP);
Chandler Carruth8545f7f2015-08-19 03:02:12 +00001763 dumpUsedSet(MP);
Dan Gohman95df6192010-08-12 23:50:08 +00001764
Devang Patel58e0ef12007-07-19 18:02:32 +00001765 verifyPreservedAnalysis(MP);
Devang Patel1554c852006-12-16 00:56:26 +00001766 removeNotPreservedAnalysis(MP);
1767 recordAvailableAnalysis(MP);
Benjamin Kramer69cee612009-12-08 13:07:38 +00001768 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel92c45ee2006-11-07 22:03:15 +00001769 }
Torok Edwin1970a892009-06-29 18:49:09 +00001770
Pedro Artigas6eda0812012-11-29 17:47:05 +00001771 // Finalize module passes
Pedro Artigasd1abec32012-12-05 17:12:22 +00001772 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigas6eda0812012-11-29 17:47:05 +00001773 Changed |= getContainedPass(Index)->doFinalization(M);
1774
Torok Edwin1970a892009-06-29 18:49:09 +00001775 // Finalize on-the-fly passes
Yaron Kerened3a7f52015-06-05 14:15:07 +00001776 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1777 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin1970a892009-06-29 18:49:09 +00001778 // We don't know when is the last time an on-the-fly pass is run,
1779 // so we need to releaseMemory / finalize here
1780 FPP->releaseMemoryOnTheFly();
1781 Changed |= FPP->doFinalization(M);
1782 }
Andrew Trick2cccc622013-09-18 23:31:10 +00001783
Devang Patel92c45ee2006-11-07 22:03:15 +00001784 return Changed;
1785}
1786
Devang Patel569a6fd2007-04-16 20:12:57 +00001787/// Add RequiredPass into list of lower level passes required by pass P.
1788/// RequiredPass is run on the fly by Pass Manager when P requests it
1789/// through getAnalysis interface.
1790void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001791 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1792 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001793 assert((P->getPotentialPassManagerType() <
Chris Lattnerf9574362009-03-06 05:53:14 +00001794 RequiredPass->getPotentialPassManagerType()) &&
1795 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick3c1f2eb2014-04-08 03:40:34 +00001796 if (!RequiredPass)
1797 return;
Devang Patel569a6fd2007-04-16 20:12:57 +00001798
Devang Pateldfa1ec32007-04-26 17:50:19 +00001799 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel0ed8df32007-04-16 20:27:05 +00001800 if (!FPP) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001801 FPP = new FunctionPassManagerImpl();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001802 // FPP is the top level manager.
1803 FPP->setTopLevelManager(FPP);
1804
Devang Patel0ed8df32007-04-16 20:27:05 +00001805 OnTheFlyManagers[P] = FPP;
1806 }
Chandler Carruth69774fa2015-01-28 09:47:21 +00001807 const PassInfo *RequiredPassPI =
1808 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel0ed8df32007-04-16 20:27:05 +00001809
Craig Topperec0f0bc2014-04-09 06:08:46 +00001810 Pass *FoundPass = nullptr;
Andrew Trick3c1f2eb2014-04-08 03:40:34 +00001811 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1812 FoundPass =
1813 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patelc67d1842011-09-13 21:13:29 +00001814 }
Andrew Trick3c1f2eb2014-04-08 03:40:34 +00001815 if (!FoundPass) {
1816 FoundPass = RequiredPass;
1817 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledrua7f09412014-08-11 18:04:46 +00001818 // that we checked for an available analysis above.
Andrew Trick3c1f2eb2014-04-08 03:40:34 +00001819 FPP->add(RequiredPass);
1820 }
1821 // Register P as the last user of FoundPass or RequiredPass.
1822 SmallVector<Pass *, 1> LU;
1823 LU.push_back(FoundPass);
1824 FPP->setLastUser(LU, P);
Devang Patel569a6fd2007-04-16 20:12:57 +00001825}
Devang Patel0ed8df32007-04-16 20:27:05 +00001826
Dan Gohman95df6192010-08-12 23:50:08 +00001827/// Return function pass corresponding to PassInfo PI, that is
Devang Patel0ed8df32007-04-16 20:27:05 +00001828/// required by module pass MP. Instantiate analysis pass, by using
1829/// its runOnFunction() for function F.
Owen Anderson90c579d2010-08-06 18:33:48 +00001830Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Pateldfa1ec32007-04-26 17:50:19 +00001831 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattnerf9574362009-03-06 05:53:14 +00001832 assert(FPP && "Unable to find on the fly pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001833
Torok Edwin1970a892009-06-29 18:49:09 +00001834 FPP->releaseMemoryOnTheFly();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001835 FPP->run(F);
Chris Lattner77c95ed2010-01-22 05:37:10 +00001836 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel0ed8df32007-04-16 20:27:05 +00001837}
1838
1839
Devang Patel06e86562006-12-07 19:39:39 +00001840//===----------------------------------------------------------------------===//
1841// PassManagerImpl implementation
Owen Anderson40b6fdb2012-11-15 00:14:15 +00001842
Devang Patel37a6f792006-12-13 00:09:23 +00001843//
Devang Patelb30803b2006-11-07 22:23:34 +00001844/// run - Execute all of the passes scheduled for execution. Keep track of
1845/// whether any of the passes modifies the module, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001846bool PassManagerImpl::run(Module &M) {
Devang Patelb30803b2006-11-07 22:23:34 +00001847 bool Changed = false;
Devang Patel8e58a1b2006-12-14 00:59:42 +00001848
Devang Patelc32cf542006-12-13 22:10:00 +00001849 dumpArguments();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001850 dumpPasses();
Devang Patel45dc02d2006-12-13 20:03:48 +00001851
Yaron Kerened3a7f52015-06-05 14:15:07 +00001852 for (ImmutablePass *ImPass : getImmutablePasses())
1853 Changed |= ImPass->doInitialization(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001854
Devang Patel1336a6b2006-12-21 00:16:50 +00001855 initializeAllAnalysisInfo();
Juergen Ributzka9bc1b732014-05-16 02:33:15 +00001856 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavlov69a1a202017-01-15 10:23:18 +00001857 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka9bc1b732014-05-16 02:33:15 +00001858 M.getContext().yield();
1859 }
Pedro Artigasd1abec32012-12-05 17:12:22 +00001860
Yaron Kerened3a7f52015-06-05 14:15:07 +00001861 for (ImmutablePass *ImPass : getImmutablePasses())
1862 Changed |= ImPass->doFinalization(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001863
Devang Patelb30803b2006-11-07 22:23:34 +00001864 return Changed;
1865}
Devang Patel5a39b2e2006-11-08 10:29:57 +00001866
Devang Patel06e86562006-12-07 19:39:39 +00001867//===----------------------------------------------------------------------===//
1868// PassManager implementation
1869
Devang Patel5a39b2e2006-11-08 10:29:57 +00001870/// Create new pass manager
Devang Patel31626912006-12-13 02:36:01 +00001871PassManager::PassManager() {
Andrew Trick0e122d12011-08-29 17:07:00 +00001872 PM = new PassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001873 // PM is the top level manager
1874 PM->setTopLevelManager(PM);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001875}
1876
Devang Patel31626912006-12-13 02:36:01 +00001877PassManager::~PassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001878 delete PM;
1879}
1880
Chris Lattnerf9574362009-03-06 05:53:14 +00001881void PassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001882 PM->add(P);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001883}
1884
1885/// run - Execute all of the passes scheduled for execution. Keep track of
1886/// whether any of the passes modifies the module, and if so, return true.
Chris Lattnerf9574362009-03-06 05:53:14 +00001887bool PassManager::run(Module &M) {
Devang Patel5a39b2e2006-11-08 10:29:57 +00001888 return PM->run(M);
1889}
1890
Devang Patel8e58a1b2006-12-14 00:59:42 +00001891//===----------------------------------------------------------------------===//
Devang Patel09e6e432007-01-08 19:29:38 +00001892// PMStack implementation
1893//
Devang Patel36bcb822007-01-11 22:15:30 +00001894
Devang Patel09e6e432007-01-08 19:29:38 +00001895// Pop Pass Manager from the stack and clear its analysis info.
1896void PMStack::pop() {
1897
1898 PMDataManager *Top = this->top();
1899 Top->initializeAnalysisInfo();
1900
1901 S.pop_back();
1902}
1903
1904// Push PM on the stack and set its top level manager.
Dan Gohmanc2f12ab2008-03-13 01:21:31 +00001905void PMStack::push(PMDataManager *PM) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001906 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick0e122d12011-08-29 17:07:00 +00001907 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel09e6e432007-01-08 19:29:38 +00001908
Chris Lattnerf9574362009-03-06 05:53:14 +00001909 if (!this->empty()) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001910 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1911 && "pushing bad pass manager to PMStack");
Chris Lattnerf9574362009-03-06 05:53:14 +00001912 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel09e6e432007-01-08 19:29:38 +00001913
Chris Lattnerf9574362009-03-06 05:53:14 +00001914 assert(TPM && "Unable to find top level manager");
Devang Patel97149732007-01-11 00:19:00 +00001915 TPM->addIndirectPassManager(PM);
1916 PM->setTopLevelManager(TPM);
Andrew Trick0e122d12011-08-29 17:07:00 +00001917 PM->setDepth(this->top()->getDepth()+1);
Craig Topper97fe3d92013-02-06 06:50:38 +00001918 } else {
Benjamin Kramer4a3d0a52011-08-29 18:14:15 +00001919 assert((PM->getPassManagerType() == PMT_ModulePassManager
1920 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick0e122d12011-08-29 17:07:00 +00001921 && "pushing bad pass manager to PMStack");
1922 PM->setDepth(1);
Devang Patel97149732007-01-11 00:19:00 +00001923 }
1924
Devang Patel97149732007-01-11 00:19:00 +00001925 S.push_back(PM);
1926}
1927
1928// Dump content of the pass manager stack.
Yaron Keren55307982016-01-29 20:50:44 +00001929LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren485adca2015-06-05 17:48:47 +00001930 for (PMDataManager *Manager : S)
1931 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattnerf9574362009-03-06 05:53:14 +00001932
Devang Patel97149732007-01-11 00:19:00 +00001933 if (!S.empty())
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +00001934 dbgs() << '\n';
Devang Patel09e6e432007-01-08 19:29:38 +00001935}
1936
Devang Patel09e6e432007-01-08 19:29:38 +00001937/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohman95df6192010-08-12 23:50:08 +00001938/// add self into that manager.
1939void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001940 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001941 // Find Module Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001942 while (!PMS.empty()) {
Devang Patel44b0d292007-01-17 21:19:23 +00001943 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1944 if (TopPMType == PreferredType)
1945 break; // We found desired pass manager
1946 else if (TopPMType > PMT_ModulePassManager)
Devang Patel09e6e432007-01-08 19:29:38 +00001947 PMS.pop(); // Pop children pass managers
Devang Patel6b9420e2007-01-11 19:59:06 +00001948 else
1949 break;
Devang Patel09e6e432007-01-08 19:29:38 +00001950 }
Devang Patelbd6dc7a2008-09-09 21:38:40 +00001951 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel44b0d292007-01-17 21:19:23 +00001952 PMS.top()->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001953}
1954
Devang Patel9d133e12007-01-16 21:43:18 +00001955/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001956/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001957void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001958 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001959
Andrew Trick11e43292012-02-01 07:16:20 +00001960 // Find Function Pass Manager
Chris Lattner77c95ed2010-01-22 05:37:10 +00001961 while (!PMS.empty()) {
Devang Patel6b9420e2007-01-11 19:59:06 +00001962 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1963 PMS.pop();
Devang Patel09e6e432007-01-08 19:29:38 +00001964 else
Dan Gohman95df6192010-08-12 23:50:08 +00001965 break;
Devang Patel9d133e12007-01-16 21:43:18 +00001966 }
Devang Patel9d133e12007-01-16 21:43:18 +00001967
Chris Lattner77c95ed2010-01-22 05:37:10 +00001968 // Create new Function Pass Manager if needed.
1969 FPPassManager *FPP;
1970 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1971 FPP = (FPPassManager *)PMS.top();
1972 } else {
Devang Patel9d133e12007-01-16 21:43:18 +00001973 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1974 PMDataManager *PMD = PMS.top();
1975
1976 // [1] Create new Function Pass Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00001977 FPP = new FPPassManager();
Devang Patelbed7e682008-03-20 01:09:53 +00001978 FPP->populateInheritedAnalysis(PMS);
Devang Patel9d133e12007-01-16 21:43:18 +00001979
1980 // [2] Set up new manager's top level manager
1981 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1982 TPM->addIndirectPassManager(FPP);
1983
1984 // [3] Assign manager to manage this new manager. This may create
1985 // and push new managers into PMS
Devang Patel0938f742008-09-09 17:56:50 +00001986 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel9d133e12007-01-16 21:43:18 +00001987
1988 // [4] Push new manager into PMS
1989 PMS.push(FPP);
Devang Patel09e6e432007-01-08 19:29:38 +00001990 }
1991
Devang Patel9d133e12007-01-16 21:43:18 +00001992 // Assign FPP as the manager of this pass.
1993 FPP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001994}
1995
Devang Patel9d133e12007-01-16 21:43:18 +00001996/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001997/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001998void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001999 PassManagerType PreferredType) {
Chris Lattner77c95ed2010-01-22 05:37:10 +00002000 BBPassManager *BBP;
Devang Patel09e6e432007-01-08 19:29:38 +00002001
Devang Patel97149732007-01-11 00:19:00 +00002002 // Basic Pass Manager is a leaf pass manager. It does not handle
2003 // any other pass manager.
Dan Gohman95df6192010-08-12 23:50:08 +00002004 if (!PMS.empty() &&
Chris Lattner77c95ed2010-01-22 05:37:10 +00002005 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
2006 BBP = (BBPassManager *)PMS.top();
2007 } else {
2008 // If leaf manager is not Basic Block Pass manager then create new
2009 // basic Block Pass manager.
Devang Patel9d133e12007-01-16 21:43:18 +00002010 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
2011 PMDataManager *PMD = PMS.top();
2012
2013 // [1] Create new Basic Block Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00002014 BBP = new BBPassManager();
Devang Patel9d133e12007-01-16 21:43:18 +00002015
2016 // [2] Set up new manager's top level manager
2017 // Basic Block Pass Manager does not live by itself
2018 PMTopLevelManager *TPM = PMD->getTopLevelManager();
2019 TPM->addIndirectPassManager(BBP);
2020
Devang Patel97149732007-01-11 00:19:00 +00002021 // [3] Assign manager to manage this new manager. This may create
2022 // and push new managers into PMS
David Greenee89f1c42010-05-10 20:24:27 +00002023 BBP->assignPassManager(PMS, PreferredType);
Devang Patel97149732007-01-11 00:19:00 +00002024
Devang Patel9d133e12007-01-16 21:43:18 +00002025 // [4] Push new manager into PMS
2026 PMS.push(BBP);
2027 }
Devang Patel09e6e432007-01-08 19:29:38 +00002028
Devang Patel9d133e12007-01-16 21:43:18 +00002029 // Assign BBP as the manager of this pass.
2030 BBP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00002031}
2032
Dan Gohman580b8992008-03-11 16:41:42 +00002033PassManagerBase::~PassManagerBase() {}