blob: ecedb1db66b0678346073a66c49d5210fa423f85 [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmande6188a2010-08-12 23:50:08 +000010// This file implements the LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Dan Gohmana19631f2010-08-07 01:18:18 +000016#include "llvm/PassManager.h"
Devang Patelfa31d382011-03-10 00:21:25 +000017#include "llvm/DebugInfoProbe.h"
David Greene9b063df2010-04-02 23:17:14 +000018#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000019#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000020#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000021#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000022#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000023#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000025#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000026#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000027#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Mutex.h"
Devang Patelfa31d382011-03-10 00:21:25 +000029#include "llvm/ADT/StringMap.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000030#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000031#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000033
Devang Patele7599552007-01-12 18:52:44 +000034// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000035
Devang Patelf1567a52006-12-13 20:03:48 +000036namespace llvm {
37
38//===----------------------------------------------------------------------===//
39// Pass debugging information. Often it is useful to find out what pass is
40// running when a crash occurs in a utility. When this library is compiled with
41// debugging on, a command line option (--debug-pass) is enabled that causes the
42// pass name to be printed before it executes.
43//
44
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
47 None, Arguments, Structure, Executions, Details
48};
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000060
61typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
63
64// Print IR out before/after specified passes.
65static PassOptionList
66PrintBefore("print-before",
Eric Christopher787ba0b2011-03-09 19:46:51 +000067 llvm::cl::desc("Print IR before specified passes"),
68 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000069
70static PassOptionList
71PrintAfter("print-after",
Eric Christopher787ba0b2011-03-09 19:46:51 +000072 llvm::cl::desc("Print IR after specified passes"),
73 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000074
75static cl::opt<bool>
76PrintBeforeAll("print-before-all",
77 llvm::cl::desc("Print IR before each pass"),
78 cl::init(false));
79static cl::opt<bool>
80PrintAfterAll("print-after-all",
81 llvm::cl::desc("Print IR after each pass"),
82 cl::init(false));
83
84/// This is a helper to determine whether to print IR before or
85/// after a pass.
86
Owen Andersona7aed182010-08-06 18:33:48 +000087static bool ShouldPrintBeforeOrAfterPass(const void *PassID,
David Greene9b063df2010-04-02 23:17:14 +000088 PassOptionList &PassesToPrint) {
Owen Andersona7aed182010-08-06 18:33:48 +000089 if (const llvm::PassInfo *PI =
90 PassRegistry::getPassRegistry()->getPassInfo(PassID)) {
91 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
92 const llvm::PassInfo *PassInf = PassesToPrint[i];
93 if (PassInf)
94 if (PassInf->getPassArgument() == PI->getPassArgument()) {
95 return true;
96 }
97 }
David Greene9b063df2010-04-02 23:17:14 +000098 }
99 return false;
100}
Dan Gohmande6188a2010-08-12 23:50:08 +0000101
David Greene9b063df2010-04-02 23:17:14 +0000102
103/// This is a utility to check whether a pass should have IR dumped
104/// before it.
Owen Andersona7aed182010-08-06 18:33:48 +0000105static bool ShouldPrintBeforePass(const void *PassID) {
106 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000107}
108
109/// This is a utility to check whether a pass should have IR dumped
110/// after it.
Owen Andersona7aed182010-08-06 18:33:48 +0000111static bool ShouldPrintAfterPass(const void *PassID) {
112 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000113}
114
Devang Patelf1567a52006-12-13 20:03:48 +0000115} // End of llvm namespace
116
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000117/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
118/// or higher is specified.
119bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
120 return PassDebugging >= Executions;
121}
122
123
124
125
Chris Lattner4c1e9542009-03-06 06:45:05 +0000126void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
127 if (V == 0 && M == 0)
128 OS << "Releasing pass '";
129 else
130 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000131
Chris Lattner4c1e9542009-03-06 06:45:05 +0000132 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000133
Chris Lattner4c1e9542009-03-06 06:45:05 +0000134 if (M) {
135 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
136 return;
137 }
138 if (V == 0) {
139 OS << '\n';
140 return;
141 }
142
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000144 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000145 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000146 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000147 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000148 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000149 OS << "value";
150
151 OS << " '";
152 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
153 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000154}
155
156
Devang Patelffca9102006-12-15 19:39:30 +0000157namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000158
Devang Patelf33f3eb2006-12-07 19:21:29 +0000159//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000160// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000161//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000162/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000163/// pass together and sequence them to process one basic block before
164/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000165class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000166
167public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000168 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000169 explicit BBPassManager()
170 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000171
Devang Patelca58e352006-11-08 10:05:38 +0000172 /// Execute all of the passes scheduled for execution. Keep track of
173 /// whether any of the passes modifies the function, and if so, return true.
174 bool runOnFunction(Function &F);
175
Devang Patelf9d96b92006-12-07 19:57:52 +0000176 /// Pass Manager itself does not invalidate any analysis info.
177 void getAnalysisUsage(AnalysisUsage &Info) const {
178 Info.setPreservesAll();
179 }
180
Devang Patel475c4532006-12-08 00:59:05 +0000181 bool doInitialization(Module &M);
182 bool doInitialization(Function &F);
183 bool doFinalization(Module &M);
184 bool doFinalization(Function &F);
185
Chris Lattner2fa26e52010-01-22 05:24:46 +0000186 virtual PMDataManager *getAsPMDataManager() { return this; }
187 virtual Pass *getAsPass() { return this; }
188
Devang Patele3858e62007-02-01 22:08:25 +0000189 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000190 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000191 }
192
Devang Pateleda56172006-12-12 23:34:33 +0000193 // Print passes managed by this manager
194 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000195 llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000196 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
197 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000198 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000200 }
201 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000202
203 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000204 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000205 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
206 return BP;
207 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000208
Dan Gohmande6188a2010-08-12 23:50:08 +0000209 virtual PassManagerType getPassManagerType() const {
210 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000211 }
Devang Patelca58e352006-11-08 10:05:38 +0000212};
213
Devang Patel8c78a0b2007-05-03 01:11:54 +0000214char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000215}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216
Devang Patele7599552007-01-12 18:52:44 +0000217namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000218
Devang Patel10c2ca62006-12-12 22:47:13 +0000219//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000221//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000222/// FunctionPassManagerImpl manages FPPassManagers
223class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000224 public PMDataManager,
225 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000226private:
227 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000229 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000230 explicit FunctionPassManagerImpl() :
231 Pass(PT_PassManager, ID), PMDataManager(),
232 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233
234 /// add - Add a pass to the queue of passes to run. This passes ownership of
235 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
236 /// will be destroyed as well, so there is no need to delete the pass. This
237 /// implies that all passes MUST be allocated with 'new'.
238 void add(Pass *P) {
239 schedulePass(P);
240 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000241
242 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000243 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
244 return createPrintFunctionPass(Banner, &O);
245 }
246
Torok Edwin24c78352009-06-29 18:49:09 +0000247 // Prepare for running an on the fly pass, freeing memory if needed
248 // from a previous run.
249 void releaseMemoryOnTheFly();
250
Devang Patel67d6a5e2006-12-19 19:46:59 +0000251 /// run - Execute all of the passes scheduled for execution. Keep track of
252 /// whether any of the passes modifies the module, and if so, return true.
253 bool run(Function &F);
254
255 /// doInitialization - Run all of the initializers for the function passes.
256 ///
257 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000258
Dan Gohmane6656eb2007-07-30 14:51:13 +0000259 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000260 ///
261 bool doFinalization(Module &M);
262
Dan Gohmande6188a2010-08-12 23:50:08 +0000263
Chris Lattner2fa26e52010-01-22 05:24:46 +0000264 virtual PMDataManager *getAsPMDataManager() { return this; }
265 virtual Pass *getAsPass() { return this; }
266
Devang Patel67d6a5e2006-12-19 19:46:59 +0000267 /// Pass Manager itself does not invalidate any analysis info.
268 void getAnalysisUsage(AnalysisUsage &Info) const {
269 Info.setPreservesAll();
270 }
271
Dan Gohman30614852010-08-16 21:57:30 +0000272 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000273 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274 // P is a immutable pass and it will be managed by this
275 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000276 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000277 P->setResolver(AR);
278 initializeAnalysisImpl(P);
279 addImmutablePass(IP);
280 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000281 } else {
David Greene103d4b42010-05-10 20:24:27 +0000282 P->assignPassManager(activeStack, PMT_FunctionPassManager);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283 }
Devang Patel0f080042007-01-12 17:23:48 +0000284
Devang Patel67d6a5e2006-12-19 19:46:59 +0000285 }
286
287 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000288 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000289 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
290 return FP;
291 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000292};
293
Devang Patel8c78a0b2007-05-03 01:11:54 +0000294char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000295
Devang Patel67d6a5e2006-12-19 19:46:59 +0000296//===----------------------------------------------------------------------===//
297// MPPassManager
298//
299/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000300/// It batches all Module passes and function pass managers together and
301/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000302class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000303public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000304 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000305 explicit MPPassManager() :
306 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000307
308 // Delete on the fly managers.
309 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000310 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000311 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
312 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000313 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000314 delete FPP;
315 }
316 }
317
Dan Gohmande6188a2010-08-12 23:50:08 +0000318 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000319 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
320 return createPrintModulePass(&O, false, Banner);
321 }
322
Devang Patelca58e352006-11-08 10:05:38 +0000323 /// run - Execute all of the passes scheduled for execution. Keep track of
324 /// whether any of the passes modifies the module, and if so, return true.
325 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000326
Devang Patelf9d96b92006-12-07 19:57:52 +0000327 /// Pass Manager itself does not invalidate any analysis info.
328 void getAnalysisUsage(AnalysisUsage &Info) const {
329 Info.setPreservesAll();
330 }
331
Devang Patele64d3052007-04-16 20:12:57 +0000332 /// Add RequiredPass into list of lower level passes required by pass P.
333 /// RequiredPass is run on the fly by Pass Manager when P requests it
334 /// through getAnalysis interface.
335 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
336
Dan Gohmande6188a2010-08-12 23:50:08 +0000337 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000338 /// required by module pass MP. Instantiate analysis pass, by using
339 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000340 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000341
Devang Patele3858e62007-02-01 22:08:25 +0000342 virtual const char *getPassName() const {
343 return "Module Pass Manager";
344 }
345
Chris Lattner2fa26e52010-01-22 05:24:46 +0000346 virtual PMDataManager *getAsPMDataManager() { return this; }
347 virtual Pass *getAsPass() { return this; }
348
Devang Pateleda56172006-12-12 23:34:33 +0000349 // Print passes managed by this manager
350 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000351 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000352 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
353 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000354 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000355 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
356 OnTheFlyManagers.find(MP);
357 if (I != OnTheFlyManagers.end())
358 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000359 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000360 }
361 }
362
Devang Patelabfbe3b2006-12-16 00:56:26 +0000363 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000364 assert(N < PassVector.size() && "Pass number out of range!");
365 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000366 }
367
Dan Gohmande6188a2010-08-12 23:50:08 +0000368 virtual PassManagerType getPassManagerType() const {
369 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000370 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000371
372 private:
373 /// Collection of on the fly FPPassManagers. These managers manage
374 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000375 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000376};
377
Devang Patel8c78a0b2007-05-03 01:11:54 +0000378char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000379//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000380// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000381//
Devang Patel09f162c2007-05-01 21:15:47 +0000382
Devang Patel67d6a5e2006-12-19 19:46:59 +0000383/// PassManagerImpl manages MPPassManagers
384class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000385 public PMDataManager,
386 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000387
388public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000389 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000390 explicit PassManagerImpl() :
391 Pass(PT_PassManager, ID), PMDataManager(),
392 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000393
Devang Patel376fefa2006-11-08 10:29:57 +0000394 /// add - Add a pass to the queue of passes to run. This passes ownership of
395 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
396 /// will be destroyed as well, so there is no need to delete the pass. This
397 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000398 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000399 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000400 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000401
402 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000403 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
404 return createPrintModulePass(&O, false, Banner);
405 }
406
Devang Patel376fefa2006-11-08 10:29:57 +0000407 /// run - Execute all of the passes scheduled for execution. Keep track of
408 /// whether any of the passes modifies the module, and if so, return true.
409 bool run(Module &M);
410
Devang Patelf9d96b92006-12-07 19:57:52 +0000411 /// Pass Manager itself does not invalidate any analysis info.
412 void getAnalysisUsage(AnalysisUsage &Info) const {
413 Info.setPreservesAll();
414 }
415
Dan Gohman30614852010-08-16 21:57:30 +0000416 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000417 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000418 // P is a immutable pass and it will be managed by this
419 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000420 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000421 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000422 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000423 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000424 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000425 } else {
David Greene103d4b42010-05-10 20:24:27 +0000426 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000427 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000428 }
429
Chris Lattner2fa26e52010-01-22 05:24:46 +0000430 virtual PMDataManager *getAsPMDataManager() { return this; }
431 virtual Pass *getAsPass() { return this; }
432
Devang Patel67d6a5e2006-12-19 19:46:59 +0000433 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000434 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000435 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
436 return MP;
437 }
Devang Patel376fefa2006-11-08 10:29:57 +0000438};
439
Devang Patel8c78a0b2007-05-03 01:11:54 +0000440char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000441} // End of llvm namespace
442
443namespace {
444
445//===----------------------------------------------------------------------===//
Devang Patelfa31d382011-03-10 00:21:25 +0000446// DebugInfoProbe
447
448static DebugInfoProbeInfo *TheDebugProbe;
449static void createDebugInfoProbe() {
450 if (TheDebugProbe) return;
Andrew Trickb3bddf02011-06-03 00:44:32 +0000451
452 // Constructed the first time this is called. This guarantees that the
453 // object will be constructed, if -enable-debug-info-probe is set,
Devang Patelfa31d382011-03-10 00:21:25 +0000454 // before static globals, thus it will be destroyed before them.
455 static ManagedStatic<DebugInfoProbeInfo> DIP;
456 TheDebugProbe = &*DIP;
457}
458
459//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000460/// TimingInfo Class - This class is used to calculate information about the
461/// amount of time each pass takes to execute. This only happens when
462/// -time-passes is enabled on the command line.
463///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000464
Owen Anderson5a6960f2009-06-18 20:51:00 +0000465static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000466
Nick Lewycky02d5f772009-10-25 06:33:48 +0000467class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000468 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000469 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000470public:
471 // Use 'create' member to get this.
472 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000473
Devang Patel1c3633e2007-01-29 23:10:37 +0000474 // TimingDtor - Print out information about timing information
475 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000476 // Delete all of the timers, which accumulate their info into the
477 // TimerGroup.
478 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
479 E = TimingData.end(); I != E; ++I)
480 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000481 // TimerGroup is deleted next, printing the report.
482 }
483
484 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
485 // to a non null value (if the -time-passes option is enabled) or it leaves it
486 // null. It may be called multiple times.
487 static void createTheTimeInfo();
488
Chris Lattner707431c2010-03-30 04:03:22 +0000489 /// getPassTimer - Return the timer for the specified pass if it exists.
490 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000491 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000492 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000493
Owen Anderson5c96ef72009-07-07 18:33:04 +0000494 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000495 Timer *&T = TimingData[P];
496 if (T == 0)
497 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000498 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000499 }
500};
501
Devang Patel1c3633e2007-01-29 23:10:37 +0000502} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000503
Dan Gohmand78c4002008-05-13 00:00:25 +0000504static TimingInfo *TheTimeInfo;
505
Devang Patela1514cb2006-12-07 19:39:39 +0000506//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000507// PMTopLevelManager implementation
508
Devang Patel4268fc02007-01-16 02:00:38 +0000509/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000510PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
511 PMDM->setTopLevelManager(this);
512 addPassManager(PMDM);
513 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000514}
515
Devang Patelafb1f3622006-12-12 22:35:25 +0000516/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000517void
518PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
519 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000520 unsigned PDepth = 0;
521 if (P->getResolver())
522 PDepth = P->getResolver()->getPMDataManager().getDepth();
523
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000524 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000525 E = AnalysisPasses.end(); I != E; ++I) {
526 Pass *AP = *I;
527 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000528
Devang Patel01919d22007-03-08 19:05:01 +0000529 if (P == AP)
530 continue;
531
Tobias Grosserf07426b2011-01-20 21:03:22 +0000532 // Update the last users of passes that are required transitive by AP.
533 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
534 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
535 SmallVector<Pass *, 12> LastUses;
536 SmallVector<Pass *, 12> LastPMUses;
537 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
538 E = IDs.end(); I != E; ++I) {
539 Pass *AnalysisPass = findAnalysisPass(*I);
540 assert(AnalysisPass && "Expected analysis pass to exist.");
541 AnalysisResolver *AR = AnalysisPass->getResolver();
542 assert(AR && "Expected analysis resolver to exist.");
543 unsigned APDepth = AR->getPMDataManager().getDepth();
544
545 if (PDepth == APDepth)
546 LastUses.push_back(AnalysisPass);
547 else if (PDepth > APDepth)
548 LastPMUses.push_back(AnalysisPass);
549 }
550
551 setLastUser(LastUses, P);
552
553 // If this pass has a corresponding pass manager, push higher level
554 // analysis to this pass manager.
555 if (P->getResolver())
556 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
557
558
Devang Patelafb1f3622006-12-12 22:35:25 +0000559 // If AP is the last user of other passes then make P last user of
560 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000561 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000562 LUE = LastUser.end(); LUI != LUE; ++LUI) {
563 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000564 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000565 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000566 LastUser[LUI->first] = P;
567 }
568 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000569}
570
571/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000572void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000573 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000574 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000575 InversedLastUser.find(P);
576 if (DMI == InversedLastUser.end())
577 return;
578
579 SmallPtrSet<Pass *, 8> &LU = DMI->second;
580 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
581 E = LU.end(); I != E; ++I) {
582 LastUses.push_back(*I);
583 }
584
Devang Patelafb1f3622006-12-12 22:35:25 +0000585}
586
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000587AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
588 AnalysisUsage *AnUsage = NULL;
589 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000590 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000591 AnUsage = DMI->second;
592 else {
593 AnUsage = new AnalysisUsage();
594 P->getAnalysisUsage(*AnUsage);
595 AnUsageMap[P] = AnUsage;
596 }
597 return AnUsage;
598}
599
Devang Patelafb1f3622006-12-12 22:35:25 +0000600/// Schedule pass P for execution. Make sure that passes required by
601/// P are run before P is run. Update analysis info maintained by
602/// the manager. Remove dead passes. This is a recursive function.
603void PMTopLevelManager::schedulePass(Pass *P) {
604
Devang Patel3312f752007-01-16 21:43:18 +0000605 // TODO : Allocate function manager for this pass, other wise required set
606 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000607
Devang Pateld74ede72007-03-06 01:06:16 +0000608 // Give pass a chance to prepare the stage.
609 P->preparePassManager(activeStack);
610
Devang Patel864970e2008-03-18 00:39:19 +0000611 // If P is an analysis pass and it is available then do not
612 // generate the analysis again. Stale analysis info should not be
613 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000614 const PassInfo *PI =
615 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
616 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000617 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000618 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000619 }
Devang Patel864970e2008-03-18 00:39:19 +0000620
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000621 AnalysisUsage *AnUsage = findAnalysisUsage(P);
622
Devang Patelfdee7032008-08-14 23:07:48 +0000623 bool checkAnalysis = true;
624 while (checkAnalysis) {
625 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000626
Devang Patelfdee7032008-08-14 23:07:48 +0000627 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
628 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
629 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000630
Devang Patelfdee7032008-08-14 23:07:48 +0000631 Pass *AnalysisPass = findAnalysisPass(*I);
632 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000633 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000634 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000635 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000636 if (P->getPotentialPassManagerType () ==
637 AnalysisPass->getPotentialPassManagerType())
638 // Schedule analysis pass that is managed by the same pass manager.
639 schedulePass(AnalysisPass);
640 else if (P->getPotentialPassManagerType () >
641 AnalysisPass->getPotentialPassManagerType()) {
642 // Schedule analysis pass that is managed by a new manager.
643 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000644 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000645 // are already checked are still available.
646 checkAnalysis = true;
647 }
648 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000649 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000650 // passes are run on the fly.
651 delete AnalysisPass;
652 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000653 }
654 }
655
656 // Now all required passes are available.
657 addTopLevelPass(P);
658}
659
660/// Find the pass that implements Analysis AID. Search immutable
661/// passes and all pass managers. If desired pass is not found
662/// then return NULL.
663Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
664
Devang Patelcd6ba152006-12-12 22:50:05 +0000665 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000666 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000667 E = PassManagers.end(); I != E; ++I)
668 if (Pass *P = (*I)->findAnalysisPass(AID, false))
669 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000670
671 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000672 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000673 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000674 E = IndirectPassManagers.end(); I != E; ++I)
675 if (Pass *P = (*I)->findAnalysisPass(AID, false))
676 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000677
Dan Gohman844dd0a2010-10-11 23:19:01 +0000678 // Check the immutable passes. Iterate in reverse order so that we find
679 // the most recently registered passes first.
680 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
681 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000682 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000683 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000684 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000685
686 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000687 const PassInfo *PassInf =
688 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000689 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000690 const std::vector<const PassInfo*> &ImmPI =
691 PassInf->getInterfacesImplemented();
692 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
693 EE = ImmPI.end(); II != EE; ++II) {
694 if ((*II)->getTypeInfo() == AID)
695 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000696 }
697 }
698
Dan Gohman844dd0a2010-10-11 23:19:01 +0000699 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000700}
701
Devang Pateleda56172006-12-12 23:34:33 +0000702// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000703void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000704
Devang Patelfd4184322007-01-17 20:33:36 +0000705 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000706 return;
707
Devang Pateleda56172006-12-12 23:34:33 +0000708 // Print out the immutable passes
709 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000710 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000711 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000712
Dan Gohmanf71c5212010-08-19 01:29:07 +0000713 // Every class that derives from PMDataManager also derives from Pass
714 // (sometimes indirectly), but there's no inheritance relationship
715 // between PMDataManager and Pass, so we have to getAsPass to get
716 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000717 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000718 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000719 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000720}
721
Devang Patel991aeba2006-12-15 20:13:01 +0000722void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000723
Devang Patelfd4184322007-01-17 20:33:36 +0000724 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000725 return;
726
David Greene994e1bb2010-01-05 01:30:02 +0000727 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000728 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
729 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
730 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000731 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
732 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000733 if (!PI->isAnalysisGroup())
734 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000735 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000736 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000737 E = PassManagers.end(); I != E; ++I)
738 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000739 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000740}
741
Devang Patele3068402006-12-21 00:16:50 +0000742void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000743 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000744 E = PassManagers.end(); I != E; ++I)
745 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000746
Devang Patele3068402006-12-21 00:16:50 +0000747 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000748 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000749 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
750 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000751 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000752
Chris Lattner60987362009-03-06 05:53:14 +0000753 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000754 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000755 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000756 InversedLastUser.find(DMI->second);
757 if (InvDMI != InversedLastUser.end()) {
758 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
759 L.insert(DMI->first);
760 } else {
761 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
762 InversedLastUser[DMI->second] = L;
763 }
764 }
Devang Patele3068402006-12-21 00:16:50 +0000765}
766
Devang Patele7599552007-01-12 18:52:44 +0000767/// Destructor
768PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000769 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000770 E = PassManagers.end(); I != E; ++I)
771 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000772
Dan Gohman060d5ba2010-10-12 00:15:27 +0000773 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000774 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
775 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000776
777 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000778 DME = AnUsageMap.end(); DMI != DME; ++DMI)
779 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000780}
781
Devang Patelafb1f3622006-12-12 22:35:25 +0000782//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000783// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000784
Devang Patel643676c2006-11-11 01:10:19 +0000785/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000786void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000787 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000788
Chris Lattner60987362009-03-06 05:53:14 +0000789 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000790
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000791 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000792
Dan Gohmande6188a2010-08-12 23:50:08 +0000793 // This pass is the current implementation of all of the interfaces it
794 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000795 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
796 if (PInf == 0) return;
797 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000798 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000799 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000800}
801
Devang Patel9d9fc902007-03-06 17:52:53 +0000802// Return true if P preserves high level analysis used by other
803// passes managed by this manager
804bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000805 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000806 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000807 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000808
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000809 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000810 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000811 E = HigherLevelAnalysis.end(); I != E; ++I) {
812 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000813 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000814 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000815 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000816 PreservedSet.end())
817 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000818 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000819
Devang Patel9d9fc902007-03-06 17:52:53 +0000820 return true;
821}
822
Chris Lattner02eb94c2008-08-07 07:34:50 +0000823/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000824void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000825 // Don't do this unless assertions are enabled.
826#ifdef NDEBUG
827 return;
828#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000829 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
830 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000831
Devang Patelef432532007-07-19 05:36:09 +0000832 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000833 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000834 E = PreservedSet.end(); I != E; ++I) {
835 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000836 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000837 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000838 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000839 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000840 }
841}
842
Devang Patel67c79a42008-07-01 19:50:56 +0000843/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000844void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000845 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
846 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000847 return;
848
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000849 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000850 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000851 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000852 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000853 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000854 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000855 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000856 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000857 if (PassDebugging >= Details) {
858 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000859 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
860 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000861 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000862 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000863 }
Devang Patel349170f2006-11-11 01:24:55 +0000864 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000865
Devang Patel42dd1e92007-03-06 01:55:46 +0000866 // Check inherited analysis also. If P is not preserving analysis
867 // provided by parent manager then remove it here.
868 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
869
870 if (!InheritedAnalysis[Index])
871 continue;
872
Dan Gohmande6188a2010-08-12 23:50:08 +0000873 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000874 I = InheritedAnalysis[Index]->begin(),
875 E = InheritedAnalysis[Index]->end(); I != E; ) {
876 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000877 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000878 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000879 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000880 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000881 if (PassDebugging >= Details) {
882 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000883 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
884 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000885 }
Devang Patel01919d22007-03-08 19:05:01 +0000886 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000887 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000888 }
889 }
Devang Patelf68a3492006-11-07 22:35:17 +0000890}
891
Devang Patelca189262006-11-14 03:05:08 +0000892/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000893void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000894 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000895
Devang Patel8adae862007-07-20 18:04:54 +0000896 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000897
Devang Patel2ff44922007-04-16 20:39:59 +0000898 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000899 if (!TPM)
900 return;
901
Devang Patel17ad0962006-12-08 00:37:52 +0000902 TPM->collectLastUses(DeadPasses, P);
903
Devang Patel656a9172008-06-06 17:50:36 +0000904 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000905 dbgs() << " -*- '" << P->getPassName();
906 dbgs() << "' is the last user of following pass instances.";
907 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000908 }
909
Dan Gohman060d5ba2010-10-12 00:15:27 +0000910 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000911 E = DeadPasses.end(); I != E; ++I)
912 freePass(*I, Msg, DBG_STR);
913}
Devang Patel200d3052006-12-13 23:50:44 +0000914
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000915void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000916 enum PassDebuggingString DBG_STR) {
917 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000918
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000919 {
920 // If the pass crashes releasing memory, remember this.
921 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000922 TimeRegion PassTimer(getPassTimer(P));
923
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000924 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000925 }
926
Owen Andersona7aed182010-08-06 18:33:48 +0000927 AnalysisID PI = P->getPassID();
928 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000929 // Remove the pass itself (if it is not already removed).
930 AvailableAnalysis.erase(PI);
931
932 // Remove all interfaces this pass implements, for which it is also
933 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000934 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000935 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000936 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000937 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000938 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000939 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000940 }
Devang Patel17ad0962006-12-08 00:37:52 +0000941 }
Devang Patelca189262006-11-14 03:05:08 +0000942}
943
Dan Gohmande6188a2010-08-12 23:50:08 +0000944/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000945/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000946void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000947 // This manager is going to manage pass P. Set up analysis resolver
948 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000949 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000950 P->setResolver(AR);
951
Devang Patelec2b9a72007-03-05 22:57:49 +0000952 // If a FunctionPass F is the last user of ModulePass info M
953 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000954 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000955
Chris Lattner60987362009-03-06 05:53:14 +0000956 if (!ProcessAnalysis) {
957 // Add pass
958 PassVector.push_back(P);
959 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000960 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000961
Chris Lattner60987362009-03-06 05:53:14 +0000962 // At the moment, this pass is the last user of all required passes.
963 SmallVector<Pass *, 12> LastUses;
964 SmallVector<Pass *, 8> RequiredPasses;
965 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
966
967 unsigned PDepth = this->getDepth();
968
Dan Gohmande6188a2010-08-12 23:50:08 +0000969 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000970 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000971 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000972 E = RequiredPasses.end(); I != E; ++I) {
973 Pass *PRequired = *I;
974 unsigned RDepth = 0;
975
976 assert(PRequired->getResolver() && "Analysis Resolver is not set");
977 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
978 RDepth = DM.getDepth();
979
980 if (PDepth == RDepth)
981 LastUses.push_back(PRequired);
982 else if (PDepth > RDepth) {
983 // Let the parent claim responsibility of last use
984 TransferLastUses.push_back(PRequired);
985 // Keep track of higher level analysis used by this manager.
986 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000987 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000988 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000989 }
990
991 // Set P as P's last user until someone starts using P.
992 // However, if P is a Pass Manager then it does not need
993 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000994 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000995 LastUses.push_back(P);
996 TPM->setLastUser(LastUses, P);
997
998 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000999 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001000 TPM->setLastUser(TransferLastUses, My_PM);
1001 TransferLastUses.clear();
1002 }
1003
Dan Gohman6304db32010-08-16 22:57:28 +00001004 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001005 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001006 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001007 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001008 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1009 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001010 this->addLowerLevelRequiredPass(P, AnalysisPass);
1011 }
1012
1013 // Take a note of analysis required and made available by this pass.
1014 // Remove the analysis not preserved by this pass
1015 removeNotPreservedAnalysis(P);
1016 recordAvailableAnalysis(P);
1017
Devang Patel8cad70d2006-11-11 01:51:02 +00001018 // Add pass
1019 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001020}
1021
Devang Patele64d3052007-04-16 20:12:57 +00001022
1023/// Populate RP with analysis pass that are required by
1024/// pass P and are available. Populate RP_NotAvail with analysis
1025/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001026void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1027 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001028 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001029 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1030 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001031 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001032 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001033 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001034 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001035 else
Chris Lattner60987362009-03-06 05:53:14 +00001036 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001037 }
Devang Patelf58183d2006-12-12 23:09:32 +00001038
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001039 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001040 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001041 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001042 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001043 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001044 else
Chris Lattner60987362009-03-06 05:53:14 +00001045 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001046 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001047}
1048
Devang Patel07f4f582006-11-14 21:49:36 +00001049// All Required analyses should be available to the pass as it runs! Here
1050// we fill in the AnalysisImpls member of the pass so that it can
1051// successfully use the getAnalysis() method to retrieve the
1052// implementations it needs.
1053//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001054void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001055 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1056
Chris Lattnercbd160f2008-08-08 05:33:04 +00001057 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001058 I = AnUsage->getRequiredSet().begin(),
1059 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001060 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001061 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001062 // This may be analysis pass that is initialized on the fly.
1063 // If that is not the case then it will raise an assert when it is used.
1064 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001065 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001066 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001067 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001068 }
1069}
1070
Devang Patel640c5bb2006-12-08 22:30:11 +00001071/// Find the pass that implements Analysis AID. If desired pass is not found
1072/// then return NULL.
1073Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1074
1075 // Check if AvailableAnalysis map has one entry.
1076 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1077
1078 if (I != AvailableAnalysis.end())
1079 return I->second;
1080
1081 // Search Parents through TopLevelManager
1082 if (SearchParent)
1083 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001084
Devang Patel9d759b82006-12-09 00:09:12 +00001085 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001086}
1087
Devang Patel991aeba2006-12-15 20:13:01 +00001088// Print list of passes that are last used by P.
1089void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1090
Devang Patel8adae862007-07-20 18:04:54 +00001091 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001092
1093 // If this is a on the fly manager then it does not have TPM.
1094 if (!TPM)
1095 return;
1096
Devang Patel991aeba2006-12-15 20:13:01 +00001097 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001098
Dan Gohman7224bce2010-10-12 00:11:18 +00001099 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001100 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001101 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001102 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001103 }
1104}
1105
1106void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001107 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001108 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001109 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001110 PMD->dumpPassArguments();
1111 else
Owen Andersona7aed182010-08-06 18:33:48 +00001112 if (const PassInfo *PI =
1113 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001114 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001115 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001116 }
1117}
1118
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001119void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1120 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001121 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001122 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001123 return;
David Greene994e1bb2010-01-05 01:30:02 +00001124 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001125 switch (S1) {
1126 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001127 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001128 break;
1129 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001130 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001131 break;
1132 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001134 break;
1135 default:
1136 break;
1137 }
1138 switch (S2) {
1139 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
1145 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001148 case ON_REGION_MSG:
1149 dbgs() << "' on Region '" << Msg << "'...\n";
1150 break;
Devang Patel003a5592007-03-05 20:01:30 +00001151 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
1154 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001155 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001156 break;
1157 default:
1158 break;
1159 }
Devang Patel991aeba2006-12-15 20:13:01 +00001160}
1161
Chris Lattner4c1e9542009-03-06 06:45:05 +00001162void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001163 if (PassDebugging < Details)
1164 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001165
Chris Lattner4c493d92008-08-08 15:14:09 +00001166 AnalysisUsage analysisUsage;
1167 P->getAnalysisUsage(analysisUsage);
1168 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1169}
1170
Chris Lattner4c1e9542009-03-06 06:45:05 +00001171void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001172 if (PassDebugging < Details)
1173 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001174
Chris Lattner4c493d92008-08-08 15:14:09 +00001175 AnalysisUsage analysisUsage;
1176 P->getAnalysisUsage(analysisUsage);
1177 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1178}
1179
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001180void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001181 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001182 assert(PassDebugging >= Details);
1183 if (Set.empty())
1184 return;
David Greene994e1bb2010-01-05 01:30:02 +00001185 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001186 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001187 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001188 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001189 if (!PInf) {
1190 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1191 // all drivers.
1192 dbgs() << " Uninitialized Pass";
1193 continue;
1194 }
Owen Andersona7aed182010-08-06 18:33:48 +00001195 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001196 }
David Greene994e1bb2010-01-05 01:30:02 +00001197 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001198}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001199
Devang Patel004937b2007-07-27 20:06:09 +00001200/// Add RequiredPass into list of lower level passes required by pass P.
1201/// RequiredPass is run on the fly by Pass Manager when P requests it
1202/// through getAnalysis interface.
1203/// This should be handled by specific pass manager.
1204void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1205 if (TPM) {
1206 TPM->dumpArguments();
1207 TPM->dumpPasses();
1208 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001209
Dan Gohmande6188a2010-08-12 23:50:08 +00001210 // Module Level pass may required Function Level analysis info
1211 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1212 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001213 // module level pass is requiring lower level analysis info managed by
1214 // lower level pass manager.
1215
1216 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001217 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001218 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001219#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001220 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1221 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001222#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001223 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001224}
1225
Owen Andersona7aed182010-08-06 18:33:48 +00001226Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001227 assert(0 && "Unable to find on the fly pass");
1228 return NULL;
1229}
1230
Devang Patele7599552007-01-12 18:52:44 +00001231// Destructor
1232PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001233 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001234 E = PassVector.end(); I != E; ++I)
1235 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001236}
1237
Devang Patel9bdf7d42006-12-08 23:28:54 +00001238//===----------------------------------------------------------------------===//
1239// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001240// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1241Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001242 return PM.findAnalysisPass(ID, dir);
1243}
1244
Dan Gohmande6188a2010-08-12 23:50:08 +00001245Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001246 Function &F) {
1247 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1248}
1249
Devang Patela1514cb2006-12-07 19:39:39 +00001250//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001251// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001252
Dan Gohmande6188a2010-08-12 23:50:08 +00001253/// Execute all of the passes scheduled for execution by invoking
1254/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001255/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001256bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001257 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001258 return false;
1259
Devang Patele9585592006-12-08 01:38:28 +00001260 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001261
Devang Patel6e5a1132006-11-07 21:31:57 +00001262 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001263 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1264 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001265 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001266
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001267 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001268 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001269
Devang Patelabfbe3b2006-12-16 00:56:26 +00001270 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001271
Chris Lattner4c1e9542009-03-06 06:45:05 +00001272 {
1273 // If the pass crashes, remember this.
1274 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001275 TimeRegion PassTimer(getPassTimer(BP));
1276
Dan Gohman74b189f2010-03-01 17:34:28 +00001277 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001278 }
Devang Patel93a197c2006-12-14 00:08:04 +00001279
Dan Gohman74b189f2010-03-01 17:34:28 +00001280 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001281 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001282 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001283 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001284 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001285
Devang Patela273d1c2007-07-19 18:02:32 +00001286 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001287 removeNotPreservedAnalysis(BP);
1288 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001289 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001290 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001291
Bill Wendling6ce6d262009-12-25 13:50:18 +00001292 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001293}
1294
Devang Patel475c4532006-12-08 00:59:05 +00001295// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001296bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001297 bool Changed = false;
1298
Chris Lattner4c1e9542009-03-06 06:45:05 +00001299 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1300 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001301
1302 return Changed;
1303}
1304
Duncan Sands51495602009-02-13 09:42:34 +00001305bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001306 bool Changed = false;
1307
Chris Lattner4c1e9542009-03-06 06:45:05 +00001308 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1309 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001310
1311 return Changed;
1312}
1313
Duncan Sands51495602009-02-13 09:42:34 +00001314bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001315 bool Changed = false;
1316
Devang Patelabfbe3b2006-12-16 00:56:26 +00001317 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1318 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001319 Changed |= BP->doInitialization(F);
1320 }
1321
1322 return Changed;
1323}
1324
Duncan Sands51495602009-02-13 09:42:34 +00001325bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001326 bool Changed = false;
1327
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1329 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001330 Changed |= BP->doFinalization(F);
1331 }
1332
1333 return Changed;
1334}
1335
1336
Devang Patela1514cb2006-12-07 19:39:39 +00001337//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001338// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001339
Devang Patel4e12f862006-11-08 10:44:40 +00001340/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001341FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001342 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001343 // FPM is the top level manager.
1344 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001345
Dan Gohman565df952008-03-13 02:08:36 +00001346 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001347 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001348}
1349
Devang Patelb67904d2006-12-13 02:36:01 +00001350FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001351 delete FPM;
1352}
1353
David Greene103d4b42010-05-10 20:24:27 +00001354/// addImpl - Add a pass to the queue of passes to run, without
1355/// checking whether to add a printer pass.
1356void FunctionPassManager::addImpl(Pass *P) {
1357 FPM->add(P);
1358}
1359
Devang Patel4e12f862006-11-08 10:44:40 +00001360/// add - Add a pass to the queue of passes to run. This passes
1361/// ownership of the Pass to the PassManager. When the
1362/// PassManager_X is destroyed, the pass will be destroyed as well, so
1363/// there is no need to delete the pass. (TODO delete passes.)
1364/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001365void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001366 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001367 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001368 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001369 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001370 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1371 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001372
David Greene103d4b42010-05-10 20:24:27 +00001373 addImpl(P);
1374
1375 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001376 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001377 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1378 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001379}
1380
Devang Patel9f3083e2006-11-15 19:39:54 +00001381/// run - Execute all of the passes scheduled for execution. Keep
1382/// track of whether any of the passes modifies the function, and if
1383/// so, return true.
1384///
Devang Patelb67904d2006-12-13 02:36:01 +00001385bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001386 if (F.isMaterializable()) {
1387 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001388 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001389 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001390 }
Devang Patel272908d2006-12-08 22:57:48 +00001391 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001392}
1393
1394
Devang Patelff631ae2006-11-15 01:27:05 +00001395/// doInitialization - Run all of the initializers for the function passes.
1396///
Devang Patelb67904d2006-12-13 02:36:01 +00001397bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001398 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001399}
1400
Dan Gohmane6656eb2007-07-30 14:51:13 +00001401/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001402///
Devang Patelb67904d2006-12-13 02:36:01 +00001403bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001404 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001405}
1406
Devang Patela1514cb2006-12-07 19:39:39 +00001407//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001408// FunctionPassManagerImpl implementation
1409//
Duncan Sands51495602009-02-13 09:42:34 +00001410bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001411 bool Changed = false;
1412
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001413 dumpArguments();
1414 dumpPasses();
1415
Chris Lattner4c1e9542009-03-06 06:45:05 +00001416 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1417 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001418
1419 return Changed;
1420}
1421
Duncan Sands51495602009-02-13 09:42:34 +00001422bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423 bool Changed = false;
1424
Chris Lattner4c1e9542009-03-06 06:45:05 +00001425 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1426 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427
1428 return Changed;
1429}
1430
Devang Patelec9c58f2009-04-01 22:34:41 +00001431/// cleanup - After running all passes, clean up pass manager cache.
1432void FPPassManager::cleanup() {
1433 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1434 FunctionPass *FP = getContainedPass(Index);
1435 AnalysisResolver *AR = FP->getResolver();
1436 assert(AR && "Analysis Resolver is not set");
1437 AR->clearAnalysisImpls();
1438 }
1439}
1440
Torok Edwin24c78352009-06-29 18:49:09 +00001441void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1442 if (!wasRun)
1443 return;
1444 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1445 FPPassManager *FPPM = getContainedManager(Index);
1446 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1447 FPPM->getContainedPass(Index)->releaseMemory();
1448 }
1449 }
Torok Edwin896556e2009-06-29 21:05:10 +00001450 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001451}
1452
Devang Patel67d6a5e2006-12-19 19:46:59 +00001453// Execute all the passes managed by this top level manager.
1454// Return true if any function is modified by a pass.
1455bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001456 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001457 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001458 createDebugInfoProbe();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001459
Devang Patele3068402006-12-21 00:16:50 +00001460 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001461 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1462 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001463
1464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1465 getContainedManager(Index)->cleanup();
1466
Torok Edwin24c78352009-06-29 18:49:09 +00001467 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001468 return Changed;
1469}
1470
1471//===----------------------------------------------------------------------===//
1472// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001473
Devang Patel8c78a0b2007-05-03 01:11:54 +00001474char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001475/// Print passes managed by this manager
1476void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001477 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001478 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1479 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001480 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001481 dumpLastUses(FP, Offset+1);
1482 }
1483}
1484
1485
Dan Gohmande6188a2010-08-12 23:50:08 +00001486/// Execute all of the passes scheduled for execution by invoking
1487/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001488/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001489bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001490 if (F.isDeclaration())
1491 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001492
1493 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001494
Devang Patelcbbf2912008-03-20 01:09:53 +00001495 // Collect inherited analysis from Module level pass manager.
1496 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001497
Devang Patelabfbe3b2006-12-16 00:56:26 +00001498 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1499 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001500 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001501
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001502 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001503 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001504
Devang Patelabfbe3b2006-12-16 00:56:26 +00001505 initializeAnalysisImpl(FP);
Devang Patelfa31d382011-03-10 00:21:25 +00001506 if (TheDebugProbe)
1507 TheDebugProbe->initialize(FP, F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001508 {
1509 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001510 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001511
Dan Gohman74b189f2010-03-01 17:34:28 +00001512 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001513 }
Devang Patelfa31d382011-03-10 00:21:25 +00001514 if (TheDebugProbe)
1515 TheDebugProbe->finalize(FP, F);
Devang Patel93a197c2006-12-14 00:08:04 +00001516
Dan Gohman74b189f2010-03-01 17:34:28 +00001517 Changed |= LocalChanged;
1518 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001519 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001520 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001521
Devang Patela273d1c2007-07-19 18:02:32 +00001522 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001523 removeNotPreservedAnalysis(FP);
1524 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001526 }
1527 return Changed;
1528}
1529
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001531 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001532
Dan Gohmane7630be2010-05-11 20:30:00 +00001533 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001534 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535
Bill Wendling6ce6d262009-12-25 13:50:18 +00001536 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537}
1538
Duncan Sands51495602009-02-13 09:42:34 +00001539bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001540 bool Changed = false;
1541
Chris Lattner4c1e9542009-03-06 06:45:05 +00001542 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1543 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001544
1545 return Changed;
1546}
1547
Duncan Sands51495602009-02-13 09:42:34 +00001548bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001549 bool Changed = false;
1550
Chris Lattner4c1e9542009-03-06 06:45:05 +00001551 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1552 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001553
Devang Patelff631ae2006-11-15 01:27:05 +00001554 return Changed;
1555}
1556
Devang Patela1514cb2006-12-07 19:39:39 +00001557//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001559
Dan Gohmande6188a2010-08-12 23:50:08 +00001560/// Execute all of the passes scheduled for execution by invoking
1561/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001562/// the module, and if so, return true.
1563bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001564MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001565 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001566
Torok Edwin24c78352009-06-29 18:49:09 +00001567 // Initialize on-the-fly passes
1568 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1569 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1570 I != E; ++I) {
1571 FunctionPassManagerImpl *FPP = I->second;
1572 Changed |= FPP->doInitialization(M);
1573 }
1574
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1576 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001577 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001578
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001579 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001580 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001581
Devang Patelabfbe3b2006-12-16 00:56:26 +00001582 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001583
Chris Lattner4c1e9542009-03-06 06:45:05 +00001584 {
1585 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001586 TimeRegion PassTimer(getPassTimer(MP));
1587
Dan Gohman74b189f2010-03-01 17:34:28 +00001588 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001589 }
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Dan Gohman74b189f2010-03-01 17:34:28 +00001591 Changed |= LocalChanged;
1592 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001593 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001594 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001595 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001596
Devang Patela273d1c2007-07-19 18:02:32 +00001597 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001598 removeNotPreservedAnalysis(MP);
1599 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001600 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001601 }
Torok Edwin24c78352009-06-29 18:49:09 +00001602
1603 // Finalize on-the-fly passes
1604 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1605 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1606 I != E; ++I) {
1607 FunctionPassManagerImpl *FPP = I->second;
1608 // We don't know when is the last time an on-the-fly pass is run,
1609 // so we need to releaseMemory / finalize here
1610 FPP->releaseMemoryOnTheFly();
1611 Changed |= FPP->doFinalization(M);
1612 }
Devang Patel05e1a972006-11-07 22:03:15 +00001613 return Changed;
1614}
1615
Devang Patele64d3052007-04-16 20:12:57 +00001616/// Add RequiredPass into list of lower level passes required by pass P.
1617/// RequiredPass is run on the fly by Pass Manager when P requests it
1618/// through getAnalysis interface.
1619void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001620 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1621 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001622 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001623 RequiredPass->getPotentialPassManagerType()) &&
1624 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001625
Devang Patel68f72b12007-04-26 17:50:19 +00001626 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001627 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001628 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001629 // FPP is the top level manager.
1630 FPP->setTopLevelManager(FPP);
1631
Devang Patel69e9f6d2007-04-16 20:27:05 +00001632 OnTheFlyManagers[P] = FPP;
1633 }
Devang Patel68f72b12007-04-26 17:50:19 +00001634 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001635
Devang Patel68f72b12007-04-26 17:50:19 +00001636 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001637 if (RequiredPass) {
1638 SmallVector<Pass *, 1> LU;
1639 LU.push_back(RequiredPass);
1640 FPP->setLastUser(LU, P);
1641 }
Devang Patele64d3052007-04-16 20:12:57 +00001642}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001643
Dan Gohmande6188a2010-08-12 23:50:08 +00001644/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001645/// required by module pass MP. Instantiate analysis pass, by using
1646/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001647Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001648 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001649 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001650
Torok Edwin24c78352009-06-29 18:49:09 +00001651 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001652 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001653 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001654}
1655
1656
Devang Patela1514cb2006-12-07 19:39:39 +00001657//===----------------------------------------------------------------------===//
1658// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001659//
Devang Patelc290c8a2006-11-07 22:23:34 +00001660/// run - Execute all of the passes scheduled for execution. Keep track of
1661/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001662bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001663 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001664 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001665 createDebugInfoProbe();
Devang Patelb8817b92006-12-14 00:59:42 +00001666
Devang Patelcfd70c42006-12-13 22:10:00 +00001667 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001668 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001669
Devang Patele3068402006-12-21 00:16:50 +00001670 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001671 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1672 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001673 return Changed;
1674}
Devang Patel376fefa2006-11-08 10:29:57 +00001675
Devang Patela1514cb2006-12-07 19:39:39 +00001676//===----------------------------------------------------------------------===//
1677// PassManager implementation
1678
Devang Patel376fefa2006-11-08 10:29:57 +00001679/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001680PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001681 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001682 // PM is the top level manager
1683 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001684}
1685
Devang Patelb67904d2006-12-13 02:36:01 +00001686PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001687 delete PM;
1688}
1689
David Greene103d4b42010-05-10 20:24:27 +00001690/// addImpl - Add a pass to the queue of passes to run, without
1691/// checking whether to add a printer pass.
1692void PassManager::addImpl(Pass *P) {
1693 PM->add(P);
1694}
1695
Devang Patel376fefa2006-11-08 10:29:57 +00001696/// add - Add a pass to the queue of passes to run. This passes ownership of
1697/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1698/// will be destroyed as well, so there is no need to delete the pass. This
1699/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001700void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001701 const void* PassID = P->getPassID();
1702 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001703 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1704 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001705
David Greene103d4b42010-05-10 20:24:27 +00001706 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001707
Owen Andersona7aed182010-08-06 18:33:48 +00001708 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001709 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1710 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001711}
1712
1713/// run - Execute all of the passes scheduled for execution. Keep track of
1714/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001715bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001716 return PM->run(M);
1717}
1718
Devang Patelb8817b92006-12-14 00:59:42 +00001719//===----------------------------------------------------------------------===//
1720// TimingInfo Class - This class is used to calculate information about the
1721// amount of time each pass takes to execute. This only happens with
1722// -time-passes is enabled on the command line.
1723//
1724bool llvm::TimePassesIsEnabled = false;
1725static cl::opt<bool,true>
1726EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1727 cl::desc("Time each pass, printing elapsed time for each on exit"));
1728
1729// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1730// a non null value (if the -time-passes option is enabled) or it leaves it
1731// null. It may be called multiple times.
1732void TimingInfo::createTheTimeInfo() {
1733 if (!TimePassesIsEnabled || TheTimeInfo) return;
1734
1735 // Constructed the first time this is called, iff -time-passes is enabled.
1736 // This guarantees that the object will be constructed before static globals,
1737 // thus it will be destroyed before them.
1738 static ManagedStatic<TimingInfo> TTI;
1739 TheTimeInfo = &*TTI;
1740}
1741
Devang Patel1c3633e2007-01-29 23:10:37 +00001742/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001743Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001744 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001745 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001746 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001747}
1748
Devang Patel1c56a632007-01-08 19:29:38 +00001749//===----------------------------------------------------------------------===//
1750// PMStack implementation
1751//
Devang Patelad98d232007-01-11 22:15:30 +00001752
Devang Patel1c56a632007-01-08 19:29:38 +00001753// Pop Pass Manager from the stack and clear its analysis info.
1754void PMStack::pop() {
1755
1756 PMDataManager *Top = this->top();
1757 Top->initializeAnalysisInfo();
1758
1759 S.pop_back();
1760}
1761
1762// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001763void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001764 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001765 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001766
Chris Lattner60987362009-03-06 05:53:14 +00001767 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001768 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1769 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001770 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001771
Chris Lattner60987362009-03-06 05:53:14 +00001772 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001773 TPM->addIndirectPassManager(PM);
1774 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001775 PM->setDepth(this->top()->getDepth()+1);
1776 }
1777 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001778 assert((PM->getPassManagerType() == PMT_ModulePassManager
1779 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001780 && "pushing bad pass manager to PMStack");
1781 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001782 }
1783
Devang Patel15701b52007-01-11 00:19:00 +00001784 S.push_back(PM);
1785}
1786
1787// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001788void PMStack::dump() const {
1789 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001790 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001791 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001792
Devang Patel15701b52007-01-11 00:19:00 +00001793 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001794 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001795}
1796
Devang Patel1c56a632007-01-08 19:29:38 +00001797/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001798/// add self into that manager.
1799void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001800 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001801 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001802 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001803 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1804 if (TopPMType == PreferredType)
1805 break; // We found desired pass manager
1806 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001807 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001808 else
1809 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001810 }
Devang Patel18ff6362008-09-09 21:38:40 +00001811 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001812 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001813}
1814
Devang Patel3312f752007-01-16 21:43:18 +00001815/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001816/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001817void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001818 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001819
Devang Patela3286902008-09-09 17:56:50 +00001820 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001821 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001822 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1823 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001824 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001825 break;
Devang Patel3312f752007-01-16 21:43:18 +00001826 }
Devang Patel3312f752007-01-16 21:43:18 +00001827
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001828 // Create new Function Pass Manager if needed.
1829 FPPassManager *FPP;
1830 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1831 FPP = (FPPassManager *)PMS.top();
1832 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001833 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1834 PMDataManager *PMD = PMS.top();
1835
1836 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001837 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001838 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001839
1840 // [2] Set up new manager's top level manager
1841 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1842 TPM->addIndirectPassManager(FPP);
1843
1844 // [3] Assign manager to manage this new manager. This may create
1845 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001846 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001847
1848 // [4] Push new manager into PMS
1849 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001850 }
1851
Devang Patel3312f752007-01-16 21:43:18 +00001852 // Assign FPP as the manager of this pass.
1853 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001854}
1855
Devang Patel3312f752007-01-16 21:43:18 +00001856/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001857/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001858void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001859 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001860 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001861
Devang Patel15701b52007-01-11 00:19:00 +00001862 // Basic Pass Manager is a leaf pass manager. It does not handle
1863 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001864 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001865 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1866 BBP = (BBPassManager *)PMS.top();
1867 } else {
1868 // If leaf manager is not Basic Block Pass manager then create new
1869 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001870 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1871 PMDataManager *PMD = PMS.top();
1872
1873 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001874 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001875
1876 // [2] Set up new manager's top level manager
1877 // Basic Block Pass Manager does not live by itself
1878 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1879 TPM->addIndirectPassManager(BBP);
1880
Devang Patel15701b52007-01-11 00:19:00 +00001881 // [3] Assign manager to manage this new manager. This may create
1882 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001883 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001884
Devang Patel3312f752007-01-16 21:43:18 +00001885 // [4] Push new manager into PMS
1886 PMS.push(BBP);
1887 }
Devang Patel1c56a632007-01-08 19:29:38 +00001888
Devang Patel3312f752007-01-16 21:43:18 +00001889 // Assign BBP as the manager of this pass.
1890 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001891}
1892
Dan Gohmand3a20c92008-03-11 16:41:42 +00001893PassManagerBase::~PassManagerBase() {}