blob: 71bb3dd957f272d8413db6ad7916c3f5edb165b9 [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
14
Juergen Ributzka34390c72014-05-16 02:33:15 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000016#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000018#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000019#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/IR/Module.h"
Devang Patelf1567a52006-12-13 20:03:48 +000021#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000022#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000024#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000025#include "llvm/Support/Mutex.h"
Chandler Carruth20c56932014-04-27 23:59:25 +000026#include "llvm/Support/TimeValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/Timer.h"
28#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000029#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000030#include <map>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000031#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000033using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000034
Devang Patele7599552007-01-12 18:52:44 +000035// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000036
Devang Patelf1567a52006-12-13 20:03:48 +000037//===----------------------------------------------------------------------===//
38// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
43
Chandler Carruth7caea412013-11-09 12:26:54 +000044namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000047 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000048};
Chandler Carruth7caea412013-11-09 12:26:54 +000049}
Devang Patel03fb5872006-12-13 21:13:31 +000050
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000051static cl::opt<enum PassDebugLevel>
52PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000053 cl::desc("Print PassManager debugging information"),
54 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000055 clEnumVal(Disabled , "disable debug output"),
56 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
57 clEnumVal(Structure , "print pass structure before run()"),
58 clEnumVal(Executions, "print pass name before it is executed"),
59 clEnumVal(Details , "print pass details when it is executed"),
60 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000061
Chandler Carruth7caea412013-11-09 12:26:54 +000062namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000063typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
64PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000065}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000066
67// Print IR out before/after specified passes.
68static PassOptionList
69PrintBefore("print-before",
70 llvm::cl::desc("Print IR before specified passes"),
71 cl::Hidden);
72
73static PassOptionList
74PrintAfter("print-after",
75 llvm::cl::desc("Print IR after specified passes"),
76 cl::Hidden);
77
78static cl::opt<bool>
79PrintBeforeAll("print-before-all",
80 llvm::cl::desc("Print IR before each pass"),
81 cl::init(false));
82static cl::opt<bool>
83PrintAfterAll("print-after-all",
84 llvm::cl::desc("Print IR after each pass"),
85 cl::init(false));
86
Weiming Zhao0f1762c2016-01-06 22:55:03 +000087static cl::list<std::string>
88 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
89 cl::desc("Only print IR for functions whose name "
90 "match this for all print-[before|after][-all] "
91 "options"),
92 cl::CommaSeparated);
93
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000094/// This is a helper to determine whether to print IR before or
95/// after a pass.
96
97static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
98 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +000099 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000100 if (PassInf)
101 if (PassInf->getPassArgument() == PI->getPassArgument()) {
102 return true;
103 }
David Greene9b063df2010-04-02 23:17:14 +0000104 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000105 return false;
106}
Dan Gohmande6188a2010-08-12 23:50:08 +0000107
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000108/// This is a utility to check whether a pass should have IR dumped
109/// before it.
110static bool ShouldPrintBeforePass(const PassInfo *PI) {
111 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
112}
David Greene9b063df2010-04-02 23:17:14 +0000113
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000114/// This is a utility to check whether a pass should have IR dumped
115/// after it.
116static bool ShouldPrintAfterPass(const PassInfo *PI) {
117 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000118}
119
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000120bool llvm::isFunctionInPrintList(StringRef FunctionName) {
121 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
122 PrintFuncsList.end());
123 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
124}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000125/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
126/// or higher is specified.
127bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000128 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000129}
130
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000131
132
133
Chris Lattner4c1e9542009-03-06 06:45:05 +0000134void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000135 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000136 OS << "Releasing pass '";
137 else
138 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000139
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000141
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 if (M) {
143 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
144 return;
145 }
Craig Topperc6207612014-04-09 06:08:46 +0000146 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000147 OS << '\n';
148 return;
149 }
150
Dan Gohman79fc0e92009-03-10 18:47:59 +0000151 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000152 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000153 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000154 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000155 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000156 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000157 OS << "value";
158
159 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000160 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000161 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000162}
163
164
Devang Patelffca9102006-12-15 19:39:30 +0000165namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000166//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000168//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000169/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000170/// pass together and sequence them to process one basic block before
171/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000172class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000173
174public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000175 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000176 explicit BBPassManager()
177 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000178
Devang Patelca58e352006-11-08 10:05:38 +0000179 /// Execute all of the passes scheduled for execution. Keep track of
180 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000181 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000182
Devang Patelf9d96b92006-12-07 19:57:52 +0000183 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000184 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000185 Info.setPreservesAll();
186 }
187
Craig Topperf398d7c2014-03-05 06:35:38 +0000188 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000189 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000190 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000191 bool doFinalization(Function &F);
192
Craig Topperf398d7c2014-03-05 06:35:38 +0000193 PMDataManager *getAsPMDataManager() override { return this; }
194 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000195
Mehdi Amini117296c2016-10-01 02:56:57 +0000196 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000197
Devang Pateleda56172006-12-12 23:34:33 +0000198 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000199 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000200 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000201 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
202 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000203 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000204 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000205 }
206 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000207
208 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000209 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000210 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
211 return BP;
212 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000213
Craig Topperf398d7c2014-03-05 06:35:38 +0000214 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000215 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000216 }
Devang Patelca58e352006-11-08 10:05:38 +0000217};
218
Devang Patel8c78a0b2007-05-03 01:11:54 +0000219char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000220} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221
Devang Patele7599552007-01-12 18:52:44 +0000222namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000223namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000224//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000226//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227/// FunctionPassManagerImpl manages FPPassManagers
228class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000229 public PMDataManager,
230 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000231 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000232private:
233 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000234public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000235 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000236 explicit FunctionPassManagerImpl() :
237 Pass(PT_PassManager, ID), PMDataManager(),
238 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000239
Matthias Braun0880c602014-12-12 01:27:01 +0000240 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000241 void add(Pass *P) {
242 schedulePass(P);
243 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000244
245 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000246 Pass *createPrinterPass(raw_ostream &O,
247 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000248 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000249 }
250
Torok Edwin24c78352009-06-29 18:49:09 +0000251 // Prepare for running an on the fly pass, freeing memory if needed
252 // from a previous run.
253 void releaseMemoryOnTheFly();
254
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255 /// run - Execute all of the passes scheduled for execution. Keep track of
256 /// whether any of the passes modifies the module, and if so, return true.
257 bool run(Function &F);
258
259 /// doInitialization - Run all of the initializers for the function passes.
260 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000261 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000262
Dan Gohmane6656eb2007-07-30 14:51:13 +0000263 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000265 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000266
Dan Gohmande6188a2010-08-12 23:50:08 +0000267
Craig Topperf398d7c2014-03-05 06:35:38 +0000268 PMDataManager *getAsPMDataManager() override { return this; }
269 Pass *getAsPass() override { return this; }
270 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000271 return PMT_FunctionPassManager;
272 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000273
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000275 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000276 Info.setPreservesAll();
277 }
278
Devang Patel67d6a5e2006-12-19 19:46:59 +0000279 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000280 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
282 return FP;
283 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284};
285
David Blaikiea379b1812011-12-20 02:50:00 +0000286void FunctionPassManagerImpl::anchor() {}
287
Devang Patel8c78a0b2007-05-03 01:11:54 +0000288char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000289} // End of legacy namespace
290} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000291
Chandler Carruth7caea412013-11-09 12:26:54 +0000292namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000293//===----------------------------------------------------------------------===//
294// MPPassManager
295//
296/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000297/// It batches all Module passes and function pass managers together and
298/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000300public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000301 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000302 explicit MPPassManager() :
303 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000304
305 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000306 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000307 for (auto &OnTheFlyManager : OnTheFlyManagers) {
308 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000309 delete FPP;
310 }
311 }
312
Dan Gohmande6188a2010-08-12 23:50:08 +0000313 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000314 Pass *createPrinterPass(raw_ostream &O,
315 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000316 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000317 }
318
Devang Patelca58e352006-11-08 10:05:38 +0000319 /// run - Execute all of the passes scheduled for execution. Keep track of
320 /// whether any of the passes modifies the module, and if so, return true.
321 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000322
Pedro Artigase4348b02012-12-03 21:56:57 +0000323 using llvm::Pass::doInitialization;
324 using llvm::Pass::doFinalization;
325
Devang Patelf9d96b92006-12-07 19:57:52 +0000326 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000327 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000328 Info.setPreservesAll();
329 }
330
Devang Patele64d3052007-04-16 20:12:57 +0000331 /// Add RequiredPass into list of lower level passes required by pass P.
332 /// RequiredPass is run on the fly by Pass Manager when P requests it
333 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000334 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000335
Dan Gohmande6188a2010-08-12 23:50:08 +0000336 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000337 /// required by module pass MP. Instantiate analysis pass, by using
338 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000339 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000340
Mehdi Amini117296c2016-10-01 02:56:57 +0000341 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000342
Craig Topperf398d7c2014-03-05 06:35:38 +0000343 PMDataManager *getAsPMDataManager() override { return this; }
344 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000345
Devang Pateleda56172006-12-12 23:34:33 +0000346 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000347 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000348 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000349 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
350 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000351 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000352 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
353 OnTheFlyManagers.find(MP);
354 if (I != OnTheFlyManagers.end())
355 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000356 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000357 }
358 }
359
Devang Patelabfbe3b2006-12-16 00:56:26 +0000360 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000361 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000362 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000363 }
364
Craig Topperf398d7c2014-03-05 06:35:38 +0000365 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000366 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000367 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000368
369 private:
370 /// Collection of on the fly FPPassManagers. These managers manage
371 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000372 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000373};
374
Devang Patel8c78a0b2007-05-03 01:11:54 +0000375char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000376} // End anonymous namespace
377
378namespace llvm {
379namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000380//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000381// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000382//
Devang Patel09f162c2007-05-01 21:15:47 +0000383
Devang Patel67d6a5e2006-12-19 19:46:59 +0000384/// PassManagerImpl manages MPPassManagers
385class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000386 public PMDataManager,
387 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000388 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000389
390public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000391 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000392 explicit PassManagerImpl() :
393 Pass(PT_PassManager, ID), PMDataManager(),
394 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000395
Matthias Braun0880c602014-12-12 01:27:01 +0000396 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000397 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000398 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000399 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000400
401 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000402 Pass *createPrinterPass(raw_ostream &O,
403 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000404 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000405 }
406
Devang Patel376fefa2006-11-08 10:29:57 +0000407 /// run - Execute all of the passes scheduled for execution. Keep track of
408 /// whether any of the passes modifies the module, and if so, return true.
409 bool run(Module &M);
410
Pedro Artigase4348b02012-12-03 21:56:57 +0000411 using llvm::Pass::doInitialization;
412 using llvm::Pass::doFinalization;
413
Devang Patelf9d96b92006-12-07 19:57:52 +0000414 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000415 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000416 Info.setPreservesAll();
417 }
418
Craig Topperf398d7c2014-03-05 06:35:38 +0000419 PMDataManager *getAsPMDataManager() override { return this; }
420 Pass *getAsPass() override { return this; }
421 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000422 return PMT_ModulePassManager;
423 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000424
Devang Patel67d6a5e2006-12-19 19:46:59 +0000425 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000426 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000427 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
428 return MP;
429 }
Devang Patel376fefa2006-11-08 10:29:57 +0000430};
431
David Blaikiea379b1812011-12-20 02:50:00 +0000432void PassManagerImpl::anchor() {}
433
Devang Patel8c78a0b2007-05-03 01:11:54 +0000434char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000435} // End of legacy namespace
436} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000437
438namespace {
439
440//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000441/// TimingInfo Class - This class is used to calculate information about the
442/// amount of time each pass takes to execute. This only happens when
443/// -time-passes is enabled on the command line.
444///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000445
Owen Anderson5a6960f2009-06-18 20:51:00 +0000446static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000447
Nick Lewycky02d5f772009-10-25 06:33:48 +0000448class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000449 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000450 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000451public:
452 // Use 'create' member to get this.
453 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000454
Devang Patel1c3633e2007-01-29 23:10:37 +0000455 // TimingDtor - Print out information about timing information
456 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000457 // Delete all of the timers, which accumulate their info into the
458 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000459 for (auto &I : TimingData)
460 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000461 // TimerGroup is deleted next, printing the report.
462 }
463
464 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000465 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000466 // null. It may be called multiple times.
467 static void createTheTimeInfo();
468
Chris Lattner707431c2010-03-30 04:03:22 +0000469 /// getPassTimer - Return the timer for the specified pass if it exists.
470 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000471 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000472 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000473
Owen Anderson5c96ef72009-07-07 18:33:04 +0000474 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000475 Timer *&T = TimingData[P];
Craig Topperc6207612014-04-09 06:08:46 +0000476 if (!T)
Chris Lattner707431c2010-03-30 04:03:22 +0000477 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000478 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000479 }
480};
481
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000482} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000483
Dan Gohmand78c4002008-05-13 00:00:25 +0000484static TimingInfo *TheTimeInfo;
485
Devang Patela1514cb2006-12-07 19:39:39 +0000486//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000487// PMTopLevelManager implementation
488
Devang Patel4268fc02007-01-16 02:00:38 +0000489/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000490PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
491 PMDM->setTopLevelManager(this);
492 addPassManager(PMDM);
493 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000494}
495
Devang Patelafb1f3622006-12-12 22:35:25 +0000496/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000497void
Bill Wendlingea857e12012-05-14 07:53:40 +0000498PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000499 unsigned PDepth = 0;
500 if (P->getResolver())
501 PDepth = P->getResolver()->getPMDataManager().getDepth();
502
Yaron Keren4849aa32015-06-05 17:48:47 +0000503 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000504 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000505
Devang Patel01919d22007-03-08 19:05:01 +0000506 if (P == AP)
507 continue;
508
Tobias Grosserf07426b2011-01-20 21:03:22 +0000509 // Update the last users of passes that are required transitive by AP.
510 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
511 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
512 SmallVector<Pass *, 12> LastUses;
513 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000514 for (AnalysisID ID : IDs) {
515 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000516 assert(AnalysisPass && "Expected analysis pass to exist.");
517 AnalysisResolver *AR = AnalysisPass->getResolver();
518 assert(AR && "Expected analysis resolver to exist.");
519 unsigned APDepth = AR->getPMDataManager().getDepth();
520
521 if (PDepth == APDepth)
522 LastUses.push_back(AnalysisPass);
523 else if (PDepth > APDepth)
524 LastPMUses.push_back(AnalysisPass);
525 }
526
527 setLastUser(LastUses, P);
528
529 // If this pass has a corresponding pass manager, push higher level
530 // analysis to this pass manager.
531 if (P->getResolver())
532 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
533
534
Devang Patelafb1f3622006-12-12 22:35:25 +0000535 // If AP is the last user of other passes then make P last user of
536 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000537 for (auto LU : LastUser) {
538 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000539 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000540 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000541 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000542 }
543 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000544}
545
546/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000547void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000548 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000549 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000550 InversedLastUser.find(P);
551 if (DMI == InversedLastUser.end())
552 return;
553
554 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000555 for (Pass *LUP : LU) {
556 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000557 }
558
Devang Patelafb1f3622006-12-12 22:35:25 +0000559}
560
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000561AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000562 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000563 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000564 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000565 AnUsage = DMI->second;
566 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000567 // Look up the analysis usage from the pass instance (different instances
568 // of the same pass can produce different results), but unique the
569 // resulting object to reduce memory usage. This helps to greatly reduce
570 // memory usage when we have many instances of only a few pass types
571 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
572 // of dependencies.
573 AnalysisUsage AU;
574 P->getAnalysisUsage(AU);
575
576 AUFoldingSetNode* Node = nullptr;
577 FoldingSetNodeID ID;
578 AUFoldingSetNode::Profile(ID, AU);
579 void *IP = nullptr;
580 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
581 Node = N;
582 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000583 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000584 UniqueAnalysisUsages.InsertNode(Node, IP);
585 }
586 assert(Node && "cached analysis usage must be non null");
587
588 AnUsageMap[P] = &Node->AU;
589 AnUsage = &Node->AU;;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000590 }
591 return AnUsage;
592}
593
Devang Patelafb1f3622006-12-12 22:35:25 +0000594/// Schedule pass P for execution. Make sure that passes required by
595/// P are run before P is run. Update analysis info maintained by
596/// the manager. Remove dead passes. This is a recursive function.
597void PMTopLevelManager::schedulePass(Pass *P) {
598
Devang Patel3312f752007-01-16 21:43:18 +0000599 // TODO : Allocate function manager for this pass, other wise required set
600 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000601
Devang Pateld74ede72007-03-06 01:06:16 +0000602 // Give pass a chance to prepare the stage.
603 P->preparePassManager(activeStack);
604
Devang Patel864970e2008-03-18 00:39:19 +0000605 // If P is an analysis pass and it is available then do not
606 // generate the analysis again. Stale analysis info should not be
607 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000608 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000609 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000610 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000611 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000612 }
Devang Patel864970e2008-03-18 00:39:19 +0000613
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000614 AnalysisUsage *AnUsage = findAnalysisUsage(P);
615
Devang Patelfdee7032008-08-14 23:07:48 +0000616 bool checkAnalysis = true;
617 while (checkAnalysis) {
618 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000619
Devang Patelfdee7032008-08-14 23:07:48 +0000620 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
621 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
622 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000623
Devang Patelfdee7032008-08-14 23:07:48 +0000624 Pass *AnalysisPass = findAnalysisPass(*I);
625 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000626 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000627
Craig Topperc6207612014-04-09 06:08:46 +0000628 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000629 // Pass P is not in the global PassRegistry
630 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
631 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
632 dbgs() << "Required Passes:" << "\n";
633 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
634 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
635 Pass *AnalysisPass2 = findAnalysisPass(*I2);
636 if (AnalysisPass2) {
637 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000638 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000639 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
640 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
641 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
642 }
643 }
644 }
645
Andrew Trick6bbaf132011-06-03 00:48:58 +0000646 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000647 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000648 if (P->getPotentialPassManagerType () ==
649 AnalysisPass->getPotentialPassManagerType())
650 // Schedule analysis pass that is managed by the same pass manager.
651 schedulePass(AnalysisPass);
652 else if (P->getPotentialPassManagerType () >
653 AnalysisPass->getPotentialPassManagerType()) {
654 // Schedule analysis pass that is managed by a new manager.
655 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000656 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000657 // are already checked are still available.
658 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000659 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000660 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000661 // passes are run on the fly.
662 delete AnalysisPass;
663 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000664 }
665 }
666
667 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000668 if (ImmutablePass *IP = P->getAsImmutablePass()) {
669 // P is a immutable pass and it will be managed by this
670 // top level manager. Set up analysis resolver to connect them.
671 PMDataManager *DM = getAsPMDataManager();
672 AnalysisResolver *AR = new AnalysisResolver(*DM);
673 P->setResolver(AR);
674 DM->initializeAnalysisImpl(P);
675 addImmutablePass(IP);
676 DM->recordAvailableAnalysis(IP);
677 return;
678 }
679
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000680 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000681 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000682 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000683 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
684 }
685
686 // Add the requested pass to the best available pass manager.
687 P->assignPassManager(activeStack, getTopLevelPassManagerType());
688
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000689 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000690 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000691 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000692 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
693 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000694}
695
696/// Find the pass that implements Analysis AID. Search immutable
697/// passes and all pass managers. If desired pass is not found
698/// then return NULL.
699Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000700 // For immutable passes we have a direct mapping from ID to pass, so check
701 // that first.
702 if (Pass *P = ImmutablePassMap.lookup(AID))
703 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000704
Devang Patelcd6ba152006-12-12 22:50:05 +0000705 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000706 for (PMDataManager *PassManager : PassManagers)
707 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000708 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000709
710 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000711 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
712 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000713 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000714
Craig Topperc6207612014-04-09 06:08:46 +0000715 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000716}
717
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000718const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
719 const PassInfo *&PI = AnalysisPassInfos[AID];
720 if (!PI)
721 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
722 else
723 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
724 "The pass info pointer changed for an analysis ID!");
725
726 return PI;
727}
728
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000729void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
730 P->initializePass();
731 ImmutablePasses.push_back(P);
732
733 // Add this pass to the map from its analysis ID. We clobber any prior runs
734 // of the pass in the map so that the last one added is the one found when
735 // doing lookups.
736 AnalysisID AID = P->getPassID();
737 ImmutablePassMap[AID] = P;
738
739 // Also add any interfaces implemented by the immutable pass to the map for
740 // fast lookup.
741 const PassInfo *PassInf = findAnalysisPassInfo(AID);
742 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000743 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000744 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
745}
746
Devang Pateleda56172006-12-12 23:34:33 +0000747// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000748void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000749
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000750 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000751 return;
752
Devang Pateleda56172006-12-12 23:34:33 +0000753 // Print out the immutable passes
754 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000755 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000756 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000757
Dan Gohmanf71c5212010-08-19 01:29:07 +0000758 // Every class that derives from PMDataManager also derives from Pass
759 // (sometimes indirectly), but there's no inheritance relationship
760 // between PMDataManager and Pass, so we have to getAsPass to get
761 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000762 for (PMDataManager *Manager : PassManagers)
763 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000764}
765
Devang Patel991aeba2006-12-15 20:13:01 +0000766void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000767
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000768 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000769 return;
770
David Greene994e1bb2010-01-05 01:30:02 +0000771 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000772 for (ImmutablePass *P : ImmutablePasses)
773 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000774 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000775 if (!PI->isAnalysisGroup())
776 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000777 }
Yaron Keren83009952016-04-28 14:49:44 +0000778 for (PMDataManager *PM : PassManagers)
779 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000780 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000781}
782
Devang Patele3068402006-12-21 00:16:50 +0000783void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000784 for (PMDataManager *PM : PassManagers)
785 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000786
Devang Patele3068402006-12-21 00:16:50 +0000787 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000788 for (PMDataManager *IPM : IndirectPassManagers)
789 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000790
Yaron Kerenbd873192016-10-02 19:21:41 +0000791 for (auto LU : LastUser) {
792 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
793 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000794 }
Devang Patele3068402006-12-21 00:16:50 +0000795}
796
Devang Patele7599552007-01-12 18:52:44 +0000797/// Destructor
798PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000799 for (PMDataManager *PM : PassManagers)
800 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000801
Yaron Keren83009952016-04-28 14:49:44 +0000802 for (ImmutablePass *P : ImmutablePasses)
803 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000804}
805
Devang Patelafb1f3622006-12-12 22:35:25 +0000806//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000807// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000808
Devang Patel643676c2006-11-11 01:10:19 +0000809/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000810void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000811 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000812
Chris Lattner60987362009-03-06 05:53:14 +0000813 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000814
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000815 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000816
Dan Gohmande6188a2010-08-12 23:50:08 +0000817 // This pass is the current implementation of all of the interfaces it
818 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000819 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000820 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000821 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000822 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000823 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000824}
825
Devang Patel9d9fc902007-03-06 17:52:53 +0000826// Return true if P preserves high level analysis used by other
827// passes managed by this manager
828bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000829 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000830 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000831 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000832
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000833 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000834 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000835 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000836 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000837 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000838 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000839
Devang Patel9d9fc902007-03-06 17:52:53 +0000840 return true;
841}
842
Chris Lattner02eb94c2008-08-07 07:34:50 +0000843/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000844void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000845 // Don't do this unless assertions are enabled.
846#ifdef NDEBUG
847 return;
848#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000849 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
850 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000851
Devang Patelef432532007-07-19 05:36:09 +0000852 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000853 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000854 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000855 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000856 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000857 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000858 }
859}
860
Devang Patel67c79a42008-07-01 19:50:56 +0000861/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000862void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000863 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
864 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000865 return;
866
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000867 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000868 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000869 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000870 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000871 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000872 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000873 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000874 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000875 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000876 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
877 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000878 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000879 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000880 }
Devang Patel349170f2006-11-11 01:24:55 +0000881 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000882
Devang Patel42dd1e92007-03-06 01:55:46 +0000883 // Check inherited analysis also. If P is not preserving analysis
884 // provided by parent manager then remove it here.
885 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
886
887 if (!InheritedAnalysis[Index])
888 continue;
889
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000890 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000891 I = InheritedAnalysis[Index]->begin(),
892 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000893 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000894 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000895 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000896 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000897 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000898 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000899 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
900 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000901 }
Devang Patel01919d22007-03-08 19:05:01 +0000902 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000903 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000904 }
905 }
Devang Patelf68a3492006-11-07 22:35:17 +0000906}
907
Devang Patelca189262006-11-14 03:05:08 +0000908/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000909void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000910 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000911
Devang Patel8adae862007-07-20 18:04:54 +0000912 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000913
Devang Patel2ff44922007-04-16 20:39:59 +0000914 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000915 if (!TPM)
916 return;
917
Devang Patel17ad0962006-12-08 00:37:52 +0000918 TPM->collectLastUses(DeadPasses, P);
919
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000920 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000921 dbgs() << " -*- '" << P->getPassName();
922 dbgs() << "' is the last user of following pass instances.";
923 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000924 }
925
Yaron Kerenbd873192016-10-02 19:21:41 +0000926 for (Pass *P : DeadPasses)
927 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000928}
Devang Patel200d3052006-12-13 23:50:44 +0000929
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000930void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000931 enum PassDebuggingString DBG_STR) {
932 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000933
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000934 {
935 // If the pass crashes releasing memory, remember this.
936 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000937 TimeRegion PassTimer(getPassTimer(P));
938
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000939 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000940 }
941
Owen Andersona7aed182010-08-06 18:33:48 +0000942 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000943 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000944 // Remove the pass itself (if it is not already removed).
945 AvailableAnalysis.erase(PI);
946
947 // Remove all interfaces this pass implements, for which it is also
948 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000949 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000950 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000951 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000952 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000954 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000955 }
Devang Patel17ad0962006-12-08 00:37:52 +0000956 }
Devang Patelca189262006-11-14 03:05:08 +0000957}
958
Dan Gohmande6188a2010-08-12 23:50:08 +0000959/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000960/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000961void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000962 // This manager is going to manage pass P. Set up analysis resolver
963 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000964 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000965 P->setResolver(AR);
966
Devang Patelec2b9a72007-03-05 22:57:49 +0000967 // If a FunctionPass F is the last user of ModulePass info M
968 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000969 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000970
Chris Lattner60987362009-03-06 05:53:14 +0000971 if (!ProcessAnalysis) {
972 // Add pass
973 PassVector.push_back(P);
974 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000975 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000976
Chris Lattner60987362009-03-06 05:53:14 +0000977 // At the moment, this pass is the last user of all required passes.
978 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000979 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000980 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
981
982 unsigned PDepth = this->getDepth();
983
Chandler Carruth44a13852015-08-19 03:02:12 +0000984 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
985 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000986 unsigned RDepth = 0;
987
Chandler Carruth44a13852015-08-19 03:02:12 +0000988 assert(PUsed->getResolver() && "Analysis Resolver is not set");
989 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +0000990 RDepth = DM.getDepth();
991
992 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +0000993 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000994 else if (PDepth > RDepth) {
995 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +0000996 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000997 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +0000998 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +0000999 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001000 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001001 }
1002
1003 // Set P as P's last user until someone starts using P.
1004 // However, if P is a Pass Manager then it does not need
1005 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001006 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001007 LastUses.push_back(P);
1008 TPM->setLastUser(LastUses, P);
1009
1010 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001011 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001012 TPM->setLastUser(TransferLastUses, My_PM);
1013 TransferLastUses.clear();
1014 }
1015
Dan Gohman6304db32010-08-16 22:57:28 +00001016 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001017 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1018 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001019 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001020 this->addLowerLevelRequiredPass(P, AnalysisPass);
1021 }
1022
1023 // Take a note of analysis required and made available by this pass.
1024 // Remove the analysis not preserved by this pass
1025 removeNotPreservedAnalysis(P);
1026 recordAvailableAnalysis(P);
1027
Devang Patel8cad70d2006-11-11 01:51:02 +00001028 // Add pass
1029 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001030}
1031
Devang Patele64d3052007-04-16 20:12:57 +00001032
Chandler Carruth44a13852015-08-19 03:02:12 +00001033/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001034/// pass P and are available. Populate RP_NotAvail with analysis
1035/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001036void PMDataManager::collectRequiredAndUsedAnalyses(
1037 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1038 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001039 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001040
1041 for (const auto &UsedID : AnUsage->getUsedSet())
1042 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1043 UP.push_back(AnalysisPass);
1044
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001045 for (const auto &RequiredID : AnUsage->getRequiredSet())
1046 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001047 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001048 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001049 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001050
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001051 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1052 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001053 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001054 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001055 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001056}
1057
Devang Patel07f4f582006-11-14 21:49:36 +00001058// All Required analyses should be available to the pass as it runs! Here
1059// we fill in the AnalysisImpls member of the pass so that it can
1060// successfully use the getAnalysis() method to retrieve the
1061// implementations it needs.
1062//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001063void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001064 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1065
Chris Lattnercbd160f2008-08-08 05:33:04 +00001066 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001067 I = AnUsage->getRequiredSet().begin(),
1068 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001069 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001070 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001071 // This may be analysis pass that is initialized on the fly.
1072 // If that is not the case then it will raise an assert when it is used.
1073 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001074 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001075 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001076 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001077 }
1078}
1079
Devang Patel640c5bb2006-12-08 22:30:11 +00001080/// Find the pass that implements Analysis AID. If desired pass is not found
1081/// then return NULL.
1082Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1083
1084 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001085 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001086
1087 if (I != AvailableAnalysis.end())
1088 return I->second;
1089
1090 // Search Parents through TopLevelManager
1091 if (SearchParent)
1092 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001093
Craig Topperc6207612014-04-09 06:08:46 +00001094 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001095}
1096
Devang Patel991aeba2006-12-15 20:13:01 +00001097// Print list of passes that are last used by P.
1098void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1099
Devang Patel8adae862007-07-20 18:04:54 +00001100 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001101
1102 // If this is a on the fly manager then it does not have TPM.
1103 if (!TPM)
1104 return;
1105
Devang Patel991aeba2006-12-15 20:13:01 +00001106 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001107
Dan Gohman7224bce2010-10-12 00:11:18 +00001108 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001109 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001110 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001111 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001112 }
1113}
1114
1115void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001116 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001117 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001118 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001119 PMD->dumpPassArguments();
1120 else
Owen Andersona7aed182010-08-06 18:33:48 +00001121 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001122 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001123 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001124 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001125 }
1126}
1127
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001128void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1129 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001130 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001131 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001132 return;
Chandler Carruth20c56932014-04-27 23:59:25 +00001133 dbgs() << "[" << sys::TimeValue::now().str() << "] " << (void *)this
1134 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001135 switch (S1) {
1136 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001137 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001138 break;
1139 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
1145 default:
1146 break;
1147 }
1148 switch (S2) {
1149 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001151 break;
1152 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001154 break;
1155 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001158 case ON_REGION_MSG:
1159 dbgs() << "' on Region '" << Msg << "'...\n";
1160 break;
Devang Patel003a5592007-03-05 20:01:30 +00001161 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001165 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001166 break;
1167 default:
1168 break;
1169 }
Devang Patel991aeba2006-12-15 20:13:01 +00001170}
1171
Chris Lattner4c1e9542009-03-06 06:45:05 +00001172void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001173 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001174 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001175
Chris Lattner4c493d92008-08-08 15:14:09 +00001176 AnalysisUsage analysisUsage;
1177 P->getAnalysisUsage(analysisUsage);
1178 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1179}
1180
Chris Lattner4c1e9542009-03-06 06:45:05 +00001181void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001182 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001183 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001184
Chris Lattner4c493d92008-08-08 15:14:09 +00001185 AnalysisUsage analysisUsage;
1186 P->getAnalysisUsage(analysisUsage);
1187 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1188}
1189
Chandler Carruth44a13852015-08-19 03:02:12 +00001190void PMDataManager::dumpUsedSet(const Pass *P) const {
1191 if (PassDebugging < Details)
1192 return;
1193
1194 AnalysisUsage analysisUsage;
1195 P->getAnalysisUsage(analysisUsage);
1196 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1197}
1198
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001199void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001200 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001201 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001202 if (Set.empty())
1203 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001204 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001205 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001206 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001207 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001208 if (!PInf) {
1209 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1210 // all drivers.
1211 dbgs() << " Uninitialized Pass";
1212 continue;
1213 }
Owen Andersona7aed182010-08-06 18:33:48 +00001214 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001215 }
David Greene994e1bb2010-01-05 01:30:02 +00001216 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001217}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001218
Devang Patel004937b2007-07-27 20:06:09 +00001219/// Add RequiredPass into list of lower level passes required by pass P.
1220/// RequiredPass is run on the fly by Pass Manager when P requests it
1221/// through getAnalysis interface.
1222/// This should be handled by specific pass manager.
1223void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1224 if (TPM) {
1225 TPM->dumpArguments();
1226 TPM->dumpPasses();
1227 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001228
Dan Gohmande6188a2010-08-12 23:50:08 +00001229 // Module Level pass may required Function Level analysis info
1230 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1231 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001232 // module level pass is requiring lower level analysis info managed by
1233 // lower level pass manager.
1234
1235 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001236 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001237 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001238#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001239 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1240 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001241#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001242 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001243}
1244
Owen Andersona7aed182010-08-06 18:33:48 +00001245Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001246 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001247}
1248
Devang Patele7599552007-01-12 18:52:44 +00001249// Destructor
1250PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001251 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001252 E = PassVector.end(); I != E; ++I)
1253 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001254}
1255
Devang Patel9bdf7d42006-12-08 23:28:54 +00001256//===----------------------------------------------------------------------===//
1257// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001258// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1259Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001260 return PM.findAnalysisPass(ID, dir);
1261}
1262
Dan Gohmande6188a2010-08-12 23:50:08 +00001263Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001264 Function &F) {
1265 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1266}
1267
Devang Patela1514cb2006-12-07 19:39:39 +00001268//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001269// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001270
Dan Gohmande6188a2010-08-12 23:50:08 +00001271/// Execute all of the passes scheduled for execution by invoking
1272/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001273/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001274bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001275 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001276 return false;
1277
Devang Patele9585592006-12-08 01:38:28 +00001278 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001279
Devang Patel6e5a1132006-11-07 21:31:57 +00001280 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001281 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1282 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001283 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001284
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001285 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001286 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001287
Devang Patelabfbe3b2006-12-16 00:56:26 +00001288 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001289
Chris Lattner4c1e9542009-03-06 06:45:05 +00001290 {
1291 // If the pass crashes, remember this.
1292 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001293 TimeRegion PassTimer(getPassTimer(BP));
1294
Dan Gohman74b189f2010-03-01 17:34:28 +00001295 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001296 }
Devang Patel93a197c2006-12-14 00:08:04 +00001297
Dan Gohman74b189f2010-03-01 17:34:28 +00001298 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001299 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001300 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001301 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001302 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001303 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001304
Devang Patela273d1c2007-07-19 18:02:32 +00001305 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001306 removeNotPreservedAnalysis(BP);
1307 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001308 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001309 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001310
Bill Wendling6ce6d262009-12-25 13:50:18 +00001311 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001312}
1313
Devang Patel475c4532006-12-08 00:59:05 +00001314// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001315bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001316 bool Changed = false;
1317
Chris Lattner4c1e9542009-03-06 06:45:05 +00001318 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1319 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001320
1321 return Changed;
1322}
1323
Duncan Sands51495602009-02-13 09:42:34 +00001324bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001325 bool Changed = false;
1326
Pedro Artigas41b98842012-12-05 17:12:22 +00001327 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001328 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001329
1330 return Changed;
1331}
1332
Duncan Sands51495602009-02-13 09:42:34 +00001333bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001334 bool Changed = false;
1335
Devang Patelabfbe3b2006-12-16 00:56:26 +00001336 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1337 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001338 Changed |= BP->doInitialization(F);
1339 }
1340
1341 return Changed;
1342}
1343
Duncan Sands51495602009-02-13 09:42:34 +00001344bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001345 bool Changed = false;
1346
Devang Patelabfbe3b2006-12-16 00:56:26 +00001347 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1348 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001349 Changed |= BP->doFinalization(F);
1350 }
1351
1352 return Changed;
1353}
1354
1355
Devang Patela1514cb2006-12-07 19:39:39 +00001356//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001357// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001358
Devang Patel4e12f862006-11-08 10:44:40 +00001359/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001360FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001361 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001362 // FPM is the top level manager.
1363 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001364
Dan Gohman565df952008-03-13 02:08:36 +00001365 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001366 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001367}
1368
Devang Patelb67904d2006-12-13 02:36:01 +00001369FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001370 delete FPM;
1371}
1372
Dan Gohmande6188a2010-08-12 23:50:08 +00001373void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001374 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001375}
1376
Devang Patel9f3083e2006-11-15 19:39:54 +00001377/// run - Execute all of the passes scheduled for execution. Keep
1378/// track of whether any of the passes modifies the function, and if
1379/// so, return true.
1380///
Devang Patelb67904d2006-12-13 02:36:01 +00001381bool FunctionPassManager::run(Function &F) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +00001382 if (std::error_code EC = F.materialize())
1383 report_fatal_error("Error reading bitcode file: " + EC.message());
Devang Patel272908d2006-12-08 22:57:48 +00001384 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001385}
1386
1387
Devang Patelff631ae2006-11-15 01:27:05 +00001388/// doInitialization - Run all of the initializers for the function passes.
1389///
Devang Patelb67904d2006-12-13 02:36:01 +00001390bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001391 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001392}
1393
Dan Gohmane6656eb2007-07-30 14:51:13 +00001394/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001395///
Devang Patelb67904d2006-12-13 02:36:01 +00001396bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001397 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001398}
1399
Devang Patela1514cb2006-12-07 19:39:39 +00001400//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001401// FunctionPassManagerImpl implementation
1402//
Duncan Sands51495602009-02-13 09:42:34 +00001403bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001404 bool Changed = false;
1405
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001406 dumpArguments();
1407 dumpPasses();
1408
Yaron Keren4849aa32015-06-05 17:48:47 +00001409 for (ImmutablePass *ImPass : getImmutablePasses())
1410 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001411
Chris Lattner4c1e9542009-03-06 06:45:05 +00001412 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1413 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001414
1415 return Changed;
1416}
1417
Duncan Sands51495602009-02-13 09:42:34 +00001418bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001419 bool Changed = false;
1420
Pedro Artigas41b98842012-12-05 17:12:22 +00001421 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001422 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423
Yaron Keren4849aa32015-06-05 17:48:47 +00001424 for (ImmutablePass *ImPass : getImmutablePasses())
1425 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001426
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427 return Changed;
1428}
1429
Devang Patelec9c58f2009-04-01 22:34:41 +00001430/// cleanup - After running all passes, clean up pass manager cache.
1431void FPPassManager::cleanup() {
1432 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1433 FunctionPass *FP = getContainedPass(Index);
1434 AnalysisResolver *AR = FP->getResolver();
1435 assert(AR && "Analysis Resolver is not set");
1436 AR->clearAnalysisImpls();
1437 }
1438}
1439
Torok Edwin24c78352009-06-29 18:49:09 +00001440void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1441 if (!wasRun)
1442 return;
1443 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1444 FPPassManager *FPPM = getContainedManager(Index);
1445 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1446 FPPM->getContainedPass(Index)->releaseMemory();
1447 }
1448 }
Torok Edwin896556e2009-06-29 21:05:10 +00001449 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001450}
1451
Devang Patel67d6a5e2006-12-19 19:46:59 +00001452// Execute all the passes managed by this top level manager.
1453// Return true if any function is modified by a pass.
1454bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001456 TimingInfo::createTheTimeInfo();
1457
Devang Patele3068402006-12-21 00:16:50 +00001458 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001459 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001460 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001461 F.getContext().yield();
1462 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001463
1464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1465 getContainedManager(Index)->cleanup();
1466
Torok Edwin24c78352009-06-29 18:49:09 +00001467 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001468 return Changed;
1469}
1470
1471//===----------------------------------------------------------------------===//
1472// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001473
Devang Patel8c78a0b2007-05-03 01:11:54 +00001474char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001475/// Print passes managed by this manager
1476void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001477 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001478 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1479 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001480 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001481 dumpLastUses(FP, Offset+1);
1482 }
1483}
1484
1485
Dan Gohmande6188a2010-08-12 23:50:08 +00001486/// Execute all of the passes scheduled for execution by invoking
1487/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001488/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001489bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001490 if (F.isDeclaration())
1491 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001492
1493 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001494
Devang Patelcbbf2912008-03-20 01:09:53 +00001495 // Collect inherited analysis from Module level pass manager.
1496 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001497
Devang Patelabfbe3b2006-12-16 00:56:26 +00001498 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1499 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001500 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001501
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001502 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001503 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001504
Devang Patelabfbe3b2006-12-16 00:56:26 +00001505 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001506
Chris Lattner4c1e9542009-03-06 06:45:05 +00001507 {
1508 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001509 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001510
Dan Gohman74b189f2010-03-01 17:34:28 +00001511 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001512 }
Devang Patel93a197c2006-12-14 00:08:04 +00001513
Dan Gohman74b189f2010-03-01 17:34:28 +00001514 Changed |= LocalChanged;
1515 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001516 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001517 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001518 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001519
Devang Patela273d1c2007-07-19 18:02:32 +00001520 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001521 removeNotPreservedAnalysis(FP);
1522 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001523 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001524 }
1525 return Changed;
1526}
1527
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001529 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001531 for (Function &F : M)
1532 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001533
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001534 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535}
1536
Duncan Sands51495602009-02-13 09:42:34 +00001537bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001538 bool Changed = false;
1539
Chris Lattner4c1e9542009-03-06 06:45:05 +00001540 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1541 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001542
Devang Patelff631ae2006-11-15 01:27:05 +00001543 return Changed;
1544}
1545
Duncan Sands51495602009-02-13 09:42:34 +00001546bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001547 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001548
1549 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001550 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001551
Devang Patelff631ae2006-11-15 01:27:05 +00001552 return Changed;
1553}
1554
Devang Patela1514cb2006-12-07 19:39:39 +00001555//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001556// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001557
Dan Gohmande6188a2010-08-12 23:50:08 +00001558/// Execute all of the passes scheduled for execution by invoking
1559/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001560/// the module, and if so, return true.
1561bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001562MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001563 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001564
Torok Edwin24c78352009-06-29 18:49:09 +00001565 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001566 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1567 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001568 Changed |= FPP->doInitialization(M);
1569 }
1570
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001571 // Initialize module passes
1572 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1573 Changed |= getContainedPass(Index)->doInitialization(M);
1574
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1576 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001577 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001578
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001579 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001580 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001581
Devang Patelabfbe3b2006-12-16 00:56:26 +00001582 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001583
Chris Lattner4c1e9542009-03-06 06:45:05 +00001584 {
1585 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001586 TimeRegion PassTimer(getPassTimer(MP));
1587
Dan Gohman74b189f2010-03-01 17:34:28 +00001588 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001589 }
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Dan Gohman74b189f2010-03-01 17:34:28 +00001591 Changed |= LocalChanged;
1592 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001593 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001594 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001595 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001596 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001597
Devang Patela273d1c2007-07-19 18:02:32 +00001598 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001599 removeNotPreservedAnalysis(MP);
1600 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001601 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001602 }
Torok Edwin24c78352009-06-29 18:49:09 +00001603
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001604 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001605 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001606 Changed |= getContainedPass(Index)->doFinalization(M);
1607
Torok Edwin24c78352009-06-29 18:49:09 +00001608 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001609 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1610 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001611 // We don't know when is the last time an on-the-fly pass is run,
1612 // so we need to releaseMemory / finalize here
1613 FPP->releaseMemoryOnTheFly();
1614 Changed |= FPP->doFinalization(M);
1615 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001616
Devang Patel05e1a972006-11-07 22:03:15 +00001617 return Changed;
1618}
1619
Devang Patele64d3052007-04-16 20:12:57 +00001620/// Add RequiredPass into list of lower level passes required by pass P.
1621/// RequiredPass is run on the fly by Pass Manager when P requests it
1622/// through getAnalysis interface.
1623void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001624 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1625 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001626 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001627 RequiredPass->getPotentialPassManagerType()) &&
1628 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001629 if (!RequiredPass)
1630 return;
Devang Patele64d3052007-04-16 20:12:57 +00001631
Devang Patel68f72b12007-04-26 17:50:19 +00001632 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001633 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001634 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001635 // FPP is the top level manager.
1636 FPP->setTopLevelManager(FPP);
1637
Devang Patel69e9f6d2007-04-16 20:27:05 +00001638 OnTheFlyManagers[P] = FPP;
1639 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001640 const PassInfo *RequiredPassPI =
1641 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001642
Craig Topperc6207612014-04-09 06:08:46 +00001643 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001644 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1645 FoundPass =
1646 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001647 }
Andrew Trick02066f22014-04-08 03:40:34 +00001648 if (!FoundPass) {
1649 FoundPass = RequiredPass;
1650 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001651 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001652 FPP->add(RequiredPass);
1653 }
1654 // Register P as the last user of FoundPass or RequiredPass.
1655 SmallVector<Pass *, 1> LU;
1656 LU.push_back(FoundPass);
1657 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001658}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001659
Dan Gohmande6188a2010-08-12 23:50:08 +00001660/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001661/// required by module pass MP. Instantiate analysis pass, by using
1662/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001663Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001664 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001665 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001666
Torok Edwin24c78352009-06-29 18:49:09 +00001667 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001668 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001669 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001670}
1671
1672
Devang Patela1514cb2006-12-07 19:39:39 +00001673//===----------------------------------------------------------------------===//
1674// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001675
Devang Patelab97cf42006-12-13 00:09:23 +00001676//
Devang Patelc290c8a2006-11-07 22:23:34 +00001677/// run - Execute all of the passes scheduled for execution. Keep track of
1678/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001679bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001680 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001681 TimingInfo::createTheTimeInfo();
1682
Devang Patelcfd70c42006-12-13 22:10:00 +00001683 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001684 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001685
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001686 for (ImmutablePass *ImPass : getImmutablePasses())
1687 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001688
Devang Patele3068402006-12-21 00:16:50 +00001689 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001690 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001691 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001692 M.getContext().yield();
1693 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001694
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001695 for (ImmutablePass *ImPass : getImmutablePasses())
1696 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001697
Devang Patelc290c8a2006-11-07 22:23:34 +00001698 return Changed;
1699}
Devang Patel376fefa2006-11-08 10:29:57 +00001700
Devang Patela1514cb2006-12-07 19:39:39 +00001701//===----------------------------------------------------------------------===//
1702// PassManager implementation
1703
Devang Patel376fefa2006-11-08 10:29:57 +00001704/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001705PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001706 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001707 // PM is the top level manager
1708 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001709}
1710
Devang Patelb67904d2006-12-13 02:36:01 +00001711PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001712 delete PM;
1713}
1714
Chris Lattner60987362009-03-06 05:53:14 +00001715void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001716 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001717}
1718
1719/// run - Execute all of the passes scheduled for execution. Keep track of
1720/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001721bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001722 return PM->run(M);
1723}
1724
Devang Patelb8817b92006-12-14 00:59:42 +00001725//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001726// TimingInfo implementation
1727
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001728bool llvm::TimePassesIsEnabled = false;
1729static cl::opt<bool,true>
1730EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1731 cl::desc("Time each pass, printing elapsed time for each on exit"));
1732
Devang Patelb8817b92006-12-14 00:59:42 +00001733// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001734// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001735// null. It may be called multiple times.
1736void TimingInfo::createTheTimeInfo() {
1737 if (!TimePassesIsEnabled || TheTimeInfo) return;
1738
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001739 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001740 // This guarantees that the object will be constructed before static globals,
1741 // thus it will be destroyed before them.
1742 static ManagedStatic<TimingInfo> TTI;
1743 TheTimeInfo = &*TTI;
1744}
1745
Devang Patel1c3633e2007-01-29 23:10:37 +00001746/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001747Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001748 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001749 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001750 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001751}
1752
Devang Patel1c56a632007-01-08 19:29:38 +00001753//===----------------------------------------------------------------------===//
1754// PMStack implementation
1755//
Devang Patelad98d232007-01-11 22:15:30 +00001756
Devang Patel1c56a632007-01-08 19:29:38 +00001757// Pop Pass Manager from the stack and clear its analysis info.
1758void PMStack::pop() {
1759
1760 PMDataManager *Top = this->top();
1761 Top->initializeAnalysisInfo();
1762
1763 S.pop_back();
1764}
1765
1766// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001767void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001768 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001769 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001770
Chris Lattner60987362009-03-06 05:53:14 +00001771 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001772 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1773 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001774 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001775
Chris Lattner60987362009-03-06 05:53:14 +00001776 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001777 TPM->addIndirectPassManager(PM);
1778 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001779 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001780 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001781 assert((PM->getPassManagerType() == PMT_ModulePassManager
1782 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001783 && "pushing bad pass manager to PMStack");
1784 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001785 }
1786
Devang Patel15701b52007-01-11 00:19:00 +00001787 S.push_back(PM);
1788}
1789
1790// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001791LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001792 for (PMDataManager *Manager : S)
1793 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001794
Devang Patel15701b52007-01-11 00:19:00 +00001795 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001796 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001797}
1798
Devang Patel1c56a632007-01-08 19:29:38 +00001799/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001800/// add self into that manager.
1801void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001802 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001803 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001804 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001805 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1806 if (TopPMType == PreferredType)
1807 break; // We found desired pass manager
1808 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001809 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001810 else
1811 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001812 }
Devang Patel18ff6362008-09-09 21:38:40 +00001813 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001814 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001815}
1816
Devang Patel3312f752007-01-16 21:43:18 +00001817/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001818/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001819void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001820 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001821
Andrew Trickcbc845f2012-02-01 07:16:20 +00001822 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001823 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001824 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1825 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001826 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001827 break;
Devang Patel3312f752007-01-16 21:43:18 +00001828 }
Devang Patel3312f752007-01-16 21:43:18 +00001829
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001830 // Create new Function Pass Manager if needed.
1831 FPPassManager *FPP;
1832 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1833 FPP = (FPPassManager *)PMS.top();
1834 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001835 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1836 PMDataManager *PMD = PMS.top();
1837
1838 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001839 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001840 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001841
1842 // [2] Set up new manager's top level manager
1843 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1844 TPM->addIndirectPassManager(FPP);
1845
1846 // [3] Assign manager to manage this new manager. This may create
1847 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001848 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001849
1850 // [4] Push new manager into PMS
1851 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001852 }
1853
Devang Patel3312f752007-01-16 21:43:18 +00001854 // Assign FPP as the manager of this pass.
1855 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001856}
1857
Devang Patel3312f752007-01-16 21:43:18 +00001858/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001859/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001860void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001861 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001862 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001863
Devang Patel15701b52007-01-11 00:19:00 +00001864 // Basic Pass Manager is a leaf pass manager. It does not handle
1865 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001866 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001867 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1868 BBP = (BBPassManager *)PMS.top();
1869 } else {
1870 // If leaf manager is not Basic Block Pass manager then create new
1871 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001872 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1873 PMDataManager *PMD = PMS.top();
1874
1875 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001876 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001877
1878 // [2] Set up new manager's top level manager
1879 // Basic Block Pass Manager does not live by itself
1880 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1881 TPM->addIndirectPassManager(BBP);
1882
Devang Patel15701b52007-01-11 00:19:00 +00001883 // [3] Assign manager to manage this new manager. This may create
1884 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001885 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001886
Devang Patel3312f752007-01-16 21:43:18 +00001887 // [4] Push new manager into PMS
1888 PMS.push(BBP);
1889 }
Devang Patel1c56a632007-01-08 19:29:38 +00001890
Devang Patel3312f752007-01-16 21:43:18 +00001891 // Assign BBP as the manager of this pass.
1892 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001893}
1894
Dan Gohmand3a20c92008-03-11 16:41:42 +00001895PassManagerBase::~PassManagerBase() {}