blob: a0ed6cab38719efd579cee3a5b6ff6eca543a572 [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 Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/IR/Module.h"
Devang Patelf1567a52006-12-13 20:03:48 +000019#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000020#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000022#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000023#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/PassNameParser.h"
25#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.
167 bool runOnFunction(Function &F);
168
Devang Patelf9d96b92006-12-07 19:57:52 +0000169 /// Pass Manager itself does not invalidate any analysis info.
170 void getAnalysisUsage(AnalysisUsage &Info) const {
171 Info.setPreservesAll();
172 }
173
Devang Patel475c4532006-12-08 00:59:05 +0000174 bool doInitialization(Module &M);
175 bool doInitialization(Function &F);
176 bool doFinalization(Module &M);
177 bool doFinalization(Function &F);
178
Chris Lattner2fa26e52010-01-22 05:24:46 +0000179 virtual PMDataManager *getAsPMDataManager() { return this; }
180 virtual Pass *getAsPass() { return this; }
181
Devang Patele3858e62007-02-01 22:08:25 +0000182 virtual const char *getPassName() const {
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
187 void dumpPassStructure(unsigned Offset) {
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
Dan Gohmande6188a2010-08-12 23:50:08 +0000202 virtual PassManagerType getPassManagerType() const {
203 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.
David Greene9b063df2010-04-02 23:17:14 +0000237 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000238 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000239 }
240
Torok Edwin24c78352009-06-29 18:49:09 +0000241 // Prepare for running an on the fly pass, freeing memory if needed
242 // from a previous run.
243 void releaseMemoryOnTheFly();
244
Devang Patel67d6a5e2006-12-19 19:46:59 +0000245 /// run - Execute all of the passes scheduled for execution. Keep track of
246 /// whether any of the passes modifies the module, and if so, return true.
247 bool run(Function &F);
248
249 /// doInitialization - Run all of the initializers for the function passes.
250 ///
251 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000252
Dan Gohmane6656eb2007-07-30 14:51:13 +0000253 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000254 ///
255 bool doFinalization(Module &M);
256
Dan Gohmande6188a2010-08-12 23:50:08 +0000257
Chris Lattner2fa26e52010-01-22 05:24:46 +0000258 virtual PMDataManager *getAsPMDataManager() { return this; }
259 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000260 virtual PassManagerType getTopLevelPassManagerType() {
261 return PMT_FunctionPassManager;
262 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000263
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264 /// Pass Manager itself does not invalidate any analysis info.
265 void getAnalysisUsage(AnalysisUsage &Info) const {
266 Info.setPreservesAll();
267 }
268
Devang Patel67d6a5e2006-12-19 19:46:59 +0000269 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000270 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000271 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
272 return FP;
273 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274};
275
David Blaikiea379b1812011-12-20 02:50:00 +0000276void FunctionPassManagerImpl::anchor() {}
277
Devang Patel8c78a0b2007-05-03 01:11:54 +0000278char FunctionPassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000279} // End of legacy namespace
280} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000281
Chandler Carruth7caea412013-11-09 12:26:54 +0000282namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283//===----------------------------------------------------------------------===//
284// MPPassManager
285//
286/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000287/// It batches all Module passes and function pass managers together and
288/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000289class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000290public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000291 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000292 explicit MPPassManager() :
293 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000294
295 // Delete on the fly managers.
296 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000297 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000298 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
299 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000300 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000301 delete FPP;
302 }
303 }
304
Dan Gohmande6188a2010-08-12 23:50:08 +0000305 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000306 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000307 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000308 }
309
Devang Patelca58e352006-11-08 10:05:38 +0000310 /// run - Execute all of the passes scheduled for execution. Keep track of
311 /// whether any of the passes modifies the module, and if so, return true.
312 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000313
Pedro Artigase4348b02012-12-03 21:56:57 +0000314 using llvm::Pass::doInitialization;
315 using llvm::Pass::doFinalization;
316
Owen Anderson1aa27512012-11-15 00:14:15 +0000317 /// doInitialization - Run all of the initializers for the module passes.
318 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000319 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000320
321 /// doFinalization - Run all of the finalizers for the module passes.
322 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000323 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000324
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 /// Pass Manager itself does not invalidate any analysis info.
326 void getAnalysisUsage(AnalysisUsage &Info) const {
327 Info.setPreservesAll();
328 }
329
Devang Patele64d3052007-04-16 20:12:57 +0000330 /// Add RequiredPass into list of lower level passes required by pass P.
331 /// RequiredPass is run on the fly by Pass Manager when P requests it
332 /// through getAnalysis interface.
333 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
334
Dan Gohmande6188a2010-08-12 23:50:08 +0000335 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000336 /// required by module pass MP. Instantiate analysis pass, by using
337 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000338 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000339
Devang Patele3858e62007-02-01 22:08:25 +0000340 virtual const char *getPassName() const {
341 return "Module Pass Manager";
342 }
343
Chris Lattner2fa26e52010-01-22 05:24:46 +0000344 virtual PMDataManager *getAsPMDataManager() { return this; }
345 virtual Pass *getAsPass() { return this; }
346
Devang Pateleda56172006-12-12 23:34:33 +0000347 // Print passes managed by this manager
348 void dumpPassStructure(unsigned Offset) {
Eric Christophera13839f2014-02-26 23:27:16 +0000349 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
351 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000352 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000353 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
354 OnTheFlyManagers.find(MP);
355 if (I != OnTheFlyManagers.end())
356 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000357 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000358 }
359 }
360
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000362 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000363 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000364 }
365
Dan Gohmande6188a2010-08-12 23:50:08 +0000366 virtual PassManagerType getPassManagerType() const {
367 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000368 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000369
370 private:
371 /// Collection of on the fly FPPassManagers. These managers manage
372 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000373 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000374};
375
Devang Patel8c78a0b2007-05-03 01:11:54 +0000376char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000377} // End anonymous namespace
378
379namespace llvm {
380namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000381//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000383//
Devang Patel09f162c2007-05-01 21:15:47 +0000384
Devang Patel67d6a5e2006-12-19 19:46:59 +0000385/// PassManagerImpl manages MPPassManagers
386class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000387 public PMDataManager,
388 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000389 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000390
391public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000392 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000393 explicit PassManagerImpl() :
394 Pass(PT_PassManager, ID), PMDataManager(),
395 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000396
Devang Patel376fefa2006-11-08 10:29:57 +0000397 /// add - Add a pass to the queue of passes to run. This passes ownership of
398 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
399 /// will be destroyed as well, so there is no need to delete the pass. This
400 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000401 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000402 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000403 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000404
405 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000406 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000407 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000408 }
409
Devang Patel376fefa2006-11-08 10:29:57 +0000410 /// run - Execute all of the passes scheduled for execution. Keep track of
411 /// whether any of the passes modifies the module, and if so, return true.
412 bool run(Module &M);
413
Pedro Artigase4348b02012-12-03 21:56:57 +0000414 using llvm::Pass::doInitialization;
415 using llvm::Pass::doFinalization;
416
Owen Anderson1aa27512012-11-15 00:14:15 +0000417 /// doInitialization - Run all of the initializers for the module passes.
418 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000419 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000420
421 /// doFinalization - Run all of the finalizers for the module passes.
422 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000423 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000424
Devang Patelf9d96b92006-12-07 19:57:52 +0000425 /// Pass Manager itself does not invalidate any analysis info.
426 void getAnalysisUsage(AnalysisUsage &Info) const {
427 Info.setPreservesAll();
428 }
429
Chris Lattner2fa26e52010-01-22 05:24:46 +0000430 virtual PMDataManager *getAsPMDataManager() { return this; }
431 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000432 virtual PassManagerType getTopLevelPassManagerType() {
433 return PMT_ModulePassManager;
434 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000435
Devang Patel67d6a5e2006-12-19 19:46:59 +0000436 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000437 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000438 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
439 return MP;
440 }
Devang Patel376fefa2006-11-08 10:29:57 +0000441};
442
David Blaikiea379b1812011-12-20 02:50:00 +0000443void PassManagerImpl::anchor() {}
444
Devang Patel8c78a0b2007-05-03 01:11:54 +0000445char PassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000446} // End of legacy namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000447} // End of llvm namespace
448
449namespace {
450
451//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000452/// TimingInfo Class - This class is used to calculate information about the
453/// amount of time each pass takes to execute. This only happens when
454/// -time-passes is enabled on the command line.
455///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000456
Owen Anderson5a6960f2009-06-18 20:51:00 +0000457static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000458
Nick Lewycky02d5f772009-10-25 06:33:48 +0000459class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000460 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000461 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000462public:
463 // Use 'create' member to get this.
464 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000465
Devang Patel1c3633e2007-01-29 23:10:37 +0000466 // TimingDtor - Print out information about timing information
467 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000468 // Delete all of the timers, which accumulate their info into the
469 // TimerGroup.
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000470 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattner707431c2010-03-30 04:03:22 +0000471 E = TimingData.end(); I != E; ++I)
472 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000473 // TimerGroup is deleted next, printing the report.
474 }
475
476 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000477 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000478 // null. It may be called multiple times.
479 static void createTheTimeInfo();
480
Chris Lattner707431c2010-03-30 04:03:22 +0000481 /// getPassTimer - Return the timer for the specified pass if it exists.
482 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000483 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000484 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000485
Owen Anderson5c96ef72009-07-07 18:33:04 +0000486 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000487 Timer *&T = TimingData[P];
Chris Lattner707431c2010-03-30 04:03:22 +0000488 if (T == 0)
489 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000490 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000491 }
492};
493
Devang Patel1c3633e2007-01-29 23:10:37 +0000494} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000495
Dan Gohmand78c4002008-05-13 00:00:25 +0000496static TimingInfo *TheTimeInfo;
497
Devang Patela1514cb2006-12-07 19:39:39 +0000498//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000499// PMTopLevelManager implementation
500
Devang Patel4268fc02007-01-16 02:00:38 +0000501/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000502PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
503 PMDM->setTopLevelManager(this);
504 addPassManager(PMDM);
505 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000506}
507
Devang Patelafb1f3622006-12-12 22:35:25 +0000508/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000509void
Bill Wendlingea857e12012-05-14 07:53:40 +0000510PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000511 unsigned PDepth = 0;
512 if (P->getResolver())
513 PDepth = P->getResolver()->getPMDataManager().getDepth();
514
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000515 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000516 E = AnalysisPasses.end(); I != E; ++I) {
517 Pass *AP = *I;
518 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000519
Devang Patel01919d22007-03-08 19:05:01 +0000520 if (P == AP)
521 continue;
522
Tobias Grosserf07426b2011-01-20 21:03:22 +0000523 // Update the last users of passes that are required transitive by AP.
524 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
525 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
526 SmallVector<Pass *, 12> LastUses;
527 SmallVector<Pass *, 12> LastPMUses;
528 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
529 E = IDs.end(); I != E; ++I) {
530 Pass *AnalysisPass = findAnalysisPass(*I);
531 assert(AnalysisPass && "Expected analysis pass to exist.");
532 AnalysisResolver *AR = AnalysisPass->getResolver();
533 assert(AR && "Expected analysis resolver to exist.");
534 unsigned APDepth = AR->getPMDataManager().getDepth();
535
536 if (PDepth == APDepth)
537 LastUses.push_back(AnalysisPass);
538 else if (PDepth > APDepth)
539 LastPMUses.push_back(AnalysisPass);
540 }
541
542 setLastUser(LastUses, P);
543
544 // If this pass has a corresponding pass manager, push higher level
545 // analysis to this pass manager.
546 if (P->getResolver())
547 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
548
549
Devang Patelafb1f3622006-12-12 22:35:25 +0000550 // If AP is the last user of other passes then make P last user of
551 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000552 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000553 LUE = LastUser.end(); LUI != LUE; ++LUI) {
554 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000555 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000556 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000557 LastUser[LUI->first] = P;
558 }
559 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000560}
561
562/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000563void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000564 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000565 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000566 InversedLastUser.find(P);
567 if (DMI == InversedLastUser.end())
568 return;
569
570 SmallPtrSet<Pass *, 8> &LU = DMI->second;
571 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
572 E = LU.end(); I != E; ++I) {
573 LastUses.push_back(*I);
574 }
575
Devang Patelafb1f3622006-12-12 22:35:25 +0000576}
577
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000578AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
579 AnalysisUsage *AnUsage = NULL;
580 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000581 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000582 AnUsage = DMI->second;
583 else {
584 AnUsage = new AnalysisUsage();
585 P->getAnalysisUsage(*AnUsage);
586 AnUsageMap[P] = AnUsage;
587 }
588 return AnUsage;
589}
590
Devang Patelafb1f3622006-12-12 22:35:25 +0000591/// Schedule pass P for execution. Make sure that passes required by
592/// P are run before P is run. Update analysis info maintained by
593/// the manager. Remove dead passes. This is a recursive function.
594void PMTopLevelManager::schedulePass(Pass *P) {
595
Devang Patel3312f752007-01-16 21:43:18 +0000596 // TODO : Allocate function manager for this pass, other wise required set
597 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000598
Devang Pateld74ede72007-03-06 01:06:16 +0000599 // Give pass a chance to prepare the stage.
600 P->preparePassManager(activeStack);
601
Devang Patel864970e2008-03-18 00:39:19 +0000602 // If P is an analysis pass and it is available then do not
603 // generate the analysis again. Stale analysis info should not be
604 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000605 const PassInfo *PI =
606 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
607 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000608 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000609 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000610 }
Devang Patel864970e2008-03-18 00:39:19 +0000611
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000612 AnalysisUsage *AnUsage = findAnalysisUsage(P);
613
Devang Patelfdee7032008-08-14 23:07:48 +0000614 bool checkAnalysis = true;
615 while (checkAnalysis) {
616 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000617
Devang Patelfdee7032008-08-14 23:07:48 +0000618 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
619 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
620 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000621
Devang Patelfdee7032008-08-14 23:07:48 +0000622 Pass *AnalysisPass = findAnalysisPass(*I);
623 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000624 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000625
626 if (PI == NULL) {
627 // Pass P is not in the global PassRegistry
628 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
629 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
630 dbgs() << "Required Passes:" << "\n";
631 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
632 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
633 Pass *AnalysisPass2 = findAnalysisPass(*I2);
634 if (AnalysisPass2) {
635 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000636 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000637 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
638 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
639 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
640 }
641 }
642 }
643
Andrew Trick6bbaf132011-06-03 00:48:58 +0000644 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000645 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000646 if (P->getPotentialPassManagerType () ==
647 AnalysisPass->getPotentialPassManagerType())
648 // Schedule analysis pass that is managed by the same pass manager.
649 schedulePass(AnalysisPass);
650 else if (P->getPotentialPassManagerType () >
651 AnalysisPass->getPotentialPassManagerType()) {
652 // Schedule analysis pass that is managed by a new manager.
653 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000654 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000655 // are already checked are still available.
656 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000657 } else
Dan Gohmande6188a2010-08-12 23:50:08 +0000658 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000659 // passes are run on the fly.
660 delete AnalysisPass;
661 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000662 }
663 }
664
665 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000666 if (ImmutablePass *IP = P->getAsImmutablePass()) {
667 // P is a immutable pass and it will be managed by this
668 // top level manager. Set up analysis resolver to connect them.
669 PMDataManager *DM = getAsPMDataManager();
670 AnalysisResolver *AR = new AnalysisResolver(*DM);
671 P->setResolver(AR);
672 DM->initializeAnalysisImpl(P);
673 addImmutablePass(IP);
674 DM->recordAvailableAnalysis(IP);
675 return;
676 }
677
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000678 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000679 Pass *PP = P->createPrinterPass(
680 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
681 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
682 }
683
684 // Add the requested pass to the best available pass manager.
685 P->assignPassManager(activeStack, getTopLevelPassManagerType());
686
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000687 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000688 Pass *PP = P->createPrinterPass(
689 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
690 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
691 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000692}
693
694/// Find the pass that implements Analysis AID. Search immutable
695/// passes and all pass managers. If desired pass is not found
696/// then return NULL.
697Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
698
Devang Patelcd6ba152006-12-12 22:50:05 +0000699 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000700 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000701 E = PassManagers.end(); I != E; ++I)
702 if (Pass *P = (*I)->findAnalysisPass(AID, false))
703 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000704
705 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000706 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000707 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000708 E = IndirectPassManagers.end(); I != E; ++I)
709 if (Pass *P = (*I)->findAnalysisPass(AID, false))
710 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000711
Dan Gohman844dd0a2010-10-11 23:19:01 +0000712 // Check the immutable passes. Iterate in reverse order so that we find
713 // the most recently registered passes first.
Craig Topper31ee5862013-07-03 15:07:05 +0000714 for (SmallVectorImpl<ImmutablePass *>::reverse_iterator I =
Dan Gohman844dd0a2010-10-11 23:19:01 +0000715 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000716 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000717 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000718 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000719
720 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000721 const PassInfo *PassInf =
722 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000723 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000724 const std::vector<const PassInfo*> &ImmPI =
725 PassInf->getInterfacesImplemented();
726 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
727 EE = ImmPI.end(); II != EE; ++II) {
728 if ((*II)->getTypeInfo() == AID)
729 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000730 }
731 }
732
Dan Gohman844dd0a2010-10-11 23:19:01 +0000733 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000734}
735
Devang Pateleda56172006-12-12 23:34:33 +0000736// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000737void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000738
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000739 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000740 return;
741
Devang Pateleda56172006-12-12 23:34:33 +0000742 // Print out the immutable passes
743 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000744 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000745 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000746
Dan Gohmanf71c5212010-08-19 01:29:07 +0000747 // Every class that derives from PMDataManager also derives from Pass
748 // (sometimes indirectly), but there's no inheritance relationship
749 // between PMDataManager and Pass, so we have to getAsPass to get
750 // from a PMDataManager* to a Pass*.
Craig Topper31ee5862013-07-03 15:07:05 +0000751 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
752 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000753 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000754}
755
Devang Patel991aeba2006-12-15 20:13:01 +0000756void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000757
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000758 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000759 return;
760
David Greene994e1bb2010-01-05 01:30:02 +0000761 dbgs() << "Pass Arguments: ";
Craig Topper31ee5862013-07-03 15:07:05 +0000762 for (SmallVectorImpl<ImmutablePass *>::const_iterator I =
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000763 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
764 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000765 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
766 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000767 if (!PI->isAnalysisGroup())
768 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000769 }
Craig Topper31ee5862013-07-03 15:07:05 +0000770 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
771 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000772 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000773 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000774}
775
Devang Patele3068402006-12-21 00:16:50 +0000776void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000777 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000778 E = PassManagers.end(); I != E; ++I)
779 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000780
Devang Patele3068402006-12-21 00:16:50 +0000781 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000782 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000783 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
784 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000785 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000786
Chris Lattner60987362009-03-06 05:53:14 +0000787 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000788 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000789 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000790 InversedLastUser.find(DMI->second);
791 if (InvDMI != InversedLastUser.end()) {
792 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
793 L.insert(DMI->first);
794 } else {
795 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
796 InversedLastUser[DMI->second] = L;
797 }
798 }
Devang Patele3068402006-12-21 00:16:50 +0000799}
800
Devang Patele7599552007-01-12 18:52:44 +0000801/// Destructor
802PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000803 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000804 E = PassManagers.end(); I != E; ++I)
805 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000806
Dan Gohman060d5ba2010-10-12 00:15:27 +0000807 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000808 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
809 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000810
811 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000812 DME = AnUsageMap.end(); DMI != DME; ++DMI)
813 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000814}
815
Devang Patelafb1f3622006-12-12 22:35:25 +0000816//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000817// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000818
Devang Patel643676c2006-11-11 01:10:19 +0000819/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000820void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000821 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000822
Chris Lattner60987362009-03-06 05:53:14 +0000823 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000824
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000825 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000826
Dan Gohmande6188a2010-08-12 23:50:08 +0000827 // This pass is the current implementation of all of the interfaces it
828 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000829 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
830 if (PInf == 0) return;
831 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000832 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000833 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000834}
835
Devang Patel9d9fc902007-03-06 17:52:53 +0000836// Return true if P preserves high level analysis used by other
837// passes managed by this manager
838bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000839 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000840 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000841 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000842
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000843 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000844 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000845 E = HigherLevelAnalysis.end(); I != E; ++I) {
846 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000847 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000848 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000849 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000850 PreservedSet.end())
851 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000852 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000853
Devang Patel9d9fc902007-03-06 17:52:53 +0000854 return true;
855}
856
Chris Lattner02eb94c2008-08-07 07:34:50 +0000857/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000858void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000859 // Don't do this unless assertions are enabled.
860#ifdef NDEBUG
861 return;
862#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000863 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
864 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000865
Devang Patelef432532007-07-19 05:36:09 +0000866 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000867 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000868 E = PreservedSet.end(); I != E; ++I) {
869 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000870 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000871 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000872 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000873 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000874 }
875}
876
Devang Patel67c79a42008-07-01 19:50:56 +0000877/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000878void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000879 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
880 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000881 return;
882
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000883 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000884 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000885 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000886 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000887 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000888 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000889 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000890 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000891 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000892 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000893 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
894 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000895 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000896 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000897 }
Devang Patel349170f2006-11-11 01:24:55 +0000898 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000899
Devang Patel42dd1e92007-03-06 01:55:46 +0000900 // Check inherited analysis also. If P is not preserving analysis
901 // provided by parent manager then remove it here.
902 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
903
904 if (!InheritedAnalysis[Index])
905 continue;
906
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000907 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000908 I = InheritedAnalysis[Index]->begin(),
909 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000910 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000911 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000912 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000913 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000914 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000915 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000916 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000917 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
918 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000919 }
Devang Patel01919d22007-03-08 19:05:01 +0000920 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000921 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000922 }
923 }
Devang Patelf68a3492006-11-07 22:35:17 +0000924}
925
Devang Patelca189262006-11-14 03:05:08 +0000926/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000927void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000928 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000929
Devang Patel8adae862007-07-20 18:04:54 +0000930 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000931
Devang Patel2ff44922007-04-16 20:39:59 +0000932 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000933 if (!TPM)
934 return;
935
Devang Patel17ad0962006-12-08 00:37:52 +0000936 TPM->collectLastUses(DeadPasses, P);
937
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000938 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000939 dbgs() << " -*- '" << P->getPassName();
940 dbgs() << "' is the last user of following pass instances.";
941 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000942 }
943
Dan Gohman060d5ba2010-10-12 00:15:27 +0000944 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000945 E = DeadPasses.end(); I != E; ++I)
946 freePass(*I, Msg, DBG_STR);
947}
Devang Patel200d3052006-12-13 23:50:44 +0000948
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000949void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000950 enum PassDebuggingString DBG_STR) {
951 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000952
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 {
954 // If the pass crashes releasing memory, remember this.
955 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000956 TimeRegion PassTimer(getPassTimer(P));
957
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000958 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000959 }
960
Owen Andersona7aed182010-08-06 18:33:48 +0000961 AnalysisID PI = P->getPassID();
962 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000963 // Remove the pass itself (if it is not already removed).
964 AvailableAnalysis.erase(PI);
965
966 // Remove all interfaces this pass implements, for which it is also
967 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000968 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000969 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000970 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000971 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000972 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000973 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000974 }
Devang Patel17ad0962006-12-08 00:37:52 +0000975 }
Devang Patelca189262006-11-14 03:05:08 +0000976}
977
Dan Gohmande6188a2010-08-12 23:50:08 +0000978/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000979/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000980void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000981 // This manager is going to manage pass P. Set up analysis resolver
982 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000983 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000984 P->setResolver(AR);
985
Devang Patelec2b9a72007-03-05 22:57:49 +0000986 // If a FunctionPass F is the last user of ModulePass info M
987 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000988 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000989
Chris Lattner60987362009-03-06 05:53:14 +0000990 if (!ProcessAnalysis) {
991 // Add pass
992 PassVector.push_back(P);
993 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000994 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000995
Chris Lattner60987362009-03-06 05:53:14 +0000996 // At the moment, this pass is the last user of all required passes.
997 SmallVector<Pass *, 12> LastUses;
998 SmallVector<Pass *, 8> RequiredPasses;
999 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1000
1001 unsigned PDepth = this->getDepth();
1002
Dan Gohmande6188a2010-08-12 23:50:08 +00001003 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +00001004 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +00001005 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001006 E = RequiredPasses.end(); I != E; ++I) {
1007 Pass *PRequired = *I;
1008 unsigned RDepth = 0;
1009
1010 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1011 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1012 RDepth = DM.getDepth();
1013
1014 if (PDepth == RDepth)
1015 LastUses.push_back(PRequired);
1016 else if (PDepth > RDepth) {
1017 // Let the parent claim responsibility of last use
1018 TransferLastUses.push_back(PRequired);
1019 // Keep track of higher level analysis used by this manager.
1020 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001021 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001022 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001023 }
1024
1025 // Set P as P's last user until someone starts using P.
1026 // However, if P is a Pass Manager then it does not need
1027 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001028 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001029 LastUses.push_back(P);
1030 TPM->setLastUser(LastUses, P);
1031
1032 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001033 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001034 TPM->setLastUser(TransferLastUses, My_PM);
1035 TransferLastUses.clear();
1036 }
1037
Dan Gohman6304db32010-08-16 22:57:28 +00001038 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001039 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001040 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001041 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001042 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1043 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001044 this->addLowerLevelRequiredPass(P, AnalysisPass);
1045 }
1046
1047 // Take a note of analysis required and made available by this pass.
1048 // Remove the analysis not preserved by this pass
1049 removeNotPreservedAnalysis(P);
1050 recordAvailableAnalysis(P);
1051
Devang Patel8cad70d2006-11-11 01:51:02 +00001052 // Add pass
1053 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001054}
1055
Devang Patele64d3052007-04-16 20:12:57 +00001056
1057/// Populate RP with analysis pass that are required by
1058/// pass P and are available. Populate RP_NotAvail with analysis
1059/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001060void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1061 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001062 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001063 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1064 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001065 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001066 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001067 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001068 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001069 else
Chris Lattner60987362009-03-06 05:53:14 +00001070 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001071 }
Devang Patelf58183d2006-12-12 23:09:32 +00001072
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001073 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001074 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001075 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001076 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001077 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001078 else
Chris Lattner60987362009-03-06 05:53:14 +00001079 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001080 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001081}
1082
Devang Patel07f4f582006-11-14 21:49:36 +00001083// All Required analyses should be available to the pass as it runs! Here
1084// we fill in the AnalysisImpls member of the pass so that it can
1085// successfully use the getAnalysis() method to retrieve the
1086// implementations it needs.
1087//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001088void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001089 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1090
Chris Lattnercbd160f2008-08-08 05:33:04 +00001091 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001092 I = AnUsage->getRequiredSet().begin(),
1093 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001094 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001095 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001096 // This may be analysis pass that is initialized on the fly.
1097 // If that is not the case then it will raise an assert when it is used.
1098 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001099 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001100 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001101 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001102 }
1103}
1104
Devang Patel640c5bb2006-12-08 22:30:11 +00001105/// Find the pass that implements Analysis AID. If desired pass is not found
1106/// then return NULL.
1107Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1108
1109 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001110 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001111
1112 if (I != AvailableAnalysis.end())
1113 return I->second;
1114
1115 // Search Parents through TopLevelManager
1116 if (SearchParent)
1117 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001118
Devang Patel9d759b82006-12-09 00:09:12 +00001119 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001120}
1121
Devang Patel991aeba2006-12-15 20:13:01 +00001122// Print list of passes that are last used by P.
1123void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1124
Devang Patel8adae862007-07-20 18:04:54 +00001125 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001126
1127 // If this is a on the fly manager then it does not have TPM.
1128 if (!TPM)
1129 return;
1130
Devang Patel991aeba2006-12-15 20:13:01 +00001131 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001132
Dan Gohman7224bce2010-10-12 00:11:18 +00001133 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001134 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001135 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001136 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001137 }
1138}
1139
1140void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001141 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001142 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001143 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001144 PMD->dumpPassArguments();
1145 else
Owen Andersona7aed182010-08-06 18:33:48 +00001146 if (const PassInfo *PI =
1147 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001148 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001150 }
1151}
1152
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001153void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1154 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001155 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001156 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001157 return;
David Greene994e1bb2010-01-05 01:30:02 +00001158 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001159 switch (S1) {
1160 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001161 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001162 break;
1163 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001164 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001165 break;
1166 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001167 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001168 break;
1169 default:
1170 break;
1171 }
1172 switch (S2) {
1173 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001174 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001175 break;
1176 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001177 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001178 break;
1179 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001180 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001181 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001182 case ON_REGION_MSG:
1183 dbgs() << "' on Region '" << Msg << "'...\n";
1184 break;
Devang Patel003a5592007-03-05 20:01:30 +00001185 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001186 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001187 break;
1188 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001189 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001190 break;
1191 default:
1192 break;
1193 }
Devang Patel991aeba2006-12-15 20:13:01 +00001194}
1195
Chris Lattner4c1e9542009-03-06 06:45:05 +00001196void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001197 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001198 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001199
Chris Lattner4c493d92008-08-08 15:14:09 +00001200 AnalysisUsage analysisUsage;
1201 P->getAnalysisUsage(analysisUsage);
1202 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1203}
1204
Chris Lattner4c1e9542009-03-06 06:45:05 +00001205void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001206 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001207 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001208
Chris Lattner4c493d92008-08-08 15:14:09 +00001209 AnalysisUsage analysisUsage;
1210 P->getAnalysisUsage(analysisUsage);
1211 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1212}
1213
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001214void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001215 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001216 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001217 if (Set.empty())
1218 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001219 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001220 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001221 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001222 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001223 if (!PInf) {
1224 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1225 // all drivers.
1226 dbgs() << " Uninitialized Pass";
1227 continue;
1228 }
Owen Andersona7aed182010-08-06 18:33:48 +00001229 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001230 }
David Greene994e1bb2010-01-05 01:30:02 +00001231 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001232}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001233
Devang Patel004937b2007-07-27 20:06:09 +00001234/// Add RequiredPass into list of lower level passes required by pass P.
1235/// RequiredPass is run on the fly by Pass Manager when P requests it
1236/// through getAnalysis interface.
1237/// This should be handled by specific pass manager.
1238void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1239 if (TPM) {
1240 TPM->dumpArguments();
1241 TPM->dumpPasses();
1242 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001243
Dan Gohmande6188a2010-08-12 23:50:08 +00001244 // Module Level pass may required Function Level analysis info
1245 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1246 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001247 // module level pass is requiring lower level analysis info managed by
1248 // lower level pass manager.
1249
1250 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001251 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001252 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001253#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001254 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1255 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001256#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001257 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001258}
1259
Owen Andersona7aed182010-08-06 18:33:48 +00001260Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001261 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001262}
1263
Devang Patele7599552007-01-12 18:52:44 +00001264// Destructor
1265PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001266 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001267 E = PassVector.end(); I != E; ++I)
1268 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001269}
1270
Devang Patel9bdf7d42006-12-08 23:28:54 +00001271//===----------------------------------------------------------------------===//
1272// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001273// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1274Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001275 return PM.findAnalysisPass(ID, dir);
1276}
1277
Dan Gohmande6188a2010-08-12 23:50:08 +00001278Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001279 Function &F) {
1280 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1281}
1282
Devang Patela1514cb2006-12-07 19:39:39 +00001283//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001284// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001285
Dan Gohmande6188a2010-08-12 23:50:08 +00001286/// Execute all of the passes scheduled for execution by invoking
1287/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001288/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001289bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001290 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001291 return false;
1292
Devang Patele9585592006-12-08 01:38:28 +00001293 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001294
Devang Patel6e5a1132006-11-07 21:31:57 +00001295 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001296 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1297 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001298 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001299
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001300 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001301 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001302
Devang Patelabfbe3b2006-12-16 00:56:26 +00001303 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001304
Chris Lattner4c1e9542009-03-06 06:45:05 +00001305 {
1306 // If the pass crashes, remember this.
1307 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001308 TimeRegion PassTimer(getPassTimer(BP));
1309
Dan Gohman74b189f2010-03-01 17:34:28 +00001310 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001311 }
Devang Patel93a197c2006-12-14 00:08:04 +00001312
Dan Gohman74b189f2010-03-01 17:34:28 +00001313 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001314 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001315 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001316 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001317 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001318
Devang Patela273d1c2007-07-19 18:02:32 +00001319 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001320 removeNotPreservedAnalysis(BP);
1321 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001322 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001323 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001324
Bill Wendling6ce6d262009-12-25 13:50:18 +00001325 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001326}
1327
Devang Patel475c4532006-12-08 00:59:05 +00001328// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001329bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001330 bool Changed = false;
1331
Chris Lattner4c1e9542009-03-06 06:45:05 +00001332 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1333 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001334
1335 return Changed;
1336}
1337
Duncan Sands51495602009-02-13 09:42:34 +00001338bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001339 bool Changed = false;
1340
Pedro Artigas41b98842012-12-05 17:12:22 +00001341 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001342 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001343
1344 return Changed;
1345}
1346
Duncan Sands51495602009-02-13 09:42:34 +00001347bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001348 bool Changed = false;
1349
Devang Patelabfbe3b2006-12-16 00:56:26 +00001350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1351 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001352 Changed |= BP->doInitialization(F);
1353 }
1354
1355 return Changed;
1356}
1357
Duncan Sands51495602009-02-13 09:42:34 +00001358bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001359 bool Changed = false;
1360
Devang Patelabfbe3b2006-12-16 00:56:26 +00001361 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1362 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001363 Changed |= BP->doFinalization(F);
1364 }
1365
1366 return Changed;
1367}
1368
1369
Devang Patela1514cb2006-12-07 19:39:39 +00001370//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001371// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001372
Devang Patel4e12f862006-11-08 10:44:40 +00001373/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001374FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001375 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001376 // FPM is the top level manager.
1377 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001378
Dan Gohman565df952008-03-13 02:08:36 +00001379 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001380 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001381}
1382
Devang Patelb67904d2006-12-13 02:36:01 +00001383FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001384 delete FPM;
1385}
1386
Devang Patel4e12f862006-11-08 10:44:40 +00001387/// add - Add a pass to the queue of passes to run. This passes
1388/// ownership of the Pass to the PassManager. When the
1389/// PassManager_X is destroyed, the pass will be destroyed as well, so
1390/// there is no need to delete the pass. (TODO delete passes.)
1391/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001392void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001393 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001394}
1395
Devang Patel9f3083e2006-11-15 19:39:54 +00001396/// run - Execute all of the passes scheduled for execution. Keep
1397/// track of whether any of the passes modifies the function, and if
1398/// so, return true.
1399///
Devang Patelb67904d2006-12-13 02:36:01 +00001400bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001401 if (F.isMaterializable()) {
1402 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001403 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001404 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001405 }
Devang Patel272908d2006-12-08 22:57:48 +00001406 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001407}
1408
1409
Devang Patelff631ae2006-11-15 01:27:05 +00001410/// doInitialization - Run all of the initializers for the function passes.
1411///
Devang Patelb67904d2006-12-13 02:36:01 +00001412bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001413 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001414}
1415
Dan Gohmane6656eb2007-07-30 14:51:13 +00001416/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001417///
Devang Patelb67904d2006-12-13 02:36:01 +00001418bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001419 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001420}
1421
Devang Patela1514cb2006-12-07 19:39:39 +00001422//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423// FunctionPassManagerImpl implementation
1424//
Duncan Sands51495602009-02-13 09:42:34 +00001425bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001426 bool Changed = false;
1427
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001428 dumpArguments();
1429 dumpPasses();
1430
Pedro Artigas41b98842012-12-05 17:12:22 +00001431 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1432 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1433 E = IPV.end(); I != E; ++I) {
1434 Changed |= (*I)->doInitialization(M);
1435 }
1436
Chris Lattner4c1e9542009-03-06 06:45:05 +00001437 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1438 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001439
1440 return Changed;
1441}
1442
Duncan Sands51495602009-02-13 09:42:34 +00001443bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001444 bool Changed = false;
1445
Pedro Artigas41b98842012-12-05 17:12:22 +00001446 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001447 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001448
Pedro Artigas41b98842012-12-05 17:12:22 +00001449 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1450 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1451 E = IPV.end(); I != E; ++I) {
1452 Changed |= (*I)->doFinalization(M);
1453 }
1454
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455 return Changed;
1456}
1457
Devang Patelec9c58f2009-04-01 22:34:41 +00001458/// cleanup - After running all passes, clean up pass manager cache.
1459void FPPassManager::cleanup() {
1460 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1461 FunctionPass *FP = getContainedPass(Index);
1462 AnalysisResolver *AR = FP->getResolver();
1463 assert(AR && "Analysis Resolver is not set");
1464 AR->clearAnalysisImpls();
1465 }
1466}
1467
Torok Edwin24c78352009-06-29 18:49:09 +00001468void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1469 if (!wasRun)
1470 return;
1471 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1472 FPPassManager *FPPM = getContainedManager(Index);
1473 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1474 FPPM->getContainedPass(Index)->releaseMemory();
1475 }
1476 }
Torok Edwin896556e2009-06-29 21:05:10 +00001477 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001478}
1479
Devang Patel67d6a5e2006-12-19 19:46:59 +00001480// Execute all the passes managed by this top level manager.
1481// Return true if any function is modified by a pass.
1482bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001484 TimingInfo::createTheTimeInfo();
1485
Devang Patele3068402006-12-21 00:16:50 +00001486 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001487 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1488 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001489
1490 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1491 getContainedManager(Index)->cleanup();
1492
Torok Edwin24c78352009-06-29 18:49:09 +00001493 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001494 return Changed;
1495}
1496
1497//===----------------------------------------------------------------------===//
1498// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001499
Devang Patel8c78a0b2007-05-03 01:11:54 +00001500char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001501/// Print passes managed by this manager
1502void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001503 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001504 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1505 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001506 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001507 dumpLastUses(FP, Offset+1);
1508 }
1509}
1510
1511
Dan Gohmande6188a2010-08-12 23:50:08 +00001512/// Execute all of the passes scheduled for execution by invoking
1513/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001514/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001516 if (F.isDeclaration())
1517 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001518
1519 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001520
Devang Patelcbbf2912008-03-20 01:09:53 +00001521 // Collect inherited analysis from Module level pass manager.
1522 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001523
Devang Patelabfbe3b2006-12-16 00:56:26 +00001524 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1525 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001526 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001527
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001528 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001529 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001530
Devang Patelabfbe3b2006-12-16 00:56:26 +00001531 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001532
Chris Lattner4c1e9542009-03-06 06:45:05 +00001533 {
1534 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001535 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001536
Dan Gohman74b189f2010-03-01 17:34:28 +00001537 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001538 }
Devang Patel93a197c2006-12-14 00:08:04 +00001539
Dan Gohman74b189f2010-03-01 17:34:28 +00001540 Changed |= LocalChanged;
1541 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001542 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001543 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001544
Devang Patela273d1c2007-07-19 18:02:32 +00001545 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001546 removeNotPreservedAnalysis(FP);
1547 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001548 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001549 }
1550 return Changed;
1551}
1552
Devang Patel67d6a5e2006-12-19 19:46:59 +00001553bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001554 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001555
Dan Gohmane7630be2010-05-11 20:30:00 +00001556 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001557 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001559 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001560}
1561
Duncan Sands51495602009-02-13 09:42:34 +00001562bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001563 bool Changed = false;
1564
Chris Lattner4c1e9542009-03-06 06:45:05 +00001565 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1566 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001567
Devang Patelff631ae2006-11-15 01:27:05 +00001568 return Changed;
1569}
1570
Duncan Sands51495602009-02-13 09:42:34 +00001571bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001572 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001573
1574 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001575 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001576
Devang Patelff631ae2006-11-15 01:27:05 +00001577 return Changed;
1578}
1579
Devang Patela1514cb2006-12-07 19:39:39 +00001580//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001581// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001582
Dan Gohmande6188a2010-08-12 23:50:08 +00001583/// Execute all of the passes scheduled for execution by invoking
1584/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001585/// the module, and if so, return true.
1586bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001587MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001588 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001589
Torok Edwin24c78352009-06-29 18:49:09 +00001590 // Initialize on-the-fly passes
1591 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1592 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1593 I != E; ++I) {
1594 FunctionPassManagerImpl *FPP = I->second;
1595 Changed |= FPP->doInitialization(M);
1596 }
1597
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001598 // Initialize module passes
1599 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1600 Changed |= getContainedPass(Index)->doInitialization(M);
1601
Devang Patelabfbe3b2006-12-16 00:56:26 +00001602 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1603 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001604 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001605
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001606 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001607 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001608
Devang Patelabfbe3b2006-12-16 00:56:26 +00001609 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001610
Chris Lattner4c1e9542009-03-06 06:45:05 +00001611 {
1612 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001613 TimeRegion PassTimer(getPassTimer(MP));
1614
Dan Gohman74b189f2010-03-01 17:34:28 +00001615 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001616 }
Devang Patel93a197c2006-12-14 00:08:04 +00001617
Dan Gohman74b189f2010-03-01 17:34:28 +00001618 Changed |= LocalChanged;
1619 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001620 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001621 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001622 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001623
Devang Patela273d1c2007-07-19 18:02:32 +00001624 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001625 removeNotPreservedAnalysis(MP);
1626 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001627 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001628 }
Torok Edwin24c78352009-06-29 18:49:09 +00001629
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001630 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001631 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001632 Changed |= getContainedPass(Index)->doFinalization(M);
1633
Torok Edwin24c78352009-06-29 18:49:09 +00001634 // Finalize on-the-fly passes
1635 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1636 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1637 I != E; ++I) {
1638 FunctionPassManagerImpl *FPP = I->second;
1639 // We don't know when is the last time an on-the-fly pass is run,
1640 // so we need to releaseMemory / finalize here
1641 FPP->releaseMemoryOnTheFly();
1642 Changed |= FPP->doFinalization(M);
1643 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001644
Devang Patel05e1a972006-11-07 22:03:15 +00001645 return Changed;
1646}
1647
Devang Patele64d3052007-04-16 20:12:57 +00001648/// Add RequiredPass into list of lower level passes required by pass P.
1649/// RequiredPass is run on the fly by Pass Manager when P requests it
1650/// through getAnalysis interface.
1651void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001652 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1653 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001654 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001655 RequiredPass->getPotentialPassManagerType()) &&
1656 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001657
Devang Patel68f72b12007-04-26 17:50:19 +00001658 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001659 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001660 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001661 // FPP is the top level manager.
1662 FPP->setTopLevelManager(FPP);
1663
Devang Patel69e9f6d2007-04-16 20:27:05 +00001664 OnTheFlyManagers[P] = FPP;
1665 }
Devang Patel68f72b12007-04-26 17:50:19 +00001666 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001667
Devang Patel68f72b12007-04-26 17:50:19 +00001668 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001669 if (RequiredPass) {
1670 SmallVector<Pass *, 1> LU;
1671 LU.push_back(RequiredPass);
1672 FPP->setLastUser(LU, P);
1673 }
Devang Patele64d3052007-04-16 20:12:57 +00001674}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001675
Dan Gohmande6188a2010-08-12 23:50:08 +00001676/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001677/// required by module pass MP. Instantiate analysis pass, by using
1678/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001679Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001680 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001681 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001682
Torok Edwin24c78352009-06-29 18:49:09 +00001683 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001684 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001685 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001686}
1687
1688
Devang Patela1514cb2006-12-07 19:39:39 +00001689//===----------------------------------------------------------------------===//
1690// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001691
Devang Patelab97cf42006-12-13 00:09:23 +00001692//
Devang Patelc290c8a2006-11-07 22:23:34 +00001693/// run - Execute all of the passes scheduled for execution. Keep track of
1694/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001695bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001696 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001697 TimingInfo::createTheTimeInfo();
1698
Devang Patelcfd70c42006-12-13 22:10:00 +00001699 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001700 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001701
Pedro Artigas41b98842012-12-05 17:12:22 +00001702 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1703 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1704 E = IPV.end(); I != E; ++I) {
1705 Changed |= (*I)->doInitialization(M);
1706 }
1707
Devang Patele3068402006-12-21 00:16:50 +00001708 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001709 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1710 Changed |= getContainedManager(Index)->runOnModule(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001711
1712 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1713 E = IPV.end(); I != E; ++I) {
1714 Changed |= (*I)->doFinalization(M);
1715 }
1716
Devang Patelc290c8a2006-11-07 22:23:34 +00001717 return Changed;
1718}
Devang Patel376fefa2006-11-08 10:29:57 +00001719
Devang Patela1514cb2006-12-07 19:39:39 +00001720//===----------------------------------------------------------------------===//
1721// PassManager implementation
1722
Devang Patel376fefa2006-11-08 10:29:57 +00001723/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001724PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001725 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001726 // PM is the top level manager
1727 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001728}
1729
Devang Patelb67904d2006-12-13 02:36:01 +00001730PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001731 delete PM;
1732}
1733
Devang Patel376fefa2006-11-08 10:29:57 +00001734/// add - Add a pass to the queue of passes to run. This passes ownership of
1735/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1736/// will be destroyed as well, so there is no need to delete the pass. This
1737/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001738void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001739 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001740}
1741
1742/// run - Execute all of the passes scheduled for execution. Keep track of
1743/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001744bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001745 return PM->run(M);
1746}
1747
Devang Patelb8817b92006-12-14 00:59:42 +00001748//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001749// TimingInfo implementation
1750
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001751bool llvm::TimePassesIsEnabled = false;
1752static cl::opt<bool,true>
1753EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1754 cl::desc("Time each pass, printing elapsed time for each on exit"));
1755
Devang Patelb8817b92006-12-14 00:59:42 +00001756// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001757// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001758// null. It may be called multiple times.
1759void TimingInfo::createTheTimeInfo() {
1760 if (!TimePassesIsEnabled || TheTimeInfo) return;
1761
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001762 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001763 // This guarantees that the object will be constructed before static globals,
1764 // thus it will be destroyed before them.
1765 static ManagedStatic<TimingInfo> TTI;
1766 TheTimeInfo = &*TTI;
1767}
1768
Devang Patel1c3633e2007-01-29 23:10:37 +00001769/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001770Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001771 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001772 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001773 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001774}
1775
Devang Patel1c56a632007-01-08 19:29:38 +00001776//===----------------------------------------------------------------------===//
1777// PMStack implementation
1778//
Devang Patelad98d232007-01-11 22:15:30 +00001779
Devang Patel1c56a632007-01-08 19:29:38 +00001780// Pop Pass Manager from the stack and clear its analysis info.
1781void PMStack::pop() {
1782
1783 PMDataManager *Top = this->top();
1784 Top->initializeAnalysisInfo();
1785
1786 S.pop_back();
1787}
1788
1789// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001790void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001791 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001792 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001793
Chris Lattner60987362009-03-06 05:53:14 +00001794 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001795 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1796 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001797 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001798
Chris Lattner60987362009-03-06 05:53:14 +00001799 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001800 TPM->addIndirectPassManager(PM);
1801 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001802 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001803 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001804 assert((PM->getPassManagerType() == PMT_ModulePassManager
1805 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001806 && "pushing bad pass manager to PMStack");
1807 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001808 }
1809
Devang Patel15701b52007-01-11 00:19:00 +00001810 S.push_back(PM);
1811}
1812
1813// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001814void PMStack::dump() const {
1815 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001816 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001817 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001818
Devang Patel15701b52007-01-11 00:19:00 +00001819 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001820 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001821}
1822
Devang Patel1c56a632007-01-08 19:29:38 +00001823/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001824/// add self into that manager.
1825void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001826 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001827 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001828 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001829 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1830 if (TopPMType == PreferredType)
1831 break; // We found desired pass manager
1832 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001833 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001834 else
1835 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001836 }
Devang Patel18ff6362008-09-09 21:38:40 +00001837 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001838 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001839}
1840
Devang Patel3312f752007-01-16 21:43:18 +00001841/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001842/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001843void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001844 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001845
Andrew Trickcbc845f2012-02-01 07:16:20 +00001846 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001847 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001848 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1849 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001850 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001851 break;
Devang Patel3312f752007-01-16 21:43:18 +00001852 }
Devang Patel3312f752007-01-16 21:43:18 +00001853
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001854 // Create new Function Pass Manager if needed.
1855 FPPassManager *FPP;
1856 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1857 FPP = (FPPassManager *)PMS.top();
1858 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001859 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1860 PMDataManager *PMD = PMS.top();
1861
1862 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001863 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001864 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001865
1866 // [2] Set up new manager's top level manager
1867 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1868 TPM->addIndirectPassManager(FPP);
1869
1870 // [3] Assign manager to manage this new manager. This may create
1871 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001872 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001873
1874 // [4] Push new manager into PMS
1875 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001876 }
1877
Devang Patel3312f752007-01-16 21:43:18 +00001878 // Assign FPP as the manager of this pass.
1879 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001880}
1881
Devang Patel3312f752007-01-16 21:43:18 +00001882/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001883/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001884void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001885 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001886 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001887
Devang Patel15701b52007-01-11 00:19:00 +00001888 // Basic Pass Manager is a leaf pass manager. It does not handle
1889 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001890 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001891 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1892 BBP = (BBPassManager *)PMS.top();
1893 } else {
1894 // If leaf manager is not Basic Block Pass manager then create new
1895 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001896 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1897 PMDataManager *PMD = PMS.top();
1898
1899 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001900 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001901
1902 // [2] Set up new manager's top level manager
1903 // Basic Block Pass Manager does not live by itself
1904 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1905 TPM->addIndirectPassManager(BBP);
1906
Devang Patel15701b52007-01-11 00:19:00 +00001907 // [3] Assign manager to manage this new manager. This may create
1908 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001909 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001910
Devang Patel3312f752007-01-16 21:43:18 +00001911 // [4] Push new manager into PMS
1912 PMS.push(BBP);
1913 }
Devang Patel1c56a632007-01-08 19:29:38 +00001914
Devang Patel3312f752007-01-16 21:43:18 +00001915 // Assign BBP as the manager of this pass.
1916 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001917}
1918
Dan Gohmand3a20c92008-03-11 16:41:42 +00001919PassManagerBase::~PassManagerBase() {}