blob: 8ce8d7df4d55f709735c7703d11de9f87b81d64d [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel6e5a1132006-11-07 21:31:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruth7caea412013-11-09 12:26:54 +000010// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth7caea412013-11-09 12:26:54 +000014#include "llvm/IR/LegacyPassManager.h"
Florian Hahna4ffa3a2018-05-24 21:33:17 +000015#include "llvm/ADT/MapVector.h"
James Henderson852f6fd2017-05-16 09:43:21 +000016#include "llvm/ADT/Statistic.h"
Jessica Paquettee49374d2018-05-18 17:26:39 +000017#include "llvm/IR/DiagnosticInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000018#include "llvm/IR/IRPrintingPasses.h"
19#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000020#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000021#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/IR/Module.h"
Fedor Sergeev43083112018-08-28 21:06:51 +000023#include "llvm/IR/PassTimingInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000024#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000025#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000026#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000027#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000028#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000030#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Timer.h"
32#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000033#include <algorithm>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000034#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000035using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000036using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000037
Devang Patele7599552007-01-12 18:52:44 +000038// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000039
Devang Patelf1567a52006-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 Carruth7caea412013-11-09 12:26:54 +000047namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000048// Different debug levels that can be enabled...
49enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000050 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000051};
Chandler Carruth7caea412013-11-09 12:26:54 +000052}
Devang Patel03fb5872006-12-13 21:13:31 +000053
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000054static cl::opt<enum PassDebugLevel>
55PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000056 cl::desc("Print PassManager debugging information"),
57 cl::values(
Andrew Trickb5e1e6c2013-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 Amini732afdd2016-10-08 19:41:06 +000062 clEnumVal(Details , "print pass details when it is executed")));
David Greene9b063df2010-04-02 23:17:14 +000063
Chandler Carruth7caea412013-11-09 12:26:54 +000064namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000065typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
66PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000067}
Andrew Trickb5e1e6c2013-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 Turner8065f0b2017-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 Trickb5e1e6c2013-09-19 06:02:43 +000086
Fedor Sergeev94dca7c2017-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 Topperf5730c32018-04-01 21:54:26 +000091 cl::init(false), cl::Hidden);
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000092
Weiming Zhao0f1762c2016-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 Turner8065f0b2017-12-01 00:53:10 +000098 cl::CommaSeparated, cl::Hidden);
Weiming Zhao0f1762c2016-01-06 22:55:03 +000099
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000100/// This is a helper to determine whether to print IR before or
101/// after a pass.
102
103static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
104 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +0000105 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000106 if (PassInf)
107 if (PassInf->getPassArgument() == PI->getPassArgument()) {
108 return true;
109 }
David Greene9b063df2010-04-02 23:17:14 +0000110 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000111 return false;
112}
Dan Gohmande6188a2010-08-12 23:50:08 +0000113
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000114/// This is a utility to check whether a pass should have IR dumped
115/// before it.
116static bool ShouldPrintBeforePass(const PassInfo *PI) {
117 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
118}
David Greene9b063df2010-04-02 23:17:14 +0000119
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000120/// This is a utility to check whether a pass should have IR dumped
121/// after it.
122static bool ShouldPrintAfterPass(const PassInfo *PI) {
123 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000124}
125
Fedor Sergeev94dca7c2017-12-01 17:42:46 +0000126bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
127
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000128bool llvm::isFunctionInPrintList(StringRef FunctionName) {
129 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
130 PrintFuncsList.end());
131 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
132}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000133/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
134/// or higher is specified.
135bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000136 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000137}
138
Jessica Paquettee49374d2018-05-18 17:26:39 +0000139unsigned PMDataManager::initSizeRemarkInfo(Module &M) {
140 // Only calculate getInstructionCount if the size-info remark is requested.
Xin Tong023e25a2018-07-22 05:27:41 +0000141 return M.getInstructionCount();
Jessica Paquettee49374d2018-05-18 17:26:39 +0000142}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000143
Jessica Paquettee49374d2018-05-18 17:26:39 +0000144void PMDataManager::emitInstrCountChangedRemark(Pass *P, Module &M,
145 unsigned CountBefore) {
Jessica Paquettee49374d2018-05-18 17:26:39 +0000146 // We need a function containing at least one basic block in order to output
147 // remarks. Since it's possible that the first function in the module doesn't
148 // actually contain a basic block, we have to go and find one that's suitable
149 // for emitting remarks.
150 auto It = std::find_if(M.begin(), M.end(),
151 [](const Function &Fn) { return !Fn.empty(); });
152
153 // Didn't find a function. Quit.
154 if (It == M.end())
155 return;
156
157 // We found a function containing at least one basic block.
158 Function *F = &*It;
159
160 // How many instructions are in the module now?
161 unsigned CountAfter = M.getInstructionCount();
162
163 // If there was no change, don't emit a remark.
164 if (CountBefore == CountAfter)
165 return;
166
167 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
168 // that the only passes that return non-null with getAsPMDataManager are pass
169 // managers.) The reason we have to do this is to avoid emitting remarks for
170 // CGSCC passes.
171 if (P->getAsPMDataManager())
172 return;
173
174 // Compute a possibly negative delta between the instruction count before
175 // running P, and after running P.
Jessica Paquettec6048172018-05-18 20:04:21 +0000176 int64_t Delta =
177 static_cast<int64_t>(CountAfter) - static_cast<int64_t>(CountBefore);
Jessica Paquettee49374d2018-05-18 17:26:39 +0000178
179 BasicBlock &BB = *F->begin();
180 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
181 DiagnosticLocation(), &BB);
182 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
183 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
184 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
185 << ": IR instruction count changed from "
186 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
187 << " to "
188 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
189 << "; Delta: "
190 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
191 F->getContext().diagnose(R); // Not using ORE for layering reasons.
192}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000193
Chris Lattner4c1e9542009-03-06 06:45:05 +0000194void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000195 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000196 OS << "Releasing pass '";
197 else
198 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000199
Chris Lattner4c1e9542009-03-06 06:45:05 +0000200 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000201
Chris Lattner4c1e9542009-03-06 06:45:05 +0000202 if (M) {
203 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
204 return;
205 }
Craig Topperc6207612014-04-09 06:08:46 +0000206 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000207 OS << '\n';
208 return;
209 }
210
Dan Gohman79fc0e92009-03-10 18:47:59 +0000211 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000212 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000213 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000214 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000215 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000216 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000217 OS << "value";
218
219 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000220 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000221 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000222}
223
224
Devang Patelffca9102006-12-15 19:39:30 +0000225namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000226//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000228//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000229/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000230/// pass together and sequence them to process one basic block before
231/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000232class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000233
234public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000235 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000236 explicit BBPassManager()
237 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000238
Devang Patelca58e352006-11-08 10:05:38 +0000239 /// Execute all of the passes scheduled for execution. Keep track of
240 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000241 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000242
Devang Patelf9d96b92006-12-07 19:57:52 +0000243 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000244 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000245 Info.setPreservesAll();
246 }
247
Craig Topperf398d7c2014-03-05 06:35:38 +0000248 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000249 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000250 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000251 bool doFinalization(Function &F);
252
Craig Topperf398d7c2014-03-05 06:35:38 +0000253 PMDataManager *getAsPMDataManager() override { return this; }
254 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000255
Mehdi Amini117296c2016-10-01 02:56:57 +0000256 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000257
Devang Pateleda56172006-12-12 23:34:33 +0000258 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000259 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000260 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000261 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
262 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000263 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000264 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000265 }
266 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000267
268 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000269 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000270 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
271 return BP;
272 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000273
Craig Topperf398d7c2014-03-05 06:35:38 +0000274 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000275 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000276 }
Devang Patelca58e352006-11-08 10:05:38 +0000277};
278
Devang Patel8c78a0b2007-05-03 01:11:54 +0000279char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000280} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281
Devang Patele7599552007-01-12 18:52:44 +0000282namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000283namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000284//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000285// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000286//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000287/// FunctionPassManagerImpl manages FPPassManagers
288class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000289 public PMDataManager,
290 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000291 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000292private:
293 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000294public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000295 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000296 explicit FunctionPassManagerImpl() :
297 Pass(PT_PassManager, ID), PMDataManager(),
298 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299
Matthias Braun0880c602014-12-12 01:27:01 +0000300 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000301 void add(Pass *P) {
302 schedulePass(P);
303 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000304
305 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000306 Pass *createPrinterPass(raw_ostream &O,
307 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000308 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000309 }
310
Torok Edwin24c78352009-06-29 18:49:09 +0000311 // Prepare for running an on the fly pass, freeing memory if needed
312 // from a previous run.
313 void releaseMemoryOnTheFly();
314
Devang Patel67d6a5e2006-12-19 19:46:59 +0000315 /// run - Execute all of the passes scheduled for execution. Keep track of
316 /// whether any of the passes modifies the module, and if so, return true.
317 bool run(Function &F);
318
319 /// doInitialization - Run all of the initializers for the function passes.
320 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000321 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000322
Dan Gohmane6656eb2007-07-30 14:51:13 +0000323 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000324 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000325 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000326
Dan Gohmande6188a2010-08-12 23:50:08 +0000327
Craig Topperf398d7c2014-03-05 06:35:38 +0000328 PMDataManager *getAsPMDataManager() override { return this; }
329 Pass *getAsPass() override { return this; }
330 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000331 return PMT_FunctionPassManager;
332 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000333
Devang Patel67d6a5e2006-12-19 19:46:59 +0000334 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000335 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000336 Info.setPreservesAll();
337 }
338
Devang Patel67d6a5e2006-12-19 19:46:59 +0000339 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000340 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000341 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
342 return FP;
343 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000344};
345
David Blaikiea379b1812011-12-20 02:50:00 +0000346void FunctionPassManagerImpl::anchor() {}
347
Devang Patel8c78a0b2007-05-03 01:11:54 +0000348char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000349} // End of legacy namespace
350} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000351
Chandler Carruth7caea412013-11-09 12:26:54 +0000352namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000353//===----------------------------------------------------------------------===//
354// MPPassManager
355//
356/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000357/// It batches all Module passes and function pass managers together and
358/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000359class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000360public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000361 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000362 explicit MPPassManager() :
363 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000364
365 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000366 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000367 for (auto &OnTheFlyManager : OnTheFlyManagers) {
368 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000369 delete FPP;
370 }
371 }
372
Dan Gohmande6188a2010-08-12 23:50:08 +0000373 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000374 Pass *createPrinterPass(raw_ostream &O,
375 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000376 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000377 }
378
Devang Patelca58e352006-11-08 10:05:38 +0000379 /// run - Execute all of the passes scheduled for execution. Keep track of
380 /// whether any of the passes modifies the module, and if so, return true.
381 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000382
Pedro Artigase4348b02012-12-03 21:56:57 +0000383 using llvm::Pass::doInitialization;
384 using llvm::Pass::doFinalization;
385
Devang Patelf9d96b92006-12-07 19:57:52 +0000386 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000387 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000388 Info.setPreservesAll();
389 }
390
Devang Patele64d3052007-04-16 20:12:57 +0000391 /// Add RequiredPass into list of lower level passes required by pass P.
392 /// RequiredPass is run on the fly by Pass Manager when P requests it
393 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000394 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000395
Dan Gohmande6188a2010-08-12 23:50:08 +0000396 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000397 /// required by module pass MP. Instantiate analysis pass, by using
398 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000399 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000400
Mehdi Amini117296c2016-10-01 02:56:57 +0000401 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000402
Craig Topperf398d7c2014-03-05 06:35:38 +0000403 PMDataManager *getAsPMDataManager() override { return this; }
404 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000405
Devang Pateleda56172006-12-12 23:34:33 +0000406 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000407 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000408 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000409 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
410 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000411 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000412 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
413 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000414 if (I != OnTheFlyManagers.end())
415 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000416 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000417 }
418 }
419
Devang Patelabfbe3b2006-12-16 00:56:26 +0000420 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000421 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000422 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000423 }
424
Craig Topperf398d7c2014-03-05 06:35:38 +0000425 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000426 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000427 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000428
429 private:
430 /// Collection of on the fly FPPassManagers. These managers manage
431 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000432 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000433};
434
Devang Patel8c78a0b2007-05-03 01:11:54 +0000435char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000436} // End anonymous namespace
437
438namespace llvm {
439namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000440//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000441// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000442//
Devang Patel09f162c2007-05-01 21:15:47 +0000443
Devang Patel67d6a5e2006-12-19 19:46:59 +0000444/// PassManagerImpl manages MPPassManagers
445class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000446 public PMDataManager,
447 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000448 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000449
450public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000451 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000452 explicit PassManagerImpl() :
453 Pass(PT_PassManager, ID), PMDataManager(),
454 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000455
Matthias Braun0880c602014-12-12 01:27:01 +0000456 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000457 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000458 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000459 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000460
461 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000462 Pass *createPrinterPass(raw_ostream &O,
463 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000464 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000465 }
466
Devang Patel376fefa2006-11-08 10:29:57 +0000467 /// run - Execute all of the passes scheduled for execution. Keep track of
468 /// whether any of the passes modifies the module, and if so, return true.
469 bool run(Module &M);
470
Pedro Artigase4348b02012-12-03 21:56:57 +0000471 using llvm::Pass::doInitialization;
472 using llvm::Pass::doFinalization;
473
Devang Patelf9d96b92006-12-07 19:57:52 +0000474 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000475 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000476 Info.setPreservesAll();
477 }
478
Craig Topperf398d7c2014-03-05 06:35:38 +0000479 PMDataManager *getAsPMDataManager() override { return this; }
480 Pass *getAsPass() override { return this; }
481 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000482 return PMT_ModulePassManager;
483 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000484
Devang Patel67d6a5e2006-12-19 19:46:59 +0000485 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000486 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000487 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
488 return MP;
489 }
Devang Patel376fefa2006-11-08 10:29:57 +0000490};
491
David Blaikiea379b1812011-12-20 02:50:00 +0000492void PassManagerImpl::anchor() {}
493
Devang Patel8c78a0b2007-05-03 01:11:54 +0000494char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000495} // End of legacy namespace
496} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000497
Devang Patela1514cb2006-12-07 19:39:39 +0000498//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000499// PMTopLevelManager implementation
500
Devang Patel4268fc02007-01-16 02:00:38 +0000501/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000502PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
503 PMDM->setTopLevelManager(this);
504 addPassManager(PMDM);
505 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000506}
507
Devang Patelafb1f3622006-12-12 22:35:25 +0000508/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000509void
Bill Wendlingea857e12012-05-14 07:53:40 +0000510PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000511 unsigned PDepth = 0;
512 if (P->getResolver())
513 PDepth = P->getResolver()->getPMDataManager().getDepth();
514
Yaron Keren4849aa32015-06-05 17:48:47 +0000515 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000516 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000517
Devang Patel01919d22007-03-08 19:05:01 +0000518 if (P == AP)
519 continue;
520
Tobias Grosserf07426b2011-01-20 21:03:22 +0000521 // Update the last users of passes that are required transitive by AP.
522 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
523 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
524 SmallVector<Pass *, 12> LastUses;
525 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000526 for (AnalysisID ID : IDs) {
527 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000528 assert(AnalysisPass && "Expected analysis pass to exist.");
529 AnalysisResolver *AR = AnalysisPass->getResolver();
530 assert(AR && "Expected analysis resolver to exist.");
531 unsigned APDepth = AR->getPMDataManager().getDepth();
532
533 if (PDepth == APDepth)
534 LastUses.push_back(AnalysisPass);
535 else if (PDepth > APDepth)
536 LastPMUses.push_back(AnalysisPass);
537 }
538
539 setLastUser(LastUses, P);
540
541 // If this pass has a corresponding pass manager, push higher level
542 // analysis to this pass manager.
543 if (P->getResolver())
544 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
545
546
Devang Patelafb1f3622006-12-12 22:35:25 +0000547 // If AP is the last user of other passes then make P last user of
548 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000549 for (auto LU : LastUser) {
550 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000551 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000552 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000553 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000554 }
555 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000556}
557
558/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000559void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000560 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000561 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000562 InversedLastUser.find(P);
563 if (DMI == InversedLastUser.end())
564 return;
565
566 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000567 for (Pass *LUP : LU) {
568 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000569 }
570
Devang Patelafb1f3622006-12-12 22:35:25 +0000571}
572
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000573AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000574 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000575 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000576 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000577 AnUsage = DMI->second;
578 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000579 // Look up the analysis usage from the pass instance (different instances
580 // of the same pass can produce different results), but unique the
581 // resulting object to reduce memory usage. This helps to greatly reduce
582 // memory usage when we have many instances of only a few pass types
583 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
584 // of dependencies.
585 AnalysisUsage AU;
586 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000587
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000588 AUFoldingSetNode* Node = nullptr;
589 FoldingSetNodeID ID;
590 AUFoldingSetNode::Profile(ID, AU);
591 void *IP = nullptr;
592 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
593 Node = N;
594 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000595 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000596 UniqueAnalysisUsages.InsertNode(Node, IP);
597 }
598 assert(Node && "cached analysis usage must be non null");
599
600 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000601 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000602 }
603 return AnUsage;
604}
605
Devang Patelafb1f3622006-12-12 22:35:25 +0000606/// Schedule pass P for execution. Make sure that passes required by
607/// P are run before P is run. Update analysis info maintained by
608/// the manager. Remove dead passes. This is a recursive function.
609void PMTopLevelManager::schedulePass(Pass *P) {
610
Devang Patel3312f752007-01-16 21:43:18 +0000611 // TODO : Allocate function manager for this pass, other wise required set
612 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000613
Devang Pateld74ede72007-03-06 01:06:16 +0000614 // Give pass a chance to prepare the stage.
615 P->preparePassManager(activeStack);
616
Devang Patel864970e2008-03-18 00:39:19 +0000617 // If P is an analysis pass and it is available then do not
618 // generate the analysis again. Stale analysis info should not be
619 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000620 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000621 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topper4ee28412018-08-20 20:57:30 +0000622 // Remove any cached AnalysisUsage information.
623 AnUsageMap.erase(P);
Nuno Lopes0460bb22008-11-04 23:03:58 +0000624 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000625 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000626 }
Devang Patel864970e2008-03-18 00:39:19 +0000627
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000628 AnalysisUsage *AnUsage = findAnalysisUsage(P);
629
Devang Patelfdee7032008-08-14 23:07:48 +0000630 bool checkAnalysis = true;
631 while (checkAnalysis) {
632 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000633
Devang Patelfdee7032008-08-14 23:07:48 +0000634 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000635 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000636
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000637 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000638 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000639 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000640
Craig Topperc6207612014-04-09 06:08:46 +0000641 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000642 // Pass P is not in the global PassRegistry
643 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
644 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
645 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000646 for (const AnalysisID ID2 : RequiredSet) {
647 if (ID == ID2)
648 break;
649 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000650 if (AnalysisPass2) {
651 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000652 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000653 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
654 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
655 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
656 }
657 }
658 }
659
Andrew Trick6bbaf132011-06-03 00:48:58 +0000660 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000661 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000662 if (P->getPotentialPassManagerType () ==
663 AnalysisPass->getPotentialPassManagerType())
664 // Schedule analysis pass that is managed by the same pass manager.
665 schedulePass(AnalysisPass);
666 else if (P->getPotentialPassManagerType () >
667 AnalysisPass->getPotentialPassManagerType()) {
668 // Schedule analysis pass that is managed by a new manager.
669 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000670 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000671 // are already checked are still available.
672 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000673 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000674 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000675 // passes are run on the fly.
676 delete AnalysisPass;
677 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000678 }
679 }
680
681 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000682 if (ImmutablePass *IP = P->getAsImmutablePass()) {
683 // P is a immutable pass and it will be managed by this
684 // top level manager. Set up analysis resolver to connect them.
685 PMDataManager *DM = getAsPMDataManager();
686 AnalysisResolver *AR = new AnalysisResolver(*DM);
687 P->setResolver(AR);
688 DM->initializeAnalysisImpl(P);
689 addImmutablePass(IP);
690 DM->recordAvailableAnalysis(IP);
691 return;
692 }
693
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000694 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000695 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000696 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000697 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
698 }
699
700 // Add the requested pass to the best available pass manager.
701 P->assignPassManager(activeStack, getTopLevelPassManagerType());
702
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000703 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000704 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000705 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000706 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
707 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000708}
709
710/// Find the pass that implements Analysis AID. Search immutable
711/// passes and all pass managers. If desired pass is not found
712/// then return NULL.
713Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000714 // For immutable passes we have a direct mapping from ID to pass, so check
715 // that first.
716 if (Pass *P = ImmutablePassMap.lookup(AID))
717 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000718
Devang Patelcd6ba152006-12-12 22:50:05 +0000719 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000720 for (PMDataManager *PassManager : PassManagers)
721 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000722 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000723
724 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000725 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
726 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000727 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000728
Craig Topperc6207612014-04-09 06:08:46 +0000729 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000730}
731
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000732const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
733 const PassInfo *&PI = AnalysisPassInfos[AID];
734 if (!PI)
735 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
736 else
737 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
738 "The pass info pointer changed for an analysis ID!");
739
740 return PI;
741}
742
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000743void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
744 P->initializePass();
745 ImmutablePasses.push_back(P);
746
747 // Add this pass to the map from its analysis ID. We clobber any prior runs
748 // of the pass in the map so that the last one added is the one found when
749 // doing lookups.
750 AnalysisID AID = P->getPassID();
751 ImmutablePassMap[AID] = P;
752
753 // Also add any interfaces implemented by the immutable pass to the map for
754 // fast lookup.
755 const PassInfo *PassInf = findAnalysisPassInfo(AID);
756 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000757 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000758 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
759}
760
Devang Pateleda56172006-12-12 23:34:33 +0000761// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000762void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000763
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000764 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000765 return;
766
Devang Pateleda56172006-12-12 23:34:33 +0000767 // Print out the immutable passes
768 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000769 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000770 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000771
Dan Gohmanf71c5212010-08-19 01:29:07 +0000772 // Every class that derives from PMDataManager also derives from Pass
773 // (sometimes indirectly), but there's no inheritance relationship
774 // between PMDataManager and Pass, so we have to getAsPass to get
775 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000776 for (PMDataManager *Manager : PassManagers)
777 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000778}
779
Devang Patel991aeba2006-12-15 20:13:01 +0000780void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000781
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000782 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000783 return;
784
David Greene994e1bb2010-01-05 01:30:02 +0000785 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000786 for (ImmutablePass *P : ImmutablePasses)
787 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000788 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000789 if (!PI->isAnalysisGroup())
790 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000791 }
Yaron Keren83009952016-04-28 14:49:44 +0000792 for (PMDataManager *PM : PassManagers)
793 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000794 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000795}
796
Devang Patele3068402006-12-21 00:16:50 +0000797void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000798 for (PMDataManager *PM : PassManagers)
799 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000800
Devang Patele3068402006-12-21 00:16:50 +0000801 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000802 for (PMDataManager *IPM : IndirectPassManagers)
803 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000804
Yaron Kerenbd873192016-10-02 19:21:41 +0000805 for (auto LU : LastUser) {
806 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
807 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000808 }
Devang Patele3068402006-12-21 00:16:50 +0000809}
810
Devang Patele7599552007-01-12 18:52:44 +0000811/// Destructor
812PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000813 for (PMDataManager *PM : PassManagers)
814 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000815
Yaron Keren83009952016-04-28 14:49:44 +0000816 for (ImmutablePass *P : ImmutablePasses)
817 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000818}
819
Devang Patelafb1f3622006-12-12 22:35:25 +0000820//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000821// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000822
Devang Patel643676c2006-11-11 01:10:19 +0000823/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000824void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000825 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000826
Chris Lattner60987362009-03-06 05:53:14 +0000827 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000828
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000829 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000830
Dan Gohmande6188a2010-08-12 23:50:08 +0000831 // This pass is the current implementation of all of the interfaces it
832 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000833 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000834 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000835 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000836 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000837 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000838}
839
Devang Patel9d9fc902007-03-06 17:52:53 +0000840// Return true if P preserves high level analysis used by other
841// passes managed by this manager
842bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000843 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000844 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000845 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000846
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000847 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000848 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000849 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000850 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000851 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000852 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000853
Devang Patel9d9fc902007-03-06 17:52:53 +0000854 return true;
855}
856
Chris Lattner02eb94c2008-08-07 07:34:50 +0000857/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000858void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000859 // Don't do this unless assertions are enabled.
860#ifdef NDEBUG
861 return;
862#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000863 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
864 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000865
Devang Patelef432532007-07-19 05:36:09 +0000866 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000867 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000868 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000869 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000870 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000871 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000872 }
873}
874
Devang Patel67c79a42008-07-01 19:50:56 +0000875/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000876void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000877 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
878 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000879 return;
880
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000881 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000882 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000883 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000884 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000885 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000886 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000887 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000888 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000889 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000890 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
891 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000892 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000893 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000894 }
Devang Patel349170f2006-11-11 01:24:55 +0000895 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000896
Devang Patel42dd1e92007-03-06 01:55:46 +0000897 // Check inherited analysis also. If P is not preserving analysis
898 // provided by parent manager then remove it here.
899 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
900
901 if (!InheritedAnalysis[Index])
902 continue;
903
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000904 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000905 I = InheritedAnalysis[Index]->begin(),
906 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000907 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000908 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000909 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000910 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000911 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000912 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000913 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
914 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000915 }
Devang Patel01919d22007-03-08 19:05:01 +0000916 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000917 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000918 }
919 }
Devang Patelf68a3492006-11-07 22:35:17 +0000920}
921
Devang Patelca189262006-11-14 03:05:08 +0000922/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000923void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000924 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000925
Devang Patel8adae862007-07-20 18:04:54 +0000926 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000927
Devang Patel2ff44922007-04-16 20:39:59 +0000928 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000929 if (!TPM)
930 return;
931
Devang Patel17ad0962006-12-08 00:37:52 +0000932 TPM->collectLastUses(DeadPasses, P);
933
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000934 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000935 dbgs() << " -*- '" << P->getPassName();
936 dbgs() << "' is the last user of following pass instances.";
937 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000938 }
939
Yaron Kerenbd873192016-10-02 19:21:41 +0000940 for (Pass *P : DeadPasses)
941 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000942}
Devang Patel200d3052006-12-13 23:50:44 +0000943
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000944void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000945 enum PassDebuggingString DBG_STR) {
946 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000947
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000948 {
949 // If the pass crashes releasing memory, remember this.
950 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000951 TimeRegion PassTimer(getPassTimer(P));
952
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000954 }
955
Owen Andersona7aed182010-08-06 18:33:48 +0000956 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000957 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000958 // Remove the pass itself (if it is not already removed).
959 AvailableAnalysis.erase(PI);
960
961 // Remove all interfaces this pass implements, for which it is also
962 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000963 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000964 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000965 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000966 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000967 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000968 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000969 }
Devang Patel17ad0962006-12-08 00:37:52 +0000970 }
Devang Patelca189262006-11-14 03:05:08 +0000971}
972
Dan Gohmande6188a2010-08-12 23:50:08 +0000973/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000974/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000975void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000976 // This manager is going to manage pass P. Set up analysis resolver
977 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000978 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000979 P->setResolver(AR);
980
Devang Patelec2b9a72007-03-05 22:57:49 +0000981 // If a FunctionPass F is the last user of ModulePass info M
982 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000983 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000984
Chris Lattner60987362009-03-06 05:53:14 +0000985 if (!ProcessAnalysis) {
986 // Add pass
987 PassVector.push_back(P);
988 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000989 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000990
Chris Lattner60987362009-03-06 05:53:14 +0000991 // At the moment, this pass is the last user of all required passes.
992 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000993 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000994 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
995
996 unsigned PDepth = this->getDepth();
997
Chandler Carruth44a13852015-08-19 03:02:12 +0000998 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
999 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001000 unsigned RDepth = 0;
1001
Chandler Carruth44a13852015-08-19 03:02:12 +00001002 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1003 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001004 RDepth = DM.getDepth();
1005
1006 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001007 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001008 else if (PDepth > RDepth) {
1009 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001010 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001011 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001012 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001013 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001014 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001015 }
1016
1017 // Set P as P's last user until someone starts using P.
1018 // However, if P is a Pass Manager then it does not need
1019 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001020 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001021 LastUses.push_back(P);
1022 TPM->setLastUser(LastUses, P);
1023
1024 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001025 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001026 TPM->setLastUser(TransferLastUses, My_PM);
1027 TransferLastUses.clear();
1028 }
1029
Dan Gohman6304db32010-08-16 22:57:28 +00001030 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001031 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1032 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001033 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001034 this->addLowerLevelRequiredPass(P, AnalysisPass);
1035 }
1036
1037 // Take a note of analysis required and made available by this pass.
1038 // Remove the analysis not preserved by this pass
1039 removeNotPreservedAnalysis(P);
1040 recordAvailableAnalysis(P);
1041
Devang Patel8cad70d2006-11-11 01:51:02 +00001042 // Add pass
1043 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001044}
1045
Devang Patele64d3052007-04-16 20:12:57 +00001046
Chandler Carruth44a13852015-08-19 03:02:12 +00001047/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001048/// pass P and are available. Populate RP_NotAvail with analysis
1049/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001050void PMDataManager::collectRequiredAndUsedAnalyses(
1051 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1052 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001053 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001054
1055 for (const auto &UsedID : AnUsage->getUsedSet())
1056 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1057 UP.push_back(AnalysisPass);
1058
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001059 for (const auto &RequiredID : AnUsage->getRequiredSet())
1060 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001061 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001062 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001063 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001064
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001065 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1066 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001067 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001068 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001069 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001070}
1071
Devang Patel07f4f582006-11-14 21:49:36 +00001072// All Required analyses should be available to the pass as it runs! Here
1073// we fill in the AnalysisImpls member of the pass so that it can
1074// successfully use the getAnalysis() method to retrieve the
1075// implementations it needs.
1076//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001077void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001078 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1079
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001080 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1081 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001082 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001083 // This may be analysis pass that is initialized on the fly.
1084 // If that is not the case then it will raise an assert when it is used.
1085 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001086 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001087 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001088 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001089 }
1090}
1091
Devang Patel640c5bb2006-12-08 22:30:11 +00001092/// Find the pass that implements Analysis AID. If desired pass is not found
1093/// then return NULL.
1094Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1095
1096 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001097 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001098
1099 if (I != AvailableAnalysis.end())
1100 return I->second;
1101
1102 // Search Parents through TopLevelManager
1103 if (SearchParent)
1104 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001105
Craig Topperc6207612014-04-09 06:08:46 +00001106 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001107}
1108
Devang Patel991aeba2006-12-15 20:13:01 +00001109// Print list of passes that are last used by P.
1110void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1111
Devang Patel8adae862007-07-20 18:04:54 +00001112 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001113
1114 // If this is a on the fly manager then it does not have TPM.
1115 if (!TPM)
1116 return;
1117
Devang Patel991aeba2006-12-15 20:13:01 +00001118 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001119
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001120 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001121 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001122 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001123 }
1124}
1125
1126void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001127 for (Pass *P : PassVector) {
1128 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001129 PMD->dumpPassArguments();
1130 else
Owen Andersona7aed182010-08-06 18:33:48 +00001131 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001132 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001133 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001134 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001135 }
1136}
1137
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001138void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1139 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001140 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001141 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001142 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001143 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001144 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001145 switch (S1) {
1146 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001147 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001148 break;
1149 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001151 break;
1152 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001154 break;
1155 default:
1156 break;
1157 }
1158 switch (S2) {
1159 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001160 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001161 break;
1162 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001163 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001164 break;
1165 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001166 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001167 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001168 case ON_REGION_MSG:
1169 dbgs() << "' on Region '" << Msg << "'...\n";
1170 break;
Devang Patel003a5592007-03-05 20:01:30 +00001171 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001172 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001173 break;
1174 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001175 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001176 break;
1177 default:
1178 break;
1179 }
Devang Patel991aeba2006-12-15 20:13:01 +00001180}
1181
Chris Lattner4c1e9542009-03-06 06:45:05 +00001182void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001183 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001184 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001185
Chris Lattner4c493d92008-08-08 15:14:09 +00001186 AnalysisUsage analysisUsage;
1187 P->getAnalysisUsage(analysisUsage);
1188 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1189}
1190
Chris Lattner4c1e9542009-03-06 06:45:05 +00001191void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001192 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001193 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001194
Chris Lattner4c493d92008-08-08 15:14:09 +00001195 AnalysisUsage analysisUsage;
1196 P->getAnalysisUsage(analysisUsage);
1197 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1198}
1199
Chandler Carruth44a13852015-08-19 03:02:12 +00001200void PMDataManager::dumpUsedSet(const Pass *P) const {
1201 if (PassDebugging < Details)
1202 return;
1203
1204 AnalysisUsage analysisUsage;
1205 P->getAnalysisUsage(analysisUsage);
1206 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1207}
1208
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001209void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001210 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001211 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001212 if (Set.empty())
1213 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001214 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001215 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001216 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001217 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001218 if (!PInf) {
1219 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1220 // all drivers.
1221 dbgs() << " Uninitialized Pass";
1222 continue;
1223 }
Owen Andersona7aed182010-08-06 18:33:48 +00001224 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001225 }
David Greene994e1bb2010-01-05 01:30:02 +00001226 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001227}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001228
Devang Patel004937b2007-07-27 20:06:09 +00001229/// Add RequiredPass into list of lower level passes required by pass P.
1230/// RequiredPass is run on the fly by Pass Manager when P requests it
1231/// through getAnalysis interface.
1232/// This should be handled by specific pass manager.
1233void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1234 if (TPM) {
1235 TPM->dumpArguments();
1236 TPM->dumpPasses();
1237 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001238
Dan Gohmande6188a2010-08-12 23:50:08 +00001239 // Module Level pass may required Function Level analysis info
1240 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1241 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001242 // module level pass is requiring lower level analysis info managed by
1243 // lower level pass manager.
1244
1245 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001246 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001247 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001248#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001249 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1250 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001251#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001252 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001253}
1254
Owen Andersona7aed182010-08-06 18:33:48 +00001255Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001256 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001257}
1258
Devang Patele7599552007-01-12 18:52:44 +00001259// Destructor
1260PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001261 for (Pass *P : PassVector)
1262 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001263}
1264
Devang Patel9bdf7d42006-12-08 23:28:54 +00001265//===----------------------------------------------------------------------===//
1266// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001267// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1268Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001269 return PM.findAnalysisPass(ID, dir);
1270}
1271
Dan Gohmande6188a2010-08-12 23:50:08 +00001272Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001273 Function &F) {
1274 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1275}
1276
Devang Patela1514cb2006-12-07 19:39:39 +00001277//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001278// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001279
Dan Gohmande6188a2010-08-12 23:50:08 +00001280/// Execute all of the passes scheduled for execution by invoking
1281/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001282/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001283bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001284 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001285 return false;
1286
Devang Patele9585592006-12-08 01:38:28 +00001287 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001288 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001289
Xin Tong023e25a2018-07-22 05:27:41 +00001290 unsigned InstrCount = 0;
1291 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001292 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001293 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1294 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001295 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001296
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001297 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001298 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001299
Devang Patelabfbe3b2006-12-16 00:56:26 +00001300 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001301
Chris Lattner4c1e9542009-03-06 06:45:05 +00001302 {
1303 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001304 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001305 TimeRegion PassTimer(getPassTimer(BP));
Xin Tong023e25a2018-07-22 05:27:41 +00001306 if (EmitICRemark)
1307 InstrCount = initSizeRemarkInfo(M);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001308 LocalChanged |= BP->runOnBasicBlock(BB);
Xin Tong023e25a2018-07-22 05:27:41 +00001309 if (EmitICRemark)
1310 emitInstrCountChangedRemark(BP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001311 }
Devang Patel93a197c2006-12-14 00:08:04 +00001312
Dan Gohman74b189f2010-03-01 17:34:28 +00001313 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001314 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001315 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001316 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001317 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001318 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001319
Devang Patela273d1c2007-07-19 18:02:32 +00001320 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001321 removeNotPreservedAnalysis(BP);
1322 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001323 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001324 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001325
Bill Wendling6ce6d262009-12-25 13:50:18 +00001326 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001327}
1328
Devang Patel475c4532006-12-08 00:59:05 +00001329// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001330bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001331 bool Changed = false;
1332
Chris Lattner4c1e9542009-03-06 06:45:05 +00001333 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1334 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001335
1336 return Changed;
1337}
1338
Duncan Sands51495602009-02-13 09:42:34 +00001339bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001340 bool Changed = false;
1341
Pedro Artigas41b98842012-12-05 17:12:22 +00001342 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001343 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001344
1345 return Changed;
1346}
1347
Duncan Sands51495602009-02-13 09:42:34 +00001348bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001349 bool Changed = false;
1350
Devang Patelabfbe3b2006-12-16 00:56:26 +00001351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1352 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001353 Changed |= BP->doInitialization(F);
1354 }
1355
1356 return Changed;
1357}
1358
Duncan Sands51495602009-02-13 09:42:34 +00001359bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001360 bool Changed = false;
1361
Devang Patelabfbe3b2006-12-16 00:56:26 +00001362 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1363 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001364 Changed |= BP->doFinalization(F);
1365 }
1366
1367 return Changed;
1368}
1369
1370
Devang Patela1514cb2006-12-07 19:39:39 +00001371//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001372// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001373
Devang Patel4e12f862006-11-08 10:44:40 +00001374/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001375FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001376 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001377 // FPM is the top level manager.
1378 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001379
Dan Gohman565df952008-03-13 02:08:36 +00001380 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001381 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001382}
1383
Devang Patelb67904d2006-12-13 02:36:01 +00001384FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001385 delete FPM;
1386}
1387
Dan Gohmande6188a2010-08-12 23:50:08 +00001388void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001389 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001390}
1391
Devang Patel9f3083e2006-11-15 19:39:54 +00001392/// run - Execute all of the passes scheduled for execution. Keep
1393/// track of whether any of the passes modifies the function, and if
1394/// so, return true.
1395///
Devang Patelb67904d2006-12-13 02:36:01 +00001396bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001397 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1398 report_fatal_error("Error reading bitcode file: " + EIB.message());
1399 });
Devang Patel272908d2006-12-08 22:57:48 +00001400 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001401}
1402
1403
Devang Patelff631ae2006-11-15 01:27:05 +00001404/// doInitialization - Run all of the initializers for the function passes.
1405///
Devang Patelb67904d2006-12-13 02:36:01 +00001406bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001407 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001408}
1409
Dan Gohmane6656eb2007-07-30 14:51:13 +00001410/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001411///
Devang Patelb67904d2006-12-13 02:36:01 +00001412bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001413 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001414}
1415
Devang Patela1514cb2006-12-07 19:39:39 +00001416//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001417// FunctionPassManagerImpl implementation
1418//
Duncan Sands51495602009-02-13 09:42:34 +00001419bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001420 bool Changed = false;
1421
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001422 dumpArguments();
1423 dumpPasses();
1424
Yaron Keren4849aa32015-06-05 17:48:47 +00001425 for (ImmutablePass *ImPass : getImmutablePasses())
1426 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001427
Chris Lattner4c1e9542009-03-06 06:45:05 +00001428 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1429 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001430
1431 return Changed;
1432}
1433
Duncan Sands51495602009-02-13 09:42:34 +00001434bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001435 bool Changed = false;
1436
Pedro Artigas41b98842012-12-05 17:12:22 +00001437 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001438 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001439
Yaron Keren4849aa32015-06-05 17:48:47 +00001440 for (ImmutablePass *ImPass : getImmutablePasses())
1441 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001442
Devang Patel67d6a5e2006-12-19 19:46:59 +00001443 return Changed;
1444}
1445
Devang Patelec9c58f2009-04-01 22:34:41 +00001446/// cleanup - After running all passes, clean up pass manager cache.
1447void FPPassManager::cleanup() {
1448 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1449 FunctionPass *FP = getContainedPass(Index);
1450 AnalysisResolver *AR = FP->getResolver();
1451 assert(AR && "Analysis Resolver is not set");
1452 AR->clearAnalysisImpls();
1453 }
1454}
1455
Torok Edwin24c78352009-06-29 18:49:09 +00001456void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1457 if (!wasRun)
1458 return;
1459 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1460 FPPassManager *FPPM = getContainedManager(Index);
1461 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1462 FPPM->getContainedPass(Index)->releaseMemory();
1463 }
1464 }
Torok Edwin896556e2009-06-29 21:05:10 +00001465 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001466}
1467
Devang Patel67d6a5e2006-12-19 19:46:59 +00001468// Execute all the passes managed by this top level manager.
1469// Return true if any function is modified by a pass.
1470bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001471 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001472
Devang Patele3068402006-12-21 00:16:50 +00001473 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001474 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001475 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001476 F.getContext().yield();
1477 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001478
1479 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1480 getContainedManager(Index)->cleanup();
1481
Torok Edwin24c78352009-06-29 18:49:09 +00001482 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483 return Changed;
1484}
1485
1486//===----------------------------------------------------------------------===//
1487// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001488
Devang Patel8c78a0b2007-05-03 01:11:54 +00001489char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001490/// Print passes managed by this manager
1491void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001492 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001493 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1494 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001495 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001496 dumpLastUses(FP, Offset+1);
1497 }
1498}
1499
1500
Dan Gohmande6188a2010-08-12 23:50:08 +00001501/// Execute all of the passes scheduled for execution by invoking
1502/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001503/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001504bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001505 if (F.isDeclaration())
1506 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001507
1508 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001509 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001510 // Collect inherited analysis from Module level pass manager.
1511 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001512
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001513 unsigned InstrCount, FunctionSize = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001514 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001515 // Collect the initial size of the module.
1516 if (EmitICRemark) {
1517 InstrCount = initSizeRemarkInfo(M);
1518 FunctionSize = F.getInstructionCount();
1519 }
1520
Devang Patelabfbe3b2006-12-16 00:56:26 +00001521 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1522 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001523 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001524
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001526 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001527
Devang Patelabfbe3b2006-12-16 00:56:26 +00001528 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001529
Chris Lattner4c1e9542009-03-06 06:45:05 +00001530 {
1531 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001532 TimeRegion PassTimer(getPassTimer(FP));
Dan Gohman74b189f2010-03-01 17:34:28 +00001533 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001534 if (EmitICRemark) {
1535 unsigned NewSize = F.getInstructionCount();
1536
1537 // Update the size of the function, emit a remark, and update the size
1538 // of the module.
1539 if (NewSize != FunctionSize) {
1540 emitInstrCountChangedRemark(FP, M, InstrCount);
1541 int64_t Delta = static_cast<int64_t>(NewSize) -
1542 static_cast<int64_t>(FunctionSize);
1543 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1544 FunctionSize = NewSize;
1545 }
1546 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001547 }
Devang Patel93a197c2006-12-14 00:08:04 +00001548
Dan Gohman74b189f2010-03-01 17:34:28 +00001549 Changed |= LocalChanged;
1550 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001551 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001552 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001553 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001554
Devang Patela273d1c2007-07-19 18:02:32 +00001555 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001556 removeNotPreservedAnalysis(FP);
1557 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001558 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001559 }
1560 return Changed;
1561}
1562
Devang Patel67d6a5e2006-12-19 19:46:59 +00001563bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001564 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001565
Serge Pavloved5eb932017-01-15 10:23:18 +00001566 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001567 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001568
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001569 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001570}
1571
Duncan Sands51495602009-02-13 09:42:34 +00001572bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001573 bool Changed = false;
1574
Chris Lattner4c1e9542009-03-06 06:45:05 +00001575 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1576 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001577
Devang Patelff631ae2006-11-15 01:27:05 +00001578 return Changed;
1579}
1580
Duncan Sands51495602009-02-13 09:42:34 +00001581bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001582 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001583
1584 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001585 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001586
Devang Patelff631ae2006-11-15 01:27:05 +00001587 return Changed;
1588}
1589
Devang Patela1514cb2006-12-07 19:39:39 +00001590//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001591// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001592
Dan Gohmande6188a2010-08-12 23:50:08 +00001593/// Execute all of the passes scheduled for execution by invoking
1594/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001595/// the module, and if so, return true.
1596bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001597MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001598 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001599
Torok Edwin24c78352009-06-29 18:49:09 +00001600 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001601 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1602 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001603 Changed |= FPP->doInitialization(M);
1604 }
1605
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001606 // Initialize module passes
1607 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1608 Changed |= getContainedPass(Index)->doInitialization(M);
1609
Xin Tong023e25a2018-07-22 05:27:41 +00001610 unsigned InstrCount = 0;
1611 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001612 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1613 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001614 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001615
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001616 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001617 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001618
Devang Patelabfbe3b2006-12-16 00:56:26 +00001619 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001620
Chris Lattner4c1e9542009-03-06 06:45:05 +00001621 {
1622 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001623 TimeRegion PassTimer(getPassTimer(MP));
1624
Xin Tong023e25a2018-07-22 05:27:41 +00001625 if (EmitICRemark)
1626 InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001627 LocalChanged |= MP->runOnModule(M);
Xin Tong023e25a2018-07-22 05:27:41 +00001628 if (EmitICRemark)
1629 emitInstrCountChangedRemark(MP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001630 }
Devang Patel93a197c2006-12-14 00:08:04 +00001631
Dan Gohman74b189f2010-03-01 17:34:28 +00001632 Changed |= LocalChanged;
1633 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001634 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001635 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001636 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001637 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001638
Devang Patela273d1c2007-07-19 18:02:32 +00001639 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001640 removeNotPreservedAnalysis(MP);
1641 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001642 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001643 }
Torok Edwin24c78352009-06-29 18:49:09 +00001644
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001645 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001646 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001647 Changed |= getContainedPass(Index)->doFinalization(M);
1648
Torok Edwin24c78352009-06-29 18:49:09 +00001649 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001650 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1651 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001652 // We don't know when is the last time an on-the-fly pass is run,
1653 // so we need to releaseMemory / finalize here
1654 FPP->releaseMemoryOnTheFly();
1655 Changed |= FPP->doFinalization(M);
1656 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001657
Devang Patel05e1a972006-11-07 22:03:15 +00001658 return Changed;
1659}
1660
Devang Patele64d3052007-04-16 20:12:57 +00001661/// Add RequiredPass into list of lower level passes required by pass P.
1662/// RequiredPass is run on the fly by Pass Manager when P requests it
1663/// through getAnalysis interface.
1664void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001665 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1666 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001667 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001668 RequiredPass->getPotentialPassManagerType()) &&
1669 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001670 if (!RequiredPass)
1671 return;
Devang Patele64d3052007-04-16 20:12:57 +00001672
Devang Patel68f72b12007-04-26 17:50:19 +00001673 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001674 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001675 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001676 // FPP is the top level manager.
1677 FPP->setTopLevelManager(FPP);
1678
Devang Patel69e9f6d2007-04-16 20:27:05 +00001679 OnTheFlyManagers[P] = FPP;
1680 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001681 const PassInfo *RequiredPassPI =
1682 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001683
Craig Topperc6207612014-04-09 06:08:46 +00001684 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001685 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1686 FoundPass =
1687 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001688 }
Andrew Trick02066f22014-04-08 03:40:34 +00001689 if (!FoundPass) {
1690 FoundPass = RequiredPass;
1691 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001692 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001693 FPP->add(RequiredPass);
1694 }
1695 // Register P as the last user of FoundPass or RequiredPass.
1696 SmallVector<Pass *, 1> LU;
1697 LU.push_back(FoundPass);
1698 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001699}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001700
Dan Gohmande6188a2010-08-12 23:50:08 +00001701/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001702/// required by module pass MP. Instantiate analysis pass, by using
1703/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001704Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001705 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001706 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001707
Torok Edwin24c78352009-06-29 18:49:09 +00001708 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001709 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001710 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001711}
1712
1713
Devang Patela1514cb2006-12-07 19:39:39 +00001714//===----------------------------------------------------------------------===//
1715// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001716
Devang Patelab97cf42006-12-13 00:09:23 +00001717//
Devang Patelc290c8a2006-11-07 22:23:34 +00001718/// run - Execute all of the passes scheduled for execution. Keep track of
1719/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001720bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001721 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001722
Devang Patelcfd70c42006-12-13 22:10:00 +00001723 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001724 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001725
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001726 for (ImmutablePass *ImPass : getImmutablePasses())
1727 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001728
Devang Patele3068402006-12-21 00:16:50 +00001729 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001730 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001731 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001732 M.getContext().yield();
1733 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001734
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001735 for (ImmutablePass *ImPass : getImmutablePasses())
1736 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001737
Devang Patelc290c8a2006-11-07 22:23:34 +00001738 return Changed;
1739}
Devang Patel376fefa2006-11-08 10:29:57 +00001740
Devang Patela1514cb2006-12-07 19:39:39 +00001741//===----------------------------------------------------------------------===//
1742// PassManager implementation
1743
Devang Patel376fefa2006-11-08 10:29:57 +00001744/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001745PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001746 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001747 // PM is the top level manager
1748 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001749}
1750
Devang Patelb67904d2006-12-13 02:36:01 +00001751PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001752 delete PM;
1753}
1754
Chris Lattner60987362009-03-06 05:53:14 +00001755void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001756 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001757}
1758
1759/// run - Execute all of the passes scheduled for execution. Keep track of
1760/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001761bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001762 return PM->run(M);
1763}
1764
Devang Patelb8817b92006-12-14 00:59:42 +00001765//===----------------------------------------------------------------------===//
Devang Patel1c56a632007-01-08 19:29:38 +00001766// PMStack implementation
1767//
Devang Patelad98d232007-01-11 22:15:30 +00001768
Devang Patel1c56a632007-01-08 19:29:38 +00001769// Pop Pass Manager from the stack and clear its analysis info.
1770void PMStack::pop() {
1771
1772 PMDataManager *Top = this->top();
1773 Top->initializeAnalysisInfo();
1774
1775 S.pop_back();
1776}
1777
1778// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001779void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001780 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001781 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001782
Chris Lattner60987362009-03-06 05:53:14 +00001783 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001784 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1785 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001786 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001787
Chris Lattner60987362009-03-06 05:53:14 +00001788 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001789 TPM->addIndirectPassManager(PM);
1790 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001791 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001792 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001793 assert((PM->getPassManagerType() == PMT_ModulePassManager
1794 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001795 && "pushing bad pass manager to PMStack");
1796 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001797 }
1798
Devang Patel15701b52007-01-11 00:19:00 +00001799 S.push_back(PM);
1800}
1801
1802// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001803LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001804 for (PMDataManager *Manager : S)
1805 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001806
Devang Patel15701b52007-01-11 00:19:00 +00001807 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001808 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001809}
1810
Devang Patel1c56a632007-01-08 19:29:38 +00001811/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001812/// add self into that manager.
1813void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001814 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001815 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001816 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001817 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1818 if (TopPMType == PreferredType)
1819 break; // We found desired pass manager
1820 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001821 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001822 else
1823 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001824 }
Devang Patel18ff6362008-09-09 21:38:40 +00001825 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001826 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001827}
1828
Devang Patel3312f752007-01-16 21:43:18 +00001829/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001830/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001831void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001832 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001833
Andrew Trickcbc845f2012-02-01 07:16:20 +00001834 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001835 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001836 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1837 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001838 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001839 break;
Devang Patel3312f752007-01-16 21:43:18 +00001840 }
Devang Patel3312f752007-01-16 21:43:18 +00001841
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001842 // Create new Function Pass Manager if needed.
1843 FPPassManager *FPP;
1844 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1845 FPP = (FPPassManager *)PMS.top();
1846 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001847 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1848 PMDataManager *PMD = PMS.top();
1849
1850 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001851 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001852 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001853
1854 // [2] Set up new manager's top level manager
1855 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1856 TPM->addIndirectPassManager(FPP);
1857
1858 // [3] Assign manager to manage this new manager. This may create
1859 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001860 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001861
1862 // [4] Push new manager into PMS
1863 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001864 }
1865
Devang Patel3312f752007-01-16 21:43:18 +00001866 // Assign FPP as the manager of this pass.
1867 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001868}
1869
Devang Patel3312f752007-01-16 21:43:18 +00001870/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001871/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001872void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001873 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001874 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001875
Devang Patel15701b52007-01-11 00:19:00 +00001876 // Basic Pass Manager is a leaf pass manager. It does not handle
1877 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001878 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001879 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1880 BBP = (BBPassManager *)PMS.top();
1881 } else {
1882 // If leaf manager is not Basic Block Pass manager then create new
1883 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001884 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1885 PMDataManager *PMD = PMS.top();
1886
1887 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001888 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001889
1890 // [2] Set up new manager's top level manager
1891 // Basic Block Pass Manager does not live by itself
1892 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1893 TPM->addIndirectPassManager(BBP);
1894
Devang Patel15701b52007-01-11 00:19:00 +00001895 // [3] Assign manager to manage this new manager. This may create
1896 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001897 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001898
Devang Patel3312f752007-01-16 21:43:18 +00001899 // [4] Push new manager into PMS
1900 PMS.push(BBP);
1901 }
Devang Patel1c56a632007-01-08 19:29:38 +00001902
Devang Patel3312f752007-01-16 21:43:18 +00001903 // Assign BBP as the manager of this pass.
1904 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001905}
1906
Dan Gohmand3a20c92008-03-11 16:41:42 +00001907PassManagerBase::~PassManagerBase() {}