blob: 6ca35ac0260f686337086861d6dcccc9a5e63f9b [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//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
David Greene9b063df2010-04-02 23:17:14 +000016#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000017#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000018#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000019#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000020#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000021#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000023#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000024#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000025#include "llvm/Support/raw_ostream.h"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000026#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000027#include "llvm/System/Threading.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000028#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000029#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000030#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000031#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000033
Devang Patele7599552007-01-12 18:52:44 +000034// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000035
Devang Patelf1567a52006-12-13 20:03:48 +000036namespace llvm {
37
38//===----------------------------------------------------------------------===//
39// Pass debugging information. Often it is useful to find out what pass is
40// running when a crash occurs in a utility. When this library is compiled with
41// debugging on, a command line option (--debug-pass) is enabled that causes the
42// pass name to be printed before it executes.
43//
44
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
47 None, Arguments, Structure, Executions, Details
48};
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000060
61typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
63
64// Print IR out before/after specified passes.
65static PassOptionList
66PrintBefore("print-before",
67 llvm::cl::desc("Print IR before specified passes"));
68
69static PassOptionList
70PrintAfter("print-after",
71 llvm::cl::desc("Print IR after specified passes"));
72
73static cl::opt<bool>
74PrintBeforeAll("print-before-all",
75 llvm::cl::desc("Print IR before each pass"),
76 cl::init(false));
77static cl::opt<bool>
78PrintAfterAll("print-after-all",
79 llvm::cl::desc("Print IR after each pass"),
80 cl::init(false));
81
82/// This is a helper to determine whether to print IR before or
83/// after a pass.
84
85static bool ShouldPrintBeforeOrAfterPass(Pass *P,
86 PassOptionList &PassesToPrint) {
87 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
88 const llvm::PassInfo *PassInf = PassesToPrint[i];
89 if (PassInf && P->getPassInfo())
90 if (PassInf->getPassArgument() ==
91 P->getPassInfo()->getPassArgument()) {
92 return true;
93 }
94 }
95 return false;
96}
97
98
99/// This is a utility to check whether a pass should have IR dumped
100/// before it.
101static bool ShouldPrintBeforePass(Pass *P) {
102 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(P, PrintBefore);
103}
104
105/// This is a utility to check whether a pass should have IR dumped
106/// after it.
107static bool ShouldPrintAfterPass(Pass *P) {
108 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(P, PrintAfter);
109}
110
Devang Patelf1567a52006-12-13 20:03:48 +0000111} // End of llvm namespace
112
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000113/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
114/// or higher is specified.
115bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
116 return PassDebugging >= Executions;
117}
118
119
120
121
Chris Lattner4c1e9542009-03-06 06:45:05 +0000122void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
123 if (V == 0 && M == 0)
124 OS << "Releasing pass '";
125 else
126 OS << "Running pass '";
127
128 OS << P->getPassName() << "'";
129
130 if (M) {
131 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
132 return;
133 }
134 if (V == 0) {
135 OS << '\n';
136 return;
137 }
138
Dan Gohman79fc0e92009-03-10 18:47:59 +0000139 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000141 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000143 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000144 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000145 OS << "value";
146
147 OS << " '";
148 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
149 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000150}
151
152
Devang Patelffca9102006-12-15 19:39:30 +0000153namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000154
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000157//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000159/// pass together and sequence them to process one basic block before
160/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000161class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000162
163public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000164 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000165 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000166 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000167
Devang Patelca58e352006-11-08 10:05:38 +0000168 /// Execute all of the passes scheduled for execution. Keep track of
169 /// whether any of the passes modifies the function, and if so, return true.
170 bool runOnFunction(Function &F);
171
Devang Patelf9d96b92006-12-07 19:57:52 +0000172 /// Pass Manager itself does not invalidate any analysis info.
173 void getAnalysisUsage(AnalysisUsage &Info) const {
174 Info.setPreservesAll();
175 }
176
Devang Patel475c4532006-12-08 00:59:05 +0000177 bool doInitialization(Module &M);
178 bool doInitialization(Function &F);
179 bool doFinalization(Module &M);
180 bool doFinalization(Function &F);
181
Chris Lattner2fa26e52010-01-22 05:24:46 +0000182 virtual PMDataManager *getAsPMDataManager() { return this; }
183 virtual Pass *getAsPass() { return this; }
184
Devang Patele3858e62007-02-01 22:08:25 +0000185 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000186 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000187 }
188
Devang Pateleda56172006-12-12 23:34:33 +0000189 // Print passes managed by this manager
190 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000191 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000192 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
193 BasicBlockPass *BP = getContainedPass(Index);
194 BP->dumpPassStructure(Offset + 1);
195 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000196 }
197 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000198
199 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000200 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000201 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
202 return BP;
203 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000204
Devang Patel28349ab2007-02-27 15:00:39 +0000205 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000206 return PMT_BasicBlockPassManager;
207 }
Devang Patelca58e352006-11-08 10:05:38 +0000208};
209
Devang Patel8c78a0b2007-05-03 01:11:54 +0000210char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000211}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000212
Devang Patele7599552007-01-12 18:52:44 +0000213namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000214
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000217//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218/// FunctionPassManagerImpl manages FPPassManagers
219class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000220 public PMDataManager,
221 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000222private:
223 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000224public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000225 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000226 explicit FunctionPassManagerImpl(int Depth) :
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000227 Pass(PT_PassManager, &ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000228 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000229
230 /// add - Add a pass to the queue of passes to run. This passes ownership of
231 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
232 /// will be destroyed as well, so there is no need to delete the pass. This
233 /// implies that all passes MUST be allocated with 'new'.
234 void add(Pass *P) {
235 schedulePass(P);
236 }
237
David Greene9b063df2010-04-02 23:17:14 +0000238 /// createPrinterPass - Get a function printer pass.
239 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
240 return createPrintFunctionPass(Banner, &O);
241 }
242
Torok Edwin24c78352009-06-29 18:49:09 +0000243 // Prepare for running an on the fly pass, freeing memory if needed
244 // from a previous run.
245 void releaseMemoryOnTheFly();
246
Devang Patel67d6a5e2006-12-19 19:46:59 +0000247 /// run - Execute all of the passes scheduled for execution. Keep track of
248 /// whether any of the passes modifies the module, and if so, return true.
249 bool run(Function &F);
250
251 /// doInitialization - Run all of the initializers for the function passes.
252 ///
253 bool doInitialization(Module &M);
254
Dan Gohmane6656eb2007-07-30 14:51:13 +0000255 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000256 ///
257 bool doFinalization(Module &M);
258
Chris Lattner2fa26e52010-01-22 05:24:46 +0000259
260 virtual PMDataManager *getAsPMDataManager() { return this; }
261 virtual Pass *getAsPass() { return this; }
262
Devang Patel67d6a5e2006-12-19 19:46:59 +0000263 /// Pass Manager itself does not invalidate any analysis info.
264 void getAnalysisUsage(AnalysisUsage &Info) const {
265 Info.setPreservesAll();
266 }
267
268 inline void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000269 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 // P is a immutable pass and it will be managed by this
271 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000272 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000273 P->setResolver(AR);
274 initializeAnalysisImpl(P);
275 addImmutablePass(IP);
276 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000277 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000278 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000279 }
Devang Patel0f080042007-01-12 17:23:48 +0000280
Devang Patel67d6a5e2006-12-19 19:46:59 +0000281 }
282
283 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000284 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000285 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
286 return FP;
287 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000288};
289
Devang Patel8c78a0b2007-05-03 01:11:54 +0000290char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000291//===----------------------------------------------------------------------===//
292// MPPassManager
293//
294/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000295/// It batches all Module passes and function pass managers together and
296/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000297class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000298public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000299 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000300 explicit MPPassManager(int Depth) :
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000301 Pass(PT_PassManager, &ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000302
303 // Delete on the fly managers.
304 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000305 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000306 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
307 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000308 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000309 delete FPP;
310 }
311 }
312
David Greene9b063df2010-04-02 23:17:14 +0000313 /// createPrinterPass - Get a module printer pass.
314 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
315 return createPrintModulePass(&O, false, Banner);
316 }
317
Devang Patelca58e352006-11-08 10:05:38 +0000318 /// run - Execute all of the passes scheduled for execution. Keep track of
319 /// whether any of the passes modifies the module, and if so, return true.
320 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000321
Devang Patelf9d96b92006-12-07 19:57:52 +0000322 /// Pass Manager itself does not invalidate any analysis info.
323 void getAnalysisUsage(AnalysisUsage &Info) const {
324 Info.setPreservesAll();
325 }
326
Devang Patele64d3052007-04-16 20:12:57 +0000327 /// Add RequiredPass into list of lower level passes required by pass P.
328 /// RequiredPass is run on the fly by Pass Manager when P requests it
329 /// through getAnalysis interface.
330 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
331
Devang Patel69e9f6d2007-04-16 20:27:05 +0000332 /// Return function pass corresponding to PassInfo PI, that is
333 /// required by module pass MP. Instantiate analysis pass, by using
334 /// its runOnFunction() for function F.
335 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
336
Devang Patele3858e62007-02-01 22:08:25 +0000337 virtual const char *getPassName() const {
338 return "Module Pass Manager";
339 }
340
Chris Lattner2fa26e52010-01-22 05:24:46 +0000341 virtual PMDataManager *getAsPMDataManager() { return this; }
342 virtual Pass *getAsPass() { return this; }
343
Devang Pateleda56172006-12-12 23:34:33 +0000344 // Print passes managed by this manager
345 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000346 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000347 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
348 ModulePass *MP = getContainedPass(Index);
349 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000350 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
351 OnTheFlyManagers.find(MP);
352 if (I != OnTheFlyManagers.end())
353 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000354 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000355 }
356 }
357
Devang Patelabfbe3b2006-12-16 00:56:26 +0000358 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000359 assert(N < PassVector.size() && "Pass number out of range!");
360 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361 }
362
Devang Patel28349ab2007-02-27 15:00:39 +0000363 virtual PassManagerType getPassManagerType() const {
364 return PMT_ModulePassManager;
365 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000366
367 private:
368 /// Collection of on the fly FPPassManagers. These managers manage
369 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000370 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000371};
372
Devang Patel8c78a0b2007-05-03 01:11:54 +0000373char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000374//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000375// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000376//
Devang Patel09f162c2007-05-01 21:15:47 +0000377
Devang Patel67d6a5e2006-12-19 19:46:59 +0000378/// PassManagerImpl manages MPPassManagers
379class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000380 public PMDataManager,
381 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000382
383public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000384 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000385 explicit PassManagerImpl(int Depth) :
Chris Lattnerc7a8eaf2010-01-22 06:03:06 +0000386 Pass(PT_PassManager, &ID), PMDataManager(Depth),
387 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000388
Devang Patel376fefa2006-11-08 10:29:57 +0000389 /// add - Add a pass to the queue of passes to run. This passes ownership of
390 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
391 /// will be destroyed as well, so there is no need to delete the pass. This
392 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000393 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000394 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000395 }
Devang Patel376fefa2006-11-08 10:29:57 +0000396
David Greene9b063df2010-04-02 23:17:14 +0000397 /// createPrinterPass - Get a module printer pass.
398 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
399 return createPrintModulePass(&O, false, Banner);
400 }
401
Devang Patel376fefa2006-11-08 10:29:57 +0000402 /// run - Execute all of the passes scheduled for execution. Keep track of
403 /// whether any of the passes modifies the module, and if so, return true.
404 bool run(Module &M);
405
Devang Patelf9d96b92006-12-07 19:57:52 +0000406 /// Pass Manager itself does not invalidate any analysis info.
407 void getAnalysisUsage(AnalysisUsage &Info) const {
408 Info.setPreservesAll();
409 }
410
Devang Patelabcd1d32006-12-07 21:27:23 +0000411 inline void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000412 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000413 // P is a immutable pass and it will be managed by this
414 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000415 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000416 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000417 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000418 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000419 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000420 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000421 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000422 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000423 }
424
Chris Lattner2fa26e52010-01-22 05:24:46 +0000425 virtual PMDataManager *getAsPMDataManager() { return this; }
426 virtual Pass *getAsPass() { return this; }
427
Devang Patel67d6a5e2006-12-19 19:46:59 +0000428 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000429 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000430 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
431 return MP;
432 }
Devang Patel376fefa2006-11-08 10:29:57 +0000433};
434
Devang Patel8c78a0b2007-05-03 01:11:54 +0000435char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000436} // End of llvm namespace
437
438namespace {
439
440//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000441/// TimingInfo Class - This class is used to calculate information about the
442/// amount of time each pass takes to execute. This only happens when
443/// -time-passes is enabled on the command line.
444///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000445
Owen Anderson5a6960f2009-06-18 20:51:00 +0000446static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000447
Nick Lewycky02d5f772009-10-25 06:33:48 +0000448class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000449 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000450 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000451public:
452 // Use 'create' member to get this.
453 TimingInfo() : TG("... Pass execution timing report ...") {}
454
455 // TimingDtor - Print out information about timing information
456 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000457 // Delete all of the timers, which accumulate their info into the
458 // TimerGroup.
459 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
460 E = TimingData.end(); I != E; ++I)
461 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000462 // TimerGroup is deleted next, printing the report.
463 }
464
465 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
466 // to a non null value (if the -time-passes option is enabled) or it leaves it
467 // null. It may be called multiple times.
468 static void createTheTimeInfo();
469
Chris Lattner707431c2010-03-30 04:03:22 +0000470 /// getPassTimer - Return the timer for the specified pass if it exists.
471 Timer *getPassTimer(Pass *P) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000472 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000473 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000474
Owen Anderson5c96ef72009-07-07 18:33:04 +0000475 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000476 Timer *&T = TimingData[P];
477 if (T == 0)
478 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000479 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000480 }
481};
482
Devang Patel1c3633e2007-01-29 23:10:37 +0000483} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000484
Dan Gohmand78c4002008-05-13 00:00:25 +0000485static TimingInfo *TheTimeInfo;
486
Devang Patela1514cb2006-12-07 19:39:39 +0000487//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000488// PMTopLevelManager implementation
489
Devang Patel4268fc02007-01-16 02:00:38 +0000490/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000491PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000492 if (t == TLM_Pass) {
493 MPPassManager *MPP = new MPPassManager(1);
494 MPP->setTopLevelManager(this);
495 addPassManager(MPP);
496 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000497 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000498 FPPassManager *FPP = new FPPassManager(1);
499 FPP->setTopLevelManager(this);
500 addPassManager(FPP);
501 activeStack.push(FPP);
502 }
503}
504
Devang Patelafb1f3622006-12-12 22:35:25 +0000505/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000506void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000507 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000508 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000509 E = AnalysisPasses.end(); I != E; ++I) {
510 Pass *AP = *I;
511 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000512
513 if (P == AP)
514 continue;
515
Devang Patelafb1f3622006-12-12 22:35:25 +0000516 // If AP is the last user of other passes then make P last user of
517 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000518 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 LUE = LastUser.end(); LUI != LUE; ++LUI) {
520 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000521 // DenseMap iterator is not invalidated here because
522 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000523 LastUser[LUI->first] = P;
524 }
525 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000526}
527
528/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000529void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000530 Pass *P) {
531 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
532 InversedLastUser.find(P);
533 if (DMI == InversedLastUser.end())
534 return;
535
536 SmallPtrSet<Pass *, 8> &LU = DMI->second;
537 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
538 E = LU.end(); I != E; ++I) {
539 LastUses.push_back(*I);
540 }
541
Devang Patelafb1f3622006-12-12 22:35:25 +0000542}
543
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000544AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
545 AnalysisUsage *AnUsage = NULL;
546 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
547 if (DMI != AnUsageMap.end())
548 AnUsage = DMI->second;
549 else {
550 AnUsage = new AnalysisUsage();
551 P->getAnalysisUsage(*AnUsage);
552 AnUsageMap[P] = AnUsage;
553 }
554 return AnUsage;
555}
556
Devang Patelafb1f3622006-12-12 22:35:25 +0000557/// Schedule pass P for execution. Make sure that passes required by
558/// P are run before P is run. Update analysis info maintained by
559/// the manager. Remove dead passes. This is a recursive function.
560void PMTopLevelManager::schedulePass(Pass *P) {
561
Devang Patel3312f752007-01-16 21:43:18 +0000562 // TODO : Allocate function manager for this pass, other wise required set
563 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000564
Devang Pateld74ede72007-03-06 01:06:16 +0000565 // Give pass a chance to prepare the stage.
566 P->preparePassManager(activeStack);
567
Devang Patel864970e2008-03-18 00:39:19 +0000568 // If P is an analysis pass and it is available then do not
569 // generate the analysis again. Stale analysis info should not be
570 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000571 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000572 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
573 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000574 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000575 }
Devang Patel864970e2008-03-18 00:39:19 +0000576
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000577 AnalysisUsage *AnUsage = findAnalysisUsage(P);
578
Devang Patelfdee7032008-08-14 23:07:48 +0000579 bool checkAnalysis = true;
580 while (checkAnalysis) {
581 checkAnalysis = false;
582
583 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
584 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
585 E = RequiredSet.end(); I != E; ++I) {
586
587 Pass *AnalysisPass = findAnalysisPass(*I);
588 if (!AnalysisPass) {
589 AnalysisPass = (*I)->createPass();
590 if (P->getPotentialPassManagerType () ==
591 AnalysisPass->getPotentialPassManagerType())
592 // Schedule analysis pass that is managed by the same pass manager.
593 schedulePass(AnalysisPass);
594 else if (P->getPotentialPassManagerType () >
595 AnalysisPass->getPotentialPassManagerType()) {
596 // Schedule analysis pass that is managed by a new manager.
597 schedulePass(AnalysisPass);
598 // Recheck analysis passes to ensure that required analysises that
599 // are already checked are still available.
600 checkAnalysis = true;
601 }
602 else
603 // Do not schedule this analysis. Lower level analsyis
604 // passes are run on the fly.
605 delete AnalysisPass;
606 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000607 }
608 }
609
610 // Now all required passes are available.
611 addTopLevelPass(P);
612}
613
614/// Find the pass that implements Analysis AID. Search immutable
615/// passes and all pass managers. If desired pass is not found
616/// then return NULL.
617Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
618
619 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000620 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000621 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000622 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000623 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000624 P = PMD->findAnalysisPass(AID, false);
625 }
626
627 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000628 for (SmallVector<PMDataManager *, 8>::iterator
629 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000630 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
631 P = (*I)->findAnalysisPass(AID, false);
632
Devang Patel0d29ae02008-08-12 15:44:31 +0000633 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000634 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
635 const PassInfo *PI = (*I)->getPassInfo();
636 if (PI == AID)
637 P = *I;
638
639 // If Pass not found then check the interfaces implemented by Immutable Pass
640 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000641 const std::vector<const PassInfo*> &ImmPI =
642 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000643 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
644 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000645 }
646 }
647
Devang Patelafb1f3622006-12-12 22:35:25 +0000648 return P;
649}
650
Devang Pateleda56172006-12-12 23:34:33 +0000651// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000652void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000653
Devang Patelfd4184322007-01-17 20:33:36 +0000654 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000655 return;
656
Devang Pateleda56172006-12-12 23:34:33 +0000657 // Print out the immutable passes
658 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
659 ImmutablePasses[i]->dumpPassStructure(0);
660 }
661
Dan Gohman73caf5f2008-03-13 01:48:32 +0000662 // Every class that derives from PMDataManager also derives from Pass
663 // (sometimes indirectly), but there's no inheritance relationship
Chris Lattner2fa26e52010-01-22 05:24:46 +0000664 // between PMDataManager and Pass, so we have to getAsPass to get
Dan Gohman73caf5f2008-03-13 01:48:32 +0000665 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000666 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000667 E = PassManagers.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +0000668 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000669}
670
Devang Patel991aeba2006-12-15 20:13:01 +0000671void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000672
Devang Patelfd4184322007-01-17 20:33:36 +0000673 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000674 return;
675
David Greene994e1bb2010-01-05 01:30:02 +0000676 dbgs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000677 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000678 E = PassManagers.end(); I != E; ++I)
679 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000680 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000681}
682
Devang Patele3068402006-12-21 00:16:50 +0000683void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000684 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000685 E = PassManagers.end(); I != E; ++I)
686 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000687
688 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000689 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000690 E = IndirectPassManagers.end(); I != E; ++I)
691 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000692
Chris Lattner60987362009-03-06 05:53:14 +0000693 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000694 DME = LastUser.end(); DMI != DME; ++DMI) {
695 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
696 InversedLastUser.find(DMI->second);
697 if (InvDMI != InversedLastUser.end()) {
698 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
699 L.insert(DMI->first);
700 } else {
701 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
702 InversedLastUser[DMI->second] = L;
703 }
704 }
Devang Patele3068402006-12-21 00:16:50 +0000705}
706
Devang Patele7599552007-01-12 18:52:44 +0000707/// Destructor
708PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000709 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000710 E = PassManagers.end(); I != E; ++I)
711 delete *I;
712
Devang Patel0d29ae02008-08-12 15:44:31 +0000713 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000714 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
715 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000716
717 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000718 DME = AnUsageMap.end(); DMI != DME; ++DMI)
719 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000720}
721
Devang Patelafb1f3622006-12-12 22:35:25 +0000722//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000723// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000724
Devang Patel643676c2006-11-11 01:10:19 +0000725/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000726void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000727 const PassInfo *PI = P->getPassInfo();
728 if (PI == 0) return;
729
730 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000731
Chris Lattner60987362009-03-06 05:53:14 +0000732 //This pass is the current implementation of all of the interfaces it
733 //implements as well.
734 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
735 for (unsigned i = 0, e = II.size(); i != e; ++i)
736 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000737}
738
Devang Patel9d9fc902007-03-06 17:52:53 +0000739// Return true if P preserves high level analysis used by other
740// passes managed by this manager
741bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000742 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000743 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000744 return true;
745
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000746 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000747 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000748 E = HigherLevelAnalysis.end(); I != E; ++I) {
749 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000750 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000751 std::find(PreservedSet.begin(), PreservedSet.end(),
752 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000753 PreservedSet.end())
754 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000755 }
756
757 return true;
758}
759
Chris Lattner02eb94c2008-08-07 07:34:50 +0000760/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000761void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000762 // Don't do this unless assertions are enabled.
763#ifdef NDEBUG
764 return;
765#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000766 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
767 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000768
Devang Patelef432532007-07-19 05:36:09 +0000769 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000770 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000771 E = PreservedSet.end(); I != E; ++I) {
772 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000773 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000774 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000775 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000776 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000777 }
778}
779
Devang Patel67c79a42008-07-01 19:50:56 +0000780/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000781void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000782 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
783 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000784 return;
785
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000786 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000787 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000788 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000789 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000790 if (Info->second->getAsImmutablePass() == 0 &&
791 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000792 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000793 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000794 if (PassDebugging >= Details) {
795 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000796 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
797 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000798 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000799 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000800 }
Devang Patel349170f2006-11-11 01:24:55 +0000801 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000802
803 // Check inherited analysis also. If P is not preserving analysis
804 // provided by parent manager then remove it here.
805 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
806
807 if (!InheritedAnalysis[Index])
808 continue;
809
810 for (std::map<AnalysisID, Pass*>::iterator
811 I = InheritedAnalysis[Index]->begin(),
812 E = InheritedAnalysis[Index]->end(); I != E; ) {
813 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000814 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000815 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000816 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000817 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000818 if (PassDebugging >= Details) {
819 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000820 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
821 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000822 }
Devang Patel01919d22007-03-08 19:05:01 +0000823 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000824 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000825 }
826 }
Devang Patelf68a3492006-11-07 22:35:17 +0000827}
828
Devang Patelca189262006-11-14 03:05:08 +0000829/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000830void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000831 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000832
Devang Patel8adae862007-07-20 18:04:54 +0000833 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000834
Devang Patel2ff44922007-04-16 20:39:59 +0000835 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000836 if (!TPM)
837 return;
838
Devang Patel17ad0962006-12-08 00:37:52 +0000839 TPM->collectLastUses(DeadPasses, P);
840
Devang Patel656a9172008-06-06 17:50:36 +0000841 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000842 dbgs() << " -*- '" << P->getPassName();
843 dbgs() << "' is the last user of following pass instances.";
844 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000845 }
846
Devang Patel8adae862007-07-20 18:04:54 +0000847 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000848 E = DeadPasses.end(); I != E; ++I)
849 freePass(*I, Msg, DBG_STR);
850}
Devang Patel200d3052006-12-13 23:50:44 +0000851
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000852void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000853 enum PassDebuggingString DBG_STR) {
854 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000855
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000856 {
857 // If the pass crashes releasing memory, remember this.
858 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000859 TimeRegion PassTimer(getPassTimer(P));
860
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000861 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000862 }
863
864 if (const PassInfo *PI = P->getPassInfo()) {
865 // Remove the pass itself (if it is not already removed).
866 AvailableAnalysis.erase(PI);
867
868 // Remove all interfaces this pass implements, for which it is also
869 // listed as the available implementation.
870 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
871 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000872 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000873 AvailableAnalysis.find(II[i]);
874 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000875 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000876 }
Devang Patel17ad0962006-12-08 00:37:52 +0000877 }
Devang Patelca189262006-11-14 03:05:08 +0000878}
879
Devang Patel8f677ce2006-12-07 18:47:25 +0000880/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000881/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000882void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000883 // This manager is going to manage pass P. Set up analysis resolver
884 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000885 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000886 P->setResolver(AR);
887
Devang Patelec2b9a72007-03-05 22:57:49 +0000888 // If a FunctionPass F is the last user of ModulePass info M
889 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000890 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000891
Chris Lattner60987362009-03-06 05:53:14 +0000892 if (!ProcessAnalysis) {
893 // Add pass
894 PassVector.push_back(P);
895 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000896 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000897
Chris Lattner60987362009-03-06 05:53:14 +0000898 // At the moment, this pass is the last user of all required passes.
899 SmallVector<Pass *, 12> LastUses;
900 SmallVector<Pass *, 8> RequiredPasses;
901 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
902
903 unsigned PDepth = this->getDepth();
904
905 collectRequiredAnalysis(RequiredPasses,
906 ReqAnalysisNotAvailable, P);
907 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
908 E = RequiredPasses.end(); I != E; ++I) {
909 Pass *PRequired = *I;
910 unsigned RDepth = 0;
911
912 assert(PRequired->getResolver() && "Analysis Resolver is not set");
913 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
914 RDepth = DM.getDepth();
915
916 if (PDepth == RDepth)
917 LastUses.push_back(PRequired);
918 else if (PDepth > RDepth) {
919 // Let the parent claim responsibility of last use
920 TransferLastUses.push_back(PRequired);
921 // Keep track of higher level analysis used by this manager.
922 HigherLevelAnalysis.push_back(PRequired);
923 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000924 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000925 }
926
927 // Set P as P's last user until someone starts using P.
928 // However, if P is a Pass Manager then it does not need
929 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000930 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000931 LastUses.push_back(P);
932 TPM->setLastUser(LastUses, P);
933
934 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000935 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000936 TPM->setLastUser(TransferLastUses, My_PM);
937 TransferLastUses.clear();
938 }
939
940 // Now, take care of required analysises that are not available.
941 for (SmallVector<AnalysisID, 8>::iterator
942 I = ReqAnalysisNotAvailable.begin(),
943 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
944 Pass *AnalysisPass = (*I)->createPass();
945 this->addLowerLevelRequiredPass(P, AnalysisPass);
946 }
947
948 // Take a note of analysis required and made available by this pass.
949 // Remove the analysis not preserved by this pass
950 removeNotPreservedAnalysis(P);
951 recordAvailableAnalysis(P);
952
Devang Patel8cad70d2006-11-11 01:51:02 +0000953 // Add pass
954 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000955}
956
Devang Patele64d3052007-04-16 20:12:57 +0000957
958/// Populate RP with analysis pass that are required by
959/// pass P and are available. Populate RP_NotAvail with analysis
960/// pass that are required by pass P but are not available.
961void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
962 SmallVector<AnalysisID, 8> &RP_NotAvail,
963 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000964 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
965 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000966 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000967 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000968 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
969 RP.push_back(AnalysisPass);
970 else
Chris Lattner60987362009-03-06 05:53:14 +0000971 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000972 }
Devang Patelf58183d2006-12-12 23:09:32 +0000973
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000974 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000975 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000976 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000977 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
978 RP.push_back(AnalysisPass);
979 else
Chris Lattner60987362009-03-06 05:53:14 +0000980 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000981 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000982}
983
Devang Patel07f4f582006-11-14 21:49:36 +0000984// All Required analyses should be available to the pass as it runs! Here
985// we fill in the AnalysisImpls member of the pass so that it can
986// successfully use the getAnalysis() method to retrieve the
987// implementations it needs.
988//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000989void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000990 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
991
Chris Lattnercbd160f2008-08-08 05:33:04 +0000992 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000993 I = AnUsage->getRequiredSet().begin(),
994 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000995 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000996 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000997 // This may be analysis pass that is initialized on the fly.
998 // If that is not the case then it will raise an assert when it is used.
999 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001000 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001001 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001002 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001003 }
1004}
1005
Devang Patel640c5bb2006-12-08 22:30:11 +00001006/// Find the pass that implements Analysis AID. If desired pass is not found
1007/// then return NULL.
1008Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1009
1010 // Check if AvailableAnalysis map has one entry.
1011 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1012
1013 if (I != AvailableAnalysis.end())
1014 return I->second;
1015
1016 // Search Parents through TopLevelManager
1017 if (SearchParent)
1018 return TPM->findAnalysisPass(AID);
1019
Devang Patel9d759b82006-12-09 00:09:12 +00001020 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001021}
1022
Devang Patel991aeba2006-12-15 20:13:01 +00001023// Print list of passes that are last used by P.
1024void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1025
Devang Patel8adae862007-07-20 18:04:54 +00001026 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001027
1028 // If this is a on the fly manager then it does not have TPM.
1029 if (!TPM)
1030 return;
1031
Devang Patel991aeba2006-12-15 20:13:01 +00001032 TPM->collectLastUses(LUses, P);
1033
Devang Patel8adae862007-07-20 18:04:54 +00001034 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001035 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001036 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +00001037 (*I)->dumpPassStructure(0);
1038 }
1039}
1040
1041void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001042 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001043 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001044 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001045 PMD->dumpPassArguments();
1046 else
1047 if (const PassInfo *PI = (*I)->getPassInfo())
1048 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001049 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001050 }
1051}
1052
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001053void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1054 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001055 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001056 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001057 return;
David Greene994e1bb2010-01-05 01:30:02 +00001058 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001059 switch (S1) {
1060 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001061 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001062 break;
1063 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001064 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001065 break;
1066 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001067 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001068 break;
1069 default:
1070 break;
1071 }
1072 switch (S2) {
1073 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001074 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001075 break;
1076 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001077 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001078 break;
1079 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001080 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001081 break;
1082 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001083 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001084 break;
1085 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001086 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001087 break;
1088 default:
1089 break;
1090 }
Devang Patel991aeba2006-12-15 20:13:01 +00001091}
1092
Chris Lattner4c1e9542009-03-06 06:45:05 +00001093void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001094 if (PassDebugging < Details)
1095 return;
1096
1097 AnalysisUsage analysisUsage;
1098 P->getAnalysisUsage(analysisUsage);
1099 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1100}
1101
Chris Lattner4c1e9542009-03-06 06:45:05 +00001102void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001103 if (PassDebugging < Details)
1104 return;
1105
1106 AnalysisUsage analysisUsage;
1107 P->getAnalysisUsage(analysisUsage);
1108 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1109}
1110
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001111void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001112 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001113 assert(PassDebugging >= Details);
1114 if (Set.empty())
1115 return;
David Greene994e1bb2010-01-05 01:30:02 +00001116 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001117 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001118 if (i) dbgs() << ',';
1119 dbgs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001120 }
David Greene994e1bb2010-01-05 01:30:02 +00001121 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001122}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001123
Devang Patel004937b2007-07-27 20:06:09 +00001124/// Add RequiredPass into list of lower level passes required by pass P.
1125/// RequiredPass is run on the fly by Pass Manager when P requests it
1126/// through getAnalysis interface.
1127/// This should be handled by specific pass manager.
1128void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1129 if (TPM) {
1130 TPM->dumpArguments();
1131 TPM->dumpPasses();
1132 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001133
1134 // Module Level pass may required Function Level analysis info
1135 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1136 // to provide this on demand. In that case, in Pass manager terminology,
1137 // module level pass is requiring lower level analysis info managed by
1138 // lower level pass manager.
1139
1140 // When Pass manager is not able to order required analysis info, Pass manager
1141 // checks whether any lower level manager will be able to provide this
1142 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001143#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001144 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1145 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001146#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001147 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001148}
1149
Devang Patele7599552007-01-12 18:52:44 +00001150// Destructor
1151PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001152 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001153 E = PassVector.end(); I != E; ++I)
1154 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001155}
1156
Devang Patel9bdf7d42006-12-08 23:28:54 +00001157//===----------------------------------------------------------------------===//
1158// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001159// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1160Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001161 return PM.findAnalysisPass(ID, dir);
1162}
1163
Devang Patel92942812007-04-16 20:56:24 +00001164Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1165 Function &F) {
1166 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1167}
1168
Devang Patela1514cb2006-12-07 19:39:39 +00001169//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001170// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001171
Devang Patel6e5a1132006-11-07 21:31:57 +00001172/// Execute all of the passes scheduled for execution by invoking
1173/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1174/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001175bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001176 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001177 return false;
1178
Devang Patele9585592006-12-08 01:38:28 +00001179 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001180
Devang Patel6e5a1132006-11-07 21:31:57 +00001181 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001182 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1183 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001184 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001185
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001186 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001187 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001188
Devang Patelabfbe3b2006-12-16 00:56:26 +00001189 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001190
Chris Lattner4c1e9542009-03-06 06:45:05 +00001191 {
1192 // If the pass crashes, remember this.
1193 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001194 TimeRegion PassTimer(getPassTimer(BP));
1195
Dan Gohman74b189f2010-03-01 17:34:28 +00001196 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001197 }
Devang Patel93a197c2006-12-14 00:08:04 +00001198
Dan Gohman74b189f2010-03-01 17:34:28 +00001199 Changed |= LocalChanged;
1200 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001201 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001202 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001203 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001204
Devang Patela273d1c2007-07-19 18:02:32 +00001205 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001206 removeNotPreservedAnalysis(BP);
1207 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001208 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001209 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001210
Bill Wendling6ce6d262009-12-25 13:50:18 +00001211 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001212}
1213
Devang Patel475c4532006-12-08 00:59:05 +00001214// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001215bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001216 bool Changed = false;
1217
Chris Lattner4c1e9542009-03-06 06:45:05 +00001218 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1219 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001220
1221 return Changed;
1222}
1223
Duncan Sands51495602009-02-13 09:42:34 +00001224bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001225 bool Changed = false;
1226
Chris Lattner4c1e9542009-03-06 06:45:05 +00001227 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1228 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001229
1230 return Changed;
1231}
1232
Duncan Sands51495602009-02-13 09:42:34 +00001233bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001234 bool Changed = false;
1235
Devang Patelabfbe3b2006-12-16 00:56:26 +00001236 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1237 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001238 Changed |= BP->doInitialization(F);
1239 }
1240
1241 return Changed;
1242}
1243
Duncan Sands51495602009-02-13 09:42:34 +00001244bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001245 bool Changed = false;
1246
Devang Patelabfbe3b2006-12-16 00:56:26 +00001247 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1248 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001249 Changed |= BP->doFinalization(F);
1250 }
1251
1252 return Changed;
1253}
1254
1255
Devang Patela1514cb2006-12-07 19:39:39 +00001256//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001257// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001258
Devang Patel4e12f862006-11-08 10:44:40 +00001259/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001260FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001261 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001262 // FPM is the top level manager.
1263 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001264
Dan Gohman565df952008-03-13 02:08:36 +00001265 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001266 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001267}
1268
Devang Patelb67904d2006-12-13 02:36:01 +00001269FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001270 delete FPM;
1271}
1272
Devang Patel4e12f862006-11-08 10:44:40 +00001273/// add - Add a pass to the queue of passes to run. This passes
1274/// ownership of the Pass to the PassManager. When the
1275/// PassManager_X is destroyed, the pass will be destroyed as well, so
1276/// there is no need to delete the pass. (TODO delete passes.)
1277/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001278void FunctionPassManager::add(Pass *P) {
David Greene9b063df2010-04-02 23:17:14 +00001279 if (ShouldPrintBeforePass(P))
1280 add(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1281 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001282 FPM->add(P);
David Greene9b063df2010-04-02 23:17:14 +00001283
1284 if (ShouldPrintAfterPass(P))
1285 add(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1286 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001287}
1288
Devang Patel9f3083e2006-11-15 19:39:54 +00001289/// run - Execute all of the passes scheduled for execution. Keep
1290/// track of whether any of the passes modifies the function, and if
1291/// so, return true.
1292///
Devang Patelb67904d2006-12-13 02:36:01 +00001293bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001294 if (F.isMaterializable()) {
1295 std::string errstr;
1296 if (F.Materialize(&errstr)) {
1297 llvm_report_error("Error reading bitcode file: " + errstr);
1298 }
Devang Patel9f3083e2006-11-15 19:39:54 +00001299 }
Devang Patel272908d2006-12-08 22:57:48 +00001300 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001301}
1302
1303
Devang Patelff631ae2006-11-15 01:27:05 +00001304/// doInitialization - Run all of the initializers for the function passes.
1305///
Devang Patelb67904d2006-12-13 02:36:01 +00001306bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001307 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001308}
1309
Dan Gohmane6656eb2007-07-30 14:51:13 +00001310/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001311///
Devang Patelb67904d2006-12-13 02:36:01 +00001312bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001313 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001314}
1315
Devang Patela1514cb2006-12-07 19:39:39 +00001316//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001317// FunctionPassManagerImpl implementation
1318//
Duncan Sands51495602009-02-13 09:42:34 +00001319bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001320 bool Changed = false;
1321
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001322 dumpArguments();
1323 dumpPasses();
1324
Chris Lattner4c1e9542009-03-06 06:45:05 +00001325 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1326 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001327
1328 return Changed;
1329}
1330
Duncan Sands51495602009-02-13 09:42:34 +00001331bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001332 bool Changed = false;
1333
Chris Lattner4c1e9542009-03-06 06:45:05 +00001334 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1335 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001336
1337 return Changed;
1338}
1339
Devang Patelec9c58f2009-04-01 22:34:41 +00001340/// cleanup - After running all passes, clean up pass manager cache.
1341void FPPassManager::cleanup() {
1342 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1343 FunctionPass *FP = getContainedPass(Index);
1344 AnalysisResolver *AR = FP->getResolver();
1345 assert(AR && "Analysis Resolver is not set");
1346 AR->clearAnalysisImpls();
1347 }
1348}
1349
Torok Edwin24c78352009-06-29 18:49:09 +00001350void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1351 if (!wasRun)
1352 return;
1353 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1354 FPPassManager *FPPM = getContainedManager(Index);
1355 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1356 FPPM->getContainedPass(Index)->releaseMemory();
1357 }
1358 }
Torok Edwin896556e2009-06-29 21:05:10 +00001359 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001360}
1361
Devang Patel67d6a5e2006-12-19 19:46:59 +00001362// Execute all the passes managed by this top level manager.
1363// Return true if any function is modified by a pass.
1364bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001365 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001366 TimingInfo::createTheTimeInfo();
1367
Devang Patele3068402006-12-21 00:16:50 +00001368 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001369 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1370 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001371
1372 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1373 getContainedManager(Index)->cleanup();
1374
Torok Edwin24c78352009-06-29 18:49:09 +00001375 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001376 return Changed;
1377}
1378
1379//===----------------------------------------------------------------------===//
1380// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001381
Devang Patel8c78a0b2007-05-03 01:11:54 +00001382char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001383/// Print passes managed by this manager
1384void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001385 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001386 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1387 FunctionPass *FP = getContainedPass(Index);
1388 FP->dumpPassStructure(Offset + 1);
1389 dumpLastUses(FP, Offset+1);
1390 }
1391}
1392
1393
Devang Patel0c2012f2006-11-07 21:49:50 +00001394/// Execute all of the passes scheduled for execution by invoking
1395/// runOnFunction method. Keep track of whether any of the passes modifies
1396/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001397bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001398 if (F.isDeclaration())
1399 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001400
1401 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001402
Devang Patelcbbf2912008-03-20 01:09:53 +00001403 // Collect inherited analysis from Module level pass manager.
1404 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001405
Devang Patelabfbe3b2006-12-16 00:56:26 +00001406 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1407 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001408 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001409
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001410 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001411 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001412
Devang Patelabfbe3b2006-12-16 00:56:26 +00001413 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001414
Chris Lattner4c1e9542009-03-06 06:45:05 +00001415 {
1416 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001417 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001418
Dan Gohman74b189f2010-03-01 17:34:28 +00001419 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001420 }
Devang Patel93a197c2006-12-14 00:08:04 +00001421
Dan Gohman74b189f2010-03-01 17:34:28 +00001422 Changed |= LocalChanged;
1423 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001424 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001425 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001426
Devang Patela273d1c2007-07-19 18:02:32 +00001427 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001428 removeNotPreservedAnalysis(FP);
1429 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001430 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001431 }
1432 return Changed;
1433}
1434
Devang Patel67d6a5e2006-12-19 19:46:59 +00001435bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001436 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001437
Chris Lattner60987362009-03-06 05:53:14 +00001438 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1439 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001440
Bill Wendling6ce6d262009-12-25 13:50:18 +00001441 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442}
1443
Duncan Sands51495602009-02-13 09:42:34 +00001444bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001445 bool Changed = false;
1446
Chris Lattner4c1e9542009-03-06 06:45:05 +00001447 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1448 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001449
1450 return Changed;
1451}
1452
Duncan Sands51495602009-02-13 09:42:34 +00001453bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001454 bool Changed = false;
1455
Chris Lattner4c1e9542009-03-06 06:45:05 +00001456 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1457 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001458
Devang Patelff631ae2006-11-15 01:27:05 +00001459 return Changed;
1460}
1461
Devang Patela1514cb2006-12-07 19:39:39 +00001462//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001463// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001464
Devang Patel05e1a972006-11-07 22:03:15 +00001465/// Execute all of the passes scheduled for execution by invoking
1466/// runOnModule method. Keep track of whether any of the passes modifies
1467/// the module, and if so, return true.
1468bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001469MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001470 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001471
Torok Edwin24c78352009-06-29 18:49:09 +00001472 // Initialize on-the-fly passes
1473 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1474 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1475 I != E; ++I) {
1476 FunctionPassManagerImpl *FPP = I->second;
1477 Changed |= FPP->doInitialization(M);
1478 }
1479
Devang Patelabfbe3b2006-12-16 00:56:26 +00001480 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1481 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001482 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001483
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001484 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001485 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001486
Devang Patelabfbe3b2006-12-16 00:56:26 +00001487 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001488
Chris Lattner4c1e9542009-03-06 06:45:05 +00001489 {
1490 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001491 TimeRegion PassTimer(getPassTimer(MP));
1492
Dan Gohman74b189f2010-03-01 17:34:28 +00001493 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001494 }
Devang Patel93a197c2006-12-14 00:08:04 +00001495
Dan Gohman74b189f2010-03-01 17:34:28 +00001496 Changed |= LocalChanged;
1497 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001498 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001499 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001500 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001501
Devang Patela273d1c2007-07-19 18:02:32 +00001502 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001503 removeNotPreservedAnalysis(MP);
1504 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001505 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001506 }
Torok Edwin24c78352009-06-29 18:49:09 +00001507
1508 // Finalize on-the-fly passes
1509 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1510 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1511 I != E; ++I) {
1512 FunctionPassManagerImpl *FPP = I->second;
1513 // We don't know when is the last time an on-the-fly pass is run,
1514 // so we need to releaseMemory / finalize here
1515 FPP->releaseMemoryOnTheFly();
1516 Changed |= FPP->doFinalization(M);
1517 }
Devang Patel05e1a972006-11-07 22:03:15 +00001518 return Changed;
1519}
1520
Devang Patele64d3052007-04-16 20:12:57 +00001521/// Add RequiredPass into list of lower level passes required by pass P.
1522/// RequiredPass is run on the fly by Pass Manager when P requests it
1523/// through getAnalysis interface.
1524void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001525 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1526 "Unable to handle Pass that requires lower level Analysis pass");
1527 assert((P->getPotentialPassManagerType() <
1528 RequiredPass->getPotentialPassManagerType()) &&
1529 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001530
Devang Patel68f72b12007-04-26 17:50:19 +00001531 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001532 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001533 FPP = new FunctionPassManagerImpl(0);
1534 // FPP is the top level manager.
1535 FPP->setTopLevelManager(FPP);
1536
Devang Patel69e9f6d2007-04-16 20:27:05 +00001537 OnTheFlyManagers[P] = FPP;
1538 }
Devang Patel68f72b12007-04-26 17:50:19 +00001539 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001540
Devang Patel68f72b12007-04-26 17:50:19 +00001541 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001542 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001543 LU.push_back(RequiredPass);
1544 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001545}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001546
1547/// Return function pass corresponding to PassInfo PI, that is
1548/// required by module pass MP. Instantiate analysis pass, by using
1549/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001550Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001551 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001552 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001553
Torok Edwin24c78352009-06-29 18:49:09 +00001554 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001555 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001556 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001557}
1558
1559
Devang Patela1514cb2006-12-07 19:39:39 +00001560//===----------------------------------------------------------------------===//
1561// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001562//
Devang Patelc290c8a2006-11-07 22:23:34 +00001563/// run - Execute all of the passes scheduled for execution. Keep track of
1564/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001565bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001566 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001567 TimingInfo::createTheTimeInfo();
1568
Devang Patelcfd70c42006-12-13 22:10:00 +00001569 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001570 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001571
Devang Patele3068402006-12-21 00:16:50 +00001572 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001573 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1574 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001575 return Changed;
1576}
Devang Patel376fefa2006-11-08 10:29:57 +00001577
Devang Patela1514cb2006-12-07 19:39:39 +00001578//===----------------------------------------------------------------------===//
1579// PassManager implementation
1580
Devang Patel376fefa2006-11-08 10:29:57 +00001581/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001582PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001583 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001584 // PM is the top level manager
1585 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001586}
1587
Devang Patelb67904d2006-12-13 02:36:01 +00001588PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001589 delete PM;
1590}
1591
Devang Patel376fefa2006-11-08 10:29:57 +00001592/// add - Add a pass to the queue of passes to run. This passes ownership of
1593/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1594/// will be destroyed as well, so there is no need to delete the pass. This
1595/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001596void PassManager::add(Pass *P) {
David Greene9b063df2010-04-02 23:17:14 +00001597 if (ShouldPrintBeforePass(P))
1598 add(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1599 + P->getPassName() + " ***"));
1600
Devang Patel376fefa2006-11-08 10:29:57 +00001601 PM->add(P);
David Greene9b063df2010-04-02 23:17:14 +00001602
1603 if (ShouldPrintAfterPass(P))
1604 add(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1605 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001606}
1607
1608/// run - Execute all of the passes scheduled for execution. Keep track of
1609/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001610bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001611 return PM->run(M);
1612}
1613
Devang Patelb8817b92006-12-14 00:59:42 +00001614//===----------------------------------------------------------------------===//
1615// TimingInfo Class - This class is used to calculate information about the
1616// amount of time each pass takes to execute. This only happens with
1617// -time-passes is enabled on the command line.
1618//
1619bool llvm::TimePassesIsEnabled = false;
1620static cl::opt<bool,true>
1621EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1622 cl::desc("Time each pass, printing elapsed time for each on exit"));
1623
1624// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1625// a non null value (if the -time-passes option is enabled) or it leaves it
1626// null. It may be called multiple times.
1627void TimingInfo::createTheTimeInfo() {
1628 if (!TimePassesIsEnabled || TheTimeInfo) return;
1629
1630 // Constructed the first time this is called, iff -time-passes is enabled.
1631 // This guarantees that the object will be constructed before static globals,
1632 // thus it will be destroyed before them.
1633 static ManagedStatic<TimingInfo> TTI;
1634 TheTimeInfo = &*TTI;
1635}
1636
Devang Patel1c3633e2007-01-29 23:10:37 +00001637/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001638Timer *llvm::getPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001639 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001640 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001641 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001642}
1643
Devang Patel1c56a632007-01-08 19:29:38 +00001644//===----------------------------------------------------------------------===//
1645// PMStack implementation
1646//
Devang Patelad98d232007-01-11 22:15:30 +00001647
Devang Patel1c56a632007-01-08 19:29:38 +00001648// Pop Pass Manager from the stack and clear its analysis info.
1649void PMStack::pop() {
1650
1651 PMDataManager *Top = this->top();
1652 Top->initializeAnalysisInfo();
1653
1654 S.pop_back();
1655}
1656
1657// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001658void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001659 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001660
Chris Lattner60987362009-03-06 05:53:14 +00001661 if (!this->empty()) {
1662 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001663
Chris Lattner60987362009-03-06 05:53:14 +00001664 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001665 TPM->addIndirectPassManager(PM);
1666 PM->setTopLevelManager(TPM);
1667 }
1668
Devang Patel15701b52007-01-11 00:19:00 +00001669 S.push_back(PM);
1670}
1671
1672// Dump content of the pass manager stack.
1673void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001674 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1675 E = S.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +00001676 printf("%s ", (*I)->getAsPass()->getPassName());
Chris Lattner60987362009-03-06 05:53:14 +00001677
Devang Patel15701b52007-01-11 00:19:00 +00001678 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001679 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001680}
1681
Devang Patel1c56a632007-01-08 19:29:38 +00001682/// Find appropriate Module Pass Manager in the PM Stack and
1683/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001684void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001685 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001686 // Find Module Pass Manager
1687 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001688 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1689 if (TopPMType == PreferredType)
1690 break; // We found desired pass manager
1691 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001692 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001693 else
1694 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001695 }
Devang Patel18ff6362008-09-09 21:38:40 +00001696 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001697 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001698}
1699
Devang Patel3312f752007-01-16 21:43:18 +00001700/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1701/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001702void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001703 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001704
Devang Patela3286902008-09-09 17:56:50 +00001705 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001706 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001707 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1708 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001709 else
Devang Patel3312f752007-01-16 21:43:18 +00001710 break;
1711 }
Devang Patel3312f752007-01-16 21:43:18 +00001712
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001713 // Create new Function Pass Manager if needed.
1714 FPPassManager *FPP;
1715 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1716 FPP = (FPPassManager *)PMS.top();
1717 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001718 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1719 PMDataManager *PMD = PMS.top();
1720
1721 // [1] Create new Function Pass Manager
1722 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001723 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001724
1725 // [2] Set up new manager's top level manager
1726 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1727 TPM->addIndirectPassManager(FPP);
1728
1729 // [3] Assign manager to manage this new manager. This may create
1730 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001731 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001732
1733 // [4] Push new manager into PMS
1734 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001735 }
1736
Devang Patel3312f752007-01-16 21:43:18 +00001737 // Assign FPP as the manager of this pass.
1738 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001739}
1740
Devang Patel3312f752007-01-16 21:43:18 +00001741/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001742/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001743void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001744 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001745 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001746
Devang Patel15701b52007-01-11 00:19:00 +00001747 // Basic Pass Manager is a leaf pass manager. It does not handle
1748 // any other pass manager.
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001749 if (!PMS.empty() &&
1750 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1751 BBP = (BBPassManager *)PMS.top();
1752 } else {
1753 // If leaf manager is not Basic Block Pass manager then create new
1754 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001755 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1756 PMDataManager *PMD = PMS.top();
1757
1758 // [1] Create new Basic Block Manager
1759 BBP = new BBPassManager(PMD->getDepth() + 1);
1760
1761 // [2] Set up new manager's top level manager
1762 // Basic Block Pass Manager does not live by itself
1763 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1764 TPM->addIndirectPassManager(BBP);
1765
Devang Patel15701b52007-01-11 00:19:00 +00001766 // [3] Assign manager to manage this new manager. This may create
1767 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001768 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001769
Devang Patel3312f752007-01-16 21:43:18 +00001770 // [4] Push new manager into PMS
1771 PMS.push(BBP);
1772 }
Devang Patel1c56a632007-01-08 19:29:38 +00001773
Devang Patel3312f752007-01-16 21:43:18 +00001774 // Assign BBP as the manager of this pass.
1775 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001776}
1777
Dan Gohmand3a20c92008-03-11 16:41:42 +00001778PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001779
1780/*===-- C Bindings --------------------------------------------------------===*/
1781
1782LLVMPassManagerRef LLVMCreatePassManager() {
1783 return wrap(new PassManager());
1784}
1785
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +00001786LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
1787 return wrap(new FunctionPassManager(unwrap(M)));
1788}
1789
Gordon Henriksen878114b2008-03-16 04:20:44 +00001790LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +00001791 return LLVMCreateFunctionPassManagerForModule(
1792 reinterpret_cast<LLVMModuleRef>(P));
Gordon Henriksen878114b2008-03-16 04:20:44 +00001793}
1794
Chris Lattner25963c62010-01-09 22:27:07 +00001795LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001796 return unwrap<PassManager>(PM)->run(*unwrap(M));
1797}
1798
Chris Lattner25963c62010-01-09 22:27:07 +00001799LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001800 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1801}
1802
Chris Lattner25963c62010-01-09 22:27:07 +00001803LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001804 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1805}
1806
Chris Lattner25963c62010-01-09 22:27:07 +00001807LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001808 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1809}
1810
1811void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1812 delete unwrap(PM);
1813}