blob: d11a2b3c01a282bf71d5259e86acef6695a3b536 [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())) {
Craig Topper4ee28412018-08-20 20:57:30 +0000680 // Remove any cached AnalysisUsage information.
681 AnUsageMap.erase(P);
Nuno Lopes0460bb22008-11-04 23:03:58 +0000682 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000683 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000684 }
Devang Patel864970e2008-03-18 00:39:19 +0000685
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000686 AnalysisUsage *AnUsage = findAnalysisUsage(P);
687
Devang Patelfdee7032008-08-14 23:07:48 +0000688 bool checkAnalysis = true;
689 while (checkAnalysis) {
690 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000691
Devang Patelfdee7032008-08-14 23:07:48 +0000692 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000693 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000694
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000695 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000696 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000697 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000698
Craig Topperc6207612014-04-09 06:08:46 +0000699 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000700 // Pass P is not in the global PassRegistry
701 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
702 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
703 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000704 for (const AnalysisID ID2 : RequiredSet) {
705 if (ID == ID2)
706 break;
707 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000708 if (AnalysisPass2) {
709 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000710 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000711 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
712 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
713 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
714 }
715 }
716 }
717
Andrew Trick6bbaf132011-06-03 00:48:58 +0000718 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000719 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000720 if (P->getPotentialPassManagerType () ==
721 AnalysisPass->getPotentialPassManagerType())
722 // Schedule analysis pass that is managed by the same pass manager.
723 schedulePass(AnalysisPass);
724 else if (P->getPotentialPassManagerType () >
725 AnalysisPass->getPotentialPassManagerType()) {
726 // Schedule analysis pass that is managed by a new manager.
727 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000728 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000729 // are already checked are still available.
730 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000731 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000732 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000733 // passes are run on the fly.
734 delete AnalysisPass;
735 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000736 }
737 }
738
739 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000740 if (ImmutablePass *IP = P->getAsImmutablePass()) {
741 // P is a immutable pass and it will be managed by this
742 // top level manager. Set up analysis resolver to connect them.
743 PMDataManager *DM = getAsPMDataManager();
744 AnalysisResolver *AR = new AnalysisResolver(*DM);
745 P->setResolver(AR);
746 DM->initializeAnalysisImpl(P);
747 addImmutablePass(IP);
748 DM->recordAvailableAnalysis(IP);
749 return;
750 }
751
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000752 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000753 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000754 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000755 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
756 }
757
758 // Add the requested pass to the best available pass manager.
759 P->assignPassManager(activeStack, getTopLevelPassManagerType());
760
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000761 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000762 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000763 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000764 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
765 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000766}
767
768/// Find the pass that implements Analysis AID. Search immutable
769/// passes and all pass managers. If desired pass is not found
770/// then return NULL.
771Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000772 // For immutable passes we have a direct mapping from ID to pass, so check
773 // that first.
774 if (Pass *P = ImmutablePassMap.lookup(AID))
775 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000776
Devang Patelcd6ba152006-12-12 22:50:05 +0000777 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000778 for (PMDataManager *PassManager : PassManagers)
779 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000780 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000781
782 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000783 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
784 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000785 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000786
Craig Topperc6207612014-04-09 06:08:46 +0000787 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000788}
789
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000790const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
791 const PassInfo *&PI = AnalysisPassInfos[AID];
792 if (!PI)
793 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
794 else
795 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
796 "The pass info pointer changed for an analysis ID!");
797
798 return PI;
799}
800
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000801void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
802 P->initializePass();
803 ImmutablePasses.push_back(P);
804
805 // Add this pass to the map from its analysis ID. We clobber any prior runs
806 // of the pass in the map so that the last one added is the one found when
807 // doing lookups.
808 AnalysisID AID = P->getPassID();
809 ImmutablePassMap[AID] = P;
810
811 // Also add any interfaces implemented by the immutable pass to the map for
812 // fast lookup.
813 const PassInfo *PassInf = findAnalysisPassInfo(AID);
814 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000815 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000816 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
817}
818
Devang Pateleda56172006-12-12 23:34:33 +0000819// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000820void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000821
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000822 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000823 return;
824
Devang Pateleda56172006-12-12 23:34:33 +0000825 // Print out the immutable passes
826 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000827 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000828 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000829
Dan Gohmanf71c5212010-08-19 01:29:07 +0000830 // Every class that derives from PMDataManager also derives from Pass
831 // (sometimes indirectly), but there's no inheritance relationship
832 // between PMDataManager and Pass, so we have to getAsPass to get
833 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000834 for (PMDataManager *Manager : PassManagers)
835 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000836}
837
Devang Patel991aeba2006-12-15 20:13:01 +0000838void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000839
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000840 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000841 return;
842
David Greene994e1bb2010-01-05 01:30:02 +0000843 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000844 for (ImmutablePass *P : ImmutablePasses)
845 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000846 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000847 if (!PI->isAnalysisGroup())
848 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000849 }
Yaron Keren83009952016-04-28 14:49:44 +0000850 for (PMDataManager *PM : PassManagers)
851 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000852 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000853}
854
Devang Patele3068402006-12-21 00:16:50 +0000855void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000856 for (PMDataManager *PM : PassManagers)
857 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000858
Devang Patele3068402006-12-21 00:16:50 +0000859 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000860 for (PMDataManager *IPM : IndirectPassManagers)
861 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000862
Yaron Kerenbd873192016-10-02 19:21:41 +0000863 for (auto LU : LastUser) {
864 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
865 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000866 }
Devang Patele3068402006-12-21 00:16:50 +0000867}
868
Devang Patele7599552007-01-12 18:52:44 +0000869/// Destructor
870PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000871 for (PMDataManager *PM : PassManagers)
872 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000873
Yaron Keren83009952016-04-28 14:49:44 +0000874 for (ImmutablePass *P : ImmutablePasses)
875 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000876}
877
Devang Patelafb1f3622006-12-12 22:35:25 +0000878//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000879// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000880
Devang Patel643676c2006-11-11 01:10:19 +0000881/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000882void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000883 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000884
Chris Lattner60987362009-03-06 05:53:14 +0000885 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000886
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000887 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000888
Dan Gohmande6188a2010-08-12 23:50:08 +0000889 // This pass is the current implementation of all of the interfaces it
890 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000891 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000892 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000893 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000894 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000895 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000896}
897
Devang Patel9d9fc902007-03-06 17:52:53 +0000898// Return true if P preserves high level analysis used by other
899// passes managed by this manager
900bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000901 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000902 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000903 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000904
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000905 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000906 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000907 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000908 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000909 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000910 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000911
Devang Patel9d9fc902007-03-06 17:52:53 +0000912 return true;
913}
914
Chris Lattner02eb94c2008-08-07 07:34:50 +0000915/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000916void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000917 // Don't do this unless assertions are enabled.
918#ifdef NDEBUG
919 return;
920#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000921 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
922 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000923
Devang Patelef432532007-07-19 05:36:09 +0000924 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000925 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000926 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000927 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000928 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000929 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000930 }
931}
932
Devang Patel67c79a42008-07-01 19:50:56 +0000933/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000934void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000935 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
936 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000937 return;
938
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000939 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000940 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000941 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000942 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000943 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000944 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000945 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000946 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000947 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000948 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
949 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000950 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000951 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000952 }
Devang Patel349170f2006-11-11 01:24:55 +0000953 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000954
Devang Patel42dd1e92007-03-06 01:55:46 +0000955 // Check inherited analysis also. If P is not preserving analysis
956 // provided by parent manager then remove it here.
957 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
958
959 if (!InheritedAnalysis[Index])
960 continue;
961
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000962 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000963 I = InheritedAnalysis[Index]->begin(),
964 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000965 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000966 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000967 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000968 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000969 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000970 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000971 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
972 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000973 }
Devang Patel01919d22007-03-08 19:05:01 +0000974 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000975 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000976 }
977 }
Devang Patelf68a3492006-11-07 22:35:17 +0000978}
979
Devang Patelca189262006-11-14 03:05:08 +0000980/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000981void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000982 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000983
Devang Patel8adae862007-07-20 18:04:54 +0000984 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000985
Devang Patel2ff44922007-04-16 20:39:59 +0000986 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000987 if (!TPM)
988 return;
989
Devang Patel17ad0962006-12-08 00:37:52 +0000990 TPM->collectLastUses(DeadPasses, P);
991
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000992 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000993 dbgs() << " -*- '" << P->getPassName();
994 dbgs() << "' is the last user of following pass instances.";
995 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000996 }
997
Yaron Kerenbd873192016-10-02 19:21:41 +0000998 for (Pass *P : DeadPasses)
999 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001000}
Devang Patel200d3052006-12-13 23:50:44 +00001001
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001002void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001003 enum PassDebuggingString DBG_STR) {
1004 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +00001005
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001006 {
1007 // If the pass crashes releasing memory, remember this.
1008 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +00001009 TimeRegion PassTimer(getPassTimer(P));
1010
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001011 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001012 }
1013
Owen Andersona7aed182010-08-06 18:33:48 +00001014 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001015 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001016 // Remove the pass itself (if it is not already removed).
1017 AvailableAnalysis.erase(PI);
1018
1019 // Remove all interfaces this pass implements, for which it is also
1020 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +00001021 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +00001022 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001023 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +00001024 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001025 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +00001026 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +00001027 }
Devang Patel17ad0962006-12-08 00:37:52 +00001028 }
Devang Patelca189262006-11-14 03:05:08 +00001029}
1030
Dan Gohmande6188a2010-08-12 23:50:08 +00001031/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +00001032/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +00001033void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +00001034 // This manager is going to manage pass P. Set up analysis resolver
1035 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001036 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001037 P->setResolver(AR);
1038
Devang Patelec2b9a72007-03-05 22:57:49 +00001039 // If a FunctionPass F is the last user of ModulePass info M
1040 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +00001041 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +00001042
Chris Lattner60987362009-03-06 05:53:14 +00001043 if (!ProcessAnalysis) {
1044 // Add pass
1045 PassVector.push_back(P);
1046 return;
Devang Patel90b05e02006-11-11 02:04:19 +00001047 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001048
Chris Lattner60987362009-03-06 05:53:14 +00001049 // At the moment, this pass is the last user of all required passes.
1050 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +00001051 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +00001052 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1053
1054 unsigned PDepth = this->getDepth();
1055
Chandler Carruth44a13852015-08-19 03:02:12 +00001056 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1057 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001058 unsigned RDepth = 0;
1059
Chandler Carruth44a13852015-08-19 03:02:12 +00001060 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1061 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001062 RDepth = DM.getDepth();
1063
1064 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001065 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001066 else if (PDepth > RDepth) {
1067 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001068 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001069 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001070 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001071 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001072 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001073 }
1074
1075 // Set P as P's last user until someone starts using P.
1076 // However, if P is a Pass Manager then it does not need
1077 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001078 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001079 LastUses.push_back(P);
1080 TPM->setLastUser(LastUses, P);
1081
1082 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001083 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001084 TPM->setLastUser(TransferLastUses, My_PM);
1085 TransferLastUses.clear();
1086 }
1087
Dan Gohman6304db32010-08-16 22:57:28 +00001088 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001089 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1090 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001091 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001092 this->addLowerLevelRequiredPass(P, AnalysisPass);
1093 }
1094
1095 // Take a note of analysis required and made available by this pass.
1096 // Remove the analysis not preserved by this pass
1097 removeNotPreservedAnalysis(P);
1098 recordAvailableAnalysis(P);
1099
Devang Patel8cad70d2006-11-11 01:51:02 +00001100 // Add pass
1101 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001102}
1103
Devang Patele64d3052007-04-16 20:12:57 +00001104
Chandler Carruth44a13852015-08-19 03:02:12 +00001105/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001106/// pass P and are available. Populate RP_NotAvail with analysis
1107/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001108void PMDataManager::collectRequiredAndUsedAnalyses(
1109 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1110 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001111 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001112
1113 for (const auto &UsedID : AnUsage->getUsedSet())
1114 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1115 UP.push_back(AnalysisPass);
1116
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001117 for (const auto &RequiredID : AnUsage->getRequiredSet())
1118 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001119 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001120 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001121 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001122
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001123 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1124 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001125 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001126 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001127 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001128}
1129
Devang Patel07f4f582006-11-14 21:49:36 +00001130// All Required analyses should be available to the pass as it runs! Here
1131// we fill in the AnalysisImpls member of the pass so that it can
1132// successfully use the getAnalysis() method to retrieve the
1133// implementations it needs.
1134//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001135void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001136 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1137
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001138 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1139 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001140 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001141 // This may be analysis pass that is initialized on the fly.
1142 // If that is not the case then it will raise an assert when it is used.
1143 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001144 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001145 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001146 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001147 }
1148}
1149
Devang Patel640c5bb2006-12-08 22:30:11 +00001150/// Find the pass that implements Analysis AID. If desired pass is not found
1151/// then return NULL.
1152Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1153
1154 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001155 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001156
1157 if (I != AvailableAnalysis.end())
1158 return I->second;
1159
1160 // Search Parents through TopLevelManager
1161 if (SearchParent)
1162 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001163
Craig Topperc6207612014-04-09 06:08:46 +00001164 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001165}
1166
Devang Patel991aeba2006-12-15 20:13:01 +00001167// Print list of passes that are last used by P.
1168void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1169
Devang Patel8adae862007-07-20 18:04:54 +00001170 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001171
1172 // If this is a on the fly manager then it does not have TPM.
1173 if (!TPM)
1174 return;
1175
Devang Patel991aeba2006-12-15 20:13:01 +00001176 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001177
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001178 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001179 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001180 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001181 }
1182}
1183
1184void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001185 for (Pass *P : PassVector) {
1186 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001187 PMD->dumpPassArguments();
1188 else
Owen Andersona7aed182010-08-06 18:33:48 +00001189 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001190 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001191 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001192 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001193 }
1194}
1195
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001196void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1197 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001198 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001199 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001200 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001201 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001202 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001203 switch (S1) {
1204 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001205 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001206 break;
1207 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001208 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001209 break;
1210 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001211 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001212 break;
1213 default:
1214 break;
1215 }
1216 switch (S2) {
1217 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001218 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001219 break;
1220 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001221 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001222 break;
1223 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001224 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001225 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001226 case ON_REGION_MSG:
1227 dbgs() << "' on Region '" << Msg << "'...\n";
1228 break;
Devang Patel003a5592007-03-05 20:01:30 +00001229 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001230 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001231 break;
1232 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001233 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001234 break;
1235 default:
1236 break;
1237 }
Devang Patel991aeba2006-12-15 20:13:01 +00001238}
1239
Chris Lattner4c1e9542009-03-06 06:45:05 +00001240void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001241 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001242 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001243
Chris Lattner4c493d92008-08-08 15:14:09 +00001244 AnalysisUsage analysisUsage;
1245 P->getAnalysisUsage(analysisUsage);
1246 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1247}
1248
Chris Lattner4c1e9542009-03-06 06:45:05 +00001249void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001250 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001251 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001252
Chris Lattner4c493d92008-08-08 15:14:09 +00001253 AnalysisUsage analysisUsage;
1254 P->getAnalysisUsage(analysisUsage);
1255 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1256}
1257
Chandler Carruth44a13852015-08-19 03:02:12 +00001258void PMDataManager::dumpUsedSet(const Pass *P) const {
1259 if (PassDebugging < Details)
1260 return;
1261
1262 AnalysisUsage analysisUsage;
1263 P->getAnalysisUsage(analysisUsage);
1264 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1265}
1266
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001267void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001268 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001269 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001270 if (Set.empty())
1271 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001272 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001273 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001274 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001275 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001276 if (!PInf) {
1277 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1278 // all drivers.
1279 dbgs() << " Uninitialized Pass";
1280 continue;
1281 }
Owen Andersona7aed182010-08-06 18:33:48 +00001282 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001283 }
David Greene994e1bb2010-01-05 01:30:02 +00001284 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001285}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001286
Devang Patel004937b2007-07-27 20:06:09 +00001287/// Add RequiredPass into list of lower level passes required by pass P.
1288/// RequiredPass is run on the fly by Pass Manager when P requests it
1289/// through getAnalysis interface.
1290/// This should be handled by specific pass manager.
1291void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1292 if (TPM) {
1293 TPM->dumpArguments();
1294 TPM->dumpPasses();
1295 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001296
Dan Gohmande6188a2010-08-12 23:50:08 +00001297 // Module Level pass may required Function Level analysis info
1298 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1299 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001300 // module level pass is requiring lower level analysis info managed by
1301 // lower level pass manager.
1302
1303 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001304 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001305 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001306#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001307 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1308 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001309#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001310 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001311}
1312
Owen Andersona7aed182010-08-06 18:33:48 +00001313Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001314 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001315}
1316
Devang Patele7599552007-01-12 18:52:44 +00001317// Destructor
1318PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001319 for (Pass *P : PassVector)
1320 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001321}
1322
Devang Patel9bdf7d42006-12-08 23:28:54 +00001323//===----------------------------------------------------------------------===//
1324// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001325// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1326Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001327 return PM.findAnalysisPass(ID, dir);
1328}
1329
Dan Gohmande6188a2010-08-12 23:50:08 +00001330Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001331 Function &F) {
1332 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1333}
1334
Devang Patela1514cb2006-12-07 19:39:39 +00001335//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001336// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001337
Dan Gohmande6188a2010-08-12 23:50:08 +00001338/// Execute all of the passes scheduled for execution by invoking
1339/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001340/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001341bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001342 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001343 return false;
1344
Devang Patele9585592006-12-08 01:38:28 +00001345 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001346 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001347
Xin Tong023e25a2018-07-22 05:27:41 +00001348 unsigned InstrCount = 0;
1349 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001350 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1352 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001353 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001354
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001355 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001356 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001357
Devang Patelabfbe3b2006-12-16 00:56:26 +00001358 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001359
Chris Lattner4c1e9542009-03-06 06:45:05 +00001360 {
1361 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001362 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001363 TimeRegion PassTimer(getPassTimer(BP));
Xin Tong023e25a2018-07-22 05:27:41 +00001364 if (EmitICRemark)
1365 InstrCount = initSizeRemarkInfo(M);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001366 LocalChanged |= BP->runOnBasicBlock(BB);
Xin Tong023e25a2018-07-22 05:27:41 +00001367 if (EmitICRemark)
1368 emitInstrCountChangedRemark(BP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001369 }
Devang Patel93a197c2006-12-14 00:08:04 +00001370
Dan Gohman74b189f2010-03-01 17:34:28 +00001371 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001372 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001373 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001374 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001375 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001376 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001377
Devang Patela273d1c2007-07-19 18:02:32 +00001378 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001379 removeNotPreservedAnalysis(BP);
1380 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001381 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001382 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001383
Bill Wendling6ce6d262009-12-25 13:50:18 +00001384 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001385}
1386
Devang Patel475c4532006-12-08 00:59:05 +00001387// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001388bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001389 bool Changed = false;
1390
Chris Lattner4c1e9542009-03-06 06:45:05 +00001391 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1392 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001393
1394 return Changed;
1395}
1396
Duncan Sands51495602009-02-13 09:42:34 +00001397bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001398 bool Changed = false;
1399
Pedro Artigas41b98842012-12-05 17:12:22 +00001400 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001401 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001402
1403 return Changed;
1404}
1405
Duncan Sands51495602009-02-13 09:42:34 +00001406bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001407 bool Changed = false;
1408
Devang Patelabfbe3b2006-12-16 00:56:26 +00001409 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1410 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001411 Changed |= BP->doInitialization(F);
1412 }
1413
1414 return Changed;
1415}
1416
Duncan Sands51495602009-02-13 09:42:34 +00001417bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001418 bool Changed = false;
1419
Devang Patelabfbe3b2006-12-16 00:56:26 +00001420 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1421 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001422 Changed |= BP->doFinalization(F);
1423 }
1424
1425 return Changed;
1426}
1427
1428
Devang Patela1514cb2006-12-07 19:39:39 +00001429//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001430// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001431
Devang Patel4e12f862006-11-08 10:44:40 +00001432/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001433FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001434 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001435 // FPM is the top level manager.
1436 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001437
Dan Gohman565df952008-03-13 02:08:36 +00001438 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001439 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001440}
1441
Devang Patelb67904d2006-12-13 02:36:01 +00001442FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001443 delete FPM;
1444}
1445
Dan Gohmande6188a2010-08-12 23:50:08 +00001446void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001447 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001448}
1449
Devang Patel9f3083e2006-11-15 19:39:54 +00001450/// run - Execute all of the passes scheduled for execution. Keep
1451/// track of whether any of the passes modifies the function, and if
1452/// so, return true.
1453///
Devang Patelb67904d2006-12-13 02:36:01 +00001454bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001455 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1456 report_fatal_error("Error reading bitcode file: " + EIB.message());
1457 });
Devang Patel272908d2006-12-08 22:57:48 +00001458 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001459}
1460
1461
Devang Patelff631ae2006-11-15 01:27:05 +00001462/// doInitialization - Run all of the initializers for the function passes.
1463///
Devang Patelb67904d2006-12-13 02:36:01 +00001464bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001465 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001466}
1467
Dan Gohmane6656eb2007-07-30 14:51:13 +00001468/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001469///
Devang Patelb67904d2006-12-13 02:36:01 +00001470bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001471 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001472}
1473
Devang Patela1514cb2006-12-07 19:39:39 +00001474//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001475// FunctionPassManagerImpl implementation
1476//
Duncan Sands51495602009-02-13 09:42:34 +00001477bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001478 bool Changed = false;
1479
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001480 dumpArguments();
1481 dumpPasses();
1482
Yaron Keren4849aa32015-06-05 17:48:47 +00001483 for (ImmutablePass *ImPass : getImmutablePasses())
1484 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001485
Chris Lattner4c1e9542009-03-06 06:45:05 +00001486 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1487 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001488
1489 return Changed;
1490}
1491
Duncan Sands51495602009-02-13 09:42:34 +00001492bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001493 bool Changed = false;
1494
Pedro Artigas41b98842012-12-05 17:12:22 +00001495 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001496 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001497
Yaron Keren4849aa32015-06-05 17:48:47 +00001498 for (ImmutablePass *ImPass : getImmutablePasses())
1499 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001500
Devang Patel67d6a5e2006-12-19 19:46:59 +00001501 return Changed;
1502}
1503
Devang Patelec9c58f2009-04-01 22:34:41 +00001504/// cleanup - After running all passes, clean up pass manager cache.
1505void FPPassManager::cleanup() {
1506 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1507 FunctionPass *FP = getContainedPass(Index);
1508 AnalysisResolver *AR = FP->getResolver();
1509 assert(AR && "Analysis Resolver is not set");
1510 AR->clearAnalysisImpls();
1511 }
1512}
1513
Torok Edwin24c78352009-06-29 18:49:09 +00001514void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1515 if (!wasRun)
1516 return;
1517 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1518 FPPassManager *FPPM = getContainedManager(Index);
1519 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1520 FPPM->getContainedPass(Index)->releaseMemory();
1521 }
1522 }
Torok Edwin896556e2009-06-29 21:05:10 +00001523 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001524}
1525
Devang Patel67d6a5e2006-12-19 19:46:59 +00001526// Execute all the passes managed by this top level manager.
1527// Return true if any function is modified by a pass.
1528bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001529 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530 TimingInfo::createTheTimeInfo();
1531
Devang Patele3068402006-12-21 00:16:50 +00001532 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001533 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001534 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001535 F.getContext().yield();
1536 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001537
1538 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1539 getContainedManager(Index)->cleanup();
1540
Torok Edwin24c78352009-06-29 18:49:09 +00001541 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001542 return Changed;
1543}
1544
1545//===----------------------------------------------------------------------===//
1546// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001547
Devang Patel8c78a0b2007-05-03 01:11:54 +00001548char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001549/// Print passes managed by this manager
1550void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001551 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001552 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1553 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001554 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001555 dumpLastUses(FP, Offset+1);
1556 }
1557}
1558
1559
Dan Gohmande6188a2010-08-12 23:50:08 +00001560/// Execute all of the passes scheduled for execution by invoking
1561/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001562/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001563bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001564 if (F.isDeclaration())
1565 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001566
1567 bool Changed = false;
Jessica Paquettee49374d2018-05-18 17:26:39 +00001568 Module &M = *F.getParent();
Devang Patelcbbf2912008-03-20 01:09:53 +00001569 // Collect inherited analysis from Module level pass manager.
1570 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001571
Xin Tong023e25a2018-07-22 05:27:41 +00001572 unsigned InstrCount = 0;
1573 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001574 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1575 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001576 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001577
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001578 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001579 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001580
Devang Patelabfbe3b2006-12-16 00:56:26 +00001581 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001582
Chris Lattner4c1e9542009-03-06 06:45:05 +00001583 {
1584 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001585 TimeRegion PassTimer(getPassTimer(FP));
Xin Tong023e25a2018-07-22 05:27:41 +00001586 if (EmitICRemark)
1587 InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001588 LocalChanged |= FP->runOnFunction(F);
Xin Tong023e25a2018-07-22 05:27:41 +00001589 if (EmitICRemark)
1590 emitInstrCountChangedRemark(FP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001591 }
Devang Patel93a197c2006-12-14 00:08:04 +00001592
Dan Gohman74b189f2010-03-01 17:34:28 +00001593 Changed |= LocalChanged;
1594 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001595 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001596 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001597 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001598
Devang Patela273d1c2007-07-19 18:02:32 +00001599 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001600 removeNotPreservedAnalysis(FP);
1601 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001602 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001603 }
1604 return Changed;
1605}
1606
Devang Patel67d6a5e2006-12-19 19:46:59 +00001607bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001608 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001609
Serge Pavloved5eb932017-01-15 10:23:18 +00001610 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001611 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001612
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001613 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001614}
1615
Duncan Sands51495602009-02-13 09:42:34 +00001616bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001617 bool Changed = false;
1618
Chris Lattner4c1e9542009-03-06 06:45:05 +00001619 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1620 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001621
Devang Patelff631ae2006-11-15 01:27:05 +00001622 return Changed;
1623}
1624
Duncan Sands51495602009-02-13 09:42:34 +00001625bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001626 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001627
1628 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001629 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001630
Devang Patelff631ae2006-11-15 01:27:05 +00001631 return Changed;
1632}
1633
Devang Patela1514cb2006-12-07 19:39:39 +00001634//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001635// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001636
Dan Gohmande6188a2010-08-12 23:50:08 +00001637/// Execute all of the passes scheduled for execution by invoking
1638/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001639/// the module, and if so, return true.
1640bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001641MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001642 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001643
Torok Edwin24c78352009-06-29 18:49:09 +00001644 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001645 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1646 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001647 Changed |= FPP->doInitialization(M);
1648 }
1649
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001650 // Initialize module passes
1651 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1652 Changed |= getContainedPass(Index)->doInitialization(M);
1653
Xin Tong023e25a2018-07-22 05:27:41 +00001654 unsigned InstrCount = 0;
1655 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Devang Patelabfbe3b2006-12-16 00:56:26 +00001656 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1657 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001658 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001659
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001660 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001661 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001662
Devang Patelabfbe3b2006-12-16 00:56:26 +00001663 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001664
Chris Lattner4c1e9542009-03-06 06:45:05 +00001665 {
1666 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001667 TimeRegion PassTimer(getPassTimer(MP));
1668
Xin Tong023e25a2018-07-22 05:27:41 +00001669 if (EmitICRemark)
1670 InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001671 LocalChanged |= MP->runOnModule(M);
Xin Tong023e25a2018-07-22 05:27:41 +00001672 if (EmitICRemark)
1673 emitInstrCountChangedRemark(MP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001674 }
Devang Patel93a197c2006-12-14 00:08:04 +00001675
Dan Gohman74b189f2010-03-01 17:34:28 +00001676 Changed |= LocalChanged;
1677 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001678 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001679 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001680 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001681 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001682
Devang Patela273d1c2007-07-19 18:02:32 +00001683 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001684 removeNotPreservedAnalysis(MP);
1685 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001686 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001687 }
Torok Edwin24c78352009-06-29 18:49:09 +00001688
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001689 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001690 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001691 Changed |= getContainedPass(Index)->doFinalization(M);
1692
Torok Edwin24c78352009-06-29 18:49:09 +00001693 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001694 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1695 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001696 // We don't know when is the last time an on-the-fly pass is run,
1697 // so we need to releaseMemory / finalize here
1698 FPP->releaseMemoryOnTheFly();
1699 Changed |= FPP->doFinalization(M);
1700 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001701
Devang Patel05e1a972006-11-07 22:03:15 +00001702 return Changed;
1703}
1704
Devang Patele64d3052007-04-16 20:12:57 +00001705/// Add RequiredPass into list of lower level passes required by pass P.
1706/// RequiredPass is run on the fly by Pass Manager when P requests it
1707/// through getAnalysis interface.
1708void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001709 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1710 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001711 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001712 RequiredPass->getPotentialPassManagerType()) &&
1713 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001714 if (!RequiredPass)
1715 return;
Devang Patele64d3052007-04-16 20:12:57 +00001716
Devang Patel68f72b12007-04-26 17:50:19 +00001717 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001718 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001719 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001720 // FPP is the top level manager.
1721 FPP->setTopLevelManager(FPP);
1722
Devang Patel69e9f6d2007-04-16 20:27:05 +00001723 OnTheFlyManagers[P] = FPP;
1724 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001725 const PassInfo *RequiredPassPI =
1726 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001727
Craig Topperc6207612014-04-09 06:08:46 +00001728 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001729 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1730 FoundPass =
1731 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001732 }
Andrew Trick02066f22014-04-08 03:40:34 +00001733 if (!FoundPass) {
1734 FoundPass = RequiredPass;
1735 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001736 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001737 FPP->add(RequiredPass);
1738 }
1739 // Register P as the last user of FoundPass or RequiredPass.
1740 SmallVector<Pass *, 1> LU;
1741 LU.push_back(FoundPass);
1742 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001743}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001744
Dan Gohmande6188a2010-08-12 23:50:08 +00001745/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001746/// required by module pass MP. Instantiate analysis pass, by using
1747/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001748Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001749 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001750 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001751
Torok Edwin24c78352009-06-29 18:49:09 +00001752 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001753 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001754 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001755}
1756
1757
Devang Patela1514cb2006-12-07 19:39:39 +00001758//===----------------------------------------------------------------------===//
1759// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001760
Devang Patelab97cf42006-12-13 00:09:23 +00001761//
Devang Patelc290c8a2006-11-07 22:23:34 +00001762/// run - Execute all of the passes scheduled for execution. Keep track of
1763/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001764bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001765 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001766 TimingInfo::createTheTimeInfo();
1767
Devang Patelcfd70c42006-12-13 22:10:00 +00001768 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001769 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001770
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001771 for (ImmutablePass *ImPass : getImmutablePasses())
1772 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001773
Devang Patele3068402006-12-21 00:16:50 +00001774 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001775 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001776 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001777 M.getContext().yield();
1778 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001779
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001780 for (ImmutablePass *ImPass : getImmutablePasses())
1781 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001782
Devang Patelc290c8a2006-11-07 22:23:34 +00001783 return Changed;
1784}
Devang Patel376fefa2006-11-08 10:29:57 +00001785
Devang Patela1514cb2006-12-07 19:39:39 +00001786//===----------------------------------------------------------------------===//
1787// PassManager implementation
1788
Devang Patel376fefa2006-11-08 10:29:57 +00001789/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001790PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001791 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001792 // PM is the top level manager
1793 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001794}
1795
Devang Patelb67904d2006-12-13 02:36:01 +00001796PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001797 delete PM;
1798}
1799
Chris Lattner60987362009-03-06 05:53:14 +00001800void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001801 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001802}
1803
1804/// run - Execute all of the passes scheduled for execution. Keep track of
1805/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001806bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001807 return PM->run(M);
1808}
1809
Devang Patelb8817b92006-12-14 00:59:42 +00001810//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001811// TimingInfo implementation
1812
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001813bool llvm::TimePassesIsEnabled = false;
Zachary Turner8065f0b2017-12-01 00:53:10 +00001814static cl::opt<bool, true> EnableTiming(
1815 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
1816 cl::desc("Time each pass, printing elapsed time for each on exit"));
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001817
Devang Patelb8817b92006-12-14 00:59:42 +00001818// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001819// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001820// null. It may be called multiple times.
1821void TimingInfo::createTheTimeInfo() {
1822 if (!TimePassesIsEnabled || TheTimeInfo) return;
1823
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001824 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001825 // This guarantees that the object will be constructed before static globals,
1826 // thus it will be destroyed before them.
1827 static ManagedStatic<TimingInfo> TTI;
1828 TheTimeInfo = &*TTI;
1829}
1830
Devang Patel1c3633e2007-01-29 23:10:37 +00001831/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001832Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001833 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001834 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001835 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001836}
1837
James Henderson852f6fd2017-05-16 09:43:21 +00001838/// If timing is enabled, report the times collected up to now and then reset
1839/// them.
1840void llvm::reportAndResetTimings() {
1841 if (TheTimeInfo)
1842 TheTimeInfo->print();
1843}
1844
Devang Patel1c56a632007-01-08 19:29:38 +00001845//===----------------------------------------------------------------------===//
1846// PMStack implementation
1847//
Devang Patelad98d232007-01-11 22:15:30 +00001848
Devang Patel1c56a632007-01-08 19:29:38 +00001849// Pop Pass Manager from the stack and clear its analysis info.
1850void PMStack::pop() {
1851
1852 PMDataManager *Top = this->top();
1853 Top->initializeAnalysisInfo();
1854
1855 S.pop_back();
1856}
1857
1858// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001859void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001860 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001861 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001862
Chris Lattner60987362009-03-06 05:53:14 +00001863 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001864 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1865 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001866 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001867
Chris Lattner60987362009-03-06 05:53:14 +00001868 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001869 TPM->addIndirectPassManager(PM);
1870 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001871 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001872 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001873 assert((PM->getPassManagerType() == PMT_ModulePassManager
1874 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001875 && "pushing bad pass manager to PMStack");
1876 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001877 }
1878
Devang Patel15701b52007-01-11 00:19:00 +00001879 S.push_back(PM);
1880}
1881
1882// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001883LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001884 for (PMDataManager *Manager : S)
1885 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001886
Devang Patel15701b52007-01-11 00:19:00 +00001887 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001888 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001889}
1890
Devang Patel1c56a632007-01-08 19:29:38 +00001891/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001892/// add self into that manager.
1893void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001894 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001895 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001896 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001897 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1898 if (TopPMType == PreferredType)
1899 break; // We found desired pass manager
1900 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001901 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001902 else
1903 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001904 }
Devang Patel18ff6362008-09-09 21:38:40 +00001905 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001906 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001907}
1908
Devang Patel3312f752007-01-16 21:43:18 +00001909/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001910/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001911void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001912 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001913
Andrew Trickcbc845f2012-02-01 07:16:20 +00001914 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001915 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001916 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1917 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001918 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001919 break;
Devang Patel3312f752007-01-16 21:43:18 +00001920 }
Devang Patel3312f752007-01-16 21:43:18 +00001921
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001922 // Create new Function Pass Manager if needed.
1923 FPPassManager *FPP;
1924 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1925 FPP = (FPPassManager *)PMS.top();
1926 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001927 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1928 PMDataManager *PMD = PMS.top();
1929
1930 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001931 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001932 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001933
1934 // [2] Set up new manager's top level manager
1935 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1936 TPM->addIndirectPassManager(FPP);
1937
1938 // [3] Assign manager to manage this new manager. This may create
1939 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001940 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001941
1942 // [4] Push new manager into PMS
1943 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001944 }
1945
Devang Patel3312f752007-01-16 21:43:18 +00001946 // Assign FPP as the manager of this pass.
1947 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001948}
1949
Devang Patel3312f752007-01-16 21:43:18 +00001950/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001951/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001952void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001953 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001954 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001955
Devang Patel15701b52007-01-11 00:19:00 +00001956 // Basic Pass Manager is a leaf pass manager. It does not handle
1957 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001958 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001959 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1960 BBP = (BBPassManager *)PMS.top();
1961 } else {
1962 // If leaf manager is not Basic Block Pass manager then create new
1963 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001964 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1965 PMDataManager *PMD = PMS.top();
1966
1967 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001968 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001969
1970 // [2] Set up new manager's top level manager
1971 // Basic Block Pass Manager does not live by itself
1972 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1973 TPM->addIndirectPassManager(BBP);
1974
Devang Patel15701b52007-01-11 00:19:00 +00001975 // [3] Assign manager to manage this new manager. This may create
1976 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001977 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001978
Devang Patel3312f752007-01-16 21:43:18 +00001979 // [4] Push new manager into PMS
1980 PMS.push(BBP);
1981 }
Devang Patel1c56a632007-01-08 19:29:38 +00001982
Devang Patel3312f752007-01-16 21:43:18 +00001983 // Assign BBP as the manager of this pass.
1984 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001985}
1986
Dan Gohmand3a20c92008-03-11 16:41:42 +00001987PassManagerBase::~PassManagerBase() {}