blob: d4638c57827f60233844e09836c660eec0ed5dd3 [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) {
Chris Lattner60987362009-03-06 05:53:14 +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) {
Chris Lattner60987362009-03-06 05:53:14 +0000349 assert(N < PassVector.size() && "Pass number out of range!");
350 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);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000596 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000597 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000598 if (P->getPotentialPassManagerType () ==
599 AnalysisPass->getPotentialPassManagerType())
600 // Schedule analysis pass that is managed by the same pass manager.
601 schedulePass(AnalysisPass);
602 else if (P->getPotentialPassManagerType () >
603 AnalysisPass->getPotentialPassManagerType()) {
604 // Schedule analysis pass that is managed by a new manager.
605 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000606 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000607 // are already checked are still available.
608 checkAnalysis = true;
609 }
610 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000611 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000612 // passes are run on the fly.
613 delete AnalysisPass;
614 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000615 }
616 }
617
618 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000619 if (ImmutablePass *IP = P->getAsImmutablePass()) {
620 // P is a immutable pass and it will be managed by this
621 // top level manager. Set up analysis resolver to connect them.
622 PMDataManager *DM = getAsPMDataManager();
623 AnalysisResolver *AR = new AnalysisResolver(*DM);
624 P->setResolver(AR);
625 DM->initializeAnalysisImpl(P);
626 addImmutablePass(IP);
627 DM->recordAvailableAnalysis(IP);
628 return;
629 }
630
631 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
632 Pass *PP = P->createPrinterPass(
633 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
634 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
635 }
636
637 // Add the requested pass to the best available pass manager.
638 P->assignPassManager(activeStack, getTopLevelPassManagerType());
639
640 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
641 Pass *PP = P->createPrinterPass(
642 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
643 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
644 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000645}
646
647/// Find the pass that implements Analysis AID. Search immutable
648/// passes and all pass managers. If desired pass is not found
649/// then return NULL.
650Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
651
Devang Patelcd6ba152006-12-12 22:50:05 +0000652 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000653 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000654 E = PassManagers.end(); I != E; ++I)
655 if (Pass *P = (*I)->findAnalysisPass(AID, false))
656 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000657
658 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000659 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000660 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000661 E = IndirectPassManagers.end(); I != E; ++I)
662 if (Pass *P = (*I)->findAnalysisPass(AID, false))
663 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000664
Dan Gohman844dd0a2010-10-11 23:19:01 +0000665 // Check the immutable passes. Iterate in reverse order so that we find
666 // the most recently registered passes first.
667 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
668 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000669 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000670 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000671 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000672
673 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000674 const PassInfo *PassInf =
675 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000676 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000677 const std::vector<const PassInfo*> &ImmPI =
678 PassInf->getInterfacesImplemented();
679 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
680 EE = ImmPI.end(); II != EE; ++II) {
681 if ((*II)->getTypeInfo() == AID)
682 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000683 }
684 }
685
Dan Gohman844dd0a2010-10-11 23:19:01 +0000686 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000687}
688
Devang Pateleda56172006-12-12 23:34:33 +0000689// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000690void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000691
Devang Patelfd4184322007-01-17 20:33:36 +0000692 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000693 return;
694
Devang Pateleda56172006-12-12 23:34:33 +0000695 // Print out the immutable passes
696 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000697 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000698 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000699
Dan Gohmanf71c5212010-08-19 01:29:07 +0000700 // Every class that derives from PMDataManager also derives from Pass
701 // (sometimes indirectly), but there's no inheritance relationship
702 // between PMDataManager and Pass, so we have to getAsPass to get
703 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000704 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000705 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000706 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000707}
708
Devang Patel991aeba2006-12-15 20:13:01 +0000709void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000710
Devang Patelfd4184322007-01-17 20:33:36 +0000711 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000712 return;
713
David Greene994e1bb2010-01-05 01:30:02 +0000714 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000715 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
716 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
717 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000718 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
719 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000720 if (!PI->isAnalysisGroup())
721 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000722 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000723 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000724 E = PassManagers.end(); I != E; ++I)
725 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000726 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000727}
728
Devang Patele3068402006-12-21 00:16:50 +0000729void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000730 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000731 E = PassManagers.end(); I != E; ++I)
732 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000733
Devang Patele3068402006-12-21 00:16:50 +0000734 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000735 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000736 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
737 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000738 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000739
Chris Lattner60987362009-03-06 05:53:14 +0000740 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000741 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000742 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000743 InversedLastUser.find(DMI->second);
744 if (InvDMI != InversedLastUser.end()) {
745 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
746 L.insert(DMI->first);
747 } else {
748 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
749 InversedLastUser[DMI->second] = L;
750 }
751 }
Devang Patele3068402006-12-21 00:16:50 +0000752}
753
Devang Patele7599552007-01-12 18:52:44 +0000754/// Destructor
755PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000756 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000757 E = PassManagers.end(); I != E; ++I)
758 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000759
Dan Gohman060d5ba2010-10-12 00:15:27 +0000760 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000761 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
762 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000763
764 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000765 DME = AnUsageMap.end(); DMI != DME; ++DMI)
766 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000767}
768
Devang Patelafb1f3622006-12-12 22:35:25 +0000769//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000770// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000771
Devang Patel643676c2006-11-11 01:10:19 +0000772/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000773void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000774 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000775
Chris Lattner60987362009-03-06 05:53:14 +0000776 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000777
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000778 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000779
Dan Gohmande6188a2010-08-12 23:50:08 +0000780 // This pass is the current implementation of all of the interfaces it
781 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000782 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
783 if (PInf == 0) return;
784 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000785 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000786 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000787}
788
Devang Patel9d9fc902007-03-06 17:52:53 +0000789// Return true if P preserves high level analysis used by other
790// passes managed by this manager
791bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000792 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000793 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000794 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000795
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000796 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000797 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000798 E = HigherLevelAnalysis.end(); I != E; ++I) {
799 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000800 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000801 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000802 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000803 PreservedSet.end())
804 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000805 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000806
Devang Patel9d9fc902007-03-06 17:52:53 +0000807 return true;
808}
809
Chris Lattner02eb94c2008-08-07 07:34:50 +0000810/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000811void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000812 // Don't do this unless assertions are enabled.
813#ifdef NDEBUG
814 return;
815#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000816 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
817 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000818
Devang Patelef432532007-07-19 05:36:09 +0000819 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000820 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000821 E = PreservedSet.end(); I != E; ++I) {
822 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000823 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000824 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000825 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000826 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000827 }
828}
829
Devang Patel67c79a42008-07-01 19:50:56 +0000830/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000831void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000832 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
833 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000834 return;
835
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000836 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000837 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000838 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000839 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000840 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000841 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000842 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000843 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000844 if (PassDebugging >= Details) {
845 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000846 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
847 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000848 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000849 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000850 }
Devang Patel349170f2006-11-11 01:24:55 +0000851 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000852
Devang Patel42dd1e92007-03-06 01:55:46 +0000853 // Check inherited analysis also. If P is not preserving analysis
854 // provided by parent manager then remove it here.
855 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
856
857 if (!InheritedAnalysis[Index])
858 continue;
859
Dan Gohmande6188a2010-08-12 23:50:08 +0000860 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000861 I = InheritedAnalysis[Index]->begin(),
862 E = InheritedAnalysis[Index]->end(); I != E; ) {
863 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000864 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000865 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000866 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000867 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000868 if (PassDebugging >= Details) {
869 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000870 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
871 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000872 }
Devang Patel01919d22007-03-08 19:05:01 +0000873 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000874 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000875 }
876 }
Devang Patelf68a3492006-11-07 22:35:17 +0000877}
878
Devang Patelca189262006-11-14 03:05:08 +0000879/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000880void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000881 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000882
Devang Patel8adae862007-07-20 18:04:54 +0000883 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000884
Devang Patel2ff44922007-04-16 20:39:59 +0000885 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000886 if (!TPM)
887 return;
888
Devang Patel17ad0962006-12-08 00:37:52 +0000889 TPM->collectLastUses(DeadPasses, P);
890
Devang Patel656a9172008-06-06 17:50:36 +0000891 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000892 dbgs() << " -*- '" << P->getPassName();
893 dbgs() << "' is the last user of following pass instances.";
894 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000895 }
896
Dan Gohman060d5ba2010-10-12 00:15:27 +0000897 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000898 E = DeadPasses.end(); I != E; ++I)
899 freePass(*I, Msg, DBG_STR);
900}
Devang Patel200d3052006-12-13 23:50:44 +0000901
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000902void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000903 enum PassDebuggingString DBG_STR) {
904 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000905
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000906 {
907 // If the pass crashes releasing memory, remember this.
908 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000909 TimeRegion PassTimer(getPassTimer(P));
910
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000911 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000912 }
913
Owen Andersona7aed182010-08-06 18:33:48 +0000914 AnalysisID PI = P->getPassID();
915 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000916 // Remove the pass itself (if it is not already removed).
917 AvailableAnalysis.erase(PI);
918
919 // Remove all interfaces this pass implements, for which it is also
920 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000921 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000922 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000923 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000924 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000925 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000926 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000927 }
Devang Patel17ad0962006-12-08 00:37:52 +0000928 }
Devang Patelca189262006-11-14 03:05:08 +0000929}
930
Dan Gohmande6188a2010-08-12 23:50:08 +0000931/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000932/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000933void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000934 // This manager is going to manage pass P. Set up analysis resolver
935 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000936 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000937 P->setResolver(AR);
938
Devang Patelec2b9a72007-03-05 22:57:49 +0000939 // If a FunctionPass F is the last user of ModulePass info M
940 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000941 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000942
Chris Lattner60987362009-03-06 05:53:14 +0000943 if (!ProcessAnalysis) {
944 // Add pass
945 PassVector.push_back(P);
946 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000947 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000948
Chris Lattner60987362009-03-06 05:53:14 +0000949 // At the moment, this pass is the last user of all required passes.
950 SmallVector<Pass *, 12> LastUses;
951 SmallVector<Pass *, 8> RequiredPasses;
952 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
953
954 unsigned PDepth = this->getDepth();
955
Dan Gohmande6188a2010-08-12 23:50:08 +0000956 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000957 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000958 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000959 E = RequiredPasses.end(); I != E; ++I) {
960 Pass *PRequired = *I;
961 unsigned RDepth = 0;
962
963 assert(PRequired->getResolver() && "Analysis Resolver is not set");
964 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
965 RDepth = DM.getDepth();
966
967 if (PDepth == RDepth)
968 LastUses.push_back(PRequired);
969 else if (PDepth > RDepth) {
970 // Let the parent claim responsibility of last use
971 TransferLastUses.push_back(PRequired);
972 // Keep track of higher level analysis used by this manager.
973 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000974 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000975 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000976 }
977
978 // Set P as P's last user until someone starts using P.
979 // However, if P is a Pass Manager then it does not need
980 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000981 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000982 LastUses.push_back(P);
983 TPM->setLastUser(LastUses, P);
984
985 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000986 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000987 TPM->setLastUser(TransferLastUses, My_PM);
988 TransferLastUses.clear();
989 }
990
Dan Gohman6304db32010-08-16 22:57:28 +0000991 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +0000992 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000993 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000994 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000995 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
996 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +0000997 this->addLowerLevelRequiredPass(P, AnalysisPass);
998 }
999
1000 // Take a note of analysis required and made available by this pass.
1001 // Remove the analysis not preserved by this pass
1002 removeNotPreservedAnalysis(P);
1003 recordAvailableAnalysis(P);
1004
Devang Patel8cad70d2006-11-11 01:51:02 +00001005 // Add pass
1006 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001007}
1008
Devang Patele64d3052007-04-16 20:12:57 +00001009
1010/// Populate RP with analysis pass that are required by
1011/// pass P and are available. Populate RP_NotAvail with analysis
1012/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001013void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1014 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001015 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001016 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1017 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001018 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001019 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001020 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001021 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001022 else
Chris Lattner60987362009-03-06 05:53:14 +00001023 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001024 }
Devang Patelf58183d2006-12-12 23:09:32 +00001025
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001026 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001027 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001028 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001029 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001030 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001031 else
Chris Lattner60987362009-03-06 05:53:14 +00001032 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001033 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001034}
1035
Devang Patel07f4f582006-11-14 21:49:36 +00001036// All Required analyses should be available to the pass as it runs! Here
1037// we fill in the AnalysisImpls member of the pass so that it can
1038// successfully use the getAnalysis() method to retrieve the
1039// implementations it needs.
1040//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001041void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001042 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1043
Chris Lattnercbd160f2008-08-08 05:33:04 +00001044 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001045 I = AnUsage->getRequiredSet().begin(),
1046 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001047 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001048 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001049 // This may be analysis pass that is initialized on the fly.
1050 // If that is not the case then it will raise an assert when it is used.
1051 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001052 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001053 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001054 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001055 }
1056}
1057
Devang Patel640c5bb2006-12-08 22:30:11 +00001058/// Find the pass that implements Analysis AID. If desired pass is not found
1059/// then return NULL.
1060Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1061
1062 // Check if AvailableAnalysis map has one entry.
1063 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1064
1065 if (I != AvailableAnalysis.end())
1066 return I->second;
1067
1068 // Search Parents through TopLevelManager
1069 if (SearchParent)
1070 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001071
Devang Patel9d759b82006-12-09 00:09:12 +00001072 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001073}
1074
Devang Patel991aeba2006-12-15 20:13:01 +00001075// Print list of passes that are last used by P.
1076void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1077
Devang Patel8adae862007-07-20 18:04:54 +00001078 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001079
1080 // If this is a on the fly manager then it does not have TPM.
1081 if (!TPM)
1082 return;
1083
Devang Patel991aeba2006-12-15 20:13:01 +00001084 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001085
Dan Gohman7224bce2010-10-12 00:11:18 +00001086 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001087 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001088 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001089 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001090 }
1091}
1092
1093void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001094 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001095 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001096 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001097 PMD->dumpPassArguments();
1098 else
Owen Andersona7aed182010-08-06 18:33:48 +00001099 if (const PassInfo *PI =
1100 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001101 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001102 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001103 }
1104}
1105
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001106void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1107 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001108 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001109 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001110 return;
David Greene994e1bb2010-01-05 01:30:02 +00001111 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001112 switch (S1) {
1113 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001114 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001115 break;
1116 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001117 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001118 break;
1119 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001120 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001121 break;
1122 default:
1123 break;
1124 }
1125 switch (S2) {
1126 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001127 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001128 break;
1129 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001130 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001131 break;
1132 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001134 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001135 case ON_REGION_MSG:
1136 dbgs() << "' on Region '" << Msg << "'...\n";
1137 break;
Devang Patel003a5592007-03-05 20:01:30 +00001138 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001139 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001140 break;
1141 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001142 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001143 break;
1144 default:
1145 break;
1146 }
Devang Patel991aeba2006-12-15 20:13:01 +00001147}
1148
Chris Lattner4c1e9542009-03-06 06:45:05 +00001149void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001150 if (PassDebugging < Details)
1151 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001152
Chris Lattner4c493d92008-08-08 15:14:09 +00001153 AnalysisUsage analysisUsage;
1154 P->getAnalysisUsage(analysisUsage);
1155 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1156}
1157
Chris Lattner4c1e9542009-03-06 06:45:05 +00001158void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001159 if (PassDebugging < Details)
1160 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001161
Chris Lattner4c493d92008-08-08 15:14:09 +00001162 AnalysisUsage analysisUsage;
1163 P->getAnalysisUsage(analysisUsage);
1164 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1165}
1166
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001167void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001168 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001169 assert(PassDebugging >= Details);
1170 if (Set.empty())
1171 return;
David Greene994e1bb2010-01-05 01:30:02 +00001172 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001173 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001174 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001175 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001176 if (!PInf) {
1177 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1178 // all drivers.
1179 dbgs() << " Uninitialized Pass";
1180 continue;
1181 }
Owen Andersona7aed182010-08-06 18:33:48 +00001182 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001183 }
David Greene994e1bb2010-01-05 01:30:02 +00001184 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001185}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001186
Devang Patel004937b2007-07-27 20:06:09 +00001187/// Add RequiredPass into list of lower level passes required by pass P.
1188/// RequiredPass is run on the fly by Pass Manager when P requests it
1189/// through getAnalysis interface.
1190/// This should be handled by specific pass manager.
1191void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1192 if (TPM) {
1193 TPM->dumpArguments();
1194 TPM->dumpPasses();
1195 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001196
Dan Gohmande6188a2010-08-12 23:50:08 +00001197 // Module Level pass may required Function Level analysis info
1198 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1199 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001200 // module level pass is requiring lower level analysis info managed by
1201 // lower level pass manager.
1202
1203 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001204 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001205 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001206#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001207 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1208 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001209#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001210 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001211}
1212
Owen Andersona7aed182010-08-06 18:33:48 +00001213Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001214 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001215}
1216
Devang Patele7599552007-01-12 18:52:44 +00001217// Destructor
1218PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001219 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001220 E = PassVector.end(); I != E; ++I)
1221 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001222}
1223
Devang Patel9bdf7d42006-12-08 23:28:54 +00001224//===----------------------------------------------------------------------===//
1225// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001226// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1227Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001228 return PM.findAnalysisPass(ID, dir);
1229}
1230
Dan Gohmande6188a2010-08-12 23:50:08 +00001231Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001232 Function &F) {
1233 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1234}
1235
Devang Patela1514cb2006-12-07 19:39:39 +00001236//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001237// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001238
Dan Gohmande6188a2010-08-12 23:50:08 +00001239/// Execute all of the passes scheduled for execution by invoking
1240/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001241/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001242bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001243 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001244 return false;
1245
Devang Patele9585592006-12-08 01:38:28 +00001246 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001247
Devang Patel6e5a1132006-11-07 21:31:57 +00001248 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001249 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1250 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001251 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001252
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001253 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001254 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001255
Devang Patelabfbe3b2006-12-16 00:56:26 +00001256 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001257
Chris Lattner4c1e9542009-03-06 06:45:05 +00001258 {
1259 // If the pass crashes, remember this.
1260 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001261 TimeRegion PassTimer(getPassTimer(BP));
1262
Dan Gohman74b189f2010-03-01 17:34:28 +00001263 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001264 }
Devang Patel93a197c2006-12-14 00:08:04 +00001265
Dan Gohman74b189f2010-03-01 17:34:28 +00001266 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001267 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001268 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001269 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001270 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001271
Devang Patela273d1c2007-07-19 18:02:32 +00001272 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001273 removeNotPreservedAnalysis(BP);
1274 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001275 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001276 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001277
Bill Wendling6ce6d262009-12-25 13:50:18 +00001278 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001279}
1280
Devang Patel475c4532006-12-08 00:59:05 +00001281// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001282bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001283 bool Changed = false;
1284
Chris Lattner4c1e9542009-03-06 06:45:05 +00001285 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1286 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001287
1288 return Changed;
1289}
1290
Duncan Sands51495602009-02-13 09:42:34 +00001291bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001292 bool Changed = false;
1293
Chris Lattner4c1e9542009-03-06 06:45:05 +00001294 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1295 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001296
1297 return Changed;
1298}
1299
Duncan Sands51495602009-02-13 09:42:34 +00001300bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001301 bool Changed = false;
1302
Devang Patelabfbe3b2006-12-16 00:56:26 +00001303 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1304 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001305 Changed |= BP->doInitialization(F);
1306 }
1307
1308 return Changed;
1309}
1310
Duncan Sands51495602009-02-13 09:42:34 +00001311bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001312 bool Changed = false;
1313
Devang Patelabfbe3b2006-12-16 00:56:26 +00001314 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1315 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001316 Changed |= BP->doFinalization(F);
1317 }
1318
1319 return Changed;
1320}
1321
1322
Devang Patela1514cb2006-12-07 19:39:39 +00001323//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001324// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001325
Devang Patel4e12f862006-11-08 10:44:40 +00001326/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001327FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001328 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001329 // FPM is the top level manager.
1330 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001331
Dan Gohman565df952008-03-13 02:08:36 +00001332 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001333 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001334}
1335
Devang Patelb67904d2006-12-13 02:36:01 +00001336FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001337 delete FPM;
1338}
1339
Devang Patel4e12f862006-11-08 10:44:40 +00001340/// add - Add a pass to the queue of passes to run. This passes
1341/// ownership of the Pass to the PassManager. When the
1342/// PassManager_X is destroyed, the pass will be destroyed as well, so
1343/// there is no need to delete the pass. (TODO delete passes.)
1344/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001345void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001346 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001347}
1348
Devang Patel9f3083e2006-11-15 19:39:54 +00001349/// run - Execute all of the passes scheduled for execution. Keep
1350/// track of whether any of the passes modifies the function, and if
1351/// so, return true.
1352///
Devang Patelb67904d2006-12-13 02:36:01 +00001353bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001354 if (F.isMaterializable()) {
1355 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001356 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001357 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001358 }
Devang Patel272908d2006-12-08 22:57:48 +00001359 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001360}
1361
1362
Devang Patelff631ae2006-11-15 01:27:05 +00001363/// doInitialization - Run all of the initializers for the function passes.
1364///
Devang Patelb67904d2006-12-13 02:36:01 +00001365bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001366 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001367}
1368
Dan Gohmane6656eb2007-07-30 14:51:13 +00001369/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001370///
Devang Patelb67904d2006-12-13 02:36:01 +00001371bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001372 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001373}
1374
Devang Patela1514cb2006-12-07 19:39:39 +00001375//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001376// FunctionPassManagerImpl implementation
1377//
Duncan Sands51495602009-02-13 09:42:34 +00001378bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001379 bool Changed = false;
1380
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001381 dumpArguments();
1382 dumpPasses();
1383
Chris Lattner4c1e9542009-03-06 06:45:05 +00001384 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1385 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001386
1387 return Changed;
1388}
1389
Duncan Sands51495602009-02-13 09:42:34 +00001390bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001391 bool Changed = false;
1392
Chris Lattner4c1e9542009-03-06 06:45:05 +00001393 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1394 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001395
1396 return Changed;
1397}
1398
Devang Patelec9c58f2009-04-01 22:34:41 +00001399/// cleanup - After running all passes, clean up pass manager cache.
1400void FPPassManager::cleanup() {
1401 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1402 FunctionPass *FP = getContainedPass(Index);
1403 AnalysisResolver *AR = FP->getResolver();
1404 assert(AR && "Analysis Resolver is not set");
1405 AR->clearAnalysisImpls();
1406 }
1407}
1408
Torok Edwin24c78352009-06-29 18:49:09 +00001409void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1410 if (!wasRun)
1411 return;
1412 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1413 FPPassManager *FPPM = getContainedManager(Index);
1414 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1415 FPPM->getContainedPass(Index)->releaseMemory();
1416 }
1417 }
Torok Edwin896556e2009-06-29 21:05:10 +00001418 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001419}
1420
Devang Patel67d6a5e2006-12-19 19:46:59 +00001421// Execute all the passes managed by this top level manager.
1422// Return true if any function is modified by a pass.
1423bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425 TimingInfo::createTheTimeInfo();
1426
Devang Patele3068402006-12-21 00:16:50 +00001427 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001428 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1429 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001430
1431 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1432 getContainedManager(Index)->cleanup();
1433
Torok Edwin24c78352009-06-29 18:49:09 +00001434 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001435 return Changed;
1436}
1437
1438//===----------------------------------------------------------------------===//
1439// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001440
Devang Patel8c78a0b2007-05-03 01:11:54 +00001441char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001442/// Print passes managed by this manager
1443void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001444 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001445 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1446 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001447 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001448 dumpLastUses(FP, Offset+1);
1449 }
1450}
1451
1452
Dan Gohmande6188a2010-08-12 23:50:08 +00001453/// Execute all of the passes scheduled for execution by invoking
1454/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001455/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001456bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001457 if (F.isDeclaration())
1458 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001459
1460 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001461
Devang Patelcbbf2912008-03-20 01:09:53 +00001462 // Collect inherited analysis from Module level pass manager.
1463 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001464
Devang Patelabfbe3b2006-12-16 00:56:26 +00001465 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1466 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001467 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001468
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001469 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001470 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001471
Devang Patelabfbe3b2006-12-16 00:56:26 +00001472 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001473
Chris Lattner4c1e9542009-03-06 06:45:05 +00001474 {
1475 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001476 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001477
Dan Gohman74b189f2010-03-01 17:34:28 +00001478 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001479 }
Devang Patel93a197c2006-12-14 00:08:04 +00001480
Dan Gohman74b189f2010-03-01 17:34:28 +00001481 Changed |= LocalChanged;
1482 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001483 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001484 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001485
Devang Patela273d1c2007-07-19 18:02:32 +00001486 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001487 removeNotPreservedAnalysis(FP);
1488 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001489 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001490 }
1491 return Changed;
1492}
1493
Devang Patel67d6a5e2006-12-19 19:46:59 +00001494bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001495 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001496
Dan Gohmane7630be2010-05-11 20:30:00 +00001497 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001498 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001499
Bill Wendling6ce6d262009-12-25 13:50:18 +00001500 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001501}
1502
Duncan Sands51495602009-02-13 09:42:34 +00001503bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001504 bool Changed = false;
1505
Chris Lattner4c1e9542009-03-06 06:45:05 +00001506 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1507 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001508
1509 return Changed;
1510}
1511
Duncan Sands51495602009-02-13 09:42:34 +00001512bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001513 bool Changed = false;
1514
Chris Lattner4c1e9542009-03-06 06:45:05 +00001515 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1516 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001517
Devang Patelff631ae2006-11-15 01:27:05 +00001518 return Changed;
1519}
1520
Devang Patela1514cb2006-12-07 19:39:39 +00001521//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001522// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001523
Dan Gohmande6188a2010-08-12 23:50:08 +00001524/// Execute all of the passes scheduled for execution by invoking
1525/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001526/// the module, and if so, return true.
1527bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001529 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001530
Torok Edwin24c78352009-06-29 18:49:09 +00001531 // Initialize on-the-fly passes
1532 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1533 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1534 I != E; ++I) {
1535 FunctionPassManagerImpl *FPP = I->second;
1536 Changed |= FPP->doInitialization(M);
1537 }
1538
Devang Patelabfbe3b2006-12-16 00:56:26 +00001539 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1540 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001541 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001542
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001543 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001544 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001545
Devang Patelabfbe3b2006-12-16 00:56:26 +00001546 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001547
Chris Lattner4c1e9542009-03-06 06:45:05 +00001548 {
1549 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001550 TimeRegion PassTimer(getPassTimer(MP));
1551
Dan Gohman74b189f2010-03-01 17:34:28 +00001552 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001553 }
Devang Patel93a197c2006-12-14 00:08:04 +00001554
Dan Gohman74b189f2010-03-01 17:34:28 +00001555 Changed |= LocalChanged;
1556 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001557 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001558 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001559 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001560
Devang Patela273d1c2007-07-19 18:02:32 +00001561 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001562 removeNotPreservedAnalysis(MP);
1563 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001564 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001565 }
Torok Edwin24c78352009-06-29 18:49:09 +00001566
1567 // Finalize on-the-fly passes
1568 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1569 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1570 I != E; ++I) {
1571 FunctionPassManagerImpl *FPP = I->second;
1572 // We don't know when is the last time an on-the-fly pass is run,
1573 // so we need to releaseMemory / finalize here
1574 FPP->releaseMemoryOnTheFly();
1575 Changed |= FPP->doFinalization(M);
1576 }
Devang Patel05e1a972006-11-07 22:03:15 +00001577 return Changed;
1578}
1579
Devang Patele64d3052007-04-16 20:12:57 +00001580/// Add RequiredPass into list of lower level passes required by pass P.
1581/// RequiredPass is run on the fly by Pass Manager when P requests it
1582/// through getAnalysis interface.
1583void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001584 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1585 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001586 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001587 RequiredPass->getPotentialPassManagerType()) &&
1588 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001589
Devang Patel68f72b12007-04-26 17:50:19 +00001590 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001591 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001592 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001593 // FPP is the top level manager.
1594 FPP->setTopLevelManager(FPP);
1595
Devang Patel69e9f6d2007-04-16 20:27:05 +00001596 OnTheFlyManagers[P] = FPP;
1597 }
Devang Patel68f72b12007-04-26 17:50:19 +00001598 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001599
Devang Patel68f72b12007-04-26 17:50:19 +00001600 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001601 if (RequiredPass) {
1602 SmallVector<Pass *, 1> LU;
1603 LU.push_back(RequiredPass);
1604 FPP->setLastUser(LU, P);
1605 }
Devang Patele64d3052007-04-16 20:12:57 +00001606}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001607
Dan Gohmande6188a2010-08-12 23:50:08 +00001608/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001609/// required by module pass MP. Instantiate analysis pass, by using
1610/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001611Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001612 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001613 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001614
Torok Edwin24c78352009-06-29 18:49:09 +00001615 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001616 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001617 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001618}
1619
1620
Devang Patela1514cb2006-12-07 19:39:39 +00001621//===----------------------------------------------------------------------===//
1622// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001623//
Devang Patelc290c8a2006-11-07 22:23:34 +00001624/// run - Execute all of the passes scheduled for execution. Keep track of
1625/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001626bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001627 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001628 TimingInfo::createTheTimeInfo();
1629
Devang Patelcfd70c42006-12-13 22:10:00 +00001630 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001631 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001632
Devang Patele3068402006-12-21 00:16:50 +00001633 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001634 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1635 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001636 return Changed;
1637}
Devang Patel376fefa2006-11-08 10:29:57 +00001638
Devang Patela1514cb2006-12-07 19:39:39 +00001639//===----------------------------------------------------------------------===//
1640// PassManager implementation
1641
Devang Patel376fefa2006-11-08 10:29:57 +00001642/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001643PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001644 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001645 // PM is the top level manager
1646 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001647}
1648
Devang Patelb67904d2006-12-13 02:36:01 +00001649PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001650 delete PM;
1651}
1652
Devang Patel376fefa2006-11-08 10:29:57 +00001653/// add - Add a pass to the queue of passes to run. This passes ownership of
1654/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1655/// will be destroyed as well, so there is no need to delete the pass. This
1656/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001657void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001658 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001659}
1660
1661/// run - Execute all of the passes scheduled for execution. Keep track of
1662/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001663bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001664 return PM->run(M);
1665}
1666
Devang Patelb8817b92006-12-14 00:59:42 +00001667//===----------------------------------------------------------------------===//
1668// TimingInfo Class - This class is used to calculate information about the
1669// amount of time each pass takes to execute. This only happens with
1670// -time-passes is enabled on the command line.
1671//
1672bool llvm::TimePassesIsEnabled = false;
1673static cl::opt<bool,true>
1674EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1675 cl::desc("Time each pass, printing elapsed time for each on exit"));
1676
1677// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1678// a non null value (if the -time-passes option is enabled) or it leaves it
1679// null. It may be called multiple times.
1680void TimingInfo::createTheTimeInfo() {
1681 if (!TimePassesIsEnabled || TheTimeInfo) return;
1682
1683 // Constructed the first time this is called, iff -time-passes is enabled.
1684 // This guarantees that the object will be constructed before static globals,
1685 // thus it will be destroyed before them.
1686 static ManagedStatic<TimingInfo> TTI;
1687 TheTimeInfo = &*TTI;
1688}
1689
Devang Patel1c3633e2007-01-29 23:10:37 +00001690/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001691Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001692 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001693 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001694 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001695}
1696
Devang Patel1c56a632007-01-08 19:29:38 +00001697//===----------------------------------------------------------------------===//
1698// PMStack implementation
1699//
Devang Patelad98d232007-01-11 22:15:30 +00001700
Devang Patel1c56a632007-01-08 19:29:38 +00001701// Pop Pass Manager from the stack and clear its analysis info.
1702void PMStack::pop() {
1703
1704 PMDataManager *Top = this->top();
1705 Top->initializeAnalysisInfo();
1706
1707 S.pop_back();
1708}
1709
1710// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001711void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001712 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001713 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001714
Chris Lattner60987362009-03-06 05:53:14 +00001715 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001716 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1717 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001718 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001719
Chris Lattner60987362009-03-06 05:53:14 +00001720 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001721 TPM->addIndirectPassManager(PM);
1722 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001723 PM->setDepth(this->top()->getDepth()+1);
1724 }
1725 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001726 assert((PM->getPassManagerType() == PMT_ModulePassManager
1727 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001728 && "pushing bad pass manager to PMStack");
1729 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001730 }
1731
Devang Patel15701b52007-01-11 00:19:00 +00001732 S.push_back(PM);
1733}
1734
1735// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001736void PMStack::dump() const {
1737 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001738 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001739 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001740
Devang Patel15701b52007-01-11 00:19:00 +00001741 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001742 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001743}
1744
Devang Patel1c56a632007-01-08 19:29:38 +00001745/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001746/// add self into that manager.
1747void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001748 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001749 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001750 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001751 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1752 if (TopPMType == PreferredType)
1753 break; // We found desired pass manager
1754 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001755 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001756 else
1757 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001758 }
Devang Patel18ff6362008-09-09 21:38:40 +00001759 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001760 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001761}
1762
Devang Patel3312f752007-01-16 21:43:18 +00001763/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001764/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001765void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001766 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001767
Andrew Trickcbc845f2012-02-01 07:16:20 +00001768 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001769 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001770 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1771 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001772 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001773 break;
Devang Patel3312f752007-01-16 21:43:18 +00001774 }
Devang Patel3312f752007-01-16 21:43:18 +00001775
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001776 // Create new Function Pass Manager if needed.
1777 FPPassManager *FPP;
1778 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1779 FPP = (FPPassManager *)PMS.top();
1780 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001781 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1782 PMDataManager *PMD = PMS.top();
1783
1784 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001785 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001786 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001787
1788 // [2] Set up new manager's top level manager
1789 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1790 TPM->addIndirectPassManager(FPP);
1791
1792 // [3] Assign manager to manage this new manager. This may create
1793 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001794 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001795
1796 // [4] Push new manager into PMS
1797 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001798 }
1799
Devang Patel3312f752007-01-16 21:43:18 +00001800 // Assign FPP as the manager of this pass.
1801 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001802}
1803
Devang Patel3312f752007-01-16 21:43:18 +00001804/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001805/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001806void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001807 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001808 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001809
Devang Patel15701b52007-01-11 00:19:00 +00001810 // Basic Pass Manager is a leaf pass manager. It does not handle
1811 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001812 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001813 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1814 BBP = (BBPassManager *)PMS.top();
1815 } else {
1816 // If leaf manager is not Basic Block Pass manager then create new
1817 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001818 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1819 PMDataManager *PMD = PMS.top();
1820
1821 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001822 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001823
1824 // [2] Set up new manager's top level manager
1825 // Basic Block Pass Manager does not live by itself
1826 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1827 TPM->addIndirectPassManager(BBP);
1828
Devang Patel15701b52007-01-11 00:19:00 +00001829 // [3] Assign manager to manage this new manager. This may create
1830 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001831 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001832
Devang Patel3312f752007-01-16 21:43:18 +00001833 // [4] Push new manager into PMS
1834 PMS.push(BBP);
1835 }
Devang Patel1c56a632007-01-08 19:29:38 +00001836
Devang Patel3312f752007-01-16 21:43:18 +00001837 // Assign BBP as the manager of this pass.
1838 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001839}
1840
Dan Gohmand3a20c92008-03-11 16:41:42 +00001841PassManagerBase::~PassManagerBase() {}