blob: 88c4186e36293fb434f4bc0e3dfaaa15b13d1c5e [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 {
David Blaikiea379b1812011-12-20 02:50:00 +0000226 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000227private:
228 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000229public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000230 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000231 explicit FunctionPassManagerImpl() :
232 Pass(PT_PassManager, ID), PMDataManager(),
233 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000234
235 /// add - Add a pass to the queue of passes to run. This passes ownership of
236 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
237 /// will be destroyed as well, so there is no need to delete the pass. This
238 /// implies that all passes MUST be allocated with 'new'.
239 void add(Pass *P) {
240 schedulePass(P);
241 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000242
243 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000244 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
245 return createPrintFunctionPass(Banner, &O);
246 }
247
Torok Edwin24c78352009-06-29 18:49:09 +0000248 // Prepare for running an on the fly pass, freeing memory if needed
249 // from a previous run.
250 void releaseMemoryOnTheFly();
251
Devang Patel67d6a5e2006-12-19 19:46:59 +0000252 /// run - Execute all of the passes scheduled for execution. Keep track of
253 /// whether any of the passes modifies the module, and if so, return true.
254 bool run(Function &F);
255
256 /// doInitialization - Run all of the initializers for the function passes.
257 ///
258 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000259
Dan Gohmane6656eb2007-07-30 14:51:13 +0000260 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000261 ///
262 bool doFinalization(Module &M);
263
Dan Gohmande6188a2010-08-12 23:50:08 +0000264
Chris Lattner2fa26e52010-01-22 05:24:46 +0000265 virtual PMDataManager *getAsPMDataManager() { return this; }
266 virtual Pass *getAsPass() { return this; }
267
Devang Patel67d6a5e2006-12-19 19:46:59 +0000268 /// Pass Manager itself does not invalidate any analysis info.
269 void getAnalysisUsage(AnalysisUsage &Info) const {
270 Info.setPreservesAll();
271 }
272
Dan Gohman30614852010-08-16 21:57:30 +0000273 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000274 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000275 // P is a immutable pass and it will be managed by this
276 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000277 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000278 P->setResolver(AR);
279 initializeAnalysisImpl(P);
280 addImmutablePass(IP);
281 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000282 } else {
David Greene103d4b42010-05-10 20:24:27 +0000283 P->assignPassManager(activeStack, PMT_FunctionPassManager);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284 }
Devang Patel0f080042007-01-12 17:23:48 +0000285
Devang Patel67d6a5e2006-12-19 19:46:59 +0000286 }
287
288 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000289 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
291 return FP;
292 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000293};
294
David Blaikiea379b1812011-12-20 02:50:00 +0000295void FunctionPassManagerImpl::anchor() {}
296
Devang Patel8c78a0b2007-05-03 01:11:54 +0000297char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000298
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299//===----------------------------------------------------------------------===//
300// MPPassManager
301//
302/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000303/// It batches all Module passes and function pass managers together and
304/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000305class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000306public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000307 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000308 explicit MPPassManager() :
309 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000310
311 // Delete on the fly managers.
312 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000313 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000314 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
315 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000316 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000317 delete FPP;
318 }
319 }
320
Dan Gohmande6188a2010-08-12 23:50:08 +0000321 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000322 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
323 return createPrintModulePass(&O, false, Banner);
324 }
325
Devang Patelca58e352006-11-08 10:05:38 +0000326 /// run - Execute all of the passes scheduled for execution. Keep track of
327 /// whether any of the passes modifies the module, and if so, return true.
328 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000329
Devang Patelf9d96b92006-12-07 19:57:52 +0000330 /// Pass Manager itself does not invalidate any analysis info.
331 void getAnalysisUsage(AnalysisUsage &Info) const {
332 Info.setPreservesAll();
333 }
334
Devang Patele64d3052007-04-16 20:12:57 +0000335 /// Add RequiredPass into list of lower level passes required by pass P.
336 /// RequiredPass is run on the fly by Pass Manager when P requests it
337 /// through getAnalysis interface.
338 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
339
Dan Gohmande6188a2010-08-12 23:50:08 +0000340 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000341 /// required by module pass MP. Instantiate analysis pass, by using
342 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000343 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000344
Devang Patele3858e62007-02-01 22:08:25 +0000345 virtual const char *getPassName() const {
346 return "Module Pass Manager";
347 }
348
Chris Lattner2fa26e52010-01-22 05:24:46 +0000349 virtual PMDataManager *getAsPMDataManager() { return this; }
350 virtual Pass *getAsPass() { return this; }
351
Devang Pateleda56172006-12-12 23:34:33 +0000352 // Print passes managed by this manager
353 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer4dd515c2011-08-29 18:14:17 +0000354 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000355 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
356 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000357 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000358 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
359 OnTheFlyManagers.find(MP);
360 if (I != OnTheFlyManagers.end())
361 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000363 }
364 }
365
Devang Patelabfbe3b2006-12-16 00:56:26 +0000366 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000367 assert(N < PassVector.size() && "Pass number out of range!");
368 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000369 }
370
Dan Gohmande6188a2010-08-12 23:50:08 +0000371 virtual PassManagerType getPassManagerType() const {
372 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000373 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000374
375 private:
376 /// Collection of on the fly FPPassManagers. These managers manage
377 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000378 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000379};
380
Devang Patel8c78a0b2007-05-03 01:11:54 +0000381char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000382//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000383// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000384//
Devang Patel09f162c2007-05-01 21:15:47 +0000385
Devang Patel67d6a5e2006-12-19 19:46:59 +0000386/// PassManagerImpl manages MPPassManagers
387class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000388 public PMDataManager,
389 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000390 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000391
392public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000393 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000394 explicit PassManagerImpl() :
395 Pass(PT_PassManager, ID), PMDataManager(),
396 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000397
Devang Patel376fefa2006-11-08 10:29:57 +0000398 /// add - Add a pass to the queue of passes to run. This passes ownership of
399 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
400 /// will be destroyed as well, so there is no need to delete the pass. This
401 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000402 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000403 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000404 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000405
406 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000407 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
408 return createPrintModulePass(&O, false, Banner);
409 }
410
Devang Patel376fefa2006-11-08 10:29:57 +0000411 /// run - Execute all of the passes scheduled for execution. Keep track of
412 /// whether any of the passes modifies the module, and if so, return true.
413 bool run(Module &M);
414
Devang Patelf9d96b92006-12-07 19:57:52 +0000415 /// Pass Manager itself does not invalidate any analysis info.
416 void getAnalysisUsage(AnalysisUsage &Info) const {
417 Info.setPreservesAll();
418 }
419
Dan Gohman30614852010-08-16 21:57:30 +0000420 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000421 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000422 // P is a immutable pass and it will be managed by this
423 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000424 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000425 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000426 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000427 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000428 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000429 } else {
David Greene103d4b42010-05-10 20:24:27 +0000430 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000431 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000432 }
433
Chris Lattner2fa26e52010-01-22 05:24:46 +0000434 virtual PMDataManager *getAsPMDataManager() { return this; }
435 virtual Pass *getAsPass() { return this; }
436
Devang Patel67d6a5e2006-12-19 19:46:59 +0000437 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000438 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000439 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
440 return MP;
441 }
Devang Patel376fefa2006-11-08 10:29:57 +0000442};
443
David Blaikiea379b1812011-12-20 02:50:00 +0000444void PassManagerImpl::anchor() {}
445
Devang Patel8c78a0b2007-05-03 01:11:54 +0000446char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000447} // End of llvm namespace
448
449namespace {
450
451//===----------------------------------------------------------------------===//
Devang Patelfa31d382011-03-10 00:21:25 +0000452// DebugInfoProbe
453
454static DebugInfoProbeInfo *TheDebugProbe;
455static void createDebugInfoProbe() {
456 if (TheDebugProbe) return;
Andrew Trickb3bddf02011-06-03 00:44:32 +0000457
458 // Constructed the first time this is called. This guarantees that the
459 // object will be constructed, if -enable-debug-info-probe is set,
Devang Patelfa31d382011-03-10 00:21:25 +0000460 // before static globals, thus it will be destroyed before them.
461 static ManagedStatic<DebugInfoProbeInfo> DIP;
462 TheDebugProbe = &*DIP;
463}
464
465//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000466/// TimingInfo Class - This class is used to calculate information about the
467/// amount of time each pass takes to execute. This only happens when
468/// -time-passes is enabled on the command line.
469///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000470
Owen Anderson5a6960f2009-06-18 20:51:00 +0000471static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000472
Nick Lewycky02d5f772009-10-25 06:33:48 +0000473class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000474 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000475 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000476public:
477 // Use 'create' member to get this.
478 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000479
Devang Patel1c3633e2007-01-29 23:10:37 +0000480 // TimingDtor - Print out information about timing information
481 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000482 // Delete all of the timers, which accumulate their info into the
483 // TimerGroup.
484 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
485 E = TimingData.end(); I != E; ++I)
486 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000487 // TimerGroup is deleted next, printing the report.
488 }
489
490 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
491 // to a non null value (if the -time-passes option is enabled) or it leaves it
492 // null. It may be called multiple times.
493 static void createTheTimeInfo();
494
Chris Lattner707431c2010-03-30 04:03:22 +0000495 /// getPassTimer - Return the timer for the specified pass if it exists.
496 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000497 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000498 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000499
Owen Anderson5c96ef72009-07-07 18:33:04 +0000500 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000501 Timer *&T = TimingData[P];
502 if (T == 0)
503 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000504 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000505 }
506};
507
Devang Patel1c3633e2007-01-29 23:10:37 +0000508} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000509
Dan Gohmand78c4002008-05-13 00:00:25 +0000510static TimingInfo *TheTimeInfo;
511
Devang Patela1514cb2006-12-07 19:39:39 +0000512//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000513// PMTopLevelManager implementation
514
Devang Patel4268fc02007-01-16 02:00:38 +0000515/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000516PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
517 PMDM->setTopLevelManager(this);
518 addPassManager(PMDM);
519 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000520}
521
Devang Patelafb1f3622006-12-12 22:35:25 +0000522/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000523void
524PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
525 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000526 unsigned PDepth = 0;
527 if (P->getResolver())
528 PDepth = P->getResolver()->getPMDataManager().getDepth();
529
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000530 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000531 E = AnalysisPasses.end(); I != E; ++I) {
532 Pass *AP = *I;
533 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000534
Devang Patel01919d22007-03-08 19:05:01 +0000535 if (P == AP)
536 continue;
537
Tobias Grosserf07426b2011-01-20 21:03:22 +0000538 // Update the last users of passes that are required transitive by AP.
539 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
540 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
541 SmallVector<Pass *, 12> LastUses;
542 SmallVector<Pass *, 12> LastPMUses;
543 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
544 E = IDs.end(); I != E; ++I) {
545 Pass *AnalysisPass = findAnalysisPass(*I);
546 assert(AnalysisPass && "Expected analysis pass to exist.");
547 AnalysisResolver *AR = AnalysisPass->getResolver();
548 assert(AR && "Expected analysis resolver to exist.");
549 unsigned APDepth = AR->getPMDataManager().getDepth();
550
551 if (PDepth == APDepth)
552 LastUses.push_back(AnalysisPass);
553 else if (PDepth > APDepth)
554 LastPMUses.push_back(AnalysisPass);
555 }
556
557 setLastUser(LastUses, P);
558
559 // If this pass has a corresponding pass manager, push higher level
560 // analysis to this pass manager.
561 if (P->getResolver())
562 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
563
564
Devang Patelafb1f3622006-12-12 22:35:25 +0000565 // If AP is the last user of other passes then make P last user of
566 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000567 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000568 LUE = LastUser.end(); LUI != LUE; ++LUI) {
569 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000570 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000571 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000572 LastUser[LUI->first] = P;
573 }
574 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000575}
576
577/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000578void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000579 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000580 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000581 InversedLastUser.find(P);
582 if (DMI == InversedLastUser.end())
583 return;
584
585 SmallPtrSet<Pass *, 8> &LU = DMI->second;
586 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
587 E = LU.end(); I != E; ++I) {
588 LastUses.push_back(*I);
589 }
590
Devang Patelafb1f3622006-12-12 22:35:25 +0000591}
592
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000593AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
594 AnalysisUsage *AnUsage = NULL;
595 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000596 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000597 AnUsage = DMI->second;
598 else {
599 AnUsage = new AnalysisUsage();
600 P->getAnalysisUsage(*AnUsage);
601 AnUsageMap[P] = AnUsage;
602 }
603 return AnUsage;
604}
605
Devang Patelafb1f3622006-12-12 22:35:25 +0000606/// Schedule pass P for execution. Make sure that passes required by
607/// P are run before P is run. Update analysis info maintained by
608/// the manager. Remove dead passes. This is a recursive function.
609void PMTopLevelManager::schedulePass(Pass *P) {
610
Devang Patel3312f752007-01-16 21:43:18 +0000611 // TODO : Allocate function manager for this pass, other wise required set
612 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000613
Devang Pateld74ede72007-03-06 01:06:16 +0000614 // Give pass a chance to prepare the stage.
615 P->preparePassManager(activeStack);
616
Devang Patel864970e2008-03-18 00:39:19 +0000617 // If P is an analysis pass and it is available then do not
618 // generate the analysis again. Stale analysis info should not be
619 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000620 const PassInfo *PI =
621 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
622 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000623 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000624 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000625 }
Devang Patel864970e2008-03-18 00:39:19 +0000626
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000627 AnalysisUsage *AnUsage = findAnalysisUsage(P);
628
Devang Patelfdee7032008-08-14 23:07:48 +0000629 bool checkAnalysis = true;
630 while (checkAnalysis) {
631 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000632
Devang Patelfdee7032008-08-14 23:07:48 +0000633 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
634 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
635 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000636
Devang Patelfdee7032008-08-14 23:07:48 +0000637 Pass *AnalysisPass = findAnalysisPass(*I);
638 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000639 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000640 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000641 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000642 if (P->getPotentialPassManagerType () ==
643 AnalysisPass->getPotentialPassManagerType())
644 // Schedule analysis pass that is managed by the same pass manager.
645 schedulePass(AnalysisPass);
646 else if (P->getPotentialPassManagerType () >
647 AnalysisPass->getPotentialPassManagerType()) {
648 // Schedule analysis pass that is managed by a new manager.
649 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000650 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000651 // are already checked are still available.
652 checkAnalysis = true;
653 }
654 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000655 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000656 // passes are run on the fly.
657 delete AnalysisPass;
658 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000659 }
660 }
661
662 // Now all required passes are available.
663 addTopLevelPass(P);
664}
665
666/// Find the pass that implements Analysis AID. Search immutable
667/// passes and all pass managers. If desired pass is not found
668/// then return NULL.
669Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
670
Devang Patelcd6ba152006-12-12 22:50:05 +0000671 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000672 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000673 E = PassManagers.end(); I != E; ++I)
674 if (Pass *P = (*I)->findAnalysisPass(AID, false))
675 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000676
677 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000678 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000679 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000680 E = IndirectPassManagers.end(); I != E; ++I)
681 if (Pass *P = (*I)->findAnalysisPass(AID, false))
682 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000683
Dan Gohman844dd0a2010-10-11 23:19:01 +0000684 // Check the immutable passes. Iterate in reverse order so that we find
685 // the most recently registered passes first.
686 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
687 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000688 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000689 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000690 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000691
692 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000693 const PassInfo *PassInf =
694 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000695 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000696 const std::vector<const PassInfo*> &ImmPI =
697 PassInf->getInterfacesImplemented();
698 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
699 EE = ImmPI.end(); II != EE; ++II) {
700 if ((*II)->getTypeInfo() == AID)
701 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000702 }
703 }
704
Dan Gohman844dd0a2010-10-11 23:19:01 +0000705 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000706}
707
Devang Pateleda56172006-12-12 23:34:33 +0000708// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000709void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000710
Devang Patelfd4184322007-01-17 20:33:36 +0000711 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000712 return;
713
Devang Pateleda56172006-12-12 23:34:33 +0000714 // Print out the immutable passes
715 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000716 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000717 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000718
Dan Gohmanf71c5212010-08-19 01:29:07 +0000719 // Every class that derives from PMDataManager also derives from Pass
720 // (sometimes indirectly), but there's no inheritance relationship
721 // between PMDataManager and Pass, so we have to getAsPass to get
722 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000723 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000724 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000725 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000726}
727
Devang Patel991aeba2006-12-15 20:13:01 +0000728void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000729
Devang Patelfd4184322007-01-17 20:33:36 +0000730 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000731 return;
732
David Greene994e1bb2010-01-05 01:30:02 +0000733 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000734 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
735 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
736 if (const PassInfo *PI =
Andrew Trick6bbaf132011-06-03 00:48:58 +0000737 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
738 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000739 if (!PI->isAnalysisGroup())
740 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000741 }
Devang Patel0d29ae02008-08-12 15:44:31 +0000742 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000743 E = PassManagers.end(); I != E; ++I)
744 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000745 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000746}
747
Devang Patele3068402006-12-21 00:16:50 +0000748void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000749 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000750 E = PassManagers.end(); I != E; ++I)
751 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000752
Devang Patele3068402006-12-21 00:16:50 +0000753 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000754 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000755 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
756 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000757 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000758
Chris Lattner60987362009-03-06 05:53:14 +0000759 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000760 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000761 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000762 InversedLastUser.find(DMI->second);
763 if (InvDMI != InversedLastUser.end()) {
764 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
765 L.insert(DMI->first);
766 } else {
767 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
768 InversedLastUser[DMI->second] = L;
769 }
770 }
Devang Patele3068402006-12-21 00:16:50 +0000771}
772
Devang Patele7599552007-01-12 18:52:44 +0000773/// Destructor
774PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000775 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000776 E = PassManagers.end(); I != E; ++I)
777 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000778
Dan Gohman060d5ba2010-10-12 00:15:27 +0000779 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000780 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
781 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000782
783 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000784 DME = AnUsageMap.end(); DMI != DME; ++DMI)
785 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000786}
787
Devang Patelafb1f3622006-12-12 22:35:25 +0000788//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000789// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000790
Devang Patel643676c2006-11-11 01:10:19 +0000791/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000792void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000793 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000794
Chris Lattner60987362009-03-06 05:53:14 +0000795 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000796
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000797 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000798
Dan Gohmande6188a2010-08-12 23:50:08 +0000799 // This pass is the current implementation of all of the interfaces it
800 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000801 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
802 if (PInf == 0) return;
803 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000804 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000805 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000806}
807
Devang Patel9d9fc902007-03-06 17:52:53 +0000808// Return true if P preserves high level analysis used by other
809// passes managed by this manager
810bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000811 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000812 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000813 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000814
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000815 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000816 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000817 E = HigherLevelAnalysis.end(); I != E; ++I) {
818 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000819 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000820 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000821 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000822 PreservedSet.end())
823 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000824 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000825
Devang Patel9d9fc902007-03-06 17:52:53 +0000826 return true;
827}
828
Chris Lattner02eb94c2008-08-07 07:34:50 +0000829/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000830void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000831 // Don't do this unless assertions are enabled.
832#ifdef NDEBUG
833 return;
834#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000835 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
836 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000837
Devang Patelef432532007-07-19 05:36:09 +0000838 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000839 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000840 E = PreservedSet.end(); I != E; ++I) {
841 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000842 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000843 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000844 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000845 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000846 }
847}
848
Devang Patel67c79a42008-07-01 19:50:56 +0000849/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000850void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000851 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
852 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000853 return;
854
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000855 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000856 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000857 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000858 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000859 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000860 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000861 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000862 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000863 if (PassDebugging >= Details) {
864 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000865 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
866 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000867 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000868 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000869 }
Devang Patel349170f2006-11-11 01:24:55 +0000870 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000871
Devang Patel42dd1e92007-03-06 01:55:46 +0000872 // Check inherited analysis also. If P is not preserving analysis
873 // provided by parent manager then remove it here.
874 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
875
876 if (!InheritedAnalysis[Index])
877 continue;
878
Dan Gohmande6188a2010-08-12 23:50:08 +0000879 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000880 I = InheritedAnalysis[Index]->begin(),
881 E = InheritedAnalysis[Index]->end(); I != E; ) {
882 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000883 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000884 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000885 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000886 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000887 if (PassDebugging >= Details) {
888 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000889 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
890 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000891 }
Devang Patel01919d22007-03-08 19:05:01 +0000892 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000893 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000894 }
895 }
Devang Patelf68a3492006-11-07 22:35:17 +0000896}
897
Devang Patelca189262006-11-14 03:05:08 +0000898/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000899void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000900 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000901
Devang Patel8adae862007-07-20 18:04:54 +0000902 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000903
Devang Patel2ff44922007-04-16 20:39:59 +0000904 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000905 if (!TPM)
906 return;
907
Devang Patel17ad0962006-12-08 00:37:52 +0000908 TPM->collectLastUses(DeadPasses, P);
909
Devang Patel656a9172008-06-06 17:50:36 +0000910 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000911 dbgs() << " -*- '" << P->getPassName();
912 dbgs() << "' is the last user of following pass instances.";
913 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000914 }
915
Dan Gohman060d5ba2010-10-12 00:15:27 +0000916 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000917 E = DeadPasses.end(); I != E; ++I)
918 freePass(*I, Msg, DBG_STR);
919}
Devang Patel200d3052006-12-13 23:50:44 +0000920
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000921void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000922 enum PassDebuggingString DBG_STR) {
923 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000924
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000925 {
926 // If the pass crashes releasing memory, remember this.
927 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000928 TimeRegion PassTimer(getPassTimer(P));
929
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000930 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000931 }
932
Owen Andersona7aed182010-08-06 18:33:48 +0000933 AnalysisID PI = P->getPassID();
934 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000935 // Remove the pass itself (if it is not already removed).
936 AvailableAnalysis.erase(PI);
937
938 // Remove all interfaces this pass implements, for which it is also
939 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000940 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000941 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000942 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000943 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000944 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000945 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000946 }
Devang Patel17ad0962006-12-08 00:37:52 +0000947 }
Devang Patelca189262006-11-14 03:05:08 +0000948}
949
Dan Gohmande6188a2010-08-12 23:50:08 +0000950/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000951/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000952void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000953 // This manager is going to manage pass P. Set up analysis resolver
954 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000955 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000956 P->setResolver(AR);
957
Devang Patelec2b9a72007-03-05 22:57:49 +0000958 // If a FunctionPass F is the last user of ModulePass info M
959 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000960 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000961
Chris Lattner60987362009-03-06 05:53:14 +0000962 if (!ProcessAnalysis) {
963 // Add pass
964 PassVector.push_back(P);
965 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000966 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000967
Chris Lattner60987362009-03-06 05:53:14 +0000968 // At the moment, this pass is the last user of all required passes.
969 SmallVector<Pass *, 12> LastUses;
970 SmallVector<Pass *, 8> RequiredPasses;
971 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
972
973 unsigned PDepth = this->getDepth();
974
Dan Gohmande6188a2010-08-12 23:50:08 +0000975 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000976 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000977 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000978 E = RequiredPasses.end(); I != E; ++I) {
979 Pass *PRequired = *I;
980 unsigned RDepth = 0;
981
982 assert(PRequired->getResolver() && "Analysis Resolver is not set");
983 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
984 RDepth = DM.getDepth();
985
986 if (PDepth == RDepth)
987 LastUses.push_back(PRequired);
988 else if (PDepth > RDepth) {
989 // Let the parent claim responsibility of last use
990 TransferLastUses.push_back(PRequired);
991 // Keep track of higher level analysis used by this manager.
992 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000993 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000994 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000995 }
996
997 // Set P as P's last user until someone starts using P.
998 // However, if P is a Pass Manager then it does not need
999 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +00001000 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +00001001 LastUses.push_back(P);
1002 TPM->setLastUser(LastUses, P);
1003
1004 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001005 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001006 TPM->setLastUser(TransferLastUses, My_PM);
1007 TransferLastUses.clear();
1008 }
1009
Dan Gohman6304db32010-08-16 22:57:28 +00001010 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001011 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001012 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001013 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001014 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1015 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001016 this->addLowerLevelRequiredPass(P, AnalysisPass);
1017 }
1018
1019 // Take a note of analysis required and made available by this pass.
1020 // Remove the analysis not preserved by this pass
1021 removeNotPreservedAnalysis(P);
1022 recordAvailableAnalysis(P);
1023
Devang Patel8cad70d2006-11-11 01:51:02 +00001024 // Add pass
1025 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001026}
1027
Devang Patele64d3052007-04-16 20:12:57 +00001028
1029/// Populate RP with analysis pass that are required by
1030/// pass P and are available. Populate RP_NotAvail with analysis
1031/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001032void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1033 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001034 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001035 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1036 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001037 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001038 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001039 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001040 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001041 else
Chris Lattner60987362009-03-06 05:53:14 +00001042 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001043 }
Devang Patelf58183d2006-12-12 23:09:32 +00001044
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001045 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001046 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001047 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001048 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001049 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001050 else
Chris Lattner60987362009-03-06 05:53:14 +00001051 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001052 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001053}
1054
Devang Patel07f4f582006-11-14 21:49:36 +00001055// All Required analyses should be available to the pass as it runs! Here
1056// we fill in the AnalysisImpls member of the pass so that it can
1057// successfully use the getAnalysis() method to retrieve the
1058// implementations it needs.
1059//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001060void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001061 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1062
Chris Lattnercbd160f2008-08-08 05:33:04 +00001063 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001064 I = AnUsage->getRequiredSet().begin(),
1065 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001066 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001067 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001068 // This may be analysis pass that is initialized on the fly.
1069 // If that is not the case then it will raise an assert when it is used.
1070 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001071 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001072 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001073 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001074 }
1075}
1076
Devang Patel640c5bb2006-12-08 22:30:11 +00001077/// Find the pass that implements Analysis AID. If desired pass is not found
1078/// then return NULL.
1079Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1080
1081 // Check if AvailableAnalysis map has one entry.
1082 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1083
1084 if (I != AvailableAnalysis.end())
1085 return I->second;
1086
1087 // Search Parents through TopLevelManager
1088 if (SearchParent)
1089 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001090
Devang Patel9d759b82006-12-09 00:09:12 +00001091 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001092}
1093
Devang Patel991aeba2006-12-15 20:13:01 +00001094// Print list of passes that are last used by P.
1095void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1096
Devang Patel8adae862007-07-20 18:04:54 +00001097 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001098
1099 // If this is a on the fly manager then it does not have TPM.
1100 if (!TPM)
1101 return;
1102
Devang Patel991aeba2006-12-15 20:13:01 +00001103 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001104
Dan Gohman7224bce2010-10-12 00:11:18 +00001105 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001106 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001107 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001108 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001109 }
1110}
1111
1112void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001113 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001114 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001115 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001116 PMD->dumpPassArguments();
1117 else
Owen Andersona7aed182010-08-06 18:33:48 +00001118 if (const PassInfo *PI =
1119 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001120 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001121 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001122 }
1123}
1124
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001125void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1126 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001127 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001128 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001129 return;
David Greene994e1bb2010-01-05 01:30:02 +00001130 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001131 switch (S1) {
1132 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001134 break;
1135 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001136 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001137 break;
1138 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001139 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001140 break;
1141 default:
1142 break;
1143 }
1144 switch (S2) {
1145 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
1148 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001154 case ON_REGION_MSG:
1155 dbgs() << "' on Region '" << Msg << "'...\n";
1156 break;
Devang Patel003a5592007-03-05 20:01:30 +00001157 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001158 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001159 break;
1160 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001161 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001162 break;
1163 default:
1164 break;
1165 }
Devang Patel991aeba2006-12-15 20:13:01 +00001166}
1167
Chris Lattner4c1e9542009-03-06 06:45:05 +00001168void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001169 if (PassDebugging < Details)
1170 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001171
Chris Lattner4c493d92008-08-08 15:14:09 +00001172 AnalysisUsage analysisUsage;
1173 P->getAnalysisUsage(analysisUsage);
1174 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1175}
1176
Chris Lattner4c1e9542009-03-06 06:45:05 +00001177void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001178 if (PassDebugging < Details)
1179 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001180
Chris Lattner4c493d92008-08-08 15:14:09 +00001181 AnalysisUsage analysisUsage;
1182 P->getAnalysisUsage(analysisUsage);
1183 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1184}
1185
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001186void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001187 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001188 assert(PassDebugging >= Details);
1189 if (Set.empty())
1190 return;
David Greene994e1bb2010-01-05 01:30:02 +00001191 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001192 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001193 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001194 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001195 if (!PInf) {
1196 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1197 // all drivers.
1198 dbgs() << " Uninitialized Pass";
1199 continue;
1200 }
Owen Andersona7aed182010-08-06 18:33:48 +00001201 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001202 }
David Greene994e1bb2010-01-05 01:30:02 +00001203 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001204}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001205
Devang Patel004937b2007-07-27 20:06:09 +00001206/// Add RequiredPass into list of lower level passes required by pass P.
1207/// RequiredPass is run on the fly by Pass Manager when P requests it
1208/// through getAnalysis interface.
1209/// This should be handled by specific pass manager.
1210void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1211 if (TPM) {
1212 TPM->dumpArguments();
1213 TPM->dumpPasses();
1214 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001215
Dan Gohmande6188a2010-08-12 23:50:08 +00001216 // Module Level pass may required Function Level analysis info
1217 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1218 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001219 // module level pass is requiring lower level analysis info managed by
1220 // lower level pass manager.
1221
1222 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001223 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001224 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001225#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001226 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1227 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001228#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001229 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001230}
1231
Owen Andersona7aed182010-08-06 18:33:48 +00001232Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001233 assert(0 && "Unable to find on the fly pass");
1234 return NULL;
1235}
1236
Devang Patele7599552007-01-12 18:52:44 +00001237// Destructor
1238PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001239 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001240 E = PassVector.end(); I != E; ++I)
1241 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001242}
1243
Devang Patel9bdf7d42006-12-08 23:28:54 +00001244//===----------------------------------------------------------------------===//
1245// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001246// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1247Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001248 return PM.findAnalysisPass(ID, dir);
1249}
1250
Dan Gohmande6188a2010-08-12 23:50:08 +00001251Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001252 Function &F) {
1253 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1254}
1255
Devang Patela1514cb2006-12-07 19:39:39 +00001256//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001257// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001258
Dan Gohmande6188a2010-08-12 23:50:08 +00001259/// Execute all of the passes scheduled for execution by invoking
1260/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001261/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001262bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001263 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001264 return false;
1265
Devang Patele9585592006-12-08 01:38:28 +00001266 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001267
Devang Patel6e5a1132006-11-07 21:31:57 +00001268 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001269 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1270 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001271 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001272
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001273 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001274 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001275
Devang Patelabfbe3b2006-12-16 00:56:26 +00001276 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001277
Chris Lattner4c1e9542009-03-06 06:45:05 +00001278 {
1279 // If the pass crashes, remember this.
1280 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001281 TimeRegion PassTimer(getPassTimer(BP));
1282
Dan Gohman74b189f2010-03-01 17:34:28 +00001283 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001284 }
Devang Patel93a197c2006-12-14 00:08:04 +00001285
Dan Gohman74b189f2010-03-01 17:34:28 +00001286 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001287 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001288 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001289 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001290 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001291
Devang Patela273d1c2007-07-19 18:02:32 +00001292 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001293 removeNotPreservedAnalysis(BP);
1294 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001295 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001296 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001297
Bill Wendling6ce6d262009-12-25 13:50:18 +00001298 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001299}
1300
Devang Patel475c4532006-12-08 00:59:05 +00001301// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001302bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001303 bool Changed = false;
1304
Chris Lattner4c1e9542009-03-06 06:45:05 +00001305 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1306 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001307
1308 return Changed;
1309}
1310
Duncan Sands51495602009-02-13 09:42:34 +00001311bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001312 bool Changed = false;
1313
Chris Lattner4c1e9542009-03-06 06:45:05 +00001314 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1315 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001316
1317 return Changed;
1318}
1319
Duncan Sands51495602009-02-13 09:42:34 +00001320bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001321 bool Changed = false;
1322
Devang Patelabfbe3b2006-12-16 00:56:26 +00001323 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1324 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001325 Changed |= BP->doInitialization(F);
1326 }
1327
1328 return Changed;
1329}
1330
Duncan Sands51495602009-02-13 09:42:34 +00001331bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001332 bool Changed = false;
1333
Devang Patelabfbe3b2006-12-16 00:56:26 +00001334 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1335 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001336 Changed |= BP->doFinalization(F);
1337 }
1338
1339 return Changed;
1340}
1341
1342
Devang Patela1514cb2006-12-07 19:39:39 +00001343//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001344// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001345
Devang Patel4e12f862006-11-08 10:44:40 +00001346/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001347FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001348 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001349 // FPM is the top level manager.
1350 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001351
Dan Gohman565df952008-03-13 02:08:36 +00001352 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001353 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001354}
1355
Devang Patelb67904d2006-12-13 02:36:01 +00001356FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001357 delete FPM;
1358}
1359
David Greene103d4b42010-05-10 20:24:27 +00001360/// addImpl - Add a pass to the queue of passes to run, without
1361/// checking whether to add a printer pass.
1362void FunctionPassManager::addImpl(Pass *P) {
1363 FPM->add(P);
1364}
1365
Devang Patel4e12f862006-11-08 10:44:40 +00001366/// add - Add a pass to the queue of passes to run. This passes
1367/// ownership of the Pass to the PassManager. When the
1368/// PassManager_X is destroyed, the pass will be destroyed as well, so
1369/// there is no need to delete the pass. (TODO delete passes.)
1370/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001371void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001372 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001373 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001374 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001375 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001376 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1377 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001378
David Greene103d4b42010-05-10 20:24:27 +00001379 addImpl(P);
1380
1381 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001382 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001383 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1384 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001385}
1386
Devang Patel9f3083e2006-11-15 19:39:54 +00001387/// run - Execute all of the passes scheduled for execution. Keep
1388/// track of whether any of the passes modifies the function, and if
1389/// so, return true.
1390///
Devang Patelb67904d2006-12-13 02:36:01 +00001391bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001392 if (F.isMaterializable()) {
1393 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001394 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001395 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001396 }
Devang Patel272908d2006-12-08 22:57:48 +00001397 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001398}
1399
1400
Devang Patelff631ae2006-11-15 01:27:05 +00001401/// doInitialization - Run all of the initializers for the function passes.
1402///
Devang Patelb67904d2006-12-13 02:36:01 +00001403bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001404 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001405}
1406
Dan Gohmane6656eb2007-07-30 14:51:13 +00001407/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001408///
Devang Patelb67904d2006-12-13 02:36:01 +00001409bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001410 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001411}
1412
Devang Patela1514cb2006-12-07 19:39:39 +00001413//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001414// FunctionPassManagerImpl implementation
1415//
Duncan Sands51495602009-02-13 09:42:34 +00001416bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001417 bool Changed = false;
1418
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001419 dumpArguments();
1420 dumpPasses();
1421
Chris Lattner4c1e9542009-03-06 06:45:05 +00001422 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1423 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424
1425 return Changed;
1426}
1427
Duncan Sands51495602009-02-13 09:42:34 +00001428bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001429 bool Changed = false;
1430
Chris Lattner4c1e9542009-03-06 06:45:05 +00001431 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1432 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001433
1434 return Changed;
1435}
1436
Devang Patelec9c58f2009-04-01 22:34:41 +00001437/// cleanup - After running all passes, clean up pass manager cache.
1438void FPPassManager::cleanup() {
1439 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1440 FunctionPass *FP = getContainedPass(Index);
1441 AnalysisResolver *AR = FP->getResolver();
1442 assert(AR && "Analysis Resolver is not set");
1443 AR->clearAnalysisImpls();
1444 }
1445}
1446
Torok Edwin24c78352009-06-29 18:49:09 +00001447void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1448 if (!wasRun)
1449 return;
1450 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1451 FPPassManager *FPPM = getContainedManager(Index);
1452 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1453 FPPM->getContainedPass(Index)->releaseMemory();
1454 }
1455 }
Torok Edwin896556e2009-06-29 21:05:10 +00001456 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001457}
1458
Devang Patel67d6a5e2006-12-19 19:46:59 +00001459// Execute all the passes managed by this top level manager.
1460// Return true if any function is modified by a pass.
1461bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001462 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001463 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001464 createDebugInfoProbe();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001465
Devang Patele3068402006-12-21 00:16:50 +00001466 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001467 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1468 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001469
1470 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1471 getContainedManager(Index)->cleanup();
1472
Torok Edwin24c78352009-06-29 18:49:09 +00001473 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001474 return Changed;
1475}
1476
1477//===----------------------------------------------------------------------===//
1478// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001479
Devang Patel8c78a0b2007-05-03 01:11:54 +00001480char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001481/// Print passes managed by this manager
1482void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001483 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001484 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1485 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001486 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001487 dumpLastUses(FP, Offset+1);
1488 }
1489}
1490
1491
Dan Gohmande6188a2010-08-12 23:50:08 +00001492/// Execute all of the passes scheduled for execution by invoking
1493/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001494/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001495bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001496 if (F.isDeclaration())
1497 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001498
1499 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001500
Devang Patelcbbf2912008-03-20 01:09:53 +00001501 // Collect inherited analysis from Module level pass manager.
1502 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001503
Devang Patelabfbe3b2006-12-16 00:56:26 +00001504 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1505 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001506 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001507
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001508 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001509 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001510
Devang Patelabfbe3b2006-12-16 00:56:26 +00001511 initializeAnalysisImpl(FP);
Devang Patelfa31d382011-03-10 00:21:25 +00001512 if (TheDebugProbe)
1513 TheDebugProbe->initialize(FP, F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001514 {
1515 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001516 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001517
Dan Gohman74b189f2010-03-01 17:34:28 +00001518 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001519 }
Devang Patelfa31d382011-03-10 00:21:25 +00001520 if (TheDebugProbe)
1521 TheDebugProbe->finalize(FP, F);
Devang Patel93a197c2006-12-14 00:08:04 +00001522
Dan Gohman74b189f2010-03-01 17:34:28 +00001523 Changed |= LocalChanged;
1524 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001526 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001527
Devang Patela273d1c2007-07-19 18:02:32 +00001528 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001529 removeNotPreservedAnalysis(FP);
1530 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001531 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001532 }
1533 return Changed;
1534}
1535
Devang Patel67d6a5e2006-12-19 19:46:59 +00001536bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001538
Dan Gohmane7630be2010-05-11 20:30:00 +00001539 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendlingd12cec82011-08-08 23:01:10 +00001540 Changed |= runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001541
Bill Wendling6ce6d262009-12-25 13:50:18 +00001542 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001543}
1544
Duncan Sands51495602009-02-13 09:42:34 +00001545bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001546 bool Changed = false;
1547
Chris Lattner4c1e9542009-03-06 06:45:05 +00001548 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1549 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001550
1551 return Changed;
1552}
1553
Duncan Sands51495602009-02-13 09:42:34 +00001554bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001555 bool Changed = false;
1556
Chris Lattner4c1e9542009-03-06 06:45:05 +00001557 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1558 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001559
Devang Patelff631ae2006-11-15 01:27:05 +00001560 return Changed;
1561}
1562
Devang Patela1514cb2006-12-07 19:39:39 +00001563//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001564// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001565
Dan Gohmande6188a2010-08-12 23:50:08 +00001566/// Execute all of the passes scheduled for execution by invoking
1567/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001568/// the module, and if so, return true.
1569bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001570MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001571 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001572
Torok Edwin24c78352009-06-29 18:49:09 +00001573 // Initialize on-the-fly passes
1574 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1575 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1576 I != E; ++I) {
1577 FunctionPassManagerImpl *FPP = I->second;
1578 Changed |= FPP->doInitialization(M);
1579 }
1580
Devang Patelabfbe3b2006-12-16 00:56:26 +00001581 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1582 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001583 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001584
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001585 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001586 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001587
Devang Patelabfbe3b2006-12-16 00:56:26 +00001588 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001589
Chris Lattner4c1e9542009-03-06 06:45:05 +00001590 {
1591 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001592 TimeRegion PassTimer(getPassTimer(MP));
1593
Dan Gohman74b189f2010-03-01 17:34:28 +00001594 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001595 }
Devang Patel93a197c2006-12-14 00:08:04 +00001596
Dan Gohman74b189f2010-03-01 17:34:28 +00001597 Changed |= LocalChanged;
1598 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001599 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001600 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001601 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001602
Devang Patela273d1c2007-07-19 18:02:32 +00001603 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001604 removeNotPreservedAnalysis(MP);
1605 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001606 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001607 }
Torok Edwin24c78352009-06-29 18:49:09 +00001608
1609 // Finalize on-the-fly passes
1610 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1611 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1612 I != E; ++I) {
1613 FunctionPassManagerImpl *FPP = I->second;
1614 // We don't know when is the last time an on-the-fly pass is run,
1615 // so we need to releaseMemory / finalize here
1616 FPP->releaseMemoryOnTheFly();
1617 Changed |= FPP->doFinalization(M);
1618 }
Devang Patel05e1a972006-11-07 22:03:15 +00001619 return Changed;
1620}
1621
Devang Patele64d3052007-04-16 20:12:57 +00001622/// Add RequiredPass into list of lower level passes required by pass P.
1623/// RequiredPass is run on the fly by Pass Manager when P requests it
1624/// through getAnalysis interface.
1625void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001626 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1627 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001628 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001629 RequiredPass->getPotentialPassManagerType()) &&
1630 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001631
Devang Patel68f72b12007-04-26 17:50:19 +00001632 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001633 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001634 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001635 // FPP is the top level manager.
1636 FPP->setTopLevelManager(FPP);
1637
Devang Patel69e9f6d2007-04-16 20:27:05 +00001638 OnTheFlyManagers[P] = FPP;
1639 }
Devang Patel68f72b12007-04-26 17:50:19 +00001640 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001641
Devang Patel68f72b12007-04-26 17:50:19 +00001642 // Register P as the last user of RequiredPass.
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001643 if (RequiredPass) {
1644 SmallVector<Pass *, 1> LU;
1645 LU.push_back(RequiredPass);
1646 FPP->setLastUser(LU, P);
1647 }
Devang Patele64d3052007-04-16 20:12:57 +00001648}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001649
Dan Gohmande6188a2010-08-12 23:50:08 +00001650/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001651/// required by module pass MP. Instantiate analysis pass, by using
1652/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001653Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001654 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001655 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001656
Torok Edwin24c78352009-06-29 18:49:09 +00001657 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001658 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001659 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001660}
1661
1662
Devang Patela1514cb2006-12-07 19:39:39 +00001663//===----------------------------------------------------------------------===//
1664// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001665//
Devang Patelc290c8a2006-11-07 22:23:34 +00001666/// run - Execute all of the passes scheduled for execution. Keep track of
1667/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001668bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001669 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001670 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001671 createDebugInfoProbe();
Devang Patelb8817b92006-12-14 00:59:42 +00001672
Devang Patelcfd70c42006-12-13 22:10:00 +00001673 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001674 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001675
Devang Patele3068402006-12-21 00:16:50 +00001676 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001677 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1678 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001679 return Changed;
1680}
Devang Patel376fefa2006-11-08 10:29:57 +00001681
Devang Patela1514cb2006-12-07 19:39:39 +00001682//===----------------------------------------------------------------------===//
1683// PassManager implementation
1684
Devang Patel376fefa2006-11-08 10:29:57 +00001685/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001686PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001687 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001688 // PM is the top level manager
1689 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001690}
1691
Devang Patelb67904d2006-12-13 02:36:01 +00001692PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001693 delete PM;
1694}
1695
David Greene103d4b42010-05-10 20:24:27 +00001696/// addImpl - Add a pass to the queue of passes to run, without
1697/// checking whether to add a printer pass.
1698void PassManager::addImpl(Pass *P) {
1699 PM->add(P);
1700}
1701
Devang Patel376fefa2006-11-08 10:29:57 +00001702/// add - Add a pass to the queue of passes to run. This passes ownership of
1703/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1704/// will be destroyed as well, so there is no need to delete the pass. This
1705/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001706void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001707 const void* PassID = P->getPassID();
1708 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001709 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1710 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001711
David Greene103d4b42010-05-10 20:24:27 +00001712 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001713
Owen Andersona7aed182010-08-06 18:33:48 +00001714 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001715 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1716 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001717}
1718
1719/// run - Execute all of the passes scheduled for execution. Keep track of
1720/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001721bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001722 return PM->run(M);
1723}
1724
Devang Patelb8817b92006-12-14 00:59:42 +00001725//===----------------------------------------------------------------------===//
1726// TimingInfo Class - This class is used to calculate information about the
1727// amount of time each pass takes to execute. This only happens with
1728// -time-passes is enabled on the command line.
1729//
1730bool llvm::TimePassesIsEnabled = false;
1731static cl::opt<bool,true>
1732EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1733 cl::desc("Time each pass, printing elapsed time for each on exit"));
1734
1735// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1736// a non null value (if the -time-passes option is enabled) or it leaves it
1737// null. It may be called multiple times.
1738void TimingInfo::createTheTimeInfo() {
1739 if (!TimePassesIsEnabled || TheTimeInfo) return;
1740
1741 // Constructed the first time this is called, iff -time-passes is enabled.
1742 // This guarantees that the object will be constructed before static globals,
1743 // thus it will be destroyed before them.
1744 static ManagedStatic<TimingInfo> TTI;
1745 TheTimeInfo = &*TTI;
1746}
1747
Devang Patel1c3633e2007-01-29 23:10:37 +00001748/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001749Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001750 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001751 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001752 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001753}
1754
Devang Patel1c56a632007-01-08 19:29:38 +00001755//===----------------------------------------------------------------------===//
1756// PMStack implementation
1757//
Devang Patelad98d232007-01-11 22:15:30 +00001758
Devang Patel1c56a632007-01-08 19:29:38 +00001759// Pop Pass Manager from the stack and clear its analysis info.
1760void PMStack::pop() {
1761
1762 PMDataManager *Top = this->top();
1763 Top->initializeAnalysisInfo();
1764
1765 S.pop_back();
1766}
1767
1768// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001769void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001770 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001771 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001772
Chris Lattner60987362009-03-06 05:53:14 +00001773 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001774 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1775 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001776 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001777
Chris Lattner60987362009-03-06 05:53:14 +00001778 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001779 TPM->addIndirectPassManager(PM);
1780 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001781 PM->setDepth(this->top()->getDepth()+1);
1782 }
1783 else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001784 assert((PM->getPassManagerType() == PMT_ModulePassManager
1785 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001786 && "pushing bad pass manager to PMStack");
1787 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001788 }
1789
Devang Patel15701b52007-01-11 00:19:00 +00001790 S.push_back(PM);
1791}
1792
1793// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001794void PMStack::dump() const {
1795 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001796 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001797 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001798
Devang Patel15701b52007-01-11 00:19:00 +00001799 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001800 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001801}
1802
Devang Patel1c56a632007-01-08 19:29:38 +00001803/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001804/// add self into that manager.
1805void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001806 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001807 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001808 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001809 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1810 if (TopPMType == PreferredType)
1811 break; // We found desired pass manager
1812 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001813 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001814 else
1815 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001816 }
Devang Patel18ff6362008-09-09 21:38:40 +00001817 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001818 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001819}
1820
Devang Patel3312f752007-01-16 21:43:18 +00001821/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001822/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001823void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001824 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001825
Devang Patela3286902008-09-09 17:56:50 +00001826 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001827 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001828 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1829 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001830 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001831 break;
Devang Patel3312f752007-01-16 21:43:18 +00001832 }
Devang Patel3312f752007-01-16 21:43:18 +00001833
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001834 // Create new Function Pass Manager if needed.
1835 FPPassManager *FPP;
1836 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1837 FPP = (FPPassManager *)PMS.top();
1838 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001839 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1840 PMDataManager *PMD = PMS.top();
1841
1842 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001843 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001844 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001845
1846 // [2] Set up new manager's top level manager
1847 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1848 TPM->addIndirectPassManager(FPP);
1849
1850 // [3] Assign manager to manage this new manager. This may create
1851 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001852 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001853
1854 // [4] Push new manager into PMS
1855 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001856 }
1857
Devang Patel3312f752007-01-16 21:43:18 +00001858 // Assign FPP as the manager of this pass.
1859 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001860}
1861
Devang Patel3312f752007-01-16 21:43:18 +00001862/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001863/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001864void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001865 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001866 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001867
Devang Patel15701b52007-01-11 00:19:00 +00001868 // Basic Pass Manager is a leaf pass manager. It does not handle
1869 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001870 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001871 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1872 BBP = (BBPassManager *)PMS.top();
1873 } else {
1874 // If leaf manager is not Basic Block Pass manager then create new
1875 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001876 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1877 PMDataManager *PMD = PMS.top();
1878
1879 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001880 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001881
1882 // [2] Set up new manager's top level manager
1883 // Basic Block Pass Manager does not live by itself
1884 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1885 TPM->addIndirectPassManager(BBP);
1886
Devang Patel15701b52007-01-11 00:19:00 +00001887 // [3] Assign manager to manage this new manager. This may create
1888 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001889 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001890
Devang Patel3312f752007-01-16 21:43:18 +00001891 // [4] Push new manager into PMS
1892 PMS.push(BBP);
1893 }
Devang Patel1c56a632007-01-08 19:29:38 +00001894
Devang Patel3312f752007-01-16 21:43:18 +00001895 // Assign BBP as the manager of this pass.
1896 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001897}
1898
Dan Gohmand3a20c92008-03-11 16:41:42 +00001899PassManagerBase::~PassManagerBase() {}