blob: a413f6ed9253b4dace127545d26469f830ef056e [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 Paquette71e97782018-08-31 20:54:37 +0000155 // Do we have a function we can use to emit a remark?
156 if (F == nullptr) {
157 // We need a function containing at least one basic block in order to output
158 // remarks. Since it's possible that the first function in the module
159 // doesn't actually contain a basic block, we have to go and find one that's
160 // suitable for emitting remarks.
161 auto It = std::find_if(M.begin(), M.end(),
162 [](const Function &Fn) { return !Fn.empty(); });
Jessica Paquettee49374d2018-05-18 17:26:39 +0000163
Jessica Paquette71e97782018-08-31 20:54:37 +0000164 // Didn't find a function. Quit.
165 if (It == M.end())
166 return;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000167
Jessica Paquette71e97782018-08-31 20:54:37 +0000168 // We found a function containing at least one basic block.
169 F = &*It;
170 }
Jessica Paquette9a23c552018-08-31 20:20:57 +0000171 int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
Jessica Paquettee49374d2018-05-18 17:26:39 +0000172 BasicBlock &BB = *F->begin();
173 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
174 DiagnosticLocation(), &BB);
175 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
176 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
177 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
178 << ": IR instruction count changed from "
179 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
180 << " to "
181 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
182 << "; Delta: "
183 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
184 F->getContext().diagnose(R); // Not using ORE for layering reasons.
185}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000186
Chris Lattner4c1e9542009-03-06 06:45:05 +0000187void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000188 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000189 OS << "Releasing pass '";
190 else
191 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000192
Chris Lattner4c1e9542009-03-06 06:45:05 +0000193 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000194
Chris Lattner4c1e9542009-03-06 06:45:05 +0000195 if (M) {
196 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
197 return;
198 }
Craig Topperc6207612014-04-09 06:08:46 +0000199 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000200 OS << '\n';
201 return;
202 }
203
Dan Gohman79fc0e92009-03-10 18:47:59 +0000204 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000205 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000206 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000207 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000208 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000209 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000210 OS << "value";
211
212 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000213 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000214 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000215}
216
217
Devang Patelffca9102006-12-15 19:39:30 +0000218namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000219//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000221//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000222/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000223/// pass together and sequence them to process one basic block before
224/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000225class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000226
227public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000228 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000229 explicit BBPassManager()
230 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000231
Devang Patelca58e352006-11-08 10:05:38 +0000232 /// Execute all of the passes scheduled for execution. Keep track of
233 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000234 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000235
Devang Patelf9d96b92006-12-07 19:57:52 +0000236 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000237 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000238 Info.setPreservesAll();
239 }
240
Craig Topperf398d7c2014-03-05 06:35:38 +0000241 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000242 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000243 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000244 bool doFinalization(Function &F);
245
Craig Topperf398d7c2014-03-05 06:35:38 +0000246 PMDataManager *getAsPMDataManager() override { return this; }
247 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000248
Mehdi Amini117296c2016-10-01 02:56:57 +0000249 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000250
Devang Pateleda56172006-12-12 23:34:33 +0000251 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000252 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000253 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000254 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
255 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000256 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000257 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000258 }
259 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000260
261 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000262 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000263 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
264 return BP;
265 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000266
Craig Topperf398d7c2014-03-05 06:35:38 +0000267 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000268 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000269 }
Devang Patelca58e352006-11-08 10:05:38 +0000270};
271
Devang Patel8c78a0b2007-05-03 01:11:54 +0000272char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000273} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274
Devang Patele7599552007-01-12 18:52:44 +0000275namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000276namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000277//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000278// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000279//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280/// FunctionPassManagerImpl manages FPPassManagers
281class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000282 public PMDataManager,
283 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000284 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000285private:
286 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000287public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000288 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000289 explicit FunctionPassManagerImpl() :
290 Pass(PT_PassManager, ID), PMDataManager(),
291 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000292
Matthias Braun0880c602014-12-12 01:27:01 +0000293 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000294 void add(Pass *P) {
295 schedulePass(P);
296 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000297
298 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000299 Pass *createPrinterPass(raw_ostream &O,
300 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000301 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000302 }
303
Torok Edwin24c78352009-06-29 18:49:09 +0000304 // Prepare for running an on the fly pass, freeing memory if needed
305 // from a previous run.
306 void releaseMemoryOnTheFly();
307
Devang Patel67d6a5e2006-12-19 19:46:59 +0000308 /// run - Execute all of the passes scheduled for execution. Keep track of
309 /// whether any of the passes modifies the module, and if so, return true.
310 bool run(Function &F);
311
312 /// doInitialization - Run all of the initializers for the function passes.
313 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000314 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000315
Dan Gohmane6656eb2007-07-30 14:51:13 +0000316 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000317 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000318 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000319
Dan Gohmande6188a2010-08-12 23:50:08 +0000320
Craig Topperf398d7c2014-03-05 06:35:38 +0000321 PMDataManager *getAsPMDataManager() override { return this; }
322 Pass *getAsPass() override { return this; }
323 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000324 return PMT_FunctionPassManager;
325 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000326
Devang Patel67d6a5e2006-12-19 19:46:59 +0000327 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000328 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000329 Info.setPreservesAll();
330 }
331
Devang Patel67d6a5e2006-12-19 19:46:59 +0000332 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000333 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000334 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
335 return FP;
336 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000337};
338
David Blaikiea379b1812011-12-20 02:50:00 +0000339void FunctionPassManagerImpl::anchor() {}
340
Devang Patel8c78a0b2007-05-03 01:11:54 +0000341char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000342} // End of legacy namespace
343} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000344
Chandler Carruth7caea412013-11-09 12:26:54 +0000345namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000346//===----------------------------------------------------------------------===//
347// MPPassManager
348//
349/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000350/// It batches all Module passes and function pass managers together and
351/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000352class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000353public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000354 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000355 explicit MPPassManager() :
356 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000357
358 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000359 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000360 for (auto &OnTheFlyManager : OnTheFlyManagers) {
361 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000362 delete FPP;
363 }
364 }
365
Dan Gohmande6188a2010-08-12 23:50:08 +0000366 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000367 Pass *createPrinterPass(raw_ostream &O,
368 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000369 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000370 }
371
Devang Patelca58e352006-11-08 10:05:38 +0000372 /// run - Execute all of the passes scheduled for execution. Keep track of
373 /// whether any of the passes modifies the module, and if so, return true.
374 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000375
Pedro Artigase4348b02012-12-03 21:56:57 +0000376 using llvm::Pass::doInitialization;
377 using llvm::Pass::doFinalization;
378
Devang Patelf9d96b92006-12-07 19:57:52 +0000379 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000380 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000381 Info.setPreservesAll();
382 }
383
Devang Patele64d3052007-04-16 20:12:57 +0000384 /// Add RequiredPass into list of lower level passes required by pass P.
385 /// RequiredPass is run on the fly by Pass Manager when P requests it
386 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000387 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000388
Dan Gohmande6188a2010-08-12 23:50:08 +0000389 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000390 /// required by module pass MP. Instantiate analysis pass, by using
391 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000392 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000393
Mehdi Amini117296c2016-10-01 02:56:57 +0000394 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000395
Craig Topperf398d7c2014-03-05 06:35:38 +0000396 PMDataManager *getAsPMDataManager() override { return this; }
397 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000398
Devang Pateleda56172006-12-12 23:34:33 +0000399 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000400 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000401 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000402 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
403 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000404 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000405 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
406 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000407 if (I != OnTheFlyManagers.end())
408 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000409 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000410 }
411 }
412
Devang Patelabfbe3b2006-12-16 00:56:26 +0000413 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000414 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000415 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000416 }
417
Craig Topperf398d7c2014-03-05 06:35:38 +0000418 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000419 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000420 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000421
422 private:
423 /// Collection of on the fly FPPassManagers. These managers manage
424 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000425 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000426};
427
Devang Patel8c78a0b2007-05-03 01:11:54 +0000428char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000429} // End anonymous namespace
430
431namespace llvm {
432namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000433//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000434// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000435//
Devang Patel09f162c2007-05-01 21:15:47 +0000436
Devang Patel67d6a5e2006-12-19 19:46:59 +0000437/// PassManagerImpl manages MPPassManagers
438class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000439 public PMDataManager,
440 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000441 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000442
443public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000444 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000445 explicit PassManagerImpl() :
446 Pass(PT_PassManager, ID), PMDataManager(),
447 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000448
Matthias Braun0880c602014-12-12 01:27:01 +0000449 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000450 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000451 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000452 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000453
454 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000455 Pass *createPrinterPass(raw_ostream &O,
456 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000457 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000458 }
459
Devang Patel376fefa2006-11-08 10:29:57 +0000460 /// run - Execute all of the passes scheduled for execution. Keep track of
461 /// whether any of the passes modifies the module, and if so, return true.
462 bool run(Module &M);
463
Pedro Artigase4348b02012-12-03 21:56:57 +0000464 using llvm::Pass::doInitialization;
465 using llvm::Pass::doFinalization;
466
Devang Patelf9d96b92006-12-07 19:57:52 +0000467 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000468 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000469 Info.setPreservesAll();
470 }
471
Craig Topperf398d7c2014-03-05 06:35:38 +0000472 PMDataManager *getAsPMDataManager() override { return this; }
473 Pass *getAsPass() override { return this; }
474 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000475 return PMT_ModulePassManager;
476 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000477
Devang Patel67d6a5e2006-12-19 19:46:59 +0000478 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000479 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000480 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
481 return MP;
482 }
Devang Patel376fefa2006-11-08 10:29:57 +0000483};
484
David Blaikiea379b1812011-12-20 02:50:00 +0000485void PassManagerImpl::anchor() {}
486
Devang Patel8c78a0b2007-05-03 01:11:54 +0000487char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000488} // End of legacy namespace
489} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000490
Devang Patela1514cb2006-12-07 19:39:39 +0000491//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000492// PMTopLevelManager implementation
493
Devang Patel4268fc02007-01-16 02:00:38 +0000494/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000495PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
496 PMDM->setTopLevelManager(this);
497 addPassManager(PMDM);
498 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000499}
500
Devang Patelafb1f3622006-12-12 22:35:25 +0000501/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000502void
Bill Wendlingea857e12012-05-14 07:53:40 +0000503PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000504 unsigned PDepth = 0;
505 if (P->getResolver())
506 PDepth = P->getResolver()->getPMDataManager().getDepth();
507
Yaron Keren4849aa32015-06-05 17:48:47 +0000508 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000509 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000510
Devang Patel01919d22007-03-08 19:05:01 +0000511 if (P == AP)
512 continue;
513
Tobias Grosserf07426b2011-01-20 21:03:22 +0000514 // Update the last users of passes that are required transitive by AP.
515 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
516 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
517 SmallVector<Pass *, 12> LastUses;
518 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000519 for (AnalysisID ID : IDs) {
520 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000521 assert(AnalysisPass && "Expected analysis pass to exist.");
522 AnalysisResolver *AR = AnalysisPass->getResolver();
523 assert(AR && "Expected analysis resolver to exist.");
524 unsigned APDepth = AR->getPMDataManager().getDepth();
525
526 if (PDepth == APDepth)
527 LastUses.push_back(AnalysisPass);
528 else if (PDepth > APDepth)
529 LastPMUses.push_back(AnalysisPass);
530 }
531
532 setLastUser(LastUses, P);
533
534 // If this pass has a corresponding pass manager, push higher level
535 // analysis to this pass manager.
536 if (P->getResolver())
537 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
538
539
Devang Patelafb1f3622006-12-12 22:35:25 +0000540 // If AP is the last user of other passes then make P last user of
541 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000542 for (auto LU : LastUser) {
543 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000544 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000545 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000546 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000547 }
548 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000549}
550
551/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000552void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000553 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000554 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000555 InversedLastUser.find(P);
556 if (DMI == InversedLastUser.end())
557 return;
558
559 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000560 for (Pass *LUP : LU) {
561 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000562 }
563
Devang Patelafb1f3622006-12-12 22:35:25 +0000564}
565
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000566AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000567 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000568 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000569 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000570 AnUsage = DMI->second;
571 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000572 // Look up the analysis usage from the pass instance (different instances
573 // of the same pass can produce different results), but unique the
574 // resulting object to reduce memory usage. This helps to greatly reduce
575 // memory usage when we have many instances of only a few pass types
576 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
577 // of dependencies.
578 AnalysisUsage AU;
579 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000580
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000581 AUFoldingSetNode* Node = nullptr;
582 FoldingSetNodeID ID;
583 AUFoldingSetNode::Profile(ID, AU);
584 void *IP = nullptr;
585 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
586 Node = N;
587 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000588 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000589 UniqueAnalysisUsages.InsertNode(Node, IP);
590 }
591 assert(Node && "cached analysis usage must be non null");
592
593 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000594 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000595 }
596 return AnUsage;
597}
598
Devang Patelafb1f3622006-12-12 22:35:25 +0000599/// Schedule pass P for execution. Make sure that passes required by
600/// P are run before P is run. Update analysis info maintained by
601/// the manager. Remove dead passes. This is a recursive function.
602void PMTopLevelManager::schedulePass(Pass *P) {
603
Devang Patel3312f752007-01-16 21:43:18 +0000604 // TODO : Allocate function manager for this pass, other wise required set
605 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000606
Devang Pateld74ede72007-03-06 01:06:16 +0000607 // Give pass a chance to prepare the stage.
608 P->preparePassManager(activeStack);
609
Devang Patel864970e2008-03-18 00:39:19 +0000610 // If P is an analysis pass and it is available then do not
611 // generate the analysis again. Stale analysis info should not be
612 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000613 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000614 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Craig Topper4ee28412018-08-20 20:57:30 +0000615 // Remove any cached AnalysisUsage information.
616 AnUsageMap.erase(P);
Nuno Lopes0460bb22008-11-04 23:03:58 +0000617 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000618 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000619 }
Devang Patel864970e2008-03-18 00:39:19 +0000620
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000621 AnalysisUsage *AnUsage = findAnalysisUsage(P);
622
Devang Patelfdee7032008-08-14 23:07:48 +0000623 bool checkAnalysis = true;
624 while (checkAnalysis) {
625 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000626
Devang Patelfdee7032008-08-14 23:07:48 +0000627 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000628 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000629
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000630 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000631 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000632 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000633
Craig Topperc6207612014-04-09 06:08:46 +0000634 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000635 // Pass P is not in the global PassRegistry
636 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
637 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
638 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000639 for (const AnalysisID ID2 : RequiredSet) {
640 if (ID == ID2)
641 break;
642 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000643 if (AnalysisPass2) {
644 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000645 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000646 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
647 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
648 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
649 }
650 }
651 }
652
Andrew Trick6bbaf132011-06-03 00:48:58 +0000653 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000654 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000655 if (P->getPotentialPassManagerType () ==
656 AnalysisPass->getPotentialPassManagerType())
657 // Schedule analysis pass that is managed by the same pass manager.
658 schedulePass(AnalysisPass);
659 else if (P->getPotentialPassManagerType () >
660 AnalysisPass->getPotentialPassManagerType()) {
661 // Schedule analysis pass that is managed by a new manager.
662 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000663 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000664 // are already checked are still available.
665 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000666 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000667 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000668 // passes are run on the fly.
669 delete AnalysisPass;
670 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000671 }
672 }
673
674 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000675 if (ImmutablePass *IP = P->getAsImmutablePass()) {
676 // P is a immutable pass and it will be managed by this
677 // top level manager. Set up analysis resolver to connect them.
678 PMDataManager *DM = getAsPMDataManager();
679 AnalysisResolver *AR = new AnalysisResolver(*DM);
680 P->setResolver(AR);
681 DM->initializeAnalysisImpl(P);
682 addImmutablePass(IP);
683 DM->recordAvailableAnalysis(IP);
684 return;
685 }
686
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000687 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000688 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000689 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000690 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
691 }
692
693 // Add the requested pass to the best available pass manager.
694 P->assignPassManager(activeStack, getTopLevelPassManagerType());
695
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000696 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000697 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000698 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000699 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
700 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000701}
702
703/// Find the pass that implements Analysis AID. Search immutable
704/// passes and all pass managers. If desired pass is not found
705/// then return NULL.
706Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000707 // For immutable passes we have a direct mapping from ID to pass, so check
708 // that first.
709 if (Pass *P = ImmutablePassMap.lookup(AID))
710 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000711
Devang Patelcd6ba152006-12-12 22:50:05 +0000712 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000713 for (PMDataManager *PassManager : PassManagers)
714 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000715 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000716
717 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000718 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
719 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000720 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000721
Craig Topperc6207612014-04-09 06:08:46 +0000722 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000723}
724
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000725const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
726 const PassInfo *&PI = AnalysisPassInfos[AID];
727 if (!PI)
728 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
729 else
730 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
731 "The pass info pointer changed for an analysis ID!");
732
733 return PI;
734}
735
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000736void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
737 P->initializePass();
738 ImmutablePasses.push_back(P);
739
740 // Add this pass to the map from its analysis ID. We clobber any prior runs
741 // of the pass in the map so that the last one added is the one found when
742 // doing lookups.
743 AnalysisID AID = P->getPassID();
744 ImmutablePassMap[AID] = P;
745
746 // Also add any interfaces implemented by the immutable pass to the map for
747 // fast lookup.
748 const PassInfo *PassInf = findAnalysisPassInfo(AID);
749 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000750 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000751 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
752}
753
Devang Pateleda56172006-12-12 23:34:33 +0000754// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000755void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000756
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000757 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000758 return;
759
Devang Pateleda56172006-12-12 23:34:33 +0000760 // Print out the immutable passes
761 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000762 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000763 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000764
Dan Gohmanf71c5212010-08-19 01:29:07 +0000765 // Every class that derives from PMDataManager also derives from Pass
766 // (sometimes indirectly), but there's no inheritance relationship
767 // between PMDataManager and Pass, so we have to getAsPass to get
768 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000769 for (PMDataManager *Manager : PassManagers)
770 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000771}
772
Devang Patel991aeba2006-12-15 20:13:01 +0000773void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000774
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000775 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000776 return;
777
David Greene994e1bb2010-01-05 01:30:02 +0000778 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000779 for (ImmutablePass *P : ImmutablePasses)
780 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000781 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000782 if (!PI->isAnalysisGroup())
783 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000784 }
Yaron Keren83009952016-04-28 14:49:44 +0000785 for (PMDataManager *PM : PassManagers)
786 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000787 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000788}
789
Devang Patele3068402006-12-21 00:16:50 +0000790void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000791 for (PMDataManager *PM : PassManagers)
792 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000793
Devang Patele3068402006-12-21 00:16:50 +0000794 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000795 for (PMDataManager *IPM : IndirectPassManagers)
796 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000797
Yaron Kerenbd873192016-10-02 19:21:41 +0000798 for (auto LU : LastUser) {
799 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
800 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000801 }
Devang Patele3068402006-12-21 00:16:50 +0000802}
803
Devang Patele7599552007-01-12 18:52:44 +0000804/// Destructor
805PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000806 for (PMDataManager *PM : PassManagers)
807 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000808
Yaron Keren83009952016-04-28 14:49:44 +0000809 for (ImmutablePass *P : ImmutablePasses)
810 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000811}
812
Devang Patelafb1f3622006-12-12 22:35:25 +0000813//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000814// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000815
Devang Patel643676c2006-11-11 01:10:19 +0000816/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000817void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000818 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000819
Chris Lattner60987362009-03-06 05:53:14 +0000820 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000821
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000822 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000823
Dan Gohmande6188a2010-08-12 23:50:08 +0000824 // This pass is the current implementation of all of the interfaces it
825 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000826 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000827 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000828 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000829 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000830 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000831}
832
Devang Patel9d9fc902007-03-06 17:52:53 +0000833// Return true if P preserves high level analysis used by other
834// passes managed by this manager
835bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000836 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000837 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000838 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000839
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000840 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000841 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000842 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000843 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000844 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000845 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000846
Devang Patel9d9fc902007-03-06 17:52:53 +0000847 return true;
848}
849
Chris Lattner02eb94c2008-08-07 07:34:50 +0000850/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000851void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000852 // Don't do this unless assertions are enabled.
853#ifdef NDEBUG
854 return;
855#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000856 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
857 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000858
Devang Patelef432532007-07-19 05:36:09 +0000859 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000860 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000861 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000862 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000863 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000864 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000865 }
866}
867
Devang Patel67c79a42008-07-01 19:50:56 +0000868/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000869void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000870 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
871 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000872 return;
873
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000874 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000875 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000876 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000877 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000878 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000879 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000880 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000881 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000882 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000883 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
884 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000885 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000886 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000887 }
Devang Patel349170f2006-11-11 01:24:55 +0000888 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000889
Devang Patel42dd1e92007-03-06 01:55:46 +0000890 // Check inherited analysis also. If P is not preserving analysis
891 // provided by parent manager then remove it here.
892 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
893
894 if (!InheritedAnalysis[Index])
895 continue;
896
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000897 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000898 I = InheritedAnalysis[Index]->begin(),
899 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000900 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000901 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000902 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000903 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000904 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000905 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000906 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
907 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000908 }
Devang Patel01919d22007-03-08 19:05:01 +0000909 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000910 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000911 }
912 }
Devang Patelf68a3492006-11-07 22:35:17 +0000913}
914
Devang Patelca189262006-11-14 03:05:08 +0000915/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000916void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000917 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000918
Devang Patel8adae862007-07-20 18:04:54 +0000919 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000920
Devang Patel2ff44922007-04-16 20:39:59 +0000921 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000922 if (!TPM)
923 return;
924
Devang Patel17ad0962006-12-08 00:37:52 +0000925 TPM->collectLastUses(DeadPasses, P);
926
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000927 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000928 dbgs() << " -*- '" << P->getPassName();
929 dbgs() << "' is the last user of following pass instances.";
930 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000931 }
932
Yaron Kerenbd873192016-10-02 19:21:41 +0000933 for (Pass *P : DeadPasses)
934 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000935}
Devang Patel200d3052006-12-13 23:50:44 +0000936
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000937void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000938 enum PassDebuggingString DBG_STR) {
939 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000940
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000941 {
942 // If the pass crashes releasing memory, remember this.
943 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000944 TimeRegion PassTimer(getPassTimer(P));
945
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000946 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000947 }
948
Owen Andersona7aed182010-08-06 18:33:48 +0000949 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000950 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000951 // Remove the pass itself (if it is not already removed).
952 AvailableAnalysis.erase(PI);
953
954 // Remove all interfaces this pass implements, for which it is also
955 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000956 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000957 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000958 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000959 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000960 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000961 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000962 }
Devang Patel17ad0962006-12-08 00:37:52 +0000963 }
Devang Patelca189262006-11-14 03:05:08 +0000964}
965
Dan Gohmande6188a2010-08-12 23:50:08 +0000966/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000967/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000968void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000969 // This manager is going to manage pass P. Set up analysis resolver
970 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000971 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000972 P->setResolver(AR);
973
Devang Patelec2b9a72007-03-05 22:57:49 +0000974 // If a FunctionPass F is the last user of ModulePass info M
975 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000976 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000977
Chris Lattner60987362009-03-06 05:53:14 +0000978 if (!ProcessAnalysis) {
979 // Add pass
980 PassVector.push_back(P);
981 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000982 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000983
Chris Lattner60987362009-03-06 05:53:14 +0000984 // At the moment, this pass is the last user of all required passes.
985 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000986 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000987 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
988
989 unsigned PDepth = this->getDepth();
990
Chandler Carruth44a13852015-08-19 03:02:12 +0000991 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
992 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000993 unsigned RDepth = 0;
994
Chandler Carruth44a13852015-08-19 03:02:12 +0000995 assert(PUsed->getResolver() && "Analysis Resolver is not set");
996 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +0000997 RDepth = DM.getDepth();
998
999 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001000 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001001 else if (PDepth > RDepth) {
1002 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001003 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001004 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001005 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001006 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001007 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001008 }
1009
1010 // Set P as P's last user until someone starts using P.
1011 // However, if P is a Pass Manager then it does not need
1012 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001013 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001014 LastUses.push_back(P);
1015 TPM->setLastUser(LastUses, P);
1016
1017 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001018 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001019 TPM->setLastUser(TransferLastUses, My_PM);
1020 TransferLastUses.clear();
1021 }
1022
Dan Gohman6304db32010-08-16 22:57:28 +00001023 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001024 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1025 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001026 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001027 this->addLowerLevelRequiredPass(P, AnalysisPass);
1028 }
1029
1030 // Take a note of analysis required and made available by this pass.
1031 // Remove the analysis not preserved by this pass
1032 removeNotPreservedAnalysis(P);
1033 recordAvailableAnalysis(P);
1034
Devang Patel8cad70d2006-11-11 01:51:02 +00001035 // Add pass
1036 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001037}
1038
Devang Patele64d3052007-04-16 20:12:57 +00001039
Chandler Carruth44a13852015-08-19 03:02:12 +00001040/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001041/// pass P and are available. Populate RP_NotAvail with analysis
1042/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001043void PMDataManager::collectRequiredAndUsedAnalyses(
1044 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1045 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001046 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001047
1048 for (const auto &UsedID : AnUsage->getUsedSet())
1049 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1050 UP.push_back(AnalysisPass);
1051
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001052 for (const auto &RequiredID : AnUsage->getRequiredSet())
1053 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001054 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001055 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001056 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001057
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001058 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1059 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001060 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001061 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001062 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001063}
1064
Devang Patel07f4f582006-11-14 21:49:36 +00001065// All Required analyses should be available to the pass as it runs! Here
1066// we fill in the AnalysisImpls member of the pass so that it can
1067// successfully use the getAnalysis() method to retrieve the
1068// implementations it needs.
1069//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001070void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001071 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1072
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001073 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1074 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001075 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001076 // This may be analysis pass that is initialized on the fly.
1077 // If that is not the case then it will raise an assert when it is used.
1078 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001079 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001080 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001081 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001082 }
1083}
1084
Devang Patel640c5bb2006-12-08 22:30:11 +00001085/// Find the pass that implements Analysis AID. If desired pass is not found
1086/// then return NULL.
1087Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1088
1089 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001090 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001091
1092 if (I != AvailableAnalysis.end())
1093 return I->second;
1094
1095 // Search Parents through TopLevelManager
1096 if (SearchParent)
1097 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001098
Craig Topperc6207612014-04-09 06:08:46 +00001099 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001100}
1101
Devang Patel991aeba2006-12-15 20:13:01 +00001102// Print list of passes that are last used by P.
1103void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1104
Devang Patel8adae862007-07-20 18:04:54 +00001105 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001106
1107 // If this is a on the fly manager then it does not have TPM.
1108 if (!TPM)
1109 return;
1110
Devang Patel991aeba2006-12-15 20:13:01 +00001111 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001112
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001113 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001114 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001115 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001116 }
1117}
1118
1119void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001120 for (Pass *P : PassVector) {
1121 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001122 PMD->dumpPassArguments();
1123 else
Owen Andersona7aed182010-08-06 18:33:48 +00001124 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001125 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001126 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001127 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001128 }
1129}
1130
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001131void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1132 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001133 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001134 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001135 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001136 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001137 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001138 switch (S1) {
1139 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
1145 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
1148 default:
1149 break;
1150 }
1151 switch (S2) {
1152 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001154 break;
1155 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
1158 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001161 case ON_REGION_MSG:
1162 dbgs() << "' on Region '" << Msg << "'...\n";
1163 break;
Devang Patel003a5592007-03-05 20:01:30 +00001164 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001165 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001166 break;
1167 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001168 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001169 break;
1170 default:
1171 break;
1172 }
Devang Patel991aeba2006-12-15 20:13:01 +00001173}
1174
Chris Lattner4c1e9542009-03-06 06:45:05 +00001175void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001176 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001177 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001178
Chris Lattner4c493d92008-08-08 15:14:09 +00001179 AnalysisUsage analysisUsage;
1180 P->getAnalysisUsage(analysisUsage);
1181 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1182}
1183
Chris Lattner4c1e9542009-03-06 06:45:05 +00001184void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001185 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001186 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001187
Chris Lattner4c493d92008-08-08 15:14:09 +00001188 AnalysisUsage analysisUsage;
1189 P->getAnalysisUsage(analysisUsage);
1190 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1191}
1192
Chandler Carruth44a13852015-08-19 03:02:12 +00001193void PMDataManager::dumpUsedSet(const Pass *P) const {
1194 if (PassDebugging < Details)
1195 return;
1196
1197 AnalysisUsage analysisUsage;
1198 P->getAnalysisUsage(analysisUsage);
1199 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1200}
1201
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001202void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001203 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001204 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001205 if (Set.empty())
1206 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001207 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001208 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001209 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001210 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001211 if (!PInf) {
1212 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1213 // all drivers.
1214 dbgs() << " Uninitialized Pass";
1215 continue;
1216 }
Owen Andersona7aed182010-08-06 18:33:48 +00001217 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001218 }
David Greene994e1bb2010-01-05 01:30:02 +00001219 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001220}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001221
Devang Patel004937b2007-07-27 20:06:09 +00001222/// Add RequiredPass into list of lower level passes required by pass P.
1223/// RequiredPass is run on the fly by Pass Manager when P requests it
1224/// through getAnalysis interface.
1225/// This should be handled by specific pass manager.
1226void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1227 if (TPM) {
1228 TPM->dumpArguments();
1229 TPM->dumpPasses();
1230 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001231
Dan Gohmande6188a2010-08-12 23:50:08 +00001232 // Module Level pass may required Function Level analysis info
1233 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1234 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001235 // module level pass is requiring lower level analysis info managed by
1236 // lower level pass manager.
1237
1238 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001239 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001240 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001241#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001242 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1243 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001244#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001245 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001246}
1247
Owen Andersona7aed182010-08-06 18:33:48 +00001248Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001249 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001250}
1251
Devang Patele7599552007-01-12 18:52:44 +00001252// Destructor
1253PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001254 for (Pass *P : PassVector)
1255 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001256}
1257
Devang Patel9bdf7d42006-12-08 23:28:54 +00001258//===----------------------------------------------------------------------===//
1259// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001260// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1261Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001262 return PM.findAnalysisPass(ID, dir);
1263}
1264
Dan Gohmande6188a2010-08-12 23:50:08 +00001265Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001266 Function &F) {
1267 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1268}
1269
Devang Patela1514cb2006-12-07 19:39:39 +00001270//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001271// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001272
Dan Gohmande6188a2010-08-12 23:50:08 +00001273/// Execute all of the passes scheduled for execution by invoking
1274/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001275/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001276bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001277 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001278 return false;
1279
Devang Patele9585592006-12-08 01:38:28 +00001280 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001281 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001282
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001283 unsigned InstrCount, BBSize = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001284 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001285 if (EmitICRemark)
1286 InstrCount = initSizeRemarkInfo(M);
1287
1288 for (BasicBlock &BB : F) {
1289 // Collect the initial size of the basic block.
1290 if (EmitICRemark)
1291 BBSize = BB.size();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001292 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1293 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001294 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001295
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001296 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001297 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001298
Devang Patelabfbe3b2006-12-16 00:56:26 +00001299 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001300
Chris Lattner4c1e9542009-03-06 06:45:05 +00001301 {
1302 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001303 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001304 TimeRegion PassTimer(getPassTimer(BP));
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001305 LocalChanged |= BP->runOnBasicBlock(BB);
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001306 if (EmitICRemark) {
1307 unsigned NewSize = BB.size();
1308 // Update the size of the basic block, emit a remark, and update the
1309 // size of the module.
1310 if (NewSize != BBSize) {
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001311 int64_t Delta =
1312 static_cast<int64_t>(NewSize) - static_cast<int64_t>(BBSize);
Jessica Paquette71e97782018-08-31 20:54:37 +00001313 emitInstrCountChangedRemark(BP, M, Delta, InstrCount, &F);
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001314 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1315 BBSize = NewSize;
1316 }
1317 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001318 }
Devang Patel93a197c2006-12-14 00:08:04 +00001319
Dan Gohman74b189f2010-03-01 17:34:28 +00001320 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001321 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001322 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001323 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001324 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001325 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001326
Devang Patela273d1c2007-07-19 18:02:32 +00001327 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 removeNotPreservedAnalysis(BP);
1329 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001330 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001331 }
Jessica Paquette9eda13e2018-08-31 20:20:53 +00001332 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001333
Bill Wendling6ce6d262009-12-25 13:50:18 +00001334 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001335}
1336
Devang Patel475c4532006-12-08 00:59:05 +00001337// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001338bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001339 bool Changed = false;
1340
Chris Lattner4c1e9542009-03-06 06:45:05 +00001341 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1342 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001343
1344 return Changed;
1345}
1346
Duncan Sands51495602009-02-13 09:42:34 +00001347bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001348 bool Changed = false;
1349
Pedro Artigas41b98842012-12-05 17:12:22 +00001350 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001351 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001352
1353 return Changed;
1354}
1355
Duncan Sands51495602009-02-13 09:42:34 +00001356bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001357 bool Changed = false;
1358
Devang Patelabfbe3b2006-12-16 00:56:26 +00001359 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1360 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001361 Changed |= BP->doInitialization(F);
1362 }
1363
1364 return Changed;
1365}
1366
Duncan Sands51495602009-02-13 09:42:34 +00001367bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001368 bool Changed = false;
1369
Devang Patelabfbe3b2006-12-16 00:56:26 +00001370 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1371 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001372 Changed |= BP->doFinalization(F);
1373 }
1374
1375 return Changed;
1376}
1377
1378
Devang Patela1514cb2006-12-07 19:39:39 +00001379//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001380// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001381
Devang Patel4e12f862006-11-08 10:44:40 +00001382/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001383FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001384 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001385 // FPM is the top level manager.
1386 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001387
Dan Gohman565df952008-03-13 02:08:36 +00001388 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001389 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001390}
1391
Devang Patelb67904d2006-12-13 02:36:01 +00001392FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001393 delete FPM;
1394}
1395
Dan Gohmande6188a2010-08-12 23:50:08 +00001396void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001397 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001398}
1399
Devang Patel9f3083e2006-11-15 19:39:54 +00001400/// run - Execute all of the passes scheduled for execution. Keep
1401/// track of whether any of the passes modifies the function, and if
1402/// so, return true.
1403///
Devang Patelb67904d2006-12-13 02:36:01 +00001404bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001405 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1406 report_fatal_error("Error reading bitcode file: " + EIB.message());
1407 });
Devang Patel272908d2006-12-08 22:57:48 +00001408 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001409}
1410
1411
Devang Patelff631ae2006-11-15 01:27:05 +00001412/// doInitialization - Run all of the initializers for the function passes.
1413///
Devang Patelb67904d2006-12-13 02:36:01 +00001414bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001415 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001416}
1417
Dan Gohmane6656eb2007-07-30 14:51:13 +00001418/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001419///
Devang Patelb67904d2006-12-13 02:36:01 +00001420bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001421 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001422}
1423
Devang Patela1514cb2006-12-07 19:39:39 +00001424//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425// FunctionPassManagerImpl implementation
1426//
Duncan Sands51495602009-02-13 09:42:34 +00001427bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428 bool Changed = false;
1429
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001430 dumpArguments();
1431 dumpPasses();
1432
Yaron Keren4849aa32015-06-05 17:48:47 +00001433 for (ImmutablePass *ImPass : getImmutablePasses())
1434 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001435
Chris Lattner4c1e9542009-03-06 06:45:05 +00001436 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1437 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001438
1439 return Changed;
1440}
1441
Duncan Sands51495602009-02-13 09:42:34 +00001442bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001443 bool Changed = false;
1444
Pedro Artigas41b98842012-12-05 17:12:22 +00001445 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001446 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001447
Yaron Keren4849aa32015-06-05 17:48:47 +00001448 for (ImmutablePass *ImPass : getImmutablePasses())
1449 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001450
Devang Patel67d6a5e2006-12-19 19:46:59 +00001451 return Changed;
1452}
1453
Devang Patelec9c58f2009-04-01 22:34:41 +00001454/// cleanup - After running all passes, clean up pass manager cache.
1455void FPPassManager::cleanup() {
1456 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1457 FunctionPass *FP = getContainedPass(Index);
1458 AnalysisResolver *AR = FP->getResolver();
1459 assert(AR && "Analysis Resolver is not set");
1460 AR->clearAnalysisImpls();
1461 }
1462}
1463
Torok Edwin24c78352009-06-29 18:49:09 +00001464void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1465 if (!wasRun)
1466 return;
1467 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1468 FPPassManager *FPPM = getContainedManager(Index);
1469 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1470 FPPM->getContainedPass(Index)->releaseMemory();
1471 }
1472 }
Torok Edwin896556e2009-06-29 21:05:10 +00001473 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001474}
1475
Devang Patel67d6a5e2006-12-19 19:46:59 +00001476// Execute all the passes managed by this top level manager.
1477// Return true if any function is modified by a pass.
1478bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001479 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001480
Devang Patele3068402006-12-21 00:16:50 +00001481 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001482 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001483 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001484 F.getContext().yield();
1485 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001486
1487 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1488 getContainedManager(Index)->cleanup();
1489
Torok Edwin24c78352009-06-29 18:49:09 +00001490 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001491 return Changed;
1492}
1493
1494//===----------------------------------------------------------------------===//
1495// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001496
Devang Patel8c78a0b2007-05-03 01:11:54 +00001497char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001498/// Print passes managed by this manager
1499void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001500 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001501 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1502 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001503 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001504 dumpLastUses(FP, Offset+1);
1505 }
1506}
1507
1508
Dan Gohmande6188a2010-08-12 23:50:08 +00001509/// Execute all of the passes scheduled for execution by invoking
1510/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001511/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001512bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001513 if (F.isDeclaration())
1514 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001515
1516 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001517 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001518 // Collect inherited analysis from Module level pass manager.
1519 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001520
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001521 unsigned InstrCount, FunctionSize = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001522 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001523 // Collect the initial size of the module.
1524 if (EmitICRemark) {
1525 InstrCount = initSizeRemarkInfo(M);
1526 FunctionSize = F.getInstructionCount();
1527 }
1528
Devang Patelabfbe3b2006-12-16 00:56:26 +00001529 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1530 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001531 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001532
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001533 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001534 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001535
Devang Patelabfbe3b2006-12-16 00:56:26 +00001536 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001537
Chris Lattner4c1e9542009-03-06 06:45:05 +00001538 {
1539 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001540 TimeRegion PassTimer(getPassTimer(FP));
Dan Gohman74b189f2010-03-01 17:34:28 +00001541 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001542 if (EmitICRemark) {
1543 unsigned NewSize = F.getInstructionCount();
1544
1545 // Update the size of the function, emit a remark, and update the size
1546 // of the module.
1547 if (NewSize != FunctionSize) {
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001548 int64_t Delta = static_cast<int64_t>(NewSize) -
1549 static_cast<int64_t>(FunctionSize);
Jessica Paquette71e97782018-08-31 20:54:37 +00001550 emitInstrCountChangedRemark(FP, M, Delta, InstrCount, &F);
Jessica Paquettef2a202c2018-08-31 20:19:41 +00001551 InstrCount = static_cast<int64_t>(InstrCount) + Delta;
1552 FunctionSize = NewSize;
1553 }
1554 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001555 }
Devang Patel93a197c2006-12-14 00:08:04 +00001556
Dan Gohman74b189f2010-03-01 17:34:28 +00001557 Changed |= LocalChanged;
1558 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001559 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001560 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001561 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001562
Devang Patela273d1c2007-07-19 18:02:32 +00001563 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001564 removeNotPreservedAnalysis(FP);
1565 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001566 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001567 }
1568 return Changed;
1569}
1570
Devang Patel67d6a5e2006-12-19 19:46:59 +00001571bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001572 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001573
Serge Pavloved5eb932017-01-15 10:23:18 +00001574 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001575 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001576
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001577 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001578}
1579
Duncan Sands51495602009-02-13 09:42:34 +00001580bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001581 bool Changed = false;
1582
Chris Lattner4c1e9542009-03-06 06:45:05 +00001583 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1584 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001585
Devang Patelff631ae2006-11-15 01:27:05 +00001586 return Changed;
1587}
1588
Duncan Sands51495602009-02-13 09:42:34 +00001589bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001590 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001591
1592 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001593 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001594
Devang Patelff631ae2006-11-15 01:27:05 +00001595 return Changed;
1596}
1597
Devang Patela1514cb2006-12-07 19:39:39 +00001598//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001599// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001600
Dan Gohmande6188a2010-08-12 23:50:08 +00001601/// Execute all of the passes scheduled for execution by invoking
1602/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001603/// the module, and if so, return true.
1604bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001605MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001606 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001607
Torok Edwin24c78352009-06-29 18:49:09 +00001608 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001609 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1610 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001611 Changed |= FPP->doInitialization(M);
1612 }
1613
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001614 // Initialize module passes
1615 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1616 Changed |= getContainedPass(Index)->doInitialization(M);
1617
Jessica Paquette454d1032018-08-31 20:20:55 +00001618 unsigned InstrCount, ModuleCount = 0;
Xin Tong023e25a2018-07-22 05:27:41 +00001619 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Jessica Paquette454d1032018-08-31 20:20:55 +00001620 // Collect the initial size of the module.
1621 if (EmitICRemark) {
1622 InstrCount = initSizeRemarkInfo(M);
1623 ModuleCount = InstrCount;
1624 }
1625
Devang Patelabfbe3b2006-12-16 00:56:26 +00001626 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1627 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001628 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001629
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001630 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001631 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001632
Devang Patelabfbe3b2006-12-16 00:56:26 +00001633 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001634
Chris Lattner4c1e9542009-03-06 06:45:05 +00001635 {
1636 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001637 TimeRegion PassTimer(getPassTimer(MP));
1638
Dan Gohman74b189f2010-03-01 17:34:28 +00001639 LocalChanged |= MP->runOnModule(M);
Jessica Paquette454d1032018-08-31 20:20:55 +00001640 if (EmitICRemark) {
1641 // Update the size of the module.
Jessica Paquette454d1032018-08-31 20:20:55 +00001642 ModuleCount = M.getInstructionCount();
1643 if (ModuleCount != InstrCount) {
Jessica Paquette9a23c552018-08-31 20:20:57 +00001644 int64_t Delta = static_cast<int64_t>(ModuleCount) -
1645 static_cast<int64_t>(InstrCount);
1646 emitInstrCountChangedRemark(MP, M, Delta, InstrCount);
Jessica Paquette454d1032018-08-31 20:20:55 +00001647 ModuleCount = InstrCount;
1648 }
1649 }
Chris Lattner4c1e9542009-03-06 06:45:05 +00001650 }
Devang Patel93a197c2006-12-14 00:08:04 +00001651
Dan Gohman74b189f2010-03-01 17:34:28 +00001652 Changed |= LocalChanged;
1653 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001654 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001655 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001656 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001657 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001658
Devang Patela273d1c2007-07-19 18:02:32 +00001659 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001660 removeNotPreservedAnalysis(MP);
1661 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001662 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001663 }
Torok Edwin24c78352009-06-29 18:49:09 +00001664
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001665 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001666 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001667 Changed |= getContainedPass(Index)->doFinalization(M);
1668
Torok Edwin24c78352009-06-29 18:49:09 +00001669 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001670 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1671 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001672 // We don't know when is the last time an on-the-fly pass is run,
1673 // so we need to releaseMemory / finalize here
1674 FPP->releaseMemoryOnTheFly();
1675 Changed |= FPP->doFinalization(M);
1676 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001677
Devang Patel05e1a972006-11-07 22:03:15 +00001678 return Changed;
1679}
1680
Devang Patele64d3052007-04-16 20:12:57 +00001681/// Add RequiredPass into list of lower level passes required by pass P.
1682/// RequiredPass is run on the fly by Pass Manager when P requests it
1683/// through getAnalysis interface.
1684void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001685 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1686 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001687 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001688 RequiredPass->getPotentialPassManagerType()) &&
1689 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001690 if (!RequiredPass)
1691 return;
Devang Patele64d3052007-04-16 20:12:57 +00001692
Devang Patel68f72b12007-04-26 17:50:19 +00001693 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001694 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001695 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001696 // FPP is the top level manager.
1697 FPP->setTopLevelManager(FPP);
1698
Devang Patel69e9f6d2007-04-16 20:27:05 +00001699 OnTheFlyManagers[P] = FPP;
1700 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001701 const PassInfo *RequiredPassPI =
1702 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001703
Craig Topperc6207612014-04-09 06:08:46 +00001704 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001705 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1706 FoundPass =
1707 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001708 }
Andrew Trick02066f22014-04-08 03:40:34 +00001709 if (!FoundPass) {
1710 FoundPass = RequiredPass;
1711 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001712 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001713 FPP->add(RequiredPass);
1714 }
1715 // Register P as the last user of FoundPass or RequiredPass.
1716 SmallVector<Pass *, 1> LU;
1717 LU.push_back(FoundPass);
1718 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001719}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001720
Dan Gohmande6188a2010-08-12 23:50:08 +00001721/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001722/// required by module pass MP. Instantiate analysis pass, by using
1723/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001724Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001725 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001726 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001727
Torok Edwin24c78352009-06-29 18:49:09 +00001728 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001729 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001730 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001731}
1732
1733
Devang Patela1514cb2006-12-07 19:39:39 +00001734//===----------------------------------------------------------------------===//
1735// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001736
Devang Patelab97cf42006-12-13 00:09:23 +00001737//
Devang Patelc290c8a2006-11-07 22:23:34 +00001738/// run - Execute all of the passes scheduled for execution. Keep track of
1739/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001740bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001741 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001742
Devang Patelcfd70c42006-12-13 22:10:00 +00001743 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001744 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001745
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001746 for (ImmutablePass *ImPass : getImmutablePasses())
1747 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001748
Devang Patele3068402006-12-21 00:16:50 +00001749 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001750 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001751 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001752 M.getContext().yield();
1753 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001754
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001755 for (ImmutablePass *ImPass : getImmutablePasses())
1756 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001757
Devang Patelc290c8a2006-11-07 22:23:34 +00001758 return Changed;
1759}
Devang Patel376fefa2006-11-08 10:29:57 +00001760
Devang Patela1514cb2006-12-07 19:39:39 +00001761//===----------------------------------------------------------------------===//
1762// PassManager implementation
1763
Devang Patel376fefa2006-11-08 10:29:57 +00001764/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001765PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001766 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001767 // PM is the top level manager
1768 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001769}
1770
Devang Patelb67904d2006-12-13 02:36:01 +00001771PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001772 delete PM;
1773}
1774
Chris Lattner60987362009-03-06 05:53:14 +00001775void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001776 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001777}
1778
1779/// run - Execute all of the passes scheduled for execution. Keep track of
1780/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001781bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001782 return PM->run(M);
1783}
1784
Devang Patelb8817b92006-12-14 00:59:42 +00001785//===----------------------------------------------------------------------===//
Devang Patel1c56a632007-01-08 19:29:38 +00001786// PMStack implementation
1787//
Devang Patelad98d232007-01-11 22:15:30 +00001788
Devang Patel1c56a632007-01-08 19:29:38 +00001789// Pop Pass Manager from the stack and clear its analysis info.
1790void PMStack::pop() {
1791
1792 PMDataManager *Top = this->top();
1793 Top->initializeAnalysisInfo();
1794
1795 S.pop_back();
1796}
1797
1798// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001799void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001800 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001801 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001802
Chris Lattner60987362009-03-06 05:53:14 +00001803 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001804 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1805 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001806 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001807
Chris Lattner60987362009-03-06 05:53:14 +00001808 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001809 TPM->addIndirectPassManager(PM);
1810 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001811 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001812 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001813 assert((PM->getPassManagerType() == PMT_ModulePassManager
1814 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001815 && "pushing bad pass manager to PMStack");
1816 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001817 }
1818
Devang Patel15701b52007-01-11 00:19:00 +00001819 S.push_back(PM);
1820}
1821
1822// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001823LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001824 for (PMDataManager *Manager : S)
1825 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001826
Devang Patel15701b52007-01-11 00:19:00 +00001827 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001828 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001829}
1830
Devang Patel1c56a632007-01-08 19:29:38 +00001831/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001832/// add self into that manager.
1833void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001834 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001835 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001836 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001837 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1838 if (TopPMType == PreferredType)
1839 break; // We found desired pass manager
1840 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001841 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001842 else
1843 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001844 }
Devang Patel18ff6362008-09-09 21:38:40 +00001845 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001846 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001847}
1848
Devang Patel3312f752007-01-16 21:43:18 +00001849/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001850/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001851void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001852 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001853
Andrew Trickcbc845f2012-02-01 07:16:20 +00001854 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001855 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001856 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1857 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001858 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001859 break;
Devang Patel3312f752007-01-16 21:43:18 +00001860 }
Devang Patel3312f752007-01-16 21:43:18 +00001861
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001862 // Create new Function Pass Manager if needed.
1863 FPPassManager *FPP;
1864 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1865 FPP = (FPPassManager *)PMS.top();
1866 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001867 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1868 PMDataManager *PMD = PMS.top();
1869
1870 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001871 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001872 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001873
1874 // [2] Set up new manager's top level manager
1875 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1876 TPM->addIndirectPassManager(FPP);
1877
1878 // [3] Assign manager to manage this new manager. This may create
1879 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001880 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001881
1882 // [4] Push new manager into PMS
1883 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001884 }
1885
Devang Patel3312f752007-01-16 21:43:18 +00001886 // Assign FPP as the manager of this pass.
1887 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001888}
1889
Devang Patel3312f752007-01-16 21:43:18 +00001890/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001891/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001892void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001893 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001894 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001895
Devang Patel15701b52007-01-11 00:19:00 +00001896 // Basic Pass Manager is a leaf pass manager. It does not handle
1897 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001898 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001899 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1900 BBP = (BBPassManager *)PMS.top();
1901 } else {
1902 // If leaf manager is not Basic Block Pass manager then create new
1903 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001904 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1905 PMDataManager *PMD = PMS.top();
1906
1907 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001908 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001909
1910 // [2] Set up new manager's top level manager
1911 // Basic Block Pass Manager does not live by itself
1912 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1913 TPM->addIndirectPassManager(BBP);
1914
Devang Patel15701b52007-01-11 00:19:00 +00001915 // [3] Assign manager to manage this new manager. This may create
1916 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001917 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001918
Devang Patel3312f752007-01-16 21:43:18 +00001919 // [4] Push new manager into PMS
1920 PMS.push(BBP);
1921 }
Devang Patel1c56a632007-01-08 19:29:38 +00001922
Devang Patel3312f752007-01-16 21:43:18 +00001923 // Assign BBP as the manager of this pass.
1924 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001925}
1926
Dan Gohmand3a20c92008-03-11 16:41:42 +00001927PassManagerBase::~PassManagerBase() {}