blob: ca4455a436adc266b55714edb687d83c8c96b201 [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>
Duncan Sands26ff6f92008-10-08 07:23:46 +000031#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000032#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000033using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000034
Devang Patele7599552007-01-12 18:52:44 +000035// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000036
Devang Patelf1567a52006-12-13 20:03:48 +000037namespace llvm {
38
39//===----------------------------------------------------------------------===//
40// Pass debugging information. Often it is useful to find out what pass is
41// running when a crash occurs in a utility. When this library is compiled with
42// debugging on, a command line option (--debug-pass) is enabled that causes the
43// pass name to be printed before it executes.
44//
45
Devang Patel03fb5872006-12-13 21:13:31 +000046// Different debug levels that can be enabled...
47enum PassDebugLevel {
48 None, Arguments, Structure, Executions, Details
49};
50
Devang Patelf1567a52006-12-13 20:03:48 +000051static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000052PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000053 cl::desc("Print PassManager debugging information"),
54 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000055 clEnumVal(None , "disable debug output"),
56 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
57 clEnumVal(Structure , "print pass structure before run()"),
58 clEnumVal(Executions, "print pass name before it is executed"),
59 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000060 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000061
62typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
63PassOptionList;
64
65// Print IR out before/after specified passes.
66static PassOptionList
67PrintBefore("print-before",
Eric Christopher787ba0b2011-03-09 19:46:51 +000068 llvm::cl::desc("Print IR before specified passes"),
69 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000070
71static PassOptionList
72PrintAfter("print-after",
Eric Christopher787ba0b2011-03-09 19:46:51 +000073 llvm::cl::desc("Print IR after specified passes"),
74 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000075
76static cl::opt<bool>
77PrintBeforeAll("print-before-all",
78 llvm::cl::desc("Print IR before each pass"),
79 cl::init(false));
80static cl::opt<bool>
81PrintAfterAll("print-after-all",
82 llvm::cl::desc("Print IR after each pass"),
83 cl::init(false));
84
85/// This is a helper to determine whether to print IR before or
86/// after a pass.
87
Owen Andersona7aed182010-08-06 18:33:48 +000088static bool ShouldPrintBeforeOrAfterPass(const void *PassID,
David Greene9b063df2010-04-02 23:17:14 +000089 PassOptionList &PassesToPrint) {
Owen Andersona7aed182010-08-06 18:33:48 +000090 if (const llvm::PassInfo *PI =
91 PassRegistry::getPassRegistry()->getPassInfo(PassID)) {
92 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
93 const llvm::PassInfo *PassInf = PassesToPrint[i];
94 if (PassInf)
95 if (PassInf->getPassArgument() == PI->getPassArgument()) {
96 return true;
97 }
98 }
David Greene9b063df2010-04-02 23:17:14 +000099 }
100 return false;
101}
Dan Gohmande6188a2010-08-12 23:50:08 +0000102
David Greene9b063df2010-04-02 23:17:14 +0000103
104/// This is a utility to check whether a pass should have IR dumped
105/// before it.
Owen Andersona7aed182010-08-06 18:33:48 +0000106static bool ShouldPrintBeforePass(const void *PassID) {
107 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000108}
109
110/// This is a utility to check whether a pass should have IR dumped
111/// after it.
Owen Andersona7aed182010-08-06 18:33:48 +0000112static bool ShouldPrintAfterPass(const void *PassID) {
113 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000114}
115
Devang Patelf1567a52006-12-13 20:03:48 +0000116} // End of llvm namespace
117
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000118/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
119/// or higher is specified.
120bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
121 return PassDebugging >= Executions;
122}
123
124
125
126
Chris Lattner4c1e9542009-03-06 06:45:05 +0000127void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
128 if (V == 0 && M == 0)
129 OS << "Releasing pass '";
130 else
131 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000132
Chris Lattner4c1e9542009-03-06 06:45:05 +0000133 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000134
Chris Lattner4c1e9542009-03-06 06:45:05 +0000135 if (M) {
136 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
137 return;
138 }
139 if (V == 0) {
140 OS << '\n';
141 return;
142 }
143
Dan Gohman79fc0e92009-03-10 18:47:59 +0000144 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000145 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000146 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000147 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000148 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000149 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000150 OS << "value";
151
152 OS << " '";
153 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
154 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000155}
156
157
Devang Patelffca9102006-12-15 19:39:30 +0000158namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000159
Devang Patelf33f3eb2006-12-07 19:21:29 +0000160//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000162//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000163/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000164/// pass together and sequence them to process one basic block before
165/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000166class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000167
168public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000169 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000170 explicit BBPassManager(int Depth)
Owen Andersona7aed182010-08-06 18:33:48 +0000171 : PMDataManager(Depth), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000172
Devang Patelca58e352006-11-08 10:05:38 +0000173 /// Execute all of the passes scheduled for execution. Keep track of
174 /// whether any of the passes modifies the function, and if so, return true.
175 bool runOnFunction(Function &F);
176
Devang Patelf9d96b92006-12-07 19:57:52 +0000177 /// Pass Manager itself does not invalidate any analysis info.
178 void getAnalysisUsage(AnalysisUsage &Info) const {
179 Info.setPreservesAll();
180 }
181
Devang Patel475c4532006-12-08 00:59:05 +0000182 bool doInitialization(Module &M);
183 bool doInitialization(Function &F);
184 bool doFinalization(Module &M);
185 bool doFinalization(Function &F);
186
Chris Lattner2fa26e52010-01-22 05:24:46 +0000187 virtual PMDataManager *getAsPMDataManager() { return this; }
188 virtual Pass *getAsPass() { return this; }
189
Devang Patele3858e62007-02-01 22:08:25 +0000190 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000191 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000192 }
193
Devang Pateleda56172006-12-12 23:34:33 +0000194 // Print passes managed by this manager
195 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000196 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
198 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000199 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000200 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000201 }
202 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000203
204 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000205 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000206 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
207 return BP;
208 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000209
Dan Gohmande6188a2010-08-12 23:50:08 +0000210 virtual PassManagerType getPassManagerType() const {
211 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000212 }
Devang Patelca58e352006-11-08 10:05:38 +0000213};
214
Devang Patel8c78a0b2007-05-03 01:11:54 +0000215char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000216}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000217
Devang Patele7599552007-01-12 18:52:44 +0000218namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000219
Devang Patel10c2ca62006-12-12 22:47:13 +0000220//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000222//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223/// FunctionPassManagerImpl manages FPPassManagers
224class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000225 public PMDataManager,
226 public PMTopLevelManager {
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;
Dan Gohmande6188a2010-08-12 23:50:08 +0000231 explicit FunctionPassManagerImpl(int Depth) :
232 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000233 PMTopLevelManager(new FPPassManager(1)), 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
Devang Patel8c78a0b2007-05-03 01:11:54 +0000295char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000296
Devang Patel67d6a5e2006-12-19 19:46:59 +0000297//===----------------------------------------------------------------------===//
298// MPPassManager
299//
300/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000301/// It batches all Module passes and function pass managers together and
302/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000303class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000304public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000305 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000306 explicit MPPassManager(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000307 Pass(PT_PassManager, ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000308
309 // Delete on the fly managers.
310 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000311 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000312 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
313 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000314 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000315 delete FPP;
316 }
317 }
318
Dan Gohmande6188a2010-08-12 23:50:08 +0000319 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000320 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
321 return createPrintModulePass(&O, false, Banner);
322 }
323
Devang Patelca58e352006-11-08 10:05:38 +0000324 /// run - Execute all of the passes scheduled for execution. Keep track of
325 /// whether any of the passes modifies the module, and if so, return true.
326 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000327
Devang Patelf9d96b92006-12-07 19:57:52 +0000328 /// Pass Manager itself does not invalidate any analysis info.
329 void getAnalysisUsage(AnalysisUsage &Info) const {
330 Info.setPreservesAll();
331 }
332
Devang Patele64d3052007-04-16 20:12:57 +0000333 /// Add RequiredPass into list of lower level passes required by pass P.
334 /// RequiredPass is run on the fly by Pass Manager when P requests it
335 /// through getAnalysis interface.
336 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
337
Dan Gohmande6188a2010-08-12 23:50:08 +0000338 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000339 /// required by module pass MP. Instantiate analysis pass, by using
340 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000341 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000342
Devang Patele3858e62007-02-01 22:08:25 +0000343 virtual const char *getPassName() const {
344 return "Module Pass Manager";
345 }
346
Chris Lattner2fa26e52010-01-22 05:24:46 +0000347 virtual PMDataManager *getAsPMDataManager() { return this; }
348 virtual Pass *getAsPass() { return this; }
349
Devang Pateleda56172006-12-12 23:34:33 +0000350 // Print passes managed by this manager
351 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000352 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000353 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
354 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000355 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000356 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
357 OnTheFlyManagers.find(MP);
358 if (I != OnTheFlyManagers.end())
359 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000360 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000361 }
362 }
363
Devang Patelabfbe3b2006-12-16 00:56:26 +0000364 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000365 assert(N < PassVector.size() && "Pass number out of range!");
366 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000367 }
368
Dan Gohmande6188a2010-08-12 23:50:08 +0000369 virtual PassManagerType getPassManagerType() const {
370 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000371 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000372
373 private:
374 /// Collection of on the fly FPPassManagers. These managers manage
375 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000376 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000377};
378
Devang Patel8c78a0b2007-05-03 01:11:54 +0000379char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000380//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000381// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000382//
Devang Patel09f162c2007-05-01 21:15:47 +0000383
Devang Patel67d6a5e2006-12-19 19:46:59 +0000384/// PassManagerImpl manages MPPassManagers
385class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000386 public PMDataManager,
387 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000388
389public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000390 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000391 explicit PassManagerImpl(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000392 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000393 PMTopLevelManager(new MPPassManager(1)) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000394
Devang Patel376fefa2006-11-08 10:29:57 +0000395 /// add - Add a pass to the queue of passes to run. This passes ownership of
396 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
397 /// will be destroyed as well, so there is no need to delete the pass. This
398 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000399 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000400 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000401 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000402
403 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000404 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
405 return createPrintModulePass(&O, false, Banner);
406 }
407
Devang Patel376fefa2006-11-08 10:29:57 +0000408 /// run - Execute all of the passes scheduled for execution. Keep track of
409 /// whether any of the passes modifies the module, and if so, return true.
410 bool run(Module &M);
411
Devang Patelf9d96b92006-12-07 19:57:52 +0000412 /// Pass Manager itself does not invalidate any analysis info.
413 void getAnalysisUsage(AnalysisUsage &Info) const {
414 Info.setPreservesAll();
415 }
416
Dan Gohman30614852010-08-16 21:57:30 +0000417 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000418 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000419 // P is a immutable pass and it will be managed by this
420 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000421 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000422 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000423 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000424 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000425 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000426 } else {
David Greene103d4b42010-05-10 20:24:27 +0000427 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000428 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000429 }
430
Chris Lattner2fa26e52010-01-22 05:24:46 +0000431 virtual PMDataManager *getAsPMDataManager() { return this; }
432 virtual Pass *getAsPass() { return this; }
433
Devang Patel67d6a5e2006-12-19 19:46:59 +0000434 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000435 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000436 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
437 return MP;
438 }
Devang Patel376fefa2006-11-08 10:29:57 +0000439};
440
Devang Patel8c78a0b2007-05-03 01:11:54 +0000441char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000442} // End of llvm namespace
443
444namespace {
445
446//===----------------------------------------------------------------------===//
Devang Patelfa31d382011-03-10 00:21:25 +0000447// DebugInfoProbe
448
449static DebugInfoProbeInfo *TheDebugProbe;
450static void createDebugInfoProbe() {
451 if (TheDebugProbe) return;
452
453 // Constructed the first time this is called. This guarantees that the
454 // object will be constructed, if -enable-debug-info-probe is set,
455 // before static globals, thus it will be destroyed before them.
456 static ManagedStatic<DebugInfoProbeInfo> DIP;
457 TheDebugProbe = &*DIP;
458}
459
460//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000461/// TimingInfo Class - This class is used to calculate information about the
462/// amount of time each pass takes to execute. This only happens when
463/// -time-passes is enabled on the command line.
464///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000465
Owen Anderson5a6960f2009-06-18 20:51:00 +0000466static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000467
Nick Lewycky02d5f772009-10-25 06:33:48 +0000468class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000469 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000470 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000471public:
472 // Use 'create' member to get this.
473 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000474
Devang Patel1c3633e2007-01-29 23:10:37 +0000475 // TimingDtor - Print out information about timing information
476 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000477 // Delete all of the timers, which accumulate their info into the
478 // TimerGroup.
479 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
480 E = TimingData.end(); I != E; ++I)
481 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000482 // TimerGroup is deleted next, printing the report.
483 }
484
485 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
486 // to a non null value (if the -time-passes option is enabled) or it leaves it
487 // null. It may be called multiple times.
488 static void createTheTimeInfo();
489
Chris Lattner707431c2010-03-30 04:03:22 +0000490 /// getPassTimer - Return the timer for the specified pass if it exists.
491 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000492 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000493 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000494
Owen Anderson5c96ef72009-07-07 18:33:04 +0000495 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000496 Timer *&T = TimingData[P];
497 if (T == 0)
498 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000499 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000500 }
501};
502
Devang Patel1c3633e2007-01-29 23:10:37 +0000503} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000504
Dan Gohmand78c4002008-05-13 00:00:25 +0000505static TimingInfo *TheTimeInfo;
506
Devang Patela1514cb2006-12-07 19:39:39 +0000507//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000508// PMTopLevelManager implementation
509
Devang Patel4268fc02007-01-16 02:00:38 +0000510/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000511PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
512 PMDM->setTopLevelManager(this);
513 addPassManager(PMDM);
514 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000515}
516
Devang Patelafb1f3622006-12-12 22:35:25 +0000517/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000518void
519PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
520 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000521 unsigned PDepth = 0;
522 if (P->getResolver())
523 PDepth = P->getResolver()->getPMDataManager().getDepth();
524
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000525 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000526 E = AnalysisPasses.end(); I != E; ++I) {
527 Pass *AP = *I;
528 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000529
Devang Patel01919d22007-03-08 19:05:01 +0000530 if (P == AP)
531 continue;
532
Tobias Grosserf07426b2011-01-20 21:03:22 +0000533 // Update the last users of passes that are required transitive by AP.
534 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
535 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
536 SmallVector<Pass *, 12> LastUses;
537 SmallVector<Pass *, 12> LastPMUses;
538 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
539 E = IDs.end(); I != E; ++I) {
540 Pass *AnalysisPass = findAnalysisPass(*I);
541 assert(AnalysisPass && "Expected analysis pass to exist.");
542 AnalysisResolver *AR = AnalysisPass->getResolver();
543 assert(AR && "Expected analysis resolver to exist.");
544 unsigned APDepth = AR->getPMDataManager().getDepth();
545
546 if (PDepth == APDepth)
547 LastUses.push_back(AnalysisPass);
548 else if (PDepth > APDepth)
549 LastPMUses.push_back(AnalysisPass);
550 }
551
552 setLastUser(LastUses, P);
553
554 // If this pass has a corresponding pass manager, push higher level
555 // analysis to this pass manager.
556 if (P->getResolver())
557 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
558
559
Devang Patelafb1f3622006-12-12 22:35:25 +0000560 // If AP is the last user of other passes then make P last user of
561 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000562 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000563 LUE = LastUser.end(); LUI != LUE; ++LUI) {
564 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000565 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000566 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000567 LastUser[LUI->first] = P;
568 }
569 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000570}
571
572/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000573void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000574 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000575 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000576 InversedLastUser.find(P);
577 if (DMI == InversedLastUser.end())
578 return;
579
580 SmallPtrSet<Pass *, 8> &LU = DMI->second;
581 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
582 E = LU.end(); I != E; ++I) {
583 LastUses.push_back(*I);
584 }
585
Devang Patelafb1f3622006-12-12 22:35:25 +0000586}
587
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000588AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
589 AnalysisUsage *AnUsage = NULL;
590 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000591 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000592 AnUsage = DMI->second;
593 else {
594 AnUsage = new AnalysisUsage();
595 P->getAnalysisUsage(*AnUsage);
596 AnUsageMap[P] = AnUsage;
597 }
598 return AnUsage;
599}
600
Devang Patelafb1f3622006-12-12 22:35:25 +0000601/// Schedule pass P for execution. Make sure that passes required by
602/// P are run before P is run. Update analysis info maintained by
603/// the manager. Remove dead passes. This is a recursive function.
604void PMTopLevelManager::schedulePass(Pass *P) {
605
Devang Patel3312f752007-01-16 21:43:18 +0000606 // TODO : Allocate function manager for this pass, other wise required set
607 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000608
Devang Pateld74ede72007-03-06 01:06:16 +0000609 // Give pass a chance to prepare the stage.
610 P->preparePassManager(activeStack);
611
Devang Patel864970e2008-03-18 00:39:19 +0000612 // If P is an analysis pass and it is available then do not
613 // generate the analysis again. Stale analysis info should not be
614 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000615 const PassInfo *PI =
616 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
617 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000618 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000619 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000620 }
Devang Patel864970e2008-03-18 00:39:19 +0000621
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000622 AnalysisUsage *AnUsage = findAnalysisUsage(P);
623
Devang Patelfdee7032008-08-14 23:07:48 +0000624 bool checkAnalysis = true;
625 while (checkAnalysis) {
626 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000627
Devang Patelfdee7032008-08-14 23:07:48 +0000628 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
629 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
630 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000631
Devang Patelfdee7032008-08-14 23:07:48 +0000632 Pass *AnalysisPass = findAnalysisPass(*I);
633 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000634 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
635 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000636 if (P->getPotentialPassManagerType () ==
637 AnalysisPass->getPotentialPassManagerType())
638 // Schedule analysis pass that is managed by the same pass manager.
639 schedulePass(AnalysisPass);
640 else if (P->getPotentialPassManagerType () >
641 AnalysisPass->getPotentialPassManagerType()) {
642 // Schedule analysis pass that is managed by a new manager.
643 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000644 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000645 // are already checked are still available.
646 checkAnalysis = true;
647 }
648 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000649 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000650 // passes are run on the fly.
651 delete AnalysisPass;
652 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000653 }
654 }
655
656 // Now all required passes are available.
657 addTopLevelPass(P);
658}
659
660/// Find the pass that implements Analysis AID. Search immutable
661/// passes and all pass managers. If desired pass is not found
662/// then return NULL.
663Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
664
Devang Patelcd6ba152006-12-12 22:50:05 +0000665 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000666 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000667 E = PassManagers.end(); I != E; ++I)
668 if (Pass *P = (*I)->findAnalysisPass(AID, false))
669 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000670
671 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000672 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000673 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000674 E = IndirectPassManagers.end(); I != E; ++I)
675 if (Pass *P = (*I)->findAnalysisPass(AID, false))
676 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000677
Dan Gohman844dd0a2010-10-11 23:19:01 +0000678 // Check the immutable passes. Iterate in reverse order so that we find
679 // the most recently registered passes first.
680 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
681 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000682 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000683 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000684 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000685
686 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000687 const PassInfo *PassInf =
688 PassRegistry::getPassRegistry()->getPassInfo(PI);
689 const std::vector<const PassInfo*> &ImmPI =
690 PassInf->getInterfacesImplemented();
691 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
692 EE = ImmPI.end(); II != EE; ++II) {
693 if ((*II)->getTypeInfo() == AID)
694 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000695 }
696 }
697
Dan Gohman844dd0a2010-10-11 23:19:01 +0000698 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000699}
700
Devang Pateleda56172006-12-12 23:34:33 +0000701// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000702void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000703
Devang Patelfd4184322007-01-17 20:33:36 +0000704 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000705 return;
706
Devang Pateleda56172006-12-12 23:34:33 +0000707 // Print out the immutable passes
708 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000709 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000710 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000711
Dan Gohmanf71c5212010-08-19 01:29:07 +0000712 // Every class that derives from PMDataManager also derives from Pass
713 // (sometimes indirectly), but there's no inheritance relationship
714 // between PMDataManager and Pass, so we have to getAsPass to get
715 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000716 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000717 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000718 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000719}
720
Devang Patel991aeba2006-12-15 20:13:01 +0000721void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000722
Devang Patelfd4184322007-01-17 20:33:36 +0000723 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000724 return;
725
David Greene994e1bb2010-01-05 01:30:02 +0000726 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000727 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
728 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
729 if (const PassInfo *PI =
730 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
731 if (!PI->isAnalysisGroup())
732 dbgs() << " -" << PI->getPassArgument();
Devang Patel0d29ae02008-08-12 15:44:31 +0000733 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000734 E = PassManagers.end(); I != E; ++I)
735 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000736 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000737}
738
Devang Patele3068402006-12-21 00:16:50 +0000739void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000740 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000741 E = PassManagers.end(); I != E; ++I)
742 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000743
Devang Patele3068402006-12-21 00:16:50 +0000744 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000745 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000746 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
747 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000748 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000749
Chris Lattner60987362009-03-06 05:53:14 +0000750 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000751 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000752 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000753 InversedLastUser.find(DMI->second);
754 if (InvDMI != InversedLastUser.end()) {
755 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
756 L.insert(DMI->first);
757 } else {
758 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
759 InversedLastUser[DMI->second] = L;
760 }
761 }
Devang Patele3068402006-12-21 00:16:50 +0000762}
763
Devang Patele7599552007-01-12 18:52:44 +0000764/// Destructor
765PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000766 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000767 E = PassManagers.end(); I != E; ++I)
768 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000769
Dan Gohman060d5ba2010-10-12 00:15:27 +0000770 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000771 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
772 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000773
774 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000775 DME = AnUsageMap.end(); DMI != DME; ++DMI)
776 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000777}
778
Devang Patelafb1f3622006-12-12 22:35:25 +0000779//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000780// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000781
Devang Patel643676c2006-11-11 01:10:19 +0000782/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000783void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000784 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000785
Chris Lattner60987362009-03-06 05:53:14 +0000786 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000787
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000788 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000789
Dan Gohmande6188a2010-08-12 23:50:08 +0000790 // This pass is the current implementation of all of the interfaces it
791 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000792 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
793 if (PInf == 0) return;
794 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000795 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000796 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000797}
798
Devang Patel9d9fc902007-03-06 17:52:53 +0000799// Return true if P preserves high level analysis used by other
800// passes managed by this manager
801bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000802 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000803 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000804 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000805
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000806 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000807 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000808 E = HigherLevelAnalysis.end(); I != E; ++I) {
809 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000810 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000811 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000812 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000813 PreservedSet.end())
814 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000815 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000816
Devang Patel9d9fc902007-03-06 17:52:53 +0000817 return true;
818}
819
Chris Lattner02eb94c2008-08-07 07:34:50 +0000820/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000821void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000822 // Don't do this unless assertions are enabled.
823#ifdef NDEBUG
824 return;
825#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000826 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
827 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000828
Devang Patelef432532007-07-19 05:36:09 +0000829 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000830 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000831 E = PreservedSet.end(); I != E; ++I) {
832 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000833 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000834 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000835 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000836 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000837 }
838}
839
Devang Patel67c79a42008-07-01 19:50:56 +0000840/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000841void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000842 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
843 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000844 return;
845
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000846 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000847 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000848 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000849 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000850 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000851 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000852 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000853 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000854 if (PassDebugging >= Details) {
855 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000856 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
857 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000858 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000859 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000860 }
Devang Patel349170f2006-11-11 01:24:55 +0000861 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000862
Devang Patel42dd1e92007-03-06 01:55:46 +0000863 // Check inherited analysis also. If P is not preserving analysis
864 // provided by parent manager then remove it here.
865 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
866
867 if (!InheritedAnalysis[Index])
868 continue;
869
Dan Gohmande6188a2010-08-12 23:50:08 +0000870 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000871 I = InheritedAnalysis[Index]->begin(),
872 E = InheritedAnalysis[Index]->end(); I != E; ) {
873 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000874 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000875 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000876 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000877 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000878 if (PassDebugging >= Details) {
879 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000880 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
881 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000882 }
Devang Patel01919d22007-03-08 19:05:01 +0000883 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000884 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000885 }
886 }
Devang Patelf68a3492006-11-07 22:35:17 +0000887}
888
Devang Patelca189262006-11-14 03:05:08 +0000889/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000890void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000891 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000892
Devang Patel8adae862007-07-20 18:04:54 +0000893 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000894
Devang Patel2ff44922007-04-16 20:39:59 +0000895 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000896 if (!TPM)
897 return;
898
Devang Patel17ad0962006-12-08 00:37:52 +0000899 TPM->collectLastUses(DeadPasses, P);
900
Devang Patel656a9172008-06-06 17:50:36 +0000901 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000902 dbgs() << " -*- '" << P->getPassName();
903 dbgs() << "' is the last user of following pass instances.";
904 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000905 }
906
Dan Gohman060d5ba2010-10-12 00:15:27 +0000907 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000908 E = DeadPasses.end(); I != E; ++I)
909 freePass(*I, Msg, DBG_STR);
910}
Devang Patel200d3052006-12-13 23:50:44 +0000911
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000912void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000913 enum PassDebuggingString DBG_STR) {
914 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000915
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000916 {
917 // If the pass crashes releasing memory, remember this.
918 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000919 TimeRegion PassTimer(getPassTimer(P));
920
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000921 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000922 }
923
Owen Andersona7aed182010-08-06 18:33:48 +0000924 AnalysisID PI = P->getPassID();
925 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000926 // Remove the pass itself (if it is not already removed).
927 AvailableAnalysis.erase(PI);
928
929 // Remove all interfaces this pass implements, for which it is also
930 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000931 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000932 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000933 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000934 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000935 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000936 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000937 }
Devang Patel17ad0962006-12-08 00:37:52 +0000938 }
Devang Patelca189262006-11-14 03:05:08 +0000939}
940
Dan Gohmande6188a2010-08-12 23:50:08 +0000941/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000942/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000943void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000944 // This manager is going to manage pass P. Set up analysis resolver
945 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000946 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000947 P->setResolver(AR);
948
Devang Patelec2b9a72007-03-05 22:57:49 +0000949 // If a FunctionPass F is the last user of ModulePass info M
950 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000951 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000952
Chris Lattner60987362009-03-06 05:53:14 +0000953 if (!ProcessAnalysis) {
954 // Add pass
955 PassVector.push_back(P);
956 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000957 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000958
Chris Lattner60987362009-03-06 05:53:14 +0000959 // At the moment, this pass is the last user of all required passes.
960 SmallVector<Pass *, 12> LastUses;
961 SmallVector<Pass *, 8> RequiredPasses;
962 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
963
964 unsigned PDepth = this->getDepth();
965
Dan Gohmande6188a2010-08-12 23:50:08 +0000966 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000967 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000968 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000969 E = RequiredPasses.end(); I != E; ++I) {
970 Pass *PRequired = *I;
971 unsigned RDepth = 0;
972
973 assert(PRequired->getResolver() && "Analysis Resolver is not set");
974 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
975 RDepth = DM.getDepth();
976
977 if (PDepth == RDepth)
978 LastUses.push_back(PRequired);
979 else if (PDepth > RDepth) {
980 // Let the parent claim responsibility of last use
981 TransferLastUses.push_back(PRequired);
982 // Keep track of higher level analysis used by this manager.
983 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000984 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000985 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000986 }
987
988 // Set P as P's last user until someone starts using P.
989 // However, if P is a Pass Manager then it does not need
990 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000991 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000992 LastUses.push_back(P);
993 TPM->setLastUser(LastUses, P);
994
995 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000996 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000997 TPM->setLastUser(TransferLastUses, My_PM);
998 TransferLastUses.clear();
999 }
1000
Dan Gohman6304db32010-08-16 22:57:28 +00001001 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001002 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001003 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001004 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +00001005 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1006 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001007 this->addLowerLevelRequiredPass(P, AnalysisPass);
1008 }
1009
1010 // Take a note of analysis required and made available by this pass.
1011 // Remove the analysis not preserved by this pass
1012 removeNotPreservedAnalysis(P);
1013 recordAvailableAnalysis(P);
1014
Devang Patel8cad70d2006-11-11 01:51:02 +00001015 // Add pass
1016 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001017}
1018
Devang Patele64d3052007-04-16 20:12:57 +00001019
1020/// Populate RP with analysis pass that are required by
1021/// pass P and are available. Populate RP_NotAvail with analysis
1022/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001023void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1024 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001025 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001026 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1027 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001028 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001029 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001030 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001031 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001032 else
Chris Lattner60987362009-03-06 05:53:14 +00001033 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001034 }
Devang Patelf58183d2006-12-12 23:09:32 +00001035
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001036 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001037 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001038 E = IDs.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 Patelf58183d2006-12-12 23:09:32 +00001043 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001044}
1045
Devang Patel07f4f582006-11-14 21:49:36 +00001046// All Required analyses should be available to the pass as it runs! Here
1047// we fill in the AnalysisImpls member of the pass so that it can
1048// successfully use the getAnalysis() method to retrieve the
1049// implementations it needs.
1050//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001051void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001052 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1053
Chris Lattnercbd160f2008-08-08 05:33:04 +00001054 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001055 I = AnUsage->getRequiredSet().begin(),
1056 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001057 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001058 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001059 // This may be analysis pass that is initialized on the fly.
1060 // If that is not the case then it will raise an assert when it is used.
1061 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001062 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001063 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001064 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001065 }
1066}
1067
Devang Patel640c5bb2006-12-08 22:30:11 +00001068/// Find the pass that implements Analysis AID. If desired pass is not found
1069/// then return NULL.
1070Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1071
1072 // Check if AvailableAnalysis map has one entry.
1073 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1074
1075 if (I != AvailableAnalysis.end())
1076 return I->second;
1077
1078 // Search Parents through TopLevelManager
1079 if (SearchParent)
1080 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001081
Devang Patel9d759b82006-12-09 00:09:12 +00001082 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001083}
1084
Devang Patel991aeba2006-12-15 20:13:01 +00001085// Print list of passes that are last used by P.
1086void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1087
Devang Patel8adae862007-07-20 18:04:54 +00001088 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001089
1090 // If this is a on the fly manager then it does not have TPM.
1091 if (!TPM)
1092 return;
1093
Devang Patel991aeba2006-12-15 20:13:01 +00001094 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001095
Dan Gohman7224bce2010-10-12 00:11:18 +00001096 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001097 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001098 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001099 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001100 }
1101}
1102
1103void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001104 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001105 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001106 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001107 PMD->dumpPassArguments();
1108 else
Owen Andersona7aed182010-08-06 18:33:48 +00001109 if (const PassInfo *PI =
1110 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001111 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001112 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001113 }
1114}
1115
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001116void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1117 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001118 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001119 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001120 return;
David Greene994e1bb2010-01-05 01:30:02 +00001121 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001122 switch (S1) {
1123 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001124 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001125 break;
1126 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001127 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001128 break;
1129 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001130 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001131 break;
1132 default:
1133 break;
1134 }
1135 switch (S2) {
1136 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001137 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001138 break;
1139 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001140 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001141 break;
1142 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001143 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001144 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001145 case ON_REGION_MSG:
1146 dbgs() << "' on Region '" << Msg << "'...\n";
1147 break;
Devang Patel003a5592007-03-05 20:01:30 +00001148 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
1154 default:
1155 break;
1156 }
Devang Patel991aeba2006-12-15 20:13:01 +00001157}
1158
Chris Lattner4c1e9542009-03-06 06:45:05 +00001159void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001160 if (PassDebugging < Details)
1161 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001162
Chris Lattner4c493d92008-08-08 15:14:09 +00001163 AnalysisUsage analysisUsage;
1164 P->getAnalysisUsage(analysisUsage);
1165 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1166}
1167
Chris Lattner4c1e9542009-03-06 06:45:05 +00001168void PMDataManager::dumpPreservedSet(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("Preserved", P, analysisUsage.getPreservedSet());
1175}
1176
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001177void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001178 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001179 assert(PassDebugging >= Details);
1180 if (Set.empty())
1181 return;
David Greene994e1bb2010-01-05 01:30:02 +00001182 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001183 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001184 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001185 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
1186 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001187 }
David Greene994e1bb2010-01-05 01:30:02 +00001188 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001189}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001190
Devang Patel004937b2007-07-27 20:06:09 +00001191/// Add RequiredPass into list of lower level passes required by pass P.
1192/// RequiredPass is run on the fly by Pass Manager when P requests it
1193/// through getAnalysis interface.
1194/// This should be handled by specific pass manager.
1195void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1196 if (TPM) {
1197 TPM->dumpArguments();
1198 TPM->dumpPasses();
1199 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001200
Dan Gohmande6188a2010-08-12 23:50:08 +00001201 // Module Level pass may required Function Level analysis info
1202 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1203 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001204 // module level pass is requiring lower level analysis info managed by
1205 // lower level pass manager.
1206
1207 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001208 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001209 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001210#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001211 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1212 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001213#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001214 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001215}
1216
Owen Andersona7aed182010-08-06 18:33:48 +00001217Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001218 assert(0 && "Unable to find on the fly pass");
1219 return NULL;
1220}
1221
Devang Patele7599552007-01-12 18:52:44 +00001222// Destructor
1223PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001224 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001225 E = PassVector.end(); I != E; ++I)
1226 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001227}
1228
Devang Patel9bdf7d42006-12-08 23:28:54 +00001229//===----------------------------------------------------------------------===//
1230// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001231// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1232Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001233 return PM.findAnalysisPass(ID, dir);
1234}
1235
Dan Gohmande6188a2010-08-12 23:50:08 +00001236Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001237 Function &F) {
1238 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1239}
1240
Devang Patela1514cb2006-12-07 19:39:39 +00001241//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001242// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001243
Dan Gohmande6188a2010-08-12 23:50:08 +00001244/// Execute all of the passes scheduled for execution by invoking
1245/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001246/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001247bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001248 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001249 return false;
1250
Devang Patele9585592006-12-08 01:38:28 +00001251 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001252
Devang Patel6e5a1132006-11-07 21:31:57 +00001253 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001254 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1255 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001256 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001257
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001258 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001259 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001260
Devang Patelabfbe3b2006-12-16 00:56:26 +00001261 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001262
Chris Lattner4c1e9542009-03-06 06:45:05 +00001263 {
1264 // If the pass crashes, remember this.
1265 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001266 TimeRegion PassTimer(getPassTimer(BP));
1267
Dan Gohman74b189f2010-03-01 17:34:28 +00001268 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001269 }
Devang Patel93a197c2006-12-14 00:08:04 +00001270
Dan Gohman74b189f2010-03-01 17:34:28 +00001271 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001272 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001273 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001274 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001275 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001276
Devang Patela273d1c2007-07-19 18:02:32 +00001277 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001278 removeNotPreservedAnalysis(BP);
1279 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001280 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001281 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001282
Bill Wendling6ce6d262009-12-25 13:50:18 +00001283 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001284}
1285
Devang Patel475c4532006-12-08 00:59:05 +00001286// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001287bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001288 bool Changed = false;
1289
Chris Lattner4c1e9542009-03-06 06:45:05 +00001290 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1291 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001292
1293 return Changed;
1294}
1295
Duncan Sands51495602009-02-13 09:42:34 +00001296bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001297 bool Changed = false;
1298
Chris Lattner4c1e9542009-03-06 06:45:05 +00001299 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1300 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001301
1302 return Changed;
1303}
1304
Duncan Sands51495602009-02-13 09:42:34 +00001305bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001306 bool Changed = false;
1307
Devang Patelabfbe3b2006-12-16 00:56:26 +00001308 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1309 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001310 Changed |= BP->doInitialization(F);
1311 }
1312
1313 return Changed;
1314}
1315
Duncan Sands51495602009-02-13 09:42:34 +00001316bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001317 bool Changed = false;
1318
Devang Patelabfbe3b2006-12-16 00:56:26 +00001319 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1320 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001321 Changed |= BP->doFinalization(F);
1322 }
1323
1324 return Changed;
1325}
1326
1327
Devang Patela1514cb2006-12-07 19:39:39 +00001328//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001329// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001330
Devang Patel4e12f862006-11-08 10:44:40 +00001331/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001332FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001333 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001334 // FPM is the top level manager.
1335 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001336
Dan Gohman565df952008-03-13 02:08:36 +00001337 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001338 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001339}
1340
Devang Patelb67904d2006-12-13 02:36:01 +00001341FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001342 delete FPM;
1343}
1344
David Greene103d4b42010-05-10 20:24:27 +00001345/// addImpl - Add a pass to the queue of passes to run, without
1346/// checking whether to add a printer pass.
1347void FunctionPassManager::addImpl(Pass *P) {
1348 FPM->add(P);
1349}
1350
Devang Patel4e12f862006-11-08 10:44:40 +00001351/// add - Add a pass to the queue of passes to run. This passes
1352/// ownership of the Pass to the PassManager. When the
1353/// PassManager_X is destroyed, the pass will be destroyed as well, so
1354/// there is no need to delete the pass. (TODO delete passes.)
1355/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001356void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001357 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001358 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001359 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001360 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001361 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1362 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001363
David Greene103d4b42010-05-10 20:24:27 +00001364 addImpl(P);
1365
1366 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001367 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001368 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1369 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001370}
1371
Devang Patel9f3083e2006-11-15 19:39:54 +00001372/// run - Execute all of the passes scheduled for execution. Keep
1373/// track of whether any of the passes modifies the function, and if
1374/// so, return true.
1375///
Devang Patelb67904d2006-12-13 02:36:01 +00001376bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001377 if (F.isMaterializable()) {
1378 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001379 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001380 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001381 }
Devang Patel272908d2006-12-08 22:57:48 +00001382 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001383}
1384
1385
Devang Patelff631ae2006-11-15 01:27:05 +00001386/// doInitialization - Run all of the initializers for the function passes.
1387///
Devang Patelb67904d2006-12-13 02:36:01 +00001388bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001389 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001390}
1391
Dan Gohmane6656eb2007-07-30 14:51:13 +00001392/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001393///
Devang Patelb67904d2006-12-13 02:36:01 +00001394bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001395 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001396}
1397
Devang Patela1514cb2006-12-07 19:39:39 +00001398//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001399// FunctionPassManagerImpl implementation
1400//
Duncan Sands51495602009-02-13 09:42:34 +00001401bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001402 bool Changed = false;
1403
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001404 dumpArguments();
1405 dumpPasses();
1406
Chris Lattner4c1e9542009-03-06 06:45:05 +00001407 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1408 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001409
1410 return Changed;
1411}
1412
Duncan Sands51495602009-02-13 09:42:34 +00001413bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001414 bool Changed = false;
1415
Chris Lattner4c1e9542009-03-06 06:45:05 +00001416 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1417 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001418
1419 return Changed;
1420}
1421
Devang Patelec9c58f2009-04-01 22:34:41 +00001422/// cleanup - After running all passes, clean up pass manager cache.
1423void FPPassManager::cleanup() {
1424 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1425 FunctionPass *FP = getContainedPass(Index);
1426 AnalysisResolver *AR = FP->getResolver();
1427 assert(AR && "Analysis Resolver is not set");
1428 AR->clearAnalysisImpls();
1429 }
1430}
1431
Torok Edwin24c78352009-06-29 18:49:09 +00001432void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1433 if (!wasRun)
1434 return;
1435 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1436 FPPassManager *FPPM = getContainedManager(Index);
1437 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1438 FPPM->getContainedPass(Index)->releaseMemory();
1439 }
1440 }
Torok Edwin896556e2009-06-29 21:05:10 +00001441 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001442}
1443
Devang Patel67d6a5e2006-12-19 19:46:59 +00001444// Execute all the passes managed by this top level manager.
1445// Return true if any function is modified by a pass.
1446bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001447 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001448 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001449 createDebugInfoProbe();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001450
Devang Patele3068402006-12-21 00:16:50 +00001451 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001452 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1453 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001454
1455 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1456 getContainedManager(Index)->cleanup();
1457
Torok Edwin24c78352009-06-29 18:49:09 +00001458 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001459 return Changed;
1460}
1461
1462//===----------------------------------------------------------------------===//
1463// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001464
Devang Patel8c78a0b2007-05-03 01:11:54 +00001465char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001466/// Print passes managed by this manager
1467void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001468 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001469 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1470 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001471 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001472 dumpLastUses(FP, Offset+1);
1473 }
1474}
1475
1476
Dan Gohmande6188a2010-08-12 23:50:08 +00001477/// Execute all of the passes scheduled for execution by invoking
1478/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001479/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001480bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001481 if (F.isDeclaration())
1482 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001483
1484 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001485
Devang Patelcbbf2912008-03-20 01:09:53 +00001486 // Collect inherited analysis from Module level pass manager.
1487 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001488
Devang Patelabfbe3b2006-12-16 00:56:26 +00001489 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1490 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001491 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001492
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001493 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001494 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001495
Devang Patelabfbe3b2006-12-16 00:56:26 +00001496 initializeAnalysisImpl(FP);
Devang Patelfa31d382011-03-10 00:21:25 +00001497 if (TheDebugProbe)
1498 TheDebugProbe->initialize(FP, F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001499 {
1500 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001501 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001502
Dan Gohman74b189f2010-03-01 17:34:28 +00001503 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001504 }
Devang Patelfa31d382011-03-10 00:21:25 +00001505 if (TheDebugProbe)
1506 TheDebugProbe->finalize(FP, F);
Devang Patel93a197c2006-12-14 00:08:04 +00001507
Dan Gohman74b189f2010-03-01 17:34:28 +00001508 Changed |= LocalChanged;
1509 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001510 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001511 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001512
Devang Patela273d1c2007-07-19 18:02:32 +00001513 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001514 removeNotPreservedAnalysis(FP);
1515 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001516 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001517 }
1518 return Changed;
1519}
1520
Devang Patel67d6a5e2006-12-19 19:46:59 +00001521bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001522 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001523
Dan Gohmane7630be2010-05-11 20:30:00 +00001524 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner60987362009-03-06 05:53:14 +00001525 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001526
Bill Wendling6ce6d262009-12-25 13:50:18 +00001527 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528}
1529
Duncan Sands51495602009-02-13 09:42:34 +00001530bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001531 bool Changed = false;
1532
Chris Lattner4c1e9542009-03-06 06:45:05 +00001533 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1534 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001535
1536 return Changed;
1537}
1538
Duncan Sands51495602009-02-13 09:42:34 +00001539bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001540 bool Changed = false;
1541
Chris Lattner4c1e9542009-03-06 06:45:05 +00001542 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1543 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001544
Devang Patelff631ae2006-11-15 01:27:05 +00001545 return Changed;
1546}
1547
Devang Patela1514cb2006-12-07 19:39:39 +00001548//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001549// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001550
Dan Gohmande6188a2010-08-12 23:50:08 +00001551/// Execute all of the passes scheduled for execution by invoking
1552/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001553/// the module, and if so, return true.
1554bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001555MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001556 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001557
Torok Edwin24c78352009-06-29 18:49:09 +00001558 // Initialize on-the-fly passes
1559 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1560 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1561 I != E; ++I) {
1562 FunctionPassManagerImpl *FPP = I->second;
1563 Changed |= FPP->doInitialization(M);
1564 }
1565
Devang Patelabfbe3b2006-12-16 00:56:26 +00001566 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1567 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001568 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001569
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001570 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001571 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001572
Devang Patelabfbe3b2006-12-16 00:56:26 +00001573 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001574
Chris Lattner4c1e9542009-03-06 06:45:05 +00001575 {
1576 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001577 TimeRegion PassTimer(getPassTimer(MP));
1578
Dan Gohman74b189f2010-03-01 17:34:28 +00001579 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001580 }
Devang Patel93a197c2006-12-14 00:08:04 +00001581
Dan Gohman74b189f2010-03-01 17:34:28 +00001582 Changed |= LocalChanged;
1583 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001584 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001585 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001586 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001587
Devang Patela273d1c2007-07-19 18:02:32 +00001588 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001589 removeNotPreservedAnalysis(MP);
1590 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001591 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001592 }
Torok Edwin24c78352009-06-29 18:49:09 +00001593
1594 // Finalize on-the-fly passes
1595 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1596 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1597 I != E; ++I) {
1598 FunctionPassManagerImpl *FPP = I->second;
1599 // We don't know when is the last time an on-the-fly pass is run,
1600 // so we need to releaseMemory / finalize here
1601 FPP->releaseMemoryOnTheFly();
1602 Changed |= FPP->doFinalization(M);
1603 }
Devang Patel05e1a972006-11-07 22:03:15 +00001604 return Changed;
1605}
1606
Devang Patele64d3052007-04-16 20:12:57 +00001607/// Add RequiredPass into list of lower level passes required by pass P.
1608/// RequiredPass is run on the fly by Pass Manager when P requests it
1609/// through getAnalysis interface.
1610void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001611 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1612 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001613 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001614 RequiredPass->getPotentialPassManagerType()) &&
1615 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001616
Devang Patel68f72b12007-04-26 17:50:19 +00001617 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001618 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001619 FPP = new FunctionPassManagerImpl(0);
1620 // FPP is the top level manager.
1621 FPP->setTopLevelManager(FPP);
1622
Devang Patel69e9f6d2007-04-16 20:27:05 +00001623 OnTheFlyManagers[P] = FPP;
1624 }
Devang Patel68f72b12007-04-26 17:50:19 +00001625 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001626
Devang Patel68f72b12007-04-26 17:50:19 +00001627 // Register P as the last user of RequiredPass.
Dan Gohmanc450d2c2010-10-12 00:13:43 +00001628 SmallVector<Pass *, 1> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001629 LU.push_back(RequiredPass);
1630 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001631}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001632
Dan Gohmande6188a2010-08-12 23:50:08 +00001633/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001634/// required by module pass MP. Instantiate analysis pass, by using
1635/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001636Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001637 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001638 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001639
Torok Edwin24c78352009-06-29 18:49:09 +00001640 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001641 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001642 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001643}
1644
1645
Devang Patela1514cb2006-12-07 19:39:39 +00001646//===----------------------------------------------------------------------===//
1647// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001648//
Devang Patelc290c8a2006-11-07 22:23:34 +00001649/// run - Execute all of the passes scheduled for execution. Keep track of
1650/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001651bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001652 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001653 TimingInfo::createTheTimeInfo();
Devang Patelfa31d382011-03-10 00:21:25 +00001654 createDebugInfoProbe();
Devang Patelb8817b92006-12-14 00:59:42 +00001655
Devang Patelcfd70c42006-12-13 22:10:00 +00001656 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001657 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001658
Devang Patele3068402006-12-21 00:16:50 +00001659 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001660 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1661 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001662 return Changed;
1663}
Devang Patel376fefa2006-11-08 10:29:57 +00001664
Devang Patela1514cb2006-12-07 19:39:39 +00001665//===----------------------------------------------------------------------===//
1666// PassManager implementation
1667
Devang Patel376fefa2006-11-08 10:29:57 +00001668/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001669PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001670 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001671 // PM is the top level manager
1672 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001673}
1674
Devang Patelb67904d2006-12-13 02:36:01 +00001675PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001676 delete PM;
1677}
1678
David Greene103d4b42010-05-10 20:24:27 +00001679/// addImpl - Add a pass to the queue of passes to run, without
1680/// checking whether to add a printer pass.
1681void PassManager::addImpl(Pass *P) {
1682 PM->add(P);
1683}
1684
Devang Patel376fefa2006-11-08 10:29:57 +00001685/// add - Add a pass to the queue of passes to run. This passes ownership of
1686/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1687/// will be destroyed as well, so there is no need to delete the pass. This
1688/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001689void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001690 const void* PassID = P->getPassID();
1691 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001692 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1693 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001694
David Greene103d4b42010-05-10 20:24:27 +00001695 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001696
Owen Andersona7aed182010-08-06 18:33:48 +00001697 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001698 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1699 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001700}
1701
1702/// run - Execute all of the passes scheduled for execution. Keep track of
1703/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001704bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001705 return PM->run(M);
1706}
1707
Devang Patelb8817b92006-12-14 00:59:42 +00001708//===----------------------------------------------------------------------===//
1709// TimingInfo Class - This class is used to calculate information about the
1710// amount of time each pass takes to execute. This only happens with
1711// -time-passes is enabled on the command line.
1712//
1713bool llvm::TimePassesIsEnabled = false;
1714static cl::opt<bool,true>
1715EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1716 cl::desc("Time each pass, printing elapsed time for each on exit"));
1717
1718// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1719// a non null value (if the -time-passes option is enabled) or it leaves it
1720// null. It may be called multiple times.
1721void TimingInfo::createTheTimeInfo() {
1722 if (!TimePassesIsEnabled || TheTimeInfo) return;
1723
1724 // Constructed the first time this is called, iff -time-passes is enabled.
1725 // This guarantees that the object will be constructed before static globals,
1726 // thus it will be destroyed before them.
1727 static ManagedStatic<TimingInfo> TTI;
1728 TheTimeInfo = &*TTI;
1729}
1730
Devang Patel1c3633e2007-01-29 23:10:37 +00001731/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001732Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001733 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001734 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001735 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001736}
1737
Devang Patel1c56a632007-01-08 19:29:38 +00001738//===----------------------------------------------------------------------===//
1739// PMStack implementation
1740//
Devang Patelad98d232007-01-11 22:15:30 +00001741
Devang Patel1c56a632007-01-08 19:29:38 +00001742// Pop Pass Manager from the stack and clear its analysis info.
1743void PMStack::pop() {
1744
1745 PMDataManager *Top = this->top();
1746 Top->initializeAnalysisInfo();
1747
1748 S.pop_back();
1749}
1750
1751// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001752void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001753 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001754
Chris Lattner60987362009-03-06 05:53:14 +00001755 if (!this->empty()) {
1756 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001757
Chris Lattner60987362009-03-06 05:53:14 +00001758 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001759 TPM->addIndirectPassManager(PM);
1760 PM->setTopLevelManager(TPM);
1761 }
1762
Devang Patel15701b52007-01-11 00:19:00 +00001763 S.push_back(PM);
1764}
1765
1766// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001767void PMStack::dump() const {
1768 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001769 E = S.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +00001770 printf("%s ", (*I)->getAsPass()->getPassName());
Chris Lattner60987362009-03-06 05:53:14 +00001771
Devang Patel15701b52007-01-11 00:19:00 +00001772 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001773 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001774}
1775
Devang Patel1c56a632007-01-08 19:29:38 +00001776/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001777/// add self into that manager.
1778void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001779 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001780 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001781 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001782 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1783 if (TopPMType == PreferredType)
1784 break; // We found desired pass manager
1785 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001786 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001787 else
1788 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001789 }
Devang Patel18ff6362008-09-09 21:38:40 +00001790 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001791 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001792}
1793
Devang Patel3312f752007-01-16 21:43:18 +00001794/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001795/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001796void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001797 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001798
Devang Patela3286902008-09-09 17:56:50 +00001799 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001800 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001801 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1802 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001803 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001804 break;
Devang Patel3312f752007-01-16 21:43:18 +00001805 }
Devang Patel3312f752007-01-16 21:43:18 +00001806
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001807 // Create new Function Pass Manager if needed.
1808 FPPassManager *FPP;
1809 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1810 FPP = (FPPassManager *)PMS.top();
1811 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001812 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1813 PMDataManager *PMD = PMS.top();
1814
1815 // [1] Create new Function Pass Manager
1816 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001817 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001818
1819 // [2] Set up new manager's top level manager
1820 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1821 TPM->addIndirectPassManager(FPP);
1822
1823 // [3] Assign manager to manage this new manager. This may create
1824 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001825 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001826
1827 // [4] Push new manager into PMS
1828 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001829 }
1830
Devang Patel3312f752007-01-16 21:43:18 +00001831 // Assign FPP as the manager of this pass.
1832 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001833}
1834
Devang Patel3312f752007-01-16 21:43:18 +00001835/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001836/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001837void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001838 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001839 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001840
Devang Patel15701b52007-01-11 00:19:00 +00001841 // Basic Pass Manager is a leaf pass manager. It does not handle
1842 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001843 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001844 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1845 BBP = (BBPassManager *)PMS.top();
1846 } else {
1847 // If leaf manager is not Basic Block Pass manager then create new
1848 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001849 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1850 PMDataManager *PMD = PMS.top();
1851
1852 // [1] Create new Basic Block Manager
1853 BBP = new BBPassManager(PMD->getDepth() + 1);
1854
1855 // [2] Set up new manager's top level manager
1856 // Basic Block Pass Manager does not live by itself
1857 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1858 TPM->addIndirectPassManager(BBP);
1859
Devang Patel15701b52007-01-11 00:19:00 +00001860 // [3] Assign manager to manage this new manager. This may create
1861 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001862 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001863
Devang Patel3312f752007-01-16 21:43:18 +00001864 // [4] Push new manager into PMS
1865 PMS.push(BBP);
1866 }
Devang Patel1c56a632007-01-08 19:29:38 +00001867
Devang Patel3312f752007-01-16 21:43:18 +00001868 // Assign BBP as the manager of this pass.
1869 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001870}
1871
Dan Gohmand3a20c92008-03-11 16:41:42 +00001872PassManagerBase::~PassManagerBase() {}