blob: a431d8256d70bd07864fb392582aa245a6064c6a [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
David Greene9b063df2010-04-02 23:17:14 +000015#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000016#include "llvm/Assembly/Writer.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000019#include "llvm/IR/LegacyPassManagers.h"
Devang Patelf1567a52006-12-13 20:03:48 +000020#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000021#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000023#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/PassNameParser.h"
26#include "llvm/Support/Timer.h"
27#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000029#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000030using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000031using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000032
Devang Patele7599552007-01-12 18:52:44 +000033// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000034
Devang Patelf1567a52006-12-13 20:03:48 +000035//===----------------------------------------------------------------------===//
36// Pass debugging information. Often it is useful to find out what pass is
37// running when a crash occurs in a utility. When this library is compiled with
38// debugging on, a command line option (--debug-pass) is enabled that causes the
39// pass name to be printed before it executes.
40//
41
Chandler Carruth7caea412013-11-09 12:26:54 +000042namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000043// Different debug levels that can be enabled...
44enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000045 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000046};
Chandler Carruth7caea412013-11-09 12:26:54 +000047}
Devang Patel03fb5872006-12-13 21:13:31 +000048
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000049static cl::opt<enum PassDebugLevel>
50PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000051 cl::desc("Print PassManager debugging information"),
52 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000053 clEnumVal(Disabled , "disable debug output"),
54 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
55 clEnumVal(Structure , "print pass structure before run()"),
56 clEnumVal(Executions, "print pass name before it is executed"),
57 clEnumVal(Details , "print pass details when it is executed"),
58 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000059
Chandler Carruth7caea412013-11-09 12:26:54 +000060namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000061typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000063}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000064
65// Print IR out before/after specified passes.
66static PassOptionList
67PrintBefore("print-before",
68 llvm::cl::desc("Print IR before specified passes"),
69 cl::Hidden);
70
71static PassOptionList
72PrintAfter("print-after",
73 llvm::cl::desc("Print IR after specified passes"),
74 cl::Hidden);
75
76static cl::opt<bool>
77PrintBeforeAll("print-before-all",
78 llvm::cl::desc("Print IR before each pass"),
79 cl::init(false));
80static cl::opt<bool>
81PrintAfterAll("print-after-all",
82 llvm::cl::desc("Print IR after each pass"),
83 cl::init(false));
84
85/// This is a helper to determine whether to print IR before or
86/// after a pass.
87
88static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
89 PassOptionList &PassesToPrint) {
90 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
91 const llvm::PassInfo *PassInf = PassesToPrint[i];
92 if (PassInf)
93 if (PassInf->getPassArgument() == PI->getPassArgument()) {
94 return true;
95 }
David Greene9b063df2010-04-02 23:17:14 +000096 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000097 return false;
98}
Dan Gohmande6188a2010-08-12 23:50:08 +000099
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000100/// This is a utility to check whether a pass should have IR dumped
101/// before it.
102static bool ShouldPrintBeforePass(const PassInfo *PI) {
103 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
104}
David Greene9b063df2010-04-02 23:17:14 +0000105
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000106/// This is a utility to check whether a pass should have IR dumped
107/// after it.
108static bool ShouldPrintAfterPass(const PassInfo *PI) {
109 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000110}
111
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000112/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
113/// or higher is specified.
114bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000115 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000116}
117
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000118
119
120
Chris Lattner4c1e9542009-03-06 06:45:05 +0000121void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
122 if (V == 0 && M == 0)
123 OS << "Releasing pass '";
124 else
125 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000126
Chris Lattner4c1e9542009-03-06 06:45:05 +0000127 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000128
Chris Lattner4c1e9542009-03-06 06:45:05 +0000129 if (M) {
130 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
131 return;
132 }
133 if (V == 0) {
134 OS << '\n';
135 return;
136 }
137
Dan Gohman79fc0e92009-03-10 18:47:59 +0000138 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000139 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000140 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000141 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000142 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000143 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000144 OS << "value";
145
146 OS << " '";
147 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
148 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000149}
150
151
Devang Patelffca9102006-12-15 19:39:30 +0000152namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000153//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000154// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000155//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000157/// pass together and sequence them to process one basic block before
158/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000159class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000160
161public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000162 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000163 explicit BBPassManager()
164 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000165
Devang Patelca58e352006-11-08 10:05:38 +0000166 /// Execute all of the passes scheduled for execution. Keep track of
167 /// whether any of the passes modifies the function, and if so, return true.
168 bool runOnFunction(Function &F);
169
Devang Patelf9d96b92006-12-07 19:57:52 +0000170 /// Pass Manager itself does not invalidate any analysis info.
171 void getAnalysisUsage(AnalysisUsage &Info) const {
172 Info.setPreservesAll();
173 }
174
Devang Patel475c4532006-12-08 00:59:05 +0000175 bool doInitialization(Module &M);
176 bool doInitialization(Function &F);
177 bool doFinalization(Module &M);
178 bool doFinalization(Function &F);
179
Chris Lattner2fa26e52010-01-22 05:24:46 +0000180 virtual PMDataManager *getAsPMDataManager() { return this; }
181 virtual Pass *getAsPass() { return this; }
182
Devang Patele3858e62007-02-01 22:08:25 +0000183 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000184 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000185 }
186
Devang Pateleda56172006-12-12 23:34:33 +0000187 // Print passes managed by this manager
188 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000189 llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000190 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
191 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000192 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000193 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000194 }
195 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000196
197 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000198 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
200 return BP;
201 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000202
Dan Gohmande6188a2010-08-12 23:50:08 +0000203 virtual PassManagerType getPassManagerType() const {
204 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000205 }
Devang Patelca58e352006-11-08 10:05:38 +0000206};
207
Devang Patel8c78a0b2007-05-03 01:11:54 +0000208char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000209} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210
Devang Patele7599552007-01-12 18:52:44 +0000211namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000212namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000213//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000214// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216/// FunctionPassManagerImpl manages FPPassManagers
217class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000218 public PMDataManager,
219 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000220 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000221private:
222 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000224 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000225 explicit FunctionPassManagerImpl() :
226 Pass(PT_PassManager, ID), PMDataManager(),
227 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228
229 /// add - Add a pass to the queue of passes to run. This passes ownership of
230 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
231 /// will be destroyed as well, so there is no need to delete the pass. This
232 /// implies that all passes MUST be allocated with 'new'.
233 void add(Pass *P) {
234 schedulePass(P);
235 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000236
237 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000238 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
239 return createPrintFunctionPass(Banner, &O);
240 }
241
Torok Edwin24c78352009-06-29 18:49:09 +0000242 // Prepare for running an on the fly pass, freeing memory if needed
243 // from a previous run.
244 void releaseMemoryOnTheFly();
245
Devang Patel67d6a5e2006-12-19 19:46:59 +0000246 /// run - Execute all of the passes scheduled for execution. Keep track of
247 /// whether any of the passes modifies the module, and if so, return true.
248 bool run(Function &F);
249
250 /// doInitialization - Run all of the initializers for the function passes.
251 ///
252 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000253
Dan Gohmane6656eb2007-07-30 14:51:13 +0000254 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255 ///
256 bool doFinalization(Module &M);
257
Dan Gohmande6188a2010-08-12 23:50:08 +0000258
Chris Lattner2fa26e52010-01-22 05:24:46 +0000259 virtual PMDataManager *getAsPMDataManager() { return this; }
260 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000261 virtual PassManagerType getTopLevelPassManagerType() {
262 return PMT_FunctionPassManager;
263 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000264
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265 /// Pass Manager itself does not invalidate any analysis info.
266 void getAnalysisUsage(AnalysisUsage &Info) const {
267 Info.setPreservesAll();
268 }
269
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000271 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
273 return FP;
274 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000275};
276
David Blaikiea379b1812011-12-20 02:50:00 +0000277void FunctionPassManagerImpl::anchor() {}
278
Devang Patel8c78a0b2007-05-03 01:11:54 +0000279char FunctionPassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000280} // End of legacy namespace
281} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000282
Chandler Carruth7caea412013-11-09 12:26:54 +0000283namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284//===----------------------------------------------------------------------===//
285// MPPassManager
286//
287/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000288/// It batches all Module passes and function pass managers together and
289/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000291public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000292 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000293 explicit MPPassManager() :
294 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000295
296 // Delete on the fly managers.
297 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000298 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000299 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
300 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000301 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000302 delete FPP;
303 }
304 }
305
Dan Gohmande6188a2010-08-12 23:50:08 +0000306 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000307 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
308 return createPrintModulePass(&O, false, Banner);
309 }
310
Devang Patelca58e352006-11-08 10:05:38 +0000311 /// run - Execute all of the passes scheduled for execution. Keep track of
312 /// whether any of the passes modifies the module, and if so, return true.
313 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000314
Pedro Artigase4348b02012-12-03 21:56:57 +0000315 using llvm::Pass::doInitialization;
316 using llvm::Pass::doFinalization;
317
Owen Anderson1aa27512012-11-15 00:14:15 +0000318 /// doInitialization - Run all of the initializers for the module passes.
319 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000320 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000321
322 /// doFinalization - Run all of the finalizers for the module passes.
323 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000324 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000325
Devang Patelf9d96b92006-12-07 19:57:52 +0000326 /// Pass Manager itself does not invalidate any analysis info.
327 void getAnalysisUsage(AnalysisUsage &Info) const {
328 Info.setPreservesAll();
329 }
330
Devang Patele64d3052007-04-16 20:12:57 +0000331 /// Add RequiredPass into list of lower level passes required by pass P.
332 /// RequiredPass is run on the fly by Pass Manager when P requests it
333 /// through getAnalysis interface.
334 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
335
Dan Gohmande6188a2010-08-12 23:50:08 +0000336 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000337 /// required by module pass MP. Instantiate analysis pass, by using
338 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000339 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000340
Devang Patele3858e62007-02-01 22:08:25 +0000341 virtual const char *getPassName() const {
342 return "Module Pass Manager";
343 }
344
Chris Lattner2fa26e52010-01-22 05:24:46 +0000345 virtual PMDataManager *getAsPMDataManager() { return this; }
346 virtual Pass *getAsPass() { return this; }
347
Devang Pateleda56172006-12-12 23:34:33 +0000348 // Print passes managed by this manager
349 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000350 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
352 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000353 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000354 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
355 OnTheFlyManagers.find(MP);
356 if (I != OnTheFlyManagers.end())
357 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000358 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000359 }
360 }
361
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000363 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000364 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000365 }
366
Dan Gohmande6188a2010-08-12 23:50:08 +0000367 virtual PassManagerType getPassManagerType() const {
368 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000369 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000370
371 private:
372 /// Collection of on the fly FPPassManagers. These managers manage
373 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000374 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000375};
376
Devang Patel8c78a0b2007-05-03 01:11:54 +0000377char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000378} // End anonymous namespace
379
380namespace llvm {
381namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000382//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000383// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000384//
Devang Patel09f162c2007-05-01 21:15:47 +0000385
Devang Patel67d6a5e2006-12-19 19:46:59 +0000386/// PassManagerImpl manages MPPassManagers
387class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000388 public PMDataManager,
389 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000390 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000391
392public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000393 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000394 explicit PassManagerImpl() :
395 Pass(PT_PassManager, ID), PMDataManager(),
396 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000397
Devang Patel376fefa2006-11-08 10:29:57 +0000398 /// add - Add a pass to the queue of passes to run. This passes ownership of
399 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
400 /// will be destroyed as well, so there is no need to delete the pass. This
401 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000402 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000403 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000404 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000405
406 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000407 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
408 return createPrintModulePass(&O, false, Banner);
409 }
410
Devang Patel376fefa2006-11-08 10:29:57 +0000411 /// run - Execute all of the passes scheduled for execution. Keep track of
412 /// whether any of the passes modifies the module, and if so, return true.
413 bool run(Module &M);
414
Pedro Artigase4348b02012-12-03 21:56:57 +0000415 using llvm::Pass::doInitialization;
416 using llvm::Pass::doFinalization;
417
Owen Anderson1aa27512012-11-15 00:14:15 +0000418 /// doInitialization - Run all of the initializers for the module passes.
419 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000420 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000421
422 /// doFinalization - Run all of the finalizers for the module passes.
423 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000424 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000425
Devang Patelf9d96b92006-12-07 19:57:52 +0000426 /// Pass Manager itself does not invalidate any analysis info.
427 void getAnalysisUsage(AnalysisUsage &Info) const {
428 Info.setPreservesAll();
429 }
430
Chris Lattner2fa26e52010-01-22 05:24:46 +0000431 virtual PMDataManager *getAsPMDataManager() { return this; }
432 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000433 virtual PassManagerType getTopLevelPassManagerType() {
434 return PMT_ModulePassManager;
435 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000436
Devang Patel67d6a5e2006-12-19 19:46:59 +0000437 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000438 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000439 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
440 return MP;
441 }
Devang Patel376fefa2006-11-08 10:29:57 +0000442};
443
David Blaikiea379b1812011-12-20 02:50:00 +0000444void PassManagerImpl::anchor() {}
445
Devang Patel8c78a0b2007-05-03 01:11:54 +0000446char PassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000447} // End of legacy namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000448} // End of llvm namespace
449
450namespace {
451
452//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000453/// TimingInfo Class - This class is used to calculate information about the
454/// amount of time each pass takes to execute. This only happens when
455/// -time-passes is enabled on the command line.
456///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000457
Owen Anderson5a6960f2009-06-18 20:51:00 +0000458static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000459
Nick Lewycky02d5f772009-10-25 06:33:48 +0000460class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000461 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000462 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000463public:
464 // Use 'create' member to get this.
465 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000466
Devang Patel1c3633e2007-01-29 23:10:37 +0000467 // TimingDtor - Print out information about timing information
468 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000469 // Delete all of the timers, which accumulate their info into the
470 // TimerGroup.
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000471 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattner707431c2010-03-30 04:03:22 +0000472 E = TimingData.end(); I != E; ++I)
473 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000474 // TimerGroup is deleted next, printing the report.
475 }
476
477 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
478 // to a non null value (if the -time-passes option is enabled) or it leaves it
479 // null. It may be called multiple times.
480 static void createTheTimeInfo();
481
Chris Lattner707431c2010-03-30 04:03:22 +0000482 /// getPassTimer - Return the timer for the specified pass if it exists.
483 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000484 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000485 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000486
Owen Anderson5c96ef72009-07-07 18:33:04 +0000487 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000488 Timer *&T = TimingData[P];
Chris Lattner707431c2010-03-30 04:03:22 +0000489 if (T == 0)
490 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000491 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000492 }
493};
494
Devang Patel1c3633e2007-01-29 23:10:37 +0000495} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000496
Dan Gohmand78c4002008-05-13 00:00:25 +0000497static TimingInfo *TheTimeInfo;
498
Devang Patela1514cb2006-12-07 19:39:39 +0000499//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000500// PMTopLevelManager implementation
501
Devang Patel4268fc02007-01-16 02:00:38 +0000502/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000503PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
504 PMDM->setTopLevelManager(this);
505 addPassManager(PMDM);
506 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000507}
508
Devang Patelafb1f3622006-12-12 22:35:25 +0000509/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000510void
Bill Wendlingea857e12012-05-14 07:53:40 +0000511PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000512 unsigned PDepth = 0;
513 if (P->getResolver())
514 PDepth = P->getResolver()->getPMDataManager().getDepth();
515
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000516 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000517 E = AnalysisPasses.end(); I != E; ++I) {
518 Pass *AP = *I;
519 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000520
Devang Patel01919d22007-03-08 19:05:01 +0000521 if (P == AP)
522 continue;
523
Tobias Grosserf07426b2011-01-20 21:03:22 +0000524 // Update the last users of passes that are required transitive by AP.
525 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
526 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
527 SmallVector<Pass *, 12> LastUses;
528 SmallVector<Pass *, 12> LastPMUses;
529 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
530 E = IDs.end(); I != E; ++I) {
531 Pass *AnalysisPass = findAnalysisPass(*I);
532 assert(AnalysisPass && "Expected analysis pass to exist.");
533 AnalysisResolver *AR = AnalysisPass->getResolver();
534 assert(AR && "Expected analysis resolver to exist.");
535 unsigned APDepth = AR->getPMDataManager().getDepth();
536
537 if (PDepth == APDepth)
538 LastUses.push_back(AnalysisPass);
539 else if (PDepth > APDepth)
540 LastPMUses.push_back(AnalysisPass);
541 }
542
543 setLastUser(LastUses, P);
544
545 // If this pass has a corresponding pass manager, push higher level
546 // analysis to this pass manager.
547 if (P->getResolver())
548 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
549
550
Devang Patelafb1f3622006-12-12 22:35:25 +0000551 // If AP is the last user of other passes then make P last user of
552 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000553 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000554 LUE = LastUser.end(); LUI != LUE; ++LUI) {
555 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000556 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000557 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000558 LastUser[LUI->first] = P;
559 }
560 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000561}
562
563/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000564void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000565 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000566 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000567 InversedLastUser.find(P);
568 if (DMI == InversedLastUser.end())
569 return;
570
571 SmallPtrSet<Pass *, 8> &LU = DMI->second;
572 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
573 E = LU.end(); I != E; ++I) {
574 LastUses.push_back(*I);
575 }
576
Devang Patelafb1f3622006-12-12 22:35:25 +0000577}
578
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000579AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
580 AnalysisUsage *AnUsage = NULL;
581 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000582 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000583 AnUsage = DMI->second;
584 else {
585 AnUsage = new AnalysisUsage();
586 P->getAnalysisUsage(*AnUsage);
587 AnUsageMap[P] = AnUsage;
588 }
589 return AnUsage;
590}
591
Devang Patelafb1f3622006-12-12 22:35:25 +0000592/// Schedule pass P for execution. Make sure that passes required by
593/// P are run before P is run. Update analysis info maintained by
594/// the manager. Remove dead passes. This is a recursive function.
595void PMTopLevelManager::schedulePass(Pass *P) {
596
Devang Patel3312f752007-01-16 21:43:18 +0000597 // TODO : Allocate function manager for this pass, other wise required set
598 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000599
Devang Pateld74ede72007-03-06 01:06:16 +0000600 // Give pass a chance to prepare the stage.
601 P->preparePassManager(activeStack);
602
Devang Patel864970e2008-03-18 00:39:19 +0000603 // If P is an analysis pass and it is available then do not
604 // generate the analysis again. Stale analysis info should not be
605 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000606 const PassInfo *PI =
607 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
608 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000609 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000610 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000611 }
Devang Patel864970e2008-03-18 00:39:19 +0000612
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000613 AnalysisUsage *AnUsage = findAnalysisUsage(P);
614
Devang Patelfdee7032008-08-14 23:07:48 +0000615 bool checkAnalysis = true;
616 while (checkAnalysis) {
617 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000618
Devang Patelfdee7032008-08-14 23:07:48 +0000619 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
620 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
621 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000622
Devang Patelfdee7032008-08-14 23:07:48 +0000623 Pass *AnalysisPass = findAnalysisPass(*I);
624 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000625 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000626
627 if (PI == NULL) {
628 // Pass P is not in the global PassRegistry
629 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
630 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
631 dbgs() << "Required Passes:" << "\n";
632 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
633 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
634 Pass *AnalysisPass2 = findAnalysisPass(*I2);
635 if (AnalysisPass2) {
636 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000637 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000638 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
639 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
640 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
641 }
642 }
643 }
644
Andrew Trick6bbaf132011-06-03 00:48:58 +0000645 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000646 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000647 if (P->getPotentialPassManagerType () ==
648 AnalysisPass->getPotentialPassManagerType())
649 // Schedule analysis pass that is managed by the same pass manager.
650 schedulePass(AnalysisPass);
651 else if (P->getPotentialPassManagerType () >
652 AnalysisPass->getPotentialPassManagerType()) {
653 // Schedule analysis pass that is managed by a new manager.
654 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000655 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000656 // are already checked are still available.
657 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000658 } else
Dan Gohmande6188a2010-08-12 23:50:08 +0000659 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000660 // passes are run on the fly.
661 delete AnalysisPass;
662 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000663 }
664 }
665
666 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000667 if (ImmutablePass *IP = P->getAsImmutablePass()) {
668 // P is a immutable pass and it will be managed by this
669 // top level manager. Set up analysis resolver to connect them.
670 PMDataManager *DM = getAsPMDataManager();
671 AnalysisResolver *AR = new AnalysisResolver(*DM);
672 P->setResolver(AR);
673 DM->initializeAnalysisImpl(P);
674 addImmutablePass(IP);
675 DM->recordAvailableAnalysis(IP);
676 return;
677 }
678
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000679 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000680 Pass *PP = P->createPrinterPass(
681 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
682 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
683 }
684
685 // Add the requested pass to the best available pass manager.
686 P->assignPassManager(activeStack, getTopLevelPassManagerType());
687
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000688 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000689 Pass *PP = P->createPrinterPass(
690 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
691 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
692 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000693}
694
695/// Find the pass that implements Analysis AID. Search immutable
696/// passes and all pass managers. If desired pass is not found
697/// then return NULL.
698Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
699
Devang Patelcd6ba152006-12-12 22:50:05 +0000700 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000701 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000702 E = PassManagers.end(); I != E; ++I)
703 if (Pass *P = (*I)->findAnalysisPass(AID, false))
704 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000705
706 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000707 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000708 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000709 E = IndirectPassManagers.end(); I != E; ++I)
710 if (Pass *P = (*I)->findAnalysisPass(AID, false))
711 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000712
Dan Gohman844dd0a2010-10-11 23:19:01 +0000713 // Check the immutable passes. Iterate in reverse order so that we find
714 // the most recently registered passes first.
Craig Topper31ee5862013-07-03 15:07:05 +0000715 for (SmallVectorImpl<ImmutablePass *>::reverse_iterator I =
Dan Gohman844dd0a2010-10-11 23:19:01 +0000716 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000717 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000718 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000719 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000720
721 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000722 const PassInfo *PassInf =
723 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000724 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000725 const std::vector<const PassInfo*> &ImmPI =
726 PassInf->getInterfacesImplemented();
727 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
728 EE = ImmPI.end(); II != EE; ++II) {
729 if ((*II)->getTypeInfo() == AID)
730 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000731 }
732 }
733
Dan Gohman844dd0a2010-10-11 23:19:01 +0000734 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000735}
736
Devang Pateleda56172006-12-12 23:34:33 +0000737// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000738void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000739
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000740 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000741 return;
742
Devang Pateleda56172006-12-12 23:34:33 +0000743 // Print out the immutable passes
744 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000745 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000746 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000747
Dan Gohmanf71c5212010-08-19 01:29:07 +0000748 // Every class that derives from PMDataManager also derives from Pass
749 // (sometimes indirectly), but there's no inheritance relationship
750 // between PMDataManager and Pass, so we have to getAsPass to get
751 // from a PMDataManager* to a Pass*.
Craig Topper31ee5862013-07-03 15:07:05 +0000752 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
753 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000754 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000755}
756
Devang Patel991aeba2006-12-15 20:13:01 +0000757void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000758
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000759 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000760 return;
761
David Greene994e1bb2010-01-05 01:30:02 +0000762 dbgs() << "Pass Arguments: ";
Craig Topper31ee5862013-07-03 15:07:05 +0000763 for (SmallVectorImpl<ImmutablePass *>::const_iterator I =
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000764 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
765 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000766 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
767 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000768 if (!PI->isAnalysisGroup())
769 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000770 }
Craig Topper31ee5862013-07-03 15:07:05 +0000771 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
772 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000773 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000774 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000775}
776
Devang Patele3068402006-12-21 00:16:50 +0000777void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000778 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000779 E = PassManagers.end(); I != E; ++I)
780 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000781
Devang Patele3068402006-12-21 00:16:50 +0000782 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000783 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000784 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
785 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000786 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000787
Chris Lattner60987362009-03-06 05:53:14 +0000788 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000789 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000790 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000791 InversedLastUser.find(DMI->second);
792 if (InvDMI != InversedLastUser.end()) {
793 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
794 L.insert(DMI->first);
795 } else {
796 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
797 InversedLastUser[DMI->second] = L;
798 }
799 }
Devang Patele3068402006-12-21 00:16:50 +0000800}
801
Devang Patele7599552007-01-12 18:52:44 +0000802/// Destructor
803PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000804 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000805 E = PassManagers.end(); I != E; ++I)
806 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000807
Dan Gohman060d5ba2010-10-12 00:15:27 +0000808 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000809 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
810 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000811
812 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000813 DME = AnUsageMap.end(); DMI != DME; ++DMI)
814 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000815}
816
Devang Patelafb1f3622006-12-12 22:35:25 +0000817//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000818// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000819
Devang Patel643676c2006-11-11 01:10:19 +0000820/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000821void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000822 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000823
Chris Lattner60987362009-03-06 05:53:14 +0000824 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000825
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000826 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000827
Dan Gohmande6188a2010-08-12 23:50:08 +0000828 // This pass is the current implementation of all of the interfaces it
829 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000830 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
831 if (PInf == 0) return;
832 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000833 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000834 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000835}
836
Devang Patel9d9fc902007-03-06 17:52:53 +0000837// Return true if P preserves high level analysis used by other
838// passes managed by this manager
839bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000840 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000841 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000842 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000843
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000844 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000845 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000846 E = HigherLevelAnalysis.end(); I != E; ++I) {
847 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000848 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000849 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000850 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000851 PreservedSet.end())
852 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000853 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000854
Devang Patel9d9fc902007-03-06 17:52:53 +0000855 return true;
856}
857
Chris Lattner02eb94c2008-08-07 07:34:50 +0000858/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000859void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000860 // Don't do this unless assertions are enabled.
861#ifdef NDEBUG
862 return;
863#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000864 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
865 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000866
Devang Patelef432532007-07-19 05:36:09 +0000867 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000868 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000869 E = PreservedSet.end(); I != E; ++I) {
870 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000871 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000872 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000873 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000874 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000875 }
876}
877
Devang Patel67c79a42008-07-01 19:50:56 +0000878/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000879void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000880 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
881 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000882 return;
883
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000884 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000885 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000886 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000887 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000888 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000889 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000890 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000891 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000892 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000893 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000894 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
895 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000896 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000897 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000898 }
Devang Patel349170f2006-11-11 01:24:55 +0000899 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000900
Devang Patel42dd1e92007-03-06 01:55:46 +0000901 // Check inherited analysis also. If P is not preserving analysis
902 // provided by parent manager then remove it here.
903 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
904
905 if (!InheritedAnalysis[Index])
906 continue;
907
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000908 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000909 I = InheritedAnalysis[Index]->begin(),
910 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000911 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000912 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000913 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000914 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000915 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000916 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000917 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000918 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
919 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000920 }
Devang Patel01919d22007-03-08 19:05:01 +0000921 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000922 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000923 }
924 }
Devang Patelf68a3492006-11-07 22:35:17 +0000925}
926
Devang Patelca189262006-11-14 03:05:08 +0000927/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000928void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000929 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000930
Devang Patel8adae862007-07-20 18:04:54 +0000931 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000932
Devang Patel2ff44922007-04-16 20:39:59 +0000933 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000934 if (!TPM)
935 return;
936
Devang Patel17ad0962006-12-08 00:37:52 +0000937 TPM->collectLastUses(DeadPasses, P);
938
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000939 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000940 dbgs() << " -*- '" << P->getPassName();
941 dbgs() << "' is the last user of following pass instances.";
942 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000943 }
944
Dan Gohman060d5ba2010-10-12 00:15:27 +0000945 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000946 E = DeadPasses.end(); I != E; ++I)
947 freePass(*I, Msg, DBG_STR);
948}
Devang Patel200d3052006-12-13 23:50:44 +0000949
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000950void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000951 enum PassDebuggingString DBG_STR) {
952 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000953
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000954 {
955 // If the pass crashes releasing memory, remember this.
956 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000957 TimeRegion PassTimer(getPassTimer(P));
958
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000959 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000960 }
961
Owen Andersona7aed182010-08-06 18:33:48 +0000962 AnalysisID PI = P->getPassID();
963 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000964 // Remove the pass itself (if it is not already removed).
965 AvailableAnalysis.erase(PI);
966
967 // Remove all interfaces this pass implements, for which it is also
968 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000969 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000970 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000971 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000972 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000973 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000974 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000975 }
Devang Patel17ad0962006-12-08 00:37:52 +0000976 }
Devang Patelca189262006-11-14 03:05:08 +0000977}
978
Dan Gohmande6188a2010-08-12 23:50:08 +0000979/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000980/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000981void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000982 // This manager is going to manage pass P. Set up analysis resolver
983 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000984 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000985 P->setResolver(AR);
986
Devang Patelec2b9a72007-03-05 22:57:49 +0000987 // If a FunctionPass F is the last user of ModulePass info M
988 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000989 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000990
Chris Lattner60987362009-03-06 05:53:14 +0000991 if (!ProcessAnalysis) {
992 // Add pass
993 PassVector.push_back(P);
994 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000995 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000996
Chris Lattner60987362009-03-06 05:53:14 +0000997 // At the moment, this pass is the last user of all required passes.
998 SmallVector<Pass *, 12> LastUses;
999 SmallVector<Pass *, 8> RequiredPasses;
1000 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1001
1002 unsigned PDepth = this->getDepth();
1003
Dan Gohmande6188a2010-08-12 23:50:08 +00001004 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +00001005 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +00001006 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001007 E = RequiredPasses.end(); I != E; ++I) {
1008 Pass *PRequired = *I;
1009 unsigned RDepth = 0;
1010
1011 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1012 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1013 RDepth = DM.getDepth();
1014
1015 if (PDepth == RDepth)
1016 LastUses.push_back(PRequired);
1017 else if (PDepth > RDepth) {
1018 // Let the parent claim responsibility of last use
1019 TransferLastUses.push_back(PRequired);
1020 // Keep track of higher level analysis used by this manager.
1021 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001022 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001023 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001024 }
1025
1026 // Set P as P's last user until someone starts using P.
1027 // However, if P is a Pass Manager then it does not need
1028 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001029 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001030 LastUses.push_back(P);
1031 TPM->setLastUser(LastUses, P);
1032
1033 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001034 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001035 TPM->setLastUser(TransferLastUses, My_PM);
1036 TransferLastUses.clear();
1037 }
1038
Dan Gohman6304db32010-08-16 22:57:28 +00001039 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001040 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001041 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001042 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001043 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1044 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001045 this->addLowerLevelRequiredPass(P, AnalysisPass);
1046 }
1047
1048 // Take a note of analysis required and made available by this pass.
1049 // Remove the analysis not preserved by this pass
1050 removeNotPreservedAnalysis(P);
1051 recordAvailableAnalysis(P);
1052
Devang Patel8cad70d2006-11-11 01:51:02 +00001053 // Add pass
1054 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001055}
1056
Devang Patele64d3052007-04-16 20:12:57 +00001057
1058/// Populate RP with analysis pass that are required by
1059/// pass P and are available. Populate RP_NotAvail with analysis
1060/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001061void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1062 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001063 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001064 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1065 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001066 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001067 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001068 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001069 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001070 else
Chris Lattner60987362009-03-06 05:53:14 +00001071 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001072 }
Devang Patelf58183d2006-12-12 23:09:32 +00001073
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001074 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001075 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001076 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001077 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001078 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001079 else
Chris Lattner60987362009-03-06 05:53:14 +00001080 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001081 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001082}
1083
Devang Patel07f4f582006-11-14 21:49:36 +00001084// All Required analyses should be available to the pass as it runs! Here
1085// we fill in the AnalysisImpls member of the pass so that it can
1086// successfully use the getAnalysis() method to retrieve the
1087// implementations it needs.
1088//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001089void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001090 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1091
Chris Lattnercbd160f2008-08-08 05:33:04 +00001092 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001093 I = AnUsage->getRequiredSet().begin(),
1094 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001095 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001096 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001097 // This may be analysis pass that is initialized on the fly.
1098 // If that is not the case then it will raise an assert when it is used.
1099 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001100 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001101 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001102 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001103 }
1104}
1105
Devang Patel640c5bb2006-12-08 22:30:11 +00001106/// Find the pass that implements Analysis AID. If desired pass is not found
1107/// then return NULL.
1108Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1109
1110 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001111 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001112
1113 if (I != AvailableAnalysis.end())
1114 return I->second;
1115
1116 // Search Parents through TopLevelManager
1117 if (SearchParent)
1118 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001119
Devang Patel9d759b82006-12-09 00:09:12 +00001120 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001121}
1122
Devang Patel991aeba2006-12-15 20:13:01 +00001123// Print list of passes that are last used by P.
1124void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1125
Devang Patel8adae862007-07-20 18:04:54 +00001126 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001127
1128 // If this is a on the fly manager then it does not have TPM.
1129 if (!TPM)
1130 return;
1131
Devang Patel991aeba2006-12-15 20:13:01 +00001132 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001133
Dan Gohman7224bce2010-10-12 00:11:18 +00001134 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001135 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001136 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001137 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001138 }
1139}
1140
1141void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001142 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001143 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001144 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001145 PMD->dumpPassArguments();
1146 else
Owen Andersona7aed182010-08-06 18:33:48 +00001147 if (const PassInfo *PI =
1148 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001149 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001151 }
1152}
1153
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001154void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1155 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001156 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001157 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001158 return;
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001160 switch (S1) {
1161 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001165 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001166 break;
1167 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001168 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001169 break;
1170 default:
1171 break;
1172 }
1173 switch (S2) {
1174 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001175 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001176 break;
1177 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001178 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001179 break;
1180 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001181 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001182 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001183 case ON_REGION_MSG:
1184 dbgs() << "' on Region '" << Msg << "'...\n";
1185 break;
Devang Patel003a5592007-03-05 20:01:30 +00001186 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001187 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001188 break;
1189 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001190 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001191 break;
1192 default:
1193 break;
1194 }
Devang Patel991aeba2006-12-15 20:13:01 +00001195}
1196
Chris Lattner4c1e9542009-03-06 06:45:05 +00001197void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001198 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001199 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001200
Chris Lattner4c493d92008-08-08 15:14:09 +00001201 AnalysisUsage analysisUsage;
1202 P->getAnalysisUsage(analysisUsage);
1203 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1204}
1205
Chris Lattner4c1e9542009-03-06 06:45:05 +00001206void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001207 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001208 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001209
Chris Lattner4c493d92008-08-08 15:14:09 +00001210 AnalysisUsage analysisUsage;
1211 P->getAnalysisUsage(analysisUsage);
1212 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1213}
1214
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001215void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001216 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001217 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001218 if (Set.empty())
1219 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001220 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001221 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001222 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001223 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001224 if (!PInf) {
1225 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1226 // all drivers.
1227 dbgs() << " Uninitialized Pass";
1228 continue;
1229 }
Owen Andersona7aed182010-08-06 18:33:48 +00001230 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001231 }
David Greene994e1bb2010-01-05 01:30:02 +00001232 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001233}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001234
Devang Patel004937b2007-07-27 20:06:09 +00001235/// Add RequiredPass into list of lower level passes required by pass P.
1236/// RequiredPass is run on the fly by Pass Manager when P requests it
1237/// through getAnalysis interface.
1238/// This should be handled by specific pass manager.
1239void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1240 if (TPM) {
1241 TPM->dumpArguments();
1242 TPM->dumpPasses();
1243 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001244
Dan Gohmande6188a2010-08-12 23:50:08 +00001245 // Module Level pass may required Function Level analysis info
1246 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1247 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001248 // module level pass is requiring lower level analysis info managed by
1249 // lower level pass manager.
1250
1251 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001252 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001253 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001254#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001255 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1256 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001257#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001258 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001259}
1260
Owen Andersona7aed182010-08-06 18:33:48 +00001261Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001262 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001263}
1264
Devang Patele7599552007-01-12 18:52:44 +00001265// Destructor
1266PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001267 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001268 E = PassVector.end(); I != E; ++I)
1269 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001270}
1271
Devang Patel9bdf7d42006-12-08 23:28:54 +00001272//===----------------------------------------------------------------------===//
1273// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001274// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1275Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001276 return PM.findAnalysisPass(ID, dir);
1277}
1278
Dan Gohmande6188a2010-08-12 23:50:08 +00001279Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001280 Function &F) {
1281 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1282}
1283
Devang Patela1514cb2006-12-07 19:39:39 +00001284//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001286
Dan Gohmande6188a2010-08-12 23:50:08 +00001287/// Execute all of the passes scheduled for execution by invoking
1288/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001289/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001290bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001291 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001292 return false;
1293
Devang Patele9585592006-12-08 01:38:28 +00001294 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001295
Devang Patel6e5a1132006-11-07 21:31:57 +00001296 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001297 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1298 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001299 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001300
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001301 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001302 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001303
Devang Patelabfbe3b2006-12-16 00:56:26 +00001304 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001305
Chris Lattner4c1e9542009-03-06 06:45:05 +00001306 {
1307 // If the pass crashes, remember this.
1308 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001309 TimeRegion PassTimer(getPassTimer(BP));
1310
Dan Gohman74b189f2010-03-01 17:34:28 +00001311 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001312 }
Devang Patel93a197c2006-12-14 00:08:04 +00001313
Dan Gohman74b189f2010-03-01 17:34:28 +00001314 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001315 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001316 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001317 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001318 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001319
Devang Patela273d1c2007-07-19 18:02:32 +00001320 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001321 removeNotPreservedAnalysis(BP);
1322 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001323 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001324 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001325
Bill Wendling6ce6d262009-12-25 13:50:18 +00001326 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001327}
1328
Devang Patel475c4532006-12-08 00:59:05 +00001329// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001330bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001331 bool Changed = false;
1332
Chris Lattner4c1e9542009-03-06 06:45:05 +00001333 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1334 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001335
1336 return Changed;
1337}
1338
Duncan Sands51495602009-02-13 09:42:34 +00001339bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001340 bool Changed = false;
1341
Pedro Artigas41b98842012-12-05 17:12:22 +00001342 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001343 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001344
1345 return Changed;
1346}
1347
Duncan Sands51495602009-02-13 09:42:34 +00001348bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001349 bool Changed = false;
1350
Devang Patelabfbe3b2006-12-16 00:56:26 +00001351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1352 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001353 Changed |= BP->doInitialization(F);
1354 }
1355
1356 return Changed;
1357}
1358
Duncan Sands51495602009-02-13 09:42:34 +00001359bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001360 bool Changed = false;
1361
Devang Patelabfbe3b2006-12-16 00:56:26 +00001362 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1363 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001364 Changed |= BP->doFinalization(F);
1365 }
1366
1367 return Changed;
1368}
1369
1370
Devang Patela1514cb2006-12-07 19:39:39 +00001371//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001372// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001373
Devang Patel4e12f862006-11-08 10:44:40 +00001374/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001375FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001376 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001377 // FPM is the top level manager.
1378 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001379
Dan Gohman565df952008-03-13 02:08:36 +00001380 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001381 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001382}
1383
Devang Patelb67904d2006-12-13 02:36:01 +00001384FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001385 delete FPM;
1386}
1387
Devang Patel4e12f862006-11-08 10:44:40 +00001388/// add - Add a pass to the queue of passes to run. This passes
1389/// ownership of the Pass to the PassManager. When the
1390/// PassManager_X is destroyed, the pass will be destroyed as well, so
1391/// there is no need to delete the pass. (TODO delete passes.)
1392/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001393void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001394 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001395}
1396
Devang Patel9f3083e2006-11-15 19:39:54 +00001397/// run - Execute all of the passes scheduled for execution. Keep
1398/// track of whether any of the passes modifies the function, and if
1399/// so, return true.
1400///
Devang Patelb67904d2006-12-13 02:36:01 +00001401bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001402 if (F.isMaterializable()) {
1403 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001404 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001405 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001406 }
Devang Patel272908d2006-12-08 22:57:48 +00001407 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001408}
1409
1410
Devang Patelff631ae2006-11-15 01:27:05 +00001411/// doInitialization - Run all of the initializers for the function passes.
1412///
Devang Patelb67904d2006-12-13 02:36:01 +00001413bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001414 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001415}
1416
Dan Gohmane6656eb2007-07-30 14:51:13 +00001417/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001418///
Devang Patelb67904d2006-12-13 02:36:01 +00001419bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001420 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001421}
1422
Devang Patela1514cb2006-12-07 19:39:39 +00001423//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424// FunctionPassManagerImpl implementation
1425//
Duncan Sands51495602009-02-13 09:42:34 +00001426bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427 bool Changed = false;
1428
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001429 dumpArguments();
1430 dumpPasses();
1431
Pedro Artigas41b98842012-12-05 17:12:22 +00001432 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1433 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1434 E = IPV.end(); I != E; ++I) {
1435 Changed |= (*I)->doInitialization(M);
1436 }
1437
Chris Lattner4c1e9542009-03-06 06:45:05 +00001438 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1439 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001440
1441 return Changed;
1442}
1443
Duncan Sands51495602009-02-13 09:42:34 +00001444bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001445 bool Changed = false;
1446
Pedro Artigas41b98842012-12-05 17:12:22 +00001447 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001448 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001449
Pedro Artigas41b98842012-12-05 17:12:22 +00001450 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1451 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1452 E = IPV.end(); I != E; ++I) {
1453 Changed |= (*I)->doFinalization(M);
1454 }
1455
Devang Patel67d6a5e2006-12-19 19:46:59 +00001456 return Changed;
1457}
1458
Devang Patelec9c58f2009-04-01 22:34:41 +00001459/// cleanup - After running all passes, clean up pass manager cache.
1460void FPPassManager::cleanup() {
1461 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1462 FunctionPass *FP = getContainedPass(Index);
1463 AnalysisResolver *AR = FP->getResolver();
1464 assert(AR && "Analysis Resolver is not set");
1465 AR->clearAnalysisImpls();
1466 }
1467}
1468
Torok Edwin24c78352009-06-29 18:49:09 +00001469void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1470 if (!wasRun)
1471 return;
1472 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1473 FPPassManager *FPPM = getContainedManager(Index);
1474 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1475 FPPM->getContainedPass(Index)->releaseMemory();
1476 }
1477 }
Torok Edwin896556e2009-06-29 21:05:10 +00001478 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001479}
1480
Devang Patel67d6a5e2006-12-19 19:46:59 +00001481// Execute all the passes managed by this top level manager.
1482// Return true if any function is modified by a pass.
1483bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001484 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001485 TimingInfo::createTheTimeInfo();
1486
Devang Patele3068402006-12-21 00:16:50 +00001487 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001488 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1489 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001490
1491 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1492 getContainedManager(Index)->cleanup();
1493
Torok Edwin24c78352009-06-29 18:49:09 +00001494 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001495 return Changed;
1496}
1497
1498//===----------------------------------------------------------------------===//
1499// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001500
Devang Patel8c78a0b2007-05-03 01:11:54 +00001501char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001502/// Print passes managed by this manager
1503void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001504 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001505 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1506 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001507 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001508 dumpLastUses(FP, Offset+1);
1509 }
1510}
1511
1512
Dan Gohmande6188a2010-08-12 23:50:08 +00001513/// Execute all of the passes scheduled for execution by invoking
1514/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001515/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001516bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001517 if (F.isDeclaration())
1518 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001519
1520 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001521
Devang Patelcbbf2912008-03-20 01:09:53 +00001522 // Collect inherited analysis from Module level pass manager.
1523 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001524
Devang Patelabfbe3b2006-12-16 00:56:26 +00001525 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1526 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001527 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001528
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001529 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001530 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001531
Devang Patelabfbe3b2006-12-16 00:56:26 +00001532 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001533
Chris Lattner4c1e9542009-03-06 06:45:05 +00001534 {
1535 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001536 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001537
Dan Gohman74b189f2010-03-01 17:34:28 +00001538 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001539 }
Devang Patel93a197c2006-12-14 00:08:04 +00001540
Dan Gohman74b189f2010-03-01 17:34:28 +00001541 Changed |= LocalChanged;
1542 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001543 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001544 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001545
Devang Patela273d1c2007-07-19 18:02:32 +00001546 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001547 removeNotPreservedAnalysis(FP);
1548 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001549 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001550 }
1551 return Changed;
1552}
1553
Devang Patel67d6a5e2006-12-19 19:46:59 +00001554bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001555 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001556
Dan Gohmane7630be2010-05-11 20:30:00 +00001557 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001558 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001559
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001560 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001561}
1562
Duncan Sands51495602009-02-13 09:42:34 +00001563bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001564 bool Changed = false;
1565
Chris Lattner4c1e9542009-03-06 06:45:05 +00001566 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1567 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001568
Devang Patelff631ae2006-11-15 01:27:05 +00001569 return Changed;
1570}
1571
Duncan Sands51495602009-02-13 09:42:34 +00001572bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001573 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001574
1575 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001576 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001577
Devang Patelff631ae2006-11-15 01:27:05 +00001578 return Changed;
1579}
1580
Devang Patela1514cb2006-12-07 19:39:39 +00001581//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001582// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001583
Dan Gohmande6188a2010-08-12 23:50:08 +00001584/// Execute all of the passes scheduled for execution by invoking
1585/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001586/// the module, and if so, return true.
1587bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001588MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001589 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001590
Torok Edwin24c78352009-06-29 18:49:09 +00001591 // Initialize on-the-fly passes
1592 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1593 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1594 I != E; ++I) {
1595 FunctionPassManagerImpl *FPP = I->second;
1596 Changed |= FPP->doInitialization(M);
1597 }
1598
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001599 // Initialize module passes
1600 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1601 Changed |= getContainedPass(Index)->doInitialization(M);
1602
Devang Patelabfbe3b2006-12-16 00:56:26 +00001603 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1604 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001605 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001606
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001607 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001608 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001609
Devang Patelabfbe3b2006-12-16 00:56:26 +00001610 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001611
Chris Lattner4c1e9542009-03-06 06:45:05 +00001612 {
1613 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001614 TimeRegion PassTimer(getPassTimer(MP));
1615
Dan Gohman74b189f2010-03-01 17:34:28 +00001616 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001617 }
Devang Patel93a197c2006-12-14 00:08:04 +00001618
Dan Gohman74b189f2010-03-01 17:34:28 +00001619 Changed |= LocalChanged;
1620 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001621 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001622 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001623 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001624
Devang Patela273d1c2007-07-19 18:02:32 +00001625 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001626 removeNotPreservedAnalysis(MP);
1627 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001628 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001629 }
Torok Edwin24c78352009-06-29 18:49:09 +00001630
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001631 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001632 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001633 Changed |= getContainedPass(Index)->doFinalization(M);
1634
Torok Edwin24c78352009-06-29 18:49:09 +00001635 // Finalize on-the-fly passes
1636 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1637 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1638 I != E; ++I) {
1639 FunctionPassManagerImpl *FPP = I->second;
1640 // We don't know when is the last time an on-the-fly pass is run,
1641 // so we need to releaseMemory / finalize here
1642 FPP->releaseMemoryOnTheFly();
1643 Changed |= FPP->doFinalization(M);
1644 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001645
Devang Patel05e1a972006-11-07 22:03:15 +00001646 return Changed;
1647}
1648
Devang Patele64d3052007-04-16 20:12:57 +00001649/// Add RequiredPass into list of lower level passes required by pass P.
1650/// RequiredPass is run on the fly by Pass Manager when P requests it
1651/// through getAnalysis interface.
1652void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001653 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1654 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001655 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001656 RequiredPass->getPotentialPassManagerType()) &&
1657 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001658
Devang Patel68f72b12007-04-26 17:50:19 +00001659 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001660 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001661 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001662 // FPP is the top level manager.
1663 FPP->setTopLevelManager(FPP);
1664
Devang Patel69e9f6d2007-04-16 20:27:05 +00001665 OnTheFlyManagers[P] = FPP;
1666 }
Devang Patel68f72b12007-04-26 17:50:19 +00001667 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001668
Devang Patel68f72b12007-04-26 17:50:19 +00001669 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001670 if (RequiredPass) {
1671 SmallVector<Pass *, 1> LU;
1672 LU.push_back(RequiredPass);
1673 FPP->setLastUser(LU, P);
1674 }
Devang Patele64d3052007-04-16 20:12:57 +00001675}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001676
Dan Gohmande6188a2010-08-12 23:50:08 +00001677/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001678/// required by module pass MP. Instantiate analysis pass, by using
1679/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001680Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001681 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001682 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001683
Torok Edwin24c78352009-06-29 18:49:09 +00001684 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001685 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001686 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001687}
1688
1689
Devang Patela1514cb2006-12-07 19:39:39 +00001690//===----------------------------------------------------------------------===//
1691// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001692
Devang Patelab97cf42006-12-13 00:09:23 +00001693//
Devang Patelc290c8a2006-11-07 22:23:34 +00001694/// run - Execute all of the passes scheduled for execution. Keep track of
1695/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001696bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001697 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001698 TimingInfo::createTheTimeInfo();
1699
Devang Patelcfd70c42006-12-13 22:10:00 +00001700 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001701 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001702
Pedro Artigas41b98842012-12-05 17:12:22 +00001703 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1704 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1705 E = IPV.end(); I != E; ++I) {
1706 Changed |= (*I)->doInitialization(M);
1707 }
1708
Devang Patele3068402006-12-21 00:16:50 +00001709 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001710 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1711 Changed |= getContainedManager(Index)->runOnModule(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001712
1713 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1714 E = IPV.end(); I != E; ++I) {
1715 Changed |= (*I)->doFinalization(M);
1716 }
1717
Devang Patelc290c8a2006-11-07 22:23:34 +00001718 return Changed;
1719}
Devang Patel376fefa2006-11-08 10:29:57 +00001720
Devang Patela1514cb2006-12-07 19:39:39 +00001721//===----------------------------------------------------------------------===//
1722// PassManager implementation
1723
Devang Patel376fefa2006-11-08 10:29:57 +00001724/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001725PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001726 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001727 // PM is the top level manager
1728 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001729}
1730
Devang Patelb67904d2006-12-13 02:36:01 +00001731PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001732 delete PM;
1733}
1734
Devang Patel376fefa2006-11-08 10:29:57 +00001735/// add - Add a pass to the queue of passes to run. This passes ownership of
1736/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1737/// will be destroyed as well, so there is no need to delete the pass. This
1738/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001739void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001740 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001741}
1742
1743/// run - Execute all of the passes scheduled for execution. Keep track of
1744/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001745bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001746 return PM->run(M);
1747}
1748
Devang Patelb8817b92006-12-14 00:59:42 +00001749//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001750// TimingInfo implementation
1751
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001752bool llvm::TimePassesIsEnabled = false;
1753static cl::opt<bool,true>
1754EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1755 cl::desc("Time each pass, printing elapsed time for each on exit"));
1756
Devang Patelb8817b92006-12-14 00:59:42 +00001757// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1758// a non null value (if the -time-passes option is enabled) or it leaves it
1759// null. It may be called multiple times.
1760void TimingInfo::createTheTimeInfo() {
1761 if (!TimePassesIsEnabled || TheTimeInfo) return;
1762
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001763 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001764 // This guarantees that the object will be constructed before static globals,
1765 // thus it will be destroyed before them.
1766 static ManagedStatic<TimingInfo> TTI;
1767 TheTimeInfo = &*TTI;
1768}
1769
Devang Patel1c3633e2007-01-29 23:10:37 +00001770/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001771Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001772 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001773 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001774 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001775}
1776
Devang Patel1c56a632007-01-08 19:29:38 +00001777//===----------------------------------------------------------------------===//
1778// PMStack implementation
1779//
Devang Patelad98d232007-01-11 22:15:30 +00001780
Devang Patel1c56a632007-01-08 19:29:38 +00001781// Pop Pass Manager from the stack and clear its analysis info.
1782void PMStack::pop() {
1783
1784 PMDataManager *Top = this->top();
1785 Top->initializeAnalysisInfo();
1786
1787 S.pop_back();
1788}
1789
1790// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001791void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001792 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001793 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001794
Chris Lattner60987362009-03-06 05:53:14 +00001795 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001796 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1797 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001798 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001799
Chris Lattner60987362009-03-06 05:53:14 +00001800 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001801 TPM->addIndirectPassManager(PM);
1802 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001803 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001804 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001805 assert((PM->getPassManagerType() == PMT_ModulePassManager
1806 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001807 && "pushing bad pass manager to PMStack");
1808 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001809 }
1810
Devang Patel15701b52007-01-11 00:19:00 +00001811 S.push_back(PM);
1812}
1813
1814// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001815void PMStack::dump() const {
1816 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001817 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001818 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001819
Devang Patel15701b52007-01-11 00:19:00 +00001820 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001821 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001822}
1823
Devang Patel1c56a632007-01-08 19:29:38 +00001824/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001825/// add self into that manager.
1826void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001827 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001828 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001829 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001830 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1831 if (TopPMType == PreferredType)
1832 break; // We found desired pass manager
1833 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001834 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001835 else
1836 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001837 }
Devang Patel18ff6362008-09-09 21:38:40 +00001838 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001839 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001840}
1841
Devang Patel3312f752007-01-16 21:43:18 +00001842/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001843/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001844void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001845 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001846
Andrew Trickcbc845f2012-02-01 07:16:20 +00001847 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001848 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001849 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1850 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001851 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001852 break;
Devang Patel3312f752007-01-16 21:43:18 +00001853 }
Devang Patel3312f752007-01-16 21:43:18 +00001854
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001855 // Create new Function Pass Manager if needed.
1856 FPPassManager *FPP;
1857 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1858 FPP = (FPPassManager *)PMS.top();
1859 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001860 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1861 PMDataManager *PMD = PMS.top();
1862
1863 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001864 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001865 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001866
1867 // [2] Set up new manager's top level manager
1868 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1869 TPM->addIndirectPassManager(FPP);
1870
1871 // [3] Assign manager to manage this new manager. This may create
1872 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001873 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001874
1875 // [4] Push new manager into PMS
1876 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001877 }
1878
Devang Patel3312f752007-01-16 21:43:18 +00001879 // Assign FPP as the manager of this pass.
1880 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001881}
1882
Devang Patel3312f752007-01-16 21:43:18 +00001883/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001884/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001885void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001886 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001887 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001888
Devang Patel15701b52007-01-11 00:19:00 +00001889 // Basic Pass Manager is a leaf pass manager. It does not handle
1890 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001891 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001892 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1893 BBP = (BBPassManager *)PMS.top();
1894 } else {
1895 // If leaf manager is not Basic Block Pass manager then create new
1896 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001897 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1898 PMDataManager *PMD = PMS.top();
1899
1900 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001901 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001902
1903 // [2] Set up new manager's top level manager
1904 // Basic Block Pass Manager does not live by itself
1905 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1906 TPM->addIndirectPassManager(BBP);
1907
Devang Patel15701b52007-01-11 00:19:00 +00001908 // [3] Assign manager to manage this new manager. This may create
1909 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001910 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001911
Devang Patel3312f752007-01-16 21:43:18 +00001912 // [4] Push new manager into PMS
1913 PMS.push(BBP);
1914 }
Devang Patel1c56a632007-01-08 19:29:38 +00001915
Devang Patel3312f752007-01-16 21:43:18 +00001916 // Assign BBP as the manager of this pass.
1917 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001918}
1919
Dan Gohmand3a20c92008-03-11 16:41:42 +00001920PassManagerBase::~PassManagerBase() {}