blob: 53f11499e4b975ded1f1cd243083d8cf9e3bd429 [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
Devang Patelf9d96b92006-12-07 19:57:52 +0000312 /// Pass Manager itself does not invalidate any analysis info.
313 void getAnalysisUsage(AnalysisUsage &Info) const {
314 Info.setPreservesAll();
315 }
316
Devang Patele64d3052007-04-16 20:12:57 +0000317 /// Add RequiredPass into list of lower level passes required by pass P.
318 /// RequiredPass is run on the fly by Pass Manager when P requests it
319 /// through getAnalysis interface.
320 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
321
Dan Gohmande6188a2010-08-12 23:50:08 +0000322 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000323 /// required by module pass MP. Instantiate analysis pass, by using
324 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000325 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000326
Devang Patele3858e62007-02-01 22:08:25 +0000327 virtual const char *getPassName() const {
328 return "Module Pass Manager";
329 }
330
Chris Lattner2fa26e52010-01-22 05:24:46 +0000331 virtual PMDataManager *getAsPMDataManager() { return this; }
332 virtual Pass *getAsPass() { return this; }
333
Devang Pateleda56172006-12-12 23:34:33 +0000334 // Print passes managed by this manager
335 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000336 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000337 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
338 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000339 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000340 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
341 OnTheFlyManagers.find(MP);
342 if (I != OnTheFlyManagers.end())
343 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000344 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000345 }
346 }
347
Devang Patelabfbe3b2006-12-16 00:56:26 +0000348 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000349 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000350 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000351 }
352
Dan Gohmande6188a2010-08-12 23:50:08 +0000353 virtual PassManagerType getPassManagerType() const {
354 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000355 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000356
357 private:
358 /// Collection of on the fly FPPassManagers. These managers manage
359 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000360 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000361};
362
Devang Patel8c78a0b2007-05-03 01:11:54 +0000363char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000364//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000365// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000366//
Devang Patel09f162c2007-05-01 21:15:47 +0000367
Devang Patel67d6a5e2006-12-19 19:46:59 +0000368/// PassManagerImpl manages MPPassManagers
369class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000370 public PMDataManager,
371 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000372 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000373
374public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000375 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000376 explicit PassManagerImpl() :
377 Pass(PT_PassManager, ID), PMDataManager(),
378 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000379
Devang Patel376fefa2006-11-08 10:29:57 +0000380 /// add - Add a pass to the queue of passes to run. This passes ownership of
381 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
382 /// will be destroyed as well, so there is no need to delete the pass. This
383 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000384 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000385 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000386 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000387
388 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000389 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
390 return createPrintModulePass(&O, false, Banner);
391 }
392
Devang Patel376fefa2006-11-08 10:29:57 +0000393 /// run - Execute all of the passes scheduled for execution. Keep track of
394 /// whether any of the passes modifies the module, and if so, return true.
395 bool run(Module &M);
396
Devang Patelf9d96b92006-12-07 19:57:52 +0000397 /// Pass Manager itself does not invalidate any analysis info.
398 void getAnalysisUsage(AnalysisUsage &Info) const {
399 Info.setPreservesAll();
400 }
401
Chris Lattner2fa26e52010-01-22 05:24:46 +0000402 virtual PMDataManager *getAsPMDataManager() { return this; }
403 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000404 virtual PassManagerType getTopLevelPassManagerType() {
405 return PMT_ModulePassManager;
406 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000407
Devang Patel67d6a5e2006-12-19 19:46:59 +0000408 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000409 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000410 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
411 return MP;
412 }
Devang Patel376fefa2006-11-08 10:29:57 +0000413};
414
David Blaikiea379b1812011-12-20 02:50:00 +0000415void PassManagerImpl::anchor() {}
416
Devang Patel8c78a0b2007-05-03 01:11:54 +0000417char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000418} // End of llvm namespace
419
420namespace {
421
422//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000423/// TimingInfo Class - This class is used to calculate information about the
424/// amount of time each pass takes to execute. This only happens when
425/// -time-passes is enabled on the command line.
426///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000427
Owen Anderson5a6960f2009-06-18 20:51:00 +0000428static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000429
Nick Lewycky02d5f772009-10-25 06:33:48 +0000430class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000431 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000432 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000433public:
434 // Use 'create' member to get this.
435 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000436
Devang Patel1c3633e2007-01-29 23:10:37 +0000437 // TimingDtor - Print out information about timing information
438 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000439 // Delete all of the timers, which accumulate their info into the
440 // TimerGroup.
441 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
442 E = TimingData.end(); I != E; ++I)
443 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000444 // TimerGroup is deleted next, printing the report.
445 }
446
447 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
448 // to a non null value (if the -time-passes option is enabled) or it leaves it
449 // null. It may be called multiple times.
450 static void createTheTimeInfo();
451
Chris Lattner707431c2010-03-30 04:03:22 +0000452 /// getPassTimer - Return the timer for the specified pass if it exists.
453 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000454 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000455 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000456
Owen Anderson5c96ef72009-07-07 18:33:04 +0000457 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000458 Timer *&T = TimingData[P];
459 if (T == 0)
460 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000461 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000462 }
463};
464
Devang Patel1c3633e2007-01-29 23:10:37 +0000465} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000466
Dan Gohmand78c4002008-05-13 00:00:25 +0000467static TimingInfo *TheTimeInfo;
468
Devang Patela1514cb2006-12-07 19:39:39 +0000469//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000470// PMTopLevelManager implementation
471
Devang Patel4268fc02007-01-16 02:00:38 +0000472/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000473PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
474 PMDM->setTopLevelManager(this);
475 addPassManager(PMDM);
476 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000477}
478
Devang Patelafb1f3622006-12-12 22:35:25 +0000479/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000480void
Bill Wendlingea857e12012-05-14 07:53:40 +0000481PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000482 unsigned PDepth = 0;
483 if (P->getResolver())
484 PDepth = P->getResolver()->getPMDataManager().getDepth();
485
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000486 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000487 E = AnalysisPasses.end(); I != E; ++I) {
488 Pass *AP = *I;
489 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000490
Devang Patel01919d22007-03-08 19:05:01 +0000491 if (P == AP)
492 continue;
493
Tobias Grosserf07426b2011-01-20 21:03:22 +0000494 // Update the last users of passes that are required transitive by AP.
495 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
496 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
497 SmallVector<Pass *, 12> LastUses;
498 SmallVector<Pass *, 12> LastPMUses;
499 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
500 E = IDs.end(); I != E; ++I) {
501 Pass *AnalysisPass = findAnalysisPass(*I);
502 assert(AnalysisPass && "Expected analysis pass to exist.");
503 AnalysisResolver *AR = AnalysisPass->getResolver();
504 assert(AR && "Expected analysis resolver to exist.");
505 unsigned APDepth = AR->getPMDataManager().getDepth();
506
507 if (PDepth == APDepth)
508 LastUses.push_back(AnalysisPass);
509 else if (PDepth > APDepth)
510 LastPMUses.push_back(AnalysisPass);
511 }
512
513 setLastUser(LastUses, P);
514
515 // If this pass has a corresponding pass manager, push higher level
516 // analysis to this pass manager.
517 if (P->getResolver())
518 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
519
520
Devang Patelafb1f3622006-12-12 22:35:25 +0000521 // If AP is the last user of other passes then make P last user of
522 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000523 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000524 LUE = LastUser.end(); LUI != LUE; ++LUI) {
525 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000526 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000527 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000528 LastUser[LUI->first] = P;
529 }
530 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000531}
532
533/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000534void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000535 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000536 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000537 InversedLastUser.find(P);
538 if (DMI == InversedLastUser.end())
539 return;
540
541 SmallPtrSet<Pass *, 8> &LU = DMI->second;
542 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
543 E = LU.end(); I != E; ++I) {
544 LastUses.push_back(*I);
545 }
546
Devang Patelafb1f3622006-12-12 22:35:25 +0000547}
548
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000549AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
550 AnalysisUsage *AnUsage = NULL;
551 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000552 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000553 AnUsage = DMI->second;
554 else {
555 AnUsage = new AnalysisUsage();
556 P->getAnalysisUsage(*AnUsage);
557 AnUsageMap[P] = AnUsage;
558 }
559 return AnUsage;
560}
561
Devang Patelafb1f3622006-12-12 22:35:25 +0000562/// Schedule pass P for execution. Make sure that passes required by
563/// P are run before P is run. Update analysis info maintained by
564/// the manager. Remove dead passes. This is a recursive function.
565void PMTopLevelManager::schedulePass(Pass *P) {
566
Devang Patel3312f752007-01-16 21:43:18 +0000567 // TODO : Allocate function manager for this pass, other wise required set
568 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000569
Devang Pateld74ede72007-03-06 01:06:16 +0000570 // Give pass a chance to prepare the stage.
571 P->preparePassManager(activeStack);
572
Devang Patel864970e2008-03-18 00:39:19 +0000573 // If P is an analysis pass and it is available then do not
574 // generate the analysis again. Stale analysis info should not be
575 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000576 const PassInfo *PI =
577 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
578 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000579 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000580 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000581 }
Devang Patel864970e2008-03-18 00:39:19 +0000582
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000583 AnalysisUsage *AnUsage = findAnalysisUsage(P);
584
Devang Patelfdee7032008-08-14 23:07:48 +0000585 bool checkAnalysis = true;
586 while (checkAnalysis) {
587 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000588
Devang Patelfdee7032008-08-14 23:07:48 +0000589 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
590 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
591 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000592
Devang Patelfdee7032008-08-14 23:07:48 +0000593 Pass *AnalysisPass = findAnalysisPass(*I);
594 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000595 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000596
597 if (PI == NULL) {
598 // Pass P is not in the global PassRegistry
599 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
600 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
601 dbgs() << "Required Passes:" << "\n";
602 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
603 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
604 Pass *AnalysisPass2 = findAnalysisPass(*I2);
605 if (AnalysisPass2) {
606 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
607 }
608 else {
609 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
610 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
611 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
612 }
613 }
614 }
615
Andrew Trick6bbaf132011-06-03 00:48:58 +0000616 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000617 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000618 if (P->getPotentialPassManagerType () ==
619 AnalysisPass->getPotentialPassManagerType())
620 // Schedule analysis pass that is managed by the same pass manager.
621 schedulePass(AnalysisPass);
622 else if (P->getPotentialPassManagerType () >
623 AnalysisPass->getPotentialPassManagerType()) {
624 // Schedule analysis pass that is managed by a new manager.
625 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000626 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000627 // are already checked are still available.
628 checkAnalysis = true;
629 }
630 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000631 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000632 // passes are run on the fly.
633 delete AnalysisPass;
634 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000635 }
636 }
637
638 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000639 if (ImmutablePass *IP = P->getAsImmutablePass()) {
640 // P is a immutable pass and it will be managed by this
641 // top level manager. Set up analysis resolver to connect them.
642 PMDataManager *DM = getAsPMDataManager();
643 AnalysisResolver *AR = new AnalysisResolver(*DM);
644 P->setResolver(AR);
645 DM->initializeAnalysisImpl(P);
646 addImmutablePass(IP);
647 DM->recordAvailableAnalysis(IP);
648 return;
649 }
650
651 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
652 Pass *PP = P->createPrinterPass(
653 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
654 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
655 }
656
657 // Add the requested pass to the best available pass manager.
658 P->assignPassManager(activeStack, getTopLevelPassManagerType());
659
660 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
661 Pass *PP = P->createPrinterPass(
662 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
663 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
664 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000665}
666
667/// Find the pass that implements Analysis AID. Search immutable
668/// passes and all pass managers. If desired pass is not found
669/// then return NULL.
670Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
671
Devang Patelcd6ba152006-12-12 22:50:05 +0000672 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000673 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000674 E = PassManagers.end(); I != E; ++I)
675 if (Pass *P = (*I)->findAnalysisPass(AID, false))
676 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000677
678 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000679 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000680 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000681 E = IndirectPassManagers.end(); I != E; ++I)
682 if (Pass *P = (*I)->findAnalysisPass(AID, false))
683 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000684
Dan Gohman844dd0a2010-10-11 23:19:01 +0000685 // Check the immutable passes. Iterate in reverse order so that we find
686 // the most recently registered passes first.
687 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
688 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000689 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000690 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000691 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000692
693 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000694 const PassInfo *PassInf =
695 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000696 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000697 const std::vector<const PassInfo*> &ImmPI =
698 PassInf->getInterfacesImplemented();
699 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
700 EE = ImmPI.end(); II != EE; ++II) {
701 if ((*II)->getTypeInfo() == AID)
702 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000703 }
704 }
705
Dan Gohman844dd0a2010-10-11 23:19:01 +0000706 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000707}
708
Devang Pateleda56172006-12-12 23:34:33 +0000709// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000710void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000711
Devang Patelfd4184322007-01-17 20:33:36 +0000712 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000713 return;
714
Devang Pateleda56172006-12-12 23:34:33 +0000715 // Print out the immutable passes
716 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000717 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000718 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000719
Dan Gohmanf71c5212010-08-19 01:29:07 +0000720 // Every class that derives from PMDataManager also derives from Pass
721 // (sometimes indirectly), but there's no inheritance relationship
722 // between PMDataManager and Pass, so we have to getAsPass to get
723 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000724 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000725 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000726 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000727}
728
Devang Patel991aeba2006-12-15 20:13:01 +0000729void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000730
Devang Patelfd4184322007-01-17 20:33:36 +0000731 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000732 return;
733
David Greene994e1bb2010-01-05 01:30:02 +0000734 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000735 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
736 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
737 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000738 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
739 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000740 if (!PI->isAnalysisGroup())
741 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000742 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000743 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000744 E = PassManagers.end(); I != E; ++I)
745 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000746 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000747}
748
Devang Patele3068402006-12-21 00:16:50 +0000749void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000750 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000751 E = PassManagers.end(); I != E; ++I)
752 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000753
Devang Patele3068402006-12-21 00:16:50 +0000754 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000755 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000756 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
757 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000758 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000759
Chris Lattner60987362009-03-06 05:53:14 +0000760 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000761 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000762 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000763 InversedLastUser.find(DMI->second);
764 if (InvDMI != InversedLastUser.end()) {
765 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
766 L.insert(DMI->first);
767 } else {
768 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
769 InversedLastUser[DMI->second] = L;
770 }
771 }
Devang Patele3068402006-12-21 00:16:50 +0000772}
773
Devang Patele7599552007-01-12 18:52:44 +0000774/// Destructor
775PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000776 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000777 E = PassManagers.end(); I != E; ++I)
778 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000779
Dan Gohman060d5ba2010-10-12 00:15:27 +0000780 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000781 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
782 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000783
784 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000785 DME = AnUsageMap.end(); DMI != DME; ++DMI)
786 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000787}
788
Devang Patelafb1f3622006-12-12 22:35:25 +0000789//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000790// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000791
Devang Patel643676c2006-11-11 01:10:19 +0000792/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000793void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000794 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000795
Chris Lattner60987362009-03-06 05:53:14 +0000796 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000797
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000798 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000799
Dan Gohmande6188a2010-08-12 23:50:08 +0000800 // This pass is the current implementation of all of the interfaces it
801 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000802 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
803 if (PInf == 0) return;
804 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000805 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000806 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000807}
808
Devang Patel9d9fc902007-03-06 17:52:53 +0000809// Return true if P preserves high level analysis used by other
810// passes managed by this manager
811bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000812 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000813 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000814 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000815
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000816 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000817 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000818 E = HigherLevelAnalysis.end(); I != E; ++I) {
819 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000820 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000821 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000822 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000823 PreservedSet.end())
824 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000825 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000826
Devang Patel9d9fc902007-03-06 17:52:53 +0000827 return true;
828}
829
Chris Lattner02eb94c2008-08-07 07:34:50 +0000830/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000831void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000832 // Don't do this unless assertions are enabled.
833#ifdef NDEBUG
834 return;
835#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000836 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
837 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000838
Devang Patelef432532007-07-19 05:36:09 +0000839 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000840 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000841 E = PreservedSet.end(); I != E; ++I) {
842 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000843 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000844 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000845 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000846 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000847 }
848}
849
Devang Patel67c79a42008-07-01 19:50:56 +0000850/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000851void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000852 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
853 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000854 return;
855
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000856 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000857 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000858 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000859 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000860 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000861 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000862 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000863 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000864 if (PassDebugging >= Details) {
865 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000866 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
867 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000868 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000869 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000870 }
Devang Patel349170f2006-11-11 01:24:55 +0000871 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000872
Devang Patel42dd1e92007-03-06 01:55:46 +0000873 // Check inherited analysis also. If P is not preserving analysis
874 // provided by parent manager then remove it here.
875 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
876
877 if (!InheritedAnalysis[Index])
878 continue;
879
Dan Gohmande6188a2010-08-12 23:50:08 +0000880 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000881 I = InheritedAnalysis[Index]->begin(),
882 E = InheritedAnalysis[Index]->end(); I != E; ) {
883 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000884 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000885 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000886 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000887 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000888 if (PassDebugging >= Details) {
889 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000890 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
891 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000892 }
Devang Patel01919d22007-03-08 19:05:01 +0000893 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000894 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000895 }
896 }
Devang Patelf68a3492006-11-07 22:35:17 +0000897}
898
Devang Patelca189262006-11-14 03:05:08 +0000899/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000900void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000901 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000902
Devang Patel8adae862007-07-20 18:04:54 +0000903 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000904
Devang Patel2ff44922007-04-16 20:39:59 +0000905 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000906 if (!TPM)
907 return;
908
Devang Patel17ad0962006-12-08 00:37:52 +0000909 TPM->collectLastUses(DeadPasses, P);
910
Devang Patel656a9172008-06-06 17:50:36 +0000911 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000912 dbgs() << " -*- '" << P->getPassName();
913 dbgs() << "' is the last user of following pass instances.";
914 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000915 }
916
Dan Gohman060d5ba2010-10-12 00:15:27 +0000917 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000918 E = DeadPasses.end(); I != E; ++I)
919 freePass(*I, Msg, DBG_STR);
920}
Devang Patel200d3052006-12-13 23:50:44 +0000921
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000922void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000923 enum PassDebuggingString DBG_STR) {
924 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000925
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000926 {
927 // If the pass crashes releasing memory, remember this.
928 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000929 TimeRegion PassTimer(getPassTimer(P));
930
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000931 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000932 }
933
Owen Andersona7aed182010-08-06 18:33:48 +0000934 AnalysisID PI = P->getPassID();
935 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000936 // Remove the pass itself (if it is not already removed).
937 AvailableAnalysis.erase(PI);
938
939 // Remove all interfaces this pass implements, for which it is also
940 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000941 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000942 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000943 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000944 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000945 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000946 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000947 }
Devang Patel17ad0962006-12-08 00:37:52 +0000948 }
Devang Patelca189262006-11-14 03:05:08 +0000949}
950
Dan Gohmande6188a2010-08-12 23:50:08 +0000951/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000952/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000953void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000954 // This manager is going to manage pass P. Set up analysis resolver
955 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000956 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000957 P->setResolver(AR);
958
Devang Patelec2b9a72007-03-05 22:57:49 +0000959 // If a FunctionPass F is the last user of ModulePass info M
960 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000961 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000962
Chris Lattner60987362009-03-06 05:53:14 +0000963 if (!ProcessAnalysis) {
964 // Add pass
965 PassVector.push_back(P);
966 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000967 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000968
Chris Lattner60987362009-03-06 05:53:14 +0000969 // At the moment, this pass is the last user of all required passes.
970 SmallVector<Pass *, 12> LastUses;
971 SmallVector<Pass *, 8> RequiredPasses;
972 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
973
974 unsigned PDepth = this->getDepth();
975
Dan Gohmande6188a2010-08-12 23:50:08 +0000976 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000977 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000978 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000979 E = RequiredPasses.end(); I != E; ++I) {
980 Pass *PRequired = *I;
981 unsigned RDepth = 0;
982
983 assert(PRequired->getResolver() && "Analysis Resolver is not set");
984 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
985 RDepth = DM.getDepth();
986
987 if (PDepth == RDepth)
988 LastUses.push_back(PRequired);
989 else if (PDepth > RDepth) {
990 // Let the parent claim responsibility of last use
991 TransferLastUses.push_back(PRequired);
992 // Keep track of higher level analysis used by this manager.
993 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000994 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000995 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000996 }
997
998 // Set P as P's last user until someone starts using P.
999 // However, if P is a Pass Manager then it does not need
1000 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001001 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001002 LastUses.push_back(P);
1003 TPM->setLastUser(LastUses, P);
1004
1005 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001006 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001007 TPM->setLastUser(TransferLastUses, My_PM);
1008 TransferLastUses.clear();
1009 }
1010
Dan Gohman6304db32010-08-16 22:57:28 +00001011 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001012 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001013 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001014 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001015 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1016 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001017 this->addLowerLevelRequiredPass(P, AnalysisPass);
1018 }
1019
1020 // Take a note of analysis required and made available by this pass.
1021 // Remove the analysis not preserved by this pass
1022 removeNotPreservedAnalysis(P);
1023 recordAvailableAnalysis(P);
1024
Devang Patel8cad70d2006-11-11 01:51:02 +00001025 // Add pass
1026 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001027}
1028
Devang Patele64d3052007-04-16 20:12:57 +00001029
1030/// Populate RP with analysis pass that are required by
1031/// pass P and are available. Populate RP_NotAvail with analysis
1032/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001033void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1034 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001035 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001036 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1037 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001038 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001039 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001040 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001041 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001042 else
Chris Lattner60987362009-03-06 05:53:14 +00001043 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001044 }
Devang Patelf58183d2006-12-12 23:09:32 +00001045
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001046 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001047 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001048 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001049 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001050 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001051 else
Chris Lattner60987362009-03-06 05:53:14 +00001052 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001053 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001054}
1055
Devang Patel07f4f582006-11-14 21:49:36 +00001056// All Required analyses should be available to the pass as it runs! Here
1057// we fill in the AnalysisImpls member of the pass so that it can
1058// successfully use the getAnalysis() method to retrieve the
1059// implementations it needs.
1060//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001061void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001062 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1063
Chris Lattnercbd160f2008-08-08 05:33:04 +00001064 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001065 I = AnUsage->getRequiredSet().begin(),
1066 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001067 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001068 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001069 // This may be analysis pass that is initialized on the fly.
1070 // If that is not the case then it will raise an assert when it is used.
1071 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001072 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001073 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001074 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001075 }
1076}
1077
Devang Patel640c5bb2006-12-08 22:30:11 +00001078/// Find the pass that implements Analysis AID. If desired pass is not found
1079/// then return NULL.
1080Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1081
1082 // Check if AvailableAnalysis map has one entry.
1083 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1084
1085 if (I != AvailableAnalysis.end())
1086 return I->second;
1087
1088 // Search Parents through TopLevelManager
1089 if (SearchParent)
1090 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001091
Devang Patel9d759b82006-12-09 00:09:12 +00001092 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001093}
1094
Devang Patel991aeba2006-12-15 20:13:01 +00001095// Print list of passes that are last used by P.
1096void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1097
Devang Patel8adae862007-07-20 18:04:54 +00001098 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001099
1100 // If this is a on the fly manager then it does not have TPM.
1101 if (!TPM)
1102 return;
1103
Devang Patel991aeba2006-12-15 20:13:01 +00001104 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001105
Dan Gohman7224bce2010-10-12 00:11:18 +00001106 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001107 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001108 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001109 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001110 }
1111}
1112
1113void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001114 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001115 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001116 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001117 PMD->dumpPassArguments();
1118 else
Owen Andersona7aed182010-08-06 18:33:48 +00001119 if (const PassInfo *PI =
1120 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001121 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001122 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001123 }
1124}
1125
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001126void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1127 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001128 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001129 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001130 return;
David Greene994e1bb2010-01-05 01:30:02 +00001131 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001132 switch (S1) {
1133 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001134 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001135 break;
1136 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001137 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001138 break;
1139 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 default:
1143 break;
1144 }
1145 switch (S2) {
1146 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001147 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001148 break;
1149 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001151 break;
1152 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001153 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001154 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001155 case ON_REGION_MSG:
1156 dbgs() << "' on Region '" << Msg << "'...\n";
1157 break;
Devang Patel003a5592007-03-05 20:01:30 +00001158 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 default:
1165 break;
1166 }
Devang Patel991aeba2006-12-15 20:13:01 +00001167}
1168
Chris Lattner4c1e9542009-03-06 06:45:05 +00001169void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001170 if (PassDebugging < Details)
1171 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001172
Chris Lattner4c493d92008-08-08 15:14:09 +00001173 AnalysisUsage analysisUsage;
1174 P->getAnalysisUsage(analysisUsage);
1175 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1176}
1177
Chris Lattner4c1e9542009-03-06 06:45:05 +00001178void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001179 if (PassDebugging < Details)
1180 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001181
Chris Lattner4c493d92008-08-08 15:14:09 +00001182 AnalysisUsage analysisUsage;
1183 P->getAnalysisUsage(analysisUsage);
1184 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1185}
1186
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001187void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001188 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001189 assert(PassDebugging >= Details);
1190 if (Set.empty())
1191 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001192 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001193 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001194 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001195 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001196 if (!PInf) {
1197 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1198 // all drivers.
1199 dbgs() << " Uninitialized Pass";
1200 continue;
1201 }
Owen Andersona7aed182010-08-06 18:33:48 +00001202 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001203 }
David Greene994e1bb2010-01-05 01:30:02 +00001204 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001205}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001206
Devang Patel004937b2007-07-27 20:06:09 +00001207/// Add RequiredPass into list of lower level passes required by pass P.
1208/// RequiredPass is run on the fly by Pass Manager when P requests it
1209/// through getAnalysis interface.
1210/// This should be handled by specific pass manager.
1211void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1212 if (TPM) {
1213 TPM->dumpArguments();
1214 TPM->dumpPasses();
1215 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001216
Dan Gohmande6188a2010-08-12 23:50:08 +00001217 // Module Level pass may required Function Level analysis info
1218 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1219 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001220 // module level pass is requiring lower level analysis info managed by
1221 // lower level pass manager.
1222
1223 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001224 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001225 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001226#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001227 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1228 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001229#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001230 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001231}
1232
Owen Andersona7aed182010-08-06 18:33:48 +00001233Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001234 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001235}
1236
Devang Patele7599552007-01-12 18:52:44 +00001237// Destructor
1238PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001239 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001240 E = PassVector.end(); I != E; ++I)
1241 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001242}
1243
Devang Patel9bdf7d42006-12-08 23:28:54 +00001244//===----------------------------------------------------------------------===//
1245// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001246// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1247Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001248 return PM.findAnalysisPass(ID, dir);
1249}
1250
Dan Gohmande6188a2010-08-12 23:50:08 +00001251Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001252 Function &F) {
1253 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1254}
1255
Devang Patela1514cb2006-12-07 19:39:39 +00001256//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001257// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001258
Dan Gohmande6188a2010-08-12 23:50:08 +00001259/// Execute all of the passes scheduled for execution by invoking
1260/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001261/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001262bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001263 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001264 return false;
1265
Devang Patele9585592006-12-08 01:38:28 +00001266 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001267
Devang Patel6e5a1132006-11-07 21:31:57 +00001268 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001269 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1270 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001271 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001272
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001273 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001274 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001275
Devang Patelabfbe3b2006-12-16 00:56:26 +00001276 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001277
Chris Lattner4c1e9542009-03-06 06:45:05 +00001278 {
1279 // If the pass crashes, remember this.
1280 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001281 TimeRegion PassTimer(getPassTimer(BP));
1282
Dan Gohman74b189f2010-03-01 17:34:28 +00001283 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001284 }
Devang Patel93a197c2006-12-14 00:08:04 +00001285
Dan Gohman74b189f2010-03-01 17:34:28 +00001286 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001287 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001288 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001289 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001290 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001291
Devang Patela273d1c2007-07-19 18:02:32 +00001292 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001293 removeNotPreservedAnalysis(BP);
1294 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001295 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001296 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001297
Bill Wendling6ce6d262009-12-25 13:50:18 +00001298 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001299}
1300
Devang Patel475c4532006-12-08 00:59:05 +00001301// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001302bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001303 bool Changed = false;
1304
Chris Lattner4c1e9542009-03-06 06:45:05 +00001305 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1306 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001307
1308 return Changed;
1309}
1310
Duncan Sands51495602009-02-13 09:42:34 +00001311bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001312 bool Changed = false;
1313
Chris Lattner4c1e9542009-03-06 06:45:05 +00001314 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1315 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001316
1317 return Changed;
1318}
1319
Duncan Sands51495602009-02-13 09:42:34 +00001320bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001321 bool Changed = false;
1322
Devang Patelabfbe3b2006-12-16 00:56:26 +00001323 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1324 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001325 Changed |= BP->doInitialization(F);
1326 }
1327
1328 return Changed;
1329}
1330
Duncan Sands51495602009-02-13 09:42:34 +00001331bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001332 bool Changed = false;
1333
Devang Patelabfbe3b2006-12-16 00:56:26 +00001334 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1335 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001336 Changed |= BP->doFinalization(F);
1337 }
1338
1339 return Changed;
1340}
1341
1342
Devang Patela1514cb2006-12-07 19:39:39 +00001343//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001344// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001345
Devang Patel4e12f862006-11-08 10:44:40 +00001346/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001347FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001348 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001349 // FPM is the top level manager.
1350 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001351
Dan Gohman565df952008-03-13 02:08:36 +00001352 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001353 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001354}
1355
Devang Patelb67904d2006-12-13 02:36:01 +00001356FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001357 delete FPM;
1358}
1359
Devang Patel4e12f862006-11-08 10:44:40 +00001360/// add - Add a pass to the queue of passes to run. This passes
1361/// ownership of the Pass to the PassManager. When the
1362/// PassManager_X is destroyed, the pass will be destroyed as well, so
1363/// there is no need to delete the pass. (TODO delete passes.)
1364/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001365void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001366 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001367}
1368
Devang Patel9f3083e2006-11-15 19:39:54 +00001369/// run - Execute all of the passes scheduled for execution. Keep
1370/// track of whether any of the passes modifies the function, and if
1371/// so, return true.
1372///
Devang Patelb67904d2006-12-13 02:36:01 +00001373bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001374 if (F.isMaterializable()) {
1375 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001376 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001377 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001378 }
Devang Patel272908d2006-12-08 22:57:48 +00001379 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001380}
1381
1382
Devang Patelff631ae2006-11-15 01:27:05 +00001383/// doInitialization - Run all of the initializers for the function passes.
1384///
Devang Patelb67904d2006-12-13 02:36:01 +00001385bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001386 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001387}
1388
Dan Gohmane6656eb2007-07-30 14:51:13 +00001389/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001390///
Devang Patelb67904d2006-12-13 02:36:01 +00001391bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001392 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001393}
1394
Devang Patela1514cb2006-12-07 19:39:39 +00001395//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396// FunctionPassManagerImpl implementation
1397//
Duncan Sands51495602009-02-13 09:42:34 +00001398bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001399 bool Changed = false;
1400
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001401 dumpArguments();
1402 dumpPasses();
1403
Chris Lattner4c1e9542009-03-06 06:45:05 +00001404 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1405 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001406
1407 return Changed;
1408}
1409
Duncan Sands51495602009-02-13 09:42:34 +00001410bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001411 bool Changed = false;
1412
Chris Lattner4c1e9542009-03-06 06:45:05 +00001413 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1414 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001415
1416 return Changed;
1417}
1418
Devang Patelec9c58f2009-04-01 22:34:41 +00001419/// cleanup - After running all passes, clean up pass manager cache.
1420void FPPassManager::cleanup() {
1421 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1422 FunctionPass *FP = getContainedPass(Index);
1423 AnalysisResolver *AR = FP->getResolver();
1424 assert(AR && "Analysis Resolver is not set");
1425 AR->clearAnalysisImpls();
1426 }
1427}
1428
Torok Edwin24c78352009-06-29 18:49:09 +00001429void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1430 if (!wasRun)
1431 return;
1432 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1433 FPPassManager *FPPM = getContainedManager(Index);
1434 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1435 FPPM->getContainedPass(Index)->releaseMemory();
1436 }
1437 }
Torok Edwin896556e2009-06-29 21:05:10 +00001438 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001439}
1440
Devang Patel67d6a5e2006-12-19 19:46:59 +00001441// Execute all the passes managed by this top level manager.
1442// Return true if any function is modified by a pass.
1443bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001444 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001445 TimingInfo::createTheTimeInfo();
1446
Devang Patele3068402006-12-21 00:16:50 +00001447 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001448 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1449 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001450
1451 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1452 getContainedManager(Index)->cleanup();
1453
Torok Edwin24c78352009-06-29 18:49:09 +00001454 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455 return Changed;
1456}
1457
1458//===----------------------------------------------------------------------===//
1459// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001460
Devang Patel8c78a0b2007-05-03 01:11:54 +00001461char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001462/// Print passes managed by this manager
1463void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001464 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001465 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1466 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001467 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001468 dumpLastUses(FP, Offset+1);
1469 }
1470}
1471
1472
Dan Gohmande6188a2010-08-12 23:50:08 +00001473/// Execute all of the passes scheduled for execution by invoking
1474/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001475/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001476bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001477 if (F.isDeclaration())
1478 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001479
1480 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001481
Devang Patelcbbf2912008-03-20 01:09:53 +00001482 // Collect inherited analysis from Module level pass manager.
1483 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001484
Devang Patelabfbe3b2006-12-16 00:56:26 +00001485 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1486 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001487 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001488
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001489 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001490 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001491
Devang Patelabfbe3b2006-12-16 00:56:26 +00001492 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001493
Chris Lattner4c1e9542009-03-06 06:45:05 +00001494 {
1495 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001496 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001497
Dan Gohman74b189f2010-03-01 17:34:28 +00001498 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001499 }
Devang Patel93a197c2006-12-14 00:08:04 +00001500
Dan Gohman74b189f2010-03-01 17:34:28 +00001501 Changed |= LocalChanged;
1502 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001503 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001504 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001505
Devang Patela273d1c2007-07-19 18:02:32 +00001506 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001507 removeNotPreservedAnalysis(FP);
1508 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001509 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001510 }
1511 return Changed;
1512}
1513
Devang Patel67d6a5e2006-12-19 19:46:59 +00001514bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001516
Dan Gohmane7630be2010-05-11 20:30:00 +00001517 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001518 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001519
Bill Wendling6ce6d262009-12-25 13:50:18 +00001520 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001521}
1522
Duncan Sands51495602009-02-13 09:42:34 +00001523bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001524 bool Changed = false;
1525
Chris Lattner4c1e9542009-03-06 06:45:05 +00001526 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1527 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001528
1529 return Changed;
1530}
1531
Duncan Sands51495602009-02-13 09:42:34 +00001532bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001533 bool Changed = false;
1534
Chris Lattner4c1e9542009-03-06 06:45:05 +00001535 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1536 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001537
Devang Patelff631ae2006-11-15 01:27:05 +00001538 return Changed;
1539}
1540
Devang Patela1514cb2006-12-07 19:39:39 +00001541//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001542// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001543
Dan Gohmande6188a2010-08-12 23:50:08 +00001544/// Execute all of the passes scheduled for execution by invoking
1545/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001546/// the module, and if so, return true.
1547bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001548MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001549 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001550
Torok Edwin24c78352009-06-29 18:49:09 +00001551 // Initialize on-the-fly passes
1552 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1553 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1554 I != E; ++I) {
1555 FunctionPassManagerImpl *FPP = I->second;
1556 Changed |= FPP->doInitialization(M);
1557 }
1558
Devang Patelabfbe3b2006-12-16 00:56:26 +00001559 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1560 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001561 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001562
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001563 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001564 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001565
Devang Patelabfbe3b2006-12-16 00:56:26 +00001566 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001567
Chris Lattner4c1e9542009-03-06 06:45:05 +00001568 {
1569 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001570 TimeRegion PassTimer(getPassTimer(MP));
1571
Dan Gohman74b189f2010-03-01 17:34:28 +00001572 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001573 }
Devang Patel93a197c2006-12-14 00:08:04 +00001574
Dan Gohman74b189f2010-03-01 17:34:28 +00001575 Changed |= LocalChanged;
1576 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001577 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001578 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001579 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001580
Devang Patela273d1c2007-07-19 18:02:32 +00001581 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001582 removeNotPreservedAnalysis(MP);
1583 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001584 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001585 }
Torok Edwin24c78352009-06-29 18:49:09 +00001586
1587 // Finalize on-the-fly passes
1588 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1589 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1590 I != E; ++I) {
1591 FunctionPassManagerImpl *FPP = I->second;
1592 // We don't know when is the last time an on-the-fly pass is run,
1593 // so we need to releaseMemory / finalize here
1594 FPP->releaseMemoryOnTheFly();
1595 Changed |= FPP->doFinalization(M);
1596 }
Devang Patel05e1a972006-11-07 22:03:15 +00001597 return Changed;
1598}
1599
Devang Patele64d3052007-04-16 20:12:57 +00001600/// Add RequiredPass into list of lower level passes required by pass P.
1601/// RequiredPass is run on the fly by Pass Manager when P requests it
1602/// through getAnalysis interface.
1603void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001604 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1605 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001606 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001607 RequiredPass->getPotentialPassManagerType()) &&
1608 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001609
Devang Patel68f72b12007-04-26 17:50:19 +00001610 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001611 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001612 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001613 // FPP is the top level manager.
1614 FPP->setTopLevelManager(FPP);
1615
Devang Patel69e9f6d2007-04-16 20:27:05 +00001616 OnTheFlyManagers[P] = FPP;
1617 }
Devang Patel68f72b12007-04-26 17:50:19 +00001618 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001619
Devang Patel68f72b12007-04-26 17:50:19 +00001620 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001621 if (RequiredPass) {
1622 SmallVector<Pass *, 1> LU;
1623 LU.push_back(RequiredPass);
1624 FPP->setLastUser(LU, P);
1625 }
Devang Patele64d3052007-04-16 20:12:57 +00001626}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001627
Dan Gohmande6188a2010-08-12 23:50:08 +00001628/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001629/// required by module pass MP. Instantiate analysis pass, by using
1630/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001631Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001632 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001633 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001634
Torok Edwin24c78352009-06-29 18:49:09 +00001635 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001636 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001637 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001638}
1639
1640
Devang Patela1514cb2006-12-07 19:39:39 +00001641//===----------------------------------------------------------------------===//
1642// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001643//
Devang Patelc290c8a2006-11-07 22:23:34 +00001644/// run - Execute all of the passes scheduled for execution. Keep track of
1645/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001646bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001647 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001648 TimingInfo::createTheTimeInfo();
1649
Devang Patelcfd70c42006-12-13 22:10:00 +00001650 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001651 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001652
Devang Patele3068402006-12-21 00:16:50 +00001653 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001654 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1655 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001656 return Changed;
1657}
Devang Patel376fefa2006-11-08 10:29:57 +00001658
Devang Patela1514cb2006-12-07 19:39:39 +00001659//===----------------------------------------------------------------------===//
1660// PassManager implementation
1661
Devang Patel376fefa2006-11-08 10:29:57 +00001662/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001663PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001664 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001665 // PM is the top level manager
1666 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001667}
1668
Devang Patelb67904d2006-12-13 02:36:01 +00001669PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001670 delete PM;
1671}
1672
Devang Patel376fefa2006-11-08 10:29:57 +00001673/// add - Add a pass to the queue of passes to run. This passes ownership of
1674/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1675/// will be destroyed as well, so there is no need to delete the pass. This
1676/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001677void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001678 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001679}
1680
1681/// run - Execute all of the passes scheduled for execution. Keep track of
1682/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001683bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001684 return PM->run(M);
1685}
1686
Devang Patelb8817b92006-12-14 00:59:42 +00001687//===----------------------------------------------------------------------===//
1688// TimingInfo Class - This class is used to calculate information about the
1689// amount of time each pass takes to execute. This only happens with
1690// -time-passes is enabled on the command line.
1691//
1692bool llvm::TimePassesIsEnabled = false;
1693static cl::opt<bool,true>
1694EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1695 cl::desc("Time each pass, printing elapsed time for each on exit"));
1696
1697// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1698// a non null value (if the -time-passes option is enabled) or it leaves it
1699// null. It may be called multiple times.
1700void TimingInfo::createTheTimeInfo() {
1701 if (!TimePassesIsEnabled || TheTimeInfo) return;
1702
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001703 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001704 // This guarantees that the object will be constructed before static globals,
1705 // thus it will be destroyed before them.
1706 static ManagedStatic<TimingInfo> TTI;
1707 TheTimeInfo = &*TTI;
1708}
1709
Devang Patel1c3633e2007-01-29 23:10:37 +00001710/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001711Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001712 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001713 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001714 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001715}
1716
Devang Patel1c56a632007-01-08 19:29:38 +00001717//===----------------------------------------------------------------------===//
1718// PMStack implementation
1719//
Devang Patelad98d232007-01-11 22:15:30 +00001720
Devang Patel1c56a632007-01-08 19:29:38 +00001721// Pop Pass Manager from the stack and clear its analysis info.
1722void PMStack::pop() {
1723
1724 PMDataManager *Top = this->top();
1725 Top->initializeAnalysisInfo();
1726
1727 S.pop_back();
1728}
1729
1730// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001731void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001732 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001733 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001734
Chris Lattner60987362009-03-06 05:53:14 +00001735 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001736 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1737 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001738 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001739
Chris Lattner60987362009-03-06 05:53:14 +00001740 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001741 TPM->addIndirectPassManager(PM);
1742 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001743 PM->setDepth(this->top()->getDepth()+1);
1744 }
1745 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001746 assert((PM->getPassManagerType() == PMT_ModulePassManager
1747 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001748 && "pushing bad pass manager to PMStack");
1749 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001750 }
1751
Devang Patel15701b52007-01-11 00:19:00 +00001752 S.push_back(PM);
1753}
1754
1755// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001756void PMStack::dump() const {
1757 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001758 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001759 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001760
Devang Patel15701b52007-01-11 00:19:00 +00001761 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001762 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001763}
1764
Devang Patel1c56a632007-01-08 19:29:38 +00001765/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001766/// add self into that manager.
1767void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001768 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001769 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001770 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001771 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1772 if (TopPMType == PreferredType)
1773 break; // We found desired pass manager
1774 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001775 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001776 else
1777 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001778 }
Devang Patel18ff6362008-09-09 21:38:40 +00001779 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001780 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001781}
1782
Devang Patel3312f752007-01-16 21:43:18 +00001783/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001784/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001785void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001786 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001787
Andrew Trickcbc845f2012-02-01 07:16:20 +00001788 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001789 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001790 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1791 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001792 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001793 break;
Devang Patel3312f752007-01-16 21:43:18 +00001794 }
Devang Patel3312f752007-01-16 21:43:18 +00001795
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001796 // Create new Function Pass Manager if needed.
1797 FPPassManager *FPP;
1798 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1799 FPP = (FPPassManager *)PMS.top();
1800 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001801 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1802 PMDataManager *PMD = PMS.top();
1803
1804 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001805 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001806 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001807
1808 // [2] Set up new manager's top level manager
1809 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1810 TPM->addIndirectPassManager(FPP);
1811
1812 // [3] Assign manager to manage this new manager. This may create
1813 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001814 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001815
1816 // [4] Push new manager into PMS
1817 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001818 }
1819
Devang Patel3312f752007-01-16 21:43:18 +00001820 // Assign FPP as the manager of this pass.
1821 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001822}
1823
Devang Patel3312f752007-01-16 21:43:18 +00001824/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001825/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001826void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001827 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001828 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001829
Devang Patel15701b52007-01-11 00:19:00 +00001830 // Basic Pass Manager is a leaf pass manager. It does not handle
1831 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001832 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001833 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1834 BBP = (BBPassManager *)PMS.top();
1835 } else {
1836 // If leaf manager is not Basic Block Pass manager then create new
1837 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001838 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1839 PMDataManager *PMD = PMS.top();
1840
1841 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001842 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001843
1844 // [2] Set up new manager's top level manager
1845 // Basic Block Pass Manager does not live by itself
1846 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1847 TPM->addIndirectPassManager(BBP);
1848
Devang Patel15701b52007-01-11 00:19:00 +00001849 // [3] Assign manager to manage this new manager. This may create
1850 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001851 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001852
Devang Patel3312f752007-01-16 21:43:18 +00001853 // [4] Push new manager into PMS
1854 PMS.push(BBP);
1855 }
Devang Patel1c56a632007-01-08 19:29:38 +00001856
Devang Patel3312f752007-01-16 21:43:18 +00001857 // Assign BBP as the manager of this pass.
1858 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001859}
1860
Dan Gohmand3a20c92008-03-11 16:41:42 +00001861PassManagerBase::~PassManagerBase() {}