blob: 8362034fcf2d5af9f3da40a74f702ca9fd12ef47 [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"
Dan Gohmana19631f2010-08-07 01:18:18 +000016#include "llvm/PassManager.h"
David Greene9b063df2010-04-02 23:17:14 +000017#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000018#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000019#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000020#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000021#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000022#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000024#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000025#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Mutex.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
Owen Anderson1aa27512012-11-15 00:14:15 +0000312 /// doInitialization - Run all of the initializers for the module passes.
313 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000314 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000315
316 /// doFinalization - Run all of the finalizers for the module passes.
317 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000318 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000319
Devang Patelf9d96b92006-12-07 19:57:52 +0000320 /// Pass Manager itself does not invalidate any analysis info.
321 void getAnalysisUsage(AnalysisUsage &Info) const {
322 Info.setPreservesAll();
323 }
324
Devang Patele64d3052007-04-16 20:12:57 +0000325 /// Add RequiredPass into list of lower level passes required by pass P.
326 /// RequiredPass is run on the fly by Pass Manager when P requests it
327 /// through getAnalysis interface.
328 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
329
Dan Gohmande6188a2010-08-12 23:50:08 +0000330 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000331 /// required by module pass MP. Instantiate analysis pass, by using
332 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000333 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000334
Devang Patele3858e62007-02-01 22:08:25 +0000335 virtual const char *getPassName() const {
336 return "Module Pass Manager";
337 }
338
Chris Lattner2fa26e52010-01-22 05:24:46 +0000339 virtual PMDataManager *getAsPMDataManager() { return this; }
340 virtual Pass *getAsPass() { return this; }
341
Devang Pateleda56172006-12-12 23:34:33 +0000342 // Print passes managed by this manager
343 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000344 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000345 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
346 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000347 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000348 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
349 OnTheFlyManagers.find(MP);
350 if (I != OnTheFlyManagers.end())
351 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000352 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000353 }
354 }
355
Devang Patelabfbe3b2006-12-16 00:56:26 +0000356 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000357 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000358 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000359 }
360
Dan Gohmande6188a2010-08-12 23:50:08 +0000361 virtual PassManagerType getPassManagerType() const {
362 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000363 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000364
365 private:
366 /// Collection of on the fly FPPassManagers. These managers manage
367 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000368 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000369};
370
Devang Patel8c78a0b2007-05-03 01:11:54 +0000371char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000372//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000373// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000374//
Devang Patel09f162c2007-05-01 21:15:47 +0000375
Devang Patel67d6a5e2006-12-19 19:46:59 +0000376/// PassManagerImpl manages MPPassManagers
377class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000378 public PMDataManager,
379 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000380 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000381
382public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000383 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000384 explicit PassManagerImpl() :
385 Pass(PT_PassManager, ID), PMDataManager(),
386 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000387
Devang Patel376fefa2006-11-08 10:29:57 +0000388 /// add - Add a pass to the queue of passes to run. This passes ownership of
389 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
390 /// will be destroyed as well, so there is no need to delete the pass. This
391 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000392 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000393 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000394 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000395
396 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000397 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
398 return createPrintModulePass(&O, false, Banner);
399 }
400
Devang Patel376fefa2006-11-08 10:29:57 +0000401 /// run - Execute all of the passes scheduled for execution. Keep track of
402 /// whether any of the passes modifies the module, and if so, return true.
403 bool run(Module &M);
404
Owen Anderson1aa27512012-11-15 00:14:15 +0000405 /// doInitialization - Run all of the initializers for the module passes.
406 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000407 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000408
409 /// doFinalization - Run all of the finalizers for the module passes.
410 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000411 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000412
Devang Patelf9d96b92006-12-07 19:57:52 +0000413 /// Pass Manager itself does not invalidate any analysis info.
414 void getAnalysisUsage(AnalysisUsage &Info) const {
415 Info.setPreservesAll();
416 }
417
Chris Lattner2fa26e52010-01-22 05:24:46 +0000418 virtual PMDataManager *getAsPMDataManager() { return this; }
419 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000420 virtual PassManagerType getTopLevelPassManagerType() {
421 return PMT_ModulePassManager;
422 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000423
Devang Patel67d6a5e2006-12-19 19:46:59 +0000424 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000425 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000426 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
427 return MP;
428 }
Devang Patel376fefa2006-11-08 10:29:57 +0000429};
430
David Blaikiea379b1812011-12-20 02:50:00 +0000431void PassManagerImpl::anchor() {}
432
Devang Patel8c78a0b2007-05-03 01:11:54 +0000433char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000434} // End of llvm namespace
435
436namespace {
437
438//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000439/// TimingInfo Class - This class is used to calculate information about the
440/// amount of time each pass takes to execute. This only happens when
441/// -time-passes is enabled on the command line.
442///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000443
Owen Anderson5a6960f2009-06-18 20:51:00 +0000444static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000445
Nick Lewycky02d5f772009-10-25 06:33:48 +0000446class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000447 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000448 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000449public:
450 // Use 'create' member to get this.
451 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000452
Devang Patel1c3633e2007-01-29 23:10:37 +0000453 // TimingDtor - Print out information about timing information
454 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000455 // Delete all of the timers, which accumulate their info into the
456 // TimerGroup.
457 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
458 E = TimingData.end(); I != E; ++I)
459 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000460 // TimerGroup is deleted next, printing the report.
461 }
462
463 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
464 // to a non null value (if the -time-passes option is enabled) or it leaves it
465 // null. It may be called multiple times.
466 static void createTheTimeInfo();
467
Chris Lattner707431c2010-03-30 04:03:22 +0000468 /// getPassTimer - Return the timer for the specified pass if it exists.
469 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000470 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000471 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000472
Owen Anderson5c96ef72009-07-07 18:33:04 +0000473 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000474 Timer *&T = TimingData[P];
475 if (T == 0)
476 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000477 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000478 }
479};
480
Devang Patel1c3633e2007-01-29 23:10:37 +0000481} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000482
Dan Gohmand78c4002008-05-13 00:00:25 +0000483static TimingInfo *TheTimeInfo;
484
Devang Patela1514cb2006-12-07 19:39:39 +0000485//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000486// PMTopLevelManager implementation
487
Devang Patel4268fc02007-01-16 02:00:38 +0000488/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000489PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
490 PMDM->setTopLevelManager(this);
491 addPassManager(PMDM);
492 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000493}
494
Devang Patelafb1f3622006-12-12 22:35:25 +0000495/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000496void
Bill Wendlingea857e12012-05-14 07:53:40 +0000497PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000498 unsigned PDepth = 0;
499 if (P->getResolver())
500 PDepth = P->getResolver()->getPMDataManager().getDepth();
501
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000502 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000503 E = AnalysisPasses.end(); I != E; ++I) {
504 Pass *AP = *I;
505 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000506
Devang Patel01919d22007-03-08 19:05:01 +0000507 if (P == AP)
508 continue;
509
Tobias Grosserf07426b2011-01-20 21:03:22 +0000510 // Update the last users of passes that are required transitive by AP.
511 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
512 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
513 SmallVector<Pass *, 12> LastUses;
514 SmallVector<Pass *, 12> LastPMUses;
515 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
516 E = IDs.end(); I != E; ++I) {
517 Pass *AnalysisPass = findAnalysisPass(*I);
518 assert(AnalysisPass && "Expected analysis pass to exist.");
519 AnalysisResolver *AR = AnalysisPass->getResolver();
520 assert(AR && "Expected analysis resolver to exist.");
521 unsigned APDepth = AR->getPMDataManager().getDepth();
522
523 if (PDepth == APDepth)
524 LastUses.push_back(AnalysisPass);
525 else if (PDepth > APDepth)
526 LastPMUses.push_back(AnalysisPass);
527 }
528
529 setLastUser(LastUses, P);
530
531 // If this pass has a corresponding pass manager, push higher level
532 // analysis to this pass manager.
533 if (P->getResolver())
534 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
535
536
Devang Patelafb1f3622006-12-12 22:35:25 +0000537 // If AP is the last user of other passes then make P last user of
538 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000539 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000540 LUE = LastUser.end(); LUI != LUE; ++LUI) {
541 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000542 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000543 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000544 LastUser[LUI->first] = P;
545 }
546 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000547}
548
549/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000550void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000551 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000552 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000553 InversedLastUser.find(P);
554 if (DMI == InversedLastUser.end())
555 return;
556
557 SmallPtrSet<Pass *, 8> &LU = DMI->second;
558 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
559 E = LU.end(); I != E; ++I) {
560 LastUses.push_back(*I);
561 }
562
Devang Patelafb1f3622006-12-12 22:35:25 +0000563}
564
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000565AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
566 AnalysisUsage *AnUsage = NULL;
567 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000568 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000569 AnUsage = DMI->second;
570 else {
571 AnUsage = new AnalysisUsage();
572 P->getAnalysisUsage(*AnUsage);
573 AnUsageMap[P] = AnUsage;
574 }
575 return AnUsage;
576}
577
Devang Patelafb1f3622006-12-12 22:35:25 +0000578/// Schedule pass P for execution. Make sure that passes required by
579/// P are run before P is run. Update analysis info maintained by
580/// the manager. Remove dead passes. This is a recursive function.
581void PMTopLevelManager::schedulePass(Pass *P) {
582
Devang Patel3312f752007-01-16 21:43:18 +0000583 // TODO : Allocate function manager for this pass, other wise required set
584 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000585
Devang Pateld74ede72007-03-06 01:06:16 +0000586 // Give pass a chance to prepare the stage.
587 P->preparePassManager(activeStack);
588
Devang Patel864970e2008-03-18 00:39:19 +0000589 // If P is an analysis pass and it is available then do not
590 // generate the analysis again. Stale analysis info should not be
591 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000592 const PassInfo *PI =
593 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
594 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000595 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000596 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000597 }
Devang Patel864970e2008-03-18 00:39:19 +0000598
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000599 AnalysisUsage *AnUsage = findAnalysisUsage(P);
600
Devang Patelfdee7032008-08-14 23:07:48 +0000601 bool checkAnalysis = true;
602 while (checkAnalysis) {
603 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000604
Devang Patelfdee7032008-08-14 23:07:48 +0000605 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
606 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
607 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000608
Devang Patelfdee7032008-08-14 23:07:48 +0000609 Pass *AnalysisPass = findAnalysisPass(*I);
610 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000611 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000612
613 if (PI == NULL) {
614 // Pass P is not in the global PassRegistry
615 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
616 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
617 dbgs() << "Required Passes:" << "\n";
618 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
619 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
620 Pass *AnalysisPass2 = findAnalysisPass(*I2);
621 if (AnalysisPass2) {
622 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
623 }
624 else {
625 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
626 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
627 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
628 }
629 }
630 }
631
Andrew Trick6bbaf132011-06-03 00:48:58 +0000632 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000633 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000634 if (P->getPotentialPassManagerType () ==
635 AnalysisPass->getPotentialPassManagerType())
636 // Schedule analysis pass that is managed by the same pass manager.
637 schedulePass(AnalysisPass);
638 else if (P->getPotentialPassManagerType () >
639 AnalysisPass->getPotentialPassManagerType()) {
640 // Schedule analysis pass that is managed by a new manager.
641 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000642 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000643 // are already checked are still available.
644 checkAnalysis = true;
645 }
646 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000647 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000648 // passes are run on the fly.
649 delete AnalysisPass;
650 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000651 }
652 }
653
654 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000655 if (ImmutablePass *IP = P->getAsImmutablePass()) {
656 // P is a immutable pass and it will be managed by this
657 // top level manager. Set up analysis resolver to connect them.
658 PMDataManager *DM = getAsPMDataManager();
659 AnalysisResolver *AR = new AnalysisResolver(*DM);
660 P->setResolver(AR);
661 DM->initializeAnalysisImpl(P);
662 addImmutablePass(IP);
663 DM->recordAvailableAnalysis(IP);
664 return;
665 }
666
667 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
668 Pass *PP = P->createPrinterPass(
669 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
670 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
671 }
672
673 // Add the requested pass to the best available pass manager.
674 P->assignPassManager(activeStack, getTopLevelPassManagerType());
675
676 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
677 Pass *PP = P->createPrinterPass(
678 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
679 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
680 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000681}
682
683/// Find the pass that implements Analysis AID. Search immutable
684/// passes and all pass managers. If desired pass is not found
685/// then return NULL.
686Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
687
Devang Patelcd6ba152006-12-12 22:50:05 +0000688 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000689 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000690 E = PassManagers.end(); I != E; ++I)
691 if (Pass *P = (*I)->findAnalysisPass(AID, false))
692 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000693
694 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000695 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000696 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000697 E = IndirectPassManagers.end(); I != E; ++I)
698 if (Pass *P = (*I)->findAnalysisPass(AID, false))
699 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000700
Dan Gohman844dd0a2010-10-11 23:19:01 +0000701 // Check the immutable passes. Iterate in reverse order so that we find
702 // the most recently registered passes first.
703 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
704 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000705 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000706 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000707 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000708
709 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000710 const PassInfo *PassInf =
711 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000712 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000713 const std::vector<const PassInfo*> &ImmPI =
714 PassInf->getInterfacesImplemented();
715 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
716 EE = ImmPI.end(); II != EE; ++II) {
717 if ((*II)->getTypeInfo() == AID)
718 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000719 }
720 }
721
Dan Gohman844dd0a2010-10-11 23:19:01 +0000722 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000723}
724
Devang Pateleda56172006-12-12 23:34:33 +0000725// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000726void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000727
Devang Patelfd4184322007-01-17 20:33:36 +0000728 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000729 return;
730
Devang Pateleda56172006-12-12 23:34:33 +0000731 // Print out the immutable passes
732 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000733 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000734 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000735
Dan Gohmanf71c5212010-08-19 01:29:07 +0000736 // Every class that derives from PMDataManager also derives from Pass
737 // (sometimes indirectly), but there's no inheritance relationship
738 // between PMDataManager and Pass, so we have to getAsPass to get
739 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000740 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000741 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000742 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000743}
744
Devang Patel991aeba2006-12-15 20:13:01 +0000745void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000746
Devang Patelfd4184322007-01-17 20:33:36 +0000747 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000748 return;
749
David Greene994e1bb2010-01-05 01:30:02 +0000750 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000751 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
752 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
753 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000754 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
755 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000756 if (!PI->isAnalysisGroup())
757 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000758 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000759 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000760 E = PassManagers.end(); I != E; ++I)
761 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000762 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000763}
764
Devang Patele3068402006-12-21 00:16:50 +0000765void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000766 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000767 E = PassManagers.end(); I != E; ++I)
768 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000769
Devang Patele3068402006-12-21 00:16:50 +0000770 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000771 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000772 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
773 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000774 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000775
Chris Lattner60987362009-03-06 05:53:14 +0000776 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000777 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000778 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000779 InversedLastUser.find(DMI->second);
780 if (InvDMI != InversedLastUser.end()) {
781 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
782 L.insert(DMI->first);
783 } else {
784 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
785 InversedLastUser[DMI->second] = L;
786 }
787 }
Devang Patele3068402006-12-21 00:16:50 +0000788}
789
Devang Patele7599552007-01-12 18:52:44 +0000790/// Destructor
791PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000792 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000793 E = PassManagers.end(); I != E; ++I)
794 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000795
Dan Gohman060d5ba2010-10-12 00:15:27 +0000796 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000797 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
798 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000799
800 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000801 DME = AnUsageMap.end(); DMI != DME; ++DMI)
802 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000803}
804
Devang Patelafb1f3622006-12-12 22:35:25 +0000805//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000806// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000807
Devang Patel643676c2006-11-11 01:10:19 +0000808/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000809void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000810 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000811
Chris Lattner60987362009-03-06 05:53:14 +0000812 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000813
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000814 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000815
Dan Gohmande6188a2010-08-12 23:50:08 +0000816 // This pass is the current implementation of all of the interfaces it
817 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000818 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
819 if (PInf == 0) return;
820 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000821 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000822 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000823}
824
Devang Patel9d9fc902007-03-06 17:52:53 +0000825// Return true if P preserves high level analysis used by other
826// passes managed by this manager
827bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000828 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000829 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000830 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000831
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000832 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000833 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000834 E = HigherLevelAnalysis.end(); I != E; ++I) {
835 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000836 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000837 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000838 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000839 PreservedSet.end())
840 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000841 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000842
Devang Patel9d9fc902007-03-06 17:52:53 +0000843 return true;
844}
845
Chris Lattner02eb94c2008-08-07 07:34:50 +0000846/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000847void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000848 // Don't do this unless assertions are enabled.
849#ifdef NDEBUG
850 return;
851#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000852 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
853 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000854
Devang Patelef432532007-07-19 05:36:09 +0000855 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000856 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000857 E = PreservedSet.end(); I != E; ++I) {
858 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000859 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000860 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000861 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000862 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000863 }
864}
865
Devang Patel67c79a42008-07-01 19:50:56 +0000866/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000867void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000868 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
869 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000870 return;
871
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000872 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000873 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000874 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000875 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000876 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000877 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000878 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000879 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000880 if (PassDebugging >= Details) {
881 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000882 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
883 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000884 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000885 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000886 }
Devang Patel349170f2006-11-11 01:24:55 +0000887 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000888
Devang Patel42dd1e92007-03-06 01:55:46 +0000889 // Check inherited analysis also. If P is not preserving analysis
890 // provided by parent manager then remove it here.
891 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
892
893 if (!InheritedAnalysis[Index])
894 continue;
895
Dan Gohmande6188a2010-08-12 23:50:08 +0000896 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000897 I = InheritedAnalysis[Index]->begin(),
898 E = InheritedAnalysis[Index]->end(); I != E; ) {
899 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000900 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000901 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000902 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000903 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000904 if (PassDebugging >= Details) {
905 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000906 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
907 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000908 }
Devang Patel01919d22007-03-08 19:05:01 +0000909 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000910 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000911 }
912 }
Devang Patelf68a3492006-11-07 22:35:17 +0000913}
914
Devang Patelca189262006-11-14 03:05:08 +0000915/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000916void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000917 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000918
Devang Patel8adae862007-07-20 18:04:54 +0000919 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000920
Devang Patel2ff44922007-04-16 20:39:59 +0000921 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000922 if (!TPM)
923 return;
924
Devang Patel17ad0962006-12-08 00:37:52 +0000925 TPM->collectLastUses(DeadPasses, P);
926
Devang Patel656a9172008-06-06 17:50:36 +0000927 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000928 dbgs() << " -*- '" << P->getPassName();
929 dbgs() << "' is the last user of following pass instances.";
930 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000931 }
932
Dan Gohman060d5ba2010-10-12 00:15:27 +0000933 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000934 E = DeadPasses.end(); I != E; ++I)
935 freePass(*I, Msg, DBG_STR);
936}
Devang Patel200d3052006-12-13 23:50:44 +0000937
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000938void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000939 enum PassDebuggingString DBG_STR) {
940 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000941
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000942 {
943 // If the pass crashes releasing memory, remember this.
944 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000945 TimeRegion PassTimer(getPassTimer(P));
946
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000947 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000948 }
949
Owen Andersona7aed182010-08-06 18:33:48 +0000950 AnalysisID PI = P->getPassID();
951 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000952 // Remove the pass itself (if it is not already removed).
953 AvailableAnalysis.erase(PI);
954
955 // Remove all interfaces this pass implements, for which it is also
956 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000957 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000958 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000959 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000960 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000961 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000962 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000963 }
Devang Patel17ad0962006-12-08 00:37:52 +0000964 }
Devang Patelca189262006-11-14 03:05:08 +0000965}
966
Dan Gohmande6188a2010-08-12 23:50:08 +0000967/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000968/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000969void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000970 // This manager is going to manage pass P. Set up analysis resolver
971 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000972 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000973 P->setResolver(AR);
974
Devang Patelec2b9a72007-03-05 22:57:49 +0000975 // If a FunctionPass F is the last user of ModulePass info M
976 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000977 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000978
Chris Lattner60987362009-03-06 05:53:14 +0000979 if (!ProcessAnalysis) {
980 // Add pass
981 PassVector.push_back(P);
982 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000983 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000984
Chris Lattner60987362009-03-06 05:53:14 +0000985 // At the moment, this pass is the last user of all required passes.
986 SmallVector<Pass *, 12> LastUses;
987 SmallVector<Pass *, 8> RequiredPasses;
988 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
989
990 unsigned PDepth = this->getDepth();
991
Dan Gohmande6188a2010-08-12 23:50:08 +0000992 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000993 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000994 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000995 E = RequiredPasses.end(); I != E; ++I) {
996 Pass *PRequired = *I;
997 unsigned RDepth = 0;
998
999 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1000 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1001 RDepth = DM.getDepth();
1002
1003 if (PDepth == RDepth)
1004 LastUses.push_back(PRequired);
1005 else if (PDepth > RDepth) {
1006 // Let the parent claim responsibility of last use
1007 TransferLastUses.push_back(PRequired);
1008 // Keep track of higher level analysis used by this manager.
1009 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001010 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001011 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001012 }
1013
1014 // Set P as P's last user until someone starts using P.
1015 // However, if P is a Pass Manager then it does not need
1016 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001017 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001018 LastUses.push_back(P);
1019 TPM->setLastUser(LastUses, P);
1020
1021 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001022 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001023 TPM->setLastUser(TransferLastUses, My_PM);
1024 TransferLastUses.clear();
1025 }
1026
Dan Gohman6304db32010-08-16 22:57:28 +00001027 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001028 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001029 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001030 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001031 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1032 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001033 this->addLowerLevelRequiredPass(P, AnalysisPass);
1034 }
1035
1036 // Take a note of analysis required and made available by this pass.
1037 // Remove the analysis not preserved by this pass
1038 removeNotPreservedAnalysis(P);
1039 recordAvailableAnalysis(P);
1040
Devang Patel8cad70d2006-11-11 01:51:02 +00001041 // Add pass
1042 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001043}
1044
Devang Patele64d3052007-04-16 20:12:57 +00001045
1046/// Populate RP with analysis pass that are required by
1047/// pass P and are available. Populate RP_NotAvail with analysis
1048/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001049void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1050 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001051 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001052 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1053 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001054 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001055 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001056 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001057 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001058 else
Chris Lattner60987362009-03-06 05:53:14 +00001059 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001060 }
Devang Patelf58183d2006-12-12 23:09:32 +00001061
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001062 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001063 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001064 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001065 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001066 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001067 else
Chris Lattner60987362009-03-06 05:53:14 +00001068 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001069 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001070}
1071
Devang Patel07f4f582006-11-14 21:49:36 +00001072// All Required analyses should be available to the pass as it runs! Here
1073// we fill in the AnalysisImpls member of the pass so that it can
1074// successfully use the getAnalysis() method to retrieve the
1075// implementations it needs.
1076//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001077void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001078 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1079
Chris Lattnercbd160f2008-08-08 05:33:04 +00001080 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001081 I = AnUsage->getRequiredSet().begin(),
1082 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001083 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001084 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001085 // This may be analysis pass that is initialized on the fly.
1086 // If that is not the case then it will raise an assert when it is used.
1087 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001088 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001089 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001090 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001091 }
1092}
1093
Devang Patel640c5bb2006-12-08 22:30:11 +00001094/// Find the pass that implements Analysis AID. If desired pass is not found
1095/// then return NULL.
1096Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1097
1098 // Check if AvailableAnalysis map has one entry.
1099 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1100
1101 if (I != AvailableAnalysis.end())
1102 return I->second;
1103
1104 // Search Parents through TopLevelManager
1105 if (SearchParent)
1106 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001107
Devang Patel9d759b82006-12-09 00:09:12 +00001108 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001109}
1110
Devang Patel991aeba2006-12-15 20:13:01 +00001111// Print list of passes that are last used by P.
1112void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1113
Devang Patel8adae862007-07-20 18:04:54 +00001114 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001115
1116 // If this is a on the fly manager then it does not have TPM.
1117 if (!TPM)
1118 return;
1119
Devang Patel991aeba2006-12-15 20:13:01 +00001120 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001121
Dan Gohman7224bce2010-10-12 00:11:18 +00001122 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001123 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001124 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001125 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001126 }
1127}
1128
1129void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001130 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001131 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001132 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001133 PMD->dumpPassArguments();
1134 else
Owen Andersona7aed182010-08-06 18:33:48 +00001135 if (const PassInfo *PI =
1136 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001137 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001138 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001139 }
1140}
1141
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001142void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1143 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001144 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001145 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001146 return;
David Greene994e1bb2010-01-05 01:30:02 +00001147 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001148 switch (S1) {
1149 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001151 break;
1152 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001154 break;
1155 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
1158 default:
1159 break;
1160 }
1161 switch (S2) {
1162 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001163 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001164 break;
1165 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001166 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001167 break;
1168 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001169 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001170 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001171 case ON_REGION_MSG:
1172 dbgs() << "' on Region '" << Msg << "'...\n";
1173 break;
Devang Patel003a5592007-03-05 20:01:30 +00001174 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001175 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001176 break;
1177 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001178 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001179 break;
1180 default:
1181 break;
1182 }
Devang Patel991aeba2006-12-15 20:13:01 +00001183}
1184
Chris Lattner4c1e9542009-03-06 06:45:05 +00001185void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001186 if (PassDebugging < Details)
1187 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001188
Chris Lattner4c493d92008-08-08 15:14:09 +00001189 AnalysisUsage analysisUsage;
1190 P->getAnalysisUsage(analysisUsage);
1191 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1192}
1193
Chris Lattner4c1e9542009-03-06 06:45:05 +00001194void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001195 if (PassDebugging < Details)
1196 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001197
Chris Lattner4c493d92008-08-08 15:14:09 +00001198 AnalysisUsage analysisUsage;
1199 P->getAnalysisUsage(analysisUsage);
1200 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1201}
1202
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001203void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001204 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001205 assert(PassDebugging >= Details);
1206 if (Set.empty())
1207 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001208 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001209 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001210 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001211 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001212 if (!PInf) {
1213 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1214 // all drivers.
1215 dbgs() << " Uninitialized Pass";
1216 continue;
1217 }
Owen Andersona7aed182010-08-06 18:33:48 +00001218 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001219 }
David Greene994e1bb2010-01-05 01:30:02 +00001220 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001221}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001222
Devang Patel004937b2007-07-27 20:06:09 +00001223/// Add RequiredPass into list of lower level passes required by pass P.
1224/// RequiredPass is run on the fly by Pass Manager when P requests it
1225/// through getAnalysis interface.
1226/// This should be handled by specific pass manager.
1227void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1228 if (TPM) {
1229 TPM->dumpArguments();
1230 TPM->dumpPasses();
1231 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001232
Dan Gohmande6188a2010-08-12 23:50:08 +00001233 // Module Level pass may required Function Level analysis info
1234 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1235 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001236 // module level pass is requiring lower level analysis info managed by
1237 // lower level pass manager.
1238
1239 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001240 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001241 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001242#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001243 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1244 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001245#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001246 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001247}
1248
Owen Andersona7aed182010-08-06 18:33:48 +00001249Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001250 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001251}
1252
Devang Patele7599552007-01-12 18:52:44 +00001253// Destructor
1254PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001255 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001256 E = PassVector.end(); I != E; ++I)
1257 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001258}
1259
Devang Patel9bdf7d42006-12-08 23:28:54 +00001260//===----------------------------------------------------------------------===//
1261// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001262// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1263Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001264 return PM.findAnalysisPass(ID, dir);
1265}
1266
Dan Gohmande6188a2010-08-12 23:50:08 +00001267Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001268 Function &F) {
1269 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1270}
1271
Devang Patela1514cb2006-12-07 19:39:39 +00001272//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001273// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001274
Dan Gohmande6188a2010-08-12 23:50:08 +00001275/// Execute all of the passes scheduled for execution by invoking
1276/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001277/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001278bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001279 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001280 return false;
1281
Devang Patele9585592006-12-08 01:38:28 +00001282 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001283
Devang Patel6e5a1132006-11-07 21:31:57 +00001284 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001285 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1286 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001287 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001288
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001289 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001290 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001291
Devang Patelabfbe3b2006-12-16 00:56:26 +00001292 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001293
Chris Lattner4c1e9542009-03-06 06:45:05 +00001294 {
1295 // If the pass crashes, remember this.
1296 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001297 TimeRegion PassTimer(getPassTimer(BP));
1298
Dan Gohman74b189f2010-03-01 17:34:28 +00001299 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001300 }
Devang Patel93a197c2006-12-14 00:08:04 +00001301
Dan Gohman74b189f2010-03-01 17:34:28 +00001302 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001303 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001304 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001305 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001306 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001307
Devang Patela273d1c2007-07-19 18:02:32 +00001308 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001309 removeNotPreservedAnalysis(BP);
1310 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001311 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001312 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001313
Bill Wendling6ce6d262009-12-25 13:50:18 +00001314 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001315}
1316
Devang Patel475c4532006-12-08 00:59:05 +00001317// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001318bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001319 bool Changed = false;
1320
Chris Lattner4c1e9542009-03-06 06:45:05 +00001321 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1322 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001323
1324 return Changed;
1325}
1326
Duncan Sands51495602009-02-13 09:42:34 +00001327bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001328 bool Changed = false;
1329
Chris Lattner4c1e9542009-03-06 06:45:05 +00001330 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1331 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001332
1333 return Changed;
1334}
1335
Duncan Sands51495602009-02-13 09:42:34 +00001336bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001337 bool Changed = false;
1338
Devang Patelabfbe3b2006-12-16 00:56:26 +00001339 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1340 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001341 Changed |= BP->doInitialization(F);
1342 }
1343
1344 return Changed;
1345}
1346
Duncan Sands51495602009-02-13 09:42:34 +00001347bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001348 bool Changed = false;
1349
Devang Patelabfbe3b2006-12-16 00:56:26 +00001350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1351 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001352 Changed |= BP->doFinalization(F);
1353 }
1354
1355 return Changed;
1356}
1357
1358
Devang Patela1514cb2006-12-07 19:39:39 +00001359//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001360// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001361
Devang Patel4e12f862006-11-08 10:44:40 +00001362/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001363FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001364 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001365 // FPM is the top level manager.
1366 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001367
Dan Gohman565df952008-03-13 02:08:36 +00001368 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001369 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001370}
1371
Devang Patelb67904d2006-12-13 02:36:01 +00001372FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001373 delete FPM;
1374}
1375
Devang Patel4e12f862006-11-08 10:44:40 +00001376/// add - Add a pass to the queue of passes to run. This passes
1377/// ownership of the Pass to the PassManager. When the
1378/// PassManager_X is destroyed, the pass will be destroyed as well, so
1379/// there is no need to delete the pass. (TODO delete passes.)
1380/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001381void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001382 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001383}
1384
Devang Patel9f3083e2006-11-15 19:39:54 +00001385/// run - Execute all of the passes scheduled for execution. Keep
1386/// track of whether any of the passes modifies the function, and if
1387/// so, return true.
1388///
Devang Patelb67904d2006-12-13 02:36:01 +00001389bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001390 if (F.isMaterializable()) {
1391 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001392 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001393 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001394 }
Devang Patel272908d2006-12-08 22:57:48 +00001395 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001396}
1397
1398
Devang Patelff631ae2006-11-15 01:27:05 +00001399/// doInitialization - Run all of the initializers for the function passes.
1400///
Devang Patelb67904d2006-12-13 02:36:01 +00001401bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001402 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001403}
1404
Dan Gohmane6656eb2007-07-30 14:51:13 +00001405/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001406///
Devang Patelb67904d2006-12-13 02:36:01 +00001407bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001408 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001409}
1410
Devang Patela1514cb2006-12-07 19:39:39 +00001411//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001412// FunctionPassManagerImpl implementation
1413//
Duncan Sands51495602009-02-13 09:42:34 +00001414bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001415 bool Changed = false;
1416
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001417 dumpArguments();
1418 dumpPasses();
1419
Chris Lattner4c1e9542009-03-06 06:45:05 +00001420 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1421 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422
1423 return Changed;
1424}
1425
Duncan Sands51495602009-02-13 09:42:34 +00001426bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427 bool Changed = false;
1428
Chris Lattner4c1e9542009-03-06 06:45:05 +00001429 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1430 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001431
1432 return Changed;
1433}
1434
Devang Patelec9c58f2009-04-01 22:34:41 +00001435/// cleanup - After running all passes, clean up pass manager cache.
1436void FPPassManager::cleanup() {
1437 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1438 FunctionPass *FP = getContainedPass(Index);
1439 AnalysisResolver *AR = FP->getResolver();
1440 assert(AR && "Analysis Resolver is not set");
1441 AR->clearAnalysisImpls();
1442 }
1443}
1444
Torok Edwin24c78352009-06-29 18:49:09 +00001445void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1446 if (!wasRun)
1447 return;
1448 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1449 FPPassManager *FPPM = getContainedManager(Index);
1450 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1451 FPPM->getContainedPass(Index)->releaseMemory();
1452 }
1453 }
Torok Edwin896556e2009-06-29 21:05:10 +00001454 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001455}
1456
Devang Patel67d6a5e2006-12-19 19:46:59 +00001457// Execute all the passes managed by this top level manager.
1458// Return true if any function is modified by a pass.
1459bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001460 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001461 TimingInfo::createTheTimeInfo();
1462
Devang Patele3068402006-12-21 00:16:50 +00001463 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1465 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001466
1467 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1468 getContainedManager(Index)->cleanup();
1469
Torok Edwin24c78352009-06-29 18:49:09 +00001470 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001471 return Changed;
1472}
1473
1474//===----------------------------------------------------------------------===//
1475// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001476
Devang Patel8c78a0b2007-05-03 01:11:54 +00001477char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001478/// Print passes managed by this manager
1479void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001480 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001481 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1482 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001483 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001484 dumpLastUses(FP, Offset+1);
1485 }
1486}
1487
1488
Dan Gohmande6188a2010-08-12 23:50:08 +00001489/// Execute all of the passes scheduled for execution by invoking
1490/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001491/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001492bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001493 if (F.isDeclaration())
1494 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001495
1496 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001497
Devang Patelcbbf2912008-03-20 01:09:53 +00001498 // Collect inherited analysis from Module level pass manager.
1499 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001500
Devang Patelabfbe3b2006-12-16 00:56:26 +00001501 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1502 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001503 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001504
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001505 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001506 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001507
Devang Patelabfbe3b2006-12-16 00:56:26 +00001508 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001509
Chris Lattner4c1e9542009-03-06 06:45:05 +00001510 {
1511 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001512 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001513
Dan Gohman74b189f2010-03-01 17:34:28 +00001514 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001515 }
Devang Patel93a197c2006-12-14 00:08:04 +00001516
Dan Gohman74b189f2010-03-01 17:34:28 +00001517 Changed |= LocalChanged;
1518 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001519 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001520 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001521
Devang Patela273d1c2007-07-19 18:02:32 +00001522 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001523 removeNotPreservedAnalysis(FP);
1524 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001526 }
1527 return Changed;
1528}
1529
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001531 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001532
Dan Gohmane7630be2010-05-11 20:30:00 +00001533 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001534 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535
Bill Wendling6ce6d262009-12-25 13:50:18 +00001536 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537}
1538
Duncan Sands51495602009-02-13 09:42:34 +00001539bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001540 bool Changed = false;
1541
Chris Lattner4c1e9542009-03-06 06:45:05 +00001542 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1543 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001544
1545 return Changed;
1546}
1547
Duncan Sands51495602009-02-13 09:42:34 +00001548bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001549 bool Changed = false;
1550
Chris Lattner4c1e9542009-03-06 06:45:05 +00001551 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1552 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001553
Devang Patelff631ae2006-11-15 01:27:05 +00001554 return Changed;
1555}
1556
Devang Patela1514cb2006-12-07 19:39:39 +00001557//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001559
Dan Gohmande6188a2010-08-12 23:50:08 +00001560/// Execute all of the passes scheduled for execution by invoking
1561/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001562/// the module, and if so, return true.
1563bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001564MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001565 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001566
Torok Edwin24c78352009-06-29 18:49:09 +00001567 // Initialize on-the-fly passes
1568 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1569 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1570 I != E; ++I) {
1571 FunctionPassManagerImpl *FPP = I->second;
1572 Changed |= FPP->doInitialization(M);
1573 }
1574
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1576 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001577 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001578
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001579 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001580 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001581
Devang Patelabfbe3b2006-12-16 00:56:26 +00001582 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001583
Chris Lattner4c1e9542009-03-06 06:45:05 +00001584 {
1585 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001586 TimeRegion PassTimer(getPassTimer(MP));
1587
Dan Gohman74b189f2010-03-01 17:34:28 +00001588 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001589 }
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Dan Gohman74b189f2010-03-01 17:34:28 +00001591 Changed |= LocalChanged;
1592 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001593 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001594 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001595 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001596
Devang Patela273d1c2007-07-19 18:02:32 +00001597 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001598 removeNotPreservedAnalysis(MP);
1599 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001600 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001601 }
Torok Edwin24c78352009-06-29 18:49:09 +00001602
1603 // Finalize on-the-fly passes
1604 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1605 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1606 I != E; ++I) {
1607 FunctionPassManagerImpl *FPP = I->second;
1608 // We don't know when is the last time an on-the-fly pass is run,
1609 // so we need to releaseMemory / finalize here
1610 FPP->releaseMemoryOnTheFly();
1611 Changed |= FPP->doFinalization(M);
1612 }
Owen Anderson1aa27512012-11-15 00:14:15 +00001613
1614 return Changed;
1615}
1616
1617/// Run all of the initializers for the module passes.
1618///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +00001619bool MPPassManager::doInitialization() {
Owen Anderson1aa27512012-11-15 00:14:15 +00001620 bool Changed = false;
1621
1622 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1623 Changed |= getContainedPass(Index)->doInitialization();
1624
1625 return Changed;
1626}
1627
1628/// Run all of the finalizers for the module passes.
1629///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +00001630bool MPPassManager::doFinalization() {
Owen Anderson1aa27512012-11-15 00:14:15 +00001631 bool Changed = false;
1632
1633 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1634 Changed |= getContainedPass(Index)->doFinalization();
1635
Devang Patel05e1a972006-11-07 22:03:15 +00001636 return Changed;
1637}
1638
Devang Patele64d3052007-04-16 20:12:57 +00001639/// Add RequiredPass into list of lower level passes required by pass P.
1640/// RequiredPass is run on the fly by Pass Manager when P requests it
1641/// through getAnalysis interface.
1642void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001643 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1644 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001645 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001646 RequiredPass->getPotentialPassManagerType()) &&
1647 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001648
Devang Patel68f72b12007-04-26 17:50:19 +00001649 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001650 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001651 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001652 // FPP is the top level manager.
1653 FPP->setTopLevelManager(FPP);
1654
Devang Patel69e9f6d2007-04-16 20:27:05 +00001655 OnTheFlyManagers[P] = FPP;
1656 }
Devang Patel68f72b12007-04-26 17:50:19 +00001657 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001658
Devang Patel68f72b12007-04-26 17:50:19 +00001659 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001660 if (RequiredPass) {
1661 SmallVector<Pass *, 1> LU;
1662 LU.push_back(RequiredPass);
1663 FPP->setLastUser(LU, P);
1664 }
Devang Patele64d3052007-04-16 20:12:57 +00001665}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001666
Dan Gohmande6188a2010-08-12 23:50:08 +00001667/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001668/// required by module pass MP. Instantiate analysis pass, by using
1669/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001670Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001671 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001672 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001673
Torok Edwin24c78352009-06-29 18:49:09 +00001674 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001675 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001676 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001677}
1678
1679
Devang Patela1514cb2006-12-07 19:39:39 +00001680//===----------------------------------------------------------------------===//
1681// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001682
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +00001683bool PassManagerImpl::doInitialization() {
Owen Anderson1aa27512012-11-15 00:14:15 +00001684 bool Changed = false;
1685
1686 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1687 Changed |= getContainedManager(Index)->doInitialization();
1688
1689 return Changed;
1690}
1691
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +00001692bool PassManagerImpl::doFinalization() {
Owen Anderson1aa27512012-11-15 00:14:15 +00001693 bool Changed = false;
1694
1695 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1696 Changed |= getContainedManager(Index)->doFinalization();
1697
1698 return Changed;
1699}
1700
Devang Patelab97cf42006-12-13 00:09:23 +00001701//
Devang Patelc290c8a2006-11-07 22:23:34 +00001702/// run - Execute all of the passes scheduled for execution. Keep track of
1703/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001704bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001705 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001706 TimingInfo::createTheTimeInfo();
1707
Devang Patelcfd70c42006-12-13 22:10:00 +00001708 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001709 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001710
Devang Patele3068402006-12-21 00:16:50 +00001711 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001712 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1713 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001714 return Changed;
1715}
Devang Patel376fefa2006-11-08 10:29:57 +00001716
Devang Patela1514cb2006-12-07 19:39:39 +00001717//===----------------------------------------------------------------------===//
1718// PassManager implementation
1719
Devang Patel376fefa2006-11-08 10:29:57 +00001720/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001721PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001722 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001723 // PM is the top level manager
1724 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001725}
1726
Devang Patelb67904d2006-12-13 02:36:01 +00001727PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001728 delete PM;
1729}
1730
Devang Patel376fefa2006-11-08 10:29:57 +00001731/// add - Add a pass to the queue of passes to run. This passes ownership of
1732/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1733/// will be destroyed as well, so there is no need to delete the pass. This
1734/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001735void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001736 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001737}
1738
1739/// run - Execute all of the passes scheduled for execution. Keep track of
1740/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001741bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001742 return PM->run(M);
1743}
1744
Owen Anderson1aa27512012-11-15 00:14:15 +00001745/// doInitialization - Run all of the initializers for the module passes.
1746///
1747bool PassManager::doInitialization() {
1748 return PM->doInitialization();
1749}
1750
1751/// doFinalization - Run all of the finalizers for the module passes.
1752///
1753bool PassManager::doFinalization() {
1754 return PM->doFinalization();
1755}
1756
Devang Patelb8817b92006-12-14 00:59:42 +00001757//===----------------------------------------------------------------------===//
1758// TimingInfo Class - This class is used to calculate information about the
1759// amount of time each pass takes to execute. This only happens with
1760// -time-passes is enabled on the command line.
1761//
1762bool llvm::TimePassesIsEnabled = false;
1763static cl::opt<bool,true>
1764EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1765 cl::desc("Time each pass, printing elapsed time for each on exit"));
1766
1767// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1768// a non null value (if the -time-passes option is enabled) or it leaves it
1769// null. It may be called multiple times.
1770void TimingInfo::createTheTimeInfo() {
1771 if (!TimePassesIsEnabled || TheTimeInfo) return;
1772
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001773 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001774 // This guarantees that the object will be constructed before static globals,
1775 // thus it will be destroyed before them.
1776 static ManagedStatic<TimingInfo> TTI;
1777 TheTimeInfo = &*TTI;
1778}
1779
Devang Patel1c3633e2007-01-29 23:10:37 +00001780/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001781Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001782 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001783 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001784 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001785}
1786
Devang Patel1c56a632007-01-08 19:29:38 +00001787//===----------------------------------------------------------------------===//
1788// PMStack implementation
1789//
Devang Patelad98d232007-01-11 22:15:30 +00001790
Devang Patel1c56a632007-01-08 19:29:38 +00001791// Pop Pass Manager from the stack and clear its analysis info.
1792void PMStack::pop() {
1793
1794 PMDataManager *Top = this->top();
1795 Top->initializeAnalysisInfo();
1796
1797 S.pop_back();
1798}
1799
1800// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001801void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001802 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001803 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001804
Chris Lattner60987362009-03-06 05:53:14 +00001805 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001806 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1807 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001808 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001809
Chris Lattner60987362009-03-06 05:53:14 +00001810 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001811 TPM->addIndirectPassManager(PM);
1812 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001813 PM->setDepth(this->top()->getDepth()+1);
1814 }
1815 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001816 assert((PM->getPassManagerType() == PMT_ModulePassManager
1817 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001818 && "pushing bad pass manager to PMStack");
1819 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001820 }
1821
Devang Patel15701b52007-01-11 00:19:00 +00001822 S.push_back(PM);
1823}
1824
1825// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001826void PMStack::dump() const {
1827 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001828 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001829 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001830
Devang Patel15701b52007-01-11 00:19:00 +00001831 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001832 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001833}
1834
Devang Patel1c56a632007-01-08 19:29:38 +00001835/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001836/// add self into that manager.
1837void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001838 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001839 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001840 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001841 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1842 if (TopPMType == PreferredType)
1843 break; // We found desired pass manager
1844 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001845 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001846 else
1847 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001848 }
Devang Patel18ff6362008-09-09 21:38:40 +00001849 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001850 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001851}
1852
Devang Patel3312f752007-01-16 21:43:18 +00001853/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001854/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001855void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001856 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001857
Andrew Trickcbc845f2012-02-01 07:16:20 +00001858 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001859 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001860 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1861 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001862 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001863 break;
Devang Patel3312f752007-01-16 21:43:18 +00001864 }
Devang Patel3312f752007-01-16 21:43:18 +00001865
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001866 // Create new Function Pass Manager if needed.
1867 FPPassManager *FPP;
1868 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1869 FPP = (FPPassManager *)PMS.top();
1870 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001871 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1872 PMDataManager *PMD = PMS.top();
1873
1874 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001875 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001876 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001877
1878 // [2] Set up new manager's top level manager
1879 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1880 TPM->addIndirectPassManager(FPP);
1881
1882 // [3] Assign manager to manage this new manager. This may create
1883 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001884 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001885
1886 // [4] Push new manager into PMS
1887 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001888 }
1889
Devang Patel3312f752007-01-16 21:43:18 +00001890 // Assign FPP as the manager of this pass.
1891 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001892}
1893
Devang Patel3312f752007-01-16 21:43:18 +00001894/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001895/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001896void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001897 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001898 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001899
Devang Patel15701b52007-01-11 00:19:00 +00001900 // Basic Pass Manager is a leaf pass manager. It does not handle
1901 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001902 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001903 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1904 BBP = (BBPassManager *)PMS.top();
1905 } else {
1906 // If leaf manager is not Basic Block Pass manager then create new
1907 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001908 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1909 PMDataManager *PMD = PMS.top();
1910
1911 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001912 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001913
1914 // [2] Set up new manager's top level manager
1915 // Basic Block Pass Manager does not live by itself
1916 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1917 TPM->addIndirectPassManager(BBP);
1918
Devang Patel15701b52007-01-11 00:19:00 +00001919 // [3] Assign manager to manage this new manager. This may create
1920 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001921 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001922
Devang Patel3312f752007-01-16 21:43:18 +00001923 // [4] Push new manager into PMS
1924 PMS.push(BBP);
1925 }
Devang Patel1c56a632007-01-08 19:29:38 +00001926
Devang Patel3312f752007-01-16 21:43:18 +00001927 // Assign BBP as the manager of this pass.
1928 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001929}
1930
Dan Gohmand3a20c92008-03-11 16:41:42 +00001931PassManagerBase::~PassManagerBase() {}