blob: 5435e07e688bf28bbbf4175b665479e9d1330911 [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,
Jessica Paquette9a23c552018-08-31 20:20:57 +0000145 int64_t Delta,
Jessica Paquette71e97782018-08-31 20:54:37 +0000146 unsigned CountBefore,
147 Function *F) {
Jessica Paquette397c05d2018-08-31 20:51:54 +0000148 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
149 // that the only passes that return non-null with getAsPMDataManager are pass
150 // managers.) The reason we have to do this is to avoid emitting remarks for
151 // CGSCC passes.
152 if (P->getAsPMDataManager())
153 return;
154
Jessica Paquette31d2e5e2018-09-04 21:03:43 +0000155 // Set to true if this isn't a module pass or CGSCC pass.
156 bool CouldOnlyImpactOneFunction = (F != nullptr);
157
Jessica Paquette71e97782018-08-31 20:54:37 +0000158 // Do we have a function we can use to emit a remark?
Jessica Paquette31d2e5e2018-09-04 21:03:43 +0000159 if (!CouldOnlyImpactOneFunction) {
Jessica Paquette71e97782018-08-31 20:54:37 +0000160 // We need a function containing at least one basic block in order to output
161 // remarks. Since it's possible that the first function in the module
162 // doesn't actually contain a basic block, we have to go and find one that's
163 // suitable for emitting remarks.
164 auto It = std::find_if(M.begin(), M.end(),
165 [](const Function &Fn) { return !Fn.empty(); });
Jessica Paquettee49374d2018-05-18 17:26:39 +0000166
Jessica Paquette71e97782018-08-31 20:54:37 +0000167 // Didn't find a function. Quit.
168 if (It == M.end())
169 return;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000170
Jessica Paquette71e97782018-08-31 20:54:37 +0000171 // We found a function containing at least one basic block.
172 F = &*It;
173 }
Jessica Paquette9a23c552018-08-31 20:20:57 +0000174 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000175 BasicBlock &BB = *F->begin();
176 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
177 DiagnosticLocation(), &BB);
178 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
179 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
180 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
181 << ": IR instruction count changed from "
182 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
183 << " to "
184 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
185 << "; Delta: "
186 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
187 F->getContext().diagnose(R); // Not using ORE for layering reasons.
188}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000189
Chris Lattner4c1e9542009-03-06 06:45:05 +0000190void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000191 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000192 OS << "Releasing pass '";
193 else
194 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000195
Chris Lattner4c1e9542009-03-06 06:45:05 +0000196 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000197
Chris Lattner4c1e9542009-03-06 06:45:05 +0000198 if (M) {
199 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
200 return;
201 }
Craig Topperc6207612014-04-09 06:08:46 +0000202 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000203 OS << '\n';
204 return;
205 }
206
Dan Gohman79fc0e92009-03-10 18:47:59 +0000207 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000208 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000209 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000210 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000211 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000212 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000213 OS << "value";
214
215 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000216 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000217 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000218}
219
220
Devang Patelffca9102006-12-15 19:39:30 +0000221namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000222//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000224//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000226/// pass together and sequence them to process one basic block before
227/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000228class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000229
230public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000231 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000232 explicit BBPassManager()
233 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000234
Devang Patelca58e352006-11-08 10:05:38 +0000235 /// Execute all of the passes scheduled for execution. Keep track of
236 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000237 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000238
Devang Patelf9d96b92006-12-07 19:57:52 +0000239 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000240 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000241 Info.setPreservesAll();
242 }
243
Craig Topperf398d7c2014-03-05 06:35:38 +0000244 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000245 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000246 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000247 bool doFinalization(Function &F);
248
Craig Topperf398d7c2014-03-05 06:35:38 +0000249 PMDataManager *getAsPMDataManager() override { return this; }
250 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000251
Mehdi Amini117296c2016-10-01 02:56:57 +0000252 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000253
Devang Pateleda56172006-12-12 23:34:33 +0000254 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000255 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000256 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000257 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
258 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000259 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000260 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000261 }
262 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000263
264 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000265 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000266 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
267 return BP;
268 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000269
Craig Topperf398d7c2014-03-05 06:35:38 +0000270 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000271 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000272 }
Devang Patelca58e352006-11-08 10:05:38 +0000273};
274
Devang Patel8c78a0b2007-05-03 01:11:54 +0000275char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000276} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000277
Devang Patele7599552007-01-12 18:52:44 +0000278namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000279namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000280//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000282//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283/// FunctionPassManagerImpl manages FPPassManagers
284class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000285 public PMDataManager,
286 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000287 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000288private:
289 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000291 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000292 explicit FunctionPassManagerImpl() :
293 Pass(PT_PassManager, ID), PMDataManager(),
294 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000295
Matthias Braun0880c602014-12-12 01:27:01 +0000296 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000297 void add(Pass *P) {
298 schedulePass(P);
299 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000300
301 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000302 Pass *createPrinterPass(raw_ostream &O,
303 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000304 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000305 }
306
Torok Edwin24c78352009-06-29 18:49:09 +0000307 // Prepare for running an on the fly pass, freeing memory if needed
308 // from a previous run.
309 void releaseMemoryOnTheFly();
310
Devang Patel67d6a5e2006-12-19 19:46:59 +0000311 /// run - Execute all of the passes scheduled for execution. Keep track of
312 /// whether any of the passes modifies the module, and if so, return true.
313 bool run(Function &F);
314
315 /// doInitialization - Run all of the initializers for the function passes.
316 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000317 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000318
Dan Gohmane6656eb2007-07-30 14:51:13 +0000319 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000320 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000321 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000322
Dan Gohmande6188a2010-08-12 23:50:08 +0000323
Craig Topperf398d7c2014-03-05 06:35:38 +0000324 PMDataManager *getAsPMDataManager() override { return this; }
325 Pass *getAsPass() override { return this; }
326 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000327 return PMT_FunctionPassManager;
328 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000329
Devang Patel67d6a5e2006-12-19 19:46:59 +0000330 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000331 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000332 Info.setPreservesAll();
333 }
334
Devang Patel67d6a5e2006-12-19 19:46:59 +0000335 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000336 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000337 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
338 return FP;
339 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000340};
341
David Blaikiea379b1812011-12-20 02:50:00 +0000342void FunctionPassManagerImpl::anchor() {}
343
Devang Patel8c78a0b2007-05-03 01:11:54 +0000344char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000345} // End of legacy namespace
346} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000347
Chandler Carruth7caea412013-11-09 12:26:54 +0000348namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000349//===----------------------------------------------------------------------===//
350// MPPassManager
351//
352/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000353/// It batches all Module passes and function pass managers together and
354/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000355class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000356public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000357 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000358 explicit MPPassManager() :
359 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000360
361 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000362 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000363 for (auto &OnTheFlyManager : OnTheFlyManagers) {
364 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000365 delete FPP;
366 }
367 }
368
Dan Gohmande6188a2010-08-12 23:50:08 +0000369 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000370 Pass *createPrinterPass(raw_ostream &O,
371 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000372 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000373 }
374
Devang Patelca58e352006-11-08 10:05:38 +0000375 /// run - Execute all of the passes scheduled for execution. Keep track of
376 /// whether any of the passes modifies the module, and if so, return true.
377 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000378
Pedro Artigase4348b02012-12-03 21:56:57 +0000379 using llvm::Pass::doInitialization;
380 using llvm::Pass::doFinalization;
381
Devang Patelf9d96b92006-12-07 19:57:52 +0000382 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000383 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000384 Info.setPreservesAll();
385 }
386
Devang Patele64d3052007-04-16 20:12:57 +0000387 /// Add RequiredPass into list of lower level passes required by pass P.
388 /// RequiredPass is run on the fly by Pass Manager when P requests it
389 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000390 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000391
Dan Gohmande6188a2010-08-12 23:50:08 +0000392 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000393 /// required by module pass MP. Instantiate analysis pass, by using
394 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000395 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000396
Mehdi Amini117296c2016-10-01 02:56:57 +0000397 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000398
Craig Topperf398d7c2014-03-05 06:35:38 +0000399 PMDataManager *getAsPMDataManager() override { return this; }
400 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000401
Devang Pateleda56172006-12-12 23:34:33 +0000402 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000403 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000404 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000405 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
406 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000407 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000408 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
409 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000410 if (I != OnTheFlyManagers.end())
411 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000412 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000413 }
414 }
415
Devang Patelabfbe3b2006-12-16 00:56:26 +0000416 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000417 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000418 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000419 }
420
Craig Topperf398d7c2014-03-05 06:35:38 +0000421 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000422 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000423 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000424
425 private:
426 /// Collection of on the fly FPPassManagers. These managers manage
427 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000428 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000429};
430
Devang Patel8c78a0b2007-05-03 01:11:54 +0000431char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000432} // End anonymous namespace
433
434namespace llvm {
435namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000436//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000437// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000438//
Devang Patel09f162c2007-05-01 21:15:47 +0000439
Devang Patel67d6a5e2006-12-19 19:46:59 +0000440/// PassManagerImpl manages MPPassManagers
441class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000442 public PMDataManager,
443 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000444 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000445
446public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000447 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000448 explicit PassManagerImpl() :
449 Pass(PT_PassManager, ID), PMDataManager(),
450 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000451
Matthias Braun0880c602014-12-12 01:27:01 +0000452 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000453 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000454 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000455 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000456
457 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000458 Pass *createPrinterPass(raw_ostream &O,
459 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000460 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000461 }
462
Devang Patel376fefa2006-11-08 10:29:57 +0000463 /// run - Execute all of the passes scheduled for execution. Keep track of
464 /// whether any of the passes modifies the module, and if so, return true.
465 bool run(Module &M);
466
Pedro Artigase4348b02012-12-03 21:56:57 +0000467 using llvm::Pass::doInitialization;
468 using llvm::Pass::doFinalization;
469
Devang Patelf9d96b92006-12-07 19:57:52 +0000470 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000471 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000472 Info.setPreservesAll();
473 }
474
Craig Topperf398d7c2014-03-05 06:35:38 +0000475 PMDataManager *getAsPMDataManager() override { return this; }
476 Pass *getAsPass() override { return this; }
477 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000478 return PMT_ModulePassManager;
479 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000480
Devang Patel67d6a5e2006-12-19 19:46:59 +0000481 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000482 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000483 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
484 return MP;
485 }
Devang Patel376fefa2006-11-08 10:29:57 +0000486};
487
David Blaikiea379b1812011-12-20 02:50:00 +0000488void PassManagerImpl::anchor() {}
489
Devang Patel8c78a0b2007-05-03 01:11:54 +0000490char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000491} // End of legacy namespace
492} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000493
Devang Patela1514cb2006-12-07 19:39:39 +0000494//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000495// PMTopLevelManager implementation
496
Devang Patel4268fc02007-01-16 02:00:38 +0000497/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000498PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
499 PMDM->setTopLevelManager(this);
500 addPassManager(PMDM);
501 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000502}
503
Devang Patelafb1f3622006-12-12 22:35:25 +0000504/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000505void
Bill Wendlingea857e12012-05-14 07:53:40 +0000506PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000507 unsigned PDepth = 0;
508 if (P->getResolver())
509 PDepth = P->getResolver()->getPMDataManager().getDepth();
510
Yaron Keren4849aa32015-06-05 17:48:47 +0000511 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000512 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000513
Devang Patel01919d22007-03-08 19:05:01 +0000514 if (P == AP)
515 continue;
516
Tobias Grosserf07426b2011-01-20 21:03:22 +0000517 // Update the last users of passes that are required transitive by AP.
518 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
519 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
520 SmallVector<Pass *, 12> LastUses;
521 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000522 for (AnalysisID ID : IDs) {
523 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000524 assert(AnalysisPass && "Expected analysis pass to exist.");
525 AnalysisResolver *AR = AnalysisPass->getResolver();
526 assert(AR && "Expected analysis resolver to exist.");
527 unsigned APDepth = AR->getPMDataManager().getDepth();
528
529 if (PDepth == APDepth)
530 LastUses.push_back(AnalysisPass);
531 else if (PDepth > APDepth)
532 LastPMUses.push_back(AnalysisPass);
533 }
534
535 setLastUser(LastUses, P);
536
537 // If this pass has a corresponding pass manager, push higher level
538 // analysis to this pass manager.
539 if (P->getResolver())
540 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
541
542
Devang Patelafb1f3622006-12-12 22:35:25 +0000543 // If AP is the last user of other passes then make P last user of
544 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000545 for (auto LU : LastUser) {
546 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000547 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000548 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000549 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000550 }
551 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000552}
553
554/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000555void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000556 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000557 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000558 InversedLastUser.find(P);
559 if (DMI == InversedLastUser.end())
560 return;
561
562 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000563 for (Pass *LUP : LU) {
564 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000565 }
566
Devang Patelafb1f3622006-12-12 22:35:25 +0000567}
568
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000569AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000570 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000571 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000572 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000573 AnUsage = DMI->second;
574 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000575 // Look up the analysis usage from the pass instance (different instances
576 // of the same pass can produce different results), but unique the
577 // resulting object to reduce memory usage. This helps to greatly reduce
578 // memory usage when we have many instances of only a few pass types
579 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
580 // of dependencies.
581 AnalysisUsage AU;
582 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000583
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000584 AUFoldingSetNode* Node = nullptr;
585 FoldingSetNodeID ID;
586 AUFoldingSetNode::Profile(ID, AU);
587 void *IP = nullptr;
588 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
589 Node = N;
590 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000591 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000592 UniqueAnalysisUsages.InsertNode(Node, IP);
593 }
594 assert(Node && "cached analysis usage must be non null");
595
596 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000597 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000598 }
599 return AnUsage;
600}
601
Devang Patelafb1f3622006-12-12 22:35:25 +0000602/// Schedule pass P for execution. Make sure that passes required by
603/// P are run before P is run. Update analysis info maintained by
604/// the manager. Remove dead passes. This is a recursive function.
605void PMTopLevelManager::schedulePass(Pass *P) {
606
Devang Patel3312f752007-01-16 21:43:18 +0000607 // TODO : Allocate function manager for this pass, other wise required set
608 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000609
Devang Pateld74ede72007-03-06 01:06:16 +0000610 // Give pass a chance to prepare the stage.
611 P->preparePassManager(activeStack);
612
Devang Patel864970e2008-03-18 00:39:19 +0000613 // If P is an analysis pass and it is available then do not
614 // generate the analysis again. Stale analysis info should not be
615 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000616 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000617 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topper4ee28412018-08-20 20:57:30 +0000618 // Remove any cached AnalysisUsage information.
619 AnUsageMap.erase(P);
Nuno Lopes0460bb22008-11-04 23:03:58 +0000620 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000621 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000622 }
Devang Patel864970e2008-03-18 00:39:19 +0000623
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000624 AnalysisUsage *AnUsage = findAnalysisUsage(P);
625
Devang Patelfdee7032008-08-14 23:07:48 +0000626 bool checkAnalysis = true;
627 while (checkAnalysis) {
628 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000629
Devang Patelfdee7032008-08-14 23:07:48 +0000630 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000631 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000632
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000633 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000634 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000635 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000636
Craig Topperc6207612014-04-09 06:08:46 +0000637 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000638 // Pass P is not in the global PassRegistry
639 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
640 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
641 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000642 for (const AnalysisID ID2 : RequiredSet) {
643 if (ID == ID2)
644 break;
645 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000646 if (AnalysisPass2) {
647 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000648 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000649 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
650 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
651 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
652 }
653 }
654 }
655
Andrew Trick6bbaf132011-06-03 00:48:58 +0000656 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000657 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000658 if (P->getPotentialPassManagerType () ==
659 AnalysisPass->getPotentialPassManagerType())
660 // Schedule analysis pass that is managed by the same pass manager.
661 schedulePass(AnalysisPass);
662 else if (P->getPotentialPassManagerType () >
663 AnalysisPass->getPotentialPassManagerType()) {
664 // Schedule analysis pass that is managed by a new manager.
665 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000666 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000667 // are already checked are still available.
668 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000669 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000670 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000671 // passes are run on the fly.
672 delete AnalysisPass;
673 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000674 }
675 }
676
677 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000678 if (ImmutablePass *IP = P->getAsImmutablePass()) {
679 // P is a immutable pass and it will be managed by this
680 // top level manager. Set up analysis resolver to connect them.
681 PMDataManager *DM = getAsPMDataManager();
682 AnalysisResolver *AR = new AnalysisResolver(*DM);
683 P->setResolver(AR);
684 DM->initializeAnalysisImpl(P);
685 addImmutablePass(IP);
686 DM->recordAvailableAnalysis(IP);
687 return;
688 }
689
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000690 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000691 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000692 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000693 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
694 }
695
696 // Add the requested pass to the best available pass manager.
697 P->assignPassManager(activeStack, getTopLevelPassManagerType());
698
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000699 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000700 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000701 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000702 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
703 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000704}
705
706/// Find the pass that implements Analysis AID. Search immutable
707/// passes and all pass managers. If desired pass is not found
708/// then return NULL.
709Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000710 // For immutable passes we have a direct mapping from ID to pass, so check
711 // that first.
712 if (Pass *P = ImmutablePassMap.lookup(AID))
713 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000714
Devang Patelcd6ba152006-12-12 22:50:05 +0000715 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000716 for (PMDataManager *PassManager : PassManagers)
717 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000718 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000719
720 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000721 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
722 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000723 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000724
Craig Topperc6207612014-04-09 06:08:46 +0000725 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000726}
727
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000728const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
729 const PassInfo *&PI = AnalysisPassInfos[AID];
730 if (!PI)
731 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
732 else
733 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
734 "The pass info pointer changed for an analysis ID!");
735
736 return PI;
737}
738
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000739void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
740 P->initializePass();
741 ImmutablePasses.push_back(P);
742
743 // Add this pass to the map from its analysis ID. We clobber any prior runs
744 // of the pass in the map so that the last one added is the one found when
745 // doing lookups.
746 AnalysisID AID = P->getPassID();
747 ImmutablePassMap[AID] = P;
748
749 // Also add any interfaces implemented by the immutable pass to the map for
750 // fast lookup.
751 const PassInfo *PassInf = findAnalysisPassInfo(AID);
752 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000753 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000754 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
755}
756
Devang Pateleda56172006-12-12 23:34:33 +0000757// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000758void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000759
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000760 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000761 return;
762
Devang Pateleda56172006-12-12 23:34:33 +0000763 // Print out the immutable passes
764 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000765 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000766 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000767
Dan Gohmanf71c5212010-08-19 01:29:07 +0000768 // Every class that derives from PMDataManager also derives from Pass
769 // (sometimes indirectly), but there's no inheritance relationship
770 // between PMDataManager and Pass, so we have to getAsPass to get
771 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000772 for (PMDataManager *Manager : PassManagers)
773 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000774}
775
Devang Patel991aeba2006-12-15 20:13:01 +0000776void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000777
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000778 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000779 return;
780
David Greene994e1bb2010-01-05 01:30:02 +0000781 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000782 for (ImmutablePass *P : ImmutablePasses)
783 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000784 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000785 if (!PI->isAnalysisGroup())
786 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000787 }
Yaron Keren83009952016-04-28 14:49:44 +0000788 for (PMDataManager *PM : PassManagers)
789 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000790 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000791}
792
Devang Patele3068402006-12-21 00:16:50 +0000793void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000794 for (PMDataManager *PM : PassManagers)
795 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000796
Devang Patele3068402006-12-21 00:16:50 +0000797 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000798 for (PMDataManager *IPM : IndirectPassManagers)
799 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000800
Yaron Kerenbd873192016-10-02 19:21:41 +0000801 for (auto LU : LastUser) {
802 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
803 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000804 }
Devang Patele3068402006-12-21 00:16:50 +0000805}
806
Devang Patele7599552007-01-12 18:52:44 +0000807/// Destructor
808PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000809 for (PMDataManager *PM : PassManagers)
810 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000811
Yaron Keren83009952016-04-28 14:49:44 +0000812 for (ImmutablePass *P : ImmutablePasses)
813 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000814}
815
Devang Patelafb1f3622006-12-12 22:35:25 +0000816//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000817// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000818
Devang Patel643676c2006-11-11 01:10:19 +0000819/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000820void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000821 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000822
Chris Lattner60987362009-03-06 05:53:14 +0000823 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000824
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000825 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000826
Dan Gohmande6188a2010-08-12 23:50:08 +0000827 // This pass is the current implementation of all of the interfaces it
828 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000829 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000830 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000831 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000832 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000833 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000834}
835
Devang Patel9d9fc902007-03-06 17:52:53 +0000836// Return true if P preserves high level analysis used by other
837// passes managed by this manager
838bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000839 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000840 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000841 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000842
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000843 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000844 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000845 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000846 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000847 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000848 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000849
Devang Patel9d9fc902007-03-06 17:52:53 +0000850 return true;
851}
852
Chris Lattner02eb94c2008-08-07 07:34:50 +0000853/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000854void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000855 // Don't do this unless assertions are enabled.
856#ifdef NDEBUG
857 return;
858#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000859 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
860 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000861
Devang Patelef432532007-07-19 05:36:09 +0000862 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000863 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000864 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000865 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000866 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000867 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000868 }
869}
870
Devang Patel67c79a42008-07-01 19:50:56 +0000871/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000872void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000873 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
874 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000875 return;
876
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000877 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000878 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000879 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000880 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000881 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000882 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000883 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000884 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000885 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000886 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
887 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000888 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000889 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000890 }
Devang Patel349170f2006-11-11 01:24:55 +0000891 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000892
Devang Patel42dd1e92007-03-06 01:55:46 +0000893 // Check inherited analysis also. If P is not preserving analysis
894 // provided by parent manager then remove it here.
895 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
896
897 if (!InheritedAnalysis[Index])
898 continue;
899
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000900 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000901 I = InheritedAnalysis[Index]->begin(),
902 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000903 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000904 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000905 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000906 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000907 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000908 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000909 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
910 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000911 }
Devang Patel01919d22007-03-08 19:05:01 +0000912 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000913 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000914 }
915 }
Devang Patelf68a3492006-11-07 22:35:17 +0000916}
917
Devang Patelca189262006-11-14 03:05:08 +0000918/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000919void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000920 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000921
Devang Patel8adae862007-07-20 18:04:54 +0000922 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000923
Devang Patel2ff44922007-04-16 20:39:59 +0000924 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000925 if (!TPM)
926 return;
927
Devang Patel17ad0962006-12-08 00:37:52 +0000928 TPM->collectLastUses(DeadPasses, P);
929
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000930 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000931 dbgs() << " -*- '" << P->getPassName();
932 dbgs() << "' is the last user of following pass instances.";
933 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000934 }
935
Yaron Kerenbd873192016-10-02 19:21:41 +0000936 for (Pass *P : DeadPasses)
937 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000938}
Devang Patel200d3052006-12-13 23:50:44 +0000939
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000940void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000941 enum PassDebuggingString DBG_STR) {
942 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000943
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000944 {
945 // If the pass crashes releasing memory, remember this.
946 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000947 TimeRegion PassTimer(getPassTimer(P));
948
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000949 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000950 }
951
Owen Andersona7aed182010-08-06 18:33:48 +0000952 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000953 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000954 // Remove the pass itself (if it is not already removed).
955 AvailableAnalysis.erase(PI);
956
957 // Remove all interfaces this pass implements, for which it is also
958 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000959 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000960 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000961 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000962 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000963 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000964 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000965 }
Devang Patel17ad0962006-12-08 00:37:52 +0000966 }
Devang Patelca189262006-11-14 03:05:08 +0000967}
968
Dan Gohmande6188a2010-08-12 23:50:08 +0000969/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000970/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000971void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000972 // This manager is going to manage pass P. Set up analysis resolver
973 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000974 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000975 P->setResolver(AR);
976
Devang Patelec2b9a72007-03-05 22:57:49 +0000977 // If a FunctionPass F is the last user of ModulePass info M
978 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000979 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000980
Chris Lattner60987362009-03-06 05:53:14 +0000981 if (!ProcessAnalysis) {
982 // Add pass
983 PassVector.push_back(P);
984 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000985 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000986
Chris Lattner60987362009-03-06 05:53:14 +0000987 // At the moment, this pass is the last user of all required passes.
988 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000989 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000990 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
991
992 unsigned PDepth = this->getDepth();
993
Chandler Carruth44a13852015-08-19 03:02:12 +0000994 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
995 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000996 unsigned RDepth = 0;
997
Chandler Carruth44a13852015-08-19 03:02:12 +0000998 assert(PUsed->getResolver() && "Analysis Resolver is not set");
999 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001000 RDepth = DM.getDepth();
1001
1002 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001003 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001004 else if (PDepth > RDepth) {
1005 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001006 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001007 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001008 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001009 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001010 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001011 }
1012
1013 // Set P as P's last user until someone starts using P.
1014 // However, if P is a Pass Manager then it does not need
1015 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001016 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001017 LastUses.push_back(P);
1018 TPM->setLastUser(LastUses, P);
1019
1020 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001021 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001022 TPM->setLastUser(TransferLastUses, My_PM);
1023 TransferLastUses.clear();
1024 }
1025
Dan Gohman6304db32010-08-16 22:57:28 +00001026 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001027 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1028 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001029 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001030 this->addLowerLevelRequiredPass(P, AnalysisPass);
1031 }
1032
1033 // Take a note of analysis required and made available by this pass.
1034 // Remove the analysis not preserved by this pass
1035 removeNotPreservedAnalysis(P);
1036 recordAvailableAnalysis(P);
1037
Devang Patel8cad70d2006-11-11 01:51:02 +00001038 // Add pass
1039 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001040}
1041
Devang Patele64d3052007-04-16 20:12:57 +00001042
Chandler Carruth44a13852015-08-19 03:02:12 +00001043/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001044/// pass P and are available. Populate RP_NotAvail with analysis
1045/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001046void PMDataManager::collectRequiredAndUsedAnalyses(
1047 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1048 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001049 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001050
1051 for (const auto &UsedID : AnUsage->getUsedSet())
1052 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1053 UP.push_back(AnalysisPass);
1054
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001055 for (const auto &RequiredID : AnUsage->getRequiredSet())
1056 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001057 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001058 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001059 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001060
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001061 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1062 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001063 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001064 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001065 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001066}
1067
Devang Patel07f4f582006-11-14 21:49:36 +00001068// All Required analyses should be available to the pass as it runs! Here
1069// we fill in the AnalysisImpls member of the pass so that it can
1070// successfully use the getAnalysis() method to retrieve the
1071// implementations it needs.
1072//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001073void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001074 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1075
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001076 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1077 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001078 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001079 // This may be analysis pass that is initialized on the fly.
1080 // If that is not the case then it will raise an assert when it is used.
1081 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001082 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001083 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001084 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001085 }
1086}
1087
Devang Patel640c5bb2006-12-08 22:30:11 +00001088/// Find the pass that implements Analysis AID. If desired pass is not found
1089/// then return NULL.
1090Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1091
1092 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001093 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001094
1095 if (I != AvailableAnalysis.end())
1096 return I->second;
1097
1098 // Search Parents through TopLevelManager
1099 if (SearchParent)
1100 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001101
Craig Topperc6207612014-04-09 06:08:46 +00001102 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001103}
1104
Devang Patel991aeba2006-12-15 20:13:01 +00001105// Print list of passes that are last used by P.
1106void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1107
Devang Patel8adae862007-07-20 18:04:54 +00001108 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001109
1110 // If this is a on the fly manager then it does not have TPM.
1111 if (!TPM)
1112 return;
1113
Devang Patel991aeba2006-12-15 20:13:01 +00001114 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001115
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001116 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001117 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001118 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001119 }
1120}
1121
1122void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001123 for (Pass *P : PassVector) {
1124 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001125 PMD->dumpPassArguments();
1126 else
Owen Andersona7aed182010-08-06 18:33:48 +00001127 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001128 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001129 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001130 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001131 }
1132}
1133
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001134void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1135 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001136 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001137 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001138 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001139 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001140 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001141 switch (S1) {
1142 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
1145 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
1148 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 default:
1152 break;
1153 }
1154 switch (S2) {
1155 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
1158 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001164 case ON_REGION_MSG:
1165 dbgs() << "' on Region '" << Msg << "'...\n";
1166 break;
Devang Patel003a5592007-03-05 20:01:30 +00001167 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001168 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001169 break;
1170 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001171 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001172 break;
1173 default:
1174 break;
1175 }
Devang Patel991aeba2006-12-15 20:13:01 +00001176}
1177
Chris Lattner4c1e9542009-03-06 06:45:05 +00001178void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001179 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001180 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001181
Chris Lattner4c493d92008-08-08 15:14:09 +00001182 AnalysisUsage analysisUsage;
1183 P->getAnalysisUsage(analysisUsage);
1184 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1185}
1186
Chris Lattner4c1e9542009-03-06 06:45:05 +00001187void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001188 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001189 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001190
Chris Lattner4c493d92008-08-08 15:14:09 +00001191 AnalysisUsage analysisUsage;
1192 P->getAnalysisUsage(analysisUsage);
1193 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1194}
1195
Chandler Carruth44a13852015-08-19 03:02:12 +00001196void PMDataManager::dumpUsedSet(const Pass *P) const {
1197 if (PassDebugging < Details)
1198 return;
1199
1200 AnalysisUsage analysisUsage;
1201 P->getAnalysisUsage(analysisUsage);
1202 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1203}
1204
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001205void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001206 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001207 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001208 if (Set.empty())
1209 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001210 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001211 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001212 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001213 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001214 if (!PInf) {
1215 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1216 // all drivers.
1217 dbgs() << " Uninitialized Pass";
1218 continue;
1219 }
Owen Andersona7aed182010-08-06 18:33:48 +00001220 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001221 }
David Greene994e1bb2010-01-05 01:30:02 +00001222 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001223}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001224
Devang Patel004937b2007-07-27 20:06:09 +00001225/// Add RequiredPass into list of lower level passes required by pass P.
1226/// RequiredPass is run on the fly by Pass Manager when P requests it
1227/// through getAnalysis interface.
1228/// This should be handled by specific pass manager.
1229void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1230 if (TPM) {
1231 TPM->dumpArguments();
1232 TPM->dumpPasses();
1233 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001234
Dan Gohmande6188a2010-08-12 23:50:08 +00001235 // Module Level pass may required Function Level analysis info
1236 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1237 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001238 // module level pass is requiring lower level analysis info managed by
1239 // lower level pass manager.
1240
1241 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001242 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001243 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001244#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001245 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1246 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001247#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001248 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001249}
1250
Owen Andersona7aed182010-08-06 18:33:48 +00001251Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001252 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001253}
1254
Devang Patele7599552007-01-12 18:52:44 +00001255// Destructor
1256PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001257 for (Pass *P : PassVector)
1258 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001259}
1260
Devang Patel9bdf7d42006-12-08 23:28:54 +00001261//===----------------------------------------------------------------------===//
1262// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001263// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1264Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001265 return PM.findAnalysisPass(ID, dir);
1266}
1267
Dan Gohmande6188a2010-08-12 23:50:08 +00001268Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001269 Function &F) {
1270 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1271}
1272
Devang Patela1514cb2006-12-07 19:39:39 +00001273//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001274// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001275
Dan Gohmande6188a2010-08-12 23:50:08 +00001276/// Execute all of the passes scheduled for execution by invoking
1277/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001278/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001279bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001280 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001281 return false;
1282
Devang Patele9585592006-12-08 01:38:28 +00001283 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001284 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001285
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001286 unsigned InstrCount, BBSize = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001287 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001288 if (EmitICRemark)
1289 InstrCount = initSizeRemarkInfo(M);
1290
1291 for (BasicBlock &BB : F) {
1292 // Collect the initial size of the basic block.
1293 if (EmitICRemark)
1294 BBSize = BB.size();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001295 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1296 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001297 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001298
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001299 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001300 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001301
Devang Patelabfbe3b2006-12-16 00:56:26 +00001302 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001303
Chris Lattner4c1e9542009-03-06 06:45:05 +00001304 {
1305 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001306 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001307 TimeRegion PassTimer(getPassTimer(BP));
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001308 LocalChanged |= BP->runOnBasicBlock(BB);
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001309 if (EmitICRemark) {
1310 unsigned NewSize = BB.size();
1311 // Update the size of the basic block, emit a remark, and update the
1312 // size of the module.
1313 if (NewSize != BBSize) {
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001314 int64_t Delta =
1315 static_cast<int64_t>(NewSize) - static_cast<int64_t>(BBSize);
Jessica Paquette71e97782018-08-31 20:54:37 +00001316 emitInstrCountChangedRemark(BP, M, Delta, InstrCount, &F);
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001317 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1318 BBSize = NewSize;
1319 }
1320 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001321 }
Devang Patel93a197c2006-12-14 00:08:04 +00001322
Dan Gohman74b189f2010-03-01 17:34:28 +00001323 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001324 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001325 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001326 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001327 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001328 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001329
Devang Patela273d1c2007-07-19 18:02:32 +00001330 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001331 removeNotPreservedAnalysis(BP);
1332 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001333 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001334 }
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001335 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001336
Bill Wendling6ce6d262009-12-25 13:50:18 +00001337 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001338}
1339
Devang Patel475c4532006-12-08 00:59:05 +00001340// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001341bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001342 bool Changed = false;
1343
Chris Lattner4c1e9542009-03-06 06:45:05 +00001344 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1345 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001346
1347 return Changed;
1348}
1349
Duncan Sands51495602009-02-13 09:42:34 +00001350bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001351 bool Changed = false;
1352
Pedro Artigas41b98842012-12-05 17:12:22 +00001353 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001354 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001355
1356 return Changed;
1357}
1358
Duncan Sands51495602009-02-13 09:42:34 +00001359bool BBPassManager::doInitialization(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->doInitialization(F);
1365 }
1366
1367 return Changed;
1368}
1369
Duncan Sands51495602009-02-13 09:42:34 +00001370bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001371 bool Changed = false;
1372
Devang Patelabfbe3b2006-12-16 00:56:26 +00001373 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1374 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001375 Changed |= BP->doFinalization(F);
1376 }
1377
1378 return Changed;
1379}
1380
1381
Devang Patela1514cb2006-12-07 19:39:39 +00001382//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001383// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001384
Devang Patel4e12f862006-11-08 10:44:40 +00001385/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001386FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001387 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001388 // FPM is the top level manager.
1389 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001390
Dan Gohman565df952008-03-13 02:08:36 +00001391 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001392 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001393}
1394
Devang Patelb67904d2006-12-13 02:36:01 +00001395FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001396 delete FPM;
1397}
1398
Dan Gohmande6188a2010-08-12 23:50:08 +00001399void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001400 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001401}
1402
Devang Patel9f3083e2006-11-15 19:39:54 +00001403/// run - Execute all of the passes scheduled for execution. Keep
1404/// track of whether any of the passes modifies the function, and if
1405/// so, return true.
1406///
Devang Patelb67904d2006-12-13 02:36:01 +00001407bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001408 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1409 report_fatal_error("Error reading bitcode file: " + EIB.message());
1410 });
Devang Patel272908d2006-12-08 22:57:48 +00001411 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001412}
1413
1414
Devang Patelff631ae2006-11-15 01:27:05 +00001415/// doInitialization - Run all of the initializers for the function passes.
1416///
Devang Patelb67904d2006-12-13 02:36:01 +00001417bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001418 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001419}
1420
Dan Gohmane6656eb2007-07-30 14:51:13 +00001421/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001422///
Devang Patelb67904d2006-12-13 02:36:01 +00001423bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001424 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001425}
1426
Devang Patela1514cb2006-12-07 19:39:39 +00001427//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428// FunctionPassManagerImpl implementation
1429//
Duncan Sands51495602009-02-13 09:42:34 +00001430bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001431 bool Changed = false;
1432
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001433 dumpArguments();
1434 dumpPasses();
1435
Yaron Keren4849aa32015-06-05 17:48:47 +00001436 for (ImmutablePass *ImPass : getImmutablePasses())
1437 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001438
Chris Lattner4c1e9542009-03-06 06:45:05 +00001439 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1440 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001441
1442 return Changed;
1443}
1444
Duncan Sands51495602009-02-13 09:42:34 +00001445bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001446 bool Changed = false;
1447
Pedro Artigas41b98842012-12-05 17:12:22 +00001448 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001449 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001450
Yaron Keren4849aa32015-06-05 17:48:47 +00001451 for (ImmutablePass *ImPass : getImmutablePasses())
1452 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001453
Devang Patel67d6a5e2006-12-19 19:46:59 +00001454 return Changed;
1455}
1456
Devang Patelec9c58f2009-04-01 22:34:41 +00001457/// cleanup - After running all passes, clean up pass manager cache.
1458void FPPassManager::cleanup() {
1459 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1460 FunctionPass *FP = getContainedPass(Index);
1461 AnalysisResolver *AR = FP->getResolver();
1462 assert(AR && "Analysis Resolver is not set");
1463 AR->clearAnalysisImpls();
1464 }
1465}
1466
Torok Edwin24c78352009-06-29 18:49:09 +00001467void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1468 if (!wasRun)
1469 return;
1470 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1471 FPPassManager *FPPM = getContainedManager(Index);
1472 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1473 FPPM->getContainedPass(Index)->releaseMemory();
1474 }
1475 }
Torok Edwin896556e2009-06-29 21:05:10 +00001476 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001477}
1478
Devang Patel67d6a5e2006-12-19 19:46:59 +00001479// Execute all the passes managed by this top level manager.
1480// Return true if any function is modified by a pass.
1481bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001482 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483
Devang Patele3068402006-12-21 00:16:50 +00001484 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001485 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001486 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001487 F.getContext().yield();
1488 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001489
1490 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1491 getContainedManager(Index)->cleanup();
1492
Torok Edwin24c78352009-06-29 18:49:09 +00001493 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001494 return Changed;
1495}
1496
1497//===----------------------------------------------------------------------===//
1498// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001499
Devang Patel8c78a0b2007-05-03 01:11:54 +00001500char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001501/// Print passes managed by this manager
1502void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001503 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001504 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1505 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001506 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001507 dumpLastUses(FP, Offset+1);
1508 }
1509}
1510
1511
Dan Gohmande6188a2010-08-12 23:50:08 +00001512/// Execute all of the passes scheduled for execution by invoking
1513/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001514/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001516 if (F.isDeclaration())
1517 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001518
1519 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001520 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001521 // Collect inherited analysis from Module level pass manager.
1522 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001523
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001524 unsigned InstrCount, FunctionSize = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001525 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001526 // Collect the initial size of the module.
1527 if (EmitICRemark) {
1528 InstrCount = initSizeRemarkInfo(M);
1529 FunctionSize = F.getInstructionCount();
1530 }
1531
Devang Patelabfbe3b2006-12-16 00:56:26 +00001532 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1533 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001534 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001535
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001536 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001537 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001538
Devang Patelabfbe3b2006-12-16 00:56:26 +00001539 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001540
Chris Lattner4c1e9542009-03-06 06:45:05 +00001541 {
1542 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001543 TimeRegion PassTimer(getPassTimer(FP));
Dan Gohman74b189f2010-03-01 17:34:28 +00001544 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001545 if (EmitICRemark) {
1546 unsigned NewSize = F.getInstructionCount();
1547
1548 // Update the size of the function, emit a remark, and update the size
1549 // of the module.
1550 if (NewSize != FunctionSize) {
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001551 int64_t Delta = static_cast<int64_t>(NewSize) -
1552 static_cast<int64_t>(FunctionSize);
Jessica Paquette71e97782018-08-31 20:54:37 +00001553 emitInstrCountChangedRemark(FP, M, Delta, InstrCount, &F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001554 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1555 FunctionSize = NewSize;
1556 }
1557 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001558 }
Devang Patel93a197c2006-12-14 00:08:04 +00001559
Dan Gohman74b189f2010-03-01 17:34:28 +00001560 Changed |= LocalChanged;
1561 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001562 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001563 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001564 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001565
Devang Patela273d1c2007-07-19 18:02:32 +00001566 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001567 removeNotPreservedAnalysis(FP);
1568 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001569 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001570 }
1571 return Changed;
1572}
1573
Devang Patel67d6a5e2006-12-19 19:46:59 +00001574bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001575 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001576
Serge Pavloved5eb932017-01-15 10:23:18 +00001577 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001578 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001579
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001580 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001581}
1582
Duncan Sands51495602009-02-13 09:42:34 +00001583bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001584 bool Changed = false;
1585
Chris Lattner4c1e9542009-03-06 06:45:05 +00001586 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1587 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001588
Devang Patelff631ae2006-11-15 01:27:05 +00001589 return Changed;
1590}
1591
Duncan Sands51495602009-02-13 09:42:34 +00001592bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001593 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001594
1595 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001596 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001597
Devang Patelff631ae2006-11-15 01:27:05 +00001598 return Changed;
1599}
1600
Devang Patela1514cb2006-12-07 19:39:39 +00001601//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001602// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001603
Dan Gohmande6188a2010-08-12 23:50:08 +00001604/// Execute all of the passes scheduled for execution by invoking
1605/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001606/// the module, and if so, return true.
1607bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001608MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001609 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001610
Torok Edwin24c78352009-06-29 18:49:09 +00001611 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001612 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1613 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001614 Changed |= FPP->doInitialization(M);
1615 }
1616
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001617 // Initialize module passes
1618 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1619 Changed |= getContainedPass(Index)->doInitialization(M);
1620
Jessica Paquette454d1032018-08-31 20:20:55 +00001621 unsigned InstrCount, ModuleCount = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001622 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquette454d1032018-08-31 20:20:55 +00001623 // Collect the initial size of the module.
1624 if (EmitICRemark) {
1625 InstrCount = initSizeRemarkInfo(M);
1626 ModuleCount = InstrCount;
1627 }
1628
Devang Patelabfbe3b2006-12-16 00:56:26 +00001629 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1630 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001631 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001632
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001633 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001634 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001635
Devang Patelabfbe3b2006-12-16 00:56:26 +00001636 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001637
Chris Lattner4c1e9542009-03-06 06:45:05 +00001638 {
1639 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001640 TimeRegion PassTimer(getPassTimer(MP));
1641
Dan Gohman74b189f2010-03-01 17:34:28 +00001642 LocalChanged |= MP->runOnModule(M);
Jessica Paquette454d1032018-08-31 20:20:55 +00001643 if (EmitICRemark) {
1644 // Update the size of the module.
Jessica Paquette454d1032018-08-31 20:20:55 +00001645 ModuleCount = M.getInstructionCount();
1646 if (ModuleCount != InstrCount) {
Jessica Paquette9a23c552018-08-31 20:20:57 +00001647 int64_t Delta = static_cast<int64_t>(ModuleCount) -
1648 static_cast<int64_t>(InstrCount);
1649 emitInstrCountChangedRemark(MP, M, Delta, InstrCount);
Jessica Paquettea69696d2018-08-31 22:43:41 +00001650 InstrCount = ModuleCount;
Jessica Paquette454d1032018-08-31 20:20:55 +00001651 }
1652 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001653 }
Devang Patel93a197c2006-12-14 00:08:04 +00001654
Dan Gohman74b189f2010-03-01 17:34:28 +00001655 Changed |= LocalChanged;
1656 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001657 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001658 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001659 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001660 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001661
Devang Patela273d1c2007-07-19 18:02:32 +00001662 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001663 removeNotPreservedAnalysis(MP);
1664 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001665 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001666 }
Torok Edwin24c78352009-06-29 18:49:09 +00001667
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001668 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001669 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001670 Changed |= getContainedPass(Index)->doFinalization(M);
1671
Torok Edwin24c78352009-06-29 18:49:09 +00001672 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001673 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1674 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001675 // We don't know when is the last time an on-the-fly pass is run,
1676 // so we need to releaseMemory / finalize here
1677 FPP->releaseMemoryOnTheFly();
1678 Changed |= FPP->doFinalization(M);
1679 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001680
Devang Patel05e1a972006-11-07 22:03:15 +00001681 return Changed;
1682}
1683
Devang Patele64d3052007-04-16 20:12:57 +00001684/// Add RequiredPass into list of lower level passes required by pass P.
1685/// RequiredPass is run on the fly by Pass Manager when P requests it
1686/// through getAnalysis interface.
1687void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001688 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1689 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001690 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001691 RequiredPass->getPotentialPassManagerType()) &&
1692 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001693 if (!RequiredPass)
1694 return;
Devang Patele64d3052007-04-16 20:12:57 +00001695
Devang Patel68f72b12007-04-26 17:50:19 +00001696 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001697 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001698 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001699 // FPP is the top level manager.
1700 FPP->setTopLevelManager(FPP);
1701
Devang Patel69e9f6d2007-04-16 20:27:05 +00001702 OnTheFlyManagers[P] = FPP;
1703 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001704 const PassInfo *RequiredPassPI =
1705 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001706
Craig Topperc6207612014-04-09 06:08:46 +00001707 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001708 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1709 FoundPass =
1710 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001711 }
Andrew Trick02066f22014-04-08 03:40:34 +00001712 if (!FoundPass) {
1713 FoundPass = RequiredPass;
1714 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001715 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001716 FPP->add(RequiredPass);
1717 }
1718 // Register P as the last user of FoundPass or RequiredPass.
1719 SmallVector<Pass *, 1> LU;
1720 LU.push_back(FoundPass);
1721 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001722}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001723
Dan Gohmande6188a2010-08-12 23:50:08 +00001724/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001725/// required by module pass MP. Instantiate analysis pass, by using
1726/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001727Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001728 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001729 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001730
Torok Edwin24c78352009-06-29 18:49:09 +00001731 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001732 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001733 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001734}
1735
1736
Devang Patela1514cb2006-12-07 19:39:39 +00001737//===----------------------------------------------------------------------===//
1738// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001739
Devang Patelab97cf42006-12-13 00:09:23 +00001740//
Devang Patelc290c8a2006-11-07 22:23:34 +00001741/// run - Execute all of the passes scheduled for execution. Keep track of
1742/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001743bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001744 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001745
Devang Patelcfd70c42006-12-13 22:10:00 +00001746 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001747 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001748
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001749 for (ImmutablePass *ImPass : getImmutablePasses())
1750 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001751
Devang Patele3068402006-12-21 00:16:50 +00001752 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001753 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001754 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001755 M.getContext().yield();
1756 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001757
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001758 for (ImmutablePass *ImPass : getImmutablePasses())
1759 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001760
Devang Patelc290c8a2006-11-07 22:23:34 +00001761 return Changed;
1762}
Devang Patel376fefa2006-11-08 10:29:57 +00001763
Devang Patela1514cb2006-12-07 19:39:39 +00001764//===----------------------------------------------------------------------===//
1765// PassManager implementation
1766
Devang Patel376fefa2006-11-08 10:29:57 +00001767/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001768PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001769 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001770 // PM is the top level manager
1771 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001772}
1773
Devang Patelb67904d2006-12-13 02:36:01 +00001774PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001775 delete PM;
1776}
1777
Chris Lattner60987362009-03-06 05:53:14 +00001778void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001779 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001780}
1781
1782/// run - Execute all of the passes scheduled for execution. Keep track of
1783/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001784bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001785 return PM->run(M);
1786}
1787
Devang Patelb8817b92006-12-14 00:59:42 +00001788//===----------------------------------------------------------------------===//
Devang Patel1c56a632007-01-08 19:29:38 +00001789// PMStack implementation
1790//
Devang Patelad98d232007-01-11 22:15:30 +00001791
Devang Patel1c56a632007-01-08 19:29:38 +00001792// Pop Pass Manager from the stack and clear its analysis info.
1793void PMStack::pop() {
1794
1795 PMDataManager *Top = this->top();
1796 Top->initializeAnalysisInfo();
1797
1798 S.pop_back();
1799}
1800
1801// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001802void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001803 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001804 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001805
Chris Lattner60987362009-03-06 05:53:14 +00001806 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001807 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1808 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001809 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001810
Chris Lattner60987362009-03-06 05:53:14 +00001811 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001812 TPM->addIndirectPassManager(PM);
1813 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001814 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001815 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001816 assert((PM->getPassManagerType() == PMT_ModulePassManager
1817 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001818 && "pushing bad pass manager to PMStack");
1819 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001820 }
1821
Devang Patel15701b52007-01-11 00:19:00 +00001822 S.push_back(PM);
1823}
1824
1825// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001826LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001827 for (PMDataManager *Manager : S)
1828 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001829
Devang Patel15701b52007-01-11 00:19:00 +00001830 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001831 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001832}
1833
Devang Patel1c56a632007-01-08 19:29:38 +00001834/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001835/// add self into that manager.
1836void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001837 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001838 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001839 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001840 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1841 if (TopPMType == PreferredType)
1842 break; // We found desired pass manager
1843 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001844 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001845 else
1846 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001847 }
Devang Patel18ff6362008-09-09 21:38:40 +00001848 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001849 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001850}
1851
Devang Patel3312f752007-01-16 21:43:18 +00001852/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001853/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001854void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001855 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001856
Andrew Trickcbc845f2012-02-01 07:16:20 +00001857 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001858 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001859 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1860 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001861 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001862 break;
Devang Patel3312f752007-01-16 21:43:18 +00001863 }
Devang Patel3312f752007-01-16 21:43:18 +00001864
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001865 // Create new Function Pass Manager if needed.
1866 FPPassManager *FPP;
1867 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1868 FPP = (FPPassManager *)PMS.top();
1869 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001870 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1871 PMDataManager *PMD = PMS.top();
1872
1873 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001874 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001875 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001876
1877 // [2] Set up new manager's top level manager
1878 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1879 TPM->addIndirectPassManager(FPP);
1880
1881 // [3] Assign manager to manage this new manager. This may create
1882 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001883 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001884
1885 // [4] Push new manager into PMS
1886 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001887 }
1888
Devang Patel3312f752007-01-16 21:43:18 +00001889 // Assign FPP as the manager of this pass.
1890 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001891}
1892
Devang Patel3312f752007-01-16 21:43:18 +00001893/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001894/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001895void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001896 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001897 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001898
Devang Patel15701b52007-01-11 00:19:00 +00001899 // Basic Pass Manager is a leaf pass manager. It does not handle
1900 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001901 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001902 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1903 BBP = (BBPassManager *)PMS.top();
1904 } else {
1905 // If leaf manager is not Basic Block Pass manager then create new
1906 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001907 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1908 PMDataManager *PMD = PMS.top();
1909
1910 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001911 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001912
1913 // [2] Set up new manager's top level manager
1914 // Basic Block Pass Manager does not live by itself
1915 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1916 TPM->addIndirectPassManager(BBP);
1917
Devang Patel15701b52007-01-11 00:19:00 +00001918 // [3] Assign manager to manage this new manager. This may create
1919 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001920 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001921
Devang Patel3312f752007-01-16 21:43:18 +00001922 // [4] Push new manager into PMS
1923 PMS.push(BBP);
1924 }
Devang Patel1c56a632007-01-08 19:29:38 +00001925
Devang Patel3312f752007-01-16 21:43:18 +00001926 // Assign BBP as the manager of this pass.
1927 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001928}
1929
Dan Gohmand3a20c92008-03-11 16:41:42 +00001930PassManagerBase::~PassManagerBase() {}