blob: 773862d20305a0278076e7df05aead0f602be202 [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"
Devang Patelfa31d382011-03-10 00:21:25 +000017#include "llvm/DebugInfoProbe.h"
David Greene9b063df2010-04-02 23:17:14 +000018#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000019#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000020#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000021#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000022#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000023#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000025#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000026#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000027#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Mutex.h"
Devang Patelfa31d382011-03-10 00:21:25 +000029#include "llvm/ADT/StringMap.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000030#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000031#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000033
Devang Patele7599552007-01-12 18:52:44 +000034// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000035
Devang Patelf1567a52006-12-13 20:03:48 +000036namespace llvm {
37
38//===----------------------------------------------------------------------===//
39// Pass debugging information. Often it is useful to find out what pass is
40// running when a crash occurs in a utility. When this library is compiled with
41// debugging on, a command line option (--debug-pass) is enabled that causes the
42// pass name to be printed before it executes.
43//
44
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
47 None, Arguments, Structure, Executions, Details
48};
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000060
61typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
63
64// Print IR out before/after specified passes.
65static PassOptionList
66PrintBefore("print-before",
Eric Christopher787ba0b2011-03-09 19:46:51 +000067 llvm::cl::desc("Print IR before specified passes"),
68 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000069
70static PassOptionList
71PrintAfter("print-after",
Eric Christopher787ba0b2011-03-09 19:46:51 +000072 llvm::cl::desc("Print IR after specified passes"),
73 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000074
75static cl::opt<bool>
76PrintBeforeAll("print-before-all",
77 llvm::cl::desc("Print IR before each pass"),
78 cl::init(false));
79static cl::opt<bool>
80PrintAfterAll("print-after-all",
81 llvm::cl::desc("Print IR after each pass"),
82 cl::init(false));
83
84/// This is a helper to determine whether to print IR before or
85/// after a pass.
86
Andrew Trickcbc845f2012-02-01 07:16:20 +000087static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
David Greene9b063df2010-04-02 23:17:14 +000088 PassOptionList &PassesToPrint) {
Andrew Trickcbc845f2012-02-01 07:16:20 +000089 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
90 const llvm::PassInfo *PassInf = PassesToPrint[i];
91 if (PassInf)
92 if (PassInf->getPassArgument() == PI->getPassArgument()) {
93 return true;
94 }
David Greene9b063df2010-04-02 23:17:14 +000095 }
96 return false;
97}
Dan Gohmande6188a2010-08-12 23:50:08 +000098
David Greene9b063df2010-04-02 23:17:14 +000099/// This is a utility to check whether a pass should have IR dumped
100/// before it.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000101static bool ShouldPrintBeforePass(const PassInfo *PI) {
102 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000103}
104
105/// This is a utility to check whether a pass should have IR dumped
106/// after it.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000107static bool ShouldPrintAfterPass(const PassInfo *PI) {
108 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000109}
110
Devang Patelf1567a52006-12-13 20:03:48 +0000111} // End of llvm namespace
112
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000113/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
114/// or higher is specified.
115bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
116 return PassDebugging >= Executions;
117}
118
119
120
121
Chris Lattner4c1e9542009-03-06 06:45:05 +0000122void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
123 if (V == 0 && M == 0)
124 OS << "Releasing pass '";
125 else
126 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000127
Chris Lattner4c1e9542009-03-06 06:45:05 +0000128 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000129
Chris Lattner4c1e9542009-03-06 06:45:05 +0000130 if (M) {
131 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
132 return;
133 }
134 if (V == 0) {
135 OS << '\n';
136 return;
137 }
138
Dan Gohman79fc0e92009-03-10 18:47:59 +0000139 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000141 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000144 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000145 OS << "value";
146
147 OS << " '";
148 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
149 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000150}
151
152
Devang Patelffca9102006-12-15 19:39:30 +0000153namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000154
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000157//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000159/// pass together and sequence them to process one basic block before
160/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000161class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000162
163public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000164 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000165 explicit BBPassManager()
166 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000167
Devang Patelca58e352006-11-08 10:05:38 +0000168 /// Execute all of the passes scheduled for execution. Keep track of
169 /// whether any of the passes modifies the function, and if so, return true.
170 bool runOnFunction(Function &F);
171
Devang Patelf9d96b92006-12-07 19:57:52 +0000172 /// Pass Manager itself does not invalidate any analysis info.
173 void getAnalysisUsage(AnalysisUsage &Info) const {
174 Info.setPreservesAll();
175 }
176
Devang Patel475c4532006-12-08 00:59:05 +0000177 bool doInitialization(Module &M);
178 bool doInitialization(Function &F);
179 bool doFinalization(Module &M);
180 bool doFinalization(Function &F);
181
Chris Lattner2fa26e52010-01-22 05:24:46 +0000182 virtual PMDataManager *getAsPMDataManager() { return this; }
183 virtual Pass *getAsPass() { return this; }
184
Devang Patele3858e62007-02-01 22:08:25 +0000185 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000186 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000187 }
188
Devang Pateleda56172006-12-12 23:34:33 +0000189 // Print passes managed by this manager
190 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000191 llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000192 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
193 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000194 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000195 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000196 }
197 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000198
199 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000200 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000201 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
202 return BP;
203 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000204
Dan Gohmande6188a2010-08-12 23:50:08 +0000205 virtual PassManagerType getPassManagerType() const {
206 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000207 }
Devang Patelca58e352006-11-08 10:05:38 +0000208};
209
Devang Patel8c78a0b2007-05-03 01:11:54 +0000210char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000211}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000212
Devang Patele7599552007-01-12 18:52:44 +0000213namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000214
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000217//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218/// FunctionPassManagerImpl manages FPPassManagers
219class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000220 public PMDataManager,
221 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000222 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000223private:
224 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000226 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000227 explicit FunctionPassManagerImpl() :
228 Pass(PT_PassManager, ID), PMDataManager(),
229 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000230
231 /// add - Add a pass to the queue of passes to run. This passes ownership of
232 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
233 /// will be destroyed as well, so there is no need to delete the pass. This
234 /// implies that all passes MUST be allocated with 'new'.
235 void add(Pass *P) {
236 schedulePass(P);
237 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000238
239 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000240 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
241 return createPrintFunctionPass(Banner, &O);
242 }
243
Torok Edwin24c78352009-06-29 18:49:09 +0000244 // Prepare for running an on the fly pass, freeing memory if needed
245 // from a previous run.
246 void releaseMemoryOnTheFly();
247
Devang Patel67d6a5e2006-12-19 19:46:59 +0000248 /// run - Execute all of the passes scheduled for execution. Keep track of
249 /// whether any of the passes modifies the module, and if so, return true.
250 bool run(Function &F);
251
252 /// doInitialization - Run all of the initializers for the function passes.
253 ///
254 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000255
Dan Gohmane6656eb2007-07-30 14:51:13 +0000256 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000257 ///
258 bool doFinalization(Module &M);
259
Dan Gohmande6188a2010-08-12 23:50:08 +0000260
Chris Lattner2fa26e52010-01-22 05:24:46 +0000261 virtual PMDataManager *getAsPMDataManager() { return this; }
262 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000263 virtual PassManagerType getTopLevelPassManagerType() {
264 return PMT_FunctionPassManager;
265 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000266
Devang Patel67d6a5e2006-12-19 19:46:59 +0000267 /// Pass Manager itself does not invalidate any analysis info.
268 void getAnalysisUsage(AnalysisUsage &Info) const {
269 Info.setPreservesAll();
270 }
271
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000273 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
275 return FP;
276 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000277};
278
David Blaikiea379b1812011-12-20 02:50:00 +0000279void FunctionPassManagerImpl::anchor() {}
280
Devang Patel8c78a0b2007-05-03 01:11:54 +0000281char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000282
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283//===----------------------------------------------------------------------===//
284// MPPassManager
285//
286/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000287/// It batches all Module passes and function pass managers together and
288/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000289class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000290public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000291 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000292 explicit MPPassManager() :
293 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000294
295 // Delete on the fly managers.
296 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000297 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000298 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
299 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000300 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000301 delete FPP;
302 }
303 }
304
Dan Gohmande6188a2010-08-12 23:50:08 +0000305 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000306 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
307 return createPrintModulePass(&O, false, Banner);
308 }
309
Devang Patelca58e352006-11-08 10:05:38 +0000310 /// run - Execute all of the passes scheduled for execution. Keep track of
311 /// whether any of the passes modifies the module, and if so, return true.
312 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000313
Devang Patelf9d96b92006-12-07 19:57:52 +0000314 /// Pass Manager itself does not invalidate any analysis info.
315 void getAnalysisUsage(AnalysisUsage &Info) const {
316 Info.setPreservesAll();
317 }
318
Devang Patele64d3052007-04-16 20:12:57 +0000319 /// Add RequiredPass into list of lower level passes required by pass P.
320 /// RequiredPass is run on the fly by Pass Manager when P requests it
321 /// through getAnalysis interface.
322 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
323
Dan Gohmande6188a2010-08-12 23:50:08 +0000324 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000325 /// required by module pass MP. Instantiate analysis pass, by using
326 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000327 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000328
Devang Patele3858e62007-02-01 22:08:25 +0000329 virtual const char *getPassName() const {
330 return "Module Pass Manager";
331 }
332
Chris Lattner2fa26e52010-01-22 05:24:46 +0000333 virtual PMDataManager *getAsPMDataManager() { return this; }
334 virtual Pass *getAsPass() { return this; }
335
Devang Pateleda56172006-12-12 23:34:33 +0000336 // Print passes managed by this manager
337 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000338 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000339 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
340 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000341 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000342 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
343 OnTheFlyManagers.find(MP);
344 if (I != OnTheFlyManagers.end())
345 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000346 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000347 }
348 }
349
Devang Patelabfbe3b2006-12-16 00:56:26 +0000350 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000351 assert(N < PassVector.size() && "Pass number out of range!");
352 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000353 }
354
Dan Gohmande6188a2010-08-12 23:50:08 +0000355 virtual PassManagerType getPassManagerType() const {
356 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000357 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000358
359 private:
360 /// Collection of on the fly FPPassManagers. These managers manage
361 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000362 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000363};
364
Devang Patel8c78a0b2007-05-03 01:11:54 +0000365char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000366//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000367// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000368//
Devang Patel09f162c2007-05-01 21:15:47 +0000369
Devang Patel67d6a5e2006-12-19 19:46:59 +0000370/// PassManagerImpl manages MPPassManagers
371class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000372 public PMDataManager,
373 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000374 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000375
376public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000377 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000378 explicit PassManagerImpl() :
379 Pass(PT_PassManager, ID), PMDataManager(),
380 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000381
Devang Patel376fefa2006-11-08 10:29:57 +0000382 /// add - Add a pass to the queue of passes to run. This passes ownership of
383 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
384 /// will be destroyed as well, so there is no need to delete the pass. This
385 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000386 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000387 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000388 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000389
390 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000391 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
392 return createPrintModulePass(&O, false, Banner);
393 }
394
Devang Patel376fefa2006-11-08 10:29:57 +0000395 /// run - Execute all of the passes scheduled for execution. Keep track of
396 /// whether any of the passes modifies the module, and if so, return true.
397 bool run(Module &M);
398
Devang Patelf9d96b92006-12-07 19:57:52 +0000399 /// Pass Manager itself does not invalidate any analysis info.
400 void getAnalysisUsage(AnalysisUsage &Info) const {
401 Info.setPreservesAll();
402 }
403
Chris Lattner2fa26e52010-01-22 05:24:46 +0000404 virtual PMDataManager *getAsPMDataManager() { return this; }
405 virtual Pass *getAsPass() { return this; }
Andrew Trickcbc845f2012-02-01 07:16:20 +0000406 virtual PassManagerType getTopLevelPassManagerType() {
407 return PMT_ModulePassManager;
408 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000409
Devang Patel67d6a5e2006-12-19 19:46:59 +0000410 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000411 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000412 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
413 return MP;
414 }
Devang Patel376fefa2006-11-08 10:29:57 +0000415};
416
David Blaikiea379b1812011-12-20 02:50:00 +0000417void PassManagerImpl::anchor() {}
418
Devang Patel8c78a0b2007-05-03 01:11:54 +0000419char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000420} // End of llvm namespace
421
422namespace {
423
424//===----------------------------------------------------------------------===//
Devang Patelfa31d382011-03-10 00:21:25 +0000425// DebugInfoProbe
426
427static DebugInfoProbeInfo *TheDebugProbe;
428static void createDebugInfoProbe() {
429 if (TheDebugProbe) return;
Andrew Trickb3bddf02011-06-03 00:44:32 +0000430
431 // Constructed the first time this is called. This guarantees that the
432 // object will be constructed, if -enable-debug-info-probe is set,
Devang Patelfa31d382011-03-10 00:21:25 +0000433 // before static globals, thus it will be destroyed before them.
434 static ManagedStatic<DebugInfoProbeInfo> DIP;
435 TheDebugProbe = &*DIP;
436}
437
438//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000439/// TimingInfo Class - This class is used to calculate information about the
440/// amount of time each pass takes to execute. This only happens when
441/// -time-passes is enabled on the command line.
442///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000443
Owen Anderson5a6960f2009-06-18 20:51:00 +0000444static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000445
Nick Lewycky02d5f772009-10-25 06:33:48 +0000446class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000447 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000448 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000449public:
450 // Use 'create' member to get this.
451 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000452
Devang Patel1c3633e2007-01-29 23:10:37 +0000453 // TimingDtor - Print out information about timing information
454 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000455 // Delete all of the timers, which accumulate their info into the
456 // TimerGroup.
457 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
458 E = TimingData.end(); I != E; ++I)
459 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000460 // TimerGroup is deleted next, printing the report.
461 }
462
463 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
464 // to a non null value (if the -time-passes option is enabled) or it leaves it
465 // null. It may be called multiple times.
466 static void createTheTimeInfo();
467
Chris Lattner707431c2010-03-30 04:03:22 +0000468 /// getPassTimer - Return the timer for the specified pass if it exists.
469 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000470 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000471 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000472
Owen Anderson5c96ef72009-07-07 18:33:04 +0000473 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000474 Timer *&T = TimingData[P];
475 if (T == 0)
476 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000477 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000478 }
479};
480
Devang Patel1c3633e2007-01-29 23:10:37 +0000481} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000482
Dan Gohmand78c4002008-05-13 00:00:25 +0000483static TimingInfo *TheTimeInfo;
484
Devang Patela1514cb2006-12-07 19:39:39 +0000485//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000486// PMTopLevelManager implementation
487
Devang Patel4268fc02007-01-16 02:00:38 +0000488/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000489PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
490 PMDM->setTopLevelManager(this);
491 addPassManager(PMDM);
492 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000493}
494
Devang Patelafb1f3622006-12-12 22:35:25 +0000495/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000496void
497PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
498 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000499 unsigned PDepth = 0;
500 if (P->getResolver())
501 PDepth = P->getResolver()->getPMDataManager().getDepth();
502
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000503 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000504 E = AnalysisPasses.end(); I != E; ++I) {
505 Pass *AP = *I;
506 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000507
Devang Patel01919d22007-03-08 19:05:01 +0000508 if (P == AP)
509 continue;
510
Tobias Grosserf07426b2011-01-20 21:03:22 +0000511 // Update the last users of passes that are required transitive by AP.
512 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
513 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
514 SmallVector<Pass *, 12> LastUses;
515 SmallVector<Pass *, 12> LastPMUses;
516 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
517 E = IDs.end(); I != E; ++I) {
518 Pass *AnalysisPass = findAnalysisPass(*I);
519 assert(AnalysisPass && "Expected analysis pass to exist.");
520 AnalysisResolver *AR = AnalysisPass->getResolver();
521 assert(AR && "Expected analysis resolver to exist.");
522 unsigned APDepth = AR->getPMDataManager().getDepth();
523
524 if (PDepth == APDepth)
525 LastUses.push_back(AnalysisPass);
526 else if (PDepth > APDepth)
527 LastPMUses.push_back(AnalysisPass);
528 }
529
530 setLastUser(LastUses, P);
531
532 // If this pass has a corresponding pass manager, push higher level
533 // analysis to this pass manager.
534 if (P->getResolver())
535 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
536
537
Devang Patelafb1f3622006-12-12 22:35:25 +0000538 // If AP is the last user of other passes then make P last user of
539 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000540 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000541 LUE = LastUser.end(); LUI != LUE; ++LUI) {
542 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000543 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000544 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000545 LastUser[LUI->first] = P;
546 }
547 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000548}
549
550/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000551void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000552 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000553 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000554 InversedLastUser.find(P);
555 if (DMI == InversedLastUser.end())
556 return;
557
558 SmallPtrSet<Pass *, 8> &LU = DMI->second;
559 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
560 E = LU.end(); I != E; ++I) {
561 LastUses.push_back(*I);
562 }
563
Devang Patelafb1f3622006-12-12 22:35:25 +0000564}
565
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000566AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
567 AnalysisUsage *AnUsage = NULL;
568 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000569 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000570 AnUsage = DMI->second;
571 else {
572 AnUsage = new AnalysisUsage();
573 P->getAnalysisUsage(*AnUsage);
574 AnUsageMap[P] = AnUsage;
575 }
576 return AnUsage;
577}
578
Devang Patelafb1f3622006-12-12 22:35:25 +0000579/// Schedule pass P for execution. Make sure that passes required by
580/// P are run before P is run. Update analysis info maintained by
581/// the manager. Remove dead passes. This is a recursive function.
582void PMTopLevelManager::schedulePass(Pass *P) {
583
Devang Patel3312f752007-01-16 21:43:18 +0000584 // TODO : Allocate function manager for this pass, other wise required set
585 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000586
Devang Pateld74ede72007-03-06 01:06:16 +0000587 // Give pass a chance to prepare the stage.
588 P->preparePassManager(activeStack);
589
Devang Patel864970e2008-03-18 00:39:19 +0000590 // If P is an analysis pass and it is available then do not
591 // generate the analysis again. Stale analysis info should not be
592 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000593 const PassInfo *PI =
594 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
595 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000596 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000597 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000598 }
Devang Patel864970e2008-03-18 00:39:19 +0000599
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000600 AnalysisUsage *AnUsage = findAnalysisUsage(P);
601
Devang Patelfdee7032008-08-14 23:07:48 +0000602 bool checkAnalysis = true;
603 while (checkAnalysis) {
604 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000605
Devang Patelfdee7032008-08-14 23:07:48 +0000606 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
607 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
608 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000609
Devang Patelfdee7032008-08-14 23:07:48 +0000610 Pass *AnalysisPass = findAnalysisPass(*I);
611 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000612 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000613 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000614 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000615 if (P->getPotentialPassManagerType () ==
616 AnalysisPass->getPotentialPassManagerType())
617 // Schedule analysis pass that is managed by the same pass manager.
618 schedulePass(AnalysisPass);
619 else if (P->getPotentialPassManagerType () >
620 AnalysisPass->getPotentialPassManagerType()) {
621 // Schedule analysis pass that is managed by a new manager.
622 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000623 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000624 // are already checked are still available.
625 checkAnalysis = true;
626 }
627 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000628 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000629 // passes are run on the fly.
630 delete AnalysisPass;
631 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000632 }
633 }
634
635 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000636 if (ImmutablePass *IP = P->getAsImmutablePass()) {
637 // P is a immutable pass and it will be managed by this
638 // top level manager. Set up analysis resolver to connect them.
639 PMDataManager *DM = getAsPMDataManager();
640 AnalysisResolver *AR = new AnalysisResolver(*DM);
641 P->setResolver(AR);
642 DM->initializeAnalysisImpl(P);
643 addImmutablePass(IP);
644 DM->recordAvailableAnalysis(IP);
645 return;
646 }
647
648 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
649 Pass *PP = P->createPrinterPass(
650 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
651 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
652 }
653
654 // Add the requested pass to the best available pass manager.
655 P->assignPassManager(activeStack, getTopLevelPassManagerType());
656
657 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
658 Pass *PP = P->createPrinterPass(
659 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
660 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
661 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000662}
663
664/// Find the pass that implements Analysis AID. Search immutable
665/// passes and all pass managers. If desired pass is not found
666/// then return NULL.
667Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
668
Devang Patelcd6ba152006-12-12 22:50:05 +0000669 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000670 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000671 E = PassManagers.end(); I != E; ++I)
672 if (Pass *P = (*I)->findAnalysisPass(AID, false))
673 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000674
675 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000676 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000677 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000678 E = IndirectPassManagers.end(); I != E; ++I)
679 if (Pass *P = (*I)->findAnalysisPass(AID, false))
680 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000681
Dan Gohman844dd0a2010-10-11 23:19:01 +0000682 // Check the immutable passes. Iterate in reverse order so that we find
683 // the most recently registered passes first.
684 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
685 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000686 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000687 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000688 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000689
690 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000691 const PassInfo *PassInf =
692 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000693 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000694 const std::vector<const PassInfo*> &ImmPI =
695 PassInf->getInterfacesImplemented();
696 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
697 EE = ImmPI.end(); II != EE; ++II) {
698 if ((*II)->getTypeInfo() == AID)
699 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000700 }
701 }
702
Dan Gohman844dd0a2010-10-11 23:19:01 +0000703 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000704}
705
Devang Pateleda56172006-12-12 23:34:33 +0000706// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000707void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000708
Devang Patelfd4184322007-01-17 20:33:36 +0000709 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000710 return;
711
Devang Pateleda56172006-12-12 23:34:33 +0000712 // Print out the immutable passes
713 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000714 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000715 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000716
Dan Gohmanf71c5212010-08-19 01:29:07 +0000717 // Every class that derives from PMDataManager also derives from Pass
718 // (sometimes indirectly), but there's no inheritance relationship
719 // between PMDataManager and Pass, so we have to getAsPass to get
720 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000721 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000722 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000723 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000724}
725
Devang Patel991aeba2006-12-15 20:13:01 +0000726void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000727
Devang Patelfd4184322007-01-17 20:33:36 +0000728 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000729 return;
730
David Greene994e1bb2010-01-05 01:30:02 +0000731 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000732 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
733 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
734 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000735 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
736 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000737 if (!PI->isAnalysisGroup())
738 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000739 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000740 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000741 E = PassManagers.end(); I != E; ++I)
742 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000743 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000744}
745
Devang Patele3068402006-12-21 00:16:50 +0000746void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000747 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000748 E = PassManagers.end(); I != E; ++I)
749 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000750
Devang Patele3068402006-12-21 00:16:50 +0000751 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000752 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000753 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
754 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000755 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000756
Chris Lattner60987362009-03-06 05:53:14 +0000757 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000758 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000759 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000760 InversedLastUser.find(DMI->second);
761 if (InvDMI != InversedLastUser.end()) {
762 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
763 L.insert(DMI->first);
764 } else {
765 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
766 InversedLastUser[DMI->second] = L;
767 }
768 }
Devang Patele3068402006-12-21 00:16:50 +0000769}
770
Devang Patele7599552007-01-12 18:52:44 +0000771/// Destructor
772PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000773 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000774 E = PassManagers.end(); I != E; ++I)
775 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000776
Dan Gohman060d5ba2010-10-12 00:15:27 +0000777 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000778 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
779 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000780
781 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000782 DME = AnUsageMap.end(); DMI != DME; ++DMI)
783 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000784}
785
Devang Patelafb1f3622006-12-12 22:35:25 +0000786//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000787// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000788
Devang Patel643676c2006-11-11 01:10:19 +0000789/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000790void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000791 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000792
Chris Lattner60987362009-03-06 05:53:14 +0000793 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000794
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000795 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000796
Dan Gohmande6188a2010-08-12 23:50:08 +0000797 // This pass is the current implementation of all of the interfaces it
798 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000799 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
800 if (PInf == 0) return;
801 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000802 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000803 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000804}
805
Devang Patel9d9fc902007-03-06 17:52:53 +0000806// Return true if P preserves high level analysis used by other
807// passes managed by this manager
808bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000809 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000810 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000811 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000812
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000813 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000814 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000815 E = HigherLevelAnalysis.end(); I != E; ++I) {
816 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000817 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000818 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000819 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000820 PreservedSet.end())
821 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000822 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000823
Devang Patel9d9fc902007-03-06 17:52:53 +0000824 return true;
825}
826
Chris Lattner02eb94c2008-08-07 07:34:50 +0000827/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000828void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000829 // Don't do this unless assertions are enabled.
830#ifdef NDEBUG
831 return;
832#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000833 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
834 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000835
Devang Patelef432532007-07-19 05:36:09 +0000836 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000837 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000838 E = PreservedSet.end(); I != E; ++I) {
839 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000840 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000841 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000842 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000843 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000844 }
845}
846
Devang Patel67c79a42008-07-01 19:50:56 +0000847/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000848void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000849 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
850 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000851 return;
852
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000853 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000854 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000855 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000856 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000857 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000858 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000859 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000860 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000861 if (PassDebugging >= Details) {
862 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000863 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
864 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000865 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000866 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000867 }
Devang Patel349170f2006-11-11 01:24:55 +0000868 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000869
Devang Patel42dd1e92007-03-06 01:55:46 +0000870 // Check inherited analysis also. If P is not preserving analysis
871 // provided by parent manager then remove it here.
872 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
873
874 if (!InheritedAnalysis[Index])
875 continue;
876
Dan Gohmande6188a2010-08-12 23:50:08 +0000877 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000878 I = InheritedAnalysis[Index]->begin(),
879 E = InheritedAnalysis[Index]->end(); I != E; ) {
880 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000881 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000882 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000883 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000884 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000885 if (PassDebugging >= Details) {
886 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000887 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
888 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000889 }
Devang Patel01919d22007-03-08 19:05:01 +0000890 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000891 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000892 }
893 }
Devang Patelf68a3492006-11-07 22:35:17 +0000894}
895
Devang Patelca189262006-11-14 03:05:08 +0000896/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000897void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000898 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000899
Devang Patel8adae862007-07-20 18:04:54 +0000900 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000901
Devang Patel2ff44922007-04-16 20:39:59 +0000902 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000903 if (!TPM)
904 return;
905
Devang Patel17ad0962006-12-08 00:37:52 +0000906 TPM->collectLastUses(DeadPasses, P);
907
Devang Patel656a9172008-06-06 17:50:36 +0000908 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000909 dbgs() << " -*- '" << P->getPassName();
910 dbgs() << "' is the last user of following pass instances.";
911 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000912 }
913
Dan Gohman060d5ba2010-10-12 00:15:27 +0000914 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000915 E = DeadPasses.end(); I != E; ++I)
916 freePass(*I, Msg, DBG_STR);
917}
Devang Patel200d3052006-12-13 23:50:44 +0000918
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000919void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000920 enum PassDebuggingString DBG_STR) {
921 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000922
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000923 {
924 // If the pass crashes releasing memory, remember this.
925 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000926 TimeRegion PassTimer(getPassTimer(P));
927
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000928 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000929 }
930
Owen Andersona7aed182010-08-06 18:33:48 +0000931 AnalysisID PI = P->getPassID();
932 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000933 // Remove the pass itself (if it is not already removed).
934 AvailableAnalysis.erase(PI);
935
936 // Remove all interfaces this pass implements, for which it is also
937 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000938 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000939 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000940 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000941 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000942 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000943 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000944 }
Devang Patel17ad0962006-12-08 00:37:52 +0000945 }
Devang Patelca189262006-11-14 03:05:08 +0000946}
947
Dan Gohmande6188a2010-08-12 23:50:08 +0000948/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000949/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000950void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000951 // This manager is going to manage pass P. Set up analysis resolver
952 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000953 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000954 P->setResolver(AR);
955
Devang Patelec2b9a72007-03-05 22:57:49 +0000956 // If a FunctionPass F is the last user of ModulePass info M
957 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000958 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000959
Chris Lattner60987362009-03-06 05:53:14 +0000960 if (!ProcessAnalysis) {
961 // Add pass
962 PassVector.push_back(P);
963 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000964 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000965
Chris Lattner60987362009-03-06 05:53:14 +0000966 // At the moment, this pass is the last user of all required passes.
967 SmallVector<Pass *, 12> LastUses;
968 SmallVector<Pass *, 8> RequiredPasses;
969 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
970
971 unsigned PDepth = this->getDepth();
972
Dan Gohmande6188a2010-08-12 23:50:08 +0000973 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000974 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000975 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000976 E = RequiredPasses.end(); I != E; ++I) {
977 Pass *PRequired = *I;
978 unsigned RDepth = 0;
979
980 assert(PRequired->getResolver() && "Analysis Resolver is not set");
981 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
982 RDepth = DM.getDepth();
983
984 if (PDepth == RDepth)
985 LastUses.push_back(PRequired);
986 else if (PDepth > RDepth) {
987 // Let the parent claim responsibility of last use
988 TransferLastUses.push_back(PRequired);
989 // Keep track of higher level analysis used by this manager.
990 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000991 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000992 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000993 }
994
995 // Set P as P's last user until someone starts using P.
996 // However, if P is a Pass Manager then it does not need
997 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000998 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000999 LastUses.push_back(P);
1000 TPM->setLastUser(LastUses, P);
1001
1002 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001003 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001004 TPM->setLastUser(TransferLastUses, My_PM);
1005 TransferLastUses.clear();
1006 }
1007
Dan Gohman6304db32010-08-16 22:57:28 +00001008 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001009 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001010 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001011 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001012 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1013 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001014 this->addLowerLevelRequiredPass(P, AnalysisPass);
1015 }
1016
1017 // Take a note of analysis required and made available by this pass.
1018 // Remove the analysis not preserved by this pass
1019 removeNotPreservedAnalysis(P);
1020 recordAvailableAnalysis(P);
1021
Devang Patel8cad70d2006-11-11 01:51:02 +00001022 // Add pass
1023 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001024}
1025
Devang Patele64d3052007-04-16 20:12:57 +00001026
1027/// Populate RP with analysis pass that are required by
1028/// pass P and are available. Populate RP_NotAvail with analysis
1029/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001030void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1031 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001032 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001033 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1034 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001035 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001036 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001037 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001038 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001039 else
Chris Lattner60987362009-03-06 05:53:14 +00001040 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001041 }
Devang Patelf58183d2006-12-12 23:09:32 +00001042
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001043 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001044 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001045 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001046 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001047 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001048 else
Chris Lattner60987362009-03-06 05:53:14 +00001049 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001050 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001051}
1052
Devang Patel07f4f582006-11-14 21:49:36 +00001053// All Required analyses should be available to the pass as it runs! Here
1054// we fill in the AnalysisImpls member of the pass so that it can
1055// successfully use the getAnalysis() method to retrieve the
1056// implementations it needs.
1057//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001058void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001059 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1060
Chris Lattnercbd160f2008-08-08 05:33:04 +00001061 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001062 I = AnUsage->getRequiredSet().begin(),
1063 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001064 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001065 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001066 // This may be analysis pass that is initialized on the fly.
1067 // If that is not the case then it will raise an assert when it is used.
1068 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001069 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001070 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001071 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001072 }
1073}
1074
Devang Patel640c5bb2006-12-08 22:30:11 +00001075/// Find the pass that implements Analysis AID. If desired pass is not found
1076/// then return NULL.
1077Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1078
1079 // Check if AvailableAnalysis map has one entry.
1080 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1081
1082 if (I != AvailableAnalysis.end())
1083 return I->second;
1084
1085 // Search Parents through TopLevelManager
1086 if (SearchParent)
1087 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001088
Devang Patel9d759b82006-12-09 00:09:12 +00001089 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001090}
1091
Devang Patel991aeba2006-12-15 20:13:01 +00001092// Print list of passes that are last used by P.
1093void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1094
Devang Patel8adae862007-07-20 18:04:54 +00001095 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001096
1097 // If this is a on the fly manager then it does not have TPM.
1098 if (!TPM)
1099 return;
1100
Devang Patel991aeba2006-12-15 20:13:01 +00001101 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001102
Dan Gohman7224bce2010-10-12 00:11:18 +00001103 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001104 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001105 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001106 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001107 }
1108}
1109
1110void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001111 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001112 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001113 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001114 PMD->dumpPassArguments();
1115 else
Owen Andersona7aed182010-08-06 18:33:48 +00001116 if (const PassInfo *PI =
1117 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001118 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001119 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001120 }
1121}
1122
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001123void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1124 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001125 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001126 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001127 return;
David Greene994e1bb2010-01-05 01:30:02 +00001128 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001129 switch (S1) {
1130 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001131 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001132 break;
1133 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001134 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001135 break;
1136 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001137 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001138 break;
1139 default:
1140 break;
1141 }
1142 switch (S2) {
1143 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001144 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001145 break;
1146 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001147 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001148 break;
1149 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001150 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001151 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001152 case ON_REGION_MSG:
1153 dbgs() << "' on Region '" << Msg << "'...\n";
1154 break;
Devang Patel003a5592007-03-05 20:01:30 +00001155 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001156 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001157 break;
1158 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 default:
1162 break;
1163 }
Devang Patel991aeba2006-12-15 20:13:01 +00001164}
1165
Chris Lattner4c1e9542009-03-06 06:45:05 +00001166void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001167 if (PassDebugging < Details)
1168 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001169
Chris Lattner4c493d92008-08-08 15:14:09 +00001170 AnalysisUsage analysisUsage;
1171 P->getAnalysisUsage(analysisUsage);
1172 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1173}
1174
Chris Lattner4c1e9542009-03-06 06:45:05 +00001175void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001176 if (PassDebugging < Details)
1177 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001178
Chris Lattner4c493d92008-08-08 15:14:09 +00001179 AnalysisUsage analysisUsage;
1180 P->getAnalysisUsage(analysisUsage);
1181 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1182}
1183
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001184void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001185 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001186 assert(PassDebugging >= Details);
1187 if (Set.empty())
1188 return;
David Greene994e1bb2010-01-05 01:30:02 +00001189 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001190 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001191 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001192 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001193 if (!PInf) {
1194 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1195 // all drivers.
1196 dbgs() << " Uninitialized Pass";
1197 continue;
1198 }
Owen Andersona7aed182010-08-06 18:33:48 +00001199 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001200 }
David Greene994e1bb2010-01-05 01:30:02 +00001201 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001202}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001203
Devang Patel004937b2007-07-27 20:06:09 +00001204/// Add RequiredPass into list of lower level passes required by pass P.
1205/// RequiredPass is run on the fly by Pass Manager when P requests it
1206/// through getAnalysis interface.
1207/// This should be handled by specific pass manager.
1208void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1209 if (TPM) {
1210 TPM->dumpArguments();
1211 TPM->dumpPasses();
1212 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001213
Dan Gohmande6188a2010-08-12 23:50:08 +00001214 // Module Level pass may required Function Level analysis info
1215 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1216 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001217 // module level pass is requiring lower level analysis info managed by
1218 // lower level pass manager.
1219
1220 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001221 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001222 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001223#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001224 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1225 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001226#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001227 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001228}
1229
Owen Andersona7aed182010-08-06 18:33:48 +00001230Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001231 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001232}
1233
Devang Patele7599552007-01-12 18:52:44 +00001234// Destructor
1235PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001236 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001237 E = PassVector.end(); I != E; ++I)
1238 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001239}
1240
Devang Patel9bdf7d42006-12-08 23:28:54 +00001241//===----------------------------------------------------------------------===//
1242// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001243// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1244Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001245 return PM.findAnalysisPass(ID, dir);
1246}
1247
Dan Gohmande6188a2010-08-12 23:50:08 +00001248Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001249 Function &F) {
1250 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1251}
1252
Devang Patela1514cb2006-12-07 19:39:39 +00001253//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001254// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001255
Dan Gohmande6188a2010-08-12 23:50:08 +00001256/// Execute all of the passes scheduled for execution by invoking
1257/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001258/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001259bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001260 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001261 return false;
1262
Devang Patele9585592006-12-08 01:38:28 +00001263 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001264
Devang Patel6e5a1132006-11-07 21:31:57 +00001265 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001266 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1267 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001268 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001269
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001270 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001271 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001272
Devang Patelabfbe3b2006-12-16 00:56:26 +00001273 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001274
Chris Lattner4c1e9542009-03-06 06:45:05 +00001275 {
1276 // If the pass crashes, remember this.
1277 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001278 TimeRegion PassTimer(getPassTimer(BP));
1279
Dan Gohman74b189f2010-03-01 17:34:28 +00001280 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001281 }
Devang Patel93a197c2006-12-14 00:08:04 +00001282
Dan Gohman74b189f2010-03-01 17:34:28 +00001283 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001284 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001285 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001286 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001287 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001288
Devang Patela273d1c2007-07-19 18:02:32 +00001289 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001290 removeNotPreservedAnalysis(BP);
1291 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001292 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001293 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001294
Bill Wendling6ce6d262009-12-25 13:50:18 +00001295 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001296}
1297
Devang Patel475c4532006-12-08 00:59:05 +00001298// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001299bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001300 bool Changed = false;
1301
Chris Lattner4c1e9542009-03-06 06:45:05 +00001302 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1303 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001304
1305 return Changed;
1306}
1307
Duncan Sands51495602009-02-13 09:42:34 +00001308bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001309 bool Changed = false;
1310
Chris Lattner4c1e9542009-03-06 06:45:05 +00001311 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1312 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001313
1314 return Changed;
1315}
1316
Duncan Sands51495602009-02-13 09:42:34 +00001317bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001318 bool Changed = false;
1319
Devang Patelabfbe3b2006-12-16 00:56:26 +00001320 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1321 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001322 Changed |= BP->doInitialization(F);
1323 }
1324
1325 return Changed;
1326}
1327
Duncan Sands51495602009-02-13 09:42:34 +00001328bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001329 bool Changed = false;
1330
Devang Patelabfbe3b2006-12-16 00:56:26 +00001331 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1332 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001333 Changed |= BP->doFinalization(F);
1334 }
1335
1336 return Changed;
1337}
1338
1339
Devang Patela1514cb2006-12-07 19:39:39 +00001340//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001341// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001342
Devang Patel4e12f862006-11-08 10:44:40 +00001343/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001344FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001345 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001346 // FPM is the top level manager.
1347 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001348
Dan Gohman565df952008-03-13 02:08:36 +00001349 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001350 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001351}
1352
Devang Patelb67904d2006-12-13 02:36:01 +00001353FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001354 delete FPM;
1355}
1356
Devang Patel4e12f862006-11-08 10:44:40 +00001357/// add - Add a pass to the queue of passes to run. This passes
1358/// ownership of the Pass to the PassManager. When the
1359/// PassManager_X is destroyed, the pass will be destroyed as well, so
1360/// there is no need to delete the pass. (TODO delete passes.)
1361/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001362void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001363 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001364}
1365
Devang Patel9f3083e2006-11-15 19:39:54 +00001366/// run - Execute all of the passes scheduled for execution. Keep
1367/// track of whether any of the passes modifies the function, and if
1368/// so, return true.
1369///
Devang Patelb67904d2006-12-13 02:36:01 +00001370bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001371 if (F.isMaterializable()) {
1372 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001373 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001374 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001375 }
Devang Patel272908d2006-12-08 22:57:48 +00001376 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001377}
1378
1379
Devang Patelff631ae2006-11-15 01:27:05 +00001380/// doInitialization - Run all of the initializers for the function passes.
1381///
Devang Patelb67904d2006-12-13 02:36:01 +00001382bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001383 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001384}
1385
Dan Gohmane6656eb2007-07-30 14:51:13 +00001386/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001387///
Devang Patelb67904d2006-12-13 02:36:01 +00001388bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001389 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001390}
1391
Devang Patela1514cb2006-12-07 19:39:39 +00001392//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001393// FunctionPassManagerImpl implementation
1394//
Duncan Sands51495602009-02-13 09:42:34 +00001395bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396 bool Changed = false;
1397
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001398 dumpArguments();
1399 dumpPasses();
1400
Chris Lattner4c1e9542009-03-06 06:45:05 +00001401 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1402 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001403
1404 return Changed;
1405}
1406
Duncan Sands51495602009-02-13 09:42:34 +00001407bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001408 bool Changed = false;
1409
Chris Lattner4c1e9542009-03-06 06:45:05 +00001410 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1411 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001412
1413 return Changed;
1414}
1415
Devang Patelec9c58f2009-04-01 22:34:41 +00001416/// cleanup - After running all passes, clean up pass manager cache.
1417void FPPassManager::cleanup() {
1418 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1419 FunctionPass *FP = getContainedPass(Index);
1420 AnalysisResolver *AR = FP->getResolver();
1421 assert(AR && "Analysis Resolver is not set");
1422 AR->clearAnalysisImpls();
1423 }
1424}
1425
Torok Edwin24c78352009-06-29 18:49:09 +00001426void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1427 if (!wasRun)
1428 return;
1429 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1430 FPPassManager *FPPM = getContainedManager(Index);
1431 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1432 FPPM->getContainedPass(Index)->releaseMemory();
1433 }
1434 }
Torok Edwin896556e2009-06-29 21:05:10 +00001435 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001436}
1437
Devang Patel67d6a5e2006-12-19 19:46:59 +00001438// Execute all the passes managed by this top level manager.
1439// Return true if any function is modified by a pass.
1440bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001441 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001443 createDebugInfoProbe();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001444
Devang Patele3068402006-12-21 00:16:50 +00001445 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001446 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1447 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001448
1449 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1450 getContainedManager(Index)->cleanup();
1451
Torok Edwin24c78352009-06-29 18:49:09 +00001452 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001453 return Changed;
1454}
1455
1456//===----------------------------------------------------------------------===//
1457// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001458
Devang Patel8c78a0b2007-05-03 01:11:54 +00001459char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001460/// Print passes managed by this manager
1461void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001462 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001463 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1464 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001465 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001466 dumpLastUses(FP, Offset+1);
1467 }
1468}
1469
1470
Dan Gohmande6188a2010-08-12 23:50:08 +00001471/// Execute all of the passes scheduled for execution by invoking
1472/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001473/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001474bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001475 if (F.isDeclaration())
1476 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001477
1478 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001479
Devang Patelcbbf2912008-03-20 01:09:53 +00001480 // Collect inherited analysis from Module level pass manager.
1481 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001482
Devang Patelabfbe3b2006-12-16 00:56:26 +00001483 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1484 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001485 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001486
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001487 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001488 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001489
Devang Patelabfbe3b2006-12-16 00:56:26 +00001490 initializeAnalysisImpl(FP);
Devang Patelfa31d382011-03-10 00:21:25 +00001491 if (TheDebugProbe)
1492 TheDebugProbe->initialize(FP, F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001493 {
1494 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001495 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001496
Dan Gohman74b189f2010-03-01 17:34:28 +00001497 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001498 }
Devang Patelfa31d382011-03-10 00:21:25 +00001499 if (TheDebugProbe)
1500 TheDebugProbe->finalize(FP, F);
Devang Patel93a197c2006-12-14 00:08:04 +00001501
Dan Gohman74b189f2010-03-01 17:34:28 +00001502 Changed |= LocalChanged;
1503 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001504 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001505 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001506
Devang Patela273d1c2007-07-19 18:02:32 +00001507 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001508 removeNotPreservedAnalysis(FP);
1509 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001510 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001511 }
1512 return Changed;
1513}
1514
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001516 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001517
Dan Gohmane7630be2010-05-11 20:30:00 +00001518 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001519 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001520
Bill Wendling6ce6d262009-12-25 13:50:18 +00001521 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001522}
1523
Duncan Sands51495602009-02-13 09:42:34 +00001524bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001525 bool Changed = false;
1526
Chris Lattner4c1e9542009-03-06 06:45:05 +00001527 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1528 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001529
1530 return Changed;
1531}
1532
Duncan Sands51495602009-02-13 09:42:34 +00001533bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001534 bool Changed = false;
1535
Chris Lattner4c1e9542009-03-06 06:45:05 +00001536 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1537 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001538
Devang Patelff631ae2006-11-15 01:27:05 +00001539 return Changed;
1540}
1541
Devang Patela1514cb2006-12-07 19:39:39 +00001542//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001543// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001544
Dan Gohmande6188a2010-08-12 23:50:08 +00001545/// Execute all of the passes scheduled for execution by invoking
1546/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001547/// the module, and if so, return true.
1548bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001549MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001550 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001551
Torok Edwin24c78352009-06-29 18:49:09 +00001552 // Initialize on-the-fly passes
1553 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1554 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1555 I != E; ++I) {
1556 FunctionPassManagerImpl *FPP = I->second;
1557 Changed |= FPP->doInitialization(M);
1558 }
1559
Devang Patelabfbe3b2006-12-16 00:56:26 +00001560 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1561 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001562 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001563
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001564 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001565 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001566
Devang Patelabfbe3b2006-12-16 00:56:26 +00001567 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001568
Chris Lattner4c1e9542009-03-06 06:45:05 +00001569 {
1570 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001571 TimeRegion PassTimer(getPassTimer(MP));
1572
Dan Gohman74b189f2010-03-01 17:34:28 +00001573 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001574 }
Devang Patel93a197c2006-12-14 00:08:04 +00001575
Dan Gohman74b189f2010-03-01 17:34:28 +00001576 Changed |= LocalChanged;
1577 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001578 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001579 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001580 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001581
Devang Patela273d1c2007-07-19 18:02:32 +00001582 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001583 removeNotPreservedAnalysis(MP);
1584 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001585 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001586 }
Torok Edwin24c78352009-06-29 18:49:09 +00001587
1588 // Finalize on-the-fly passes
1589 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1590 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1591 I != E; ++I) {
1592 FunctionPassManagerImpl *FPP = I->second;
1593 // We don't know when is the last time an on-the-fly pass is run,
1594 // so we need to releaseMemory / finalize here
1595 FPP->releaseMemoryOnTheFly();
1596 Changed |= FPP->doFinalization(M);
1597 }
Devang Patel05e1a972006-11-07 22:03:15 +00001598 return Changed;
1599}
1600
Devang Patele64d3052007-04-16 20:12:57 +00001601/// Add RequiredPass into list of lower level passes required by pass P.
1602/// RequiredPass is run on the fly by Pass Manager when P requests it
1603/// through getAnalysis interface.
1604void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001605 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1606 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001607 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001608 RequiredPass->getPotentialPassManagerType()) &&
1609 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001610
Devang Patel68f72b12007-04-26 17:50:19 +00001611 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001612 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001613 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001614 // FPP is the top level manager.
1615 FPP->setTopLevelManager(FPP);
1616
Devang Patel69e9f6d2007-04-16 20:27:05 +00001617 OnTheFlyManagers[P] = FPP;
1618 }
Devang Patel68f72b12007-04-26 17:50:19 +00001619 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001620
Devang Patel68f72b12007-04-26 17:50:19 +00001621 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001622 if (RequiredPass) {
1623 SmallVector<Pass *, 1> LU;
1624 LU.push_back(RequiredPass);
1625 FPP->setLastUser(LU, P);
1626 }
Devang Patele64d3052007-04-16 20:12:57 +00001627}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001628
Dan Gohmande6188a2010-08-12 23:50:08 +00001629/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001630/// required by module pass MP. Instantiate analysis pass, by using
1631/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001632Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001633 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001634 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001635
Torok Edwin24c78352009-06-29 18:49:09 +00001636 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001637 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001638 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001639}
1640
1641
Devang Patela1514cb2006-12-07 19:39:39 +00001642//===----------------------------------------------------------------------===//
1643// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001644//
Devang Patelc290c8a2006-11-07 22:23:34 +00001645/// run - Execute all of the passes scheduled for execution. Keep track of
1646/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001647bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001648 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001649 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001650 createDebugInfoProbe();
Devang Patelb8817b92006-12-14 00:59:42 +00001651
Devang Patelcfd70c42006-12-13 22:10:00 +00001652 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001653 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001654
Devang Patele3068402006-12-21 00:16:50 +00001655 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001656 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1657 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001658 return Changed;
1659}
Devang Patel376fefa2006-11-08 10:29:57 +00001660
Devang Patela1514cb2006-12-07 19:39:39 +00001661//===----------------------------------------------------------------------===//
1662// PassManager implementation
1663
Devang Patel376fefa2006-11-08 10:29:57 +00001664/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001665PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001666 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001667 // PM is the top level manager
1668 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001669}
1670
Devang Patelb67904d2006-12-13 02:36:01 +00001671PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001672 delete PM;
1673}
1674
Devang Patel376fefa2006-11-08 10:29:57 +00001675/// add - Add a pass to the queue of passes to run. This passes ownership of
1676/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1677/// will be destroyed as well, so there is no need to delete the pass. This
1678/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001679void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001680 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001681}
1682
1683/// run - Execute all of the passes scheduled for execution. Keep track of
1684/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001685bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001686 return PM->run(M);
1687}
1688
Devang Patelb8817b92006-12-14 00:59:42 +00001689//===----------------------------------------------------------------------===//
1690// TimingInfo Class - This class is used to calculate information about the
1691// amount of time each pass takes to execute. This only happens with
1692// -time-passes is enabled on the command line.
1693//
1694bool llvm::TimePassesIsEnabled = false;
1695static cl::opt<bool,true>
1696EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1697 cl::desc("Time each pass, printing elapsed time for each on exit"));
1698
1699// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1700// a non null value (if the -time-passes option is enabled) or it leaves it
1701// null. It may be called multiple times.
1702void TimingInfo::createTheTimeInfo() {
1703 if (!TimePassesIsEnabled || TheTimeInfo) return;
1704
1705 // Constructed the first time this is called, iff -time-passes is enabled.
1706 // This guarantees that the object will be constructed before static globals,
1707 // thus it will be destroyed before them.
1708 static ManagedStatic<TimingInfo> TTI;
1709 TheTimeInfo = &*TTI;
1710}
1711
Devang Patel1c3633e2007-01-29 23:10:37 +00001712/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001713Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001714 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001715 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001716 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001717}
1718
Devang Patel1c56a632007-01-08 19:29:38 +00001719//===----------------------------------------------------------------------===//
1720// PMStack implementation
1721//
Devang Patelad98d232007-01-11 22:15:30 +00001722
Devang Patel1c56a632007-01-08 19:29:38 +00001723// Pop Pass Manager from the stack and clear its analysis info.
1724void PMStack::pop() {
1725
1726 PMDataManager *Top = this->top();
1727 Top->initializeAnalysisInfo();
1728
1729 S.pop_back();
1730}
1731
1732// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001733void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001734 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001735 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001736
Chris Lattner60987362009-03-06 05:53:14 +00001737 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001738 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1739 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001740 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001741
Chris Lattner60987362009-03-06 05:53:14 +00001742 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001743 TPM->addIndirectPassManager(PM);
1744 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001745 PM->setDepth(this->top()->getDepth()+1);
1746 }
1747 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001748 assert((PM->getPassManagerType() == PMT_ModulePassManager
1749 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001750 && "pushing bad pass manager to PMStack");
1751 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001752 }
1753
Devang Patel15701b52007-01-11 00:19:00 +00001754 S.push_back(PM);
1755}
1756
1757// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001758void PMStack::dump() const {
1759 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001760 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001761 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001762
Devang Patel15701b52007-01-11 00:19:00 +00001763 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001764 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001765}
1766
Devang Patel1c56a632007-01-08 19:29:38 +00001767/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001768/// add self into that manager.
1769void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001770 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001771 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001772 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001773 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1774 if (TopPMType == PreferredType)
1775 break; // We found desired pass manager
1776 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001777 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001778 else
1779 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001780 }
Devang Patel18ff6362008-09-09 21:38:40 +00001781 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001782 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001783}
1784
Devang Patel3312f752007-01-16 21:43:18 +00001785/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001786/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001787void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001788 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001789
Andrew Trickcbc845f2012-02-01 07:16:20 +00001790 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001791 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001792 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1793 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001794 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001795 break;
Devang Patel3312f752007-01-16 21:43:18 +00001796 }
Devang Patel3312f752007-01-16 21:43:18 +00001797
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001798 // Create new Function Pass Manager if needed.
1799 FPPassManager *FPP;
1800 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1801 FPP = (FPPassManager *)PMS.top();
1802 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001803 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1804 PMDataManager *PMD = PMS.top();
1805
1806 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001807 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001808 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001809
1810 // [2] Set up new manager's top level manager
1811 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1812 TPM->addIndirectPassManager(FPP);
1813
1814 // [3] Assign manager to manage this new manager. This may create
1815 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001816 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001817
1818 // [4] Push new manager into PMS
1819 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001820 }
1821
Devang Patel3312f752007-01-16 21:43:18 +00001822 // Assign FPP as the manager of this pass.
1823 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001824}
1825
Devang Patel3312f752007-01-16 21:43:18 +00001826/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001827/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001828void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001829 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001830 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001831
Devang Patel15701b52007-01-11 00:19:00 +00001832 // Basic Pass Manager is a leaf pass manager. It does not handle
1833 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001834 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001835 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1836 BBP = (BBPassManager *)PMS.top();
1837 } else {
1838 // If leaf manager is not Basic Block Pass manager then create new
1839 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001840 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1841 PMDataManager *PMD = PMS.top();
1842
1843 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001844 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001845
1846 // [2] Set up new manager's top level manager
1847 // Basic Block Pass Manager does not live by itself
1848 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1849 TPM->addIndirectPassManager(BBP);
1850
Devang Patel15701b52007-01-11 00:19:00 +00001851 // [3] Assign manager to manage this new manager. This may create
1852 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001853 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001854
Devang Patel3312f752007-01-16 21:43:18 +00001855 // [4] Push new manager into PMS
1856 PMS.push(BBP);
1857 }
Devang Patel1c56a632007-01-08 19:29:38 +00001858
Devang Patel3312f752007-01-16 21:43:18 +00001859 // Assign BBP as the manager of this pass.
1860 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001861}
1862
Dan Gohmand3a20c92008-03-11 16:41:42 +00001863PassManagerBase::~PassManagerBase() {}