blob: 875ab5cad7dca9bdb898d8d764c919680b036518 [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
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//
Dan Gohmande6188a2010-08-12 23:50:08 +000010// This file implements the LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
David Greene9b063df2010-04-02 23:17:14 +000016#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000017#include "llvm/Assembly/Writer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Module.h"
19#include "llvm/PassManager.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;
Devang Patelffca9102006-12-15 19:39:30 +000031
Devang Patele7599552007-01-12 18:52:44 +000032// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000033
Devang Patelf1567a52006-12-13 20:03:48 +000034namespace llvm {
35
36//===----------------------------------------------------------------------===//
37// Pass debugging information. Often it is useful to find out what pass is
38// running when a crash occurs in a utility. When this library is compiled with
39// debugging on, a command line option (--debug-pass) is enabled that causes the
40// pass name to be printed before it executes.
41//
42
Devang Patel03fb5872006-12-13 21:13:31 +000043// Different debug levels that can be enabled...
44enum PassDebugLevel {
45 None, Arguments, Structure, Executions, Details
46};
47
Devang Patelf1567a52006-12-13 20:03:48 +000048static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000049PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000050 cl::desc("Print PassManager debugging information"),
51 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000052 clEnumVal(None , "disable debug output"),
53 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
54 clEnumVal(Structure , "print pass structure before run()"),
55 clEnumVal(Executions, "print pass name before it is executed"),
56 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000057 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000058
59typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
60PassOptionList;
61
62// Print IR out before/after specified passes.
63static PassOptionList
64PrintBefore("print-before",
Eric Christopher787ba0b2011-03-09 19:46:51 +000065 llvm::cl::desc("Print IR before specified passes"),
66 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000067
68static PassOptionList
69PrintAfter("print-after",
Eric Christopher787ba0b2011-03-09 19:46:51 +000070 llvm::cl::desc("Print IR after specified passes"),
71 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000072
73static cl::opt<bool>
74PrintBeforeAll("print-before-all",
75 llvm::cl::desc("Print IR before each pass"),
76 cl::init(false));
77static cl::opt<bool>
78PrintAfterAll("print-after-all",
79 llvm::cl::desc("Print IR after each pass"),
80 cl::init(false));
81
82/// This is a helper to determine whether to print IR before or
83/// after a pass.
84
Andrew Trickcbc845f2012-02-01 07:16:20 +000085static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
David Greene9b063df2010-04-02 23:17:14 +000086 PassOptionList &PassesToPrint) {
Andrew Trickcbc845f2012-02-01 07:16:20 +000087 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
88 const llvm::PassInfo *PassInf = PassesToPrint[i];
89 if (PassInf)
90 if (PassInf->getPassArgument() == PI->getPassArgument()) {
91 return true;
92 }
David Greene9b063df2010-04-02 23:17:14 +000093 }
94 return false;
95}
Dan Gohmande6188a2010-08-12 23:50:08 +000096
David Greene9b063df2010-04-02 23:17:14 +000097/// This is a utility to check whether a pass should have IR dumped
98/// before it.
Andrew Trickcbc845f2012-02-01 07:16:20 +000099static bool ShouldPrintBeforePass(const PassInfo *PI) {
100 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000101}
102
103/// This is a utility to check whether a pass should have IR dumped
104/// after it.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000105static bool ShouldPrintAfterPass(const PassInfo *PI) {
106 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000107}
108
Devang Patelf1567a52006-12-13 20:03:48 +0000109} // End of llvm namespace
110
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000111/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
112/// or higher is specified.
113bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
114 return PassDebugging >= Executions;
115}
116
117
118
119
Chris Lattner4c1e9542009-03-06 06:45:05 +0000120void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
121 if (V == 0 && M == 0)
122 OS << "Releasing pass '";
123 else
124 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000125
Chris Lattner4c1e9542009-03-06 06:45:05 +0000126 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000127
Chris Lattner4c1e9542009-03-06 06:45:05 +0000128 if (M) {
129 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
130 return;
131 }
132 if (V == 0) {
133 OS << '\n';
134 return;
135 }
136
Dan Gohman79fc0e92009-03-10 18:47:59 +0000137 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000138 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000139 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000141 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << "value";
144
145 OS << " '";
146 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
147 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000148}
149
150
Devang Patelffca9102006-12-15 19:39:30 +0000151namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000152
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;
Devang Patele7599552007-01-12 18:52:44 +0000209}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210
Devang Patele7599552007-01-12 18:52:44 +0000211namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000212
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;
Dan Gohmande6188a2010-08-12 23:50:08 +0000280
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281//===----------------------------------------------------------------------===//
282// MPPassManager
283//
284/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000285/// It batches all Module passes and function pass managers together and
286/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000287class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000288public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000289 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000290 explicit MPPassManager() :
291 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000292
293 // Delete on the fly managers.
294 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000295 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000296 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
297 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000298 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000299 delete FPP;
300 }
301 }
302
Dan Gohmande6188a2010-08-12 23:50:08 +0000303 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000304 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
305 return createPrintModulePass(&O, false, Banner);
306 }
307
Devang Patelca58e352006-11-08 10:05:38 +0000308 /// run - Execute all of the passes scheduled for execution. Keep track of
309 /// whether any of the passes modifies the module, and if so, return true.
310 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000311
Pedro Artigase4348b02012-12-03 21:56:57 +0000312 using llvm::Pass::doInitialization;
313 using llvm::Pass::doFinalization;
314
Owen Anderson1aa27512012-11-15 00:14:15 +0000315 /// doInitialization - Run all of the initializers for the module passes.
316 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000317 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000318
319 /// doFinalization - Run all of the finalizers for the module passes.
320 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000321 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000322
Devang Patelf9d96b92006-12-07 19:57:52 +0000323 /// Pass Manager itself does not invalidate any analysis info.
324 void getAnalysisUsage(AnalysisUsage &Info) const {
325 Info.setPreservesAll();
326 }
327
Devang Patele64d3052007-04-16 20:12:57 +0000328 /// Add RequiredPass into list of lower level passes required by pass P.
329 /// RequiredPass is run on the fly by Pass Manager when P requests it
330 /// through getAnalysis interface.
331 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
332
Dan Gohmande6188a2010-08-12 23:50:08 +0000333 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000334 /// required by module pass MP. Instantiate analysis pass, by using
335 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000336 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000337
Devang Patele3858e62007-02-01 22:08:25 +0000338 virtual const char *getPassName() const {
339 return "Module Pass Manager";
340 }
341
Chris Lattner2fa26e52010-01-22 05:24:46 +0000342 virtual PMDataManager *getAsPMDataManager() { return this; }
343 virtual Pass *getAsPass() { return this; }
344
Devang Pateleda56172006-12-12 23:34:33 +0000345 // Print passes managed by this manager
346 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000347 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
349 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000350 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000351 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
352 OnTheFlyManagers.find(MP);
353 if (I != OnTheFlyManagers.end())
354 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000355 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000356 }
357 }
358
Devang Patelabfbe3b2006-12-16 00:56:26 +0000359 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000360 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000361 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 }
363
Dan Gohmande6188a2010-08-12 23:50:08 +0000364 virtual PassManagerType getPassManagerType() const {
365 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000366 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000367
368 private:
369 /// Collection of on the fly FPPassManagers. These managers manage
370 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000371 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000372};
373
Devang Patel8c78a0b2007-05-03 01:11:54 +0000374char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000375//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000376// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000377//
Devang Patel09f162c2007-05-01 21:15:47 +0000378
Devang Patel67d6a5e2006-12-19 19:46:59 +0000379/// PassManagerImpl manages MPPassManagers
380class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000381 public PMDataManager,
382 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000383 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000384
385public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000386 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000387 explicit PassManagerImpl() :
388 Pass(PT_PassManager, ID), PMDataManager(),
389 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000390
Devang Patel376fefa2006-11-08 10:29:57 +0000391 /// add - Add a pass to the queue of passes to run. This passes ownership of
392 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
393 /// will be destroyed as well, so there is no need to delete the pass. This
394 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000395 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000396 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000397 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000398
399 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000400 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
401 return createPrintModulePass(&O, false, Banner);
402 }
403
Devang Patel376fefa2006-11-08 10:29:57 +0000404 /// run - Execute all of the passes scheduled for execution. Keep track of
405 /// whether any of the passes modifies the module, and if so, return true.
406 bool run(Module &M);
407
Pedro Artigase4348b02012-12-03 21:56:57 +0000408 using llvm::Pass::doInitialization;
409 using llvm::Pass::doFinalization;
410
Owen Anderson1aa27512012-11-15 00:14:15 +0000411 /// doInitialization - Run all of the initializers for the module passes.
412 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000413 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000414
415 /// doFinalization - Run all of the finalizers for the module passes.
416 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000417 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000418
Devang Patelf9d96b92006-12-07 19:57:52 +0000419 /// Pass Manager itself does not invalidate any analysis info.
420 void getAnalysisUsage(AnalysisUsage &Info) const {
421 Info.setPreservesAll();
422 }
423
Chris Lattner2fa26e52010-01-22 05:24:46 +0000424 virtual PMDataManager *getAsPMDataManager() { return this; }
425 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000426 virtual PassManagerType getTopLevelPassManagerType() {
427 return PMT_ModulePassManager;
428 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000429
Devang Patel67d6a5e2006-12-19 19:46:59 +0000430 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000431 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000432 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
433 return MP;
434 }
Devang Patel376fefa2006-11-08 10:29:57 +0000435};
436
David Blaikiea379b1812011-12-20 02:50:00 +0000437void PassManagerImpl::anchor() {}
438
Devang Patel8c78a0b2007-05-03 01:11:54 +0000439char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000440} // End of llvm namespace
441
442namespace {
443
444//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000445/// TimingInfo Class - This class is used to calculate information about the
446/// amount of time each pass takes to execute. This only happens when
447/// -time-passes is enabled on the command line.
448///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000449
Owen Anderson5a6960f2009-06-18 20:51:00 +0000450static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000451
Nick Lewycky02d5f772009-10-25 06:33:48 +0000452class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000453 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000454 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000455public:
456 // Use 'create' member to get this.
457 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000458
Devang Patel1c3633e2007-01-29 23:10:37 +0000459 // TimingDtor - Print out information about timing information
460 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000461 // Delete all of the timers, which accumulate their info into the
462 // TimerGroup.
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000463 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattner707431c2010-03-30 04:03:22 +0000464 E = TimingData.end(); I != E; ++I)
465 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000466 // TimerGroup is deleted next, printing the report.
467 }
468
469 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
470 // to a non null value (if the -time-passes option is enabled) or it leaves it
471 // null. It may be called multiple times.
472 static void createTheTimeInfo();
473
Chris Lattner707431c2010-03-30 04:03:22 +0000474 /// getPassTimer - Return the timer for the specified pass if it exists.
475 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000476 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000477 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000478
Owen Anderson5c96ef72009-07-07 18:33:04 +0000479 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000480 Timer *&T = TimingData[P];
Chris Lattner707431c2010-03-30 04:03:22 +0000481 if (T == 0)
482 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000483 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000484 }
485};
486
Devang Patel1c3633e2007-01-29 23:10:37 +0000487} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000488
Dan Gohmand78c4002008-05-13 00:00:25 +0000489static TimingInfo *TheTimeInfo;
490
Devang Patela1514cb2006-12-07 19:39:39 +0000491//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000492// PMTopLevelManager implementation
493
Devang Patel4268fc02007-01-16 02:00:38 +0000494/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000495PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
496 PMDM->setTopLevelManager(this);
497 addPassManager(PMDM);
498 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000499}
500
Devang Patelafb1f3622006-12-12 22:35:25 +0000501/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000502void
Bill Wendlingea857e12012-05-14 07:53:40 +0000503PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000504 unsigned PDepth = 0;
505 if (P->getResolver())
506 PDepth = P->getResolver()->getPMDataManager().getDepth();
507
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000508 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000509 E = AnalysisPasses.end(); I != E; ++I) {
510 Pass *AP = *I;
511 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000512
Devang Patel01919d22007-03-08 19:05:01 +0000513 if (P == AP)
514 continue;
515
Tobias Grosserf07426b2011-01-20 21:03:22 +0000516 // Update the last users of passes that are required transitive by AP.
517 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
518 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
519 SmallVector<Pass *, 12> LastUses;
520 SmallVector<Pass *, 12> LastPMUses;
521 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
522 E = IDs.end(); I != E; ++I) {
523 Pass *AnalysisPass = findAnalysisPass(*I);
524 assert(AnalysisPass && "Expected analysis pass to exist.");
525 AnalysisResolver *AR = AnalysisPass->getResolver();
526 assert(AR && "Expected analysis resolver to exist.");
527 unsigned APDepth = AR->getPMDataManager().getDepth();
528
529 if (PDepth == APDepth)
530 LastUses.push_back(AnalysisPass);
531 else if (PDepth > APDepth)
532 LastPMUses.push_back(AnalysisPass);
533 }
534
535 setLastUser(LastUses, P);
536
537 // If this pass has a corresponding pass manager, push higher level
538 // analysis to this pass manager.
539 if (P->getResolver())
540 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
541
542
Devang Patelafb1f3622006-12-12 22:35:25 +0000543 // If AP is the last user of other passes then make P last user of
544 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000545 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000546 LUE = LastUser.end(); LUI != LUE; ++LUI) {
547 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000548 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000549 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000550 LastUser[LUI->first] = P;
551 }
552 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000553}
554
555/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000556void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000557 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000558 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000559 InversedLastUser.find(P);
560 if (DMI == InversedLastUser.end())
561 return;
562
563 SmallPtrSet<Pass *, 8> &LU = DMI->second;
564 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
565 E = LU.end(); I != E; ++I) {
566 LastUses.push_back(*I);
567 }
568
Devang Patelafb1f3622006-12-12 22:35:25 +0000569}
570
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000571AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
572 AnalysisUsage *AnUsage = NULL;
573 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000574 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000575 AnUsage = DMI->second;
576 else {
577 AnUsage = new AnalysisUsage();
578 P->getAnalysisUsage(*AnUsage);
579 AnUsageMap[P] = AnUsage;
580 }
581 return AnUsage;
582}
583
Devang Patelafb1f3622006-12-12 22:35:25 +0000584/// Schedule pass P for execution. Make sure that passes required by
585/// P are run before P is run. Update analysis info maintained by
586/// the manager. Remove dead passes. This is a recursive function.
587void PMTopLevelManager::schedulePass(Pass *P) {
588
Devang Patel3312f752007-01-16 21:43:18 +0000589 // TODO : Allocate function manager for this pass, other wise required set
590 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000591
Devang Pateld74ede72007-03-06 01:06:16 +0000592 // Give pass a chance to prepare the stage.
593 P->preparePassManager(activeStack);
594
Devang Patel864970e2008-03-18 00:39:19 +0000595 // If P is an analysis pass and it is available then do not
596 // generate the analysis again. Stale analysis info should not be
597 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000598 const PassInfo *PI =
599 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
600 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000601 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000602 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000603 }
Devang Patel864970e2008-03-18 00:39:19 +0000604
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000605 AnalysisUsage *AnUsage = findAnalysisUsage(P);
606
Devang Patelfdee7032008-08-14 23:07:48 +0000607 bool checkAnalysis = true;
608 while (checkAnalysis) {
609 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000610
Devang Patelfdee7032008-08-14 23:07:48 +0000611 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
612 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
613 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000614
Devang Patelfdee7032008-08-14 23:07:48 +0000615 Pass *AnalysisPass = findAnalysisPass(*I);
616 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000617 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000618
619 if (PI == NULL) {
620 // Pass P is not in the global PassRegistry
621 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
622 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
623 dbgs() << "Required Passes:" << "\n";
624 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
625 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
626 Pass *AnalysisPass2 = findAnalysisPass(*I2);
627 if (AnalysisPass2) {
628 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
629 }
630 else {
631 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
632 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
633 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
634 }
635 }
636 }
637
Andrew Trick6bbaf132011-06-03 00:48:58 +0000638 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000639 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000640 if (P->getPotentialPassManagerType () ==
641 AnalysisPass->getPotentialPassManagerType())
642 // Schedule analysis pass that is managed by the same pass manager.
643 schedulePass(AnalysisPass);
644 else if (P->getPotentialPassManagerType () >
645 AnalysisPass->getPotentialPassManagerType()) {
646 // Schedule analysis pass that is managed by a new manager.
647 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000648 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000649 // are already checked are still available.
650 checkAnalysis = true;
651 }
652 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000653 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000654 // passes are run on the fly.
655 delete AnalysisPass;
656 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000657 }
658 }
659
660 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000661 if (ImmutablePass *IP = P->getAsImmutablePass()) {
662 // P is a immutable pass and it will be managed by this
663 // top level manager. Set up analysis resolver to connect them.
664 PMDataManager *DM = getAsPMDataManager();
665 AnalysisResolver *AR = new AnalysisResolver(*DM);
666 P->setResolver(AR);
667 DM->initializeAnalysisImpl(P);
668 addImmutablePass(IP);
669 DM->recordAvailableAnalysis(IP);
670 return;
671 }
672
673 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
674 Pass *PP = P->createPrinterPass(
675 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
676 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
677 }
678
679 // Add the requested pass to the best available pass manager.
680 P->assignPassManager(activeStack, getTopLevelPassManagerType());
681
682 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
683 Pass *PP = P->createPrinterPass(
684 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
685 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
686 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000687}
688
689/// Find the pass that implements Analysis AID. Search immutable
690/// passes and all pass managers. If desired pass is not found
691/// then return NULL.
692Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
693
Devang Patelcd6ba152006-12-12 22:50:05 +0000694 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000695 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000696 E = PassManagers.end(); I != E; ++I)
697 if (Pass *P = (*I)->findAnalysisPass(AID, false))
698 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000699
700 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000701 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000702 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000703 E = IndirectPassManagers.end(); I != E; ++I)
704 if (Pass *P = (*I)->findAnalysisPass(AID, false))
705 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000706
Dan Gohman844dd0a2010-10-11 23:19:01 +0000707 // Check the immutable passes. Iterate in reverse order so that we find
708 // the most recently registered passes first.
709 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
710 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000711 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000712 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000713 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000714
715 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000716 const PassInfo *PassInf =
717 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000718 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000719 const std::vector<const PassInfo*> &ImmPI =
720 PassInf->getInterfacesImplemented();
721 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
722 EE = ImmPI.end(); II != EE; ++II) {
723 if ((*II)->getTypeInfo() == AID)
724 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000725 }
726 }
727
Dan Gohman844dd0a2010-10-11 23:19:01 +0000728 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000729}
730
Devang Pateleda56172006-12-12 23:34:33 +0000731// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000732void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000733
Devang Patelfd4184322007-01-17 20:33:36 +0000734 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000735 return;
736
Devang Pateleda56172006-12-12 23:34:33 +0000737 // Print out the immutable passes
738 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000739 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000740 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000741
Dan Gohmanf71c5212010-08-19 01:29:07 +0000742 // Every class that derives from PMDataManager also derives from Pass
743 // (sometimes indirectly), but there's no inheritance relationship
744 // between PMDataManager and Pass, so we have to getAsPass to get
745 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000746 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000747 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000748 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000749}
750
Devang Patel991aeba2006-12-15 20:13:01 +0000751void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000752
Devang Patelfd4184322007-01-17 20:33:36 +0000753 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000754 return;
755
David Greene994e1bb2010-01-05 01:30:02 +0000756 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000757 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
758 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
759 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000760 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
761 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000762 if (!PI->isAnalysisGroup())
763 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000764 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000765 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000766 E = PassManagers.end(); I != E; ++I)
767 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000768 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000769}
770
Devang Patele3068402006-12-21 00:16:50 +0000771void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000772 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000773 E = PassManagers.end(); I != E; ++I)
774 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000775
Devang Patele3068402006-12-21 00:16:50 +0000776 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000777 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000778 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
779 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000780 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000781
Chris Lattner60987362009-03-06 05:53:14 +0000782 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000783 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000784 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000785 InversedLastUser.find(DMI->second);
786 if (InvDMI != InversedLastUser.end()) {
787 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
788 L.insert(DMI->first);
789 } else {
790 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
791 InversedLastUser[DMI->second] = L;
792 }
793 }
Devang Patele3068402006-12-21 00:16:50 +0000794}
795
Devang Patele7599552007-01-12 18:52:44 +0000796/// Destructor
797PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000798 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000799 E = PassManagers.end(); I != E; ++I)
800 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000801
Dan Gohman060d5ba2010-10-12 00:15:27 +0000802 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000803 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
804 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000805
806 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000807 DME = AnUsageMap.end(); DMI != DME; ++DMI)
808 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000809}
810
Devang Patelafb1f3622006-12-12 22:35:25 +0000811//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000812// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000813
Devang Patel643676c2006-11-11 01:10:19 +0000814/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000815void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000816 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000817
Chris Lattner60987362009-03-06 05:53:14 +0000818 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000819
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000820 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000821
Dan Gohmande6188a2010-08-12 23:50:08 +0000822 // This pass is the current implementation of all of the interfaces it
823 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000824 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
825 if (PInf == 0) return;
826 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000827 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000828 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000829}
830
Devang Patel9d9fc902007-03-06 17:52:53 +0000831// Return true if P preserves high level analysis used by other
832// passes managed by this manager
833bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000834 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000835 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000836 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000837
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000838 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000839 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000840 E = HigherLevelAnalysis.end(); I != E; ++I) {
841 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000842 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000843 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000844 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000845 PreservedSet.end())
846 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000847 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000848
Devang Patel9d9fc902007-03-06 17:52:53 +0000849 return true;
850}
851
Chris Lattner02eb94c2008-08-07 07:34:50 +0000852/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000853void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000854 // Don't do this unless assertions are enabled.
855#ifdef NDEBUG
856 return;
857#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000858 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
859 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000860
Devang Patelef432532007-07-19 05:36:09 +0000861 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000862 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000863 E = PreservedSet.end(); I != E; ++I) {
864 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000865 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000866 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000867 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000868 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000869 }
870}
871
Devang Patel67c79a42008-07-01 19:50:56 +0000872/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000873void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000874 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
875 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000876 return;
877
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000878 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000879 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000880 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000881 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000882 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000883 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000884 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000885 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000886 if (PassDebugging >= Details) {
887 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000888 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
889 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000890 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000891 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000892 }
Devang Patel349170f2006-11-11 01:24:55 +0000893 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000894
Devang Patel42dd1e92007-03-06 01:55:46 +0000895 // Check inherited analysis also. If P is not preserving analysis
896 // provided by parent manager then remove it here.
897 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
898
899 if (!InheritedAnalysis[Index])
900 continue;
901
Dan Gohmande6188a2010-08-12 23:50:08 +0000902 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000903 I = InheritedAnalysis[Index]->begin(),
904 E = InheritedAnalysis[Index]->end(); I != E; ) {
905 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000906 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000907 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000908 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000909 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000910 if (PassDebugging >= Details) {
911 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000912 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
913 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000914 }
Devang Patel01919d22007-03-08 19:05:01 +0000915 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000916 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000917 }
918 }
Devang Patelf68a3492006-11-07 22:35:17 +0000919}
920
Devang Patelca189262006-11-14 03:05:08 +0000921/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000922void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000923 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000924
Devang Patel8adae862007-07-20 18:04:54 +0000925 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000926
Devang Patel2ff44922007-04-16 20:39:59 +0000927 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000928 if (!TPM)
929 return;
930
Devang Patel17ad0962006-12-08 00:37:52 +0000931 TPM->collectLastUses(DeadPasses, P);
932
Devang Patel656a9172008-06-06 17:50:36 +0000933 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000934 dbgs() << " -*- '" << P->getPassName();
935 dbgs() << "' is the last user of following pass instances.";
936 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000937 }
938
Dan Gohman060d5ba2010-10-12 00:15:27 +0000939 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000940 E = DeadPasses.end(); I != E; ++I)
941 freePass(*I, Msg, DBG_STR);
942}
Devang Patel200d3052006-12-13 23:50:44 +0000943
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000944void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000945 enum PassDebuggingString DBG_STR) {
946 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000947
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000948 {
949 // If the pass crashes releasing memory, remember this.
950 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000951 TimeRegion PassTimer(getPassTimer(P));
952
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000954 }
955
Owen Andersona7aed182010-08-06 18:33:48 +0000956 AnalysisID PI = P->getPassID();
957 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000958 // Remove the pass itself (if it is not already removed).
959 AvailableAnalysis.erase(PI);
960
961 // Remove all interfaces this pass implements, for which it is also
962 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000963 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000964 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000965 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000966 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000967 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000968 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000969 }
Devang Patel17ad0962006-12-08 00:37:52 +0000970 }
Devang Patelca189262006-11-14 03:05:08 +0000971}
972
Dan Gohmande6188a2010-08-12 23:50:08 +0000973/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000974/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000975void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000976 // This manager is going to manage pass P. Set up analysis resolver
977 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000978 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000979 P->setResolver(AR);
980
Devang Patelec2b9a72007-03-05 22:57:49 +0000981 // If a FunctionPass F is the last user of ModulePass info M
982 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000983 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000984
Chris Lattner60987362009-03-06 05:53:14 +0000985 if (!ProcessAnalysis) {
986 // Add pass
987 PassVector.push_back(P);
988 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000989 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000990
Chris Lattner60987362009-03-06 05:53:14 +0000991 // At the moment, this pass is the last user of all required passes.
992 SmallVector<Pass *, 12> LastUses;
993 SmallVector<Pass *, 8> RequiredPasses;
994 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
995
996 unsigned PDepth = this->getDepth();
997
Dan Gohmande6188a2010-08-12 23:50:08 +0000998 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000999 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +00001000 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001001 E = RequiredPasses.end(); I != E; ++I) {
1002 Pass *PRequired = *I;
1003 unsigned RDepth = 0;
1004
1005 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1006 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1007 RDepth = DM.getDepth();
1008
1009 if (PDepth == RDepth)
1010 LastUses.push_back(PRequired);
1011 else if (PDepth > RDepth) {
1012 // Let the parent claim responsibility of last use
1013 TransferLastUses.push_back(PRequired);
1014 // Keep track of higher level analysis used by this manager.
1015 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001016 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001017 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001018 }
1019
1020 // Set P as P's last user until someone starts using P.
1021 // However, if P is a Pass Manager then it does not need
1022 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001023 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001024 LastUses.push_back(P);
1025 TPM->setLastUser(LastUses, P);
1026
1027 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001028 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001029 TPM->setLastUser(TransferLastUses, My_PM);
1030 TransferLastUses.clear();
1031 }
1032
Dan Gohman6304db32010-08-16 22:57:28 +00001033 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001034 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001035 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001036 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001037 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1038 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001039 this->addLowerLevelRequiredPass(P, AnalysisPass);
1040 }
1041
1042 // Take a note of analysis required and made available by this pass.
1043 // Remove the analysis not preserved by this pass
1044 removeNotPreservedAnalysis(P);
1045 recordAvailableAnalysis(P);
1046
Devang Patel8cad70d2006-11-11 01:51:02 +00001047 // Add pass
1048 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001049}
1050
Devang Patele64d3052007-04-16 20:12:57 +00001051
1052/// Populate RP with analysis pass that are required by
1053/// pass P and are available. Populate RP_NotAvail with analysis
1054/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001055void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1056 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001057 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001058 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1059 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001060 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001061 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001062 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001063 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001064 else
Chris Lattner60987362009-03-06 05:53:14 +00001065 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001066 }
Devang Patelf58183d2006-12-12 23:09:32 +00001067
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001068 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001069 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001070 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001071 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001072 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001073 else
Chris Lattner60987362009-03-06 05:53:14 +00001074 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001075 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001076}
1077
Devang Patel07f4f582006-11-14 21:49:36 +00001078// All Required analyses should be available to the pass as it runs! Here
1079// we fill in the AnalysisImpls member of the pass so that it can
1080// successfully use the getAnalysis() method to retrieve the
1081// implementations it needs.
1082//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001083void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001084 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1085
Chris Lattnercbd160f2008-08-08 05:33:04 +00001086 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001087 I = AnUsage->getRequiredSet().begin(),
1088 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001089 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001090 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001091 // This may be analysis pass that is initialized on the fly.
1092 // If that is not the case then it will raise an assert when it is used.
1093 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001094 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001095 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001096 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001097 }
1098}
1099
Devang Patel640c5bb2006-12-08 22:30:11 +00001100/// Find the pass that implements Analysis AID. If desired pass is not found
1101/// then return NULL.
1102Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1103
1104 // Check if AvailableAnalysis map has one entry.
1105 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1106
1107 if (I != AvailableAnalysis.end())
1108 return I->second;
1109
1110 // Search Parents through TopLevelManager
1111 if (SearchParent)
1112 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001113
Devang Patel9d759b82006-12-09 00:09:12 +00001114 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001115}
1116
Devang Patel991aeba2006-12-15 20:13:01 +00001117// Print list of passes that are last used by P.
1118void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1119
Devang Patel8adae862007-07-20 18:04:54 +00001120 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001121
1122 // If this is a on the fly manager then it does not have TPM.
1123 if (!TPM)
1124 return;
1125
Devang Patel991aeba2006-12-15 20:13:01 +00001126 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001127
Dan Gohman7224bce2010-10-12 00:11:18 +00001128 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001129 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001130 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001131 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001132 }
1133}
1134
1135void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001136 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001137 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001138 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001139 PMD->dumpPassArguments();
1140 else
Owen Andersona7aed182010-08-06 18:33:48 +00001141 if (const PassInfo *PI =
1142 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001143 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001144 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001145 }
1146}
1147
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001148void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1149 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001150 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001151 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001152 return;
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001154 switch (S1) {
1155 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
1158 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 default:
1165 break;
1166 }
1167 switch (S2) {
1168 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001169 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001170 break;
1171 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001172 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001173 break;
1174 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001175 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001176 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001177 case ON_REGION_MSG:
1178 dbgs() << "' on Region '" << Msg << "'...\n";
1179 break;
Devang Patel003a5592007-03-05 20:01:30 +00001180 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001181 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001182 break;
1183 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001184 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001185 break;
1186 default:
1187 break;
1188 }
Devang Patel991aeba2006-12-15 20:13:01 +00001189}
1190
Chris Lattner4c1e9542009-03-06 06:45:05 +00001191void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001192 if (PassDebugging < Details)
1193 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001194
Chris Lattner4c493d92008-08-08 15:14:09 +00001195 AnalysisUsage analysisUsage;
1196 P->getAnalysisUsage(analysisUsage);
1197 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1198}
1199
Chris Lattner4c1e9542009-03-06 06:45:05 +00001200void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001201 if (PassDebugging < Details)
1202 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001203
Chris Lattner4c493d92008-08-08 15:14:09 +00001204 AnalysisUsage analysisUsage;
1205 P->getAnalysisUsage(analysisUsage);
1206 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1207}
1208
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001209void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001210 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001211 assert(PassDebugging >= Details);
1212 if (Set.empty())
1213 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001214 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001215 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001216 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001217 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001218 if (!PInf) {
1219 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1220 // all drivers.
1221 dbgs() << " Uninitialized Pass";
1222 continue;
1223 }
Owen Andersona7aed182010-08-06 18:33:48 +00001224 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001225 }
David Greene994e1bb2010-01-05 01:30:02 +00001226 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001227}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001228
Devang Patel004937b2007-07-27 20:06:09 +00001229/// Add RequiredPass into list of lower level passes required by pass P.
1230/// RequiredPass is run on the fly by Pass Manager when P requests it
1231/// through getAnalysis interface.
1232/// This should be handled by specific pass manager.
1233void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1234 if (TPM) {
1235 TPM->dumpArguments();
1236 TPM->dumpPasses();
1237 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001238
Dan Gohmande6188a2010-08-12 23:50:08 +00001239 // Module Level pass may required Function Level analysis info
1240 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1241 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001242 // module level pass is requiring lower level analysis info managed by
1243 // lower level pass manager.
1244
1245 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001246 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001247 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001248#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001249 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1250 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001251#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001252 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001253}
1254
Owen Andersona7aed182010-08-06 18:33:48 +00001255Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001256 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001257}
1258
Devang Patele7599552007-01-12 18:52:44 +00001259// Destructor
1260PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001261 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001262 E = PassVector.end(); I != E; ++I)
1263 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001264}
1265
Devang Patel9bdf7d42006-12-08 23:28:54 +00001266//===----------------------------------------------------------------------===//
1267// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001268// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1269Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001270 return PM.findAnalysisPass(ID, dir);
1271}
1272
Dan Gohmande6188a2010-08-12 23:50:08 +00001273Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001274 Function &F) {
1275 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1276}
1277
Devang Patela1514cb2006-12-07 19:39:39 +00001278//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001279// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001280
Dan Gohmande6188a2010-08-12 23:50:08 +00001281/// Execute all of the passes scheduled for execution by invoking
1282/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001283/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001284bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001285 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001286 return false;
1287
Devang Patele9585592006-12-08 01:38:28 +00001288 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001289
Devang Patel6e5a1132006-11-07 21:31:57 +00001290 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001291 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1292 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001293 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001294
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001295 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001296 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001297
Devang Patelabfbe3b2006-12-16 00:56:26 +00001298 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001299
Chris Lattner4c1e9542009-03-06 06:45:05 +00001300 {
1301 // If the pass crashes, remember this.
1302 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001303 TimeRegion PassTimer(getPassTimer(BP));
1304
Dan Gohman74b189f2010-03-01 17:34:28 +00001305 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001306 }
Devang Patel93a197c2006-12-14 00:08:04 +00001307
Dan Gohman74b189f2010-03-01 17:34:28 +00001308 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001309 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001310 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001311 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001312 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001313
Devang Patela273d1c2007-07-19 18:02:32 +00001314 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001315 removeNotPreservedAnalysis(BP);
1316 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001317 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001318 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001319
Bill Wendling6ce6d262009-12-25 13:50:18 +00001320 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001321}
1322
Devang Patel475c4532006-12-08 00:59:05 +00001323// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001324bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001325 bool Changed = false;
1326
Chris Lattner4c1e9542009-03-06 06:45:05 +00001327 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1328 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001329
1330 return Changed;
1331}
1332
Duncan Sands51495602009-02-13 09:42:34 +00001333bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001334 bool Changed = false;
1335
Chris Lattner4c1e9542009-03-06 06:45:05 +00001336 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1337 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001338
1339 return Changed;
1340}
1341
Duncan Sands51495602009-02-13 09:42:34 +00001342bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001343 bool Changed = false;
1344
Devang Patelabfbe3b2006-12-16 00:56:26 +00001345 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1346 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001347 Changed |= BP->doInitialization(F);
1348 }
1349
1350 return Changed;
1351}
1352
Duncan Sands51495602009-02-13 09:42:34 +00001353bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001354 bool Changed = false;
1355
Devang Patelabfbe3b2006-12-16 00:56:26 +00001356 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1357 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001358 Changed |= BP->doFinalization(F);
1359 }
1360
1361 return Changed;
1362}
1363
1364
Devang Patela1514cb2006-12-07 19:39:39 +00001365//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001366// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001367
Devang Patel4e12f862006-11-08 10:44:40 +00001368/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001369FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001370 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001371 // FPM is the top level manager.
1372 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001373
Dan Gohman565df952008-03-13 02:08:36 +00001374 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001375 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001376}
1377
Devang Patelb67904d2006-12-13 02:36:01 +00001378FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001379 delete FPM;
1380}
1381
Devang Patel4e12f862006-11-08 10:44:40 +00001382/// add - Add a pass to the queue of passes to run. This passes
1383/// ownership of the Pass to the PassManager. When the
1384/// PassManager_X is destroyed, the pass will be destroyed as well, so
1385/// there is no need to delete the pass. (TODO delete passes.)
1386/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001387void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001388 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001389}
1390
Devang Patel9f3083e2006-11-15 19:39:54 +00001391/// run - Execute all of the passes scheduled for execution. Keep
1392/// track of whether any of the passes modifies the function, and if
1393/// so, return true.
1394///
Devang Patelb67904d2006-12-13 02:36:01 +00001395bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001396 if (F.isMaterializable()) {
1397 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001398 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001399 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001400 }
Devang Patel272908d2006-12-08 22:57:48 +00001401 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001402}
1403
1404
Devang Patelff631ae2006-11-15 01:27:05 +00001405/// doInitialization - Run all of the initializers for the function passes.
1406///
Devang Patelb67904d2006-12-13 02:36:01 +00001407bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001408 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001409}
1410
Dan Gohmane6656eb2007-07-30 14:51:13 +00001411/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001412///
Devang Patelb67904d2006-12-13 02:36:01 +00001413bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001414 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001415}
1416
Devang Patela1514cb2006-12-07 19:39:39 +00001417//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001418// FunctionPassManagerImpl implementation
1419//
Duncan Sands51495602009-02-13 09:42:34 +00001420bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001421 bool Changed = false;
1422
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001423 dumpArguments();
1424 dumpPasses();
1425
Chris Lattner4c1e9542009-03-06 06:45:05 +00001426 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1427 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428
1429 return Changed;
1430}
1431
Duncan Sands51495602009-02-13 09:42:34 +00001432bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001433 bool Changed = false;
1434
Chris Lattner4c1e9542009-03-06 06:45:05 +00001435 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1436 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001437
1438 return Changed;
1439}
1440
Devang Patelec9c58f2009-04-01 22:34:41 +00001441/// cleanup - After running all passes, clean up pass manager cache.
1442void FPPassManager::cleanup() {
1443 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1444 FunctionPass *FP = getContainedPass(Index);
1445 AnalysisResolver *AR = FP->getResolver();
1446 assert(AR && "Analysis Resolver is not set");
1447 AR->clearAnalysisImpls();
1448 }
1449}
1450
Torok Edwin24c78352009-06-29 18:49:09 +00001451void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1452 if (!wasRun)
1453 return;
1454 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1455 FPPassManager *FPPM = getContainedManager(Index);
1456 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1457 FPPM->getContainedPass(Index)->releaseMemory();
1458 }
1459 }
Torok Edwin896556e2009-06-29 21:05:10 +00001460 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001461}
1462
Devang Patel67d6a5e2006-12-19 19:46:59 +00001463// Execute all the passes managed by this top level manager.
1464// Return true if any function is modified by a pass.
1465bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001466 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001467 TimingInfo::createTheTimeInfo();
1468
Devang Patele3068402006-12-21 00:16:50 +00001469 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001470 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1471 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001472
1473 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1474 getContainedManager(Index)->cleanup();
1475
Torok Edwin24c78352009-06-29 18:49:09 +00001476 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001477 return Changed;
1478}
1479
1480//===----------------------------------------------------------------------===//
1481// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001482
Devang Patel8c78a0b2007-05-03 01:11:54 +00001483char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001484/// Print passes managed by this manager
1485void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001486 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001487 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1488 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001489 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001490 dumpLastUses(FP, Offset+1);
1491 }
1492}
1493
1494
Dan Gohmande6188a2010-08-12 23:50:08 +00001495/// Execute all of the passes scheduled for execution by invoking
1496/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001497/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001498bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001499 if (F.isDeclaration())
1500 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001501
1502 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001503
Devang Patelcbbf2912008-03-20 01:09:53 +00001504 // Collect inherited analysis from Module level pass manager.
1505 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001506
Devang Patelabfbe3b2006-12-16 00:56:26 +00001507 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1508 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001509 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001510
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001511 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001512 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001513
Devang Patelabfbe3b2006-12-16 00:56:26 +00001514 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001515
Chris Lattner4c1e9542009-03-06 06:45:05 +00001516 {
1517 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001518 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001519
Dan Gohman74b189f2010-03-01 17:34:28 +00001520 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001521 }
Devang Patel93a197c2006-12-14 00:08:04 +00001522
Dan Gohman74b189f2010-03-01 17:34:28 +00001523 Changed |= LocalChanged;
1524 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001526 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001527
Devang Patela273d1c2007-07-19 18:02:32 +00001528 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001529 removeNotPreservedAnalysis(FP);
1530 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001531 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001532 }
1533 return Changed;
1534}
1535
Devang Patel67d6a5e2006-12-19 19:46:59 +00001536bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001537 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001538
Dan Gohmane7630be2010-05-11 20:30:00 +00001539 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001540 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001541
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001542 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001543}
1544
Duncan Sands51495602009-02-13 09:42:34 +00001545bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001546 bool Changed = false;
1547
Chris Lattner4c1e9542009-03-06 06:45:05 +00001548 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1549 Changed |= getContainedPass(Index)->doInitialization(M);
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001550
Devang Patelff631ae2006-11-15 01:27:05 +00001551 return Changed;
1552}
1553
Duncan Sands51495602009-02-13 09:42:34 +00001554bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001555 bool Changed = false;
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001556
Chris Lattner4c1e9542009-03-06 06:45:05 +00001557 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1558 Changed |= getContainedPass(Index)->doFinalization(M);
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001559
Devang Patelff631ae2006-11-15 01:27:05 +00001560 return Changed;
1561}
1562
Devang Patela1514cb2006-12-07 19:39:39 +00001563//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001564// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001565
Dan Gohmande6188a2010-08-12 23:50:08 +00001566/// Execute all of the passes scheduled for execution by invoking
1567/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001568/// the module, and if so, return true.
1569bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001570MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001571 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001572
Torok Edwin24c78352009-06-29 18:49:09 +00001573 // Initialize on-the-fly passes
1574 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1575 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1576 I != E; ++I) {
1577 FunctionPassManagerImpl *FPP = I->second;
1578 Changed |= FPP->doInitialization(M);
1579 }
1580
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001581 // Initialize module passes
1582 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1583 Changed |= getContainedPass(Index)->doInitialization(M);
1584
Devang Patelabfbe3b2006-12-16 00:56:26 +00001585 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1586 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001587 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001588
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001589 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001590 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001591
Devang Patelabfbe3b2006-12-16 00:56:26 +00001592 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001593
Chris Lattner4c1e9542009-03-06 06:45:05 +00001594 {
1595 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001596 TimeRegion PassTimer(getPassTimer(MP));
1597
Dan Gohman74b189f2010-03-01 17:34:28 +00001598 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001599 }
Devang Patel93a197c2006-12-14 00:08:04 +00001600
Dan Gohman74b189f2010-03-01 17:34:28 +00001601 Changed |= LocalChanged;
1602 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001603 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001604 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001605 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001606
Devang Patela273d1c2007-07-19 18:02:32 +00001607 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001608 removeNotPreservedAnalysis(MP);
1609 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001610 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001611 }
Torok Edwin24c78352009-06-29 18:49:09 +00001612
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001613 // Finalize module passes
1614 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1615 Changed |= getContainedPass(Index)->doFinalization(M);
1616
Torok Edwin24c78352009-06-29 18:49:09 +00001617 // Finalize on-the-fly passes
1618 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1619 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1620 I != E; ++I) {
1621 FunctionPassManagerImpl *FPP = I->second;
1622 // We don't know when is the last time an on-the-fly pass is run,
1623 // so we need to releaseMemory / finalize here
1624 FPP->releaseMemoryOnTheFly();
1625 Changed |= FPP->doFinalization(M);
1626 }
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001627
Devang Patel05e1a972006-11-07 22:03:15 +00001628 return Changed;
1629}
1630
Devang Patele64d3052007-04-16 20:12:57 +00001631/// Add RequiredPass into list of lower level passes required by pass P.
1632/// RequiredPass is run on the fly by Pass Manager when P requests it
1633/// through getAnalysis interface.
1634void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001635 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1636 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001637 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001638 RequiredPass->getPotentialPassManagerType()) &&
1639 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001640
Devang Patel68f72b12007-04-26 17:50:19 +00001641 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001642 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001643 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001644 // FPP is the top level manager.
1645 FPP->setTopLevelManager(FPP);
1646
Devang Patel69e9f6d2007-04-16 20:27:05 +00001647 OnTheFlyManagers[P] = FPP;
1648 }
Devang Patel68f72b12007-04-26 17:50:19 +00001649 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001650
Devang Patel68f72b12007-04-26 17:50:19 +00001651 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001652 if (RequiredPass) {
1653 SmallVector<Pass *, 1> LU;
1654 LU.push_back(RequiredPass);
1655 FPP->setLastUser(LU, P);
1656 }
Devang Patele64d3052007-04-16 20:12:57 +00001657}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001658
Dan Gohmande6188a2010-08-12 23:50:08 +00001659/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001660/// required by module pass MP. Instantiate analysis pass, by using
1661/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001662Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001663 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001664 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001665
Torok Edwin24c78352009-06-29 18:49:09 +00001666 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001667 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001668 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001669}
1670
1671
Devang Patela1514cb2006-12-07 19:39:39 +00001672//===----------------------------------------------------------------------===//
1673// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001674
Devang Patelab97cf42006-12-13 00:09:23 +00001675//
Devang Patelc290c8a2006-11-07 22:23:34 +00001676/// run - Execute all of the passes scheduled for execution. Keep track of
1677/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001678bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001679 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001680 TimingInfo::createTheTimeInfo();
1681
Devang Patelcfd70c42006-12-13 22:10:00 +00001682 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001683 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001684
Devang Patele3068402006-12-21 00:16:50 +00001685 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001686 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1687 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001688 return Changed;
1689}
Devang Patel376fefa2006-11-08 10:29:57 +00001690
Devang Patela1514cb2006-12-07 19:39:39 +00001691//===----------------------------------------------------------------------===//
1692// PassManager implementation
1693
Devang Patel376fefa2006-11-08 10:29:57 +00001694/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001695PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001696 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001697 // PM is the top level manager
1698 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001699}
1700
Devang Patelb67904d2006-12-13 02:36:01 +00001701PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001702 delete PM;
1703}
1704
Devang Patel376fefa2006-11-08 10:29:57 +00001705/// add - Add a pass to the queue of passes to run. This passes ownership of
1706/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1707/// will be destroyed as well, so there is no need to delete the pass. This
1708/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001709void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001710 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001711}
1712
1713/// run - Execute all of the passes scheduled for execution. Keep track of
1714/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001715bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001716 return PM->run(M);
1717}
1718
Devang Patelb8817b92006-12-14 00:59:42 +00001719//===----------------------------------------------------------------------===//
1720// TimingInfo Class - This class is used to calculate information about the
1721// amount of time each pass takes to execute. This only happens with
1722// -time-passes is enabled on the command line.
1723//
1724bool llvm::TimePassesIsEnabled = false;
1725static cl::opt<bool,true>
1726EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1727 cl::desc("Time each pass, printing elapsed time for each on exit"));
1728
1729// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1730// a non null value (if the -time-passes option is enabled) or it leaves it
1731// null. It may be called multiple times.
1732void TimingInfo::createTheTimeInfo() {
1733 if (!TimePassesIsEnabled || TheTimeInfo) return;
1734
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001735 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001736 // This guarantees that the object will be constructed before static globals,
1737 // thus it will be destroyed before them.
1738 static ManagedStatic<TimingInfo> TTI;
1739 TheTimeInfo = &*TTI;
1740}
1741
Devang Patel1c3633e2007-01-29 23:10:37 +00001742/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001743Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001744 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001745 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001746 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001747}
1748
Devang Patel1c56a632007-01-08 19:29:38 +00001749//===----------------------------------------------------------------------===//
1750// PMStack implementation
1751//
Devang Patelad98d232007-01-11 22:15:30 +00001752
Devang Patel1c56a632007-01-08 19:29:38 +00001753// Pop Pass Manager from the stack and clear its analysis info.
1754void PMStack::pop() {
1755
1756 PMDataManager *Top = this->top();
1757 Top->initializeAnalysisInfo();
1758
1759 S.pop_back();
1760}
1761
1762// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001763void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001764 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001765 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001766
Chris Lattner60987362009-03-06 05:53:14 +00001767 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001768 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1769 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001770 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001771
Chris Lattner60987362009-03-06 05:53:14 +00001772 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001773 TPM->addIndirectPassManager(PM);
1774 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001775 PM->setDepth(this->top()->getDepth()+1);
1776 }
1777 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001778 assert((PM->getPassManagerType() == PMT_ModulePassManager
1779 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001780 && "pushing bad pass manager to PMStack");
1781 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001782 }
1783
Devang Patel15701b52007-01-11 00:19:00 +00001784 S.push_back(PM);
1785}
1786
1787// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001788void PMStack::dump() const {
1789 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001790 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001791 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001792
Devang Patel15701b52007-01-11 00:19:00 +00001793 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001794 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001795}
1796
Devang Patel1c56a632007-01-08 19:29:38 +00001797/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001798/// add self into that manager.
1799void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001800 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001801 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001802 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001803 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1804 if (TopPMType == PreferredType)
1805 break; // We found desired pass manager
1806 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001807 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001808 else
1809 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001810 }
Devang Patel18ff6362008-09-09 21:38:40 +00001811 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001812 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001813}
1814
Devang Patel3312f752007-01-16 21:43:18 +00001815/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001816/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001817void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001818 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001819
Andrew Trickcbc845f2012-02-01 07:16:20 +00001820 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001821 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001822 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1823 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001824 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001825 break;
Devang Patel3312f752007-01-16 21:43:18 +00001826 }
Devang Patel3312f752007-01-16 21:43:18 +00001827
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001828 // Create new Function Pass Manager if needed.
1829 FPPassManager *FPP;
1830 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1831 FPP = (FPPassManager *)PMS.top();
1832 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001833 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1834 PMDataManager *PMD = PMS.top();
1835
1836 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001837 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001838 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001839
1840 // [2] Set up new manager's top level manager
1841 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1842 TPM->addIndirectPassManager(FPP);
1843
1844 // [3] Assign manager to manage this new manager. This may create
1845 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001846 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001847
1848 // [4] Push new manager into PMS
1849 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001850 }
1851
Devang Patel3312f752007-01-16 21:43:18 +00001852 // Assign FPP as the manager of this pass.
1853 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001854}
1855
Devang Patel3312f752007-01-16 21:43:18 +00001856/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001857/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001858void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001859 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001860 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001861
Devang Patel15701b52007-01-11 00:19:00 +00001862 // Basic Pass Manager is a leaf pass manager. It does not handle
1863 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001864 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001865 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1866 BBP = (BBPassManager *)PMS.top();
1867 } else {
1868 // If leaf manager is not Basic Block Pass manager then create new
1869 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001870 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1871 PMDataManager *PMD = PMS.top();
1872
1873 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001874 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001875
1876 // [2] Set up new manager's top level manager
1877 // Basic Block Pass Manager does not live by itself
1878 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1879 TPM->addIndirectPassManager(BBP);
1880
Devang Patel15701b52007-01-11 00:19:00 +00001881 // [3] Assign manager to manage this new manager. This may create
1882 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001883 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001884
Devang Patel3312f752007-01-16 21:43:18 +00001885 // [4] Push new manager into PMS
1886 PMS.push(BBP);
1887 }
Devang Patel1c56a632007-01-08 19:29:38 +00001888
Devang Patel3312f752007-01-16 21:43:18 +00001889 // Assign BBP as the manager of this pass.
1890 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001891}
1892
Dan Gohmand3a20c92008-03-11 16:41:42 +00001893PassManagerBase::~PassManagerBase() {}