blob: 74481b0e501f2db4baea40cfef6c19d142ee738f [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"
James Henderson852f6fd2017-05-16 09:43:21 +000015#include "llvm/ADT/Statistic.h"
Jessica Paquettee49374d2018-05-18 17:26:39 +000016#include "llvm/IR/DiagnosticInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000017#include "llvm/IR/IRPrintingPasses.h"
18#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000019#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000020#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/Module.h"
Pavel Labathec534e62016-10-25 16:20:07 +000022#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000023#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000024#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000025#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000027#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/Timer.h"
30#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000031#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000032#include <map>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000033#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000034using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000035using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000036
Devang Patele7599552007-01-12 18:52:44 +000037// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000038
Devang Patelf1567a52006-12-13 20:03:48 +000039//===----------------------------------------------------------------------===//
40// Pass debugging information. Often it is useful to find out what pass is
41// running when a crash occurs in a utility. When this library is compiled with
42// debugging on, a command line option (--debug-pass) is enabled that causes the
43// pass name to be printed before it executes.
44//
45
Chandler Carruth7caea412013-11-09 12:26:54 +000046namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000047// Different debug levels that can be enabled...
48enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000049 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000050};
Chandler Carruth7caea412013-11-09 12:26:54 +000051}
Devang Patel03fb5872006-12-13 21:13:31 +000052
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000053static cl::opt<enum PassDebugLevel>
54PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000055 cl::desc("Print PassManager debugging information"),
56 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000057 clEnumVal(Disabled , "disable debug output"),
58 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
59 clEnumVal(Structure , "print pass structure before run()"),
60 clEnumVal(Executions, "print pass name before it is executed"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000061 clEnumVal(Details , "print pass details when it is executed")));
David Greene9b063df2010-04-02 23:17:14 +000062
Chandler Carruth7caea412013-11-09 12:26:54 +000063namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000064typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
65PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000066}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000067
68// Print IR out before/after specified passes.
69static PassOptionList
70PrintBefore("print-before",
71 llvm::cl::desc("Print IR before specified passes"),
72 cl::Hidden);
73
74static PassOptionList
75PrintAfter("print-after",
76 llvm::cl::desc("Print IR after specified passes"),
77 cl::Hidden);
78
Zachary Turner8065f0b2017-12-01 00:53:10 +000079static cl::opt<bool> PrintBeforeAll("print-before-all",
80 llvm::cl::desc("Print IR before each pass"),
81 cl::init(false), cl::Hidden);
82static cl::opt<bool> PrintAfterAll("print-after-all",
83 llvm::cl::desc("Print IR after each pass"),
84 cl::init(false), cl::Hidden);
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000085
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000086static cl::opt<bool>
87 PrintModuleScope("print-module-scope",
88 cl::desc("When printing IR for print-[before|after]{-all} "
89 "always print a module IR"),
Craig Topperf5730c32018-04-01 21:54:26 +000090 cl::init(false), cl::Hidden);
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000091
Weiming Zhao0f1762c2016-01-06 22:55:03 +000092static cl::list<std::string>
93 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
94 cl::desc("Only print IR for functions whose name "
95 "match this for all print-[before|after][-all] "
96 "options"),
Zachary Turner8065f0b2017-12-01 00:53:10 +000097 cl::CommaSeparated, cl::Hidden);
Weiming Zhao0f1762c2016-01-06 22:55:03 +000098
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000099/// This is a helper to determine whether to print IR before or
100/// after a pass.
101
102static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
103 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +0000104 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000105 if (PassInf)
106 if (PassInf->getPassArgument() == PI->getPassArgument()) {
107 return true;
108 }
David Greene9b063df2010-04-02 23:17:14 +0000109 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000110 return false;
111}
Dan Gohmande6188a2010-08-12 23:50:08 +0000112
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000113/// This is a utility to check whether a pass should have IR dumped
114/// before it.
115static bool ShouldPrintBeforePass(const PassInfo *PI) {
116 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
117}
David Greene9b063df2010-04-02 23:17:14 +0000118
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000119/// This is a utility to check whether a pass should have IR dumped
120/// after it.
121static bool ShouldPrintAfterPass(const PassInfo *PI) {
122 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000123}
124
Fedor Sergeev94dca7c2017-12-01 17:42:46 +0000125bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
126
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000127bool llvm::isFunctionInPrintList(StringRef FunctionName) {
128 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
129 PrintFuncsList.end());
130 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
131}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000132/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
133/// or higher is specified.
134bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000135 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000136}
137
Jessica Paquettee49374d2018-05-18 17:26:39 +0000138unsigned PMDataManager::initSizeRemarkInfo(Module &M) {
139 // Only calculate getInstructionCount if the size-info remark is requested.
140 if (M.getContext().getDiagHandlerPtr()->isAnalysisRemarkEnabled("size-info"))
141 return M.getInstructionCount();
142 return 0;
143}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000144
Jessica Paquettee49374d2018-05-18 17:26:39 +0000145void PMDataManager::emitInstrCountChangedRemark(Pass *P, Module &M,
146 unsigned CountBefore) {
147 // Did the user request the remark? If not, quit.
148 if (!M.getContext().getDiagHandlerPtr()->isAnalysisRemarkEnabled("size-info"))
149 return;
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000150
Jessica Paquettee49374d2018-05-18 17:26:39 +0000151 // We need a function containing at least one basic block in order to output
152 // remarks. Since it's possible that the first function in the module doesn't
153 // actually contain a basic block, we have to go and find one that's suitable
154 // for emitting remarks.
155 auto It = std::find_if(M.begin(), M.end(),
156 [](const Function &Fn) { return !Fn.empty(); });
157
158 // Didn't find a function. Quit.
159 if (It == M.end())
160 return;
161
162 // We found a function containing at least one basic block.
163 Function *F = &*It;
164
165 // How many instructions are in the module now?
166 unsigned CountAfter = M.getInstructionCount();
167
168 // If there was no change, don't emit a remark.
169 if (CountBefore == CountAfter)
170 return;
171
172 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
173 // that the only passes that return non-null with getAsPMDataManager are pass
174 // managers.) The reason we have to do this is to avoid emitting remarks for
175 // CGSCC passes.
176 if (P->getAsPMDataManager())
177 return;
178
179 // Compute a possibly negative delta between the instruction count before
180 // running P, and after running P.
181 int64_t Delta = (int64_t)CountAfter - (int64_t)CountBefore;
182
183 BasicBlock &BB = *F->begin();
184 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
185 DiagnosticLocation(), &BB);
186 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
187 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
188 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
189 << ": IR instruction count changed from "
190 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
191 << " to "
192 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
193 << "; Delta: "
194 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
195 F->getContext().diagnose(R); // Not using ORE for layering reasons.
196}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000197
Chris Lattner4c1e9542009-03-06 06:45:05 +0000198void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000199 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000200 OS << "Releasing pass '";
201 else
202 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000203
Chris Lattner4c1e9542009-03-06 06:45:05 +0000204 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000205
Chris Lattner4c1e9542009-03-06 06:45:05 +0000206 if (M) {
207 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
208 return;
209 }
Craig Topperc6207612014-04-09 06:08:46 +0000210 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000211 OS << '\n';
212 return;
213 }
214
Dan Gohman79fc0e92009-03-10 18:47:59 +0000215 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000216 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000217 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000218 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000219 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000220 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000221 OS << "value";
222
223 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000224 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000225 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000226}
227
228
Devang Patelffca9102006-12-15 19:39:30 +0000229namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000230//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000231// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000232//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000234/// pass together and sequence them to process one basic block before
235/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000236class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000237
238public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000239 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000240 explicit BBPassManager()
241 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000242
Devang Patelca58e352006-11-08 10:05:38 +0000243 /// Execute all of the passes scheduled for execution. Keep track of
244 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000245 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000246
Devang Patelf9d96b92006-12-07 19:57:52 +0000247 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000248 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000249 Info.setPreservesAll();
250 }
251
Craig Topperf398d7c2014-03-05 06:35:38 +0000252 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000253 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000254 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000255 bool doFinalization(Function &F);
256
Craig Topperf398d7c2014-03-05 06:35:38 +0000257 PMDataManager *getAsPMDataManager() override { return this; }
258 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000259
Mehdi Amini117296c2016-10-01 02:56:57 +0000260 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000261
Devang Pateleda56172006-12-12 23:34:33 +0000262 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000263 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000264 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000265 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
266 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000267 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000268 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000269 }
270 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000271
272 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000273 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000274 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
275 return BP;
276 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000277
Craig Topperf398d7c2014-03-05 06:35:38 +0000278 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000279 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000280 }
Devang Patelca58e352006-11-08 10:05:38 +0000281};
282
Devang Patel8c78a0b2007-05-03 01:11:54 +0000283char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000284} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000285
Devang Patele7599552007-01-12 18:52:44 +0000286namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000287namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000288//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000289// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000290//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000291/// FunctionPassManagerImpl manages FPPassManagers
292class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000293 public PMDataManager,
294 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000295 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000296private:
297 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000298public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000299 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000300 explicit FunctionPassManagerImpl() :
301 Pass(PT_PassManager, ID), PMDataManager(),
302 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000303
Matthias Braun0880c602014-12-12 01:27:01 +0000304 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000305 void add(Pass *P) {
306 schedulePass(P);
307 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000308
309 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000310 Pass *createPrinterPass(raw_ostream &O,
311 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000312 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000313 }
314
Torok Edwin24c78352009-06-29 18:49:09 +0000315 // Prepare for running an on the fly pass, freeing memory if needed
316 // from a previous run.
317 void releaseMemoryOnTheFly();
318
Devang Patel67d6a5e2006-12-19 19:46:59 +0000319 /// run - Execute all of the passes scheduled for execution. Keep track of
320 /// whether any of the passes modifies the module, and if so, return true.
321 bool run(Function &F);
322
323 /// doInitialization - Run all of the initializers for the function passes.
324 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000325 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000326
Dan Gohmane6656eb2007-07-30 14:51:13 +0000327 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000328 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000329 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000330
Dan Gohmande6188a2010-08-12 23:50:08 +0000331
Craig Topperf398d7c2014-03-05 06:35:38 +0000332 PMDataManager *getAsPMDataManager() override { return this; }
333 Pass *getAsPass() override { return this; }
334 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000335 return PMT_FunctionPassManager;
336 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000337
Devang Patel67d6a5e2006-12-19 19:46:59 +0000338 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000339 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000340 Info.setPreservesAll();
341 }
342
Devang Patel67d6a5e2006-12-19 19:46:59 +0000343 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000344 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000345 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
346 return FP;
347 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000348};
349
David Blaikiea379b1812011-12-20 02:50:00 +0000350void FunctionPassManagerImpl::anchor() {}
351
Devang Patel8c78a0b2007-05-03 01:11:54 +0000352char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000353} // End of legacy namespace
354} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000355
Chandler Carruth7caea412013-11-09 12:26:54 +0000356namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000357//===----------------------------------------------------------------------===//
358// MPPassManager
359//
360/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000361/// It batches all Module passes and function pass managers together and
362/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000363class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000364public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000365 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000366 explicit MPPassManager() :
367 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000368
369 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000370 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000371 for (auto &OnTheFlyManager : OnTheFlyManagers) {
372 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000373 delete FPP;
374 }
375 }
376
Dan Gohmande6188a2010-08-12 23:50:08 +0000377 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000378 Pass *createPrinterPass(raw_ostream &O,
379 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000380 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000381 }
382
Devang Patelca58e352006-11-08 10:05:38 +0000383 /// run - Execute all of the passes scheduled for execution. Keep track of
384 /// whether any of the passes modifies the module, and if so, return true.
385 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000386
Pedro Artigase4348b02012-12-03 21:56:57 +0000387 using llvm::Pass::doInitialization;
388 using llvm::Pass::doFinalization;
389
Devang Patelf9d96b92006-12-07 19:57:52 +0000390 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000391 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000392 Info.setPreservesAll();
393 }
394
Devang Patele64d3052007-04-16 20:12:57 +0000395 /// Add RequiredPass into list of lower level passes required by pass P.
396 /// RequiredPass is run on the fly by Pass Manager when P requests it
397 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000398 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000399
Dan Gohmande6188a2010-08-12 23:50:08 +0000400 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000401 /// required by module pass MP. Instantiate analysis pass, by using
402 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000403 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000404
Mehdi Amini117296c2016-10-01 02:56:57 +0000405 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000406
Craig Topperf398d7c2014-03-05 06:35:38 +0000407 PMDataManager *getAsPMDataManager() override { return this; }
408 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000409
Devang Pateleda56172006-12-12 23:34:33 +0000410 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000411 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000412 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000413 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
414 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000415 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000416 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
417 OnTheFlyManagers.find(MP);
418 if (I != OnTheFlyManagers.end())
419 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000420 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000421 }
422 }
423
Devang Patelabfbe3b2006-12-16 00:56:26 +0000424 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000425 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000426 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000427 }
428
Craig Topperf398d7c2014-03-05 06:35:38 +0000429 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000430 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000431 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000432
433 private:
434 /// Collection of on the fly FPPassManagers. These managers manage
435 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000436 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000437};
438
Devang Patel8c78a0b2007-05-03 01:11:54 +0000439char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000440} // End anonymous namespace
441
442namespace llvm {
443namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000444//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000445// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000446//
Devang Patel09f162c2007-05-01 21:15:47 +0000447
Devang Patel67d6a5e2006-12-19 19:46:59 +0000448/// PassManagerImpl manages MPPassManagers
449class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000450 public PMDataManager,
451 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000452 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000453
454public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000455 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000456 explicit PassManagerImpl() :
457 Pass(PT_PassManager, ID), PMDataManager(),
458 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000459
Matthias Braun0880c602014-12-12 01:27:01 +0000460 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000461 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000462 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000463 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000464
465 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000466 Pass *createPrinterPass(raw_ostream &O,
467 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000468 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000469 }
470
Devang Patel376fefa2006-11-08 10:29:57 +0000471 /// run - Execute all of the passes scheduled for execution. Keep track of
472 /// whether any of the passes modifies the module, and if so, return true.
473 bool run(Module &M);
474
Pedro Artigase4348b02012-12-03 21:56:57 +0000475 using llvm::Pass::doInitialization;
476 using llvm::Pass::doFinalization;
477
Devang Patelf9d96b92006-12-07 19:57:52 +0000478 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000479 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000480 Info.setPreservesAll();
481 }
482
Craig Topperf398d7c2014-03-05 06:35:38 +0000483 PMDataManager *getAsPMDataManager() override { return this; }
484 Pass *getAsPass() override { return this; }
485 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000486 return PMT_ModulePassManager;
487 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000488
Devang Patel67d6a5e2006-12-19 19:46:59 +0000489 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000490 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000491 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
492 return MP;
493 }
Devang Patel376fefa2006-11-08 10:29:57 +0000494};
495
David Blaikiea379b1812011-12-20 02:50:00 +0000496void PassManagerImpl::anchor() {}
497
Devang Patel8c78a0b2007-05-03 01:11:54 +0000498char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000499} // End of legacy namespace
500} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000501
502namespace {
503
504//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000505/// TimingInfo Class - This class is used to calculate information about the
506/// amount of time each pass takes to execute. This only happens when
507/// -time-passes is enabled on the command line.
508///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000509
Owen Anderson5a6960f2009-06-18 20:51:00 +0000510static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000511
Nick Lewycky02d5f772009-10-25 06:33:48 +0000512class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000513 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000514 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000515public:
516 // Use 'create' member to get this.
Matthias Braun9f15a792016-11-18 19:43:18 +0000517 TimingInfo() : TG("pass", "... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000518
Devang Patel1c3633e2007-01-29 23:10:37 +0000519 // TimingDtor - Print out information about timing information
520 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000521 // Delete all of the timers, which accumulate their info into the
522 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000523 for (auto &I : TimingData)
524 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000525 // TimerGroup is deleted next, printing the report.
526 }
527
528 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000529 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000530 // null. It may be called multiple times.
531 static void createTheTimeInfo();
532
James Henderson852f6fd2017-05-16 09:43:21 +0000533 // print - Prints out timing information and then resets the timers.
534 void print() {
535 TG.print(*CreateInfoOutputFile());
536 }
537
Chris Lattner707431c2010-03-30 04:03:22 +0000538 /// getPassTimer - Return the timer for the specified pass if it exists.
539 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000540 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000541 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000542
Owen Anderson5c96ef72009-07-07 18:33:04 +0000543 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000544 Timer *&T = TimingData[P];
Matthias Braun9f15a792016-11-18 19:43:18 +0000545 if (!T) {
546 StringRef PassName = P->getPassName();
547 T = new Timer(PassName, PassName, TG);
548 }
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000549 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000550 }
551};
552
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000553} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000554
Dan Gohmand78c4002008-05-13 00:00:25 +0000555static TimingInfo *TheTimeInfo;
556
Devang Patela1514cb2006-12-07 19:39:39 +0000557//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000558// PMTopLevelManager implementation
559
Devang Patel4268fc02007-01-16 02:00:38 +0000560/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000561PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
562 PMDM->setTopLevelManager(this);
563 addPassManager(PMDM);
564 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000565}
566
Devang Patelafb1f3622006-12-12 22:35:25 +0000567/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000568void
Bill Wendlingea857e12012-05-14 07:53:40 +0000569PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000570 unsigned PDepth = 0;
571 if (P->getResolver())
572 PDepth = P->getResolver()->getPMDataManager().getDepth();
573
Yaron Keren4849aa32015-06-05 17:48:47 +0000574 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000575 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000576
Devang Patel01919d22007-03-08 19:05:01 +0000577 if (P == AP)
578 continue;
579
Tobias Grosserf07426b2011-01-20 21:03:22 +0000580 // Update the last users of passes that are required transitive by AP.
581 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
582 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
583 SmallVector<Pass *, 12> LastUses;
584 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000585 for (AnalysisID ID : IDs) {
586 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000587 assert(AnalysisPass && "Expected analysis pass to exist.");
588 AnalysisResolver *AR = AnalysisPass->getResolver();
589 assert(AR && "Expected analysis resolver to exist.");
590 unsigned APDepth = AR->getPMDataManager().getDepth();
591
592 if (PDepth == APDepth)
593 LastUses.push_back(AnalysisPass);
594 else if (PDepth > APDepth)
595 LastPMUses.push_back(AnalysisPass);
596 }
597
598 setLastUser(LastUses, P);
599
600 // If this pass has a corresponding pass manager, push higher level
601 // analysis to this pass manager.
602 if (P->getResolver())
603 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
604
605
Devang Patelafb1f3622006-12-12 22:35:25 +0000606 // If AP is the last user of other passes then make P last user of
607 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000608 for (auto LU : LastUser) {
609 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000610 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000611 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000612 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000613 }
614 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000615}
616
617/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000618void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000619 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000620 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000621 InversedLastUser.find(P);
622 if (DMI == InversedLastUser.end())
623 return;
624
625 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000626 for (Pass *LUP : LU) {
627 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000628 }
629
Devang Patelafb1f3622006-12-12 22:35:25 +0000630}
631
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000632AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000633 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000634 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000635 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000636 AnUsage = DMI->second;
637 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000638 // Look up the analysis usage from the pass instance (different instances
639 // of the same pass can produce different results), but unique the
640 // resulting object to reduce memory usage. This helps to greatly reduce
641 // memory usage when we have many instances of only a few pass types
642 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
643 // of dependencies.
644 AnalysisUsage AU;
645 P->getAnalysisUsage(AU);
646
647 AUFoldingSetNode* Node = nullptr;
648 FoldingSetNodeID ID;
649 AUFoldingSetNode::Profile(ID, AU);
650 void *IP = nullptr;
651 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
652 Node = N;
653 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000654 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000655 UniqueAnalysisUsages.InsertNode(Node, IP);
656 }
657 assert(Node && "cached analysis usage must be non null");
658
659 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000660 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000661 }
662 return AnUsage;
663}
664
Devang Patelafb1f3622006-12-12 22:35:25 +0000665/// Schedule pass P for execution. Make sure that passes required by
666/// P are run before P is run. Update analysis info maintained by
667/// the manager. Remove dead passes. This is a recursive function.
668void PMTopLevelManager::schedulePass(Pass *P) {
669
Devang Patel3312f752007-01-16 21:43:18 +0000670 // TODO : Allocate function manager for this pass, other wise required set
671 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000672
Devang Pateld74ede72007-03-06 01:06:16 +0000673 // Give pass a chance to prepare the stage.
674 P->preparePassManager(activeStack);
675
Devang Patel864970e2008-03-18 00:39:19 +0000676 // If P is an analysis pass and it is available then do not
677 // generate the analysis again. Stale analysis info should not be
678 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000679 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000680 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000681 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000682 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000683 }
Devang Patel864970e2008-03-18 00:39:19 +0000684
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000685 AnalysisUsage *AnUsage = findAnalysisUsage(P);
686
Devang Patelfdee7032008-08-14 23:07:48 +0000687 bool checkAnalysis = true;
688 while (checkAnalysis) {
689 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000690
Devang Patelfdee7032008-08-14 23:07:48 +0000691 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000692 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000693
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000694 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000695 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000696 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000697
Craig Topperc6207612014-04-09 06:08:46 +0000698 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000699 // Pass P is not in the global PassRegistry
700 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
701 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
702 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000703 for (const AnalysisID ID2 : RequiredSet) {
704 if (ID == ID2)
705 break;
706 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000707 if (AnalysisPass2) {
708 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000709 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000710 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
711 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
712 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
713 }
714 }
715 }
716
Andrew Trick6bbaf132011-06-03 00:48:58 +0000717 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000718 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000719 if (P->getPotentialPassManagerType () ==
720 AnalysisPass->getPotentialPassManagerType())
721 // Schedule analysis pass that is managed by the same pass manager.
722 schedulePass(AnalysisPass);
723 else if (P->getPotentialPassManagerType () >
724 AnalysisPass->getPotentialPassManagerType()) {
725 // Schedule analysis pass that is managed by a new manager.
726 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000727 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000728 // are already checked are still available.
729 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000730 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000731 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000732 // passes are run on the fly.
733 delete AnalysisPass;
734 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000735 }
736 }
737
738 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000739 if (ImmutablePass *IP = P->getAsImmutablePass()) {
740 // P is a immutable pass and it will be managed by this
741 // top level manager. Set up analysis resolver to connect them.
742 PMDataManager *DM = getAsPMDataManager();
743 AnalysisResolver *AR = new AnalysisResolver(*DM);
744 P->setResolver(AR);
745 DM->initializeAnalysisImpl(P);
746 addImmutablePass(IP);
747 DM->recordAvailableAnalysis(IP);
748 return;
749 }
750
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000751 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000752 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000753 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000754 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
755 }
756
757 // Add the requested pass to the best available pass manager.
758 P->assignPassManager(activeStack, getTopLevelPassManagerType());
759
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000760 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000761 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000762 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000763 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
764 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000765}
766
767/// Find the pass that implements Analysis AID. Search immutable
768/// passes and all pass managers. If desired pass is not found
769/// then return NULL.
770Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000771 // For immutable passes we have a direct mapping from ID to pass, so check
772 // that first.
773 if (Pass *P = ImmutablePassMap.lookup(AID))
774 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000775
Devang Patelcd6ba152006-12-12 22:50:05 +0000776 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000777 for (PMDataManager *PassManager : PassManagers)
778 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000779 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000780
781 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000782 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
783 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000784 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000785
Craig Topperc6207612014-04-09 06:08:46 +0000786 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000787}
788
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000789const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
790 const PassInfo *&PI = AnalysisPassInfos[AID];
791 if (!PI)
792 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
793 else
794 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
795 "The pass info pointer changed for an analysis ID!");
796
797 return PI;
798}
799
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000800void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
801 P->initializePass();
802 ImmutablePasses.push_back(P);
803
804 // Add this pass to the map from its analysis ID. We clobber any prior runs
805 // of the pass in the map so that the last one added is the one found when
806 // doing lookups.
807 AnalysisID AID = P->getPassID();
808 ImmutablePassMap[AID] = P;
809
810 // Also add any interfaces implemented by the immutable pass to the map for
811 // fast lookup.
812 const PassInfo *PassInf = findAnalysisPassInfo(AID);
813 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000814 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000815 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
816}
817
Devang Pateleda56172006-12-12 23:34:33 +0000818// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000819void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000820
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000821 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000822 return;
823
Devang Pateleda56172006-12-12 23:34:33 +0000824 // Print out the immutable passes
825 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000826 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000827 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000828
Dan Gohmanf71c5212010-08-19 01:29:07 +0000829 // Every class that derives from PMDataManager also derives from Pass
830 // (sometimes indirectly), but there's no inheritance relationship
831 // between PMDataManager and Pass, so we have to getAsPass to get
832 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000833 for (PMDataManager *Manager : PassManagers)
834 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000835}
836
Devang Patel991aeba2006-12-15 20:13:01 +0000837void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000838
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000839 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000840 return;
841
David Greene994e1bb2010-01-05 01:30:02 +0000842 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000843 for (ImmutablePass *P : ImmutablePasses)
844 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000845 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000846 if (!PI->isAnalysisGroup())
847 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000848 }
Yaron Keren83009952016-04-28 14:49:44 +0000849 for (PMDataManager *PM : PassManagers)
850 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000851 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000852}
853
Devang Patele3068402006-12-21 00:16:50 +0000854void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000855 for (PMDataManager *PM : PassManagers)
856 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000857
Devang Patele3068402006-12-21 00:16:50 +0000858 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000859 for (PMDataManager *IPM : IndirectPassManagers)
860 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000861
Yaron Kerenbd873192016-10-02 19:21:41 +0000862 for (auto LU : LastUser) {
863 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
864 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000865 }
Devang Patele3068402006-12-21 00:16:50 +0000866}
867
Devang Patele7599552007-01-12 18:52:44 +0000868/// Destructor
869PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000870 for (PMDataManager *PM : PassManagers)
871 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000872
Yaron Keren83009952016-04-28 14:49:44 +0000873 for (ImmutablePass *P : ImmutablePasses)
874 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000875}
876
Devang Patelafb1f3622006-12-12 22:35:25 +0000877//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000878// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000879
Devang Patel643676c2006-11-11 01:10:19 +0000880/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000881void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000882 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000883
Chris Lattner60987362009-03-06 05:53:14 +0000884 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000885
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000886 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000887
Dan Gohmande6188a2010-08-12 23:50:08 +0000888 // This pass is the current implementation of all of the interfaces it
889 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000890 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000891 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000892 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000893 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000894 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000895}
896
Devang Patel9d9fc902007-03-06 17:52:53 +0000897// Return true if P preserves high level analysis used by other
898// passes managed by this manager
899bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000900 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000901 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000902 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000903
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000904 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000905 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000906 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000907 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000908 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000909 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000910
Devang Patel9d9fc902007-03-06 17:52:53 +0000911 return true;
912}
913
Chris Lattner02eb94c2008-08-07 07:34:50 +0000914/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000915void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000916 // Don't do this unless assertions are enabled.
917#ifdef NDEBUG
918 return;
919#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000920 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
921 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000922
Devang Patelef432532007-07-19 05:36:09 +0000923 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000924 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000925 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000926 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000927 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000928 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000929 }
930}
931
Devang Patel67c79a42008-07-01 19:50:56 +0000932/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000933void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000934 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
935 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000936 return;
937
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000938 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000939 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000940 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000941 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000942 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000943 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000944 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000945 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000946 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000947 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
948 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000949 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000950 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000951 }
Devang Patel349170f2006-11-11 01:24:55 +0000952 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000953
Devang Patel42dd1e92007-03-06 01:55:46 +0000954 // Check inherited analysis also. If P is not preserving analysis
955 // provided by parent manager then remove it here.
956 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
957
958 if (!InheritedAnalysis[Index])
959 continue;
960
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000961 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000962 I = InheritedAnalysis[Index]->begin(),
963 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000964 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000965 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000966 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000967 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000968 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000969 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000970 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
971 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000972 }
Devang Patel01919d22007-03-08 19:05:01 +0000973 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000974 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000975 }
976 }
Devang Patelf68a3492006-11-07 22:35:17 +0000977}
978
Devang Patelca189262006-11-14 03:05:08 +0000979/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000980void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000981 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000982
Devang Patel8adae862007-07-20 18:04:54 +0000983 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000984
Devang Patel2ff44922007-04-16 20:39:59 +0000985 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000986 if (!TPM)
987 return;
988
Devang Patel17ad0962006-12-08 00:37:52 +0000989 TPM->collectLastUses(DeadPasses, P);
990
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000991 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000992 dbgs() << " -*- '" << P->getPassName();
993 dbgs() << "' is the last user of following pass instances.";
994 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000995 }
996
Yaron Kerenbd873192016-10-02 19:21:41 +0000997 for (Pass *P : DeadPasses)
998 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000999}
Devang Patel200d3052006-12-13 23:50:44 +00001000
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001001void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001002 enum PassDebuggingString DBG_STR) {
1003 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +00001004
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001005 {
1006 // If the pass crashes releasing memory, remember this.
1007 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +00001008 TimeRegion PassTimer(getPassTimer(P));
1009
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001010 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001011 }
1012
Owen Andersona7aed182010-08-06 18:33:48 +00001013 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001014 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001015 // Remove the pass itself (if it is not already removed).
1016 AvailableAnalysis.erase(PI);
1017
1018 // Remove all interfaces this pass implements, for which it is also
1019 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +00001020 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +00001021 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001022 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +00001023 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001024 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +00001025 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +00001026 }
Devang Patel17ad0962006-12-08 00:37:52 +00001027 }
Devang Patelca189262006-11-14 03:05:08 +00001028}
1029
Dan Gohmande6188a2010-08-12 23:50:08 +00001030/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +00001031/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +00001032void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +00001033 // This manager is going to manage pass P. Set up analysis resolver
1034 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001035 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001036 P->setResolver(AR);
1037
Devang Patelec2b9a72007-03-05 22:57:49 +00001038 // If a FunctionPass F is the last user of ModulePass info M
1039 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +00001040 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +00001041
Chris Lattner60987362009-03-06 05:53:14 +00001042 if (!ProcessAnalysis) {
1043 // Add pass
1044 PassVector.push_back(P);
1045 return;
Devang Patel90b05e02006-11-11 02:04:19 +00001046 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001047
Chris Lattner60987362009-03-06 05:53:14 +00001048 // At the moment, this pass is the last user of all required passes.
1049 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +00001050 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +00001051 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1052
1053 unsigned PDepth = this->getDepth();
1054
Chandler Carruth44a13852015-08-19 03:02:12 +00001055 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1056 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001057 unsigned RDepth = 0;
1058
Chandler Carruth44a13852015-08-19 03:02:12 +00001059 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1060 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001061 RDepth = DM.getDepth();
1062
1063 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001064 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001065 else if (PDepth > RDepth) {
1066 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001067 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001068 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001069 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001070 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001071 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001072 }
1073
1074 // Set P as P's last user until someone starts using P.
1075 // However, if P is a Pass Manager then it does not need
1076 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001077 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001078 LastUses.push_back(P);
1079 TPM->setLastUser(LastUses, P);
1080
1081 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001082 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001083 TPM->setLastUser(TransferLastUses, My_PM);
1084 TransferLastUses.clear();
1085 }
1086
Dan Gohman6304db32010-08-16 22:57:28 +00001087 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001088 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1089 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001090 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001091 this->addLowerLevelRequiredPass(P, AnalysisPass);
1092 }
1093
1094 // Take a note of analysis required and made available by this pass.
1095 // Remove the analysis not preserved by this pass
1096 removeNotPreservedAnalysis(P);
1097 recordAvailableAnalysis(P);
1098
Devang Patel8cad70d2006-11-11 01:51:02 +00001099 // Add pass
1100 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001101}
1102
Devang Patele64d3052007-04-16 20:12:57 +00001103
Chandler Carruth44a13852015-08-19 03:02:12 +00001104/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001105/// pass P and are available. Populate RP_NotAvail with analysis
1106/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001107void PMDataManager::collectRequiredAndUsedAnalyses(
1108 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1109 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001110 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001111
1112 for (const auto &UsedID : AnUsage->getUsedSet())
1113 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1114 UP.push_back(AnalysisPass);
1115
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001116 for (const auto &RequiredID : AnUsage->getRequiredSet())
1117 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001118 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001119 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001120 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001121
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001122 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1123 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001124 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001125 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001126 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001127}
1128
Devang Patel07f4f582006-11-14 21:49:36 +00001129// All Required analyses should be available to the pass as it runs! Here
1130// we fill in the AnalysisImpls member of the pass so that it can
1131// successfully use the getAnalysis() method to retrieve the
1132// implementations it needs.
1133//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001134void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001135 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1136
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001137 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1138 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001139 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001140 // This may be analysis pass that is initialized on the fly.
1141 // If that is not the case then it will raise an assert when it is used.
1142 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001143 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001144 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001145 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001146 }
1147}
1148
Devang Patel640c5bb2006-12-08 22:30:11 +00001149/// Find the pass that implements Analysis AID. If desired pass is not found
1150/// then return NULL.
1151Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1152
1153 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001154 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001155
1156 if (I != AvailableAnalysis.end())
1157 return I->second;
1158
1159 // Search Parents through TopLevelManager
1160 if (SearchParent)
1161 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001162
Craig Topperc6207612014-04-09 06:08:46 +00001163 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001164}
1165
Devang Patel991aeba2006-12-15 20:13:01 +00001166// Print list of passes that are last used by P.
1167void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1168
Devang Patel8adae862007-07-20 18:04:54 +00001169 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001170
1171 // If this is a on the fly manager then it does not have TPM.
1172 if (!TPM)
1173 return;
1174
Devang Patel991aeba2006-12-15 20:13:01 +00001175 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001176
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001177 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001178 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001179 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001180 }
1181}
1182
1183void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001184 for (Pass *P : PassVector) {
1185 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001186 PMD->dumpPassArguments();
1187 else
Owen Andersona7aed182010-08-06 18:33:48 +00001188 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001189 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001190 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001191 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001192 }
1193}
1194
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001195void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1196 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001197 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001198 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001199 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001200 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001201 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001202 switch (S1) {
1203 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001204 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001205 break;
1206 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001207 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001208 break;
1209 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001210 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001211 break;
1212 default:
1213 break;
1214 }
1215 switch (S2) {
1216 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001217 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001218 break;
1219 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001220 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001221 break;
1222 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001223 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001224 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001225 case ON_REGION_MSG:
1226 dbgs() << "' on Region '" << Msg << "'...\n";
1227 break;
Devang Patel003a5592007-03-05 20:01:30 +00001228 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001229 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001230 break;
1231 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001232 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001233 break;
1234 default:
1235 break;
1236 }
Devang Patel991aeba2006-12-15 20:13:01 +00001237}
1238
Chris Lattner4c1e9542009-03-06 06:45:05 +00001239void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001240 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001241 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001242
Chris Lattner4c493d92008-08-08 15:14:09 +00001243 AnalysisUsage analysisUsage;
1244 P->getAnalysisUsage(analysisUsage);
1245 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1246}
1247
Chris Lattner4c1e9542009-03-06 06:45:05 +00001248void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001249 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001250 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001251
Chris Lattner4c493d92008-08-08 15:14:09 +00001252 AnalysisUsage analysisUsage;
1253 P->getAnalysisUsage(analysisUsage);
1254 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1255}
1256
Chandler Carruth44a13852015-08-19 03:02:12 +00001257void PMDataManager::dumpUsedSet(const Pass *P) const {
1258 if (PassDebugging < Details)
1259 return;
1260
1261 AnalysisUsage analysisUsage;
1262 P->getAnalysisUsage(analysisUsage);
1263 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1264}
1265
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001266void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001267 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001268 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001269 if (Set.empty())
1270 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001271 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001272 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001273 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001274 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001275 if (!PInf) {
1276 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1277 // all drivers.
1278 dbgs() << " Uninitialized Pass";
1279 continue;
1280 }
Owen Andersona7aed182010-08-06 18:33:48 +00001281 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001282 }
David Greene994e1bb2010-01-05 01:30:02 +00001283 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001284}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001285
Devang Patel004937b2007-07-27 20:06:09 +00001286/// Add RequiredPass into list of lower level passes required by pass P.
1287/// RequiredPass is run on the fly by Pass Manager when P requests it
1288/// through getAnalysis interface.
1289/// This should be handled by specific pass manager.
1290void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1291 if (TPM) {
1292 TPM->dumpArguments();
1293 TPM->dumpPasses();
1294 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001295
Dan Gohmande6188a2010-08-12 23:50:08 +00001296 // Module Level pass may required Function Level analysis info
1297 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1298 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001299 // module level pass is requiring lower level analysis info managed by
1300 // lower level pass manager.
1301
1302 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001303 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001304 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001305#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001306 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1307 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001308#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001309 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001310}
1311
Owen Andersona7aed182010-08-06 18:33:48 +00001312Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001313 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001314}
1315
Devang Patele7599552007-01-12 18:52:44 +00001316// Destructor
1317PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001318 for (Pass *P : PassVector)
1319 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001320}
1321
Devang Patel9bdf7d42006-12-08 23:28:54 +00001322//===----------------------------------------------------------------------===//
1323// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001324// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1325Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001326 return PM.findAnalysisPass(ID, dir);
1327}
1328
Dan Gohmande6188a2010-08-12 23:50:08 +00001329Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001330 Function &F) {
1331 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1332}
1333
Devang Patela1514cb2006-12-07 19:39:39 +00001334//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001335// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001336
Dan Gohmande6188a2010-08-12 23:50:08 +00001337/// Execute all of the passes scheduled for execution by invoking
1338/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001339/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001340bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001341 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001342 return false;
1343
Devang Patele9585592006-12-08 01:38:28 +00001344 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001345 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001346
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001347 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1349 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001350 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001351
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001352 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001353 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001354
Devang Patelabfbe3b2006-12-16 00:56:26 +00001355 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001356
Chris Lattner4c1e9542009-03-06 06:45:05 +00001357 {
1358 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001359 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001360 TimeRegion PassTimer(getPassTimer(BP));
Jessica Paquettee49374d2018-05-18 17:26:39 +00001361 unsigned InstrCount = initSizeRemarkInfo(M);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001362 LocalChanged |= BP->runOnBasicBlock(BB);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001363 emitInstrCountChangedRemark(BP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001364 }
Devang Patel93a197c2006-12-14 00:08:04 +00001365
Dan Gohman74b189f2010-03-01 17:34:28 +00001366 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001367 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001368 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001369 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001370 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001371 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001372
Devang Patela273d1c2007-07-19 18:02:32 +00001373 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001374 removeNotPreservedAnalysis(BP);
1375 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001376 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001377 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001378
Bill Wendling6ce6d262009-12-25 13:50:18 +00001379 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001380}
1381
Devang Patel475c4532006-12-08 00:59:05 +00001382// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001383bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001384 bool Changed = false;
1385
Chris Lattner4c1e9542009-03-06 06:45:05 +00001386 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1387 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001388
1389 return Changed;
1390}
1391
Duncan Sands51495602009-02-13 09:42:34 +00001392bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001393 bool Changed = false;
1394
Pedro Artigas41b98842012-12-05 17:12:22 +00001395 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001396 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001397
1398 return Changed;
1399}
1400
Duncan Sands51495602009-02-13 09:42:34 +00001401bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001402 bool Changed = false;
1403
Devang Patelabfbe3b2006-12-16 00:56:26 +00001404 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1405 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001406 Changed |= BP->doInitialization(F);
1407 }
1408
1409 return Changed;
1410}
1411
Duncan Sands51495602009-02-13 09:42:34 +00001412bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001413 bool Changed = false;
1414
Devang Patelabfbe3b2006-12-16 00:56:26 +00001415 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1416 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001417 Changed |= BP->doFinalization(F);
1418 }
1419
1420 return Changed;
1421}
1422
1423
Devang Patela1514cb2006-12-07 19:39:39 +00001424//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001425// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001426
Devang Patel4e12f862006-11-08 10:44:40 +00001427/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001428FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001429 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001430 // FPM is the top level manager.
1431 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001432
Dan Gohman565df952008-03-13 02:08:36 +00001433 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001434 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001435}
1436
Devang Patelb67904d2006-12-13 02:36:01 +00001437FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001438 delete FPM;
1439}
1440
Dan Gohmande6188a2010-08-12 23:50:08 +00001441void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001442 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001443}
1444
Devang Patel9f3083e2006-11-15 19:39:54 +00001445/// run - Execute all of the passes scheduled for execution. Keep
1446/// track of whether any of the passes modifies the function, and if
1447/// so, return true.
1448///
Devang Patelb67904d2006-12-13 02:36:01 +00001449bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001450 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1451 report_fatal_error("Error reading bitcode file: " + EIB.message());
1452 });
Devang Patel272908d2006-12-08 22:57:48 +00001453 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001454}
1455
1456
Devang Patelff631ae2006-11-15 01:27:05 +00001457/// doInitialization - Run all of the initializers for the function passes.
1458///
Devang Patelb67904d2006-12-13 02:36:01 +00001459bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001460 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001461}
1462
Dan Gohmane6656eb2007-07-30 14:51:13 +00001463/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001464///
Devang Patelb67904d2006-12-13 02:36:01 +00001465bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001466 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001467}
1468
Devang Patela1514cb2006-12-07 19:39:39 +00001469//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001470// FunctionPassManagerImpl implementation
1471//
Duncan Sands51495602009-02-13 09:42:34 +00001472bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001473 bool Changed = false;
1474
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001475 dumpArguments();
1476 dumpPasses();
1477
Yaron Keren4849aa32015-06-05 17:48:47 +00001478 for (ImmutablePass *ImPass : getImmutablePasses())
1479 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001480
Chris Lattner4c1e9542009-03-06 06:45:05 +00001481 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1482 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483
1484 return Changed;
1485}
1486
Duncan Sands51495602009-02-13 09:42:34 +00001487bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001488 bool Changed = false;
1489
Pedro Artigas41b98842012-12-05 17:12:22 +00001490 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001491 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001492
Yaron Keren4849aa32015-06-05 17:48:47 +00001493 for (ImmutablePass *ImPass : getImmutablePasses())
1494 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001495
Devang Patel67d6a5e2006-12-19 19:46:59 +00001496 return Changed;
1497}
1498
Devang Patelec9c58f2009-04-01 22:34:41 +00001499/// cleanup - After running all passes, clean up pass manager cache.
1500void FPPassManager::cleanup() {
1501 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1502 FunctionPass *FP = getContainedPass(Index);
1503 AnalysisResolver *AR = FP->getResolver();
1504 assert(AR && "Analysis Resolver is not set");
1505 AR->clearAnalysisImpls();
1506 }
1507}
1508
Torok Edwin24c78352009-06-29 18:49:09 +00001509void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1510 if (!wasRun)
1511 return;
1512 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1513 FPPassManager *FPPM = getContainedManager(Index);
1514 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1515 FPPM->getContainedPass(Index)->releaseMemory();
1516 }
1517 }
Torok Edwin896556e2009-06-29 21:05:10 +00001518 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001519}
1520
Devang Patel67d6a5e2006-12-19 19:46:59 +00001521// Execute all the passes managed by this top level manager.
1522// Return true if any function is modified by a pass.
1523bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001524 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001525 TimingInfo::createTheTimeInfo();
1526
Devang Patele3068402006-12-21 00:16:50 +00001527 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001528 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001529 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001530 F.getContext().yield();
1531 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001532
1533 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1534 getContainedManager(Index)->cleanup();
1535
Torok Edwin24c78352009-06-29 18:49:09 +00001536 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537 return Changed;
1538}
1539
1540//===----------------------------------------------------------------------===//
1541// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001542
Devang Patel8c78a0b2007-05-03 01:11:54 +00001543char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001544/// Print passes managed by this manager
1545void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001546 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001547 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1548 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001549 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001550 dumpLastUses(FP, Offset+1);
1551 }
1552}
1553
1554
Dan Gohmande6188a2010-08-12 23:50:08 +00001555/// Execute all of the passes scheduled for execution by invoking
1556/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001557/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001559 if (F.isDeclaration())
1560 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001561
1562 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001563 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001564 // Collect inherited analysis from Module level pass manager.
1565 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001566
Devang Patelabfbe3b2006-12-16 00:56:26 +00001567 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1568 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001569 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001570
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001571 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001572 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001573
Devang Patelabfbe3b2006-12-16 00:56:26 +00001574 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001575
Chris Lattner4c1e9542009-03-06 06:45:05 +00001576 {
1577 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001578 TimeRegion PassTimer(getPassTimer(FP));
Jessica Paquettee49374d2018-05-18 17:26:39 +00001579 unsigned InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001580 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001581 emitInstrCountChangedRemark(FP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001582 }
Devang Patel93a197c2006-12-14 00:08:04 +00001583
Dan Gohman74b189f2010-03-01 17:34:28 +00001584 Changed |= LocalChanged;
1585 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001586 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001587 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001588 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001589
Devang Patela273d1c2007-07-19 18:02:32 +00001590 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001591 removeNotPreservedAnalysis(FP);
1592 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001593 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001594 }
1595 return Changed;
1596}
1597
Devang Patel67d6a5e2006-12-19 19:46:59 +00001598bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001599 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001600
Serge Pavloved5eb932017-01-15 10:23:18 +00001601 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001602 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001603
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001604 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001605}
1606
Duncan Sands51495602009-02-13 09:42:34 +00001607bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001608 bool Changed = false;
1609
Chris Lattner4c1e9542009-03-06 06:45:05 +00001610 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1611 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001612
Devang Patelff631ae2006-11-15 01:27:05 +00001613 return Changed;
1614}
1615
Duncan Sands51495602009-02-13 09:42:34 +00001616bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001617 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001618
1619 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001620 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001621
Devang Patelff631ae2006-11-15 01:27:05 +00001622 return Changed;
1623}
1624
Devang Patela1514cb2006-12-07 19:39:39 +00001625//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001626// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001627
Dan Gohmande6188a2010-08-12 23:50:08 +00001628/// Execute all of the passes scheduled for execution by invoking
1629/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001630/// the module, and if so, return true.
1631bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001632MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001633 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001634
Torok Edwin24c78352009-06-29 18:49:09 +00001635 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001636 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1637 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001638 Changed |= FPP->doInitialization(M);
1639 }
1640
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001641 // Initialize module passes
1642 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1643 Changed |= getContainedPass(Index)->doInitialization(M);
1644
Devang Patelabfbe3b2006-12-16 00:56:26 +00001645 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1646 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001647 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001648
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001649 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001650 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001651
Devang Patelabfbe3b2006-12-16 00:56:26 +00001652 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001653
Chris Lattner4c1e9542009-03-06 06:45:05 +00001654 {
1655 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001656 TimeRegion PassTimer(getPassTimer(MP));
1657
Jessica Paquettee49374d2018-05-18 17:26:39 +00001658 unsigned InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001659 LocalChanged |= MP->runOnModule(M);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001660 emitInstrCountChangedRemark(MP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001661 }
Devang Patel93a197c2006-12-14 00:08:04 +00001662
Dan Gohman74b189f2010-03-01 17:34:28 +00001663 Changed |= LocalChanged;
1664 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001665 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001666 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001667 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001668 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001669
Devang Patela273d1c2007-07-19 18:02:32 +00001670 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001671 removeNotPreservedAnalysis(MP);
1672 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001673 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001674 }
Torok Edwin24c78352009-06-29 18:49:09 +00001675
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001676 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001677 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001678 Changed |= getContainedPass(Index)->doFinalization(M);
1679
Torok Edwin24c78352009-06-29 18:49:09 +00001680 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001681 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1682 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001683 // We don't know when is the last time an on-the-fly pass is run,
1684 // so we need to releaseMemory / finalize here
1685 FPP->releaseMemoryOnTheFly();
1686 Changed |= FPP->doFinalization(M);
1687 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001688
Devang Patel05e1a972006-11-07 22:03:15 +00001689 return Changed;
1690}
1691
Devang Patele64d3052007-04-16 20:12:57 +00001692/// Add RequiredPass into list of lower level passes required by pass P.
1693/// RequiredPass is run on the fly by Pass Manager when P requests it
1694/// through getAnalysis interface.
1695void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001696 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1697 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001698 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001699 RequiredPass->getPotentialPassManagerType()) &&
1700 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001701 if (!RequiredPass)
1702 return;
Devang Patele64d3052007-04-16 20:12:57 +00001703
Devang Patel68f72b12007-04-26 17:50:19 +00001704 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001705 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001706 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001707 // FPP is the top level manager.
1708 FPP->setTopLevelManager(FPP);
1709
Devang Patel69e9f6d2007-04-16 20:27:05 +00001710 OnTheFlyManagers[P] = FPP;
1711 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001712 const PassInfo *RequiredPassPI =
1713 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001714
Craig Topperc6207612014-04-09 06:08:46 +00001715 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001716 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1717 FoundPass =
1718 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001719 }
Andrew Trick02066f22014-04-08 03:40:34 +00001720 if (!FoundPass) {
1721 FoundPass = RequiredPass;
1722 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001723 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001724 FPP->add(RequiredPass);
1725 }
1726 // Register P as the last user of FoundPass or RequiredPass.
1727 SmallVector<Pass *, 1> LU;
1728 LU.push_back(FoundPass);
1729 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001730}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001731
Dan Gohmande6188a2010-08-12 23:50:08 +00001732/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001733/// required by module pass MP. Instantiate analysis pass, by using
1734/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001735Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001736 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001737 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001738
Torok Edwin24c78352009-06-29 18:49:09 +00001739 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001740 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001741 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001742}
1743
1744
Devang Patela1514cb2006-12-07 19:39:39 +00001745//===----------------------------------------------------------------------===//
1746// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001747
Devang Patelab97cf42006-12-13 00:09:23 +00001748//
Devang Patelc290c8a2006-11-07 22:23:34 +00001749/// run - Execute all of the passes scheduled for execution. Keep track of
1750/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001751bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001752 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001753 TimingInfo::createTheTimeInfo();
1754
Devang Patelcfd70c42006-12-13 22:10:00 +00001755 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001756 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001757
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001758 for (ImmutablePass *ImPass : getImmutablePasses())
1759 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001760
Devang Patele3068402006-12-21 00:16:50 +00001761 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001762 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001763 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001764 M.getContext().yield();
1765 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001766
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001767 for (ImmutablePass *ImPass : getImmutablePasses())
1768 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001769
Devang Patelc290c8a2006-11-07 22:23:34 +00001770 return Changed;
1771}
Devang Patel376fefa2006-11-08 10:29:57 +00001772
Devang Patela1514cb2006-12-07 19:39:39 +00001773//===----------------------------------------------------------------------===//
1774// PassManager implementation
1775
Devang Patel376fefa2006-11-08 10:29:57 +00001776/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001777PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001778 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001779 // PM is the top level manager
1780 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001781}
1782
Devang Patelb67904d2006-12-13 02:36:01 +00001783PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001784 delete PM;
1785}
1786
Chris Lattner60987362009-03-06 05:53:14 +00001787void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001788 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001789}
1790
1791/// run - Execute all of the passes scheduled for execution. Keep track of
1792/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001793bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001794 return PM->run(M);
1795}
1796
Devang Patelb8817b92006-12-14 00:59:42 +00001797//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001798// TimingInfo implementation
1799
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001800bool llvm::TimePassesIsEnabled = false;
Zachary Turner8065f0b2017-12-01 00:53:10 +00001801static cl::opt<bool, true> EnableTiming(
1802 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
1803 cl::desc("Time each pass, printing elapsed time for each on exit"));
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001804
Devang Patelb8817b92006-12-14 00:59:42 +00001805// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001806// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001807// null. It may be called multiple times.
1808void TimingInfo::createTheTimeInfo() {
1809 if (!TimePassesIsEnabled || TheTimeInfo) return;
1810
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001811 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001812 // This guarantees that the object will be constructed before static globals,
1813 // thus it will be destroyed before them.
1814 static ManagedStatic<TimingInfo> TTI;
1815 TheTimeInfo = &*TTI;
1816}
1817
Devang Patel1c3633e2007-01-29 23:10:37 +00001818/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001819Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001820 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001821 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001822 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001823}
1824
James Henderson852f6fd2017-05-16 09:43:21 +00001825/// If timing is enabled, report the times collected up to now and then reset
1826/// them.
1827void llvm::reportAndResetTimings() {
1828 if (TheTimeInfo)
1829 TheTimeInfo->print();
1830}
1831
Devang Patel1c56a632007-01-08 19:29:38 +00001832//===----------------------------------------------------------------------===//
1833// PMStack implementation
1834//
Devang Patelad98d232007-01-11 22:15:30 +00001835
Devang Patel1c56a632007-01-08 19:29:38 +00001836// Pop Pass Manager from the stack and clear its analysis info.
1837void PMStack::pop() {
1838
1839 PMDataManager *Top = this->top();
1840 Top->initializeAnalysisInfo();
1841
1842 S.pop_back();
1843}
1844
1845// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001846void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001847 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001848 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001849
Chris Lattner60987362009-03-06 05:53:14 +00001850 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001851 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1852 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001853 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001854
Chris Lattner60987362009-03-06 05:53:14 +00001855 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001856 TPM->addIndirectPassManager(PM);
1857 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001858 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001859 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001860 assert((PM->getPassManagerType() == PMT_ModulePassManager
1861 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001862 && "pushing bad pass manager to PMStack");
1863 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001864 }
1865
Devang Patel15701b52007-01-11 00:19:00 +00001866 S.push_back(PM);
1867}
1868
1869// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001870LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001871 for (PMDataManager *Manager : S)
1872 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001873
Devang Patel15701b52007-01-11 00:19:00 +00001874 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001875 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001876}
1877
Devang Patel1c56a632007-01-08 19:29:38 +00001878/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001879/// add self into that manager.
1880void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001881 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001882 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001883 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001884 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1885 if (TopPMType == PreferredType)
1886 break; // We found desired pass manager
1887 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001888 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001889 else
1890 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001891 }
Devang Patel18ff6362008-09-09 21:38:40 +00001892 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001893 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001894}
1895
Devang Patel3312f752007-01-16 21:43:18 +00001896/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001897/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001898void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001899 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001900
Andrew Trickcbc845f2012-02-01 07:16:20 +00001901 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001902 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001903 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1904 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001905 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001906 break;
Devang Patel3312f752007-01-16 21:43:18 +00001907 }
Devang Patel3312f752007-01-16 21:43:18 +00001908
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001909 // Create new Function Pass Manager if needed.
1910 FPPassManager *FPP;
1911 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1912 FPP = (FPPassManager *)PMS.top();
1913 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001914 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1915 PMDataManager *PMD = PMS.top();
1916
1917 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001918 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001919 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001920
1921 // [2] Set up new manager's top level manager
1922 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1923 TPM->addIndirectPassManager(FPP);
1924
1925 // [3] Assign manager to manage this new manager. This may create
1926 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001927 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001928
1929 // [4] Push new manager into PMS
1930 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001931 }
1932
Devang Patel3312f752007-01-16 21:43:18 +00001933 // Assign FPP as the manager of this pass.
1934 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001935}
1936
Devang Patel3312f752007-01-16 21:43:18 +00001937/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001938/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001939void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001940 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001941 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001942
Devang Patel15701b52007-01-11 00:19:00 +00001943 // Basic Pass Manager is a leaf pass manager. It does not handle
1944 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001945 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001946 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1947 BBP = (BBPassManager *)PMS.top();
1948 } else {
1949 // If leaf manager is not Basic Block Pass manager then create new
1950 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001951 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1952 PMDataManager *PMD = PMS.top();
1953
1954 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001955 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001956
1957 // [2] Set up new manager's top level manager
1958 // Basic Block Pass Manager does not live by itself
1959 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1960 TPM->addIndirectPassManager(BBP);
1961
Devang Patel15701b52007-01-11 00:19:00 +00001962 // [3] Assign manager to manage this new manager. This may create
1963 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001964 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001965
Devang Patel3312f752007-01-16 21:43:18 +00001966 // [4] Push new manager into PMS
1967 PMS.push(BBP);
1968 }
Devang Patel1c56a632007-01-08 19:29:38 +00001969
Devang Patel3312f752007-01-16 21:43:18 +00001970 // Assign BBP as the manager of this pass.
1971 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001972}
1973
Dan Gohmand3a20c92008-03-11 16:41:42 +00001974PassManagerBase::~PassManagerBase() {}