blob: cd130ba28654ae065b68358e35948a82783cf00e [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.
140 if (M.getContext().getDiagHandlerPtr()->isAnalysisRemarkEnabled("size-info"))
141 return M.getInstructionCount();
142 return 0;
143}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000144
Jessica Paquettee49374d2018-05-18 17:26:39 +0000145void PMDataManager::emitInstrCountChangedRemark(Pass *P, Module &M,
146 unsigned CountBefore) {
147 // Did the user request the remark? If not, quit.
148 if (!M.getContext().getDiagHandlerPtr()->isAnalysisRemarkEnabled("size-info"))
149 return;
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000150
Jessica Paquettee49374d2018-05-18 17:26:39 +0000151 // We need a function containing at least one basic block in order to output
152 // remarks. Since it's possible that the first function in the module doesn't
153 // actually contain a basic block, we have to go and find one that's suitable
154 // for emitting remarks.
155 auto It = std::find_if(M.begin(), M.end(),
156 [](const Function &Fn) { return !Fn.empty(); });
157
158 // Didn't find a function. Quit.
159 if (It == M.end())
160 return;
161
162 // We found a function containing at least one basic block.
163 Function *F = &*It;
164
165 // How many instructions are in the module now?
166 unsigned CountAfter = M.getInstructionCount();
167
168 // If there was no change, don't emit a remark.
169 if (CountBefore == CountAfter)
170 return;
171
172 // If it's a pass manager, don't emit a remark. (This hinges on the assumption
173 // that the only passes that return non-null with getAsPMDataManager are pass
174 // managers.) The reason we have to do this is to avoid emitting remarks for
175 // CGSCC passes.
176 if (P->getAsPMDataManager())
177 return;
178
179 // Compute a possibly negative delta between the instruction count before
180 // running P, and after running P.
Jessica Paquettec6048172018-05-18 20:04:21 +0000181 int64_t Delta =
182 static_cast<int64_t>(CountAfter) - static_cast<int64_t>(CountBefore);
Jessica Paquettee49374d2018-05-18 17:26:39 +0000183
184 BasicBlock &BB = *F->begin();
185 OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
186 DiagnosticLocation(), &BB);
187 // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
188 // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
189 R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
190 << ": IR instruction count changed from "
191 << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
192 << " to "
193 << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
194 << "; Delta: "
195 << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
196 F->getContext().diagnose(R); // Not using ORE for layering reasons.
197}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000198
Chris Lattner4c1e9542009-03-06 06:45:05 +0000199void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000200 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000201 OS << "Releasing pass '";
202 else
203 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000204
Chris Lattner4c1e9542009-03-06 06:45:05 +0000205 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000206
Chris Lattner4c1e9542009-03-06 06:45:05 +0000207 if (M) {
208 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
209 return;
210 }
Craig Topperc6207612014-04-09 06:08:46 +0000211 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000212 OS << '\n';
213 return;
214 }
215
Dan Gohman79fc0e92009-03-10 18:47:59 +0000216 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000217 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000218 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000219 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000220 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000221 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000222 OS << "value";
223
224 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000225 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000226 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000227}
228
229
Devang Patelffca9102006-12-15 19:39:30 +0000230namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000231//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000232// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000233//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000234/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000235/// pass together and sequence them to process one basic block before
236/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000237class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000238
239public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000240 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000241 explicit BBPassManager()
242 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000243
Devang Patelca58e352006-11-08 10:05:38 +0000244 /// Execute all of the passes scheduled for execution. Keep track of
245 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000246 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000247
Devang Patelf9d96b92006-12-07 19:57:52 +0000248 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000249 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000250 Info.setPreservesAll();
251 }
252
Craig Topperf398d7c2014-03-05 06:35:38 +0000253 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000254 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000255 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000256 bool doFinalization(Function &F);
257
Craig Topperf398d7c2014-03-05 06:35:38 +0000258 PMDataManager *getAsPMDataManager() override { return this; }
259 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000260
Mehdi Amini117296c2016-10-01 02:56:57 +0000261 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000262
Devang Pateleda56172006-12-12 23:34:33 +0000263 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000264 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000265 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000266 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
267 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000268 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000269 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000270 }
271 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000272
273 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000274 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000275 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
276 return BP;
277 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000278
Craig Topperf398d7c2014-03-05 06:35:38 +0000279 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000280 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000281 }
Devang Patelca58e352006-11-08 10:05:38 +0000282};
283
Devang Patel8c78a0b2007-05-03 01:11:54 +0000284char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000285} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000286
Devang Patele7599552007-01-12 18:52:44 +0000287namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000288namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000289//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000291//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000292/// FunctionPassManagerImpl manages FPPassManagers
293class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000294 public PMDataManager,
295 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000296 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000297private:
298 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000300 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000301 explicit FunctionPassManagerImpl() :
302 Pass(PT_PassManager, ID), PMDataManager(),
303 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000304
Matthias Braun0880c602014-12-12 01:27:01 +0000305 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000306 void add(Pass *P) {
307 schedulePass(P);
308 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000309
310 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000311 Pass *createPrinterPass(raw_ostream &O,
312 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000313 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000314 }
315
Torok Edwin24c78352009-06-29 18:49:09 +0000316 // Prepare for running an on the fly pass, freeing memory if needed
317 // from a previous run.
318 void releaseMemoryOnTheFly();
319
Devang Patel67d6a5e2006-12-19 19:46:59 +0000320 /// run - Execute all of the passes scheduled for execution. Keep track of
321 /// whether any of the passes modifies the module, and if so, return true.
322 bool run(Function &F);
323
324 /// doInitialization - Run all of the initializers for the function passes.
325 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000326 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000327
Dan Gohmane6656eb2007-07-30 14:51:13 +0000328 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000329 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000330 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000331
Dan Gohmande6188a2010-08-12 23:50:08 +0000332
Craig Topperf398d7c2014-03-05 06:35:38 +0000333 PMDataManager *getAsPMDataManager() override { return this; }
334 Pass *getAsPass() override { return this; }
335 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000336 return PMT_FunctionPassManager;
337 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000338
Devang Patel67d6a5e2006-12-19 19:46:59 +0000339 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000340 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000341 Info.setPreservesAll();
342 }
343
Devang Patel67d6a5e2006-12-19 19:46:59 +0000344 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000345 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000346 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
347 return FP;
348 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000349};
350
David Blaikiea379b1812011-12-20 02:50:00 +0000351void FunctionPassManagerImpl::anchor() {}
352
Devang Patel8c78a0b2007-05-03 01:11:54 +0000353char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000354} // End of legacy namespace
355} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000356
Chandler Carruth7caea412013-11-09 12:26:54 +0000357namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000358//===----------------------------------------------------------------------===//
359// MPPassManager
360//
361/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000362/// It batches all Module passes and function pass managers together and
363/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000364class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000365public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000366 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000367 explicit MPPassManager() :
368 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000369
370 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000371 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000372 for (auto &OnTheFlyManager : OnTheFlyManagers) {
373 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000374 delete FPP;
375 }
376 }
377
Dan Gohmande6188a2010-08-12 23:50:08 +0000378 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000379 Pass *createPrinterPass(raw_ostream &O,
380 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000381 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000382 }
383
Devang Patelca58e352006-11-08 10:05:38 +0000384 /// run - Execute all of the passes scheduled for execution. Keep track of
385 /// whether any of the passes modifies the module, and if so, return true.
386 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000387
Pedro Artigase4348b02012-12-03 21:56:57 +0000388 using llvm::Pass::doInitialization;
389 using llvm::Pass::doFinalization;
390
Devang Patelf9d96b92006-12-07 19:57:52 +0000391 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000392 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000393 Info.setPreservesAll();
394 }
395
Devang Patele64d3052007-04-16 20:12:57 +0000396 /// Add RequiredPass into list of lower level passes required by pass P.
397 /// RequiredPass is run on the fly by Pass Manager when P requests it
398 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000399 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000400
Dan Gohmande6188a2010-08-12 23:50:08 +0000401 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000402 /// required by module pass MP. Instantiate analysis pass, by using
403 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000404 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000405
Mehdi Amini117296c2016-10-01 02:56:57 +0000406 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000407
Craig Topperf398d7c2014-03-05 06:35:38 +0000408 PMDataManager *getAsPMDataManager() override { return this; }
409 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000410
Devang Pateleda56172006-12-12 23:34:33 +0000411 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000412 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000413 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000414 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
415 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000416 MP->dumpPassStructure(Offset + 1);
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000417 MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
418 OnTheFlyManagers.find(MP);
Dan Gohman83ff1842009-07-01 23:12:33 +0000419 if (I != OnTheFlyManagers.end())
420 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000421 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000422 }
423 }
424
Devang Patelabfbe3b2006-12-16 00:56:26 +0000425 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000426 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000427 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000428 }
429
Craig Topperf398d7c2014-03-05 06:35:38 +0000430 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000431 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000432 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000433
434 private:
435 /// Collection of on the fly FPPassManagers. These managers manage
436 /// function passes that are required by module passes.
Florian Hahna4ffa3a2018-05-24 21:33:17 +0000437 MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000438};
439
Devang Patel8c78a0b2007-05-03 01:11:54 +0000440char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000441} // End anonymous namespace
442
443namespace llvm {
444namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000445//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000446// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000447//
Devang Patel09f162c2007-05-01 21:15:47 +0000448
Devang Patel67d6a5e2006-12-19 19:46:59 +0000449/// PassManagerImpl manages MPPassManagers
450class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000451 public PMDataManager,
452 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000453 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000454
455public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000456 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000457 explicit PassManagerImpl() :
458 Pass(PT_PassManager, ID), PMDataManager(),
459 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000460
Matthias Braun0880c602014-12-12 01:27:01 +0000461 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000462 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000463 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000464 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000465
466 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000467 Pass *createPrinterPass(raw_ostream &O,
468 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000469 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000470 }
471
Devang Patel376fefa2006-11-08 10:29:57 +0000472 /// run - Execute all of the passes scheduled for execution. Keep track of
473 /// whether any of the passes modifies the module, and if so, return true.
474 bool run(Module &M);
475
Pedro Artigase4348b02012-12-03 21:56:57 +0000476 using llvm::Pass::doInitialization;
477 using llvm::Pass::doFinalization;
478
Devang Patelf9d96b92006-12-07 19:57:52 +0000479 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000480 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000481 Info.setPreservesAll();
482 }
483
Craig Topperf398d7c2014-03-05 06:35:38 +0000484 PMDataManager *getAsPMDataManager() override { return this; }
485 Pass *getAsPass() override { return this; }
486 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000487 return PMT_ModulePassManager;
488 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000489
Devang Patel67d6a5e2006-12-19 19:46:59 +0000490 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000491 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000492 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
493 return MP;
494 }
Devang Patel376fefa2006-11-08 10:29:57 +0000495};
496
David Blaikiea379b1812011-12-20 02:50:00 +0000497void PassManagerImpl::anchor() {}
498
Devang Patel8c78a0b2007-05-03 01:11:54 +0000499char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000500} // End of legacy namespace
501} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000502
503namespace {
504
505//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000506/// TimingInfo Class - This class is used to calculate information about the
507/// amount of time each pass takes to execute. This only happens when
508/// -time-passes is enabled on the command line.
509///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000510
Owen Anderson5a6960f2009-06-18 20:51:00 +0000511static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000512
Nick Lewycky02d5f772009-10-25 06:33:48 +0000513class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000514 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000515 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000516public:
517 // Use 'create' member to get this.
Matthias Braun9f15a792016-11-18 19:43:18 +0000518 TimingInfo() : TG("pass", "... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000519
Devang Patel1c3633e2007-01-29 23:10:37 +0000520 // TimingDtor - Print out information about timing information
521 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000522 // Delete all of the timers, which accumulate their info into the
523 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000524 for (auto &I : TimingData)
525 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000526 // TimerGroup is deleted next, printing the report.
527 }
528
529 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000530 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000531 // null. It may be called multiple times.
532 static void createTheTimeInfo();
533
James Henderson852f6fd2017-05-16 09:43:21 +0000534 // print - Prints out timing information and then resets the timers.
535 void print() {
536 TG.print(*CreateInfoOutputFile());
537 }
538
Chris Lattner707431c2010-03-30 04:03:22 +0000539 /// getPassTimer - Return the timer for the specified pass if it exists.
540 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000541 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000542 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000543
Owen Anderson5c96ef72009-07-07 18:33:04 +0000544 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000545 Timer *&T = TimingData[P];
Matthias Braun9f15a792016-11-18 19:43:18 +0000546 if (!T) {
547 StringRef PassName = P->getPassName();
Francis Visoiu Mistrih03185792018-06-13 21:03:56 +0000548 StringRef PassArgument;
549 if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
550 PassArgument = PI->getPassArgument();
551 T = new Timer(PassArgument.empty() ? PassName : PassArgument, PassName,
552 TG);
Matthias Braun9f15a792016-11-18 19:43:18 +0000553 }
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000554 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000555 }
556};
557
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000558} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000559
Dan Gohmand78c4002008-05-13 00:00:25 +0000560static TimingInfo *TheTimeInfo;
561
Devang Patela1514cb2006-12-07 19:39:39 +0000562//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000563// PMTopLevelManager implementation
564
Devang Patel4268fc02007-01-16 02:00:38 +0000565/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000566PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
567 PMDM->setTopLevelManager(this);
568 addPassManager(PMDM);
569 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000570}
571
Devang Patelafb1f3622006-12-12 22:35:25 +0000572/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000573void
Bill Wendlingea857e12012-05-14 07:53:40 +0000574PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000575 unsigned PDepth = 0;
576 if (P->getResolver())
577 PDepth = P->getResolver()->getPMDataManager().getDepth();
578
Yaron Keren4849aa32015-06-05 17:48:47 +0000579 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000580 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000581
Devang Patel01919d22007-03-08 19:05:01 +0000582 if (P == AP)
583 continue;
584
Tobias Grosserf07426b2011-01-20 21:03:22 +0000585 // Update the last users of passes that are required transitive by AP.
586 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
587 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
588 SmallVector<Pass *, 12> LastUses;
589 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000590 for (AnalysisID ID : IDs) {
591 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000592 assert(AnalysisPass && "Expected analysis pass to exist.");
593 AnalysisResolver *AR = AnalysisPass->getResolver();
594 assert(AR && "Expected analysis resolver to exist.");
595 unsigned APDepth = AR->getPMDataManager().getDepth();
596
597 if (PDepth == APDepth)
598 LastUses.push_back(AnalysisPass);
599 else if (PDepth > APDepth)
600 LastPMUses.push_back(AnalysisPass);
601 }
602
603 setLastUser(LastUses, P);
604
605 // If this pass has a corresponding pass manager, push higher level
606 // analysis to this pass manager.
607 if (P->getResolver())
608 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
609
610
Devang Patelafb1f3622006-12-12 22:35:25 +0000611 // If AP is the last user of other passes then make P last user of
612 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000613 for (auto LU : LastUser) {
614 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000615 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000616 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000617 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000618 }
619 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000620}
621
622/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000623void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000624 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000625 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000626 InversedLastUser.find(P);
627 if (DMI == InversedLastUser.end())
628 return;
629
630 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000631 for (Pass *LUP : LU) {
632 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000633 }
634
Devang Patelafb1f3622006-12-12 22:35:25 +0000635}
636
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000637AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000638 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000639 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000640 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000641 AnUsage = DMI->second;
642 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000643 // Look up the analysis usage from the pass instance (different instances
644 // of the same pass can produce different results), but unique the
645 // resulting object to reduce memory usage. This helps to greatly reduce
646 // memory usage when we have many instances of only a few pass types
647 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
648 // of dependencies.
649 AnalysisUsage AU;
650 P->getAnalysisUsage(AU);
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000651
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000652 AUFoldingSetNode* Node = nullptr;
653 FoldingSetNodeID ID;
654 AUFoldingSetNode::Profile(ID, AU);
655 void *IP = nullptr;
656 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
657 Node = N;
658 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000659 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000660 UniqueAnalysisUsages.InsertNode(Node, IP);
661 }
662 assert(Node && "cached analysis usage must be non null");
663
664 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000665 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000666 }
667 return AnUsage;
668}
669
Devang Patelafb1f3622006-12-12 22:35:25 +0000670/// Schedule pass P for execution. Make sure that passes required by
671/// P are run before P is run. Update analysis info maintained by
672/// the manager. Remove dead passes. This is a recursive function.
673void PMTopLevelManager::schedulePass(Pass *P) {
674
Devang Patel3312f752007-01-16 21:43:18 +0000675 // TODO : Allocate function manager for this pass, other wise required set
676 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000677
Devang Pateld74ede72007-03-06 01:06:16 +0000678 // Give pass a chance to prepare the stage.
679 P->preparePassManager(activeStack);
680
Devang Patel864970e2008-03-18 00:39:19 +0000681 // If P is an analysis pass and it is available then do not
682 // generate the analysis again. Stale analysis info should not be
683 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000684 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000685 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000686 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000687 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000688 }
Devang Patel864970e2008-03-18 00:39:19 +0000689
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000690 AnalysisUsage *AnUsage = findAnalysisUsage(P);
691
Devang Patelfdee7032008-08-14 23:07:48 +0000692 bool checkAnalysis = true;
693 while (checkAnalysis) {
694 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000695
Devang Patelfdee7032008-08-14 23:07:48 +0000696 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000697 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000698
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000699 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000700 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000701 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000702
Craig Topperc6207612014-04-09 06:08:46 +0000703 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000704 // Pass P is not in the global PassRegistry
705 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
706 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
707 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000708 for (const AnalysisID ID2 : RequiredSet) {
709 if (ID == ID2)
710 break;
711 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000712 if (AnalysisPass2) {
713 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000714 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000715 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
716 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
717 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
718 }
719 }
720 }
721
Andrew Trick6bbaf132011-06-03 00:48:58 +0000722 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000723 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000724 if (P->getPotentialPassManagerType () ==
725 AnalysisPass->getPotentialPassManagerType())
726 // Schedule analysis pass that is managed by the same pass manager.
727 schedulePass(AnalysisPass);
728 else if (P->getPotentialPassManagerType () >
729 AnalysisPass->getPotentialPassManagerType()) {
730 // Schedule analysis pass that is managed by a new manager.
731 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000732 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000733 // are already checked are still available.
734 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000735 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000736 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000737 // passes are run on the fly.
738 delete AnalysisPass;
739 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000740 }
741 }
742
743 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000744 if (ImmutablePass *IP = P->getAsImmutablePass()) {
745 // P is a immutable pass and it will be managed by this
746 // top level manager. Set up analysis resolver to connect them.
747 PMDataManager *DM = getAsPMDataManager();
748 AnalysisResolver *AR = new AnalysisResolver(*DM);
749 P->setResolver(AR);
750 DM->initializeAnalysisImpl(P);
751 addImmutablePass(IP);
752 DM->recordAvailableAnalysis(IP);
753 return;
754 }
755
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000756 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000757 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000758 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000759 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
760 }
761
762 // Add the requested pass to the best available pass manager.
763 P->assignPassManager(activeStack, getTopLevelPassManagerType());
764
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000765 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000766 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000767 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000768 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
769 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000770}
771
772/// Find the pass that implements Analysis AID. Search immutable
773/// passes and all pass managers. If desired pass is not found
774/// then return NULL.
775Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000776 // For immutable passes we have a direct mapping from ID to pass, so check
777 // that first.
778 if (Pass *P = ImmutablePassMap.lookup(AID))
779 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000780
Devang Patelcd6ba152006-12-12 22:50:05 +0000781 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000782 for (PMDataManager *PassManager : PassManagers)
783 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000784 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000785
786 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000787 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
788 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000789 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000790
Craig Topperc6207612014-04-09 06:08:46 +0000791 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000792}
793
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000794const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
795 const PassInfo *&PI = AnalysisPassInfos[AID];
796 if (!PI)
797 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
798 else
799 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
800 "The pass info pointer changed for an analysis ID!");
801
802 return PI;
803}
804
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000805void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
806 P->initializePass();
807 ImmutablePasses.push_back(P);
808
809 // Add this pass to the map from its analysis ID. We clobber any prior runs
810 // of the pass in the map so that the last one added is the one found when
811 // doing lookups.
812 AnalysisID AID = P->getPassID();
813 ImmutablePassMap[AID] = P;
814
815 // Also add any interfaces implemented by the immutable pass to the map for
816 // fast lookup.
817 const PassInfo *PassInf = findAnalysisPassInfo(AID);
818 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000819 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000820 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
821}
822
Devang Pateleda56172006-12-12 23:34:33 +0000823// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000824void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000825
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000826 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000827 return;
828
Devang Pateleda56172006-12-12 23:34:33 +0000829 // Print out the immutable passes
830 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000831 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000832 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000833
Dan Gohmanf71c5212010-08-19 01:29:07 +0000834 // Every class that derives from PMDataManager also derives from Pass
835 // (sometimes indirectly), but there's no inheritance relationship
836 // between PMDataManager and Pass, so we have to getAsPass to get
837 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000838 for (PMDataManager *Manager : PassManagers)
839 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000840}
841
Devang Patel991aeba2006-12-15 20:13:01 +0000842void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000843
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000844 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000845 return;
846
David Greene994e1bb2010-01-05 01:30:02 +0000847 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000848 for (ImmutablePass *P : ImmutablePasses)
849 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000850 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000851 if (!PI->isAnalysisGroup())
852 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000853 }
Yaron Keren83009952016-04-28 14:49:44 +0000854 for (PMDataManager *PM : PassManagers)
855 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000856 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000857}
858
Devang Patele3068402006-12-21 00:16:50 +0000859void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000860 for (PMDataManager *PM : PassManagers)
861 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000862
Devang Patele3068402006-12-21 00:16:50 +0000863 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000864 for (PMDataManager *IPM : IndirectPassManagers)
865 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000866
Yaron Kerenbd873192016-10-02 19:21:41 +0000867 for (auto LU : LastUser) {
868 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
869 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000870 }
Devang Patele3068402006-12-21 00:16:50 +0000871}
872
Devang Patele7599552007-01-12 18:52:44 +0000873/// Destructor
874PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000875 for (PMDataManager *PM : PassManagers)
876 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000877
Yaron Keren83009952016-04-28 14:49:44 +0000878 for (ImmutablePass *P : ImmutablePasses)
879 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000880}
881
Devang Patelafb1f3622006-12-12 22:35:25 +0000882//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000883// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000884
Devang Patel643676c2006-11-11 01:10:19 +0000885/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000886void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000887 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000888
Chris Lattner60987362009-03-06 05:53:14 +0000889 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000890
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000891 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000892
Dan Gohmande6188a2010-08-12 23:50:08 +0000893 // This pass is the current implementation of all of the interfaces it
894 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000895 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000896 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000897 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000898 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000899 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000900}
901
Devang Patel9d9fc902007-03-06 17:52:53 +0000902// Return true if P preserves high level analysis used by other
903// passes managed by this manager
904bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000905 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000906 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000907 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000908
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000909 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000910 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000911 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000912 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000913 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000914 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000915
Devang Patel9d9fc902007-03-06 17:52:53 +0000916 return true;
917}
918
Chris Lattner02eb94c2008-08-07 07:34:50 +0000919/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000920void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000921 // Don't do this unless assertions are enabled.
922#ifdef NDEBUG
923 return;
924#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000925 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
926 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000927
Devang Patelef432532007-07-19 05:36:09 +0000928 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000929 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000930 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000931 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000932 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000933 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000934 }
935}
936
Devang Patel67c79a42008-07-01 19:50:56 +0000937/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000938void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000939 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
940 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000941 return;
942
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000943 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000944 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000945 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000946 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000947 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000948 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000949 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000950 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000951 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000952 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
953 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000954 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000955 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000956 }
Devang Patel349170f2006-11-11 01:24:55 +0000957 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000958
Devang Patel42dd1e92007-03-06 01:55:46 +0000959 // Check inherited analysis also. If P is not preserving analysis
960 // provided by parent manager then remove it here.
961 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
962
963 if (!InheritedAnalysis[Index])
964 continue;
965
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000966 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000967 I = InheritedAnalysis[Index]->begin(),
968 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000969 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000970 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000971 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000972 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000973 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000974 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000975 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
976 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000977 }
Devang Patel01919d22007-03-08 19:05:01 +0000978 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000979 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000980 }
981 }
Devang Patelf68a3492006-11-07 22:35:17 +0000982}
983
Devang Patelca189262006-11-14 03:05:08 +0000984/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000985void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000986 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000987
Devang Patel8adae862007-07-20 18:04:54 +0000988 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000989
Devang Patel2ff44922007-04-16 20:39:59 +0000990 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000991 if (!TPM)
992 return;
993
Devang Patel17ad0962006-12-08 00:37:52 +0000994 TPM->collectLastUses(DeadPasses, P);
995
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000996 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000997 dbgs() << " -*- '" << P->getPassName();
998 dbgs() << "' is the last user of following pass instances.";
999 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +00001000 }
1001
Yaron Kerenbd873192016-10-02 19:21:41 +00001002 for (Pass *P : DeadPasses)
1003 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001004}
Devang Patel200d3052006-12-13 23:50:44 +00001005
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001006void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001007 enum PassDebuggingString DBG_STR) {
1008 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +00001009
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001010 {
1011 // If the pass crashes releasing memory, remember this.
1012 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +00001013 TimeRegion PassTimer(getPassTimer(P));
1014
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001015 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001016 }
1017
Owen Andersona7aed182010-08-06 18:33:48 +00001018 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001019 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001020 // Remove the pass itself (if it is not already removed).
1021 AvailableAnalysis.erase(PI);
1022
1023 // Remove all interfaces this pass implements, for which it is also
1024 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +00001025 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +00001026 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001027 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +00001028 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +00001029 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +00001030 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +00001031 }
Devang Patel17ad0962006-12-08 00:37:52 +00001032 }
Devang Patelca189262006-11-14 03:05:08 +00001033}
1034
Dan Gohmande6188a2010-08-12 23:50:08 +00001035/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +00001036/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +00001037void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +00001038 // This manager is going to manage pass P. Set up analysis resolver
1039 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +00001040 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +00001041 P->setResolver(AR);
1042
Devang Patelec2b9a72007-03-05 22:57:49 +00001043 // If a FunctionPass F is the last user of ModulePass info M
1044 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +00001045 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +00001046
Chris Lattner60987362009-03-06 05:53:14 +00001047 if (!ProcessAnalysis) {
1048 // Add pass
1049 PassVector.push_back(P);
1050 return;
Devang Patel90b05e02006-11-11 02:04:19 +00001051 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001052
Chris Lattner60987362009-03-06 05:53:14 +00001053 // At the moment, this pass is the last user of all required passes.
1054 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +00001055 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +00001056 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1057
1058 unsigned PDepth = this->getDepth();
1059
Chandler Carruth44a13852015-08-19 03:02:12 +00001060 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
1061 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001062 unsigned RDepth = 0;
1063
Chandler Carruth44a13852015-08-19 03:02:12 +00001064 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1065 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001066 RDepth = DM.getDepth();
1067
1068 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001069 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001070 else if (PDepth > RDepth) {
1071 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001072 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001073 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001074 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001075 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001076 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001077 }
1078
1079 // Set P as P's last user until someone starts using P.
1080 // However, if P is a Pass Manager then it does not need
1081 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001082 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001083 LastUses.push_back(P);
1084 TPM->setLastUser(LastUses, P);
1085
1086 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001087 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001088 TPM->setLastUser(TransferLastUses, My_PM);
1089 TransferLastUses.clear();
1090 }
1091
Dan Gohman6304db32010-08-16 22:57:28 +00001092 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001093 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1094 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001095 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001096 this->addLowerLevelRequiredPass(P, AnalysisPass);
1097 }
1098
1099 // Take a note of analysis required and made available by this pass.
1100 // Remove the analysis not preserved by this pass
1101 removeNotPreservedAnalysis(P);
1102 recordAvailableAnalysis(P);
1103
Devang Patel8cad70d2006-11-11 01:51:02 +00001104 // Add pass
1105 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001106}
1107
Devang Patele64d3052007-04-16 20:12:57 +00001108
Chandler Carruth44a13852015-08-19 03:02:12 +00001109/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001110/// pass P and are available. Populate RP_NotAvail with analysis
1111/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001112void PMDataManager::collectRequiredAndUsedAnalyses(
1113 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1114 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001115 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001116
1117 for (const auto &UsedID : AnUsage->getUsedSet())
1118 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1119 UP.push_back(AnalysisPass);
1120
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001121 for (const auto &RequiredID : AnUsage->getRequiredSet())
1122 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001123 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001124 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001125 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001126
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001127 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1128 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001129 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001130 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001131 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001132}
1133
Devang Patel07f4f582006-11-14 21:49:36 +00001134// All Required analyses should be available to the pass as it runs! Here
1135// we fill in the AnalysisImpls member of the pass so that it can
1136// successfully use the getAnalysis() method to retrieve the
1137// implementations it needs.
1138//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001139void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001140 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1141
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001142 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1143 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001144 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001145 // This may be analysis pass that is initialized on the fly.
1146 // If that is not the case then it will raise an assert when it is used.
1147 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001148 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001149 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001150 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001151 }
1152}
1153
Devang Patel640c5bb2006-12-08 22:30:11 +00001154/// Find the pass that implements Analysis AID. If desired pass is not found
1155/// then return NULL.
1156Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1157
1158 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001159 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001160
1161 if (I != AvailableAnalysis.end())
1162 return I->second;
1163
1164 // Search Parents through TopLevelManager
1165 if (SearchParent)
1166 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001167
Craig Topperc6207612014-04-09 06:08:46 +00001168 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001169}
1170
Devang Patel991aeba2006-12-15 20:13:01 +00001171// Print list of passes that are last used by P.
1172void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1173
Devang Patel8adae862007-07-20 18:04:54 +00001174 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001175
1176 // If this is a on the fly manager then it does not have TPM.
1177 if (!TPM)
1178 return;
1179
Devang Patel991aeba2006-12-15 20:13:01 +00001180 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001181
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001182 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001183 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001184 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001185 }
1186}
1187
1188void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001189 for (Pass *P : PassVector) {
1190 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001191 PMD->dumpPassArguments();
1192 else
Owen Andersona7aed182010-08-06 18:33:48 +00001193 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001194 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001195 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001196 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001197 }
1198}
1199
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001200void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1201 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001202 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001203 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001204 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001205 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001206 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001207 switch (S1) {
1208 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001209 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001210 break;
1211 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001212 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001213 break;
1214 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001215 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001216 break;
1217 default:
1218 break;
1219 }
1220 switch (S2) {
1221 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001222 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001223 break;
1224 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001225 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001226 break;
1227 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001228 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001229 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001230 case ON_REGION_MSG:
1231 dbgs() << "' on Region '" << Msg << "'...\n";
1232 break;
Devang Patel003a5592007-03-05 20:01:30 +00001233 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001234 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001235 break;
1236 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001237 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001238 break;
1239 default:
1240 break;
1241 }
Devang Patel991aeba2006-12-15 20:13:01 +00001242}
1243
Chris Lattner4c1e9542009-03-06 06:45:05 +00001244void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001245 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001246 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001247
Chris Lattner4c493d92008-08-08 15:14:09 +00001248 AnalysisUsage analysisUsage;
1249 P->getAnalysisUsage(analysisUsage);
1250 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1251}
1252
Chris Lattner4c1e9542009-03-06 06:45:05 +00001253void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001254 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001255 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001256
Chris Lattner4c493d92008-08-08 15:14:09 +00001257 AnalysisUsage analysisUsage;
1258 P->getAnalysisUsage(analysisUsage);
1259 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1260}
1261
Chandler Carruth44a13852015-08-19 03:02:12 +00001262void PMDataManager::dumpUsedSet(const Pass *P) const {
1263 if (PassDebugging < Details)
1264 return;
1265
1266 AnalysisUsage analysisUsage;
1267 P->getAnalysisUsage(analysisUsage);
1268 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1269}
1270
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001271void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001272 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001273 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001274 if (Set.empty())
1275 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001276 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001277 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001278 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001279 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001280 if (!PInf) {
1281 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1282 // all drivers.
1283 dbgs() << " Uninitialized Pass";
1284 continue;
1285 }
Owen Andersona7aed182010-08-06 18:33:48 +00001286 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001287 }
David Greene994e1bb2010-01-05 01:30:02 +00001288 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001289}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001290
Devang Patel004937b2007-07-27 20:06:09 +00001291/// Add RequiredPass into list of lower level passes required by pass P.
1292/// RequiredPass is run on the fly by Pass Manager when P requests it
1293/// through getAnalysis interface.
1294/// This should be handled by specific pass manager.
1295void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1296 if (TPM) {
1297 TPM->dumpArguments();
1298 TPM->dumpPasses();
1299 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001300
Dan Gohmande6188a2010-08-12 23:50:08 +00001301 // Module Level pass may required Function Level analysis info
1302 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1303 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001304 // module level pass is requiring lower level analysis info managed by
1305 // lower level pass manager.
1306
1307 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001308 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001309 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001310#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001311 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1312 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001313#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001314 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001315}
1316
Owen Andersona7aed182010-08-06 18:33:48 +00001317Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001318 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001319}
1320
Devang Patele7599552007-01-12 18:52:44 +00001321// Destructor
1322PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001323 for (Pass *P : PassVector)
1324 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001325}
1326
Devang Patel9bdf7d42006-12-08 23:28:54 +00001327//===----------------------------------------------------------------------===//
1328// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001329// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1330Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001331 return PM.findAnalysisPass(ID, dir);
1332}
1333
Dan Gohmande6188a2010-08-12 23:50:08 +00001334Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001335 Function &F) {
1336 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1337}
1338
Devang Patela1514cb2006-12-07 19:39:39 +00001339//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001340// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001341
Dan Gohmande6188a2010-08-12 23:50:08 +00001342/// Execute all of the passes scheduled for execution by invoking
1343/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001344/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001345bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001346 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001347 return false;
1348
Devang Patele9585592006-12-08 01:38:28 +00001349 bool Changed = doInitialization(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001350 Module &M = *F.getParent();
Devang Patel050ec722006-11-14 01:23:29 +00001351
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001352 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001353 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1354 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001355 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001356
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001357 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001358 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001359
Devang Patelabfbe3b2006-12-16 00:56:26 +00001360 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001361
Chris Lattner4c1e9542009-03-06 06:45:05 +00001362 {
1363 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001364 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001365 TimeRegion PassTimer(getPassTimer(BP));
Jessica Paquettee49374d2018-05-18 17:26:39 +00001366 unsigned InstrCount = initSizeRemarkInfo(M);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001367 LocalChanged |= BP->runOnBasicBlock(BB);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001368 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
Devang Patelabfbe3b2006-12-16 00:56:26 +00001572 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1573 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001574 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001576 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001577 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001578
Devang Patelabfbe3b2006-12-16 00:56:26 +00001579 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001580
Chris Lattner4c1e9542009-03-06 06:45:05 +00001581 {
1582 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001583 TimeRegion PassTimer(getPassTimer(FP));
Jessica Paquettee49374d2018-05-18 17:26:39 +00001584 unsigned InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001585 LocalChanged |= FP->runOnFunction(F);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001586 emitInstrCountChangedRemark(FP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001587 }
Devang Patel93a197c2006-12-14 00:08:04 +00001588
Dan Gohman74b189f2010-03-01 17:34:28 +00001589 Changed |= LocalChanged;
1590 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001591 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001592 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001593 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001594
Devang Patela273d1c2007-07-19 18:02:32 +00001595 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001596 removeNotPreservedAnalysis(FP);
1597 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001598 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001599 }
1600 return Changed;
1601}
1602
Devang Patel67d6a5e2006-12-19 19:46:59 +00001603bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001604 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001605
Serge Pavloved5eb932017-01-15 10:23:18 +00001606 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001607 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001608
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001609 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001610}
1611
Duncan Sands51495602009-02-13 09:42:34 +00001612bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001613 bool Changed = false;
1614
Chris Lattner4c1e9542009-03-06 06:45:05 +00001615 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1616 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001617
Devang Patelff631ae2006-11-15 01:27:05 +00001618 return Changed;
1619}
1620
Duncan Sands51495602009-02-13 09:42:34 +00001621bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001622 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001623
1624 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001625 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001626
Devang Patelff631ae2006-11-15 01:27:05 +00001627 return Changed;
1628}
1629
Devang Patela1514cb2006-12-07 19:39:39 +00001630//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001631// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001632
Dan Gohmande6188a2010-08-12 23:50:08 +00001633/// Execute all of the passes scheduled for execution by invoking
1634/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001635/// the module, and if so, return true.
1636bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001637MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001638 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001639
Torok Edwin24c78352009-06-29 18:49:09 +00001640 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001641 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1642 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001643 Changed |= FPP->doInitialization(M);
1644 }
1645
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001646 // Initialize module passes
1647 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1648 Changed |= getContainedPass(Index)->doInitialization(M);
1649
Devang Patelabfbe3b2006-12-16 00:56:26 +00001650 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1651 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001652 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001653
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001654 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001655 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001656
Devang Patelabfbe3b2006-12-16 00:56:26 +00001657 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001658
Chris Lattner4c1e9542009-03-06 06:45:05 +00001659 {
1660 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001661 TimeRegion PassTimer(getPassTimer(MP));
1662
Jessica Paquettee49374d2018-05-18 17:26:39 +00001663 unsigned InstrCount = initSizeRemarkInfo(M);
Dan Gohman74b189f2010-03-01 17:34:28 +00001664 LocalChanged |= MP->runOnModule(M);
Jessica Paquettee49374d2018-05-18 17:26:39 +00001665 emitInstrCountChangedRemark(MP, M, InstrCount);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001666 }
Devang Patel93a197c2006-12-14 00:08:04 +00001667
Dan Gohman74b189f2010-03-01 17:34:28 +00001668 Changed |= LocalChanged;
1669 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001670 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001671 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001672 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001673 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001674
Devang Patela273d1c2007-07-19 18:02:32 +00001675 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001676 removeNotPreservedAnalysis(MP);
1677 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001678 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001679 }
Torok Edwin24c78352009-06-29 18:49:09 +00001680
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001681 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001682 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001683 Changed |= getContainedPass(Index)->doFinalization(M);
1684
Torok Edwin24c78352009-06-29 18:49:09 +00001685 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001686 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1687 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001688 // We don't know when is the last time an on-the-fly pass is run,
1689 // so we need to releaseMemory / finalize here
1690 FPP->releaseMemoryOnTheFly();
1691 Changed |= FPP->doFinalization(M);
1692 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001693
Devang Patel05e1a972006-11-07 22:03:15 +00001694 return Changed;
1695}
1696
Devang Patele64d3052007-04-16 20:12:57 +00001697/// Add RequiredPass into list of lower level passes required by pass P.
1698/// RequiredPass is run on the fly by Pass Manager when P requests it
1699/// through getAnalysis interface.
1700void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001701 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1702 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001703 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001704 RequiredPass->getPotentialPassManagerType()) &&
1705 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001706 if (!RequiredPass)
1707 return;
Devang Patele64d3052007-04-16 20:12:57 +00001708
Devang Patel68f72b12007-04-26 17:50:19 +00001709 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001710 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001711 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001712 // FPP is the top level manager.
1713 FPP->setTopLevelManager(FPP);
1714
Devang Patel69e9f6d2007-04-16 20:27:05 +00001715 OnTheFlyManagers[P] = FPP;
1716 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001717 const PassInfo *RequiredPassPI =
1718 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001719
Craig Topperc6207612014-04-09 06:08:46 +00001720 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001721 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1722 FoundPass =
1723 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001724 }
Andrew Trick02066f22014-04-08 03:40:34 +00001725 if (!FoundPass) {
1726 FoundPass = RequiredPass;
1727 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001728 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001729 FPP->add(RequiredPass);
1730 }
1731 // Register P as the last user of FoundPass or RequiredPass.
1732 SmallVector<Pass *, 1> LU;
1733 LU.push_back(FoundPass);
1734 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001735}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001736
Dan Gohmande6188a2010-08-12 23:50:08 +00001737/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001738/// required by module pass MP. Instantiate analysis pass, by using
1739/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001740Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001741 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001742 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001743
Torok Edwin24c78352009-06-29 18:49:09 +00001744 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001745 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001746 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001747}
1748
1749
Devang Patela1514cb2006-12-07 19:39:39 +00001750//===----------------------------------------------------------------------===//
1751// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001752
Devang Patelab97cf42006-12-13 00:09:23 +00001753//
Devang Patelc290c8a2006-11-07 22:23:34 +00001754/// run - Execute all of the passes scheduled for execution. Keep track of
1755/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001756bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001757 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001758 TimingInfo::createTheTimeInfo();
1759
Devang Patelcfd70c42006-12-13 22:10:00 +00001760 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001761 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001762
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001763 for (ImmutablePass *ImPass : getImmutablePasses())
1764 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001765
Devang Patele3068402006-12-21 00:16:50 +00001766 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001767 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001768 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001769 M.getContext().yield();
1770 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001771
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001772 for (ImmutablePass *ImPass : getImmutablePasses())
1773 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001774
Devang Patelc290c8a2006-11-07 22:23:34 +00001775 return Changed;
1776}
Devang Patel376fefa2006-11-08 10:29:57 +00001777
Devang Patela1514cb2006-12-07 19:39:39 +00001778//===----------------------------------------------------------------------===//
1779// PassManager implementation
1780
Devang Patel376fefa2006-11-08 10:29:57 +00001781/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001782PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001783 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001784 // PM is the top level manager
1785 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001786}
1787
Devang Patelb67904d2006-12-13 02:36:01 +00001788PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001789 delete PM;
1790}
1791
Chris Lattner60987362009-03-06 05:53:14 +00001792void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001793 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001794}
1795
1796/// run - Execute all of the passes scheduled for execution. Keep track of
1797/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001798bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001799 return PM->run(M);
1800}
1801
Devang Patelb8817b92006-12-14 00:59:42 +00001802//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001803// TimingInfo implementation
1804
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001805bool llvm::TimePassesIsEnabled = false;
Zachary Turner8065f0b2017-12-01 00:53:10 +00001806static cl::opt<bool, true> EnableTiming(
1807 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
1808 cl::desc("Time each pass, printing elapsed time for each on exit"));
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001809
Devang Patelb8817b92006-12-14 00:59:42 +00001810// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001811// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001812// null. It may be called multiple times.
1813void TimingInfo::createTheTimeInfo() {
1814 if (!TimePassesIsEnabled || TheTimeInfo) return;
1815
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001816 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001817 // This guarantees that the object will be constructed before static globals,
1818 // thus it will be destroyed before them.
1819 static ManagedStatic<TimingInfo> TTI;
1820 TheTimeInfo = &*TTI;
1821}
1822
Devang Patel1c3633e2007-01-29 23:10:37 +00001823/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001824Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001825 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001826 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001827 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001828}
1829
James Henderson852f6fd2017-05-16 09:43:21 +00001830/// If timing is enabled, report the times collected up to now and then reset
1831/// them.
1832void llvm::reportAndResetTimings() {
1833 if (TheTimeInfo)
1834 TheTimeInfo->print();
1835}
1836
Devang Patel1c56a632007-01-08 19:29:38 +00001837//===----------------------------------------------------------------------===//
1838// PMStack implementation
1839//
Devang Patelad98d232007-01-11 22:15:30 +00001840
Devang Patel1c56a632007-01-08 19:29:38 +00001841// Pop Pass Manager from the stack and clear its analysis info.
1842void PMStack::pop() {
1843
1844 PMDataManager *Top = this->top();
1845 Top->initializeAnalysisInfo();
1846
1847 S.pop_back();
1848}
1849
1850// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001851void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001852 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001853 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001854
Chris Lattner60987362009-03-06 05:53:14 +00001855 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001856 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1857 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001858 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001859
Chris Lattner60987362009-03-06 05:53:14 +00001860 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001861 TPM->addIndirectPassManager(PM);
1862 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001863 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001864 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001865 assert((PM->getPassManagerType() == PMT_ModulePassManager
1866 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001867 && "pushing bad pass manager to PMStack");
1868 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001869 }
1870
Devang Patel15701b52007-01-11 00:19:00 +00001871 S.push_back(PM);
1872}
1873
1874// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001875LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001876 for (PMDataManager *Manager : S)
1877 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001878
Devang Patel15701b52007-01-11 00:19:00 +00001879 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001880 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001881}
1882
Devang Patel1c56a632007-01-08 19:29:38 +00001883/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001884/// add self into that manager.
1885void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001886 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001887 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001888 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001889 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1890 if (TopPMType == PreferredType)
1891 break; // We found desired pass manager
1892 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001893 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001894 else
1895 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001896 }
Devang Patel18ff6362008-09-09 21:38:40 +00001897 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001898 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001899}
1900
Devang Patel3312f752007-01-16 21:43:18 +00001901/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001902/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001903void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001904 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001905
Andrew Trickcbc845f2012-02-01 07:16:20 +00001906 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001907 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001908 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1909 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001910 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001911 break;
Devang Patel3312f752007-01-16 21:43:18 +00001912 }
Devang Patel3312f752007-01-16 21:43:18 +00001913
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001914 // Create new Function Pass Manager if needed.
1915 FPPassManager *FPP;
1916 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1917 FPP = (FPPassManager *)PMS.top();
1918 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001919 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1920 PMDataManager *PMD = PMS.top();
1921
1922 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001923 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001924 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001925
1926 // [2] Set up new manager's top level manager
1927 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1928 TPM->addIndirectPassManager(FPP);
1929
1930 // [3] Assign manager to manage this new manager. This may create
1931 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001932 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001933
1934 // [4] Push new manager into PMS
1935 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001936 }
1937
Devang Patel3312f752007-01-16 21:43:18 +00001938 // Assign FPP as the manager of this pass.
1939 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001940}
1941
Devang Patel3312f752007-01-16 21:43:18 +00001942/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001943/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001944void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001945 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001946 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001947
Devang Patel15701b52007-01-11 00:19:00 +00001948 // Basic Pass Manager is a leaf pass manager. It does not handle
1949 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001950 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001951 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1952 BBP = (BBPassManager *)PMS.top();
1953 } else {
1954 // If leaf manager is not Basic Block Pass manager then create new
1955 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001956 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1957 PMDataManager *PMD = PMS.top();
1958
1959 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001960 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001961
1962 // [2] Set up new manager's top level manager
1963 // Basic Block Pass Manager does not live by itself
1964 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1965 TPM->addIndirectPassManager(BBP);
1966
Devang Patel15701b52007-01-11 00:19:00 +00001967 // [3] Assign manager to manage this new manager. This may create
1968 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001969 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001970
Devang Patel3312f752007-01-16 21:43:18 +00001971 // [4] Push new manager into PMS
1972 PMS.push(BBP);
1973 }
Devang Patel1c56a632007-01-08 19:29:38 +00001974
Devang Patel3312f752007-01-16 21:43:18 +00001975 // Assign BBP as the manager of this pass.
1976 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001977}
1978
Dan Gohmand3a20c92008-03-11 16:41:42 +00001979PassManagerBase::~PassManagerBase() {}