blob: 28fbaa6678411614b976ca7c85b2c8f8df7c729a [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
481PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
482 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000483 unsigned PDepth = 0;
484 if (P->getResolver())
485 PDepth = P->getResolver()->getPMDataManager().getDepth();
486
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000487 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000488 E = AnalysisPasses.end(); I != E; ++I) {
489 Pass *AP = *I;
490 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000491
Devang Patel01919d22007-03-08 19:05:01 +0000492 if (P == AP)
493 continue;
494
Tobias Grosserf07426b2011-01-20 21:03:22 +0000495 // Update the last users of passes that are required transitive by AP.
496 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
497 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
498 SmallVector<Pass *, 12> LastUses;
499 SmallVector<Pass *, 12> LastPMUses;
500 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
501 E = IDs.end(); I != E; ++I) {
502 Pass *AnalysisPass = findAnalysisPass(*I);
503 assert(AnalysisPass && "Expected analysis pass to exist.");
504 AnalysisResolver *AR = AnalysisPass->getResolver();
505 assert(AR && "Expected analysis resolver to exist.");
506 unsigned APDepth = AR->getPMDataManager().getDepth();
507
508 if (PDepth == APDepth)
509 LastUses.push_back(AnalysisPass);
510 else if (PDepth > APDepth)
511 LastPMUses.push_back(AnalysisPass);
512 }
513
514 setLastUser(LastUses, P);
515
516 // If this pass has a corresponding pass manager, push higher level
517 // analysis to this pass manager.
518 if (P->getResolver())
519 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
520
521
Devang Patelafb1f3622006-12-12 22:35:25 +0000522 // If AP is the last user of other passes then make P last user of
523 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000524 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000525 LUE = LastUser.end(); LUI != LUE; ++LUI) {
526 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000527 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000528 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000529 LastUser[LUI->first] = P;
530 }
531 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000532}
533
534/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000535void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000536 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000537 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000538 InversedLastUser.find(P);
539 if (DMI == InversedLastUser.end())
540 return;
541
542 SmallPtrSet<Pass *, 8> &LU = DMI->second;
543 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
544 E = LU.end(); I != E; ++I) {
545 LastUses.push_back(*I);
546 }
547
Devang Patelafb1f3622006-12-12 22:35:25 +0000548}
549
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000550AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
551 AnalysisUsage *AnUsage = NULL;
552 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000553 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000554 AnUsage = DMI->second;
555 else {
556 AnUsage = new AnalysisUsage();
557 P->getAnalysisUsage(*AnUsage);
558 AnUsageMap[P] = AnUsage;
559 }
560 return AnUsage;
561}
562
Devang Patelafb1f3622006-12-12 22:35:25 +0000563/// Schedule pass P for execution. Make sure that passes required by
564/// P are run before P is run. Update analysis info maintained by
565/// the manager. Remove dead passes. This is a recursive function.
566void PMTopLevelManager::schedulePass(Pass *P) {
567
Devang Patel3312f752007-01-16 21:43:18 +0000568 // TODO : Allocate function manager for this pass, other wise required set
569 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000570
Devang Pateld74ede72007-03-06 01:06:16 +0000571 // Give pass a chance to prepare the stage.
572 P->preparePassManager(activeStack);
573
Devang Patel864970e2008-03-18 00:39:19 +0000574 // If P is an analysis pass and it is available then do not
575 // generate the analysis again. Stale analysis info should not be
576 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000577 const PassInfo *PI =
578 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
579 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000580 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000581 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000582 }
Devang Patel864970e2008-03-18 00:39:19 +0000583
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000584 AnalysisUsage *AnUsage = findAnalysisUsage(P);
585
Devang Patelfdee7032008-08-14 23:07:48 +0000586 bool checkAnalysis = true;
587 while (checkAnalysis) {
588 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000589
Devang Patelfdee7032008-08-14 23:07:48 +0000590 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
591 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
592 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000593
Devang Patelfdee7032008-08-14 23:07:48 +0000594 Pass *AnalysisPass = findAnalysisPass(*I);
595 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000596 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000597 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000598 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000599 if (P->getPotentialPassManagerType () ==
600 AnalysisPass->getPotentialPassManagerType())
601 // Schedule analysis pass that is managed by the same pass manager.
602 schedulePass(AnalysisPass);
603 else if (P->getPotentialPassManagerType () >
604 AnalysisPass->getPotentialPassManagerType()) {
605 // Schedule analysis pass that is managed by a new manager.
606 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000607 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000608 // are already checked are still available.
609 checkAnalysis = true;
610 }
611 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000612 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000613 // passes are run on the fly.
614 delete AnalysisPass;
615 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000616 }
617 }
618
619 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000620 if (ImmutablePass *IP = P->getAsImmutablePass()) {
621 // P is a immutable pass and it will be managed by this
622 // top level manager. Set up analysis resolver to connect them.
623 PMDataManager *DM = getAsPMDataManager();
624 AnalysisResolver *AR = new AnalysisResolver(*DM);
625 P->setResolver(AR);
626 DM->initializeAnalysisImpl(P);
627 addImmutablePass(IP);
628 DM->recordAvailableAnalysis(IP);
629 return;
630 }
631
632 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
633 Pass *PP = P->createPrinterPass(
634 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
635 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
636 }
637
638 // Add the requested pass to the best available pass manager.
639 P->assignPassManager(activeStack, getTopLevelPassManagerType());
640
641 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
642 Pass *PP = P->createPrinterPass(
643 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
644 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
645 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000646}
647
648/// Find the pass that implements Analysis AID. Search immutable
649/// passes and all pass managers. If desired pass is not found
650/// then return NULL.
651Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
652
Devang Patelcd6ba152006-12-12 22:50:05 +0000653 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000654 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000655 E = PassManagers.end(); I != E; ++I)
656 if (Pass *P = (*I)->findAnalysisPass(AID, false))
657 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000658
659 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000660 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000661 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000662 E = IndirectPassManagers.end(); I != E; ++I)
663 if (Pass *P = (*I)->findAnalysisPass(AID, false))
664 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000665
Dan Gohman844dd0a2010-10-11 23:19:01 +0000666 // Check the immutable passes. Iterate in reverse order so that we find
667 // the most recently registered passes first.
668 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
669 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000670 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000671 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000672 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000673
674 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000675 const PassInfo *PassInf =
676 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000677 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000678 const std::vector<const PassInfo*> &ImmPI =
679 PassInf->getInterfacesImplemented();
680 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
681 EE = ImmPI.end(); II != EE; ++II) {
682 if ((*II)->getTypeInfo() == AID)
683 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000684 }
685 }
686
Dan Gohman844dd0a2010-10-11 23:19:01 +0000687 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000688}
689
Devang Pateleda56172006-12-12 23:34:33 +0000690// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000691void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000692
Devang Patelfd4184322007-01-17 20:33:36 +0000693 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000694 return;
695
Devang Pateleda56172006-12-12 23:34:33 +0000696 // Print out the immutable passes
697 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000698 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000699 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000700
Dan Gohmanf71c5212010-08-19 01:29:07 +0000701 // Every class that derives from PMDataManager also derives from Pass
702 // (sometimes indirectly), but there's no inheritance relationship
703 // between PMDataManager and Pass, so we have to getAsPass to get
704 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000705 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000706 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000707 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000708}
709
Devang Patel991aeba2006-12-15 20:13:01 +0000710void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000711
Devang Patelfd4184322007-01-17 20:33:36 +0000712 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000713 return;
714
David Greene994e1bb2010-01-05 01:30:02 +0000715 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000716 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
717 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
718 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000719 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
720 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000721 if (!PI->isAnalysisGroup())
722 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000723 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000724 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000725 E = PassManagers.end(); I != E; ++I)
726 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000727 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000728}
729
Devang Patele3068402006-12-21 00:16:50 +0000730void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000731 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000732 E = PassManagers.end(); I != E; ++I)
733 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000734
Devang Patele3068402006-12-21 00:16:50 +0000735 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000736 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000737 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
738 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000739 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000740
Chris Lattner60987362009-03-06 05:53:14 +0000741 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000742 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000743 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000744 InversedLastUser.find(DMI->second);
745 if (InvDMI != InversedLastUser.end()) {
746 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
747 L.insert(DMI->first);
748 } else {
749 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
750 InversedLastUser[DMI->second] = L;
751 }
752 }
Devang Patele3068402006-12-21 00:16:50 +0000753}
754
Devang Patele7599552007-01-12 18:52:44 +0000755/// Destructor
756PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000757 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000758 E = PassManagers.end(); I != E; ++I)
759 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000760
Dan Gohman060d5ba2010-10-12 00:15:27 +0000761 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000762 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
763 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000764
765 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000766 DME = AnUsageMap.end(); DMI != DME; ++DMI)
767 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000768}
769
Devang Patelafb1f3622006-12-12 22:35:25 +0000770//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000771// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000772
Devang Patel643676c2006-11-11 01:10:19 +0000773/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000774void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000775 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000776
Chris Lattner60987362009-03-06 05:53:14 +0000777 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000778
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000779 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000780
Dan Gohmande6188a2010-08-12 23:50:08 +0000781 // This pass is the current implementation of all of the interfaces it
782 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000783 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
784 if (PInf == 0) return;
785 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000786 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000787 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000788}
789
Devang Patel9d9fc902007-03-06 17:52:53 +0000790// Return true if P preserves high level analysis used by other
791// passes managed by this manager
792bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000793 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000794 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000795 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000796
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000797 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000798 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000799 E = HigherLevelAnalysis.end(); I != E; ++I) {
800 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000801 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000802 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000803 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000804 PreservedSet.end())
805 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000806 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000807
Devang Patel9d9fc902007-03-06 17:52:53 +0000808 return true;
809}
810
Chris Lattner02eb94c2008-08-07 07:34:50 +0000811/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000812void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000813 // Don't do this unless assertions are enabled.
814#ifdef NDEBUG
815 return;
816#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000817 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
818 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000819
Devang Patelef432532007-07-19 05:36:09 +0000820 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000821 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000822 E = PreservedSet.end(); I != E; ++I) {
823 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000824 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000825 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000826 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000827 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000828 }
829}
830
Devang Patel67c79a42008-07-01 19:50:56 +0000831/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000832void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000833 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
834 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000835 return;
836
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000837 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000838 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000839 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000840 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000841 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000842 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000843 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000844 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000845 if (PassDebugging >= Details) {
846 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000847 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
848 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000849 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000850 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000851 }
Devang Patel349170f2006-11-11 01:24:55 +0000852 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000853
Devang Patel42dd1e92007-03-06 01:55:46 +0000854 // Check inherited analysis also. If P is not preserving analysis
855 // provided by parent manager then remove it here.
856 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
857
858 if (!InheritedAnalysis[Index])
859 continue;
860
Dan Gohmande6188a2010-08-12 23:50:08 +0000861 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000862 I = InheritedAnalysis[Index]->begin(),
863 E = InheritedAnalysis[Index]->end(); I != E; ) {
864 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000865 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000866 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000867 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000868 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000869 if (PassDebugging >= Details) {
870 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000871 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
872 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000873 }
Devang Patel01919d22007-03-08 19:05:01 +0000874 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000875 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000876 }
877 }
Devang Patelf68a3492006-11-07 22:35:17 +0000878}
879
Devang Patelca189262006-11-14 03:05:08 +0000880/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000881void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000882 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000883
Devang Patel8adae862007-07-20 18:04:54 +0000884 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000885
Devang Patel2ff44922007-04-16 20:39:59 +0000886 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000887 if (!TPM)
888 return;
889
Devang Patel17ad0962006-12-08 00:37:52 +0000890 TPM->collectLastUses(DeadPasses, P);
891
Devang Patel656a9172008-06-06 17:50:36 +0000892 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000893 dbgs() << " -*- '" << P->getPassName();
894 dbgs() << "' is the last user of following pass instances.";
895 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000896 }
897
Dan Gohman060d5ba2010-10-12 00:15:27 +0000898 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000899 E = DeadPasses.end(); I != E; ++I)
900 freePass(*I, Msg, DBG_STR);
901}
Devang Patel200d3052006-12-13 23:50:44 +0000902
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000903void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000904 enum PassDebuggingString DBG_STR) {
905 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000906
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000907 {
908 // If the pass crashes releasing memory, remember this.
909 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000910 TimeRegion PassTimer(getPassTimer(P));
911
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000912 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000913 }
914
Owen Andersona7aed182010-08-06 18:33:48 +0000915 AnalysisID PI = P->getPassID();
916 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000917 // Remove the pass itself (if it is not already removed).
918 AvailableAnalysis.erase(PI);
919
920 // Remove all interfaces this pass implements, for which it is also
921 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000922 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000923 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000924 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000925 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000926 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000927 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000928 }
Devang Patel17ad0962006-12-08 00:37:52 +0000929 }
Devang Patelca189262006-11-14 03:05:08 +0000930}
931
Dan Gohmande6188a2010-08-12 23:50:08 +0000932/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000933/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000934void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000935 // This manager is going to manage pass P. Set up analysis resolver
936 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000937 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000938 P->setResolver(AR);
939
Devang Patelec2b9a72007-03-05 22:57:49 +0000940 // If a FunctionPass F is the last user of ModulePass info M
941 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000942 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000943
Chris Lattner60987362009-03-06 05:53:14 +0000944 if (!ProcessAnalysis) {
945 // Add pass
946 PassVector.push_back(P);
947 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000948 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000949
Chris Lattner60987362009-03-06 05:53:14 +0000950 // At the moment, this pass is the last user of all required passes.
951 SmallVector<Pass *, 12> LastUses;
952 SmallVector<Pass *, 8> RequiredPasses;
953 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
954
955 unsigned PDepth = this->getDepth();
956
Dan Gohmande6188a2010-08-12 23:50:08 +0000957 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000958 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000959 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000960 E = RequiredPasses.end(); I != E; ++I) {
961 Pass *PRequired = *I;
962 unsigned RDepth = 0;
963
964 assert(PRequired->getResolver() && "Analysis Resolver is not set");
965 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
966 RDepth = DM.getDepth();
967
968 if (PDepth == RDepth)
969 LastUses.push_back(PRequired);
970 else if (PDepth > RDepth) {
971 // Let the parent claim responsibility of last use
972 TransferLastUses.push_back(PRequired);
973 // Keep track of higher level analysis used by this manager.
974 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000975 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000976 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000977 }
978
979 // Set P as P's last user until someone starts using P.
980 // However, if P is a Pass Manager then it does not need
981 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000982 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000983 LastUses.push_back(P);
984 TPM->setLastUser(LastUses, P);
985
986 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000987 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000988 TPM->setLastUser(TransferLastUses, My_PM);
989 TransferLastUses.clear();
990 }
991
Dan Gohman6304db32010-08-16 22:57:28 +0000992 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +0000993 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000994 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000995 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000996 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
997 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +0000998 this->addLowerLevelRequiredPass(P, AnalysisPass);
999 }
1000
1001 // Take a note of analysis required and made available by this pass.
1002 // Remove the analysis not preserved by this pass
1003 removeNotPreservedAnalysis(P);
1004 recordAvailableAnalysis(P);
1005
Devang Patel8cad70d2006-11-11 01:51:02 +00001006 // Add pass
1007 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001008}
1009
Devang Patele64d3052007-04-16 20:12:57 +00001010
1011/// Populate RP with analysis pass that are required by
1012/// pass P and are available. Populate RP_NotAvail with analysis
1013/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001014void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1015 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001016 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001017 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1018 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001019 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001020 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001021 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001022 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001023 else
Chris Lattner60987362009-03-06 05:53:14 +00001024 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001025 }
Devang Patelf58183d2006-12-12 23:09:32 +00001026
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001027 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001028 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001029 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001030 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001031 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001032 else
Chris Lattner60987362009-03-06 05:53:14 +00001033 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001034 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001035}
1036
Devang Patel07f4f582006-11-14 21:49:36 +00001037// All Required analyses should be available to the pass as it runs! Here
1038// we fill in the AnalysisImpls member of the pass so that it can
1039// successfully use the getAnalysis() method to retrieve the
1040// implementations it needs.
1041//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001042void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001043 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1044
Chris Lattnercbd160f2008-08-08 05:33:04 +00001045 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001046 I = AnUsage->getRequiredSet().begin(),
1047 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001048 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001049 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001050 // This may be analysis pass that is initialized on the fly.
1051 // If that is not the case then it will raise an assert when it is used.
1052 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001053 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001054 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001055 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001056 }
1057}
1058
Devang Patel640c5bb2006-12-08 22:30:11 +00001059/// Find the pass that implements Analysis AID. If desired pass is not found
1060/// then return NULL.
1061Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1062
1063 // Check if AvailableAnalysis map has one entry.
1064 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1065
1066 if (I != AvailableAnalysis.end())
1067 return I->second;
1068
1069 // Search Parents through TopLevelManager
1070 if (SearchParent)
1071 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001072
Devang Patel9d759b82006-12-09 00:09:12 +00001073 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001074}
1075
Devang Patel991aeba2006-12-15 20:13:01 +00001076// Print list of passes that are last used by P.
1077void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1078
Devang Patel8adae862007-07-20 18:04:54 +00001079 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001080
1081 // If this is a on the fly manager then it does not have TPM.
1082 if (!TPM)
1083 return;
1084
Devang Patel991aeba2006-12-15 20:13:01 +00001085 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001086
Dan Gohman7224bce2010-10-12 00:11:18 +00001087 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001088 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001089 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001090 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001091 }
1092}
1093
1094void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001095 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001096 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001097 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001098 PMD->dumpPassArguments();
1099 else
Owen Andersona7aed182010-08-06 18:33:48 +00001100 if (const PassInfo *PI =
1101 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001102 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001103 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001104 }
1105}
1106
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001107void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1108 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001109 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001110 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001111 return;
David Greene994e1bb2010-01-05 01:30:02 +00001112 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001113 switch (S1) {
1114 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001115 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001116 break;
1117 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001118 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001119 break;
1120 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001121 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001122 break;
1123 default:
1124 break;
1125 }
1126 switch (S2) {
1127 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001128 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001129 break;
1130 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001131 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001132 break;
1133 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001134 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001135 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001136 case ON_REGION_MSG:
1137 dbgs() << "' on Region '" << Msg << "'...\n";
1138 break;
Devang Patel003a5592007-03-05 20:01:30 +00001139 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
1145 default:
1146 break;
1147 }
Devang Patel991aeba2006-12-15 20:13:01 +00001148}
1149
Chris Lattner4c1e9542009-03-06 06:45:05 +00001150void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001151 if (PassDebugging < Details)
1152 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001153
Chris Lattner4c493d92008-08-08 15:14:09 +00001154 AnalysisUsage analysisUsage;
1155 P->getAnalysisUsage(analysisUsage);
1156 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1157}
1158
Chris Lattner4c1e9542009-03-06 06:45:05 +00001159void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001160 if (PassDebugging < Details)
1161 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001162
Chris Lattner4c493d92008-08-08 15:14:09 +00001163 AnalysisUsage analysisUsage;
1164 P->getAnalysisUsage(analysisUsage);
1165 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1166}
1167
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001168void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001169 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001170 assert(PassDebugging >= Details);
1171 if (Set.empty())
1172 return;
David Greene994e1bb2010-01-05 01:30:02 +00001173 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001174 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001175 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001176 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001177 if (!PInf) {
1178 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1179 // all drivers.
1180 dbgs() << " Uninitialized Pass";
1181 continue;
1182 }
Owen Andersona7aed182010-08-06 18:33:48 +00001183 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001184 }
David Greene994e1bb2010-01-05 01:30:02 +00001185 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001186}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001187
Devang Patel004937b2007-07-27 20:06:09 +00001188/// Add RequiredPass into list of lower level passes required by pass P.
1189/// RequiredPass is run on the fly by Pass Manager when P requests it
1190/// through getAnalysis interface.
1191/// This should be handled by specific pass manager.
1192void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1193 if (TPM) {
1194 TPM->dumpArguments();
1195 TPM->dumpPasses();
1196 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001197
Dan Gohmande6188a2010-08-12 23:50:08 +00001198 // Module Level pass may required Function Level analysis info
1199 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1200 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001201 // module level pass is requiring lower level analysis info managed by
1202 // lower level pass manager.
1203
1204 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001205 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001206 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001207#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001208 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1209 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001210#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001211 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001212}
1213
Owen Andersona7aed182010-08-06 18:33:48 +00001214Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001215 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001216}
1217
Devang Patele7599552007-01-12 18:52:44 +00001218// Destructor
1219PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001220 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001221 E = PassVector.end(); I != E; ++I)
1222 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001223}
1224
Devang Patel9bdf7d42006-12-08 23:28:54 +00001225//===----------------------------------------------------------------------===//
1226// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001227// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1228Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001229 return PM.findAnalysisPass(ID, dir);
1230}
1231
Dan Gohmande6188a2010-08-12 23:50:08 +00001232Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001233 Function &F) {
1234 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1235}
1236
Devang Patela1514cb2006-12-07 19:39:39 +00001237//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001238// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001239
Dan Gohmande6188a2010-08-12 23:50:08 +00001240/// Execute all of the passes scheduled for execution by invoking
1241/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001242/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001243bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001244 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001245 return false;
1246
Devang Patele9585592006-12-08 01:38:28 +00001247 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001248
Devang Patel6e5a1132006-11-07 21:31:57 +00001249 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001250 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1251 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001252 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001253
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001254 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001255 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001256
Devang Patelabfbe3b2006-12-16 00:56:26 +00001257 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001258
Chris Lattner4c1e9542009-03-06 06:45:05 +00001259 {
1260 // If the pass crashes, remember this.
1261 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001262 TimeRegion PassTimer(getPassTimer(BP));
1263
Dan Gohman74b189f2010-03-01 17:34:28 +00001264 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001265 }
Devang Patel93a197c2006-12-14 00:08:04 +00001266
Dan Gohman74b189f2010-03-01 17:34:28 +00001267 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001268 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001269 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001270 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001271 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001272
Devang Patela273d1c2007-07-19 18:02:32 +00001273 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001274 removeNotPreservedAnalysis(BP);
1275 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001276 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001277 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001278
Bill Wendling6ce6d262009-12-25 13:50:18 +00001279 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001280}
1281
Devang Patel475c4532006-12-08 00:59:05 +00001282// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001283bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001284 bool Changed = false;
1285
Chris Lattner4c1e9542009-03-06 06:45:05 +00001286 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1287 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001288
1289 return Changed;
1290}
1291
Duncan Sands51495602009-02-13 09:42:34 +00001292bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001293 bool Changed = false;
1294
Chris Lattner4c1e9542009-03-06 06:45:05 +00001295 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1296 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001297
1298 return Changed;
1299}
1300
Duncan Sands51495602009-02-13 09:42:34 +00001301bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001302 bool Changed = false;
1303
Devang Patelabfbe3b2006-12-16 00:56:26 +00001304 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1305 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001306 Changed |= BP->doInitialization(F);
1307 }
1308
1309 return Changed;
1310}
1311
Duncan Sands51495602009-02-13 09:42:34 +00001312bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001313 bool Changed = false;
1314
Devang Patelabfbe3b2006-12-16 00:56:26 +00001315 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1316 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001317 Changed |= BP->doFinalization(F);
1318 }
1319
1320 return Changed;
1321}
1322
1323
Devang Patela1514cb2006-12-07 19:39:39 +00001324//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001325// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001326
Devang Patel4e12f862006-11-08 10:44:40 +00001327/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001328FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001329 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001330 // FPM is the top level manager.
1331 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001332
Dan Gohman565df952008-03-13 02:08:36 +00001333 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001334 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001335}
1336
Devang Patelb67904d2006-12-13 02:36:01 +00001337FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001338 delete FPM;
1339}
1340
Devang Patel4e12f862006-11-08 10:44:40 +00001341/// add - Add a pass to the queue of passes to run. This passes
1342/// ownership of the Pass to the PassManager. When the
1343/// PassManager_X is destroyed, the pass will be destroyed as well, so
1344/// there is no need to delete the pass. (TODO delete passes.)
1345/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001346void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001347 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001348}
1349
Devang Patel9f3083e2006-11-15 19:39:54 +00001350/// run - Execute all of the passes scheduled for execution. Keep
1351/// track of whether any of the passes modifies the function, and if
1352/// so, return true.
1353///
Devang Patelb67904d2006-12-13 02:36:01 +00001354bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001355 if (F.isMaterializable()) {
1356 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001357 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001358 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001359 }
Devang Patel272908d2006-12-08 22:57:48 +00001360 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001361}
1362
1363
Devang Patelff631ae2006-11-15 01:27:05 +00001364/// doInitialization - Run all of the initializers for the function passes.
1365///
Devang Patelb67904d2006-12-13 02:36:01 +00001366bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001367 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001368}
1369
Dan Gohmane6656eb2007-07-30 14:51:13 +00001370/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001371///
Devang Patelb67904d2006-12-13 02:36:01 +00001372bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001373 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001374}
1375
Devang Patela1514cb2006-12-07 19:39:39 +00001376//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001377// FunctionPassManagerImpl implementation
1378//
Duncan Sands51495602009-02-13 09:42:34 +00001379bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001380 bool Changed = false;
1381
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001382 dumpArguments();
1383 dumpPasses();
1384
Chris Lattner4c1e9542009-03-06 06:45:05 +00001385 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1386 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001387
1388 return Changed;
1389}
1390
Duncan Sands51495602009-02-13 09:42:34 +00001391bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001392 bool Changed = false;
1393
Chris Lattner4c1e9542009-03-06 06:45:05 +00001394 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1395 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396
1397 return Changed;
1398}
1399
Devang Patelec9c58f2009-04-01 22:34:41 +00001400/// cleanup - After running all passes, clean up pass manager cache.
1401void FPPassManager::cleanup() {
1402 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1403 FunctionPass *FP = getContainedPass(Index);
1404 AnalysisResolver *AR = FP->getResolver();
1405 assert(AR && "Analysis Resolver is not set");
1406 AR->clearAnalysisImpls();
1407 }
1408}
1409
Torok Edwin24c78352009-06-29 18:49:09 +00001410void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1411 if (!wasRun)
1412 return;
1413 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1414 FPPassManager *FPPM = getContainedManager(Index);
1415 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1416 FPPM->getContainedPass(Index)->releaseMemory();
1417 }
1418 }
Torok Edwin896556e2009-06-29 21:05:10 +00001419 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001420}
1421
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422// Execute all the passes managed by this top level manager.
1423// Return true if any function is modified by a pass.
1424bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001426 TimingInfo::createTheTimeInfo();
1427
Devang Patele3068402006-12-21 00:16:50 +00001428 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001429 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1430 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001431
1432 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1433 getContainedManager(Index)->cleanup();
1434
Torok Edwin24c78352009-06-29 18:49:09 +00001435 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001436 return Changed;
1437}
1438
1439//===----------------------------------------------------------------------===//
1440// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001441
Devang Patel8c78a0b2007-05-03 01:11:54 +00001442char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001443/// Print passes managed by this manager
1444void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001445 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001446 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1447 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001448 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001449 dumpLastUses(FP, Offset+1);
1450 }
1451}
1452
1453
Dan Gohmande6188a2010-08-12 23:50:08 +00001454/// Execute all of the passes scheduled for execution by invoking
1455/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001456/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001457bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001458 if (F.isDeclaration())
1459 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001460
1461 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001462
Devang Patelcbbf2912008-03-20 01:09:53 +00001463 // Collect inherited analysis from Module level pass manager.
1464 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001465
Devang Patelabfbe3b2006-12-16 00:56:26 +00001466 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1467 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001468 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001469
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001470 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001471 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001472
Devang Patelabfbe3b2006-12-16 00:56:26 +00001473 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001474
Chris Lattner4c1e9542009-03-06 06:45:05 +00001475 {
1476 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001477 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001478
Dan Gohman74b189f2010-03-01 17:34:28 +00001479 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001480 }
Devang Patel93a197c2006-12-14 00:08:04 +00001481
Dan Gohman74b189f2010-03-01 17:34:28 +00001482 Changed |= LocalChanged;
1483 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001484 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001485 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001486
Devang Patela273d1c2007-07-19 18:02:32 +00001487 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001488 removeNotPreservedAnalysis(FP);
1489 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001490 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001491 }
1492 return Changed;
1493}
1494
Devang Patel67d6a5e2006-12-19 19:46:59 +00001495bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001496 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001497
Dan Gohmane7630be2010-05-11 20:30:00 +00001498 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001499 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001500
Bill Wendling6ce6d262009-12-25 13:50:18 +00001501 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001502}
1503
Duncan Sands51495602009-02-13 09:42:34 +00001504bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001505 bool Changed = false;
1506
Chris Lattner4c1e9542009-03-06 06:45:05 +00001507 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1508 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001509
1510 return Changed;
1511}
1512
Duncan Sands51495602009-02-13 09:42:34 +00001513bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001514 bool Changed = false;
1515
Chris Lattner4c1e9542009-03-06 06:45:05 +00001516 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1517 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001518
Devang Patelff631ae2006-11-15 01:27:05 +00001519 return Changed;
1520}
1521
Devang Patela1514cb2006-12-07 19:39:39 +00001522//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001523// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001524
Dan Gohmande6188a2010-08-12 23:50:08 +00001525/// Execute all of the passes scheduled for execution by invoking
1526/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001527/// the module, and if so, return true.
1528bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001529MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001530 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001531
Torok Edwin24c78352009-06-29 18:49:09 +00001532 // Initialize on-the-fly passes
1533 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1534 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1535 I != E; ++I) {
1536 FunctionPassManagerImpl *FPP = I->second;
1537 Changed |= FPP->doInitialization(M);
1538 }
1539
Devang Patelabfbe3b2006-12-16 00:56:26 +00001540 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1541 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001542 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001543
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001544 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001545 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001546
Devang Patelabfbe3b2006-12-16 00:56:26 +00001547 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001548
Chris Lattner4c1e9542009-03-06 06:45:05 +00001549 {
1550 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001551 TimeRegion PassTimer(getPassTimer(MP));
1552
Dan Gohman74b189f2010-03-01 17:34:28 +00001553 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001554 }
Devang Patel93a197c2006-12-14 00:08:04 +00001555
Dan Gohman74b189f2010-03-01 17:34:28 +00001556 Changed |= LocalChanged;
1557 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001558 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001559 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001560 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001561
Devang Patela273d1c2007-07-19 18:02:32 +00001562 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001563 removeNotPreservedAnalysis(MP);
1564 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001565 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001566 }
Torok Edwin24c78352009-06-29 18:49:09 +00001567
1568 // Finalize on-the-fly passes
1569 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1570 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1571 I != E; ++I) {
1572 FunctionPassManagerImpl *FPP = I->second;
1573 // We don't know when is the last time an on-the-fly pass is run,
1574 // so we need to releaseMemory / finalize here
1575 FPP->releaseMemoryOnTheFly();
1576 Changed |= FPP->doFinalization(M);
1577 }
Devang Patel05e1a972006-11-07 22:03:15 +00001578 return Changed;
1579}
1580
Devang Patele64d3052007-04-16 20:12:57 +00001581/// Add RequiredPass into list of lower level passes required by pass P.
1582/// RequiredPass is run on the fly by Pass Manager when P requests it
1583/// through getAnalysis interface.
1584void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001585 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1586 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001587 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001588 RequiredPass->getPotentialPassManagerType()) &&
1589 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001590
Devang Patel68f72b12007-04-26 17:50:19 +00001591 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001592 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001593 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001594 // FPP is the top level manager.
1595 FPP->setTopLevelManager(FPP);
1596
Devang Patel69e9f6d2007-04-16 20:27:05 +00001597 OnTheFlyManagers[P] = FPP;
1598 }
Devang Patel68f72b12007-04-26 17:50:19 +00001599 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001600
Devang Patel68f72b12007-04-26 17:50:19 +00001601 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001602 if (RequiredPass) {
1603 SmallVector<Pass *, 1> LU;
1604 LU.push_back(RequiredPass);
1605 FPP->setLastUser(LU, P);
1606 }
Devang Patele64d3052007-04-16 20:12:57 +00001607}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001608
Dan Gohmande6188a2010-08-12 23:50:08 +00001609/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001610/// required by module pass MP. Instantiate analysis pass, by using
1611/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001612Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001613 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001614 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001615
Torok Edwin24c78352009-06-29 18:49:09 +00001616 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001617 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001618 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001619}
1620
1621
Devang Patela1514cb2006-12-07 19:39:39 +00001622//===----------------------------------------------------------------------===//
1623// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001624//
Devang Patelc290c8a2006-11-07 22:23:34 +00001625/// run - Execute all of the passes scheduled for execution. Keep track of
1626/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001627bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001628 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001629 TimingInfo::createTheTimeInfo();
1630
Devang Patelcfd70c42006-12-13 22:10:00 +00001631 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001632 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001633
Devang Patele3068402006-12-21 00:16:50 +00001634 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001635 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1636 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001637 return Changed;
1638}
Devang Patel376fefa2006-11-08 10:29:57 +00001639
Devang Patela1514cb2006-12-07 19:39:39 +00001640//===----------------------------------------------------------------------===//
1641// PassManager implementation
1642
Devang Patel376fefa2006-11-08 10:29:57 +00001643/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001644PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001645 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001646 // PM is the top level manager
1647 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001648}
1649
Devang Patelb67904d2006-12-13 02:36:01 +00001650PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001651 delete PM;
1652}
1653
Devang Patel376fefa2006-11-08 10:29:57 +00001654/// add - Add a pass to the queue of passes to run. This passes ownership of
1655/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1656/// will be destroyed as well, so there is no need to delete the pass. This
1657/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001658void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001659 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001660}
1661
1662/// run - Execute all of the passes scheduled for execution. Keep track of
1663/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001664bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001665 return PM->run(M);
1666}
1667
Devang Patelb8817b92006-12-14 00:59:42 +00001668//===----------------------------------------------------------------------===//
1669// TimingInfo Class - This class is used to calculate information about the
1670// amount of time each pass takes to execute. This only happens with
1671// -time-passes is enabled on the command line.
1672//
1673bool llvm::TimePassesIsEnabled = false;
1674static cl::opt<bool,true>
1675EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1676 cl::desc("Time each pass, printing elapsed time for each on exit"));
1677
1678// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1679// a non null value (if the -time-passes option is enabled) or it leaves it
1680// null. It may be called multiple times.
1681void TimingInfo::createTheTimeInfo() {
1682 if (!TimePassesIsEnabled || TheTimeInfo) return;
1683
1684 // Constructed the first time this is called, iff -time-passes is enabled.
1685 // This guarantees that the object will be constructed before static globals,
1686 // thus it will be destroyed before them.
1687 static ManagedStatic<TimingInfo> TTI;
1688 TheTimeInfo = &*TTI;
1689}
1690
Devang Patel1c3633e2007-01-29 23:10:37 +00001691/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001692Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001693 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001694 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001695 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001696}
1697
Devang Patel1c56a632007-01-08 19:29:38 +00001698//===----------------------------------------------------------------------===//
1699// PMStack implementation
1700//
Devang Patelad98d232007-01-11 22:15:30 +00001701
Devang Patel1c56a632007-01-08 19:29:38 +00001702// Pop Pass Manager from the stack and clear its analysis info.
1703void PMStack::pop() {
1704
1705 PMDataManager *Top = this->top();
1706 Top->initializeAnalysisInfo();
1707
1708 S.pop_back();
1709}
1710
1711// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001712void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001713 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001714 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001715
Chris Lattner60987362009-03-06 05:53:14 +00001716 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001717 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1718 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001719 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001720
Chris Lattner60987362009-03-06 05:53:14 +00001721 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001722 TPM->addIndirectPassManager(PM);
1723 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001724 PM->setDepth(this->top()->getDepth()+1);
1725 }
1726 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001727 assert((PM->getPassManagerType() == PMT_ModulePassManager
1728 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001729 && "pushing bad pass manager to PMStack");
1730 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001731 }
1732
Devang Patel15701b52007-01-11 00:19:00 +00001733 S.push_back(PM);
1734}
1735
1736// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001737void PMStack::dump() const {
1738 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001739 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001740 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001741
Devang Patel15701b52007-01-11 00:19:00 +00001742 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001743 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001744}
1745
Devang Patel1c56a632007-01-08 19:29:38 +00001746/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001747/// add self into that manager.
1748void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001749 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001750 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001751 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001752 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1753 if (TopPMType == PreferredType)
1754 break; // We found desired pass manager
1755 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001756 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001757 else
1758 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001759 }
Devang Patel18ff6362008-09-09 21:38:40 +00001760 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001761 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001762}
1763
Devang Patel3312f752007-01-16 21:43:18 +00001764/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001765/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001766void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001767 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001768
Andrew Trickcbc845f2012-02-01 07:16:20 +00001769 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001770 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001771 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1772 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001773 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001774 break;
Devang Patel3312f752007-01-16 21:43:18 +00001775 }
Devang Patel3312f752007-01-16 21:43:18 +00001776
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001777 // Create new Function Pass Manager if needed.
1778 FPPassManager *FPP;
1779 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1780 FPP = (FPPassManager *)PMS.top();
1781 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001782 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1783 PMDataManager *PMD = PMS.top();
1784
1785 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001786 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001787 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001788
1789 // [2] Set up new manager's top level manager
1790 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1791 TPM->addIndirectPassManager(FPP);
1792
1793 // [3] Assign manager to manage this new manager. This may create
1794 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001795 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001796
1797 // [4] Push new manager into PMS
1798 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001799 }
1800
Devang Patel3312f752007-01-16 21:43:18 +00001801 // Assign FPP as the manager of this pass.
1802 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001803}
1804
Devang Patel3312f752007-01-16 21:43:18 +00001805/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001806/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001807void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001808 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001809 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001810
Devang Patel15701b52007-01-11 00:19:00 +00001811 // Basic Pass Manager is a leaf pass manager. It does not handle
1812 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001813 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001814 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1815 BBP = (BBPassManager *)PMS.top();
1816 } else {
1817 // If leaf manager is not Basic Block Pass manager then create new
1818 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001819 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1820 PMDataManager *PMD = PMS.top();
1821
1822 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001823 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001824
1825 // [2] Set up new manager's top level manager
1826 // Basic Block Pass Manager does not live by itself
1827 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1828 TPM->addIndirectPassManager(BBP);
1829
Devang Patel15701b52007-01-11 00:19:00 +00001830 // [3] Assign manager to manage this new manager. This may create
1831 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001832 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001833
Devang Patel3312f752007-01-16 21:43:18 +00001834 // [4] Push new manager into PMS
1835 PMS.push(BBP);
1836 }
Devang Patel1c56a632007-01-08 19:29:38 +00001837
Devang Patel3312f752007-01-16 21:43:18 +00001838 // Assign BBP as the manager of this pass.
1839 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001840}
1841
Dan Gohmand3a20c92008-03-11 16:41:42 +00001842PassManagerBase::~PassManagerBase() {}