blob: 8bfef9855ca2706844973e8861ca37d65c1bdfed [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmande6188a2010-08-12 23:50:08 +000010// This file implements the LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Dan Gohmana19631f2010-08-07 01:18:18 +000016#include "llvm/PassManager.h"
David Greene9b063df2010-04-02 23:17:14 +000017#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000018#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000019#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000020#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000021#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000022#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000024#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000025#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Mutex.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000029#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000030#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000031using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000032
Devang Patele7599552007-01-12 18:52:44 +000033// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000034
Devang Patelf1567a52006-12-13 20:03:48 +000035namespace llvm {
36
37//===----------------------------------------------------------------------===//
38// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
43
Devang Patel03fb5872006-12-13 21:13:31 +000044// Different debug levels that can be enabled...
45enum PassDebugLevel {
46 None, Arguments, Structure, Executions, Details
47};
48
Devang Patelf1567a52006-12-13 20:03:48 +000049static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000050PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000051 cl::desc("Print PassManager debugging information"),
52 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000053 clEnumVal(None , "disable debug output"),
54 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
55 clEnumVal(Structure , "print pass structure before run()"),
56 clEnumVal(Executions, "print pass name before it is executed"),
57 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000058 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000059
60typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
61PassOptionList;
62
63// Print IR out before/after specified passes.
64static PassOptionList
65PrintBefore("print-before",
66 llvm::cl::desc("Print IR before specified passes"));
67
68static PassOptionList
69PrintAfter("print-after",
70 llvm::cl::desc("Print IR after specified passes"));
71
72static cl::opt<bool>
73PrintBeforeAll("print-before-all",
74 llvm::cl::desc("Print IR before each pass"),
75 cl::init(false));
76static cl::opt<bool>
77PrintAfterAll("print-after-all",
78 llvm::cl::desc("Print IR after each pass"),
79 cl::init(false));
80
81/// This is a helper to determine whether to print IR before or
82/// after a pass.
83
Owen Andersona7aed182010-08-06 18:33:48 +000084static bool ShouldPrintBeforeOrAfterPass(const void *PassID,
David Greene9b063df2010-04-02 23:17:14 +000085 PassOptionList &PassesToPrint) {
Owen Andersona7aed182010-08-06 18:33:48 +000086 if (const llvm::PassInfo *PI =
87 PassRegistry::getPassRegistry()->getPassInfo(PassID)) {
88 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
89 const llvm::PassInfo *PassInf = PassesToPrint[i];
90 if (PassInf)
91 if (PassInf->getPassArgument() == PI->getPassArgument()) {
92 return true;
93 }
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
100/// This is a utility to check whether a pass should have IR dumped
101/// before it.
Owen Andersona7aed182010-08-06 18:33:48 +0000102static bool ShouldPrintBeforePass(const void *PassID) {
103 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000104}
105
106/// This is a utility to check whether a pass should have IR dumped
107/// after it.
Owen Andersona7aed182010-08-06 18:33:48 +0000108static bool ShouldPrintAfterPass(const void *PassID) {
109 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000110}
111
Devang Patelf1567a52006-12-13 20:03:48 +0000112} // End of llvm namespace
113
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000114/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
115/// or higher is specified.
116bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
117 return PassDebugging >= Executions;
118}
119
120
121
122
Chris Lattner4c1e9542009-03-06 06:45:05 +0000123void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
124 if (V == 0 && M == 0)
125 OS << "Releasing pass '";
126 else
127 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000128
Chris Lattner4c1e9542009-03-06 06:45:05 +0000129 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000130
Chris Lattner4c1e9542009-03-06 06:45:05 +0000131 if (M) {
132 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
133 return;
134 }
135 if (V == 0) {
136 OS << '\n';
137 return;
138 }
139
Dan Gohman79fc0e92009-03-10 18:47:59 +0000140 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000141 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000142 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000143 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000144 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000145 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000146 OS << "value";
147
148 OS << " '";
149 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
150 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000151}
152
153
Devang Patelffca9102006-12-15 19:39:30 +0000154namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000155
Devang Patelf33f3eb2006-12-07 19:21:29 +0000156//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000157// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000158//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000159/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000160/// pass together and sequence them to process one basic block before
161/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000162class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000163
164public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000165 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000166 explicit BBPassManager(int Depth)
Owen Andersona7aed182010-08-06 18:33:48 +0000167 : PMDataManager(Depth), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000168
Devang Patelca58e352006-11-08 10:05:38 +0000169 /// Execute all of the passes scheduled for execution. Keep track of
170 /// whether any of the passes modifies the function, and if so, return true.
171 bool runOnFunction(Function &F);
172
Devang Patelf9d96b92006-12-07 19:57:52 +0000173 /// Pass Manager itself does not invalidate any analysis info.
174 void getAnalysisUsage(AnalysisUsage &Info) const {
175 Info.setPreservesAll();
176 }
177
Devang Patel475c4532006-12-08 00:59:05 +0000178 bool doInitialization(Module &M);
179 bool doInitialization(Function &F);
180 bool doFinalization(Module &M);
181 bool doFinalization(Function &F);
182
Chris Lattner2fa26e52010-01-22 05:24:46 +0000183 virtual PMDataManager *getAsPMDataManager() { return this; }
184 virtual Pass *getAsPass() { return this; }
185
Devang Patele3858e62007-02-01 22:08:25 +0000186 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000187 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000188 }
189
Devang Pateleda56172006-12-12 23:34:33 +0000190 // Print passes managed by this manager
191 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000192 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000193 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
194 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000195 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000196 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000197 }
198 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199
200 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000201 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000202 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
203 return BP;
204 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000205
Dan Gohmande6188a2010-08-12 23:50:08 +0000206 virtual PassManagerType getPassManagerType() const {
207 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000208 }
Devang Patelca58e352006-11-08 10:05:38 +0000209};
210
Devang Patel8c78a0b2007-05-03 01:11:54 +0000211char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000212}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000213
Devang Patele7599552007-01-12 18:52:44 +0000214namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000215
Devang Patel10c2ca62006-12-12 22:47:13 +0000216//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000217// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000218//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219/// FunctionPassManagerImpl manages FPPassManagers
220class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000221 public PMDataManager,
222 public PMTopLevelManager {
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;
Dan Gohmande6188a2010-08-12 23:50:08 +0000227 explicit FunctionPassManagerImpl(int Depth) :
228 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000229 PMTopLevelManager(new FPPassManager(1)), 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; }
263
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264 /// Pass Manager itself does not invalidate any analysis info.
265 void getAnalysisUsage(AnalysisUsage &Info) const {
266 Info.setPreservesAll();
267 }
268
Dan Gohman30614852010-08-16 21:57:30 +0000269 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000270 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000271 // P is a immutable pass and it will be managed by this
272 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000273 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274 P->setResolver(AR);
275 initializeAnalysisImpl(P);
276 addImmutablePass(IP);
277 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000278 } else {
David Greene103d4b42010-05-10 20:24:27 +0000279 P->assignPassManager(activeStack, PMT_FunctionPassManager);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280 }
Devang Patel0f080042007-01-12 17:23:48 +0000281
Devang Patel67d6a5e2006-12-19 19:46:59 +0000282 }
283
284 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000285 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000286 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
287 return FP;
288 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000289};
290
Devang Patel8c78a0b2007-05-03 01:11:54 +0000291char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000292
Devang Patel67d6a5e2006-12-19 19:46:59 +0000293//===----------------------------------------------------------------------===//
294// MPPassManager
295//
296/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000297/// It batches all Module passes and function pass managers together and
298/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000300public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000301 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000302 explicit MPPassManager(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000303 Pass(PT_PassManager, ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000304
305 // Delete on the fly managers.
306 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000307 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000308 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
309 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000310 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000311 delete FPP;
312 }
313 }
314
Dan Gohmande6188a2010-08-12 23:50:08 +0000315 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000316 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
317 return createPrintModulePass(&O, false, Banner);
318 }
319
Devang Patelca58e352006-11-08 10:05:38 +0000320 /// run - Execute all of the passes scheduled for execution. Keep track of
321 /// whether any of the passes modifies the module, and if so, return true.
322 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000323
Devang Patelf9d96b92006-12-07 19:57:52 +0000324 /// Pass Manager itself does not invalidate any analysis info.
325 void getAnalysisUsage(AnalysisUsage &Info) const {
326 Info.setPreservesAll();
327 }
328
Devang Patele64d3052007-04-16 20:12:57 +0000329 /// Add RequiredPass into list of lower level passes required by pass P.
330 /// RequiredPass is run on the fly by Pass Manager when P requests it
331 /// through getAnalysis interface.
332 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
333
Dan Gohmande6188a2010-08-12 23:50:08 +0000334 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000335 /// required by module pass MP. Instantiate analysis pass, by using
336 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000337 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000338
Devang Patele3858e62007-02-01 22:08:25 +0000339 virtual const char *getPassName() const {
340 return "Module Pass Manager";
341 }
342
Chris Lattner2fa26e52010-01-22 05:24:46 +0000343 virtual PMDataManager *getAsPMDataManager() { return this; }
344 virtual Pass *getAsPass() { return this; }
345
Devang Pateleda56172006-12-12 23:34:33 +0000346 // Print passes managed by this manager
347 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000348 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000349 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
350 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000351 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000352 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
353 OnTheFlyManagers.find(MP);
354 if (I != OnTheFlyManagers.end())
355 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000356 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000357 }
358 }
359
Devang Patelabfbe3b2006-12-16 00:56:26 +0000360 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000361 assert(N < PassVector.size() && "Pass number out of range!");
362 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000363 }
364
Dan Gohmande6188a2010-08-12 23:50:08 +0000365 virtual PassManagerType getPassManagerType() const {
366 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000367 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000368
369 private:
370 /// Collection of on the fly FPPassManagers. These managers manage
371 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000372 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000373};
374
Devang Patel8c78a0b2007-05-03 01:11:54 +0000375char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000376//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000377// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000378//
Devang Patel09f162c2007-05-01 21:15:47 +0000379
Devang Patel67d6a5e2006-12-19 19:46:59 +0000380/// PassManagerImpl manages MPPassManagers
381class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000382 public PMDataManager,
383 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000384
385public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000386 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000387 explicit PassManagerImpl(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000388 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000389 PMTopLevelManager(new MPPassManager(1)) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000390
Devang Patel376fefa2006-11-08 10:29:57 +0000391 /// add - Add a pass to the queue of passes to run. This passes ownership of
392 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
393 /// will be destroyed as well, so there is no need to delete the pass. This
394 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000395 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000396 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000397 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000398
399 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000400 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
401 return createPrintModulePass(&O, false, Banner);
402 }
403
Devang Patel376fefa2006-11-08 10:29:57 +0000404 /// run - Execute all of the passes scheduled for execution. Keep track of
405 /// whether any of the passes modifies the module, and if so, return true.
406 bool run(Module &M);
407
Devang Patelf9d96b92006-12-07 19:57:52 +0000408 /// Pass Manager itself does not invalidate any analysis info.
409 void getAnalysisUsage(AnalysisUsage &Info) const {
410 Info.setPreservesAll();
411 }
412
Dan Gohman30614852010-08-16 21:57:30 +0000413 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000414 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000415 // P is a immutable pass and it will be managed by this
416 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000417 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000418 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000419 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000420 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000421 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000422 } else {
David Greene103d4b42010-05-10 20:24:27 +0000423 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000424 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000425 }
426
Chris Lattner2fa26e52010-01-22 05:24:46 +0000427 virtual PMDataManager *getAsPMDataManager() { return this; }
428 virtual Pass *getAsPass() { return this; }
429
Devang Patel67d6a5e2006-12-19 19:46:59 +0000430 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000431 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000432 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
433 return MP;
434 }
Devang Patel376fefa2006-11-08 10:29:57 +0000435};
436
Devang Patel8c78a0b2007-05-03 01:11:54 +0000437char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000438} // End of llvm namespace
439
440namespace {
441
442//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000443/// TimingInfo Class - This class is used to calculate information about the
444/// amount of time each pass takes to execute. This only happens when
445/// -time-passes is enabled on the command line.
446///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000447
Owen Anderson5a6960f2009-06-18 20:51:00 +0000448static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000449
Nick Lewycky02d5f772009-10-25 06:33:48 +0000450class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000451 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000452 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000453public:
454 // Use 'create' member to get this.
455 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000456
Devang Patel1c3633e2007-01-29 23:10:37 +0000457 // TimingDtor - Print out information about timing information
458 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000459 // Delete all of the timers, which accumulate their info into the
460 // TimerGroup.
461 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
462 E = TimingData.end(); I != E; ++I)
463 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000464 // TimerGroup is deleted next, printing the report.
465 }
466
467 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
468 // to a non null value (if the -time-passes option is enabled) or it leaves it
469 // null. It may be called multiple times.
470 static void createTheTimeInfo();
471
Chris Lattner707431c2010-03-30 04:03:22 +0000472 /// getPassTimer - Return the timer for the specified pass if it exists.
473 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000474 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000475 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000476
Owen Anderson5c96ef72009-07-07 18:33:04 +0000477 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000478 Timer *&T = TimingData[P];
479 if (T == 0)
480 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000481 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000482 }
483};
484
Devang Patel1c3633e2007-01-29 23:10:37 +0000485} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000486
Dan Gohmand78c4002008-05-13 00:00:25 +0000487static TimingInfo *TheTimeInfo;
488
Devang Patela1514cb2006-12-07 19:39:39 +0000489//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000490// PMTopLevelManager implementation
491
Devang Patel4268fc02007-01-16 02:00:38 +0000492/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000493PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
494 PMDM->setTopLevelManager(this);
495 addPassManager(PMDM);
496 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000497}
498
Devang Patelafb1f3622006-12-12 22:35:25 +0000499/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000500void
501PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
502 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000503 unsigned PDepth = 0;
504 if (P->getResolver())
505 PDepth = P->getResolver()->getPMDataManager().getDepth();
506
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000507 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000508 E = AnalysisPasses.end(); I != E; ++I) {
509 Pass *AP = *I;
510 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000511
Devang Patel01919d22007-03-08 19:05:01 +0000512 if (P == AP)
513 continue;
514
Tobias Grosserf07426b2011-01-20 21:03:22 +0000515 // Update the last users of passes that are required transitive by AP.
516 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
517 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
518 SmallVector<Pass *, 12> LastUses;
519 SmallVector<Pass *, 12> LastPMUses;
520 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
521 E = IDs.end(); I != E; ++I) {
522 Pass *AnalysisPass = findAnalysisPass(*I);
523 assert(AnalysisPass && "Expected analysis pass to exist.");
524 AnalysisResolver *AR = AnalysisPass->getResolver();
525 assert(AR && "Expected analysis resolver to exist.");
526 unsigned APDepth = AR->getPMDataManager().getDepth();
527
528 if (PDepth == APDepth)
529 LastUses.push_back(AnalysisPass);
530 else if (PDepth > APDepth)
531 LastPMUses.push_back(AnalysisPass);
532 }
533
534 setLastUser(LastUses, P);
535
536 // If this pass has a corresponding pass manager, push higher level
537 // analysis to this pass manager.
538 if (P->getResolver())
539 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
540
541
Devang Patelafb1f3622006-12-12 22:35:25 +0000542 // If AP is the last user of other passes then make P last user of
543 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000544 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000545 LUE = LastUser.end(); LUI != LUE; ++LUI) {
546 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000547 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000548 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000549 LastUser[LUI->first] = P;
550 }
551 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000552}
553
554/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000555void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000556 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000557 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000558 InversedLastUser.find(P);
559 if (DMI == InversedLastUser.end())
560 return;
561
562 SmallPtrSet<Pass *, 8> &LU = DMI->second;
563 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
564 E = LU.end(); I != E; ++I) {
565 LastUses.push_back(*I);
566 }
567
Devang Patelafb1f3622006-12-12 22:35:25 +0000568}
569
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000570AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
571 AnalysisUsage *AnUsage = NULL;
572 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000573 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000574 AnUsage = DMI->second;
575 else {
576 AnUsage = new AnalysisUsage();
577 P->getAnalysisUsage(*AnUsage);
578 AnUsageMap[P] = AnUsage;
579 }
580 return AnUsage;
581}
582
Devang Patelafb1f3622006-12-12 22:35:25 +0000583/// Schedule pass P for execution. Make sure that passes required by
584/// P are run before P is run. Update analysis info maintained by
585/// the manager. Remove dead passes. This is a recursive function.
586void PMTopLevelManager::schedulePass(Pass *P) {
587
Devang Patel3312f752007-01-16 21:43:18 +0000588 // TODO : Allocate function manager for this pass, other wise required set
589 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000590
Devang Pateld74ede72007-03-06 01:06:16 +0000591 // Give pass a chance to prepare the stage.
592 P->preparePassManager(activeStack);
593
Devang Patel864970e2008-03-18 00:39:19 +0000594 // If P is an analysis pass and it is available then do not
595 // generate the analysis again. Stale analysis info should not be
596 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000597 const PassInfo *PI =
598 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
599 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000600 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000601 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000602 }
Devang Patel864970e2008-03-18 00:39:19 +0000603
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000604 AnalysisUsage *AnUsage = findAnalysisUsage(P);
605
Devang Patelfdee7032008-08-14 23:07:48 +0000606 bool checkAnalysis = true;
607 while (checkAnalysis) {
608 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000609
Devang Patelfdee7032008-08-14 23:07:48 +0000610 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
611 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
612 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000613
Devang Patelfdee7032008-08-14 23:07:48 +0000614 Pass *AnalysisPass = findAnalysisPass(*I);
615 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000616 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
617 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000618 if (P->getPotentialPassManagerType () ==
619 AnalysisPass->getPotentialPassManagerType())
620 // Schedule analysis pass that is managed by the same pass manager.
621 schedulePass(AnalysisPass);
622 else if (P->getPotentialPassManagerType () >
623 AnalysisPass->getPotentialPassManagerType()) {
624 // Schedule analysis pass that is managed by a new manager.
625 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000626 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000627 // are already checked are still available.
628 checkAnalysis = true;
629 }
630 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000631 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000632 // passes are run on the fly.
633 delete AnalysisPass;
634 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000635 }
636 }
637
638 // Now all required passes are available.
639 addTopLevelPass(P);
640}
641
642/// Find the pass that implements Analysis AID. Search immutable
643/// passes and all pass managers. If desired pass is not found
644/// then return NULL.
645Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
646
Devang Patelcd6ba152006-12-12 22:50:05 +0000647 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000648 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000649 E = PassManagers.end(); I != E; ++I)
650 if (Pass *P = (*I)->findAnalysisPass(AID, false))
651 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000652
653 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000654 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000655 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000656 E = IndirectPassManagers.end(); I != E; ++I)
657 if (Pass *P = (*I)->findAnalysisPass(AID, false))
658 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000659
Dan Gohman844dd0a2010-10-11 23:19:01 +0000660 // Check the immutable passes. Iterate in reverse order so that we find
661 // the most recently registered passes first.
662 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
663 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000664 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000665 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000666 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000667
668 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000669 const PassInfo *PassInf =
670 PassRegistry::getPassRegistry()->getPassInfo(PI);
671 const std::vector<const PassInfo*> &ImmPI =
672 PassInf->getInterfacesImplemented();
673 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
674 EE = ImmPI.end(); II != EE; ++II) {
675 if ((*II)->getTypeInfo() == AID)
676 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000677 }
678 }
679
Dan Gohman844dd0a2010-10-11 23:19:01 +0000680 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000681}
682
Devang Pateleda56172006-12-12 23:34:33 +0000683// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000684void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000685
Devang Patelfd4184322007-01-17 20:33:36 +0000686 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000687 return;
688
Devang Pateleda56172006-12-12 23:34:33 +0000689 // Print out the immutable passes
690 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000691 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000692 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000693
Dan Gohmanf71c5212010-08-19 01:29:07 +0000694 // Every class that derives from PMDataManager also derives from Pass
695 // (sometimes indirectly), but there's no inheritance relationship
696 // between PMDataManager and Pass, so we have to getAsPass to get
697 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000698 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000699 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000700 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000701}
702
Devang Patel991aeba2006-12-15 20:13:01 +0000703void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000704
Devang Patelfd4184322007-01-17 20:33:36 +0000705 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000706 return;
707
David Greene994e1bb2010-01-05 01:30:02 +0000708 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000709 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
710 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
711 if (const PassInfo *PI =
712 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
713 if (!PI->isAnalysisGroup())
714 dbgs() << " -" << PI->getPassArgument();
Devang Patel0d29ae02008-08-12 15:44:31 +0000715 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000716 E = PassManagers.end(); I != E; ++I)
717 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000718 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000719}
720
Devang Patele3068402006-12-21 00:16:50 +0000721void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000722 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000723 E = PassManagers.end(); I != E; ++I)
724 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000725
Devang Patele3068402006-12-21 00:16:50 +0000726 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000727 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000728 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
729 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000730 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000731
Chris Lattner60987362009-03-06 05:53:14 +0000732 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000733 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000734 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000735 InversedLastUser.find(DMI->second);
736 if (InvDMI != InversedLastUser.end()) {
737 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
738 L.insert(DMI->first);
739 } else {
740 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
741 InversedLastUser[DMI->second] = L;
742 }
743 }
Devang Patele3068402006-12-21 00:16:50 +0000744}
745
Devang Patele7599552007-01-12 18:52:44 +0000746/// Destructor
747PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000748 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000749 E = PassManagers.end(); I != E; ++I)
750 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000751
Dan Gohman060d5ba2010-10-12 00:15:27 +0000752 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000753 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
754 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000755
756 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000757 DME = AnUsageMap.end(); DMI != DME; ++DMI)
758 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000759}
760
Devang Patelafb1f3622006-12-12 22:35:25 +0000761//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000762// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000763
Devang Patel643676c2006-11-11 01:10:19 +0000764/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000765void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000766 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000767
Chris Lattner60987362009-03-06 05:53:14 +0000768 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000769
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000770 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000771
Dan Gohmande6188a2010-08-12 23:50:08 +0000772 // This pass is the current implementation of all of the interfaces it
773 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000774 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
775 if (PInf == 0) return;
776 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000777 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000778 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000779}
780
Devang Patel9d9fc902007-03-06 17:52:53 +0000781// Return true if P preserves high level analysis used by other
782// passes managed by this manager
783bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000784 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000785 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000786 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000787
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000788 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000789 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000790 E = HigherLevelAnalysis.end(); I != E; ++I) {
791 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000792 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000793 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000794 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000795 PreservedSet.end())
796 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000797 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000798
Devang Patel9d9fc902007-03-06 17:52:53 +0000799 return true;
800}
801
Chris Lattner02eb94c2008-08-07 07:34:50 +0000802/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000803void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000804 // Don't do this unless assertions are enabled.
805#ifdef NDEBUG
806 return;
807#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000808 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
809 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000810
Devang Patelef432532007-07-19 05:36:09 +0000811 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000812 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000813 E = PreservedSet.end(); I != E; ++I) {
814 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000815 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000816 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000817 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000818 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000819 }
820}
821
Devang Patel67c79a42008-07-01 19:50:56 +0000822/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000823void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000824 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
825 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000826 return;
827
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000828 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000829 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000830 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000831 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000832 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000833 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000834 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000835 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000836 if (PassDebugging >= Details) {
837 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000838 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
839 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000840 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000841 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000842 }
Devang Patel349170f2006-11-11 01:24:55 +0000843 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000844
Devang Patel42dd1e92007-03-06 01:55:46 +0000845 // Check inherited analysis also. If P is not preserving analysis
846 // provided by parent manager then remove it here.
847 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
848
849 if (!InheritedAnalysis[Index])
850 continue;
851
Dan Gohmande6188a2010-08-12 23:50:08 +0000852 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000853 I = InheritedAnalysis[Index]->begin(),
854 E = InheritedAnalysis[Index]->end(); I != E; ) {
855 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000856 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000857 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000858 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000859 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000860 if (PassDebugging >= Details) {
861 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000862 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
863 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000864 }
Devang Patel01919d22007-03-08 19:05:01 +0000865 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000866 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000867 }
868 }
Devang Patelf68a3492006-11-07 22:35:17 +0000869}
870
Devang Patelca189262006-11-14 03:05:08 +0000871/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000872void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000873 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000874
Devang Patel8adae862007-07-20 18:04:54 +0000875 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000876
Devang Patel2ff44922007-04-16 20:39:59 +0000877 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000878 if (!TPM)
879 return;
880
Devang Patel17ad0962006-12-08 00:37:52 +0000881 TPM->collectLastUses(DeadPasses, P);
882
Devang Patel656a9172008-06-06 17:50:36 +0000883 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000884 dbgs() << " -*- '" << P->getPassName();
885 dbgs() << "' is the last user of following pass instances.";
886 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000887 }
888
Dan Gohman060d5ba2010-10-12 00:15:27 +0000889 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000890 E = DeadPasses.end(); I != E; ++I)
891 freePass(*I, Msg, DBG_STR);
892}
Devang Patel200d3052006-12-13 23:50:44 +0000893
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000894void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000895 enum PassDebuggingString DBG_STR) {
896 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000897
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000898 {
899 // If the pass crashes releasing memory, remember this.
900 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000901 TimeRegion PassTimer(getPassTimer(P));
902
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000903 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000904 }
905
Owen Andersona7aed182010-08-06 18:33:48 +0000906 AnalysisID PI = P->getPassID();
907 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000908 // Remove the pass itself (if it is not already removed).
909 AvailableAnalysis.erase(PI);
910
911 // Remove all interfaces this pass implements, for which it is also
912 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000913 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000914 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000915 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000916 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000917 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000918 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000919 }
Devang Patel17ad0962006-12-08 00:37:52 +0000920 }
Devang Patelca189262006-11-14 03:05:08 +0000921}
922
Dan Gohmande6188a2010-08-12 23:50:08 +0000923/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000924/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000925void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000926 // This manager is going to manage pass P. Set up analysis resolver
927 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000928 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000929 P->setResolver(AR);
930
Devang Patelec2b9a72007-03-05 22:57:49 +0000931 // If a FunctionPass F is the last user of ModulePass info M
932 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000933 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000934
Chris Lattner60987362009-03-06 05:53:14 +0000935 if (!ProcessAnalysis) {
936 // Add pass
937 PassVector.push_back(P);
938 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000939 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000940
Chris Lattner60987362009-03-06 05:53:14 +0000941 // At the moment, this pass is the last user of all required passes.
942 SmallVector<Pass *, 12> LastUses;
943 SmallVector<Pass *, 8> RequiredPasses;
944 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
945
946 unsigned PDepth = this->getDepth();
947
Dan Gohmande6188a2010-08-12 23:50:08 +0000948 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000949 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000950 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000951 E = RequiredPasses.end(); I != E; ++I) {
952 Pass *PRequired = *I;
953 unsigned RDepth = 0;
954
955 assert(PRequired->getResolver() && "Analysis Resolver is not set");
956 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
957 RDepth = DM.getDepth();
958
959 if (PDepth == RDepth)
960 LastUses.push_back(PRequired);
961 else if (PDepth > RDepth) {
962 // Let the parent claim responsibility of last use
963 TransferLastUses.push_back(PRequired);
964 // Keep track of higher level analysis used by this manager.
965 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000966 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000967 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000968 }
969
970 // Set P as P's last user until someone starts using P.
971 // However, if P is a Pass Manager then it does not need
972 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000973 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000974 LastUses.push_back(P);
975 TPM->setLastUser(LastUses, P);
976
977 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000978 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000979 TPM->setLastUser(TransferLastUses, My_PM);
980 TransferLastUses.clear();
981 }
982
Dan Gohman6304db32010-08-16 22:57:28 +0000983 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +0000984 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000985 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000986 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000987 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
988 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +0000989 this->addLowerLevelRequiredPass(P, AnalysisPass);
990 }
991
992 // Take a note of analysis required and made available by this pass.
993 // Remove the analysis not preserved by this pass
994 removeNotPreservedAnalysis(P);
995 recordAvailableAnalysis(P);
996
Devang Patel8cad70d2006-11-11 01:51:02 +0000997 // Add pass
998 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000999}
1000
Devang Patele64d3052007-04-16 20:12:57 +00001001
1002/// Populate RP with analysis pass that are required by
1003/// pass P and are available. Populate RP_NotAvail with analysis
1004/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001005void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1006 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001007 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001008 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1009 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001010 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001011 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001012 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001013 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001014 else
Chris Lattner60987362009-03-06 05:53:14 +00001015 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001016 }
Devang Patelf58183d2006-12-12 23:09:32 +00001017
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001018 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001019 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001020 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001021 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001022 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001023 else
Chris Lattner60987362009-03-06 05:53:14 +00001024 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001025 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001026}
1027
Devang Patel07f4f582006-11-14 21:49:36 +00001028// All Required analyses should be available to the pass as it runs! Here
1029// we fill in the AnalysisImpls member of the pass so that it can
1030// successfully use the getAnalysis() method to retrieve the
1031// implementations it needs.
1032//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001033void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001034 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1035
Chris Lattnercbd160f2008-08-08 05:33:04 +00001036 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001037 I = AnUsage->getRequiredSet().begin(),
1038 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001039 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001040 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001041 // This may be analysis pass that is initialized on the fly.
1042 // If that is not the case then it will raise an assert when it is used.
1043 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001044 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001045 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001046 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001047 }
1048}
1049
Devang Patel640c5bb2006-12-08 22:30:11 +00001050/// Find the pass that implements Analysis AID. If desired pass is not found
1051/// then return NULL.
1052Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1053
1054 // Check if AvailableAnalysis map has one entry.
1055 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1056
1057 if (I != AvailableAnalysis.end())
1058 return I->second;
1059
1060 // Search Parents through TopLevelManager
1061 if (SearchParent)
1062 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001063
Devang Patel9d759b82006-12-09 00:09:12 +00001064 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001065}
1066
Devang Patel991aeba2006-12-15 20:13:01 +00001067// Print list of passes that are last used by P.
1068void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1069
Devang Patel8adae862007-07-20 18:04:54 +00001070 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001071
1072 // If this is a on the fly manager then it does not have TPM.
1073 if (!TPM)
1074 return;
1075
Devang Patel991aeba2006-12-15 20:13:01 +00001076 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001077
Dan Gohman7224bce2010-10-12 00:11:18 +00001078 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001079 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001080 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001081 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001082 }
1083}
1084
1085void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001086 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001087 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001088 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001089 PMD->dumpPassArguments();
1090 else
Owen Andersona7aed182010-08-06 18:33:48 +00001091 if (const PassInfo *PI =
1092 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001093 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001094 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001095 }
1096}
1097
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001098void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1099 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001100 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001101 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001102 return;
David Greene994e1bb2010-01-05 01:30:02 +00001103 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001104 switch (S1) {
1105 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001106 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001107 break;
1108 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001109 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001110 break;
1111 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001112 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001113 break;
1114 default:
1115 break;
1116 }
1117 switch (S2) {
1118 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001119 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001120 break;
1121 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001122 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001123 break;
1124 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001125 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001126 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001127 case ON_REGION_MSG:
1128 dbgs() << "' on Region '" << Msg << "'...\n";
1129 break;
Devang Patel003a5592007-03-05 20:01:30 +00001130 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001131 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001132 break;
1133 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001134 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001135 break;
1136 default:
1137 break;
1138 }
Devang Patel991aeba2006-12-15 20:13:01 +00001139}
1140
Chris Lattner4c1e9542009-03-06 06:45:05 +00001141void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001142 if (PassDebugging < Details)
1143 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001144
Chris Lattner4c493d92008-08-08 15:14:09 +00001145 AnalysisUsage analysisUsage;
1146 P->getAnalysisUsage(analysisUsage);
1147 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1148}
1149
Chris Lattner4c1e9542009-03-06 06:45:05 +00001150void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001151 if (PassDebugging < Details)
1152 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001153
Chris Lattner4c493d92008-08-08 15:14:09 +00001154 AnalysisUsage analysisUsage;
1155 P->getAnalysisUsage(analysisUsage);
1156 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1157}
1158
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001159void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001160 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001161 assert(PassDebugging >= Details);
1162 if (Set.empty())
1163 return;
David Greene994e1bb2010-01-05 01:30:02 +00001164 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001165 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001166 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001167 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
1168 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001169 }
David Greene994e1bb2010-01-05 01:30:02 +00001170 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001171}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001172
Devang Patel004937b2007-07-27 20:06:09 +00001173/// Add RequiredPass into list of lower level passes required by pass P.
1174/// RequiredPass is run on the fly by Pass Manager when P requests it
1175/// through getAnalysis interface.
1176/// This should be handled by specific pass manager.
1177void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1178 if (TPM) {
1179 TPM->dumpArguments();
1180 TPM->dumpPasses();
1181 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001182
Dan Gohmande6188a2010-08-12 23:50:08 +00001183 // Module Level pass may required Function Level analysis info
1184 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1185 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001186 // module level pass is requiring lower level analysis info managed by
1187 // lower level pass manager.
1188
1189 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001190 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001191 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001192#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001193 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1194 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001195#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001196 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001197}
1198
Owen Andersona7aed182010-08-06 18:33:48 +00001199Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001200 assert(0 && "Unable to find on the fly pass");
1201 return NULL;
1202}
1203
Devang Patele7599552007-01-12 18:52:44 +00001204// Destructor
1205PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001206 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001207 E = PassVector.end(); I != E; ++I)
1208 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001209}
1210
Devang Patel9bdf7d42006-12-08 23:28:54 +00001211//===----------------------------------------------------------------------===//
1212// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001213// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1214Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001215 return PM.findAnalysisPass(ID, dir);
1216}
1217
Dan Gohmande6188a2010-08-12 23:50:08 +00001218Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001219 Function &F) {
1220 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1221}
1222
Devang Patela1514cb2006-12-07 19:39:39 +00001223//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001224// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001225
Dan Gohmande6188a2010-08-12 23:50:08 +00001226/// Execute all of the passes scheduled for execution by invoking
1227/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001228/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001229bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001230 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001231 return false;
1232
Devang Patele9585592006-12-08 01:38:28 +00001233 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001234
Devang Patel6e5a1132006-11-07 21:31:57 +00001235 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001236 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1237 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001238 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001239
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001240 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001241 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001242
Devang Patelabfbe3b2006-12-16 00:56:26 +00001243 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001244
Chris Lattner4c1e9542009-03-06 06:45:05 +00001245 {
1246 // If the pass crashes, remember this.
1247 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001248 TimeRegion PassTimer(getPassTimer(BP));
1249
Dan Gohman74b189f2010-03-01 17:34:28 +00001250 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001251 }
Devang Patel93a197c2006-12-14 00:08:04 +00001252
Dan Gohman74b189f2010-03-01 17:34:28 +00001253 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001254 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001255 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001256 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001257 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001258
Devang Patela273d1c2007-07-19 18:02:32 +00001259 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001260 removeNotPreservedAnalysis(BP);
1261 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001262 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001263 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001264
Bill Wendling6ce6d262009-12-25 13:50:18 +00001265 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001266}
1267
Devang Patel475c4532006-12-08 00:59:05 +00001268// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001269bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001270 bool Changed = false;
1271
Chris Lattner4c1e9542009-03-06 06:45:05 +00001272 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1273 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001274
1275 return Changed;
1276}
1277
Duncan Sands51495602009-02-13 09:42:34 +00001278bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001279 bool Changed = false;
1280
Chris Lattner4c1e9542009-03-06 06:45:05 +00001281 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1282 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001283
1284 return Changed;
1285}
1286
Duncan Sands51495602009-02-13 09:42:34 +00001287bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001288 bool Changed = false;
1289
Devang Patelabfbe3b2006-12-16 00:56:26 +00001290 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1291 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001292 Changed |= BP->doInitialization(F);
1293 }
1294
1295 return Changed;
1296}
1297
Duncan Sands51495602009-02-13 09:42:34 +00001298bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001299 bool Changed = false;
1300
Devang Patelabfbe3b2006-12-16 00:56:26 +00001301 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1302 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001303 Changed |= BP->doFinalization(F);
1304 }
1305
1306 return Changed;
1307}
1308
1309
Devang Patela1514cb2006-12-07 19:39:39 +00001310//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001311// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001312
Devang Patel4e12f862006-11-08 10:44:40 +00001313/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001314FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001315 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001316 // FPM is the top level manager.
1317 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001318
Dan Gohman565df952008-03-13 02:08:36 +00001319 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001320 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001321}
1322
Devang Patelb67904d2006-12-13 02:36:01 +00001323FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001324 delete FPM;
1325}
1326
David Greene103d4b42010-05-10 20:24:27 +00001327/// addImpl - Add a pass to the queue of passes to run, without
1328/// checking whether to add a printer pass.
1329void FunctionPassManager::addImpl(Pass *P) {
1330 FPM->add(P);
1331}
1332
Devang Patel4e12f862006-11-08 10:44:40 +00001333/// add - Add a pass to the queue of passes to run. This passes
1334/// ownership of the Pass to the PassManager. When the
1335/// PassManager_X is destroyed, the pass will be destroyed as well, so
1336/// there is no need to delete the pass. (TODO delete passes.)
1337/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001338void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001339 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001340 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001341 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001342 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001343 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1344 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001345
David Greene103d4b42010-05-10 20:24:27 +00001346 addImpl(P);
1347
1348 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001349 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001350 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1351 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001352}
1353
Devang Patel9f3083e2006-11-15 19:39:54 +00001354/// run - Execute all of the passes scheduled for execution. Keep
1355/// track of whether any of the passes modifies the function, and if
1356/// so, return true.
1357///
Devang Patelb67904d2006-12-13 02:36:01 +00001358bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001359 if (F.isMaterializable()) {
1360 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001361 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001362 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001363 }
Devang Patel272908d2006-12-08 22:57:48 +00001364 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001365}
1366
1367
Devang Patelff631ae2006-11-15 01:27:05 +00001368/// doInitialization - Run all of the initializers for the function passes.
1369///
Devang Patelb67904d2006-12-13 02:36:01 +00001370bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001371 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001372}
1373
Dan Gohmane6656eb2007-07-30 14:51:13 +00001374/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001375///
Devang Patelb67904d2006-12-13 02:36:01 +00001376bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001377 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001378}
1379
Devang Patela1514cb2006-12-07 19:39:39 +00001380//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001381// FunctionPassManagerImpl implementation
1382//
Duncan Sands51495602009-02-13 09:42:34 +00001383bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001384 bool Changed = false;
1385
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001386 dumpArguments();
1387 dumpPasses();
1388
Chris Lattner4c1e9542009-03-06 06:45:05 +00001389 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1390 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001391
1392 return Changed;
1393}
1394
Duncan Sands51495602009-02-13 09:42:34 +00001395bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396 bool Changed = false;
1397
Chris Lattner4c1e9542009-03-06 06:45:05 +00001398 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1399 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001400
1401 return Changed;
1402}
1403
Devang Patelec9c58f2009-04-01 22:34:41 +00001404/// cleanup - After running all passes, clean up pass manager cache.
1405void FPPassManager::cleanup() {
1406 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1407 FunctionPass *FP = getContainedPass(Index);
1408 AnalysisResolver *AR = FP->getResolver();
1409 assert(AR && "Analysis Resolver is not set");
1410 AR->clearAnalysisImpls();
1411 }
1412}
1413
Torok Edwin24c78352009-06-29 18:49:09 +00001414void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1415 if (!wasRun)
1416 return;
1417 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1418 FPPassManager *FPPM = getContainedManager(Index);
1419 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1420 FPPM->getContainedPass(Index)->releaseMemory();
1421 }
1422 }
Torok Edwin896556e2009-06-29 21:05:10 +00001423 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001424}
1425
Devang Patel67d6a5e2006-12-19 19:46:59 +00001426// Execute all the passes managed by this top level manager.
1427// Return true if any function is modified by a pass.
1428bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001429 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001430 TimingInfo::createTheTimeInfo();
1431
Devang Patele3068402006-12-21 00:16:50 +00001432 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001433 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1434 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001435
1436 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1437 getContainedManager(Index)->cleanup();
1438
Torok Edwin24c78352009-06-29 18:49:09 +00001439 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001440 return Changed;
1441}
1442
1443//===----------------------------------------------------------------------===//
1444// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001445
Devang Patel8c78a0b2007-05-03 01:11:54 +00001446char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001447/// Print passes managed by this manager
1448void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001449 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001450 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1451 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001452 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001453 dumpLastUses(FP, Offset+1);
1454 }
1455}
1456
1457
Dan Gohmande6188a2010-08-12 23:50:08 +00001458/// Execute all of the passes scheduled for execution by invoking
1459/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001460/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001461bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001462 if (F.isDeclaration())
1463 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001464
1465 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001466
Devang Patelcbbf2912008-03-20 01:09:53 +00001467 // Collect inherited analysis from Module level pass manager.
1468 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001469
Devang Patelabfbe3b2006-12-16 00:56:26 +00001470 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1471 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001472 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001473
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001474 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001475 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001476
Devang Patelabfbe3b2006-12-16 00:56:26 +00001477 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001478
Chris Lattner4c1e9542009-03-06 06:45:05 +00001479 {
1480 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001481 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001482
Dan Gohman74b189f2010-03-01 17:34:28 +00001483 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001484 }
Devang Patel93a197c2006-12-14 00:08:04 +00001485
Dan Gohman74b189f2010-03-01 17:34:28 +00001486 Changed |= LocalChanged;
1487 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001488 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001489 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001490
Devang Patela273d1c2007-07-19 18:02:32 +00001491 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001492 removeNotPreservedAnalysis(FP);
1493 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001494 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001495 }
1496 return Changed;
1497}
1498
Devang Patel67d6a5e2006-12-19 19:46:59 +00001499bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001500 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001501
Dan Gohmane7630be2010-05-11 20:30:00 +00001502 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner60987362009-03-06 05:53:14 +00001503 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001504
Bill Wendling6ce6d262009-12-25 13:50:18 +00001505 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001506}
1507
Duncan Sands51495602009-02-13 09:42:34 +00001508bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001509 bool Changed = false;
1510
Chris Lattner4c1e9542009-03-06 06:45:05 +00001511 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1512 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001513
1514 return Changed;
1515}
1516
Duncan Sands51495602009-02-13 09:42:34 +00001517bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001518 bool Changed = false;
1519
Chris Lattner4c1e9542009-03-06 06:45:05 +00001520 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1521 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001522
Devang Patelff631ae2006-11-15 01:27:05 +00001523 return Changed;
1524}
1525
Devang Patela1514cb2006-12-07 19:39:39 +00001526//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001527// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001528
Dan Gohmande6188a2010-08-12 23:50:08 +00001529/// Execute all of the passes scheduled for execution by invoking
1530/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001531/// the module, and if so, return true.
1532bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001533MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001534 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001535
Torok Edwin24c78352009-06-29 18:49:09 +00001536 // Initialize on-the-fly passes
1537 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1538 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1539 I != E; ++I) {
1540 FunctionPassManagerImpl *FPP = I->second;
1541 Changed |= FPP->doInitialization(M);
1542 }
1543
Devang Patelabfbe3b2006-12-16 00:56:26 +00001544 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1545 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001546 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001547
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001548 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001549 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001550
Devang Patelabfbe3b2006-12-16 00:56:26 +00001551 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001552
Chris Lattner4c1e9542009-03-06 06:45:05 +00001553 {
1554 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001555 TimeRegion PassTimer(getPassTimer(MP));
1556
Dan Gohman74b189f2010-03-01 17:34:28 +00001557 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001558 }
Devang Patel93a197c2006-12-14 00:08:04 +00001559
Dan Gohman74b189f2010-03-01 17:34:28 +00001560 Changed |= LocalChanged;
1561 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001562 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001563 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001564 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001565
Devang Patela273d1c2007-07-19 18:02:32 +00001566 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001567 removeNotPreservedAnalysis(MP);
1568 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001569 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001570 }
Torok Edwin24c78352009-06-29 18:49:09 +00001571
1572 // Finalize on-the-fly passes
1573 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1574 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1575 I != E; ++I) {
1576 FunctionPassManagerImpl *FPP = I->second;
1577 // We don't know when is the last time an on-the-fly pass is run,
1578 // so we need to releaseMemory / finalize here
1579 FPP->releaseMemoryOnTheFly();
1580 Changed |= FPP->doFinalization(M);
1581 }
Devang Patel05e1a972006-11-07 22:03:15 +00001582 return Changed;
1583}
1584
Devang Patele64d3052007-04-16 20:12:57 +00001585/// Add RequiredPass into list of lower level passes required by pass P.
1586/// RequiredPass is run on the fly by Pass Manager when P requests it
1587/// through getAnalysis interface.
1588void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001589 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1590 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001591 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001592 RequiredPass->getPotentialPassManagerType()) &&
1593 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001594
Devang Patel68f72b12007-04-26 17:50:19 +00001595 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001596 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001597 FPP = new FunctionPassManagerImpl(0);
1598 // FPP is the top level manager.
1599 FPP->setTopLevelManager(FPP);
1600
Devang Patel69e9f6d2007-04-16 20:27:05 +00001601 OnTheFlyManagers[P] = FPP;
1602 }
Devang Patel68f72b12007-04-26 17:50:19 +00001603 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001604
Devang Patel68f72b12007-04-26 17:50:19 +00001605 // Register P as the last user of RequiredPass.
Dan Gohmanc450d2c2010-10-12 00:13:43 +00001606 SmallVector<Pass *, 1> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001607 LU.push_back(RequiredPass);
1608 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001609}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001610
Dan Gohmande6188a2010-08-12 23:50:08 +00001611/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001612/// required by module pass MP. Instantiate analysis pass, by using
1613/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001614Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001615 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001616 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001617
Torok Edwin24c78352009-06-29 18:49:09 +00001618 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001619 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001620 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001621}
1622
1623
Devang Patela1514cb2006-12-07 19:39:39 +00001624//===----------------------------------------------------------------------===//
1625// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001626//
Devang Patelc290c8a2006-11-07 22:23:34 +00001627/// run - Execute all of the passes scheduled for execution. Keep track of
1628/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001629bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001630 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001631 TimingInfo::createTheTimeInfo();
1632
Devang Patelcfd70c42006-12-13 22:10:00 +00001633 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001634 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001635
Devang Patele3068402006-12-21 00:16:50 +00001636 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001637 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1638 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001639 return Changed;
1640}
Devang Patel376fefa2006-11-08 10:29:57 +00001641
Devang Patela1514cb2006-12-07 19:39:39 +00001642//===----------------------------------------------------------------------===//
1643// PassManager implementation
1644
Devang Patel376fefa2006-11-08 10:29:57 +00001645/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001646PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001647 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001648 // PM is the top level manager
1649 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001650}
1651
Devang Patelb67904d2006-12-13 02:36:01 +00001652PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001653 delete PM;
1654}
1655
David Greene103d4b42010-05-10 20:24:27 +00001656/// addImpl - Add a pass to the queue of passes to run, without
1657/// checking whether to add a printer pass.
1658void PassManager::addImpl(Pass *P) {
1659 PM->add(P);
1660}
1661
Devang Patel376fefa2006-11-08 10:29:57 +00001662/// add - Add a pass to the queue of passes to run. This passes ownership of
1663/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1664/// will be destroyed as well, so there is no need to delete the pass. This
1665/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001666void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001667 const void* PassID = P->getPassID();
1668 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001669 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1670 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001671
David Greene103d4b42010-05-10 20:24:27 +00001672 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001673
Owen Andersona7aed182010-08-06 18:33:48 +00001674 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001675 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1676 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001677}
1678
1679/// run - Execute all of the passes scheduled for execution. Keep track of
1680/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001681bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001682 return PM->run(M);
1683}
1684
Devang Patelb8817b92006-12-14 00:59:42 +00001685//===----------------------------------------------------------------------===//
1686// TimingInfo Class - This class is used to calculate information about the
1687// amount of time each pass takes to execute. This only happens with
1688// -time-passes is enabled on the command line.
1689//
1690bool llvm::TimePassesIsEnabled = false;
1691static cl::opt<bool,true>
1692EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1693 cl::desc("Time each pass, printing elapsed time for each on exit"));
1694
1695// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1696// a non null value (if the -time-passes option is enabled) or it leaves it
1697// null. It may be called multiple times.
1698void TimingInfo::createTheTimeInfo() {
1699 if (!TimePassesIsEnabled || TheTimeInfo) return;
1700
1701 // Constructed the first time this is called, iff -time-passes is enabled.
1702 // This guarantees that the object will be constructed before static globals,
1703 // thus it will be destroyed before them.
1704 static ManagedStatic<TimingInfo> TTI;
1705 TheTimeInfo = &*TTI;
1706}
1707
Devang Patel1c3633e2007-01-29 23:10:37 +00001708/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001709Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001710 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001711 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001712 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001713}
1714
Devang Patel1c56a632007-01-08 19:29:38 +00001715//===----------------------------------------------------------------------===//
1716// PMStack implementation
1717//
Devang Patelad98d232007-01-11 22:15:30 +00001718
Devang Patel1c56a632007-01-08 19:29:38 +00001719// Pop Pass Manager from the stack and clear its analysis info.
1720void PMStack::pop() {
1721
1722 PMDataManager *Top = this->top();
1723 Top->initializeAnalysisInfo();
1724
1725 S.pop_back();
1726}
1727
1728// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001729void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001730 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001731
Chris Lattner60987362009-03-06 05:53:14 +00001732 if (!this->empty()) {
1733 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001734
Chris Lattner60987362009-03-06 05:53:14 +00001735 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001736 TPM->addIndirectPassManager(PM);
1737 PM->setTopLevelManager(TPM);
1738 }
1739
Devang Patel15701b52007-01-11 00:19:00 +00001740 S.push_back(PM);
1741}
1742
1743// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001744void PMStack::dump() const {
1745 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001746 E = S.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +00001747 printf("%s ", (*I)->getAsPass()->getPassName());
Chris Lattner60987362009-03-06 05:53:14 +00001748
Devang Patel15701b52007-01-11 00:19:00 +00001749 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001750 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001751}
1752
Devang Patel1c56a632007-01-08 19:29:38 +00001753/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001754/// add self into that manager.
1755void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001756 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001757 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001758 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001759 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1760 if (TopPMType == PreferredType)
1761 break; // We found desired pass manager
1762 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001763 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001764 else
1765 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001766 }
Devang Patel18ff6362008-09-09 21:38:40 +00001767 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001768 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001769}
1770
Devang Patel3312f752007-01-16 21:43:18 +00001771/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001772/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001773void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001774 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001775
Devang Patela3286902008-09-09 17:56:50 +00001776 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001777 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001778 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1779 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001780 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001781 break;
Devang Patel3312f752007-01-16 21:43:18 +00001782 }
Devang Patel3312f752007-01-16 21:43:18 +00001783
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001784 // Create new Function Pass Manager if needed.
1785 FPPassManager *FPP;
1786 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1787 FPP = (FPPassManager *)PMS.top();
1788 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001789 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1790 PMDataManager *PMD = PMS.top();
1791
1792 // [1] Create new Function Pass Manager
1793 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001794 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001795
1796 // [2] Set up new manager's top level manager
1797 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1798 TPM->addIndirectPassManager(FPP);
1799
1800 // [3] Assign manager to manage this new manager. This may create
1801 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001802 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001803
1804 // [4] Push new manager into PMS
1805 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001806 }
1807
Devang Patel3312f752007-01-16 21:43:18 +00001808 // Assign FPP as the manager of this pass.
1809 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001810}
1811
Devang Patel3312f752007-01-16 21:43:18 +00001812/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001813/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001814void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001815 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001816 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001817
Devang Patel15701b52007-01-11 00:19:00 +00001818 // Basic Pass Manager is a leaf pass manager. It does not handle
1819 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001820 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001821 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1822 BBP = (BBPassManager *)PMS.top();
1823 } else {
1824 // If leaf manager is not Basic Block Pass manager then create new
1825 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001826 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1827 PMDataManager *PMD = PMS.top();
1828
1829 // [1] Create new Basic Block Manager
1830 BBP = new BBPassManager(PMD->getDepth() + 1);
1831
1832 // [2] Set up new manager's top level manager
1833 // Basic Block Pass Manager does not live by itself
1834 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1835 TPM->addIndirectPassManager(BBP);
1836
Devang Patel15701b52007-01-11 00:19:00 +00001837 // [3] Assign manager to manage this new manager. This may create
1838 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001839 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001840
Devang Patel3312f752007-01-16 21:43:18 +00001841 // [4] Push new manager into PMS
1842 PMS.push(BBP);
1843 }
Devang Patel1c56a632007-01-08 19:29:38 +00001844
Devang Patel3312f752007-01-16 21:43:18 +00001845 // Assign BBP as the manager of this pass.
1846 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001847}
1848
Dan Gohmand3a20c92008-03-11 16:41:42 +00001849PassManagerBase::~PassManagerBase() {}