blob: 9377bf4a50384162827e9ef7a8135f471fe00fed [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"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000027#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000028#include "llvm/System/Threading.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000029#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000030#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000031#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000033
Devang Patele7599552007-01-12 18:52:44 +000034// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000035
Devang Patelf1567a52006-12-13 20:03:48 +000036namespace llvm {
37
38//===----------------------------------------------------------------------===//
39// Pass debugging information. Often it is useful to find out what pass is
40// running when a crash occurs in a utility. When this library is compiled with
41// debugging on, a command line option (--debug-pass) is enabled that causes the
42// pass name to be printed before it executes.
43//
44
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
47 None, Arguments, Structure, Executions, Details
48};
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000060
61typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
63
64// Print IR out before/after specified passes.
65static PassOptionList
66PrintBefore("print-before",
67 llvm::cl::desc("Print IR before specified passes"));
68
69static PassOptionList
70PrintAfter("print-after",
71 llvm::cl::desc("Print IR after specified passes"));
72
73static cl::opt<bool>
74PrintBeforeAll("print-before-all",
75 llvm::cl::desc("Print IR before each pass"),
76 cl::init(false));
77static cl::opt<bool>
78PrintAfterAll("print-after-all",
79 llvm::cl::desc("Print IR after each pass"),
80 cl::init(false));
81
82/// This is a helper to determine whether to print IR before or
83/// after a pass.
84
Owen Andersona7aed182010-08-06 18:33:48 +000085static bool ShouldPrintBeforeOrAfterPass(const void *PassID,
David Greene9b063df2010-04-02 23:17:14 +000086 PassOptionList &PassesToPrint) {
Owen Andersona7aed182010-08-06 18:33:48 +000087 if (const llvm::PassInfo *PI =
88 PassRegistry::getPassRegistry()->getPassInfo(PassID)) {
89 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
90 const llvm::PassInfo *PassInf = PassesToPrint[i];
91 if (PassInf)
92 if (PassInf->getPassArgument() == PI->getPassArgument()) {
93 return true;
94 }
95 }
David Greene9b063df2010-04-02 23:17:14 +000096 }
97 return false;
98}
Dan Gohmande6188a2010-08-12 23:50:08 +000099
David Greene9b063df2010-04-02 23:17:14 +0000100
101/// This is a utility to check whether a pass should have IR dumped
102/// before it.
Owen Andersona7aed182010-08-06 18:33:48 +0000103static bool ShouldPrintBeforePass(const void *PassID) {
104 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000105}
106
107/// This is a utility to check whether a pass should have IR dumped
108/// after it.
Owen Andersona7aed182010-08-06 18:33:48 +0000109static bool ShouldPrintAfterPass(const void *PassID) {
110 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000111}
112
Devang Patelf1567a52006-12-13 20:03:48 +0000113} // End of llvm namespace
114
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000115/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
116/// or higher is specified.
117bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
118 return PassDebugging >= Executions;
119}
120
121
122
123
Chris Lattner4c1e9542009-03-06 06:45:05 +0000124void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
125 if (V == 0 && M == 0)
126 OS << "Releasing pass '";
127 else
128 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000129
Chris Lattner4c1e9542009-03-06 06:45:05 +0000130 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000131
Chris Lattner4c1e9542009-03-06 06:45:05 +0000132 if (M) {
133 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
134 return;
135 }
136 if (V == 0) {
137 OS << '\n';
138 return;
139 }
140
Dan Gohman79fc0e92009-03-10 18:47:59 +0000141 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000144 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000145 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000146 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000147 OS << "value";
148
149 OS << " '";
150 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
151 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000152}
153
154
Devang Patelffca9102006-12-15 19:39:30 +0000155namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000156
Devang Patelf33f3eb2006-12-07 19:21:29 +0000157//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000159//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000160/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000161/// pass together and sequence them to process one basic block before
162/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000163class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000164
165public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000166 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000167 explicit BBPassManager(int Depth)
Owen Andersona7aed182010-08-06 18:33:48 +0000168 : PMDataManager(Depth), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000169
Devang Patelca58e352006-11-08 10:05:38 +0000170 /// Execute all of the passes scheduled for execution. Keep track of
171 /// whether any of the passes modifies the function, and if so, return true.
172 bool runOnFunction(Function &F);
173
Devang Patelf9d96b92006-12-07 19:57:52 +0000174 /// Pass Manager itself does not invalidate any analysis info.
175 void getAnalysisUsage(AnalysisUsage &Info) const {
176 Info.setPreservesAll();
177 }
178
Devang Patel475c4532006-12-08 00:59:05 +0000179 bool doInitialization(Module &M);
180 bool doInitialization(Function &F);
181 bool doFinalization(Module &M);
182 bool doFinalization(Function &F);
183
Chris Lattner2fa26e52010-01-22 05:24:46 +0000184 virtual PMDataManager *getAsPMDataManager() { return this; }
185 virtual Pass *getAsPass() { return this; }
186
Devang Patele3858e62007-02-01 22:08:25 +0000187 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000188 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000189 }
190
Devang Pateleda56172006-12-12 23:34:33 +0000191 // Print passes managed by this manager
192 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000193 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000194 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
195 BasicBlockPass *BP = getContainedPass(Index);
196 BP->dumpPassStructure(Offset + 1);
197 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000198 }
199 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000200
201 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000202 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000203 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
204 return BP;
205 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000206
Dan Gohmande6188a2010-08-12 23:50:08 +0000207 virtual PassManagerType getPassManagerType() const {
208 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000209 }
Devang Patelca58e352006-11-08 10:05:38 +0000210};
211
Devang Patel8c78a0b2007-05-03 01:11:54 +0000212char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000213}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000214
Devang Patele7599552007-01-12 18:52:44 +0000215namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000216
Devang Patel10c2ca62006-12-12 22:47:13 +0000217//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000219//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220/// FunctionPassManagerImpl manages FPPassManagers
221class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000222 public PMDataManager,
223 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000224private:
225 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000227 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000228 explicit FunctionPassManagerImpl(int Depth) :
229 Pass(PT_PassManager, ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000230 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000231
232 /// add - Add a pass to the queue of passes to run. This passes ownership of
233 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
234 /// will be destroyed as well, so there is no need to delete the pass. This
235 /// implies that all passes MUST be allocated with 'new'.
236 void add(Pass *P) {
237 schedulePass(P);
238 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000239
240 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000241 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
242 return createPrintFunctionPass(Banner, &O);
243 }
244
Torok Edwin24c78352009-06-29 18:49:09 +0000245 // Prepare for running an on the fly pass, freeing memory if needed
246 // from a previous run.
247 void releaseMemoryOnTheFly();
248
Devang Patel67d6a5e2006-12-19 19:46:59 +0000249 /// run - Execute all of the passes scheduled for execution. Keep track of
250 /// whether any of the passes modifies the module, and if so, return true.
251 bool run(Function &F);
252
253 /// doInitialization - Run all of the initializers for the function passes.
254 ///
255 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000256
Dan Gohmane6656eb2007-07-30 14:51:13 +0000257 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000258 ///
259 bool doFinalization(Module &M);
260
Dan Gohmande6188a2010-08-12 23:50:08 +0000261
Chris Lattner2fa26e52010-01-22 05:24:46 +0000262 virtual PMDataManager *getAsPMDataManager() { return this; }
263 virtual Pass *getAsPass() { return this; }
264
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265 /// Pass Manager itself does not invalidate any analysis info.
266 void getAnalysisUsage(AnalysisUsage &Info) const {
267 Info.setPreservesAll();
268 }
269
270 inline void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000271 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272 // P is a immutable pass and it will be managed by this
273 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000274 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000275 P->setResolver(AR);
276 initializeAnalysisImpl(P);
277 addImmutablePass(IP);
278 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000279 } else {
David Greene103d4b42010-05-10 20:24:27 +0000280 P->assignPassManager(activeStack, PMT_FunctionPassManager);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281 }
Devang Patel0f080042007-01-12 17:23:48 +0000282
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283 }
284
285 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000286 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000287 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
288 return FP;
289 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290};
291
Devang Patel8c78a0b2007-05-03 01:11:54 +0000292char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000293
Devang Patel67d6a5e2006-12-19 19:46:59 +0000294//===----------------------------------------------------------------------===//
295// MPPassManager
296//
297/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000298/// It batches all Module passes and function pass managers together and
299/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000300class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000301public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000302 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000303 explicit MPPassManager(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000304 Pass(PT_PassManager, ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000305
306 // Delete on the fly managers.
307 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000308 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000309 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
310 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000311 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000312 delete FPP;
313 }
314 }
315
Dan Gohmande6188a2010-08-12 23:50:08 +0000316 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000317 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
318 return createPrintModulePass(&O, false, Banner);
319 }
320
Devang Patelca58e352006-11-08 10:05:38 +0000321 /// run - Execute all of the passes scheduled for execution. Keep track of
322 /// whether any of the passes modifies the module, and if so, return true.
323 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000324
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 /// Pass Manager itself does not invalidate any analysis info.
326 void getAnalysisUsage(AnalysisUsage &Info) const {
327 Info.setPreservesAll();
328 }
329
Devang Patele64d3052007-04-16 20:12:57 +0000330 /// Add RequiredPass into list of lower level passes required by pass P.
331 /// RequiredPass is run on the fly by Pass Manager when P requests it
332 /// through getAnalysis interface.
333 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
334
Dan Gohmande6188a2010-08-12 23:50:08 +0000335 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000336 /// required by module pass MP. Instantiate analysis pass, by using
337 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000338 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000339
Devang Patele3858e62007-02-01 22:08:25 +0000340 virtual const char *getPassName() const {
341 return "Module Pass Manager";
342 }
343
Chris Lattner2fa26e52010-01-22 05:24:46 +0000344 virtual PMDataManager *getAsPMDataManager() { return this; }
345 virtual Pass *getAsPass() { return this; }
346
Devang Pateleda56172006-12-12 23:34:33 +0000347 // Print passes managed by this manager
348 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000349 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
351 ModulePass *MP = getContainedPass(Index);
352 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000353 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
354 OnTheFlyManagers.find(MP);
355 if (I != OnTheFlyManagers.end())
356 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000357 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000358 }
359 }
360
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000362 assert(N < PassVector.size() && "Pass number out of range!");
363 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000364 }
365
Dan Gohmande6188a2010-08-12 23:50:08 +0000366 virtual PassManagerType getPassManagerType() const {
367 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000368 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000369
370 private:
371 /// Collection of on the fly FPPassManagers. These managers manage
372 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000373 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000374};
375
Devang Patel8c78a0b2007-05-03 01:11:54 +0000376char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000377//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000378// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000379//
Devang Patel09f162c2007-05-01 21:15:47 +0000380
Devang Patel67d6a5e2006-12-19 19:46:59 +0000381/// PassManagerImpl manages MPPassManagers
382class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000383 public PMDataManager,
384 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000385
386public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000387 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000388 explicit PassManagerImpl(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000389 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmande6188a2010-08-12 23:50:08 +0000390 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000391
Devang Patel376fefa2006-11-08 10:29:57 +0000392 /// add - Add a pass to the queue of passes to run. This passes ownership of
393 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
394 /// will be destroyed as well, so there is no need to delete the pass. This
395 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000396 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000397 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000398 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000399
400 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000401 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
402 return createPrintModulePass(&O, false, Banner);
403 }
404
Devang Patel376fefa2006-11-08 10:29:57 +0000405 /// run - Execute all of the passes scheduled for execution. Keep track of
406 /// whether any of the passes modifies the module, and if so, return true.
407 bool run(Module &M);
408
Devang Patelf9d96b92006-12-07 19:57:52 +0000409 /// Pass Manager itself does not invalidate any analysis info.
410 void getAnalysisUsage(AnalysisUsage &Info) const {
411 Info.setPreservesAll();
412 }
413
Devang Patelabcd1d32006-12-07 21:27:23 +0000414 inline void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000415 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000416 // P is a immutable pass and it will be managed by this
417 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000418 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000419 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000420 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000421 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000422 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000423 } else {
David Greene103d4b42010-05-10 20:24:27 +0000424 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000425 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000426 }
427
Chris Lattner2fa26e52010-01-22 05:24:46 +0000428 virtual PMDataManager *getAsPMDataManager() { return this; }
429 virtual Pass *getAsPass() { return this; }
430
Devang Patel67d6a5e2006-12-19 19:46:59 +0000431 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000432 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000433 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
434 return MP;
435 }
Devang Patel376fefa2006-11-08 10:29:57 +0000436};
437
Devang Patel8c78a0b2007-05-03 01:11:54 +0000438char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000439} // End of llvm namespace
440
441namespace {
442
443//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000444/// TimingInfo Class - This class is used to calculate information about the
445/// amount of time each pass takes to execute. This only happens when
446/// -time-passes is enabled on the command line.
447///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000448
Owen Anderson5a6960f2009-06-18 20:51:00 +0000449static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000450
Nick Lewycky02d5f772009-10-25 06:33:48 +0000451class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000452 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000453 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000454public:
455 // Use 'create' member to get this.
456 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000457
Devang Patel1c3633e2007-01-29 23:10:37 +0000458 // TimingDtor - Print out information about timing information
459 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000460 // Delete all of the timers, which accumulate their info into the
461 // TimerGroup.
462 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
463 E = TimingData.end(); I != E; ++I)
464 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000465 // TimerGroup is deleted next, printing the report.
466 }
467
468 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
469 // to a non null value (if the -time-passes option is enabled) or it leaves it
470 // null. It may be called multiple times.
471 static void createTheTimeInfo();
472
Chris Lattner707431c2010-03-30 04:03:22 +0000473 /// getPassTimer - Return the timer for the specified pass if it exists.
474 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000475 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000476 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000477
Owen Anderson5c96ef72009-07-07 18:33:04 +0000478 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000479 Timer *&T = TimingData[P];
480 if (T == 0)
481 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000482 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000483 }
484};
485
Devang Patel1c3633e2007-01-29 23:10:37 +0000486} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000487
Dan Gohmand78c4002008-05-13 00:00:25 +0000488static TimingInfo *TheTimeInfo;
489
Devang Patela1514cb2006-12-07 19:39:39 +0000490//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000491// PMTopLevelManager implementation
492
Devang Patel4268fc02007-01-16 02:00:38 +0000493/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000494PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000495 if (t == TLM_Pass) {
496 MPPassManager *MPP = new MPPassManager(1);
497 MPP->setTopLevelManager(this);
498 addPassManager(MPP);
499 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000500 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000501 FPPassManager *FPP = new FPPassManager(1);
502 FPP->setTopLevelManager(this);
503 addPassManager(FPP);
504 activeStack.push(FPP);
Dan Gohmande6188a2010-08-12 23:50:08 +0000505 }
Devang Patel4268fc02007-01-16 02:00:38 +0000506}
507
Devang Patelafb1f3622006-12-12 22:35:25 +0000508/// Set pass P as the last user of the given analysis passes.
Dan Gohmande6188a2010-08-12 23:50:08 +0000509void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000510 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000511 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000512 E = AnalysisPasses.end(); I != E; ++I) {
513 Pass *AP = *I;
514 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000515
Devang Patel01919d22007-03-08 19:05:01 +0000516 if (P == AP)
517 continue;
518
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 // If AP is the last user of other passes then make P last user of
520 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000521 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000522 LUE = LastUser.end(); LUI != LUE; ++LUI) {
523 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000524 // DenseMap iterator is not invalidated here because
525 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000526 LastUser[LUI->first] = P;
527 }
528 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000529}
530
531/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000532void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000533 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000534 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000535 InversedLastUser.find(P);
536 if (DMI == InversedLastUser.end())
537 return;
538
539 SmallPtrSet<Pass *, 8> &LU = DMI->second;
540 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
541 E = LU.end(); I != E; ++I) {
542 LastUses.push_back(*I);
543 }
544
Devang Patelafb1f3622006-12-12 22:35:25 +0000545}
546
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000547AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
548 AnalysisUsage *AnUsage = NULL;
549 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000550 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000551 AnUsage = DMI->second;
552 else {
553 AnUsage = new AnalysisUsage();
554 P->getAnalysisUsage(*AnUsage);
555 AnUsageMap[P] = AnUsage;
556 }
557 return AnUsage;
558}
559
Devang Patelafb1f3622006-12-12 22:35:25 +0000560/// Schedule pass P for execution. Make sure that passes required by
561/// P are run before P is run. Update analysis info maintained by
562/// the manager. Remove dead passes. This is a recursive function.
563void PMTopLevelManager::schedulePass(Pass *P) {
564
Devang Patel3312f752007-01-16 21:43:18 +0000565 // TODO : Allocate function manager for this pass, other wise required set
566 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000567
Devang Pateld74ede72007-03-06 01:06:16 +0000568 // Give pass a chance to prepare the stage.
569 P->preparePassManager(activeStack);
570
Devang Patel864970e2008-03-18 00:39:19 +0000571 // If P is an analysis pass and it is available then do not
572 // generate the analysis again. Stale analysis info should not be
573 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000574 const PassInfo *PI =
575 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
576 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000577 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000578 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000579 }
Devang Patel864970e2008-03-18 00:39:19 +0000580
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000581 AnalysisUsage *AnUsage = findAnalysisUsage(P);
582
Devang Patelfdee7032008-08-14 23:07:48 +0000583 bool checkAnalysis = true;
584 while (checkAnalysis) {
585 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000586
Devang Patelfdee7032008-08-14 23:07:48 +0000587 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
588 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
589 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000590
Devang Patelfdee7032008-08-14 23:07:48 +0000591 Pass *AnalysisPass = findAnalysisPass(*I);
592 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000593 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
594 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000595 if (P->getPotentialPassManagerType () ==
596 AnalysisPass->getPotentialPassManagerType())
597 // Schedule analysis pass that is managed by the same pass manager.
598 schedulePass(AnalysisPass);
599 else if (P->getPotentialPassManagerType () >
600 AnalysisPass->getPotentialPassManagerType()) {
601 // Schedule analysis pass that is managed by a new manager.
602 schedulePass(AnalysisPass);
603 // Recheck analysis passes to ensure that required analysises that
604 // are already checked are still available.
605 checkAnalysis = true;
606 }
607 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000608 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000609 // passes are run on the fly.
610 delete AnalysisPass;
611 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000612 }
613 }
614
615 // Now all required passes are available.
616 addTopLevelPass(P);
617}
618
619/// Find the pass that implements Analysis AID. Search immutable
620/// passes and all pass managers. If desired pass is not found
621/// then return NULL.
622Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
623
624 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000625 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000626 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000627 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000628 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000629 P = PMD->findAnalysisPass(AID, false);
630 }
631
632 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000633 for (SmallVector<PMDataManager *, 8>::iterator
634 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000635 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
636 P = (*I)->findAnalysisPass(AID, false);
637
Devang Patel0d29ae02008-08-12 15:44:31 +0000638 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000639 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000640 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000641 if (PI == AID)
642 P = *I;
643
644 // If Pass not found then check the interfaces implemented by Immutable Pass
645 if (!P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000646 const PassInfo *PassInf =
647 PassRegistry::getPassRegistry()->getPassInfo(PI);
Owen Anderson3183ef12010-07-20 16:55:05 +0000648 const std::vector<const PassInfo*> &ImmPI =
Owen Andersona7aed182010-08-06 18:33:48 +0000649 PassInf->getInterfacesImplemented();
650 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
651 EE = ImmPI.end(); II != EE; ++II) {
652 if ((*II)->getTypeInfo() == AID)
653 P = *I;
654 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000655 }
656 }
657
Devang Patelafb1f3622006-12-12 22:35:25 +0000658 return P;
659}
660
Devang Pateleda56172006-12-12 23:34:33 +0000661// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000662void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000663
Devang Patelfd4184322007-01-17 20:33:36 +0000664 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000665 return;
666
Devang Pateleda56172006-12-12 23:34:33 +0000667 // Print out the immutable passes
668 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
669 ImmutablePasses[i]->dumpPassStructure(0);
670 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000671
Dan Gohman73caf5f2008-03-13 01:48:32 +0000672 // Every class that derives from PMDataManager also derives from Pass
673 // (sometimes indirectly), but there's no inheritance relationship
Chris Lattner2fa26e52010-01-22 05:24:46 +0000674 // between PMDataManager and Pass, so we have to getAsPass to get
Dan Gohman73caf5f2008-03-13 01:48:32 +0000675 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000676 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000677 E = PassManagers.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +0000678 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000679}
680
Devang Patel991aeba2006-12-15 20:13:01 +0000681void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000682
Devang Patelfd4184322007-01-17 20:33:36 +0000683 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000684 return;
685
David Greene994e1bb2010-01-05 01:30:02 +0000686 dbgs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000687 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000688 E = PassManagers.end(); I != E; ++I)
689 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000690 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000691}
692
Devang Patele3068402006-12-21 00:16:50 +0000693void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000694 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000695 E = PassManagers.end(); I != E; ++I)
696 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000697
Devang Patele3068402006-12-21 00:16:50 +0000698 // Initailize other pass managers
Dan Gohmande6188a2010-08-12 23:50:08 +0000699 for (SmallVector<PMDataManager *, 8>::iterator
700 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
701 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000702 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000703
Chris Lattner60987362009-03-06 05:53:14 +0000704 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000705 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000706 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000707 InversedLastUser.find(DMI->second);
708 if (InvDMI != InversedLastUser.end()) {
709 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
710 L.insert(DMI->first);
711 } else {
712 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
713 InversedLastUser[DMI->second] = L;
714 }
715 }
Devang Patele3068402006-12-21 00:16:50 +0000716}
717
Devang Patele7599552007-01-12 18:52:44 +0000718/// Destructor
719PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000720 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000721 E = PassManagers.end(); I != E; ++I)
722 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000723
Devang Patel0d29ae02008-08-12 15:44:31 +0000724 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000725 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
726 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000727
728 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000729 DME = AnUsageMap.end(); DMI != DME; ++DMI)
730 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000731}
732
Devang Patelafb1f3622006-12-12 22:35:25 +0000733//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000734// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000735
Devang Patel643676c2006-11-11 01:10:19 +0000736/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000737void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000738 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000739
Chris Lattner60987362009-03-06 05:53:14 +0000740 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000741
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000742 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000743
Dan Gohmande6188a2010-08-12 23:50:08 +0000744 // This pass is the current implementation of all of the interfaces it
745 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000746 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
747 if (PInf == 0) return;
748 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000749 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000750 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000751}
752
Devang Patel9d9fc902007-03-06 17:52:53 +0000753// Return true if P preserves high level analysis used by other
754// passes managed by this manager
755bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000756 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000757 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000758 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000759
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000760 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000761 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000762 E = HigherLevelAnalysis.end(); I != E; ++I) {
763 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000764 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000765 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000766 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000767 PreservedSet.end())
768 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000769 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000770
Devang Patel9d9fc902007-03-06 17:52:53 +0000771 return true;
772}
773
Chris Lattner02eb94c2008-08-07 07:34:50 +0000774/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000775void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000776 // Don't do this unless assertions are enabled.
777#ifdef NDEBUG
778 return;
779#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000780 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
781 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000782
Devang Patelef432532007-07-19 05:36:09 +0000783 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000784 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000785 E = PreservedSet.end(); I != E; ++I) {
786 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000787 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000788 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000789 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000790 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000791 }
792}
793
Devang Patel67c79a42008-07-01 19:50:56 +0000794/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000795void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000796 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
797 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000798 return;
799
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000800 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000801 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000802 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000803 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000804 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000805 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000806 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000807 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000808 if (PassDebugging >= Details) {
809 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000810 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
811 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000812 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000813 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000814 }
Devang Patel349170f2006-11-11 01:24:55 +0000815 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000816
Devang Patel42dd1e92007-03-06 01:55:46 +0000817 // Check inherited analysis also. If P is not preserving analysis
818 // provided by parent manager then remove it here.
819 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
820
821 if (!InheritedAnalysis[Index])
822 continue;
823
Dan Gohmande6188a2010-08-12 23:50:08 +0000824 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000825 I = InheritedAnalysis[Index]->begin(),
826 E = InheritedAnalysis[Index]->end(); I != E; ) {
827 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000828 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000829 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000830 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000831 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000832 if (PassDebugging >= Details) {
833 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000834 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
835 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000836 }
Devang Patel01919d22007-03-08 19:05:01 +0000837 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000838 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000839 }
840 }
Devang Patelf68a3492006-11-07 22:35:17 +0000841}
842
Devang Patelca189262006-11-14 03:05:08 +0000843/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000844void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000845 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000846
Devang Patel8adae862007-07-20 18:04:54 +0000847 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000848
Devang Patel2ff44922007-04-16 20:39:59 +0000849 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000850 if (!TPM)
851 return;
852
Devang Patel17ad0962006-12-08 00:37:52 +0000853 TPM->collectLastUses(DeadPasses, P);
854
Devang Patel656a9172008-06-06 17:50:36 +0000855 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000856 dbgs() << " -*- '" << P->getPassName();
857 dbgs() << "' is the last user of following pass instances.";
858 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000859 }
860
Devang Patel8adae862007-07-20 18:04:54 +0000861 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000862 E = DeadPasses.end(); I != E; ++I)
863 freePass(*I, Msg, DBG_STR);
864}
Devang Patel200d3052006-12-13 23:50:44 +0000865
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000866void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000867 enum PassDebuggingString DBG_STR) {
868 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000869
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000870 {
871 // If the pass crashes releasing memory, remember this.
872 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000873 TimeRegion PassTimer(getPassTimer(P));
874
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000875 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000876 }
877
Owen Andersona7aed182010-08-06 18:33:48 +0000878 AnalysisID PI = P->getPassID();
879 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000880 // Remove the pass itself (if it is not already removed).
881 AvailableAnalysis.erase(PI);
882
883 // Remove all interfaces this pass implements, for which it is also
884 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000885 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000886 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000887 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000888 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000889 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000890 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000891 }
Devang Patel17ad0962006-12-08 00:37:52 +0000892 }
Devang Patelca189262006-11-14 03:05:08 +0000893}
894
Dan Gohmande6188a2010-08-12 23:50:08 +0000895/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000896/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000897void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000898 // This manager is going to manage pass P. Set up analysis resolver
899 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000900 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000901 P->setResolver(AR);
902
Devang Patelec2b9a72007-03-05 22:57:49 +0000903 // If a FunctionPass F is the last user of ModulePass info M
904 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000905 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000906
Chris Lattner60987362009-03-06 05:53:14 +0000907 if (!ProcessAnalysis) {
908 // Add pass
909 PassVector.push_back(P);
910 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000911 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000912
Chris Lattner60987362009-03-06 05:53:14 +0000913 // At the moment, this pass is the last user of all required passes.
914 SmallVector<Pass *, 12> LastUses;
915 SmallVector<Pass *, 8> RequiredPasses;
916 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
917
918 unsigned PDepth = this->getDepth();
919
Dan Gohmande6188a2010-08-12 23:50:08 +0000920 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000921 ReqAnalysisNotAvailable, P);
922 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
923 E = RequiredPasses.end(); I != E; ++I) {
924 Pass *PRequired = *I;
925 unsigned RDepth = 0;
926
927 assert(PRequired->getResolver() && "Analysis Resolver is not set");
928 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
929 RDepth = DM.getDepth();
930
931 if (PDepth == RDepth)
932 LastUses.push_back(PRequired);
933 else if (PDepth > RDepth) {
934 // Let the parent claim responsibility of last use
935 TransferLastUses.push_back(PRequired);
936 // Keep track of higher level analysis used by this manager.
937 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000938 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000939 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000940 }
941
942 // Set P as P's last user until someone starts using P.
943 // However, if P is a Pass Manager then it does not need
944 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000945 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000946 LastUses.push_back(P);
947 TPM->setLastUser(LastUses, P);
948
949 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000950 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000951 TPM->setLastUser(TransferLastUses, My_PM);
952 TransferLastUses.clear();
953 }
954
955 // Now, take care of required analysises that are not available.
Dan Gohmande6188a2010-08-12 23:50:08 +0000956 for (SmallVector<AnalysisID, 8>::iterator
957 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000958 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000959 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
960 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +0000961 this->addLowerLevelRequiredPass(P, AnalysisPass);
962 }
963
964 // Take a note of analysis required and made available by this pass.
965 // Remove the analysis not preserved by this pass
966 removeNotPreservedAnalysis(P);
967 recordAvailableAnalysis(P);
968
Devang Patel8cad70d2006-11-11 01:51:02 +0000969 // Add pass
970 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000971}
972
Devang Patele64d3052007-04-16 20:12:57 +0000973
974/// Populate RP with analysis pass that are required by
975/// pass P and are available. Populate RP_NotAvail with analysis
976/// pass that are required by pass P but are not available.
977void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
978 SmallVector<AnalysisID, 8> &RP_NotAvail,
979 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000980 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
981 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +0000982 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000983 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000984 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +0000985 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +0000986 else
Chris Lattner60987362009-03-06 05:53:14 +0000987 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000988 }
Devang Patelf58183d2006-12-12 23:09:32 +0000989
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000990 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000991 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000992 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000993 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +0000994 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +0000995 else
Chris Lattner60987362009-03-06 05:53:14 +0000996 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000997 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000998}
999
Devang Patel07f4f582006-11-14 21:49:36 +00001000// All Required analyses should be available to the pass as it runs! Here
1001// we fill in the AnalysisImpls member of the pass so that it can
1002// successfully use the getAnalysis() method to retrieve the
1003// implementations it needs.
1004//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001005void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001006 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1007
Chris Lattnercbd160f2008-08-08 05:33:04 +00001008 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001009 I = AnUsage->getRequiredSet().begin(),
1010 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001011 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001012 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001013 // This may be analysis pass that is initialized on the fly.
1014 // If that is not the case then it will raise an assert when it is used.
1015 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001016 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001017 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001018 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001019 }
1020}
1021
Devang Patel640c5bb2006-12-08 22:30:11 +00001022/// Find the pass that implements Analysis AID. If desired pass is not found
1023/// then return NULL.
1024Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1025
1026 // Check if AvailableAnalysis map has one entry.
1027 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1028
1029 if (I != AvailableAnalysis.end())
1030 return I->second;
1031
1032 // Search Parents through TopLevelManager
1033 if (SearchParent)
1034 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001035
Devang Patel9d759b82006-12-09 00:09:12 +00001036 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001037}
1038
Devang Patel991aeba2006-12-15 20:13:01 +00001039// Print list of passes that are last used by P.
1040void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1041
Devang Patel8adae862007-07-20 18:04:54 +00001042 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001043
1044 // If this is a on the fly manager then it does not have TPM.
1045 if (!TPM)
1046 return;
1047
Devang Patel991aeba2006-12-15 20:13:01 +00001048 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001049
Devang Patel8adae862007-07-20 18:04:54 +00001050 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001051 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001052 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +00001053 (*I)->dumpPassStructure(0);
1054 }
1055}
1056
1057void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001058 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001059 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001060 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001061 PMD->dumpPassArguments();
1062 else
Owen Andersona7aed182010-08-06 18:33:48 +00001063 if (const PassInfo *PI =
1064 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001065 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001066 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001067 }
1068}
1069
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001070void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1071 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001072 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001073 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001074 return;
David Greene994e1bb2010-01-05 01:30:02 +00001075 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001076 switch (S1) {
1077 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001078 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001079 break;
1080 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001081 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001082 break;
1083 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001084 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001085 break;
1086 default:
1087 break;
1088 }
1089 switch (S2) {
1090 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001091 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001092 break;
1093 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001094 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001095 break;
1096 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001097 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001098 break;
1099 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001100 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001101 break;
1102 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001103 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001104 break;
1105 default:
1106 break;
1107 }
Devang Patel991aeba2006-12-15 20:13:01 +00001108}
1109
Chris Lattner4c1e9542009-03-06 06:45:05 +00001110void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001111 if (PassDebugging < Details)
1112 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001113
Chris Lattner4c493d92008-08-08 15:14:09 +00001114 AnalysisUsage analysisUsage;
1115 P->getAnalysisUsage(analysisUsage);
1116 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1117}
1118
Chris Lattner4c1e9542009-03-06 06:45:05 +00001119void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001120 if (PassDebugging < Details)
1121 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001122
Chris Lattner4c493d92008-08-08 15:14:09 +00001123 AnalysisUsage analysisUsage;
1124 P->getAnalysisUsage(analysisUsage);
1125 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1126}
1127
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001128void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001129 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001130 assert(PassDebugging >= Details);
1131 if (Set.empty())
1132 return;
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001134 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001135 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001136 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
1137 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001138 }
David Greene994e1bb2010-01-05 01:30:02 +00001139 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001140}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001141
Devang Patel004937b2007-07-27 20:06:09 +00001142/// Add RequiredPass into list of lower level passes required by pass P.
1143/// RequiredPass is run on the fly by Pass Manager when P requests it
1144/// through getAnalysis interface.
1145/// This should be handled by specific pass manager.
1146void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1147 if (TPM) {
1148 TPM->dumpArguments();
1149 TPM->dumpPasses();
1150 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001151
Dan Gohmande6188a2010-08-12 23:50:08 +00001152 // Module Level pass may required Function Level analysis info
1153 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1154 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001155 // module level pass is requiring lower level analysis info managed by
1156 // lower level pass manager.
1157
1158 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001159 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001160 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001161#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1163 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001164#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001165 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001166}
1167
Owen Andersona7aed182010-08-06 18:33:48 +00001168Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001169 assert(0 && "Unable to find on the fly pass");
1170 return NULL;
1171}
1172
Devang Patele7599552007-01-12 18:52:44 +00001173// Destructor
1174PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001175 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001176 E = PassVector.end(); I != E; ++I)
1177 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001178}
1179
Devang Patel9bdf7d42006-12-08 23:28:54 +00001180//===----------------------------------------------------------------------===//
1181// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001182// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1183Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001184 return PM.findAnalysisPass(ID, dir);
1185}
1186
Dan Gohmande6188a2010-08-12 23:50:08 +00001187Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001188 Function &F) {
1189 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1190}
1191
Devang Patela1514cb2006-12-07 19:39:39 +00001192//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001193// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001194
Dan Gohmande6188a2010-08-12 23:50:08 +00001195/// Execute all of the passes scheduled for execution by invoking
1196/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001197/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001198bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001199 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001200 return false;
1201
Devang Patele9585592006-12-08 01:38:28 +00001202 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001203
Devang Patel6e5a1132006-11-07 21:31:57 +00001204 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001205 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1206 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001207 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001208
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001209 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001210 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001211
Devang Patelabfbe3b2006-12-16 00:56:26 +00001212 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001213
Chris Lattner4c1e9542009-03-06 06:45:05 +00001214 {
1215 // If the pass crashes, remember this.
1216 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001217 TimeRegion PassTimer(getPassTimer(BP));
1218
Dan Gohman74b189f2010-03-01 17:34:28 +00001219 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001220 }
Devang Patel93a197c2006-12-14 00:08:04 +00001221
Dan Gohman74b189f2010-03-01 17:34:28 +00001222 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001223 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001224 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001225 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001226 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001227
Devang Patela273d1c2007-07-19 18:02:32 +00001228 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001229 removeNotPreservedAnalysis(BP);
1230 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001231 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001232 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001233
Bill Wendling6ce6d262009-12-25 13:50:18 +00001234 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001235}
1236
Devang Patel475c4532006-12-08 00:59:05 +00001237// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001238bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001239 bool Changed = false;
1240
Chris Lattner4c1e9542009-03-06 06:45:05 +00001241 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1242 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001243
1244 return Changed;
1245}
1246
Duncan Sands51495602009-02-13 09:42:34 +00001247bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001248 bool Changed = false;
1249
Chris Lattner4c1e9542009-03-06 06:45:05 +00001250 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1251 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001252
1253 return Changed;
1254}
1255
Duncan Sands51495602009-02-13 09:42:34 +00001256bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001257 bool Changed = false;
1258
Devang Patelabfbe3b2006-12-16 00:56:26 +00001259 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1260 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001261 Changed |= BP->doInitialization(F);
1262 }
1263
1264 return Changed;
1265}
1266
Duncan Sands51495602009-02-13 09:42:34 +00001267bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001268 bool Changed = false;
1269
Devang Patelabfbe3b2006-12-16 00:56:26 +00001270 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1271 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001272 Changed |= BP->doFinalization(F);
1273 }
1274
1275 return Changed;
1276}
1277
1278
Devang Patela1514cb2006-12-07 19:39:39 +00001279//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001280// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001281
Devang Patel4e12f862006-11-08 10:44:40 +00001282/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001283FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001284 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001285 // FPM is the top level manager.
1286 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001287
Dan Gohman565df952008-03-13 02:08:36 +00001288 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001289 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001290}
1291
Devang Patelb67904d2006-12-13 02:36:01 +00001292FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001293 delete FPM;
1294}
1295
David Greene103d4b42010-05-10 20:24:27 +00001296/// addImpl - Add a pass to the queue of passes to run, without
1297/// checking whether to add a printer pass.
1298void FunctionPassManager::addImpl(Pass *P) {
1299 FPM->add(P);
1300}
1301
Devang Patel4e12f862006-11-08 10:44:40 +00001302/// add - Add a pass to the queue of passes to run. This passes
1303/// ownership of the Pass to the PassManager. When the
1304/// PassManager_X is destroyed, the pass will be destroyed as well, so
1305/// there is no need to delete the pass. (TODO delete passes.)
1306/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001307void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001308 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001309 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001310 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001311 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001312 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1313 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001314
David Greene103d4b42010-05-10 20:24:27 +00001315 addImpl(P);
1316
1317 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001318 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001319 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1320 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001321}
1322
Devang Patel9f3083e2006-11-15 19:39:54 +00001323/// run - Execute all of the passes scheduled for execution. Keep
1324/// track of whether any of the passes modifies the function, and if
1325/// so, return true.
1326///
Devang Patelb67904d2006-12-13 02:36:01 +00001327bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001328 if (F.isMaterializable()) {
1329 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001330 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001331 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001332 }
Devang Patel272908d2006-12-08 22:57:48 +00001333 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001334}
1335
1336
Devang Patelff631ae2006-11-15 01:27:05 +00001337/// doInitialization - Run all of the initializers for the function passes.
1338///
Devang Patelb67904d2006-12-13 02:36:01 +00001339bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001340 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001341}
1342
Dan Gohmane6656eb2007-07-30 14:51:13 +00001343/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001344///
Devang Patelb67904d2006-12-13 02:36:01 +00001345bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001346 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001347}
1348
Devang Patela1514cb2006-12-07 19:39:39 +00001349//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001350// FunctionPassManagerImpl implementation
1351//
Duncan Sands51495602009-02-13 09:42:34 +00001352bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001353 bool Changed = false;
1354
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001355 dumpArguments();
1356 dumpPasses();
1357
Chris Lattner4c1e9542009-03-06 06:45:05 +00001358 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1359 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001360
1361 return Changed;
1362}
1363
Duncan Sands51495602009-02-13 09:42:34 +00001364bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001365 bool Changed = false;
1366
Chris Lattner4c1e9542009-03-06 06:45:05 +00001367 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1368 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001369
1370 return Changed;
1371}
1372
Devang Patelec9c58f2009-04-01 22:34:41 +00001373/// cleanup - After running all passes, clean up pass manager cache.
1374void FPPassManager::cleanup() {
1375 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1376 FunctionPass *FP = getContainedPass(Index);
1377 AnalysisResolver *AR = FP->getResolver();
1378 assert(AR && "Analysis Resolver is not set");
1379 AR->clearAnalysisImpls();
1380 }
1381}
1382
Torok Edwin24c78352009-06-29 18:49:09 +00001383void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1384 if (!wasRun)
1385 return;
1386 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1387 FPPassManager *FPPM = getContainedManager(Index);
1388 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1389 FPPM->getContainedPass(Index)->releaseMemory();
1390 }
1391 }
Torok Edwin896556e2009-06-29 21:05:10 +00001392 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001393}
1394
Devang Patel67d6a5e2006-12-19 19:46:59 +00001395// Execute all the passes managed by this top level manager.
1396// Return true if any function is modified by a pass.
1397bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001398 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001399 TimingInfo::createTheTimeInfo();
1400
Devang Patele3068402006-12-21 00:16:50 +00001401 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001402 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1403 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001404
1405 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1406 getContainedManager(Index)->cleanup();
1407
Torok Edwin24c78352009-06-29 18:49:09 +00001408 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001409 return Changed;
1410}
1411
1412//===----------------------------------------------------------------------===//
1413// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001414
Devang Patel8c78a0b2007-05-03 01:11:54 +00001415char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001416/// Print passes managed by this manager
1417void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001418 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001419 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1420 FunctionPass *FP = getContainedPass(Index);
1421 FP->dumpPassStructure(Offset + 1);
1422 dumpLastUses(FP, Offset+1);
1423 }
1424}
1425
1426
Dan Gohmande6188a2010-08-12 23:50:08 +00001427/// Execute all of the passes scheduled for execution by invoking
1428/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001429/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001430bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001431 if (F.isDeclaration())
1432 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001433
1434 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001435
Devang Patelcbbf2912008-03-20 01:09:53 +00001436 // Collect inherited analysis from Module level pass manager.
1437 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001438
Devang Patelabfbe3b2006-12-16 00:56:26 +00001439 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1440 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001441 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001442
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001443 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001444 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001445
Devang Patelabfbe3b2006-12-16 00:56:26 +00001446 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001447
Chris Lattner4c1e9542009-03-06 06:45:05 +00001448 {
1449 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001450 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001451
Dan Gohman74b189f2010-03-01 17:34:28 +00001452 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001453 }
Devang Patel93a197c2006-12-14 00:08:04 +00001454
Dan Gohman74b189f2010-03-01 17:34:28 +00001455 Changed |= LocalChanged;
1456 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001457 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001458 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001459
Devang Patela273d1c2007-07-19 18:02:32 +00001460 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001461 removeNotPreservedAnalysis(FP);
1462 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001463 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001464 }
1465 return Changed;
1466}
1467
Devang Patel67d6a5e2006-12-19 19:46:59 +00001468bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001469 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001470
Dan Gohmane7630be2010-05-11 20:30:00 +00001471 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner60987362009-03-06 05:53:14 +00001472 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001473
Bill Wendling6ce6d262009-12-25 13:50:18 +00001474 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001475}
1476
Duncan Sands51495602009-02-13 09:42:34 +00001477bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001478 bool Changed = false;
1479
Chris Lattner4c1e9542009-03-06 06:45:05 +00001480 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1481 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001482
1483 return Changed;
1484}
1485
Duncan Sands51495602009-02-13 09:42:34 +00001486bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001487 bool Changed = false;
1488
Chris Lattner4c1e9542009-03-06 06:45:05 +00001489 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1490 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001491
Devang Patelff631ae2006-11-15 01:27:05 +00001492 return Changed;
1493}
1494
Devang Patela1514cb2006-12-07 19:39:39 +00001495//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001496// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001497
Dan Gohmande6188a2010-08-12 23:50:08 +00001498/// Execute all of the passes scheduled for execution by invoking
1499/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001500/// the module, and if so, return true.
1501bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001502MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001503 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001504
Torok Edwin24c78352009-06-29 18:49:09 +00001505 // Initialize on-the-fly passes
1506 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1507 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1508 I != E; ++I) {
1509 FunctionPassManagerImpl *FPP = I->second;
1510 Changed |= FPP->doInitialization(M);
1511 }
1512
Devang Patelabfbe3b2006-12-16 00:56:26 +00001513 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1514 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001515 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001516
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001517 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001518 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001519
Devang Patelabfbe3b2006-12-16 00:56:26 +00001520 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001521
Chris Lattner4c1e9542009-03-06 06:45:05 +00001522 {
1523 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001524 TimeRegion PassTimer(getPassTimer(MP));
1525
Dan Gohman74b189f2010-03-01 17:34:28 +00001526 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001527 }
Devang Patel93a197c2006-12-14 00:08:04 +00001528
Dan Gohman74b189f2010-03-01 17:34:28 +00001529 Changed |= LocalChanged;
1530 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001531 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001532 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001533 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001534
Devang Patela273d1c2007-07-19 18:02:32 +00001535 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001536 removeNotPreservedAnalysis(MP);
1537 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001538 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001539 }
Torok Edwin24c78352009-06-29 18:49:09 +00001540
1541 // Finalize on-the-fly passes
1542 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1543 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1544 I != E; ++I) {
1545 FunctionPassManagerImpl *FPP = I->second;
1546 // We don't know when is the last time an on-the-fly pass is run,
1547 // so we need to releaseMemory / finalize here
1548 FPP->releaseMemoryOnTheFly();
1549 Changed |= FPP->doFinalization(M);
1550 }
Devang Patel05e1a972006-11-07 22:03:15 +00001551 return Changed;
1552}
1553
Devang Patele64d3052007-04-16 20:12:57 +00001554/// Add RequiredPass into list of lower level passes required by pass P.
1555/// RequiredPass is run on the fly by Pass Manager when P requests it
1556/// through getAnalysis interface.
1557void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001558 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1559 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001560 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001561 RequiredPass->getPotentialPassManagerType()) &&
1562 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001563
Devang Patel68f72b12007-04-26 17:50:19 +00001564 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001565 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001566 FPP = new FunctionPassManagerImpl(0);
1567 // FPP is the top level manager.
1568 FPP->setTopLevelManager(FPP);
1569
Devang Patel69e9f6d2007-04-16 20:27:05 +00001570 OnTheFlyManagers[P] = FPP;
1571 }
Devang Patel68f72b12007-04-26 17:50:19 +00001572 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001573
Devang Patel68f72b12007-04-26 17:50:19 +00001574 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001575 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001576 LU.push_back(RequiredPass);
1577 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001578}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001579
Dan Gohmande6188a2010-08-12 23:50:08 +00001580/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001581/// required by module pass MP. Instantiate analysis pass, by using
1582/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001583Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001584 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001585 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001586
Torok Edwin24c78352009-06-29 18:49:09 +00001587 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001588 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001589 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001590}
1591
1592
Devang Patela1514cb2006-12-07 19:39:39 +00001593//===----------------------------------------------------------------------===//
1594// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001595//
Devang Patelc290c8a2006-11-07 22:23:34 +00001596/// run - Execute all of the passes scheduled for execution. Keep track of
1597/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001598bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001599 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001600 TimingInfo::createTheTimeInfo();
1601
Devang Patelcfd70c42006-12-13 22:10:00 +00001602 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001603 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001604
Devang Patele3068402006-12-21 00:16:50 +00001605 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001606 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1607 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001608 return Changed;
1609}
Devang Patel376fefa2006-11-08 10:29:57 +00001610
Devang Patela1514cb2006-12-07 19:39:39 +00001611//===----------------------------------------------------------------------===//
1612// PassManager implementation
1613
Devang Patel376fefa2006-11-08 10:29:57 +00001614/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001615PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001616 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001617 // PM is the top level manager
1618 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001619}
1620
Devang Patelb67904d2006-12-13 02:36:01 +00001621PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001622 delete PM;
1623}
1624
David Greene103d4b42010-05-10 20:24:27 +00001625/// addImpl - Add a pass to the queue of passes to run, without
1626/// checking whether to add a printer pass.
1627void PassManager::addImpl(Pass *P) {
1628 PM->add(P);
1629}
1630
Devang Patel376fefa2006-11-08 10:29:57 +00001631/// add - Add a pass to the queue of passes to run. This passes ownership of
1632/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1633/// will be destroyed as well, so there is no need to delete the pass. This
1634/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001635void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001636 const void* PassID = P->getPassID();
1637 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001638 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1639 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001640
David Greene103d4b42010-05-10 20:24:27 +00001641 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001642
Owen Andersona7aed182010-08-06 18:33:48 +00001643 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001644 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1645 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001646}
1647
1648/// run - Execute all of the passes scheduled for execution. Keep track of
1649/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001650bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001651 return PM->run(M);
1652}
1653
Devang Patelb8817b92006-12-14 00:59:42 +00001654//===----------------------------------------------------------------------===//
1655// TimingInfo Class - This class is used to calculate information about the
1656// amount of time each pass takes to execute. This only happens with
1657// -time-passes is enabled on the command line.
1658//
1659bool llvm::TimePassesIsEnabled = false;
1660static cl::opt<bool,true>
1661EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1662 cl::desc("Time each pass, printing elapsed time for each on exit"));
1663
1664// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1665// a non null value (if the -time-passes option is enabled) or it leaves it
1666// null. It may be called multiple times.
1667void TimingInfo::createTheTimeInfo() {
1668 if (!TimePassesIsEnabled || TheTimeInfo) return;
1669
1670 // Constructed the first time this is called, iff -time-passes is enabled.
1671 // This guarantees that the object will be constructed before static globals,
1672 // thus it will be destroyed before them.
1673 static ManagedStatic<TimingInfo> TTI;
1674 TheTimeInfo = &*TTI;
1675}
1676
Devang Patel1c3633e2007-01-29 23:10:37 +00001677/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001678Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001679 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001680 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001681 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001682}
1683
Devang Patel1c56a632007-01-08 19:29:38 +00001684//===----------------------------------------------------------------------===//
1685// PMStack implementation
1686//
Devang Patelad98d232007-01-11 22:15:30 +00001687
Devang Patel1c56a632007-01-08 19:29:38 +00001688// Pop Pass Manager from the stack and clear its analysis info.
1689void PMStack::pop() {
1690
1691 PMDataManager *Top = this->top();
1692 Top->initializeAnalysisInfo();
1693
1694 S.pop_back();
1695}
1696
1697// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001698void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001699 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001700
Chris Lattner60987362009-03-06 05:53:14 +00001701 if (!this->empty()) {
1702 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001703
Chris Lattner60987362009-03-06 05:53:14 +00001704 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001705 TPM->addIndirectPassManager(PM);
1706 PM->setTopLevelManager(TPM);
1707 }
1708
Devang Patel15701b52007-01-11 00:19:00 +00001709 S.push_back(PM);
1710}
1711
1712// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001713void PMStack::dump() const {
1714 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001715 E = S.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +00001716 printf("%s ", (*I)->getAsPass()->getPassName());
Chris Lattner60987362009-03-06 05:53:14 +00001717
Devang Patel15701b52007-01-11 00:19:00 +00001718 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001719 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001720}
1721
Devang Patel1c56a632007-01-08 19:29:38 +00001722/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001723/// add self into that manager.
1724void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001725 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001726 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001727 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001728 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1729 if (TopPMType == PreferredType)
1730 break; // We found desired pass manager
1731 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001732 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001733 else
1734 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001735 }
Devang Patel18ff6362008-09-09 21:38:40 +00001736 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001737 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001738}
1739
Devang Patel3312f752007-01-16 21:43:18 +00001740/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001741/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001742void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001743 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001744
Devang Patela3286902008-09-09 17:56:50 +00001745 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001746 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001747 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1748 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001749 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001750 break;
Devang Patel3312f752007-01-16 21:43:18 +00001751 }
Devang Patel3312f752007-01-16 21:43:18 +00001752
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001753 // Create new Function Pass Manager if needed.
1754 FPPassManager *FPP;
1755 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1756 FPP = (FPPassManager *)PMS.top();
1757 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001758 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1759 PMDataManager *PMD = PMS.top();
1760
1761 // [1] Create new Function Pass Manager
1762 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001763 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001764
1765 // [2] Set up new manager's top level manager
1766 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1767 TPM->addIndirectPassManager(FPP);
1768
1769 // [3] Assign manager to manage this new manager. This may create
1770 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001771 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001772
1773 // [4] Push new manager into PMS
1774 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001775 }
1776
Devang Patel3312f752007-01-16 21:43:18 +00001777 // Assign FPP as the manager of this pass.
1778 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001779}
1780
Devang Patel3312f752007-01-16 21:43:18 +00001781/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001782/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001783void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001784 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001785 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001786
Devang Patel15701b52007-01-11 00:19:00 +00001787 // Basic Pass Manager is a leaf pass manager. It does not handle
1788 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001789 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001790 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1791 BBP = (BBPassManager *)PMS.top();
1792 } else {
1793 // If leaf manager is not Basic Block Pass manager then create new
1794 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001795 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1796 PMDataManager *PMD = PMS.top();
1797
1798 // [1] Create new Basic Block Manager
1799 BBP = new BBPassManager(PMD->getDepth() + 1);
1800
1801 // [2] Set up new manager's top level manager
1802 // Basic Block Pass Manager does not live by itself
1803 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1804 TPM->addIndirectPassManager(BBP);
1805
Devang Patel15701b52007-01-11 00:19:00 +00001806 // [3] Assign manager to manage this new manager. This may create
1807 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001808 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001809
Devang Patel3312f752007-01-16 21:43:18 +00001810 // [4] Push new manager into PMS
1811 PMS.push(BBP);
1812 }
Devang Patel1c56a632007-01-08 19:29:38 +00001813
Devang Patel3312f752007-01-16 21:43:18 +00001814 // Assign BBP as the manager of this pass.
1815 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001816}
1817
Dan Gohmand3a20c92008-03-11 16:41:42 +00001818PassManagerBase::~PassManagerBase() {}