blob: 7c5cc6893bca6f181bb30bdd3ac7fc82f0bf2239 [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
Chandler Carruthb8ddc702014-01-12 11:10:32 +000015#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000016#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000018#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/IR/Module.h"
Devang Patelf1567a52006-12-13 20:03:48 +000020#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000021#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000023#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/Timer.h"
26#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000027#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000028#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000029using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000030using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000031
Devang Patele7599552007-01-12 18:52:44 +000032// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000033
Devang Patelf1567a52006-12-13 20:03:48 +000034//===----------------------------------------------------------------------===//
35// Pass debugging information. Often it is useful to find out what pass is
36// running when a crash occurs in a utility. When this library is compiled with
37// debugging on, a command line option (--debug-pass) is enabled that causes the
38// pass name to be printed before it executes.
39//
40
Chandler Carruth7caea412013-11-09 12:26:54 +000041namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000042// Different debug levels that can be enabled...
43enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000044 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000045};
Chandler Carruth7caea412013-11-09 12:26:54 +000046}
Devang Patel03fb5872006-12-13 21:13:31 +000047
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000048static cl::opt<enum PassDebugLevel>
49PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000050 cl::desc("Print PassManager debugging information"),
51 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000052 clEnumVal(Disabled , "disable debug output"),
53 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
54 clEnumVal(Structure , "print pass structure before run()"),
55 clEnumVal(Executions, "print pass name before it is executed"),
56 clEnumVal(Details , "print pass details when it is executed"),
57 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000058
Chandler Carruth7caea412013-11-09 12:26:54 +000059namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000060typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
61PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000062}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000063
64// Print IR out before/after specified passes.
65static PassOptionList
66PrintBefore("print-before",
67 llvm::cl::desc("Print IR before specified passes"),
68 cl::Hidden);
69
70static PassOptionList
71PrintAfter("print-after",
72 llvm::cl::desc("Print IR after specified passes"),
73 cl::Hidden);
74
75static cl::opt<bool>
76PrintBeforeAll("print-before-all",
77 llvm::cl::desc("Print IR before each pass"),
78 cl::init(false));
79static cl::opt<bool>
80PrintAfterAll("print-after-all",
81 llvm::cl::desc("Print IR after each pass"),
82 cl::init(false));
83
84/// This is a helper to determine whether to print IR before or
85/// after a pass.
86
87static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
88 PassOptionList &PassesToPrint) {
89 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
90 const llvm::PassInfo *PassInf = PassesToPrint[i];
91 if (PassInf)
92 if (PassInf->getPassArgument() == PI->getPassArgument()) {
93 return true;
94 }
David Greene9b063df2010-04-02 23:17:14 +000095 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000096 return false;
97}
Dan Gohmande6188a2010-08-12 23:50:08 +000098
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000099/// This is a utility to check whether a pass should have IR dumped
100/// before it.
101static bool ShouldPrintBeforePass(const PassInfo *PI) {
102 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
103}
David Greene9b063df2010-04-02 23:17:14 +0000104
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000105/// This is a utility to check whether a pass should have IR dumped
106/// after it.
107static bool ShouldPrintAfterPass(const PassInfo *PI) {
108 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000109}
110
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000111/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
112/// or higher is specified.
113bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000114 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000115}
116
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000117
118
119
Chris Lattner4c1e9542009-03-06 06:45:05 +0000120void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
121 if (V == 0 && M == 0)
122 OS << "Releasing pass '";
123 else
124 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000125
Chris Lattner4c1e9542009-03-06 06:45:05 +0000126 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000127
Chris Lattner4c1e9542009-03-06 06:45:05 +0000128 if (M) {
129 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
130 return;
131 }
132 if (V == 0) {
133 OS << '\n';
134 return;
135 }
136
Dan Gohman79fc0e92009-03-10 18:47:59 +0000137 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000138 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000139 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000141 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << "value";
144
145 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000146 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000147 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000148}
149
150
Devang Patelffca9102006-12-15 19:39:30 +0000151namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000152//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000153// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000154//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000155/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000156/// pass together and sequence them to process one basic block before
157/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000158class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000159
160public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000161 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000162 explicit BBPassManager()
163 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000164
Devang Patelca58e352006-11-08 10:05:38 +0000165 /// Execute all of the passes scheduled for execution. Keep track of
166 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000167 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000168
Devang Patelf9d96b92006-12-07 19:57:52 +0000169 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000170 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000171 Info.setPreservesAll();
172 }
173
Craig Topperf398d7c2014-03-05 06:35:38 +0000174 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000175 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000176 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000177 bool doFinalization(Function &F);
178
Craig Topperf398d7c2014-03-05 06:35:38 +0000179 PMDataManager *getAsPMDataManager() override { return this; }
180 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000181
Craig Topperf398d7c2014-03-05 06:35:38 +0000182 const char *getPassName() const override {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000183 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000184 }
185
Devang Pateleda56172006-12-12 23:34:33 +0000186 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000187 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000188 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000189 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
190 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000191 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000192 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000193 }
194 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000195
196 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000197 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000198 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
199 return BP;
200 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000201
Craig Topperf398d7c2014-03-05 06:35:38 +0000202 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000203 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000204 }
Devang Patelca58e352006-11-08 10:05:38 +0000205};
206
Devang Patel8c78a0b2007-05-03 01:11:54 +0000207char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000208} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000209
Devang Patele7599552007-01-12 18:52:44 +0000210namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000211namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000212//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000213// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000214//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000215/// FunctionPassManagerImpl manages FPPassManagers
216class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000217 public PMDataManager,
218 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000219 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000220private:
221 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000222public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000223 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000224 explicit FunctionPassManagerImpl() :
225 Pass(PT_PassManager, ID), PMDataManager(),
226 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227
228 /// add - Add a pass to the queue of passes to run. This passes ownership of
229 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
230 /// will be destroyed as well, so there is no need to delete the pass. This
231 /// implies that all passes MUST be allocated with 'new'.
232 void add(Pass *P) {
233 schedulePass(P);
234 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000235
236 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000237 Pass *createPrinterPass(raw_ostream &O,
238 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000239 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000240 }
241
Torok Edwin24c78352009-06-29 18:49:09 +0000242 // Prepare for running an on the fly pass, freeing memory if needed
243 // from a previous run.
244 void releaseMemoryOnTheFly();
245
Devang Patel67d6a5e2006-12-19 19:46:59 +0000246 /// run - Execute all of the passes scheduled for execution. Keep track of
247 /// whether any of the passes modifies the module, and if so, return true.
248 bool run(Function &F);
249
250 /// doInitialization - Run all of the initializers for the function passes.
251 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000252 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000253
Dan Gohmane6656eb2007-07-30 14:51:13 +0000254 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000256 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000257
Dan Gohmande6188a2010-08-12 23:50:08 +0000258
Craig Topperf398d7c2014-03-05 06:35:38 +0000259 PMDataManager *getAsPMDataManager() override { return this; }
260 Pass *getAsPass() override { return this; }
261 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000262 return PMT_FunctionPassManager;
263 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000264
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000266 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000267 Info.setPreservesAll();
268 }
269
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000271 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
273 return FP;
274 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000275};
276
David Blaikiea379b1812011-12-20 02:50:00 +0000277void FunctionPassManagerImpl::anchor() {}
278
Devang Patel8c78a0b2007-05-03 01:11:54 +0000279char FunctionPassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000280} // End of legacy namespace
281} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000282
Chandler Carruth7caea412013-11-09 12:26:54 +0000283namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284//===----------------------------------------------------------------------===//
285// MPPassManager
286//
287/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000288/// It batches all Module passes and function pass managers together and
289/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000291public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000292 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000293 explicit MPPassManager() :
294 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000295
296 // Delete on the fly managers.
297 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000298 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000299 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
300 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000301 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000302 delete FPP;
303 }
304 }
305
Dan Gohmande6188a2010-08-12 23:50:08 +0000306 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000307 Pass *createPrinterPass(raw_ostream &O,
308 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000309 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000310 }
311
Devang Patelca58e352006-11-08 10:05:38 +0000312 /// run - Execute all of the passes scheduled for execution. Keep track of
313 /// whether any of the passes modifies the module, and if so, return true.
314 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000315
Pedro Artigase4348b02012-12-03 21:56:57 +0000316 using llvm::Pass::doInitialization;
317 using llvm::Pass::doFinalization;
318
Owen Anderson1aa27512012-11-15 00:14:15 +0000319 /// doInitialization - Run all of the initializers for the module passes.
320 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000321 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000322
323 /// doFinalization - Run all of the finalizers for the module passes.
324 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000325 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000326
Devang Patelf9d96b92006-12-07 19:57:52 +0000327 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000328 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000329 Info.setPreservesAll();
330 }
331
Devang Patele64d3052007-04-16 20:12:57 +0000332 /// Add RequiredPass into list of lower level passes required by pass P.
333 /// RequiredPass is run on the fly by Pass Manager when P requests it
334 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000335 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000336
Dan Gohmande6188a2010-08-12 23:50:08 +0000337 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000338 /// required by module pass MP. Instantiate analysis pass, by using
339 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000340 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000341
Craig Topperf398d7c2014-03-05 06:35:38 +0000342 const char *getPassName() const override {
Devang Patele3858e62007-02-01 22:08:25 +0000343 return "Module Pass Manager";
344 }
345
Craig Topperf398d7c2014-03-05 06:35:38 +0000346 PMDataManager *getAsPMDataManager() override { return this; }
347 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000348
Devang Pateleda56172006-12-12 23:34:33 +0000349 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000350 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000351 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000352 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
353 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000354 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000355 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
356 OnTheFlyManagers.find(MP);
357 if (I != OnTheFlyManagers.end())
358 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000359 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000360 }
361 }
362
Devang Patelabfbe3b2006-12-16 00:56:26 +0000363 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000364 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000365 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000366 }
367
Craig Topperf398d7c2014-03-05 06:35:38 +0000368 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000369 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000370 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000371
372 private:
373 /// Collection of on the fly FPPassManagers. These managers manage
374 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000375 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000376};
377
Devang Patel8c78a0b2007-05-03 01:11:54 +0000378char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000379} // End anonymous namespace
380
381namespace llvm {
382namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000383//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000384// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000385//
Devang Patel09f162c2007-05-01 21:15:47 +0000386
Devang Patel67d6a5e2006-12-19 19:46:59 +0000387/// PassManagerImpl manages MPPassManagers
388class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000389 public PMDataManager,
390 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000391 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000392
393public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000394 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000395 explicit PassManagerImpl() :
396 Pass(PT_PassManager, ID), PMDataManager(),
397 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000398
Devang Patel376fefa2006-11-08 10:29:57 +0000399 /// add - Add a pass to the queue of passes to run. This passes ownership of
400 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
401 /// will be destroyed as well, so there is no need to delete the pass. This
402 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000403 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000404 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000405 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000406
407 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000408 Pass *createPrinterPass(raw_ostream &O,
409 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000410 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000411 }
412
Devang Patel376fefa2006-11-08 10:29:57 +0000413 /// run - Execute all of the passes scheduled for execution. Keep track of
414 /// whether any of the passes modifies the module, and if so, return true.
415 bool run(Module &M);
416
Pedro Artigase4348b02012-12-03 21:56:57 +0000417 using llvm::Pass::doInitialization;
418 using llvm::Pass::doFinalization;
419
Owen Anderson1aa27512012-11-15 00:14:15 +0000420 /// doInitialization - Run all of the initializers for the module passes.
421 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000422 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000423
424 /// doFinalization - Run all of the finalizers for the module passes.
425 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000426 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000427
Devang Patelf9d96b92006-12-07 19:57:52 +0000428 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000429 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000430 Info.setPreservesAll();
431 }
432
Craig Topperf398d7c2014-03-05 06:35:38 +0000433 PMDataManager *getAsPMDataManager() override { return this; }
434 Pass *getAsPass() override { return this; }
435 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000436 return PMT_ModulePassManager;
437 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000438
Devang Patel67d6a5e2006-12-19 19:46:59 +0000439 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000440 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000441 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
442 return MP;
443 }
Devang Patel376fefa2006-11-08 10:29:57 +0000444};
445
David Blaikiea379b1812011-12-20 02:50:00 +0000446void PassManagerImpl::anchor() {}
447
Devang Patel8c78a0b2007-05-03 01:11:54 +0000448char PassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000449} // End of legacy namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000450} // End of llvm namespace
451
452namespace {
453
454//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000455/// TimingInfo Class - This class is used to calculate information about the
456/// amount of time each pass takes to execute. This only happens when
457/// -time-passes is enabled on the command line.
458///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000459
Owen Anderson5a6960f2009-06-18 20:51:00 +0000460static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000461
Nick Lewycky02d5f772009-10-25 06:33:48 +0000462class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000463 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000464 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000465public:
466 // Use 'create' member to get this.
467 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000468
Devang Patel1c3633e2007-01-29 23:10:37 +0000469 // TimingDtor - Print out information about timing information
470 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000471 // Delete all of the timers, which accumulate their info into the
472 // TimerGroup.
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000473 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattner707431c2010-03-30 04:03:22 +0000474 E = TimingData.end(); I != E; ++I)
475 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000476 // TimerGroup is deleted next, printing the report.
477 }
478
479 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000480 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000481 // null. It may be called multiple times.
482 static void createTheTimeInfo();
483
Chris Lattner707431c2010-03-30 04:03:22 +0000484 /// getPassTimer - Return the timer for the specified pass if it exists.
485 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000486 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000487 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000488
Owen Anderson5c96ef72009-07-07 18:33:04 +0000489 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000490 Timer *&T = TimingData[P];
Chris Lattner707431c2010-03-30 04:03:22 +0000491 if (T == 0)
492 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000493 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000494 }
495};
496
Devang Patel1c3633e2007-01-29 23:10:37 +0000497} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000498
Dan Gohmand78c4002008-05-13 00:00:25 +0000499static TimingInfo *TheTimeInfo;
500
Devang Patela1514cb2006-12-07 19:39:39 +0000501//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000502// PMTopLevelManager implementation
503
Devang Patel4268fc02007-01-16 02:00:38 +0000504/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000505PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
506 PMDM->setTopLevelManager(this);
507 addPassManager(PMDM);
508 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000509}
510
Devang Patelafb1f3622006-12-12 22:35:25 +0000511/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000512void
Bill Wendlingea857e12012-05-14 07:53:40 +0000513PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000514 unsigned PDepth = 0;
515 if (P->getResolver())
516 PDepth = P->getResolver()->getPMDataManager().getDepth();
517
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000518 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 E = AnalysisPasses.end(); I != E; ++I) {
520 Pass *AP = *I;
521 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000522
Devang Patel01919d22007-03-08 19:05:01 +0000523 if (P == AP)
524 continue;
525
Tobias Grosserf07426b2011-01-20 21:03:22 +0000526 // Update the last users of passes that are required transitive by AP.
527 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
528 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
529 SmallVector<Pass *, 12> LastUses;
530 SmallVector<Pass *, 12> LastPMUses;
531 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
532 E = IDs.end(); I != E; ++I) {
533 Pass *AnalysisPass = findAnalysisPass(*I);
534 assert(AnalysisPass && "Expected analysis pass to exist.");
535 AnalysisResolver *AR = AnalysisPass->getResolver();
536 assert(AR && "Expected analysis resolver to exist.");
537 unsigned APDepth = AR->getPMDataManager().getDepth();
538
539 if (PDepth == APDepth)
540 LastUses.push_back(AnalysisPass);
541 else if (PDepth > APDepth)
542 LastPMUses.push_back(AnalysisPass);
543 }
544
545 setLastUser(LastUses, P);
546
547 // If this pass has a corresponding pass manager, push higher level
548 // analysis to this pass manager.
549 if (P->getResolver())
550 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
551
552
Devang Patelafb1f3622006-12-12 22:35:25 +0000553 // If AP is the last user of other passes then make P last user of
554 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000555 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000556 LUE = LastUser.end(); LUI != LUE; ++LUI) {
557 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000558 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000559 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000560 LastUser[LUI->first] = P;
561 }
562 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000563}
564
565/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000566void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000567 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000568 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000569 InversedLastUser.find(P);
570 if (DMI == InversedLastUser.end())
571 return;
572
573 SmallPtrSet<Pass *, 8> &LU = DMI->second;
574 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
575 E = LU.end(); I != E; ++I) {
576 LastUses.push_back(*I);
577 }
578
Devang Patelafb1f3622006-12-12 22:35:25 +0000579}
580
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000581AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
582 AnalysisUsage *AnUsage = NULL;
583 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000584 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000585 AnUsage = DMI->second;
586 else {
587 AnUsage = new AnalysisUsage();
588 P->getAnalysisUsage(*AnUsage);
589 AnUsageMap[P] = AnUsage;
590 }
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.
Owen Andersona7aed182010-08-06 18:33:48 +0000608 const PassInfo *PI =
609 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
610 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000611 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000612 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000613 }
Devang Patel864970e2008-03-18 00:39:19 +0000614
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000615 AnalysisUsage *AnUsage = findAnalysisUsage(P);
616
Devang Patelfdee7032008-08-14 23:07:48 +0000617 bool checkAnalysis = true;
618 while (checkAnalysis) {
619 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000620
Devang Patelfdee7032008-08-14 23:07:48 +0000621 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
622 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
623 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000624
Devang Patelfdee7032008-08-14 23:07:48 +0000625 Pass *AnalysisPass = findAnalysisPass(*I);
626 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000627 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000628
629 if (PI == NULL) {
630 // Pass P is not in the global PassRegistry
631 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
632 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
633 dbgs() << "Required Passes:" << "\n";
634 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
635 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
636 Pass *AnalysisPass2 = findAnalysisPass(*I2);
637 if (AnalysisPass2) {
638 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000639 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000640 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
641 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
642 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
643 }
644 }
645 }
646
Andrew Trick6bbaf132011-06-03 00:48:58 +0000647 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000648 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000649 if (P->getPotentialPassManagerType () ==
650 AnalysisPass->getPotentialPassManagerType())
651 // Schedule analysis pass that is managed by the same pass manager.
652 schedulePass(AnalysisPass);
653 else if (P->getPotentialPassManagerType () >
654 AnalysisPass->getPotentialPassManagerType()) {
655 // Schedule analysis pass that is managed by a new manager.
656 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000657 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000658 // are already checked are still available.
659 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000660 } else
Dan Gohmande6188a2010-08-12 23:50:08 +0000661 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000662 // passes are run on the fly.
663 delete AnalysisPass;
664 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000665 }
666 }
667
668 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000669 if (ImmutablePass *IP = P->getAsImmutablePass()) {
670 // P is a immutable pass and it will be managed by this
671 // top level manager. Set up analysis resolver to connect them.
672 PMDataManager *DM = getAsPMDataManager();
673 AnalysisResolver *AR = new AnalysisResolver(*DM);
674 P->setResolver(AR);
675 DM->initializeAnalysisImpl(P);
676 addImmutablePass(IP);
677 DM->recordAvailableAnalysis(IP);
678 return;
679 }
680
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000681 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000682 Pass *PP = P->createPrinterPass(
683 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
684 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
685 }
686
687 // Add the requested pass to the best available pass manager.
688 P->assignPassManager(activeStack, getTopLevelPassManagerType());
689
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000690 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000691 Pass *PP = P->createPrinterPass(
692 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
693 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
694 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000695}
696
697/// Find the pass that implements Analysis AID. Search immutable
698/// passes and all pass managers. If desired pass is not found
699/// then return NULL.
700Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
701
Devang Patelcd6ba152006-12-12 22:50:05 +0000702 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000703 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000704 E = PassManagers.end(); I != E; ++I)
705 if (Pass *P = (*I)->findAnalysisPass(AID, false))
706 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000707
708 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000709 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000710 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000711 E = IndirectPassManagers.end(); I != E; ++I)
712 if (Pass *P = (*I)->findAnalysisPass(AID, false))
713 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000714
Dan Gohman844dd0a2010-10-11 23:19:01 +0000715 // Check the immutable passes. Iterate in reverse order so that we find
716 // the most recently registered passes first.
Craig Topper31ee5862013-07-03 15:07:05 +0000717 for (SmallVectorImpl<ImmutablePass *>::reverse_iterator I =
Dan Gohman844dd0a2010-10-11 23:19:01 +0000718 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000719 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000720 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000721 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000722
723 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000724 const PassInfo *PassInf =
725 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000726 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000727 const std::vector<const PassInfo*> &ImmPI =
728 PassInf->getInterfacesImplemented();
729 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
730 EE = ImmPI.end(); II != EE; ++II) {
731 if ((*II)->getTypeInfo() == AID)
732 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000733 }
734 }
735
Dan Gohman844dd0a2010-10-11 23:19:01 +0000736 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000737}
738
Devang Pateleda56172006-12-12 23:34:33 +0000739// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000740void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000741
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000742 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000743 return;
744
Devang Pateleda56172006-12-12 23:34:33 +0000745 // Print out the immutable passes
746 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000747 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000748 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000749
Dan Gohmanf71c5212010-08-19 01:29:07 +0000750 // Every class that derives from PMDataManager also derives from Pass
751 // (sometimes indirectly), but there's no inheritance relationship
752 // between PMDataManager and Pass, so we have to getAsPass to get
753 // from a PMDataManager* to a Pass*.
Craig Topper31ee5862013-07-03 15:07:05 +0000754 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
755 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000756 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000757}
758
Devang Patel991aeba2006-12-15 20:13:01 +0000759void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000760
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000761 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000762 return;
763
David Greene994e1bb2010-01-05 01:30:02 +0000764 dbgs() << "Pass Arguments: ";
Craig Topper31ee5862013-07-03 15:07:05 +0000765 for (SmallVectorImpl<ImmutablePass *>::const_iterator I =
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000766 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
767 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000768 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
769 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000770 if (!PI->isAnalysisGroup())
771 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000772 }
Craig Topper31ee5862013-07-03 15:07:05 +0000773 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
774 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000775 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000776 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000777}
778
Devang Patele3068402006-12-21 00:16:50 +0000779void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000780 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000781 E = PassManagers.end(); I != E; ++I)
782 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000783
Devang Patele3068402006-12-21 00:16:50 +0000784 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000785 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000786 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
787 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000788 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000789
Chris Lattner60987362009-03-06 05:53:14 +0000790 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000791 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000792 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000793 InversedLastUser.find(DMI->second);
794 if (InvDMI != InversedLastUser.end()) {
795 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
796 L.insert(DMI->first);
797 } else {
798 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
799 InversedLastUser[DMI->second] = L;
800 }
801 }
Devang Patele3068402006-12-21 00:16:50 +0000802}
803
Devang Patele7599552007-01-12 18:52:44 +0000804/// Destructor
805PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000806 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000807 E = PassManagers.end(); I != E; ++I)
808 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000809
Dan Gohman060d5ba2010-10-12 00:15:27 +0000810 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000811 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
812 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000813
814 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000815 DME = AnUsageMap.end(); DMI != DME; ++DMI)
816 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000817}
818
Devang Patelafb1f3622006-12-12 22:35:25 +0000819//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000820// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000821
Devang Patel643676c2006-11-11 01:10:19 +0000822/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000823void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000824 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000825
Chris Lattner60987362009-03-06 05:53:14 +0000826 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000827
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000828 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000829
Dan Gohmande6188a2010-08-12 23:50:08 +0000830 // This pass is the current implementation of all of the interfaces it
831 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000832 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
833 if (PInf == 0) return;
834 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000835 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000836 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000837}
838
Devang Patel9d9fc902007-03-06 17:52:53 +0000839// Return true if P preserves high level analysis used by other
840// passes managed by this manager
841bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000842 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000843 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000844 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000845
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000846 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000847 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000848 E = HigherLevelAnalysis.end(); I != E; ++I) {
849 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000850 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000851 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000852 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000853 PreservedSet.end())
854 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000855 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000856
Devang Patel9d9fc902007-03-06 17:52:53 +0000857 return true;
858}
859
Chris Lattner02eb94c2008-08-07 07:34:50 +0000860/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000861void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000862 // Don't do this unless assertions are enabled.
863#ifdef NDEBUG
864 return;
865#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000866 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
867 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000868
Devang Patelef432532007-07-19 05:36:09 +0000869 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000870 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000871 E = PreservedSet.end(); I != E; ++I) {
872 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000873 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000874 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000875 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000876 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000877 }
878}
879
Devang Patel67c79a42008-07-01 19:50:56 +0000880/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000881void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000882 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
883 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000884 return;
885
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000886 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000887 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000888 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000889 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000890 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000891 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000892 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000893 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000894 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000895 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000896 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
897 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000898 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000899 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000900 }
Devang Patel349170f2006-11-11 01:24:55 +0000901 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000902
Devang Patel42dd1e92007-03-06 01:55:46 +0000903 // Check inherited analysis also. If P is not preserving analysis
904 // provided by parent manager then remove it here.
905 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
906
907 if (!InheritedAnalysis[Index])
908 continue;
909
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000910 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000911 I = InheritedAnalysis[Index]->begin(),
912 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000913 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000914 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000915 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000916 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000917 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000918 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000919 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000920 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
921 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000922 }
Devang Patel01919d22007-03-08 19:05:01 +0000923 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000924 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000925 }
926 }
Devang Patelf68a3492006-11-07 22:35:17 +0000927}
928
Devang Patelca189262006-11-14 03:05:08 +0000929/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000930void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000931 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000932
Devang Patel8adae862007-07-20 18:04:54 +0000933 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000934
Devang Patel2ff44922007-04-16 20:39:59 +0000935 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000936 if (!TPM)
937 return;
938
Devang Patel17ad0962006-12-08 00:37:52 +0000939 TPM->collectLastUses(DeadPasses, P);
940
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000941 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000942 dbgs() << " -*- '" << P->getPassName();
943 dbgs() << "' is the last user of following pass instances.";
944 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000945 }
946
Dan Gohman060d5ba2010-10-12 00:15:27 +0000947 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000948 E = DeadPasses.end(); I != E; ++I)
949 freePass(*I, Msg, DBG_STR);
950}
Devang Patel200d3052006-12-13 23:50:44 +0000951
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000952void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 enum PassDebuggingString DBG_STR) {
954 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000955
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000956 {
957 // If the pass crashes releasing memory, remember this.
958 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000959 TimeRegion PassTimer(getPassTimer(P));
960
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000961 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000962 }
963
Owen Andersona7aed182010-08-06 18:33:48 +0000964 AnalysisID PI = P->getPassID();
965 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000966 // Remove the pass itself (if it is not already removed).
967 AvailableAnalysis.erase(PI);
968
969 // Remove all interfaces this pass implements, for which it is also
970 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000971 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000972 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000973 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000974 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000975 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000976 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000977 }
Devang Patel17ad0962006-12-08 00:37:52 +0000978 }
Devang Patelca189262006-11-14 03:05:08 +0000979}
980
Dan Gohmande6188a2010-08-12 23:50:08 +0000981/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000982/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000983void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000984 // This manager is going to manage pass P. Set up analysis resolver
985 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000986 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000987 P->setResolver(AR);
988
Devang Patelec2b9a72007-03-05 22:57:49 +0000989 // If a FunctionPass F is the last user of ModulePass info M
990 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000991 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000992
Chris Lattner60987362009-03-06 05:53:14 +0000993 if (!ProcessAnalysis) {
994 // Add pass
995 PassVector.push_back(P);
996 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000997 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000998
Chris Lattner60987362009-03-06 05:53:14 +0000999 // At the moment, this pass is the last user of all required passes.
1000 SmallVector<Pass *, 12> LastUses;
1001 SmallVector<Pass *, 8> RequiredPasses;
1002 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1003
1004 unsigned PDepth = this->getDepth();
1005
Dan Gohmande6188a2010-08-12 23:50:08 +00001006 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +00001007 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +00001008 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001009 E = RequiredPasses.end(); I != E; ++I) {
1010 Pass *PRequired = *I;
1011 unsigned RDepth = 0;
1012
1013 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1014 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1015 RDepth = DM.getDepth();
1016
1017 if (PDepth == RDepth)
1018 LastUses.push_back(PRequired);
1019 else if (PDepth > RDepth) {
1020 // Let the parent claim responsibility of last use
1021 TransferLastUses.push_back(PRequired);
1022 // Keep track of higher level analysis used by this manager.
1023 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001024 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001025 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001026 }
1027
1028 // Set P as P's last user until someone starts using P.
1029 // However, if P is a Pass Manager then it does not need
1030 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001031 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001032 LastUses.push_back(P);
1033 TPM->setLastUser(LastUses, P);
1034
1035 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001036 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001037 TPM->setLastUser(TransferLastUses, My_PM);
1038 TransferLastUses.clear();
1039 }
1040
Dan Gohman6304db32010-08-16 22:57:28 +00001041 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001042 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001043 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001044 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001045 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1046 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001047 this->addLowerLevelRequiredPass(P, AnalysisPass);
1048 }
1049
1050 // Take a note of analysis required and made available by this pass.
1051 // Remove the analysis not preserved by this pass
1052 removeNotPreservedAnalysis(P);
1053 recordAvailableAnalysis(P);
1054
Devang Patel8cad70d2006-11-11 01:51:02 +00001055 // Add pass
1056 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001057}
1058
Devang Patele64d3052007-04-16 20:12:57 +00001059
1060/// Populate RP with analysis pass that are required by
1061/// pass P and are available. Populate RP_NotAvail with analysis
1062/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001063void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1064 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001065 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001066 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1067 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001068 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001069 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001070 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001071 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001072 else
Chris Lattner60987362009-03-06 05:53:14 +00001073 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001074 }
Devang Patelf58183d2006-12-12 23:09:32 +00001075
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001076 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001077 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001078 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001079 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001080 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001081 else
Chris Lattner60987362009-03-06 05:53:14 +00001082 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001083 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001084}
1085
Devang Patel07f4f582006-11-14 21:49:36 +00001086// All Required analyses should be available to the pass as it runs! Here
1087// we fill in the AnalysisImpls member of the pass so that it can
1088// successfully use the getAnalysis() method to retrieve the
1089// implementations it needs.
1090//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001091void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001092 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1093
Chris Lattnercbd160f2008-08-08 05:33:04 +00001094 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001095 I = AnUsage->getRequiredSet().begin(),
1096 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001097 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001098 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001099 // This may be analysis pass that is initialized on the fly.
1100 // If that is not the case then it will raise an assert when it is used.
1101 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001102 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001103 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001104 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001105 }
1106}
1107
Devang Patel640c5bb2006-12-08 22:30:11 +00001108/// Find the pass that implements Analysis AID. If desired pass is not found
1109/// then return NULL.
1110Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1111
1112 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001113 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001114
1115 if (I != AvailableAnalysis.end())
1116 return I->second;
1117
1118 // Search Parents through TopLevelManager
1119 if (SearchParent)
1120 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001121
Devang Patel9d759b82006-12-09 00:09:12 +00001122 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001123}
1124
Devang Patel991aeba2006-12-15 20:13:01 +00001125// Print list of passes that are last used by P.
1126void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1127
Devang Patel8adae862007-07-20 18:04:54 +00001128 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001129
1130 // If this is a on the fly manager then it does not have TPM.
1131 if (!TPM)
1132 return;
1133
Devang Patel991aeba2006-12-15 20:13:01 +00001134 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001135
Dan Gohman7224bce2010-10-12 00:11:18 +00001136 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001137 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001138 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001139 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001140 }
1141}
1142
1143void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001144 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001145 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001146 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001147 PMD->dumpPassArguments();
1148 else
Owen Andersona7aed182010-08-06 18:33:48 +00001149 if (const PassInfo *PI =
1150 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001151 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001153 }
1154}
1155
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001156void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1157 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001158 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001159 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001160 return;
David Greene994e1bb2010-01-05 01:30:02 +00001161 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001162 switch (S1) {
1163 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001164 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001165 break;
1166 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001167 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001168 break;
1169 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001170 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001171 break;
1172 default:
1173 break;
1174 }
1175 switch (S2) {
1176 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001177 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001178 break;
1179 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001180 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001181 break;
1182 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001183 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001184 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001185 case ON_REGION_MSG:
1186 dbgs() << "' on Region '" << Msg << "'...\n";
1187 break;
Devang Patel003a5592007-03-05 20:01:30 +00001188 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001189 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001190 break;
1191 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001192 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001193 break;
1194 default:
1195 break;
1196 }
Devang Patel991aeba2006-12-15 20:13:01 +00001197}
1198
Chris Lattner4c1e9542009-03-06 06:45:05 +00001199void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001200 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001201 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001202
Chris Lattner4c493d92008-08-08 15:14:09 +00001203 AnalysisUsage analysisUsage;
1204 P->getAnalysisUsage(analysisUsage);
1205 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1206}
1207
Chris Lattner4c1e9542009-03-06 06:45:05 +00001208void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001209 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001210 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001211
Chris Lattner4c493d92008-08-08 15:14:09 +00001212 AnalysisUsage analysisUsage;
1213 P->getAnalysisUsage(analysisUsage);
1214 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1215}
1216
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001217void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001218 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001219 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001220 if (Set.empty())
1221 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001222 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001223 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001224 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001225 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001226 if (!PInf) {
1227 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1228 // all drivers.
1229 dbgs() << " Uninitialized Pass";
1230 continue;
1231 }
Owen Andersona7aed182010-08-06 18:33:48 +00001232 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001233 }
David Greene994e1bb2010-01-05 01:30:02 +00001234 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001235}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001236
Devang Patel004937b2007-07-27 20:06:09 +00001237/// Add RequiredPass into list of lower level passes required by pass P.
1238/// RequiredPass is run on the fly by Pass Manager when P requests it
1239/// through getAnalysis interface.
1240/// This should be handled by specific pass manager.
1241void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1242 if (TPM) {
1243 TPM->dumpArguments();
1244 TPM->dumpPasses();
1245 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001246
Dan Gohmande6188a2010-08-12 23:50:08 +00001247 // Module Level pass may required Function Level analysis info
1248 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1249 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001250 // module level pass is requiring lower level analysis info managed by
1251 // lower level pass manager.
1252
1253 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001254 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001255 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001256#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001257 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1258 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001259#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001260 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001261}
1262
Owen Andersona7aed182010-08-06 18:33:48 +00001263Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001264 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001265}
1266
Devang Patele7599552007-01-12 18:52:44 +00001267// Destructor
1268PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001269 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001270 E = PassVector.end(); I != E; ++I)
1271 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001272}
1273
Devang Patel9bdf7d42006-12-08 23:28:54 +00001274//===----------------------------------------------------------------------===//
1275// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001276// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1277Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001278 return PM.findAnalysisPass(ID, dir);
1279}
1280
Dan Gohmande6188a2010-08-12 23:50:08 +00001281Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001282 Function &F) {
1283 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1284}
1285
Devang Patela1514cb2006-12-07 19:39:39 +00001286//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001288
Dan Gohmande6188a2010-08-12 23:50:08 +00001289/// Execute all of the passes scheduled for execution by invoking
1290/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001291/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001292bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001293 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001294 return false;
1295
Devang Patele9585592006-12-08 01:38:28 +00001296 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001297
Devang Patel6e5a1132006-11-07 21:31:57 +00001298 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001299 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1300 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001301 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001302
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001303 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001304 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001305
Devang Patelabfbe3b2006-12-16 00:56:26 +00001306 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001307
Chris Lattner4c1e9542009-03-06 06:45:05 +00001308 {
1309 // If the pass crashes, remember this.
1310 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001311 TimeRegion PassTimer(getPassTimer(BP));
1312
Dan Gohman74b189f2010-03-01 17:34:28 +00001313 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001314 }
Devang Patel93a197c2006-12-14 00:08:04 +00001315
Dan Gohman74b189f2010-03-01 17:34:28 +00001316 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001317 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001318 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001319 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001320 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001321
Devang Patela273d1c2007-07-19 18:02:32 +00001322 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001323 removeNotPreservedAnalysis(BP);
1324 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001325 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001326 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001327
Bill Wendling6ce6d262009-12-25 13:50:18 +00001328 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001329}
1330
Devang Patel475c4532006-12-08 00:59:05 +00001331// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001332bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001333 bool Changed = false;
1334
Chris Lattner4c1e9542009-03-06 06:45:05 +00001335 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1336 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001337
1338 return Changed;
1339}
1340
Duncan Sands51495602009-02-13 09:42:34 +00001341bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001342 bool Changed = false;
1343
Pedro Artigas41b98842012-12-05 17:12:22 +00001344 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001345 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001346
1347 return Changed;
1348}
1349
Duncan Sands51495602009-02-13 09:42:34 +00001350bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001351 bool Changed = false;
1352
Devang Patelabfbe3b2006-12-16 00:56:26 +00001353 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1354 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001355 Changed |= BP->doInitialization(F);
1356 }
1357
1358 return Changed;
1359}
1360
Duncan Sands51495602009-02-13 09:42:34 +00001361bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001362 bool Changed = false;
1363
Devang Patelabfbe3b2006-12-16 00:56:26 +00001364 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1365 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001366 Changed |= BP->doFinalization(F);
1367 }
1368
1369 return Changed;
1370}
1371
1372
Devang Patela1514cb2006-12-07 19:39:39 +00001373//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001374// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001375
Devang Patel4e12f862006-11-08 10:44:40 +00001376/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001377FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001378 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001379 // FPM is the top level manager.
1380 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001381
Dan Gohman565df952008-03-13 02:08:36 +00001382 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001383 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001384}
1385
Devang Patelb67904d2006-12-13 02:36:01 +00001386FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001387 delete FPM;
1388}
1389
Devang Patel4e12f862006-11-08 10:44:40 +00001390/// add - Add a pass to the queue of passes to run. This passes
1391/// ownership of the Pass to the PassManager. When the
1392/// PassManager_X is destroyed, the pass will be destroyed as well, so
1393/// there is no need to delete the pass. (TODO delete passes.)
1394/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001395void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001396 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001397}
1398
Devang Patel9f3083e2006-11-15 19:39:54 +00001399/// run - Execute all of the passes scheduled for execution. Keep
1400/// track of whether any of the passes modifies the function, and if
1401/// so, return true.
1402///
Devang Patelb67904d2006-12-13 02:36:01 +00001403bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001404 if (F.isMaterializable()) {
1405 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001406 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001407 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001408 }
Devang Patel272908d2006-12-08 22:57:48 +00001409 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001410}
1411
1412
Devang Patelff631ae2006-11-15 01:27:05 +00001413/// doInitialization - Run all of the initializers for the function passes.
1414///
Devang Patelb67904d2006-12-13 02:36:01 +00001415bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001416 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001417}
1418
Dan Gohmane6656eb2007-07-30 14:51:13 +00001419/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001420///
Devang Patelb67904d2006-12-13 02:36:01 +00001421bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001422 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001423}
1424
Devang Patela1514cb2006-12-07 19:39:39 +00001425//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001426// FunctionPassManagerImpl implementation
1427//
Duncan Sands51495602009-02-13 09:42:34 +00001428bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001429 bool Changed = false;
1430
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001431 dumpArguments();
1432 dumpPasses();
1433
Pedro Artigas41b98842012-12-05 17:12:22 +00001434 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1435 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1436 E = IPV.end(); I != E; ++I) {
1437 Changed |= (*I)->doInitialization(M);
1438 }
1439
Chris Lattner4c1e9542009-03-06 06:45:05 +00001440 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1441 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442
1443 return Changed;
1444}
1445
Duncan Sands51495602009-02-13 09:42:34 +00001446bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001447 bool Changed = false;
1448
Pedro Artigas41b98842012-12-05 17:12:22 +00001449 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001450 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001451
Pedro Artigas41b98842012-12-05 17:12:22 +00001452 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1453 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1454 E = IPV.end(); I != E; ++I) {
1455 Changed |= (*I)->doFinalization(M);
1456 }
1457
Devang Patel67d6a5e2006-12-19 19:46:59 +00001458 return Changed;
1459}
1460
Devang Patelec9c58f2009-04-01 22:34:41 +00001461/// cleanup - After running all passes, clean up pass manager cache.
1462void FPPassManager::cleanup() {
1463 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1464 FunctionPass *FP = getContainedPass(Index);
1465 AnalysisResolver *AR = FP->getResolver();
1466 assert(AR && "Analysis Resolver is not set");
1467 AR->clearAnalysisImpls();
1468 }
1469}
1470
Torok Edwin24c78352009-06-29 18:49:09 +00001471void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1472 if (!wasRun)
1473 return;
1474 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1475 FPPassManager *FPPM = getContainedManager(Index);
1476 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1477 FPPM->getContainedPass(Index)->releaseMemory();
1478 }
1479 }
Torok Edwin896556e2009-06-29 21:05:10 +00001480 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001481}
1482
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483// Execute all the passes managed by this top level manager.
1484// Return true if any function is modified by a pass.
1485bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001486 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001487 TimingInfo::createTheTimeInfo();
1488
Devang Patele3068402006-12-21 00:16:50 +00001489 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001490 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1491 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001492
1493 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1494 getContainedManager(Index)->cleanup();
1495
Torok Edwin24c78352009-06-29 18:49:09 +00001496 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001497 return Changed;
1498}
1499
1500//===----------------------------------------------------------------------===//
1501// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001502
Devang Patel8c78a0b2007-05-03 01:11:54 +00001503char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001504/// Print passes managed by this manager
1505void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001506 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001507 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1508 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001509 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001510 dumpLastUses(FP, Offset+1);
1511 }
1512}
1513
1514
Dan Gohmande6188a2010-08-12 23:50:08 +00001515/// Execute all of the passes scheduled for execution by invoking
1516/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001517/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001518bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001519 if (F.isDeclaration())
1520 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001521
1522 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001523
Devang Patelcbbf2912008-03-20 01:09:53 +00001524 // Collect inherited analysis from Module level pass manager.
1525 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001526
Devang Patelabfbe3b2006-12-16 00:56:26 +00001527 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1528 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001529 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001530
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001531 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001532 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001533
Devang Patelabfbe3b2006-12-16 00:56:26 +00001534 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001535
Chris Lattner4c1e9542009-03-06 06:45:05 +00001536 {
1537 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001538 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001539
Dan Gohman74b189f2010-03-01 17:34:28 +00001540 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001541 }
Devang Patel93a197c2006-12-14 00:08:04 +00001542
Dan Gohman74b189f2010-03-01 17:34:28 +00001543 Changed |= LocalChanged;
1544 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001545 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001546 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001547
Devang Patela273d1c2007-07-19 18:02:32 +00001548 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001549 removeNotPreservedAnalysis(FP);
1550 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001551 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001552 }
1553 return Changed;
1554}
1555
Devang Patel67d6a5e2006-12-19 19:46:59 +00001556bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001557 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558
Dan Gohmane7630be2010-05-11 20:30:00 +00001559 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001560 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001561
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001562 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001563}
1564
Duncan Sands51495602009-02-13 09:42:34 +00001565bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001566 bool Changed = false;
1567
Chris Lattner4c1e9542009-03-06 06:45:05 +00001568 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1569 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001570
Devang Patelff631ae2006-11-15 01:27:05 +00001571 return Changed;
1572}
1573
Duncan Sands51495602009-02-13 09:42:34 +00001574bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001575 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001576
1577 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001578 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001579
Devang Patelff631ae2006-11-15 01:27:05 +00001580 return Changed;
1581}
1582
Devang Patela1514cb2006-12-07 19:39:39 +00001583//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001584// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001585
Dan Gohmande6188a2010-08-12 23:50:08 +00001586/// Execute all of the passes scheduled for execution by invoking
1587/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001588/// the module, and if so, return true.
1589bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001590MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001591 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001592
Torok Edwin24c78352009-06-29 18:49:09 +00001593 // Initialize on-the-fly passes
1594 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1595 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1596 I != E; ++I) {
1597 FunctionPassManagerImpl *FPP = I->second;
1598 Changed |= FPP->doInitialization(M);
1599 }
1600
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001601 // Initialize module passes
1602 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1603 Changed |= getContainedPass(Index)->doInitialization(M);
1604
Devang Patelabfbe3b2006-12-16 00:56:26 +00001605 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1606 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001607 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001608
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001609 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001610 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001611
Devang Patelabfbe3b2006-12-16 00:56:26 +00001612 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001613
Chris Lattner4c1e9542009-03-06 06:45:05 +00001614 {
1615 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001616 TimeRegion PassTimer(getPassTimer(MP));
1617
Dan Gohman74b189f2010-03-01 17:34:28 +00001618 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001619 }
Devang Patel93a197c2006-12-14 00:08:04 +00001620
Dan Gohman74b189f2010-03-01 17:34:28 +00001621 Changed |= LocalChanged;
1622 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001623 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001624 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001625 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001626
Devang Patela273d1c2007-07-19 18:02:32 +00001627 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001628 removeNotPreservedAnalysis(MP);
1629 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001630 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001631 }
Torok Edwin24c78352009-06-29 18:49:09 +00001632
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001633 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001634 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001635 Changed |= getContainedPass(Index)->doFinalization(M);
1636
Torok Edwin24c78352009-06-29 18:49:09 +00001637 // Finalize on-the-fly passes
1638 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1639 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1640 I != E; ++I) {
1641 FunctionPassManagerImpl *FPP = I->second;
1642 // We don't know when is the last time an on-the-fly pass is run,
1643 // so we need to releaseMemory / finalize here
1644 FPP->releaseMemoryOnTheFly();
1645 Changed |= FPP->doFinalization(M);
1646 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001647
Devang Patel05e1a972006-11-07 22:03:15 +00001648 return Changed;
1649}
1650
Devang Patele64d3052007-04-16 20:12:57 +00001651/// Add RequiredPass into list of lower level passes required by pass P.
1652/// RequiredPass is run on the fly by Pass Manager when P requests it
1653/// through getAnalysis interface.
1654void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001655 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1656 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001657 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001658 RequiredPass->getPotentialPassManagerType()) &&
1659 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001660
Devang Patel68f72b12007-04-26 17:50:19 +00001661 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001662 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001663 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001664 // FPP is the top level manager.
1665 FPP->setTopLevelManager(FPP);
1666
Devang Patel69e9f6d2007-04-16 20:27:05 +00001667 OnTheFlyManagers[P] = FPP;
1668 }
Devang Patel68f72b12007-04-26 17:50:19 +00001669 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001670
Devang Patel68f72b12007-04-26 17:50:19 +00001671 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001672 if (RequiredPass) {
1673 SmallVector<Pass *, 1> LU;
1674 LU.push_back(RequiredPass);
1675 FPP->setLastUser(LU, P);
1676 }
Devang Patele64d3052007-04-16 20:12:57 +00001677}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001678
Dan Gohmande6188a2010-08-12 23:50:08 +00001679/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001680/// required by module pass MP. Instantiate analysis pass, by using
1681/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001682Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001683 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001684 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001685
Torok Edwin24c78352009-06-29 18:49:09 +00001686 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001687 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001688 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001689}
1690
1691
Devang Patela1514cb2006-12-07 19:39:39 +00001692//===----------------------------------------------------------------------===//
1693// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001694
Devang Patelab97cf42006-12-13 00:09:23 +00001695//
Devang Patelc290c8a2006-11-07 22:23:34 +00001696/// run - Execute all of the passes scheduled for execution. Keep track of
1697/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001698bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001699 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001700 TimingInfo::createTheTimeInfo();
1701
Devang Patelcfd70c42006-12-13 22:10:00 +00001702 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001703 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001704
Pedro Artigas41b98842012-12-05 17:12:22 +00001705 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1706 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1707 E = IPV.end(); I != E; ++I) {
1708 Changed |= (*I)->doInitialization(M);
1709 }
1710
Devang Patele3068402006-12-21 00:16:50 +00001711 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001712 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1713 Changed |= getContainedManager(Index)->runOnModule(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001714
1715 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1716 E = IPV.end(); I != E; ++I) {
1717 Changed |= (*I)->doFinalization(M);
1718 }
1719
Devang Patelc290c8a2006-11-07 22:23:34 +00001720 return Changed;
1721}
Devang Patel376fefa2006-11-08 10:29:57 +00001722
Devang Patela1514cb2006-12-07 19:39:39 +00001723//===----------------------------------------------------------------------===//
1724// PassManager implementation
1725
Devang Patel376fefa2006-11-08 10:29:57 +00001726/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001727PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001728 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001729 // PM is the top level manager
1730 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001731}
1732
Devang Patelb67904d2006-12-13 02:36:01 +00001733PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001734 delete PM;
1735}
1736
Devang Patel376fefa2006-11-08 10:29:57 +00001737/// add - Add a pass to the queue of passes to run. This passes ownership of
1738/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1739/// will be destroyed as well, so there is no need to delete the pass. This
1740/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001741void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001742 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001743}
1744
1745/// run - Execute all of the passes scheduled for execution. Keep track of
1746/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001747bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001748 return PM->run(M);
1749}
1750
Devang Patelb8817b92006-12-14 00:59:42 +00001751//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001752// TimingInfo implementation
1753
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001754bool llvm::TimePassesIsEnabled = false;
1755static cl::opt<bool,true>
1756EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1757 cl::desc("Time each pass, printing elapsed time for each on exit"));
1758
Devang Patelb8817b92006-12-14 00:59:42 +00001759// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001760// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001761// null. It may be called multiple times.
1762void TimingInfo::createTheTimeInfo() {
1763 if (!TimePassesIsEnabled || TheTimeInfo) return;
1764
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001765 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001766 // This guarantees that the object will be constructed before static globals,
1767 // thus it will be destroyed before them.
1768 static ManagedStatic<TimingInfo> TTI;
1769 TheTimeInfo = &*TTI;
1770}
1771
Devang Patel1c3633e2007-01-29 23:10:37 +00001772/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001773Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001774 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001775 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001776 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001777}
1778
Devang Patel1c56a632007-01-08 19:29:38 +00001779//===----------------------------------------------------------------------===//
1780// PMStack implementation
1781//
Devang Patelad98d232007-01-11 22:15:30 +00001782
Devang Patel1c56a632007-01-08 19:29:38 +00001783// Pop Pass Manager from the stack and clear its analysis info.
1784void PMStack::pop() {
1785
1786 PMDataManager *Top = this->top();
1787 Top->initializeAnalysisInfo();
1788
1789 S.pop_back();
1790}
1791
1792// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001793void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001794 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001795 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001796
Chris Lattner60987362009-03-06 05:53:14 +00001797 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001798 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1799 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001800 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001801
Chris Lattner60987362009-03-06 05:53:14 +00001802 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001803 TPM->addIndirectPassManager(PM);
1804 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001805 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001806 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001807 assert((PM->getPassManagerType() == PMT_ModulePassManager
1808 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001809 && "pushing bad pass manager to PMStack");
1810 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001811 }
1812
Devang Patel15701b52007-01-11 00:19:00 +00001813 S.push_back(PM);
1814}
1815
1816// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001817void PMStack::dump() const {
1818 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001819 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001820 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001821
Devang Patel15701b52007-01-11 00:19:00 +00001822 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001823 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001824}
1825
Devang Patel1c56a632007-01-08 19:29:38 +00001826/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001827/// add self into that manager.
1828void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001829 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001830 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001831 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001832 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1833 if (TopPMType == PreferredType)
1834 break; // We found desired pass manager
1835 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001836 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001837 else
1838 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001839 }
Devang Patel18ff6362008-09-09 21:38:40 +00001840 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001841 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001842}
1843
Devang Patel3312f752007-01-16 21:43:18 +00001844/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001845/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001846void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001847 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001848
Andrew Trickcbc845f2012-02-01 07:16:20 +00001849 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001850 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001851 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1852 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001853 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001854 break;
Devang Patel3312f752007-01-16 21:43:18 +00001855 }
Devang Patel3312f752007-01-16 21:43:18 +00001856
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001857 // Create new Function Pass Manager if needed.
1858 FPPassManager *FPP;
1859 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1860 FPP = (FPPassManager *)PMS.top();
1861 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001862 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1863 PMDataManager *PMD = PMS.top();
1864
1865 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001866 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001867 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001868
1869 // [2] Set up new manager's top level manager
1870 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1871 TPM->addIndirectPassManager(FPP);
1872
1873 // [3] Assign manager to manage this new manager. This may create
1874 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001875 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001876
1877 // [4] Push new manager into PMS
1878 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001879 }
1880
Devang Patel3312f752007-01-16 21:43:18 +00001881 // Assign FPP as the manager of this pass.
1882 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001883}
1884
Devang Patel3312f752007-01-16 21:43:18 +00001885/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001886/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001887void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001888 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001889 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001890
Devang Patel15701b52007-01-11 00:19:00 +00001891 // Basic Pass Manager is a leaf pass manager. It does not handle
1892 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001893 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001894 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1895 BBP = (BBPassManager *)PMS.top();
1896 } else {
1897 // If leaf manager is not Basic Block Pass manager then create new
1898 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001899 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1900 PMDataManager *PMD = PMS.top();
1901
1902 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001903 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001904
1905 // [2] Set up new manager's top level manager
1906 // Basic Block Pass Manager does not live by itself
1907 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1908 TPM->addIndirectPassManager(BBP);
1909
Devang Patel15701b52007-01-11 00:19:00 +00001910 // [3] Assign manager to manage this new manager. This may create
1911 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001912 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001913
Devang Patel3312f752007-01-16 21:43:18 +00001914 // [4] Push new manager into PMS
1915 PMS.push(BBP);
1916 }
Devang Patel1c56a632007-01-08 19:29:38 +00001917
Devang Patel3312f752007-01-16 21:43:18 +00001918 // Assign BBP as the manager of this pass.
1919 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001920}
1921
Dan Gohmand3a20c92008-03-11 16:41:42 +00001922PassManagerBase::~PassManagerBase() {}