blob: 54d602d926e5dcc9b7735bfd6b26b51262bd23a8 [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel6e5a1132006-11-07 21:31:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruth7caea412013-11-09 12:26:54 +000010// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth7caea412013-11-09 12:26:54 +000014#include "llvm/IR/LegacyPassManager.h"
Florian Hahna4ffa3a2018-05-24 21:33:17 +000015#include "llvm/ADT/MapVector.h"
James Henderson852f6fd2017-05-16 09:43:21 +000016#include "llvm/ADT/Statistic.h"
Jessica Paquettee49374d2018-05-18 17:26:39 +000017#include "llvm/IR/DiagnosticInfo.h"
Pavel Labathec534e62016-10-25 16:20:07 +000018#include "llvm/IR/IRPrintingPasses.h"
19#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000020#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000021#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/IR/Module.h"
Pavel Labathec534e62016-10-25 16:20:07 +000023#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000024#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000025#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000026#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000027#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000028#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000029#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/Timer.h"
31#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000032#include <algorithm>
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.
Xin Tong023e25a2018-07-22 05:27:41 +0000140 return M.getInstructionCount();
Jessica Paquettee49374d2018-05-18 17:26:39 +0000141}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000142
Jessica Paquettee49374d2018-05-18 17:26:39 +0000143void PMDataManager::emitInstrCountChangedRemark(Pass *P, Module &M,
144 unsigned CountBefore) {
Jessica Paquettee49374d2018-05-18 17:26:39 +0000145 // We need a function containing at least one basic block in order to output
146 // remarks. Since it's possible that the first function in the module doesn't
147 // actually contain a basic block, we have to go and find one that's suitable
148 // for emitting remarks.
149 auto It = std::find_if(M.begin(), M.end(),
150 [](const Function &Fn) { return !Fn.empty(); });
151
152 // Didn't find a function. Quit.
153 if (It == M.end())
154 return;
155
156 // We found a function containing at least one basic block.
157 Function *F = &*It;
158
159 // How many instructions are in the module now?
160 unsigned CountAfter = M.getInstructionCount();
161
162 // If there was no change, don't emit a remark.
163 if (CountBefore == CountAfter)
164 return;
165
166 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
167 // that the only passes that return non-null with getAsPMDataManager are pass
168 // managers.) The reason we have to do this is to avoid emitting remarks for
169 // CGSCC passes.
170 if (P->getAsPMDataManager())
171 return;
172
173 // Compute a possibly negative delta between the instruction count before
174 // running P, and after running P.
Jessica Paquettec6048172018-05-18 20:04:21 +0000175 int64_t Delta =
176 static_cast<int64_t>(CountAfter) - static_cast<int64_t>(CountBefore);
Jessica Paquettee49374d2018-05-18 17:26:39 +0000177
178 BasicBlock &BB = *F->begin();
179 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
180 DiagnosticLocation(), &BB);
181 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
182 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
183 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
184 << ": IR instruction count changed from "
185 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
186 << " to "
187 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
188 << "; Delta: "
189 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
190 F->getContext().diagnose(R); // Not using ORE for layering reasons.
191}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000192
Chris Lattner4c1e9542009-03-06 06:45:05 +0000193void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000194 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000195 OS << "Releasing pass '";
196 else
197 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000198
Chris Lattner4c1e9542009-03-06 06:45:05 +0000199 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000200
Chris Lattner4c1e9542009-03-06 06:45:05 +0000201 if (M) {
202 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
203 return;
204 }
Craig Topperc6207612014-04-09 06:08:46 +0000205 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000206 OS << '\n';
207 return;
208 }
209
Dan Gohman79fc0e92009-03-10 18:47:59 +0000210 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000211 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000212 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000213 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000214 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000215 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000216 OS << "value";
217
218 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000219 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000220 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000221}
222
223
Devang Patelffca9102006-12-15 19:39:30 +0000224namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000225//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000227//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000229/// pass together and sequence them to process one basic block before
230/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000231class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000232
233public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000234 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000235 explicit BBPassManager()
236 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000237
Devang Patelca58e352006-11-08 10:05:38 +0000238 /// Execute all of the passes scheduled for execution. Keep track of
239 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000240 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000241
Devang Patelf9d96b92006-12-07 19:57:52 +0000242 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000243 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000244 Info.setPreservesAll();
245 }
246
Craig Topperf398d7c2014-03-05 06:35:38 +0000247 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000248 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000249 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000250 bool doFinalization(Function &F);
251
Craig Topperf398d7c2014-03-05 06:35:38 +0000252 PMDataManager *getAsPMDataManager() override { return this; }
253 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000254
Mehdi Amini117296c2016-10-01 02:56:57 +0000255 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000256
Devang Pateleda56172006-12-12 23:34:33 +0000257 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000258 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000259 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000260 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
261 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000262 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000263 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000264 }
265 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000266
267 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000268 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000269 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
270 return BP;
271 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000272
Craig Topperf398d7c2014-03-05 06:35:38 +0000273 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000274 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000275 }
Devang Patelca58e352006-11-08 10:05:38 +0000276};
277
Devang Patel8c78a0b2007-05-03 01:11:54 +0000278char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000279} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280
Devang Patele7599552007-01-12 18:52:44 +0000281namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000282namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000283//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000285//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000286/// FunctionPassManagerImpl manages FPPassManagers
287class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000288 public PMDataManager,
289 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000290 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000291private:
292 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000293public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000294 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000295 explicit FunctionPassManagerImpl() :
296 Pass(PT_PassManager, ID), PMDataManager(),
297 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000298
Matthias Braun0880c602014-12-12 01:27:01 +0000299 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000300 void add(Pass *P) {
301 schedulePass(P);
302 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000303
304 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000305 Pass *createPrinterPass(raw_ostream &O,
306 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000307 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000308 }
309
Torok Edwin24c78352009-06-29 18:49:09 +0000310 // Prepare for running an on the fly pass, freeing memory if needed
311 // from a previous run.
312 void releaseMemoryOnTheFly();
313
Devang Patel67d6a5e2006-12-19 19:46:59 +0000314 /// run - Execute all of the passes scheduled for execution. Keep track of
315 /// whether any of the passes modifies the module, and if so, return true.
316 bool run(Function &F);
317
318 /// doInitialization - Run all of the initializers for the function passes.
319 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000320 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000321
Dan Gohmane6656eb2007-07-30 14:51:13 +0000322 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000323 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000324 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000325
Dan Gohmande6188a2010-08-12 23:50:08 +0000326
Craig Topperf398d7c2014-03-05 06:35:38 +0000327 PMDataManager *getAsPMDataManager() override { return this; }
328 Pass *getAsPass() override { return this; }
329 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000330 return PMT_FunctionPassManager;
331 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000332
Devang Patel67d6a5e2006-12-19 19:46:59 +0000333 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000334 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000335 Info.setPreservesAll();
336 }
337
Devang Patel67d6a5e2006-12-19 19:46:59 +0000338 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000339 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000340 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
341 return FP;
342 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000343};
344
David Blaikiea379b1812011-12-20 02:50:00 +0000345void FunctionPassManagerImpl::anchor() {}
346
Devang Patel8c78a0b2007-05-03 01:11:54 +0000347char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000348} // End of legacy namespace
349} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000350
Chandler Carruth7caea412013-11-09 12:26:54 +0000351namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000352//===----------------------------------------------------------------------===//
353// MPPassManager
354//
355/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000356/// It batches all Module passes and function pass managers together and
357/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000358class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000359public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000360 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000361 explicit MPPassManager() :
362 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000363
364 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000365 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000366 for (auto &OnTheFlyManager : OnTheFlyManagers) {
367 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000368 delete FPP;
369 }
370 }
371
Dan Gohmande6188a2010-08-12 23:50:08 +0000372 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000373 Pass *createPrinterPass(raw_ostream &O,
374 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000375 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000376 }
377
Devang Patelca58e352006-11-08 10:05:38 +0000378 /// run - Execute all of the passes scheduled for execution. Keep track of
379 /// whether any of the passes modifies the module, and if so, return true.
380 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000381
Pedro Artigase4348b02012-12-03 21:56:57 +0000382 using llvm::Pass::doInitialization;
383 using llvm::Pass::doFinalization;
384
Devang Patelf9d96b92006-12-07 19:57:52 +0000385 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000386 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000387 Info.setPreservesAll();
388 }
389
Devang Patele64d3052007-04-16 20:12:57 +0000390 /// Add RequiredPass into list of lower level passes required by pass P.
391 /// RequiredPass is run on the fly by Pass Manager when P requests it
392 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000393 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000394
Dan Gohmande6188a2010-08-12 23:50:08 +0000395 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000396 /// required by module pass MP. Instantiate analysis pass, by using
397 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000398 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000399
Mehdi Amini117296c2016-10-01 02:56:57 +0000400 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000401
Craig Topperf398d7c2014-03-05 06:35:38 +0000402 PMDataManager *getAsPMDataManager() override { return this; }
403 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000404
Devang Pateleda56172006-12-12 23:34:33 +0000405 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000406 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000407 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000408 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
409 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000410 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000411 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
412 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000413 if (I != OnTheFlyManagers.end())
414 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000415 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000416 }
417 }
418
Devang Patelabfbe3b2006-12-16 00:56:26 +0000419 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000420 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000421 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000422 }
423
Craig Topperf398d7c2014-03-05 06:35:38 +0000424 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000425 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000426 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000427
428 private:
429 /// Collection of on the fly FPPassManagers. These managers manage
430 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000431 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000432};
433
Devang Patel8c78a0b2007-05-03 01:11:54 +0000434char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000435} // End anonymous namespace
436
437namespace llvm {
438namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000439//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000440// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000441//
Devang Patel09f162c2007-05-01 21:15:47 +0000442
Devang Patel67d6a5e2006-12-19 19:46:59 +0000443/// PassManagerImpl manages MPPassManagers
444class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000445 public PMDataManager,
446 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000447 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000448
449public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000450 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000451 explicit PassManagerImpl() :
452 Pass(PT_PassManager, ID), PMDataManager(),
453 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000454
Matthias Braun0880c602014-12-12 01:27:01 +0000455 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000456 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000457 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000458 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000459
460 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000461 Pass *createPrinterPass(raw_ostream &O,
462 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000463 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000464 }
465
Devang Patel376fefa2006-11-08 10:29:57 +0000466 /// run - Execute all of the passes scheduled for execution. Keep track of
467 /// whether any of the passes modifies the module, and if so, return true.
468 bool run(Module &M);
469
Pedro Artigase4348b02012-12-03 21:56:57 +0000470 using llvm::Pass::doInitialization;
471 using llvm::Pass::doFinalization;
472
Devang Patelf9d96b92006-12-07 19:57:52 +0000473 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000474 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000475 Info.setPreservesAll();
476 }
477
Craig Topperf398d7c2014-03-05 06:35:38 +0000478 PMDataManager *getAsPMDataManager() override { return this; }
479 Pass *getAsPass() override { return this; }
480 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000481 return PMT_ModulePassManager;
482 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000483
Devang Patel67d6a5e2006-12-19 19:46:59 +0000484 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000485 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000486 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
487 return MP;
488 }
Devang Patel376fefa2006-11-08 10:29:57 +0000489};
490
David Blaikiea379b1812011-12-20 02:50:00 +0000491void PassManagerImpl::anchor() {}
492
Devang Patel8c78a0b2007-05-03 01:11:54 +0000493char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000494} // End of legacy namespace
495} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000496
497namespace {
498
499//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000500/// TimingInfo Class - This class is used to calculate information about the
501/// amount of time each pass takes to execute. This only happens when
502/// -time-passes is enabled on the command line.
503///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000504
Owen Anderson5a6960f2009-06-18 20:51:00 +0000505static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000506
Nick Lewycky02d5f772009-10-25 06:33:48 +0000507class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000508 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000509 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000510public:
511 // Use 'create' member to get this.
Matthias Braun9f15a792016-11-18 19:43:18 +0000512 TimingInfo() : TG("pass", "... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000513
Devang Patel1c3633e2007-01-29 23:10:37 +0000514 // TimingDtor - Print out information about timing information
515 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000516 // Delete all of the timers, which accumulate their info into the
517 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000518 for (auto &I : TimingData)
519 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000520 // TimerGroup is deleted next, printing the report.
521 }
522
523 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000524 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000525 // null. It may be called multiple times.
526 static void createTheTimeInfo();
527
James Henderson852f6fd2017-05-16 09:43:21 +0000528 // print - Prints out timing information and then resets the timers.
529 void print() {
530 TG.print(*CreateInfoOutputFile());
531 }
532
Chris Lattner707431c2010-03-30 04:03:22 +0000533 /// getPassTimer - Return the timer for the specified pass if it exists.
534 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000535 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000536 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000537
Owen Anderson5c96ef72009-07-07 18:33:04 +0000538 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000539 Timer *&T = TimingData[P];
Matthias Braun9f15a792016-11-18 19:43:18 +0000540 if (!T) {
541 StringRef PassName = P->getPassName();
Francis Visoiu Mistrih03185792018-06-13 21:03:56 +0000542 StringRef PassArgument;
543 if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
544 PassArgument = PI->getPassArgument();
545 T = new Timer(PassArgument.empty() ? PassName : PassArgument, PassName,
546 TG);
Matthias Braun9f15a792016-11-18 19:43:18 +0000547 }
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000548 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000549 }
550};
551
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000552} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000553
Dan Gohmand78c4002008-05-13 00:00:25 +0000554static TimingInfo *TheTimeInfo;
555
Devang Patela1514cb2006-12-07 19:39:39 +0000556//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000557// PMTopLevelManager implementation
558
Devang Patel4268fc02007-01-16 02:00:38 +0000559/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000560PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
561 PMDM->setTopLevelManager(this);
562 addPassManager(PMDM);
563 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000564}
565
Devang Patelafb1f3622006-12-12 22:35:25 +0000566/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000567void
Bill Wendlingea857e12012-05-14 07:53:40 +0000568PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000569 unsigned PDepth = 0;
570 if (P->getResolver())
571 PDepth = P->getResolver()->getPMDataManager().getDepth();
572
Yaron Keren4849aa32015-06-05 17:48:47 +0000573 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000574 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000575
Devang Patel01919d22007-03-08 19:05:01 +0000576 if (P == AP)
577 continue;
578
Tobias Grosserf07426b2011-01-20 21:03:22 +0000579 // Update the last users of passes that are required transitive by AP.
580 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
581 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
582 SmallVector<Pass *, 12> LastUses;
583 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000584 for (AnalysisID ID : IDs) {
585 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000586 assert(AnalysisPass && "Expected analysis pass to exist.");
587 AnalysisResolver *AR = AnalysisPass->getResolver();
588 assert(AR && "Expected analysis resolver to exist.");
589 unsigned APDepth = AR->getPMDataManager().getDepth();
590
591 if (PDepth == APDepth)
592 LastUses.push_back(AnalysisPass);
593 else if (PDepth > APDepth)
594 LastPMUses.push_back(AnalysisPass);
595 }
596
597 setLastUser(LastUses, P);
598
599 // If this pass has a corresponding pass manager, push higher level
600 // analysis to this pass manager.
601 if (P->getResolver())
602 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
603
604
Devang Patelafb1f3622006-12-12 22:35:25 +0000605 // If AP is the last user of other passes then make P last user of
606 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000607 for (auto LU : LastUser) {
608 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000609 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000610 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000611 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000612 }
613 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000614}
615
616/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000617void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000618 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000619 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000620 InversedLastUser.find(P);
621 if (DMI == InversedLastUser.end())
622 return;
623
624 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000625 for (Pass *LUP : LU) {
626 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000627 }
628
Devang Patelafb1f3622006-12-12 22:35:25 +0000629}
630
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000631AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000632 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000633 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000634 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000635 AnUsage = DMI->second;
636 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000637 // Look up the analysis usage from the pass instance (different instances
638 // of the same pass can produce different results), but unique the
639 // resulting object to reduce memory usage. This helps to greatly reduce
640 // memory usage when we have many instances of only a few pass types
641 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
642 // of dependencies.
643 AnalysisUsage AU;
644 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000645
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000646 AUFoldingSetNode* Node = nullptr;
647 FoldingSetNodeID ID;
648 AUFoldingSetNode::Profile(ID, AU);
649 void *IP = nullptr;
650 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
651 Node = N;
652 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000653 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000654 UniqueAnalysisUsages.InsertNode(Node, IP);
655 }
656 assert(Node && "cached analysis usage must be non null");
657
658 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000659 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000660 }
661 return AnUsage;
662}
663
Devang Patelafb1f3622006-12-12 22:35:25 +0000664/// Schedule pass P for execution. Make sure that passes required by
665/// P are run before P is run. Update analysis info maintained by
666/// the manager. Remove dead passes. This is a recursive function.
667void PMTopLevelManager::schedulePass(Pass *P) {
668
Devang Patel3312f752007-01-16 21:43:18 +0000669 // TODO : Allocate function manager for this pass, other wise required set
670 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000671
Devang Pateld74ede72007-03-06 01:06:16 +0000672 // Give pass a chance to prepare the stage.
673 P->preparePassManager(activeStack);
674
Devang Patel864970e2008-03-18 00:39:19 +0000675 // If P is an analysis pass and it is available then do not
676 // generate the analysis again. Stale analysis info should not be
677 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000678 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000679 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000680 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000681 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000682 }
Devang Patel864970e2008-03-18 00:39:19 +0000683
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000684 AnalysisUsage *AnUsage = findAnalysisUsage(P);
685
Devang Patelfdee7032008-08-14 23:07:48 +0000686 bool checkAnalysis = true;
687 while (checkAnalysis) {
688 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000689
Devang Patelfdee7032008-08-14 23:07:48 +0000690 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000691 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000692
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000693 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000694 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000695 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000696
Craig Topperc6207612014-04-09 06:08:46 +0000697 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000698 // Pass P is not in the global PassRegistry
699 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
700 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
701 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000702 for (const AnalysisID ID2 : RequiredSet) {
703 if (ID == ID2)
704 break;
705 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000706 if (AnalysisPass2) {
707 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000708 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000709 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
710 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
711 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
712 }
713 }
714 }
715
Andrew Trick6bbaf132011-06-03 00:48:58 +0000716 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000717 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000718 if (P->getPotentialPassManagerType () ==
719 AnalysisPass->getPotentialPassManagerType())
720 // Schedule analysis pass that is managed by the same pass manager.
721 schedulePass(AnalysisPass);
722 else if (P->getPotentialPassManagerType () >
723 AnalysisPass->getPotentialPassManagerType()) {
724 // Schedule analysis pass that is managed by a new manager.
725 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000726 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000727 // are already checked are still available.
728 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000729 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000730 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000731 // passes are run on the fly.
732 delete AnalysisPass;
733 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000734 }
735 }
736
737 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000738 if (ImmutablePass *IP = P->getAsImmutablePass()) {
739 // P is a immutable pass and it will be managed by this
740 // top level manager. Set up analysis resolver to connect them.
741 PMDataManager *DM = getAsPMDataManager();
742 AnalysisResolver *AR = new AnalysisResolver(*DM);
743 P->setResolver(AR);
744 DM->initializeAnalysisImpl(P);
745 addImmutablePass(IP);
746 DM->recordAvailableAnalysis(IP);
747 return;
748 }
749
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000750 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000751 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000752 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000753 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
754 }
755
756 // Add the requested pass to the best available pass manager.
757 P->assignPassManager(activeStack, getTopLevelPassManagerType());
758
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000759 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000760 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000761 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000762 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
763 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000764}
765
766/// Find the pass that implements Analysis AID. Search immutable
767/// passes and all pass managers. If desired pass is not found
768/// then return NULL.
769Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000770 // For immutable passes we have a direct mapping from ID to pass, so check
771 // that first.
772 if (Pass *P = ImmutablePassMap.lookup(AID))
773 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000774
Devang Patelcd6ba152006-12-12 22:50:05 +0000775 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000776 for (PMDataManager *PassManager : PassManagers)
777 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000778 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000779
780 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000781 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
782 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000783 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000784
Craig Topperc6207612014-04-09 06:08:46 +0000785 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000786}
787
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000788const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
789 const PassInfo *&PI = AnalysisPassInfos[AID];
790 if (!PI)
791 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
792 else
793 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
794 "The pass info pointer changed for an analysis ID!");
795
796 return PI;
797}
798
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000799void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
800 P->initializePass();
801 ImmutablePasses.push_back(P);
802
803 // Add this pass to the map from its analysis ID. We clobber any prior runs
804 // of the pass in the map so that the last one added is the one found when
805 // doing lookups.
806 AnalysisID AID = P->getPassID();
807 ImmutablePassMap[AID] = P;
808
809 // Also add any interfaces implemented by the immutable pass to the map for
810 // fast lookup.
811 const PassInfo *PassInf = findAnalysisPassInfo(AID);
812 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000813 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000814 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
815}
816
Devang Pateleda56172006-12-12 23:34:33 +0000817// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000818void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000819
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000820 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000821 return;
822
Devang Pateleda56172006-12-12 23:34:33 +0000823 // Print out the immutable passes
824 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000825 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000826 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000827
Dan Gohmanf71c5212010-08-19 01:29:07 +0000828 // Every class that derives from PMDataManager also derives from Pass
829 // (sometimes indirectly), but there's no inheritance relationship
830 // between PMDataManager and Pass, so we have to getAsPass to get
831 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000832 for (PMDataManager *Manager : PassManagers)
833 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000834}
835
Devang Patel991aeba2006-12-15 20:13:01 +0000836void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000837
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000838 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000839 return;
840
David Greene994e1bb2010-01-05 01:30:02 +0000841 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000842 for (ImmutablePass *P : ImmutablePasses)
843 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000844 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000845 if (!PI->isAnalysisGroup())
846 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000847 }
Yaron Keren83009952016-04-28 14:49:44 +0000848 for (PMDataManager *PM : PassManagers)
849 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000850 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000851}
852
Devang Patele3068402006-12-21 00:16:50 +0000853void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000854 for (PMDataManager *PM : PassManagers)
855 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000856
Devang Patele3068402006-12-21 00:16:50 +0000857 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000858 for (PMDataManager *IPM : IndirectPassManagers)
859 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000860
Yaron Kerenbd873192016-10-02 19:21:41 +0000861 for (auto LU : LastUser) {
862 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
863 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000864 }
Devang Patele3068402006-12-21 00:16:50 +0000865}
866
Devang Patele7599552007-01-12 18:52:44 +0000867/// Destructor
868PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000869 for (PMDataManager *PM : PassManagers)
870 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000871
Yaron Keren83009952016-04-28 14:49:44 +0000872 for (ImmutablePass *P : ImmutablePasses)
873 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000874}
875
Devang Patelafb1f3622006-12-12 22:35:25 +0000876//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000877// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000878
Devang Patel643676c2006-11-11 01:10:19 +0000879/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000880void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000881 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000882
Chris Lattner60987362009-03-06 05:53:14 +0000883 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000884
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000885 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000886
Dan Gohmande6188a2010-08-12 23:50:08 +0000887 // This pass is the current implementation of all of the interfaces it
888 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000889 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000890 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000891 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000892 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000893 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000894}
895
Devang Patel9d9fc902007-03-06 17:52:53 +0000896// Return true if P preserves high level analysis used by other
897// passes managed by this manager
898bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000899 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000900 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000901 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000902
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000903 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000904 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000905 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000906 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000907 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000908 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000909
Devang Patel9d9fc902007-03-06 17:52:53 +0000910 return true;
911}
912
Chris Lattner02eb94c2008-08-07 07:34:50 +0000913/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000914void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000915 // Don't do this unless assertions are enabled.
916#ifdef NDEBUG
917 return;
918#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000919 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
920 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000921
Devang Patelef432532007-07-19 05:36:09 +0000922 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000923 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000924 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000925 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000926 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000927 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000928 }
929}
930
Devang Patel67c79a42008-07-01 19:50:56 +0000931/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000932void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000933 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
934 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000935 return;
936
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000937 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000938 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000939 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000940 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000941 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000942 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000943 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000944 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000945 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000946 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
947 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000948 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000949 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000950 }
Devang Patel349170f2006-11-11 01:24:55 +0000951 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000952
Devang Patel42dd1e92007-03-06 01:55:46 +0000953 // Check inherited analysis also. If P is not preserving analysis
954 // provided by parent manager then remove it here.
955 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
956
957 if (!InheritedAnalysis[Index])
958 continue;
959
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000960 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000961 I = InheritedAnalysis[Index]->begin(),
962 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000963 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000964 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000965 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000966 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000967 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000968 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000969 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
970 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000971 }
Devang Patel01919d22007-03-08 19:05:01 +0000972 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000973 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000974 }
975 }
Devang Patelf68a3492006-11-07 22:35:17 +0000976}
977
Devang Patelca189262006-11-14 03:05:08 +0000978/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000979void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000980 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000981
Devang Patel8adae862007-07-20 18:04:54 +0000982 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000983
Devang Patel2ff44922007-04-16 20:39:59 +0000984 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000985 if (!TPM)
986 return;
987
Devang Patel17ad0962006-12-08 00:37:52 +0000988 TPM->collectLastUses(DeadPasses, P);
989
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000990 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000991 dbgs() << " -*- '" << P->getPassName();
992 dbgs() << "' is the last user of following pass instances.";
993 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000994 }
995
Yaron Kerenbd873192016-10-02 19:21:41 +0000996 for (Pass *P : DeadPasses)
997 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000998}
Devang Patel200d3052006-12-13 23:50:44 +0000999
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001000void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001001 enum PassDebuggingString DBG_STR) {
1002 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +00001003
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001004 {
1005 // If the pass crashes releasing memory, remember this.
1006 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +00001007 TimeRegion PassTimer(getPassTimer(P));
1008
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001009 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001010 }
1011
Owen Andersona7aed182010-08-06 18:33:48 +00001012 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001013 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001014 // Remove the pass itself (if it is not already removed).
1015 AvailableAnalysis.erase(PI);
1016
1017 // Remove all interfaces this pass implements, for which it is also
1018 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +00001019 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +00001020 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001021 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +00001022 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001023 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +00001024 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +00001025 }
Devang Patel17ad0962006-12-08 00:37:52 +00001026 }
Devang Patelca189262006-11-14 03:05:08 +00001027}
1028
Dan Gohmande6188a2010-08-12 23:50:08 +00001029/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +00001030/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +00001031void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +00001032 // This manager is going to manage pass P. Set up analysis resolver
1033 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001034 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001035 P->setResolver(AR);
1036
Devang Patelec2b9a72007-03-05 22:57:49 +00001037 // If a FunctionPass F is the last user of ModulePass info M
1038 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +00001039 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +00001040
Chris Lattner60987362009-03-06 05:53:14 +00001041 if (!ProcessAnalysis) {
1042 // Add pass
1043 PassVector.push_back(P);
1044 return;
Devang Patel90b05e02006-11-11 02:04:19 +00001045 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001046
Chris Lattner60987362009-03-06 05:53:14 +00001047 // At the moment, this pass is the last user of all required passes.
1048 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +00001049 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +00001050 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1051
1052 unsigned PDepth = this->getDepth();
1053
Chandler Carruth44a13852015-08-19 03:02:12 +00001054 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1055 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001056 unsigned RDepth = 0;
1057
Chandler Carruth44a13852015-08-19 03:02:12 +00001058 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1059 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001060 RDepth = DM.getDepth();
1061
1062 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001063 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001064 else if (PDepth > RDepth) {
1065 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001066 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001067 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001068 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001069 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001070 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001071 }
1072
1073 // Set P as P's last user until someone starts using P.
1074 // However, if P is a Pass Manager then it does not need
1075 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001076 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001077 LastUses.push_back(P);
1078 TPM->setLastUser(LastUses, P);
1079
1080 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001081 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001082 TPM->setLastUser(TransferLastUses, My_PM);
1083 TransferLastUses.clear();
1084 }
1085
Dan Gohman6304db32010-08-16 22:57:28 +00001086 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001087 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1088 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001089 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001090 this->addLowerLevelRequiredPass(P, AnalysisPass);
1091 }
1092
1093 // Take a note of analysis required and made available by this pass.
1094 // Remove the analysis not preserved by this pass
1095 removeNotPreservedAnalysis(P);
1096 recordAvailableAnalysis(P);
1097
Devang Patel8cad70d2006-11-11 01:51:02 +00001098 // Add pass
1099 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001100}
1101
Devang Patele64d3052007-04-16 20:12:57 +00001102
Chandler Carruth44a13852015-08-19 03:02:12 +00001103/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001104/// pass P and are available. Populate RP_NotAvail with analysis
1105/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001106void PMDataManager::collectRequiredAndUsedAnalyses(
1107 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1108 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001109 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001110
1111 for (const auto &UsedID : AnUsage->getUsedSet())
1112 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1113 UP.push_back(AnalysisPass);
1114
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001115 for (const auto &RequiredID : AnUsage->getRequiredSet())
1116 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001117 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001118 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001119 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001120
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001121 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1122 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001123 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001124 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001125 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001126}
1127
Devang Patel07f4f582006-11-14 21:49:36 +00001128// All Required analyses should be available to the pass as it runs! Here
1129// we fill in the AnalysisImpls member of the pass so that it can
1130// successfully use the getAnalysis() method to retrieve the
1131// implementations it needs.
1132//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001133void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001134 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1135
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001136 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1137 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001138 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001139 // This may be analysis pass that is initialized on the fly.
1140 // If that is not the case then it will raise an assert when it is used.
1141 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001142 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001143 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001144 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001145 }
1146}
1147
Devang Patel640c5bb2006-12-08 22:30:11 +00001148/// Find the pass that implements Analysis AID. If desired pass is not found
1149/// then return NULL.
1150Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1151
1152 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001153 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001154
1155 if (I != AvailableAnalysis.end())
1156 return I->second;
1157
1158 // Search Parents through TopLevelManager
1159 if (SearchParent)
1160 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001161
Craig Topperc6207612014-04-09 06:08:46 +00001162 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001163}
1164
Devang Patel991aeba2006-12-15 20:13:01 +00001165// Print list of passes that are last used by P.
1166void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1167
Devang Patel8adae862007-07-20 18:04:54 +00001168 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001169
1170 // If this is a on the fly manager then it does not have TPM.
1171 if (!TPM)
1172 return;
1173
Devang Patel991aeba2006-12-15 20:13:01 +00001174 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001175
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001176 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001177 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001178 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001179 }
1180}
1181
1182void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001183 for (Pass *P : PassVector) {
1184 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001185 PMD->dumpPassArguments();
1186 else
Owen Andersona7aed182010-08-06 18:33:48 +00001187 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001188 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001189 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001190 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001191 }
1192}
1193
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001194void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1195 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001196 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001197 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001198 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001199 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001200 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001201 switch (S1) {
1202 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001203 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001204 break;
1205 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001206 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001207 break;
1208 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001209 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001210 break;
1211 default:
1212 break;
1213 }
1214 switch (S2) {
1215 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001216 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001217 break;
1218 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001219 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001220 break;
1221 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001222 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001223 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001224 case ON_REGION_MSG:
1225 dbgs() << "' on Region '" << Msg << "'...\n";
1226 break;
Devang Patel003a5592007-03-05 20:01:30 +00001227 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001228 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001229 break;
1230 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001231 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001232 break;
1233 default:
1234 break;
1235 }
Devang Patel991aeba2006-12-15 20:13:01 +00001236}
1237
Chris Lattner4c1e9542009-03-06 06:45:05 +00001238void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001239 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001240 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001241
Chris Lattner4c493d92008-08-08 15:14:09 +00001242 AnalysisUsage analysisUsage;
1243 P->getAnalysisUsage(analysisUsage);
1244 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1245}
1246
Chris Lattner4c1e9542009-03-06 06:45:05 +00001247void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001248 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001249 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001250
Chris Lattner4c493d92008-08-08 15:14:09 +00001251 AnalysisUsage analysisUsage;
1252 P->getAnalysisUsage(analysisUsage);
1253 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1254}
1255
Chandler Carruth44a13852015-08-19 03:02:12 +00001256void PMDataManager::dumpUsedSet(const Pass *P) const {
1257 if (PassDebugging < Details)
1258 return;
1259
1260 AnalysisUsage analysisUsage;
1261 P->getAnalysisUsage(analysisUsage);
1262 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1263}
1264
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001265void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001266 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001267 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001268 if (Set.empty())
1269 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001270 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001271 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001272 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001273 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001274 if (!PInf) {
1275 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1276 // all drivers.
1277 dbgs() << " Uninitialized Pass";
1278 continue;
1279 }
Owen Andersona7aed182010-08-06 18:33:48 +00001280 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001281 }
David Greene994e1bb2010-01-05 01:30:02 +00001282 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001283}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001284
Devang Patel004937b2007-07-27 20:06:09 +00001285/// Add RequiredPass into list of lower level passes required by pass P.
1286/// RequiredPass is run on the fly by Pass Manager when P requests it
1287/// through getAnalysis interface.
1288/// This should be handled by specific pass manager.
1289void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1290 if (TPM) {
1291 TPM->dumpArguments();
1292 TPM->dumpPasses();
1293 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001294
Dan Gohmande6188a2010-08-12 23:50:08 +00001295 // Module Level pass may required Function Level analysis info
1296 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1297 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001298 // module level pass is requiring lower level analysis info managed by
1299 // lower level pass manager.
1300
1301 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001302 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001303 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001304#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001305 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1306 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001307#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001308 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001309}
1310
Owen Andersona7aed182010-08-06 18:33:48 +00001311Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001312 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001313}
1314
Devang Patele7599552007-01-12 18:52:44 +00001315// Destructor
1316PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001317 for (Pass *P : PassVector)
1318 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001319}
1320
Devang Patel9bdf7d42006-12-08 23:28:54 +00001321//===----------------------------------------------------------------------===//
1322// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001323// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1324Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001325 return PM.findAnalysisPass(ID, dir);
1326}
1327
Dan Gohmande6188a2010-08-12 23:50:08 +00001328Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001329 Function &F) {
1330 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1331}
1332
Devang Patela1514cb2006-12-07 19:39:39 +00001333//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001334// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001335
Dan Gohmande6188a2010-08-12 23:50:08 +00001336/// Execute all of the passes scheduled for execution by invoking
1337/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001338/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001339bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001340 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001341 return false;
1342
Devang Patele9585592006-12-08 01:38:28 +00001343 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001344 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001345
Xin Tong023e25a2018-07-22 05:27:41 +00001346 unsigned InstrCount = 0;
1347 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001348 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001349 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1350 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001351 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001352
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001353 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001354 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001355
Devang Patelabfbe3b2006-12-16 00:56:26 +00001356 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001357
Chris Lattner4c1e9542009-03-06 06:45:05 +00001358 {
1359 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001360 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001361 TimeRegion PassTimer(getPassTimer(BP));
Xin Tong023e25a2018-07-22 05:27:41 +00001362 if (EmitICRemark)
1363 InstrCount = initSizeRemarkInfo(M);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001364 LocalChanged |= BP->runOnBasicBlock(BB);
Xin Tong023e25a2018-07-22 05:27:41 +00001365 if (EmitICRemark)
1366 emitInstrCountChangedRemark(BP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001367 }
Devang Patel93a197c2006-12-14 00:08:04 +00001368
Dan Gohman74b189f2010-03-01 17:34:28 +00001369 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001370 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001371 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001372 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001373 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001374 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001375
Devang Patela273d1c2007-07-19 18:02:32 +00001376 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001377 removeNotPreservedAnalysis(BP);
1378 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001379 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001380 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001381
Bill Wendling6ce6d262009-12-25 13:50:18 +00001382 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001383}
1384
Devang Patel475c4532006-12-08 00:59:05 +00001385// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001386bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001387 bool Changed = false;
1388
Chris Lattner4c1e9542009-03-06 06:45:05 +00001389 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1390 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001391
1392 return Changed;
1393}
1394
Duncan Sands51495602009-02-13 09:42:34 +00001395bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001396 bool Changed = false;
1397
Pedro Artigas41b98842012-12-05 17:12:22 +00001398 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001399 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001400
1401 return Changed;
1402}
1403
Duncan Sands51495602009-02-13 09:42:34 +00001404bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001405 bool Changed = false;
1406
Devang Patelabfbe3b2006-12-16 00:56:26 +00001407 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1408 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001409 Changed |= BP->doInitialization(F);
1410 }
1411
1412 return Changed;
1413}
1414
Duncan Sands51495602009-02-13 09:42:34 +00001415bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001416 bool Changed = false;
1417
Devang Patelabfbe3b2006-12-16 00:56:26 +00001418 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1419 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001420 Changed |= BP->doFinalization(F);
1421 }
1422
1423 return Changed;
1424}
1425
1426
Devang Patela1514cb2006-12-07 19:39:39 +00001427//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001428// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001429
Devang Patel4e12f862006-11-08 10:44:40 +00001430/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001431FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001432 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001433 // FPM is the top level manager.
1434 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001435
Dan Gohman565df952008-03-13 02:08:36 +00001436 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001437 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001438}
1439
Devang Patelb67904d2006-12-13 02:36:01 +00001440FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001441 delete FPM;
1442}
1443
Dan Gohmande6188a2010-08-12 23:50:08 +00001444void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001445 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001446}
1447
Devang Patel9f3083e2006-11-15 19:39:54 +00001448/// run - Execute all of the passes scheduled for execution. Keep
1449/// track of whether any of the passes modifies the function, and if
1450/// so, return true.
1451///
Devang Patelb67904d2006-12-13 02:36:01 +00001452bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001453 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1454 report_fatal_error("Error reading bitcode file: " + EIB.message());
1455 });
Devang Patel272908d2006-12-08 22:57:48 +00001456 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001457}
1458
1459
Devang Patelff631ae2006-11-15 01:27:05 +00001460/// doInitialization - Run all of the initializers for the function passes.
1461///
Devang Patelb67904d2006-12-13 02:36:01 +00001462bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001463 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001464}
1465
Dan Gohmane6656eb2007-07-30 14:51:13 +00001466/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001467///
Devang Patelb67904d2006-12-13 02:36:01 +00001468bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001469 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001470}
1471
Devang Patela1514cb2006-12-07 19:39:39 +00001472//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001473// FunctionPassManagerImpl implementation
1474//
Duncan Sands51495602009-02-13 09:42:34 +00001475bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001476 bool Changed = false;
1477
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001478 dumpArguments();
1479 dumpPasses();
1480
Yaron Keren4849aa32015-06-05 17:48:47 +00001481 for (ImmutablePass *ImPass : getImmutablePasses())
1482 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001483
Chris Lattner4c1e9542009-03-06 06:45:05 +00001484 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1485 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001486
1487 return Changed;
1488}
1489
Duncan Sands51495602009-02-13 09:42:34 +00001490bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001491 bool Changed = false;
1492
Pedro Artigas41b98842012-12-05 17:12:22 +00001493 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001494 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001495
Yaron Keren4849aa32015-06-05 17:48:47 +00001496 for (ImmutablePass *ImPass : getImmutablePasses())
1497 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001498
Devang Patel67d6a5e2006-12-19 19:46:59 +00001499 return Changed;
1500}
1501
Devang Patelec9c58f2009-04-01 22:34:41 +00001502/// cleanup - After running all passes, clean up pass manager cache.
1503void FPPassManager::cleanup() {
1504 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1505 FunctionPass *FP = getContainedPass(Index);
1506 AnalysisResolver *AR = FP->getResolver();
1507 assert(AR && "Analysis Resolver is not set");
1508 AR->clearAnalysisImpls();
1509 }
1510}
1511
Torok Edwin24c78352009-06-29 18:49:09 +00001512void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1513 if (!wasRun)
1514 return;
1515 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1516 FPPassManager *FPPM = getContainedManager(Index);
1517 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1518 FPPM->getContainedPass(Index)->releaseMemory();
1519 }
1520 }
Torok Edwin896556e2009-06-29 21:05:10 +00001521 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001522}
1523
Devang Patel67d6a5e2006-12-19 19:46:59 +00001524// Execute all the passes managed by this top level manager.
1525// Return true if any function is modified by a pass.
1526bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001527 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528 TimingInfo::createTheTimeInfo();
1529
Devang Patele3068402006-12-21 00:16:50 +00001530 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001531 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001532 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001533 F.getContext().yield();
1534 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001535
1536 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1537 getContainedManager(Index)->cleanup();
1538
Torok Edwin24c78352009-06-29 18:49:09 +00001539 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001540 return Changed;
1541}
1542
1543//===----------------------------------------------------------------------===//
1544// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001545
Devang Patel8c78a0b2007-05-03 01:11:54 +00001546char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001547/// Print passes managed by this manager
1548void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001549 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001550 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1551 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001552 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001553 dumpLastUses(FP, Offset+1);
1554 }
1555}
1556
1557
Dan Gohmande6188a2010-08-12 23:50:08 +00001558/// Execute all of the passes scheduled for execution by invoking
1559/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001560/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001561bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001562 if (F.isDeclaration())
1563 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001564
1565 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001566 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001567 // Collect inherited analysis from Module level pass manager.
1568 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001569
Xin Tong023e25a2018-07-22 05:27:41 +00001570 unsigned InstrCount = 0;
1571 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001572 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1573 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001574 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001576 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001577 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001578
Devang Patelabfbe3b2006-12-16 00:56:26 +00001579 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001580
Chris Lattner4c1e9542009-03-06 06:45:05 +00001581 {
1582 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001583 TimeRegion PassTimer(getPassTimer(FP));
Xin Tong023e25a2018-07-22 05:27:41 +00001584 if (EmitICRemark)
1585 InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001586 LocalChanged |= FP->runOnFunction(F);
Xin Tong023e25a2018-07-22 05:27:41 +00001587 if (EmitICRemark)
1588 emitInstrCountChangedRemark(FP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001589 }
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Dan Gohman74b189f2010-03-01 17:34:28 +00001591 Changed |= LocalChanged;
1592 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001593 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001594 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001595 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001596
Devang Patela273d1c2007-07-19 18:02:32 +00001597 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001598 removeNotPreservedAnalysis(FP);
1599 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001600 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001601 }
1602 return Changed;
1603}
1604
Devang Patel67d6a5e2006-12-19 19:46:59 +00001605bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001606 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001607
Serge Pavloved5eb932017-01-15 10:23:18 +00001608 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001609 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001610
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001611 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001612}
1613
Duncan Sands51495602009-02-13 09:42:34 +00001614bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001615 bool Changed = false;
1616
Chris Lattner4c1e9542009-03-06 06:45:05 +00001617 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1618 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001619
Devang Patelff631ae2006-11-15 01:27:05 +00001620 return Changed;
1621}
1622
Duncan Sands51495602009-02-13 09:42:34 +00001623bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001624 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001625
1626 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001627 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001628
Devang Patelff631ae2006-11-15 01:27:05 +00001629 return Changed;
1630}
1631
Devang Patela1514cb2006-12-07 19:39:39 +00001632//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001633// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001634
Dan Gohmande6188a2010-08-12 23:50:08 +00001635/// Execute all of the passes scheduled for execution by invoking
1636/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001637/// the module, and if so, return true.
1638bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001639MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001640 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001641
Torok Edwin24c78352009-06-29 18:49:09 +00001642 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001643 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1644 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001645 Changed |= FPP->doInitialization(M);
1646 }
1647
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001648 // Initialize module passes
1649 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1650 Changed |= getContainedPass(Index)->doInitialization(M);
1651
Xin Tong023e25a2018-07-22 05:27:41 +00001652 unsigned InstrCount = 0;
1653 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001654 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1655 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001656 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001657
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001658 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001659 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001660
Devang Patelabfbe3b2006-12-16 00:56:26 +00001661 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001662
Chris Lattner4c1e9542009-03-06 06:45:05 +00001663 {
1664 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001665 TimeRegion PassTimer(getPassTimer(MP));
1666
Xin Tong023e25a2018-07-22 05:27:41 +00001667 if (EmitICRemark)
1668 InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001669 LocalChanged |= MP->runOnModule(M);
Xin Tong023e25a2018-07-22 05:27:41 +00001670 if (EmitICRemark)
1671 emitInstrCountChangedRemark(MP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001672 }
Devang Patel93a197c2006-12-14 00:08:04 +00001673
Dan Gohman74b189f2010-03-01 17:34:28 +00001674 Changed |= LocalChanged;
1675 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001676 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001677 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001678 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001679 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001680
Devang Patela273d1c2007-07-19 18:02:32 +00001681 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001682 removeNotPreservedAnalysis(MP);
1683 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001684 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001685 }
Torok Edwin24c78352009-06-29 18:49:09 +00001686
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001687 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001688 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001689 Changed |= getContainedPass(Index)->doFinalization(M);
1690
Torok Edwin24c78352009-06-29 18:49:09 +00001691 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001692 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1693 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001694 // We don't know when is the last time an on-the-fly pass is run,
1695 // so we need to releaseMemory / finalize here
1696 FPP->releaseMemoryOnTheFly();
1697 Changed |= FPP->doFinalization(M);
1698 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001699
Devang Patel05e1a972006-11-07 22:03:15 +00001700 return Changed;
1701}
1702
Devang Patele64d3052007-04-16 20:12:57 +00001703/// Add RequiredPass into list of lower level passes required by pass P.
1704/// RequiredPass is run on the fly by Pass Manager when P requests it
1705/// through getAnalysis interface.
1706void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001707 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1708 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001709 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001710 RequiredPass->getPotentialPassManagerType()) &&
1711 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001712 if (!RequiredPass)
1713 return;
Devang Patele64d3052007-04-16 20:12:57 +00001714
Devang Patel68f72b12007-04-26 17:50:19 +00001715 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001716 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001717 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001718 // FPP is the top level manager.
1719 FPP->setTopLevelManager(FPP);
1720
Devang Patel69e9f6d2007-04-16 20:27:05 +00001721 OnTheFlyManagers[P] = FPP;
1722 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001723 const PassInfo *RequiredPassPI =
1724 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001725
Craig Topperc6207612014-04-09 06:08:46 +00001726 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001727 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1728 FoundPass =
1729 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001730 }
Andrew Trick02066f22014-04-08 03:40:34 +00001731 if (!FoundPass) {
1732 FoundPass = RequiredPass;
1733 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001734 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001735 FPP->add(RequiredPass);
1736 }
1737 // Register P as the last user of FoundPass or RequiredPass.
1738 SmallVector<Pass *, 1> LU;
1739 LU.push_back(FoundPass);
1740 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001741}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001742
Dan Gohmande6188a2010-08-12 23:50:08 +00001743/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001744/// required by module pass MP. Instantiate analysis pass, by using
1745/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001746Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001747 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001748 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001749
Torok Edwin24c78352009-06-29 18:49:09 +00001750 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001751 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001752 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001753}
1754
1755
Devang Patela1514cb2006-12-07 19:39:39 +00001756//===----------------------------------------------------------------------===//
1757// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001758
Devang Patelab97cf42006-12-13 00:09:23 +00001759//
Devang Patelc290c8a2006-11-07 22:23:34 +00001760/// run - Execute all of the passes scheduled for execution. Keep track of
1761/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001762bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001763 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001764 TimingInfo::createTheTimeInfo();
1765
Devang Patelcfd70c42006-12-13 22:10:00 +00001766 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001767 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001768
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001769 for (ImmutablePass *ImPass : getImmutablePasses())
1770 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001771
Devang Patele3068402006-12-21 00:16:50 +00001772 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001773 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001774 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001775 M.getContext().yield();
1776 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001777
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001778 for (ImmutablePass *ImPass : getImmutablePasses())
1779 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001780
Devang Patelc290c8a2006-11-07 22:23:34 +00001781 return Changed;
1782}
Devang Patel376fefa2006-11-08 10:29:57 +00001783
Devang Patela1514cb2006-12-07 19:39:39 +00001784//===----------------------------------------------------------------------===//
1785// PassManager implementation
1786
Devang Patel376fefa2006-11-08 10:29:57 +00001787/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001788PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001789 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001790 // PM is the top level manager
1791 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001792}
1793
Devang Patelb67904d2006-12-13 02:36:01 +00001794PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001795 delete PM;
1796}
1797
Chris Lattner60987362009-03-06 05:53:14 +00001798void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001799 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001800}
1801
1802/// run - Execute all of the passes scheduled for execution. Keep track of
1803/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001804bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001805 return PM->run(M);
1806}
1807
Devang Patelb8817b92006-12-14 00:59:42 +00001808//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001809// TimingInfo implementation
1810
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001811bool llvm::TimePassesIsEnabled = false;
Zachary Turner8065f0b2017-12-01 00:53:10 +00001812static cl::opt<bool, true> EnableTiming(
1813 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
1814 cl::desc("Time each pass, printing elapsed time for each on exit"));
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001815
Devang Patelb8817b92006-12-14 00:59:42 +00001816// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001817// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001818// null. It may be called multiple times.
1819void TimingInfo::createTheTimeInfo() {
1820 if (!TimePassesIsEnabled || TheTimeInfo) return;
1821
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001822 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001823 // This guarantees that the object will be constructed before static globals,
1824 // thus it will be destroyed before them.
1825 static ManagedStatic<TimingInfo> TTI;
1826 TheTimeInfo = &*TTI;
1827}
1828
Devang Patel1c3633e2007-01-29 23:10:37 +00001829/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001830Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001831 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001832 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001833 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001834}
1835
James Henderson852f6fd2017-05-16 09:43:21 +00001836/// If timing is enabled, report the times collected up to now and then reset
1837/// them.
1838void llvm::reportAndResetTimings() {
1839 if (TheTimeInfo)
1840 TheTimeInfo->print();
1841}
1842
Devang Patel1c56a632007-01-08 19:29:38 +00001843//===----------------------------------------------------------------------===//
1844// PMStack implementation
1845//
Devang Patelad98d232007-01-11 22:15:30 +00001846
Devang Patel1c56a632007-01-08 19:29:38 +00001847// Pop Pass Manager from the stack and clear its analysis info.
1848void PMStack::pop() {
1849
1850 PMDataManager *Top = this->top();
1851 Top->initializeAnalysisInfo();
1852
1853 S.pop_back();
1854}
1855
1856// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001857void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001858 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001859 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001860
Chris Lattner60987362009-03-06 05:53:14 +00001861 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001862 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1863 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001864 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001865
Chris Lattner60987362009-03-06 05:53:14 +00001866 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001867 TPM->addIndirectPassManager(PM);
1868 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001869 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001870 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001871 assert((PM->getPassManagerType() == PMT_ModulePassManager
1872 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001873 && "pushing bad pass manager to PMStack");
1874 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001875 }
1876
Devang Patel15701b52007-01-11 00:19:00 +00001877 S.push_back(PM);
1878}
1879
1880// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001881LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001882 for (PMDataManager *Manager : S)
1883 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001884
Devang Patel15701b52007-01-11 00:19:00 +00001885 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001886 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001887}
1888
Devang Patel1c56a632007-01-08 19:29:38 +00001889/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001890/// add self into that manager.
1891void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001892 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001893 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001894 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001895 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1896 if (TopPMType == PreferredType)
1897 break; // We found desired pass manager
1898 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001899 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001900 else
1901 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001902 }
Devang Patel18ff6362008-09-09 21:38:40 +00001903 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001904 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001905}
1906
Devang Patel3312f752007-01-16 21:43:18 +00001907/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001908/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001909void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001910 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001911
Andrew Trickcbc845f2012-02-01 07:16:20 +00001912 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001913 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001914 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1915 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001916 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001917 break;
Devang Patel3312f752007-01-16 21:43:18 +00001918 }
Devang Patel3312f752007-01-16 21:43:18 +00001919
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001920 // Create new Function Pass Manager if needed.
1921 FPPassManager *FPP;
1922 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1923 FPP = (FPPassManager *)PMS.top();
1924 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001925 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1926 PMDataManager *PMD = PMS.top();
1927
1928 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001929 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001930 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001931
1932 // [2] Set up new manager's top level manager
1933 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1934 TPM->addIndirectPassManager(FPP);
1935
1936 // [3] Assign manager to manage this new manager. This may create
1937 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001938 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001939
1940 // [4] Push new manager into PMS
1941 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001942 }
1943
Devang Patel3312f752007-01-16 21:43:18 +00001944 // Assign FPP as the manager of this pass.
1945 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001946}
1947
Devang Patel3312f752007-01-16 21:43:18 +00001948/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001949/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001950void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001951 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001952 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001953
Devang Patel15701b52007-01-11 00:19:00 +00001954 // Basic Pass Manager is a leaf pass manager. It does not handle
1955 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001956 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001957 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1958 BBP = (BBPassManager *)PMS.top();
1959 } else {
1960 // If leaf manager is not Basic Block Pass manager then create new
1961 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001962 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1963 PMDataManager *PMD = PMS.top();
1964
1965 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001966 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001967
1968 // [2] Set up new manager's top level manager
1969 // Basic Block Pass Manager does not live by itself
1970 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1971 TPM->addIndirectPassManager(BBP);
1972
Devang Patel15701b52007-01-11 00:19:00 +00001973 // [3] Assign manager to manage this new manager. This may create
1974 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001975 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001976
Devang Patel3312f752007-01-16 21:43:18 +00001977 // [4] Push new manager into PMS
1978 PMS.push(BBP);
1979 }
Devang Patel1c56a632007-01-08 19:29:38 +00001980
Devang Patel3312f752007-01-16 21:43:18 +00001981 // Assign BBP as the manager of this pass.
1982 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001983}
1984
Dan Gohmand3a20c92008-03-11 16:41:42 +00001985PassManagerBase::~PassManagerBase() {}