blob: cd170dff1814ddb1046be6a3e4f2d9883565326f [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmande6188a2010-08-12 23:50:08 +000010// This file implements the LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Dan Gohmana19631f2010-08-07 01:18:18 +000016#include "llvm/PassManager.h"
David Greene9b063df2010-04-02 23:17:14 +000017#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000018#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000019#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000020#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000021#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000022#include "llvm/Module.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000024#include "llvm/Support/ManagedStatic.h"
David Greene9b063df2010-04-02 23:17:14 +000025#include "llvm/Support/PassNameParser.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Mutex.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000029#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000030#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000031using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000032
Devang Patele7599552007-01-12 18:52:44 +000033// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000034
Devang Patelf1567a52006-12-13 20:03:48 +000035namespace llvm {
36
37//===----------------------------------------------------------------------===//
38// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
43
Devang Patel03fb5872006-12-13 21:13:31 +000044// Different debug levels that can be enabled...
45enum PassDebugLevel {
46 None, Arguments, Structure, Executions, Details
47};
48
Devang Patelf1567a52006-12-13 20:03:48 +000049static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000050PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000051 cl::desc("Print PassManager debugging information"),
52 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000053 clEnumVal(None , "disable debug output"),
54 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
55 clEnumVal(Structure , "print pass structure before run()"),
56 clEnumVal(Executions, "print pass name before it is executed"),
57 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000058 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000059
60typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
61PassOptionList;
62
63// Print IR out before/after specified passes.
64static PassOptionList
65PrintBefore("print-before",
Eric Christopher787ba0b2011-03-09 19:46:51 +000066 llvm::cl::desc("Print IR before specified passes"),
67 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000068
69static PassOptionList
70PrintAfter("print-after",
Eric Christopher787ba0b2011-03-09 19:46:51 +000071 llvm::cl::desc("Print IR after specified passes"),
72 cl::Hidden);
David Greene9b063df2010-04-02 23:17:14 +000073
74static cl::opt<bool>
75PrintBeforeAll("print-before-all",
76 llvm::cl::desc("Print IR before each pass"),
77 cl::init(false));
78static cl::opt<bool>
79PrintAfterAll("print-after-all",
80 llvm::cl::desc("Print IR after each pass"),
81 cl::init(false));
82
83/// This is a helper to determine whether to print IR before or
84/// after a pass.
85
Owen Andersona7aed182010-08-06 18:33:48 +000086static bool ShouldPrintBeforeOrAfterPass(const void *PassID,
David Greene9b063df2010-04-02 23:17:14 +000087 PassOptionList &PassesToPrint) {
Owen Andersona7aed182010-08-06 18:33:48 +000088 if (const llvm::PassInfo *PI =
89 PassRegistry::getPassRegistry()->getPassInfo(PassID)) {
90 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
91 const llvm::PassInfo *PassInf = PassesToPrint[i];
92 if (PassInf)
93 if (PassInf->getPassArgument() == PI->getPassArgument()) {
94 return true;
95 }
96 }
David Greene9b063df2010-04-02 23:17:14 +000097 }
98 return false;
99}
Dan Gohmande6188a2010-08-12 23:50:08 +0000100
David Greene9b063df2010-04-02 23:17:14 +0000101
102/// This is a utility to check whether a pass should have IR dumped
103/// before it.
Owen Andersona7aed182010-08-06 18:33:48 +0000104static bool ShouldPrintBeforePass(const void *PassID) {
105 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
David Greene9b063df2010-04-02 23:17:14 +0000106}
107
108/// This is a utility to check whether a pass should have IR dumped
109/// after it.
Owen Andersona7aed182010-08-06 18:33:48 +0000110static bool ShouldPrintAfterPass(const void *PassID) {
111 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000112}
113
Devang Patelf1567a52006-12-13 20:03:48 +0000114} // End of llvm namespace
115
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000116/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
117/// or higher is specified.
118bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
119 return PassDebugging >= Executions;
120}
121
122
123
124
Chris Lattner4c1e9542009-03-06 06:45:05 +0000125void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
126 if (V == 0 && M == 0)
127 OS << "Releasing pass '";
128 else
129 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000130
Chris Lattner4c1e9542009-03-06 06:45:05 +0000131 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000132
Chris Lattner4c1e9542009-03-06 06:45:05 +0000133 if (M) {
134 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
135 return;
136 }
137 if (V == 0) {
138 OS << '\n';
139 return;
140 }
141
Dan Gohman79fc0e92009-03-10 18:47:59 +0000142 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000143 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000144 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000145 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000146 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000147 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000148 OS << "value";
149
150 OS << " '";
151 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
152 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000153}
154
155
Devang Patelffca9102006-12-15 19:39:30 +0000156namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000157
Devang Patelf33f3eb2006-12-07 19:21:29 +0000158//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000159// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000160//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000162/// pass together and sequence them to process one basic block before
163/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000164class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000165
166public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000167 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000168 explicit BBPassManager(int Depth)
Owen Andersona7aed182010-08-06 18:33:48 +0000169 : PMDataManager(Depth), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000170
Devang Patelca58e352006-11-08 10:05:38 +0000171 /// Execute all of the passes scheduled for execution. Keep track of
172 /// whether any of the passes modifies the function, and if so, return true.
173 bool runOnFunction(Function &F);
174
Devang Patelf9d96b92006-12-07 19:57:52 +0000175 /// Pass Manager itself does not invalidate any analysis info.
176 void getAnalysisUsage(AnalysisUsage &Info) const {
177 Info.setPreservesAll();
178 }
179
Devang Patel475c4532006-12-08 00:59:05 +0000180 bool doInitialization(Module &M);
181 bool doInitialization(Function &F);
182 bool doFinalization(Module &M);
183 bool doFinalization(Function &F);
184
Chris Lattner2fa26e52010-01-22 05:24:46 +0000185 virtual PMDataManager *getAsPMDataManager() { return this; }
186 virtual Pass *getAsPass() { return this; }
187
Devang Patele3858e62007-02-01 22:08:25 +0000188 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000189 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000190 }
191
Devang Pateleda56172006-12-12 23:34:33 +0000192 // Print passes managed by this manager
193 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000194 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000195 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
196 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000197 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000198 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000199 }
200 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000201
202 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000203 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000204 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
205 return BP;
206 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000207
Dan Gohmande6188a2010-08-12 23:50:08 +0000208 virtual PassManagerType getPassManagerType() const {
209 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000210 }
Devang Patelca58e352006-11-08 10:05:38 +0000211};
212
Devang Patel8c78a0b2007-05-03 01:11:54 +0000213char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000214}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000215
Devang Patele7599552007-01-12 18:52:44 +0000216namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000217
Devang Patel10c2ca62006-12-12 22:47:13 +0000218//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000220//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221/// FunctionPassManagerImpl manages FPPassManagers
222class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000223 public PMDataManager,
224 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000225private:
226 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000228 static char ID;
Dan Gohmande6188a2010-08-12 23:50:08 +0000229 explicit FunctionPassManagerImpl(int Depth) :
230 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000231 PMTopLevelManager(new FPPassManager(1)), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000232
233 /// add - Add a pass to the queue of passes to run. This passes ownership of
234 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
235 /// will be destroyed as well, so there is no need to delete the pass. This
236 /// implies that all passes MUST be allocated with 'new'.
237 void add(Pass *P) {
238 schedulePass(P);
239 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000240
241 /// createPrinterPass - Get a function printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000242 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
243 return createPrintFunctionPass(Banner, &O);
244 }
245
Torok Edwin24c78352009-06-29 18:49:09 +0000246 // Prepare for running an on the fly pass, freeing memory if needed
247 // from a previous run.
248 void releaseMemoryOnTheFly();
249
Devang Patel67d6a5e2006-12-19 19:46:59 +0000250 /// run - Execute all of the passes scheduled for execution. Keep track of
251 /// whether any of the passes modifies the module, and if so, return true.
252 bool run(Function &F);
253
254 /// doInitialization - Run all of the initializers for the function passes.
255 ///
256 bool doInitialization(Module &M);
Dan Gohmande6188a2010-08-12 23:50:08 +0000257
Dan Gohmane6656eb2007-07-30 14:51:13 +0000258 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000259 ///
260 bool doFinalization(Module &M);
261
Dan Gohmande6188a2010-08-12 23:50:08 +0000262
Chris Lattner2fa26e52010-01-22 05:24:46 +0000263 virtual PMDataManager *getAsPMDataManager() { return this; }
264 virtual Pass *getAsPass() { return this; }
265
Devang Patel67d6a5e2006-12-19 19:46:59 +0000266 /// Pass Manager itself does not invalidate any analysis info.
267 void getAnalysisUsage(AnalysisUsage &Info) const {
268 Info.setPreservesAll();
269 }
270
Dan Gohman30614852010-08-16 21:57:30 +0000271 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000272 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000273 // P is a immutable pass and it will be managed by this
274 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000275 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000276 P->setResolver(AR);
277 initializeAnalysisImpl(P);
278 addImmutablePass(IP);
279 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000280 } else {
David Greene103d4b42010-05-10 20:24:27 +0000281 P->assignPassManager(activeStack, PMT_FunctionPassManager);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000282 }
Devang Patel0f080042007-01-12 17:23:48 +0000283
Devang Patel67d6a5e2006-12-19 19:46:59 +0000284 }
285
286 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000287 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000288 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
289 return FP;
290 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000291};
292
Devang Patel8c78a0b2007-05-03 01:11:54 +0000293char FunctionPassManagerImpl::ID = 0;
Dan Gohmande6188a2010-08-12 23:50:08 +0000294
Devang Patel67d6a5e2006-12-19 19:46:59 +0000295//===----------------------------------------------------------------------===//
296// MPPassManager
297//
298/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000299/// It batches all Module passes and function pass managers together and
300/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000301class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000302public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000303 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000304 explicit MPPassManager(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000305 Pass(PT_PassManager, ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000306
307 // Delete on the fly managers.
308 virtual ~MPPassManager() {
Dan Gohmande6188a2010-08-12 23:50:08 +0000309 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000310 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
311 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000312 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000313 delete FPP;
314 }
315 }
316
Dan Gohmande6188a2010-08-12 23:50:08 +0000317 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000318 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
319 return createPrintModulePass(&O, false, Banner);
320 }
321
Devang Patelca58e352006-11-08 10:05:38 +0000322 /// run - Execute all of the passes scheduled for execution. Keep track of
323 /// whether any of the passes modifies the module, and if so, return true.
324 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000325
Devang Patelf9d96b92006-12-07 19:57:52 +0000326 /// Pass Manager itself does not invalidate any analysis info.
327 void getAnalysisUsage(AnalysisUsage &Info) const {
328 Info.setPreservesAll();
329 }
330
Devang Patele64d3052007-04-16 20:12:57 +0000331 /// Add RequiredPass into list of lower level passes required by pass P.
332 /// RequiredPass is run on the fly by Pass Manager when P requests it
333 /// through getAnalysis interface.
334 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
335
Dan Gohmande6188a2010-08-12 23:50:08 +0000336 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000337 /// required by module pass MP. Instantiate analysis pass, by using
338 /// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +0000339 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel69e9f6d2007-04-16 20:27:05 +0000340
Devang Patele3858e62007-02-01 22:08:25 +0000341 virtual const char *getPassName() const {
342 return "Module Pass Manager";
343 }
344
Chris Lattner2fa26e52010-01-22 05:24:46 +0000345 virtual PMDataManager *getAsPMDataManager() { return this; }
346 virtual Pass *getAsPass() { return this; }
347
Devang Pateleda56172006-12-12 23:34:33 +0000348 // Print passes managed by this manager
349 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000350 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
352 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000353 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000354 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
355 OnTheFlyManagers.find(MP);
356 if (I != OnTheFlyManagers.end())
357 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000358 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000359 }
360 }
361
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000363 assert(N < PassVector.size() && "Pass number out of range!");
364 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000365 }
366
Dan Gohmande6188a2010-08-12 23:50:08 +0000367 virtual PassManagerType getPassManagerType() const {
368 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000369 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000370
371 private:
372 /// Collection of on the fly FPPassManagers. These managers manage
373 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000374 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000375};
376
Devang Patel8c78a0b2007-05-03 01:11:54 +0000377char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000378//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000379// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000380//
Devang Patel09f162c2007-05-01 21:15:47 +0000381
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382/// PassManagerImpl manages MPPassManagers
383class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000384 public PMDataManager,
385 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000386
387public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000388 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000389 explicit PassManagerImpl(int Depth) :
Owen Andersona7aed182010-08-06 18:33:48 +0000390 Pass(PT_PassManager, ID), PMDataManager(Depth),
Dan Gohmane85c6192010-08-16 21:38:42 +0000391 PMTopLevelManager(new MPPassManager(1)) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000392
Devang Patel376fefa2006-11-08 10:29:57 +0000393 /// add - Add a pass to the queue of passes to run. This passes ownership of
394 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
395 /// will be destroyed as well, so there is no need to delete the pass. This
396 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000397 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000398 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000399 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000400
401 /// createPrinterPass - Get a module printer pass.
David Greene9b063df2010-04-02 23:17:14 +0000402 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
403 return createPrintModulePass(&O, false, Banner);
404 }
405
Devang Patel376fefa2006-11-08 10:29:57 +0000406 /// run - Execute all of the passes scheduled for execution. Keep track of
407 /// whether any of the passes modifies the module, and if so, return true.
408 bool run(Module &M);
409
Devang Patelf9d96b92006-12-07 19:57:52 +0000410 /// Pass Manager itself does not invalidate any analysis info.
411 void getAnalysisUsage(AnalysisUsage &Info) const {
412 Info.setPreservesAll();
413 }
414
Dan Gohman30614852010-08-16 21:57:30 +0000415 void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000416 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000417 // P is a immutable pass and it will be managed by this
418 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000419 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000420 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000421 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000422 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000423 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000424 } else {
David Greene103d4b42010-05-10 20:24:27 +0000425 P->assignPassManager(activeStack, PMT_ModulePassManager);
Devang Pateld440cd92006-12-08 23:53:00 +0000426 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000427 }
428
Chris Lattner2fa26e52010-01-22 05:24:46 +0000429 virtual PMDataManager *getAsPMDataManager() { return this; }
430 virtual Pass *getAsPass() { return this; }
431
Devang Patel67d6a5e2006-12-19 19:46:59 +0000432 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000433 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000434 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
435 return MP;
436 }
Devang Patel376fefa2006-11-08 10:29:57 +0000437};
438
Devang Patel8c78a0b2007-05-03 01:11:54 +0000439char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000440} // End of llvm namespace
441
442namespace {
443
444//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000445/// TimingInfo Class - This class is used to calculate information about the
446/// amount of time each pass takes to execute. This only happens when
447/// -time-passes is enabled on the command line.
448///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000449
Owen Anderson5a6960f2009-06-18 20:51:00 +0000450static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000451
Nick Lewycky02d5f772009-10-25 06:33:48 +0000452class TimingInfo {
Chris Lattner707431c2010-03-30 04:03:22 +0000453 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000454 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000455public:
456 // Use 'create' member to get this.
457 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000458
Devang Patel1c3633e2007-01-29 23:10:37 +0000459 // TimingDtor - Print out information about timing information
460 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000461 // Delete all of the timers, which accumulate their info into the
462 // TimerGroup.
463 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
464 E = TimingData.end(); I != E; ++I)
465 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000466 // TimerGroup is deleted next, printing the report.
467 }
468
469 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
470 // to a non null value (if the -time-passes option is enabled) or it leaves it
471 // null. It may be called multiple times.
472 static void createTheTimeInfo();
473
Chris Lattner707431c2010-03-30 04:03:22 +0000474 /// getPassTimer - Return the timer for the specified pass if it exists.
475 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000476 if (P->getAsPMDataManager())
Dan Gohman277e7672009-09-28 00:07:05 +0000477 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000478
Owen Anderson5c96ef72009-07-07 18:33:04 +0000479 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Chris Lattner707431c2010-03-30 04:03:22 +0000480 Timer *&T = TimingData[P];
481 if (T == 0)
482 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000483 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000484 }
485};
486
Devang Patel1c3633e2007-01-29 23:10:37 +0000487} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000488
Dan Gohmand78c4002008-05-13 00:00:25 +0000489static TimingInfo *TheTimeInfo;
490
Devang Patela1514cb2006-12-07 19:39:39 +0000491//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000492// PMTopLevelManager implementation
493
Devang Patel4268fc02007-01-16 02:00:38 +0000494/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000495PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
496 PMDM->setTopLevelManager(this);
497 addPassManager(PMDM);
498 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000499}
500
Devang Patelafb1f3622006-12-12 22:35:25 +0000501/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000502void
503PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses,
504 Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000505 unsigned PDepth = 0;
506 if (P->getResolver())
507 PDepth = P->getResolver()->getPMDataManager().getDepth();
508
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000509 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000510 E = AnalysisPasses.end(); I != E; ++I) {
511 Pass *AP = *I;
512 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000513
Devang Patel01919d22007-03-08 19:05:01 +0000514 if (P == AP)
515 continue;
516
Tobias Grosserf07426b2011-01-20 21:03:22 +0000517 // Update the last users of passes that are required transitive by AP.
518 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
519 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
520 SmallVector<Pass *, 12> LastUses;
521 SmallVector<Pass *, 12> LastPMUses;
522 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
523 E = IDs.end(); I != E; ++I) {
524 Pass *AnalysisPass = findAnalysisPass(*I);
525 assert(AnalysisPass && "Expected analysis pass to exist.");
526 AnalysisResolver *AR = AnalysisPass->getResolver();
527 assert(AR && "Expected analysis resolver to exist.");
528 unsigned APDepth = AR->getPMDataManager().getDepth();
529
530 if (PDepth == APDepth)
531 LastUses.push_back(AnalysisPass);
532 else if (PDepth > APDepth)
533 LastPMUses.push_back(AnalysisPass);
534 }
535
536 setLastUser(LastUses, P);
537
538 // If this pass has a corresponding pass manager, push higher level
539 // analysis to this pass manager.
540 if (P->getResolver())
541 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
542
543
Devang Patelafb1f3622006-12-12 22:35:25 +0000544 // If AP is the last user of other passes then make P last user of
545 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000546 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000547 LUE = LastUser.end(); LUI != LUE; ++LUI) {
548 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000549 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000550 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000551 LastUser[LUI->first] = P;
552 }
553 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000554}
555
556/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000557void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000558 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000559 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000560 InversedLastUser.find(P);
561 if (DMI == InversedLastUser.end())
562 return;
563
564 SmallPtrSet<Pass *, 8> &LU = DMI->second;
565 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
566 E = LU.end(); I != E; ++I) {
567 LastUses.push_back(*I);
568 }
569
Devang Patelafb1f3622006-12-12 22:35:25 +0000570}
571
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000572AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
573 AnalysisUsage *AnUsage = NULL;
574 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000575 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000576 AnUsage = DMI->second;
577 else {
578 AnUsage = new AnalysisUsage();
579 P->getAnalysisUsage(*AnUsage);
580 AnUsageMap[P] = AnUsage;
581 }
582 return AnUsage;
583}
584
Devang Patelafb1f3622006-12-12 22:35:25 +0000585/// Schedule pass P for execution. Make sure that passes required by
586/// P are run before P is run. Update analysis info maintained by
587/// the manager. Remove dead passes. This is a recursive function.
588void PMTopLevelManager::schedulePass(Pass *P) {
589
Devang Patel3312f752007-01-16 21:43:18 +0000590 // TODO : Allocate function manager for this pass, other wise required set
591 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000592
Devang Pateld74ede72007-03-06 01:06:16 +0000593 // Give pass a chance to prepare the stage.
594 P->preparePassManager(activeStack);
595
Devang Patel864970e2008-03-18 00:39:19 +0000596 // If P is an analysis pass and it is available then do not
597 // generate the analysis again. Stale analysis info should not be
598 // available at this point.
Owen Andersona7aed182010-08-06 18:33:48 +0000599 const PassInfo *PI =
600 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
601 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000602 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000603 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000604 }
Devang Patel864970e2008-03-18 00:39:19 +0000605
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000606 AnalysisUsage *AnUsage = findAnalysisUsage(P);
607
Devang Patelfdee7032008-08-14 23:07:48 +0000608 bool checkAnalysis = true;
609 while (checkAnalysis) {
610 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000611
Devang Patelfdee7032008-08-14 23:07:48 +0000612 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
613 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
614 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000615
Devang Patelfdee7032008-08-14 23:07:48 +0000616 Pass *AnalysisPass = findAnalysisPass(*I);
617 if (!AnalysisPass) {
Owen Andersona7aed182010-08-06 18:33:48 +0000618 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
619 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000620 if (P->getPotentialPassManagerType () ==
621 AnalysisPass->getPotentialPassManagerType())
622 // Schedule analysis pass that is managed by the same pass manager.
623 schedulePass(AnalysisPass);
624 else if (P->getPotentialPassManagerType () >
625 AnalysisPass->getPotentialPassManagerType()) {
626 // Schedule analysis pass that is managed by a new manager.
627 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000628 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000629 // are already checked are still available.
630 checkAnalysis = true;
631 }
632 else
Dan Gohmande6188a2010-08-12 23:50:08 +0000633 // Do not schedule this analysis. Lower level analsyis
Devang Patelfdee7032008-08-14 23:07:48 +0000634 // passes are run on the fly.
635 delete AnalysisPass;
636 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000637 }
638 }
639
640 // Now all required passes are available.
641 addTopLevelPass(P);
642}
643
644/// Find the pass that implements Analysis AID. Search immutable
645/// passes and all pass managers. If desired pass is not found
646/// then return NULL.
647Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
648
Devang Patelcd6ba152006-12-12 22:50:05 +0000649 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000650 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000651 E = PassManagers.end(); I != E; ++I)
652 if (Pass *P = (*I)->findAnalysisPass(AID, false))
653 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000654
655 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000656 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000657 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000658 E = IndirectPassManagers.end(); I != E; ++I)
659 if (Pass *P = (*I)->findAnalysisPass(AID, false))
660 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000661
Dan Gohman844dd0a2010-10-11 23:19:01 +0000662 // Check the immutable passes. Iterate in reverse order so that we find
663 // the most recently registered passes first.
664 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
665 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000666 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000667 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000668 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000669
670 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman844dd0a2010-10-11 23:19:01 +0000671 const PassInfo *PassInf =
672 PassRegistry::getPassRegistry()->getPassInfo(PI);
673 const std::vector<const PassInfo*> &ImmPI =
674 PassInf->getInterfacesImplemented();
675 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
676 EE = ImmPI.end(); II != EE; ++II) {
677 if ((*II)->getTypeInfo() == AID)
678 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000679 }
680 }
681
Dan Gohman844dd0a2010-10-11 23:19:01 +0000682 return 0;
Devang Patelafb1f3622006-12-12 22:35:25 +0000683}
684
Devang Pateleda56172006-12-12 23:34:33 +0000685// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000686void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000687
Devang Patelfd4184322007-01-17 20:33:36 +0000688 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000689 return;
690
Devang Pateleda56172006-12-12 23:34:33 +0000691 // Print out the immutable passes
692 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000693 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000694 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000695
Dan Gohmanf71c5212010-08-19 01:29:07 +0000696 // Every class that derives from PMDataManager also derives from Pass
697 // (sometimes indirectly), but there's no inheritance relationship
698 // between PMDataManager and Pass, so we have to getAsPass to get
699 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000700 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000701 E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000702 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000703}
704
Devang Patel991aeba2006-12-15 20:13:01 +0000705void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000706
Devang Patelfd4184322007-01-17 20:33:36 +0000707 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000708 return;
709
David Greene994e1bb2010-01-05 01:30:02 +0000710 dbgs() << "Pass Arguments: ";
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000711 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
712 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
713 if (const PassInfo *PI =
714 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
715 if (!PI->isAnalysisGroup())
716 dbgs() << " -" << PI->getPassArgument();
Devang Patel0d29ae02008-08-12 15:44:31 +0000717 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000718 E = PassManagers.end(); I != E; ++I)
719 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000720 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000721}
722
Devang Patele3068402006-12-21 00:16:50 +0000723void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000724 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000725 E = PassManagers.end(); I != E; ++I)
726 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000727
Devang Patele3068402006-12-21 00:16:50 +0000728 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000729 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000730 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
731 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000732 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000733
Chris Lattner60987362009-03-06 05:53:14 +0000734 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000735 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000736 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000737 InversedLastUser.find(DMI->second);
738 if (InvDMI != InversedLastUser.end()) {
739 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
740 L.insert(DMI->first);
741 } else {
742 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
743 InversedLastUser[DMI->second] = L;
744 }
745 }
Devang Patele3068402006-12-21 00:16:50 +0000746}
747
Devang Patele7599552007-01-12 18:52:44 +0000748/// Destructor
749PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000750 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000751 E = PassManagers.end(); I != E; ++I)
752 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000753
Dan Gohman060d5ba2010-10-12 00:15:27 +0000754 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000755 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
756 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000757
758 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000759 DME = AnUsageMap.end(); DMI != DME; ++DMI)
760 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000761}
762
Devang Patelafb1f3622006-12-12 22:35:25 +0000763//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000764// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000765
Devang Patel643676c2006-11-11 01:10:19 +0000766/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000767void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000768 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000769
Chris Lattner60987362009-03-06 05:53:14 +0000770 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000771
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000772 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000773
Dan Gohmande6188a2010-08-12 23:50:08 +0000774 // This pass is the current implementation of all of the interfaces it
775 // implements as well.
Owen Andersona7aed182010-08-06 18:33:48 +0000776 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
777 if (PInf == 0) return;
778 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000779 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000780 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000781}
782
Devang Patel9d9fc902007-03-06 17:52:53 +0000783// Return true if P preserves high level analysis used by other
784// passes managed by this manager
785bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000786 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000787 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000788 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000789
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000790 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000791 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000792 E = HigherLevelAnalysis.end(); I != E; ++I) {
793 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000794 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000795 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000796 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000797 PreservedSet.end())
798 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000799 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000800
Devang Patel9d9fc902007-03-06 17:52:53 +0000801 return true;
802}
803
Chris Lattner02eb94c2008-08-07 07:34:50 +0000804/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000805void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000806 // Don't do this unless assertions are enabled.
807#ifdef NDEBUG
808 return;
809#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000810 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
811 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000812
Devang Patelef432532007-07-19 05:36:09 +0000813 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000814 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000815 E = PreservedSet.end(); I != E; ++I) {
816 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000817 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000818 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000819 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000820 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000821 }
822}
823
Devang Patel67c79a42008-07-01 19:50:56 +0000824/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000825void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000826 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
827 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000828 return;
829
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000830 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000831 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000832 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000833 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000834 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000835 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000836 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000837 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000838 if (PassDebugging >= Details) {
839 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000840 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
841 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000842 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000843 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000844 }
Devang Patel349170f2006-11-11 01:24:55 +0000845 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000846
Devang Patel42dd1e92007-03-06 01:55:46 +0000847 // Check inherited analysis also. If P is not preserving analysis
848 // provided by parent manager then remove it here.
849 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
850
851 if (!InheritedAnalysis[Index])
852 continue;
853
Dan Gohmande6188a2010-08-12 23:50:08 +0000854 for (std::map<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000855 I = InheritedAnalysis[Index]->begin(),
856 E = InheritedAnalysis[Index]->end(); I != E; ) {
857 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000858 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000859 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000860 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000861 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000862 if (PassDebugging >= Details) {
863 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000864 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
865 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000866 }
Devang Patel01919d22007-03-08 19:05:01 +0000867 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000868 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000869 }
870 }
Devang Patelf68a3492006-11-07 22:35:17 +0000871}
872
Devang Patelca189262006-11-14 03:05:08 +0000873/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000874void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000875 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000876
Devang Patel8adae862007-07-20 18:04:54 +0000877 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000878
Devang Patel2ff44922007-04-16 20:39:59 +0000879 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000880 if (!TPM)
881 return;
882
Devang Patel17ad0962006-12-08 00:37:52 +0000883 TPM->collectLastUses(DeadPasses, P);
884
Devang Patel656a9172008-06-06 17:50:36 +0000885 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000886 dbgs() << " -*- '" << P->getPassName();
887 dbgs() << "' is the last user of following pass instances.";
888 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000889 }
890
Dan Gohman060d5ba2010-10-12 00:15:27 +0000891 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000892 E = DeadPasses.end(); I != E; ++I)
893 freePass(*I, Msg, DBG_STR);
894}
Devang Patel200d3052006-12-13 23:50:44 +0000895
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000896void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000897 enum PassDebuggingString DBG_STR) {
898 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000899
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000900 {
901 // If the pass crashes releasing memory, remember this.
902 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000903 TimeRegion PassTimer(getPassTimer(P));
904
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000905 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000906 }
907
Owen Andersona7aed182010-08-06 18:33:48 +0000908 AnalysisID PI = P->getPassID();
909 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000910 // Remove the pass itself (if it is not already removed).
911 AvailableAnalysis.erase(PI);
912
913 // Remove all interfaces this pass implements, for which it is also
914 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000915 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000916 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000917 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000918 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000919 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000920 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000921 }
Devang Patel17ad0962006-12-08 00:37:52 +0000922 }
Devang Patelca189262006-11-14 03:05:08 +0000923}
924
Dan Gohmande6188a2010-08-12 23:50:08 +0000925/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000926/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000927void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000928 // This manager is going to manage pass P. Set up analysis resolver
929 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000930 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000931 P->setResolver(AR);
932
Devang Patelec2b9a72007-03-05 22:57:49 +0000933 // If a FunctionPass F is the last user of ModulePass info M
934 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000935 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000936
Chris Lattner60987362009-03-06 05:53:14 +0000937 if (!ProcessAnalysis) {
938 // Add pass
939 PassVector.push_back(P);
940 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000941 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000942
Chris Lattner60987362009-03-06 05:53:14 +0000943 // At the moment, this pass is the last user of all required passes.
944 SmallVector<Pass *, 12> LastUses;
945 SmallVector<Pass *, 8> RequiredPasses;
946 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
947
948 unsigned PDepth = this->getDepth();
949
Dan Gohmande6188a2010-08-12 23:50:08 +0000950 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000951 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +0000952 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000953 E = RequiredPasses.end(); I != E; ++I) {
954 Pass *PRequired = *I;
955 unsigned RDepth = 0;
956
957 assert(PRequired->getResolver() && "Analysis Resolver is not set");
958 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
959 RDepth = DM.getDepth();
960
961 if (PDepth == RDepth)
962 LastUses.push_back(PRequired);
963 else if (PDepth > RDepth) {
964 // Let the parent claim responsibility of last use
965 TransferLastUses.push_back(PRequired);
966 // Keep track of higher level analysis used by this manager.
967 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +0000968 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000969 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000970 }
971
972 // Set P as P's last user until someone starts using P.
973 // However, if P is a Pass Manager then it does not need
974 // to record its last user.
Chris Lattner2fa26e52010-01-22 05:24:46 +0000975 if (P->getAsPMDataManager() == 0)
Chris Lattner60987362009-03-06 05:53:14 +0000976 LastUses.push_back(P);
977 TPM->setLastUser(LastUses, P);
978
979 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +0000980 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +0000981 TPM->setLastUser(TransferLastUses, My_PM);
982 TransferLastUses.clear();
983 }
984
Dan Gohman6304db32010-08-16 22:57:28 +0000985 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +0000986 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000987 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000988 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000989 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
990 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +0000991 this->addLowerLevelRequiredPass(P, AnalysisPass);
992 }
993
994 // Take a note of analysis required and made available by this pass.
995 // Remove the analysis not preserved by this pass
996 removeNotPreservedAnalysis(P);
997 recordAvailableAnalysis(P);
998
Devang Patel8cad70d2006-11-11 01:51:02 +0000999 // Add pass
1000 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001001}
1002
Devang Patele64d3052007-04-16 20:12:57 +00001003
1004/// Populate RP with analysis pass that are required by
1005/// pass P and are available. Populate RP_NotAvail with analysis
1006/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001007void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1008 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001009 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001010 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1011 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001012 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001013 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001014 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001015 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001016 else
Chris Lattner60987362009-03-06 05:53:14 +00001017 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001018 }
Devang Patelf58183d2006-12-12 23:09:32 +00001019
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001020 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001021 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001022 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001023 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001024 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001025 else
Chris Lattner60987362009-03-06 05:53:14 +00001026 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001027 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001028}
1029
Devang Patel07f4f582006-11-14 21:49:36 +00001030// All Required analyses should be available to the pass as it runs! Here
1031// we fill in the AnalysisImpls member of the pass so that it can
1032// successfully use the getAnalysis() method to retrieve the
1033// implementations it needs.
1034//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001035void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001036 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1037
Chris Lattnercbd160f2008-08-08 05:33:04 +00001038 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001039 I = AnUsage->getRequiredSet().begin(),
1040 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001041 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +00001042 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +00001043 // This may be analysis pass that is initialized on the fly.
1044 // If that is not the case then it will raise an assert when it is used.
1045 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001046 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001047 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001048 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001049 }
1050}
1051
Devang Patel640c5bb2006-12-08 22:30:11 +00001052/// Find the pass that implements Analysis AID. If desired pass is not found
1053/// then return NULL.
1054Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1055
1056 // Check if AvailableAnalysis map has one entry.
1057 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1058
1059 if (I != AvailableAnalysis.end())
1060 return I->second;
1061
1062 // Search Parents through TopLevelManager
1063 if (SearchParent)
1064 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001065
Devang Patel9d759b82006-12-09 00:09:12 +00001066 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +00001067}
1068
Devang Patel991aeba2006-12-15 20:13:01 +00001069// Print list of passes that are last used by P.
1070void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1071
Devang Patel8adae862007-07-20 18:04:54 +00001072 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001073
1074 // If this is a on the fly manager then it does not have TPM.
1075 if (!TPM)
1076 return;
1077
Devang Patel991aeba2006-12-15 20:13:01 +00001078 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001079
Dan Gohman7224bce2010-10-12 00:11:18 +00001080 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001081 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +00001082 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001083 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001084 }
1085}
1086
1087void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001088 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001089 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001090 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001091 PMD->dumpPassArguments();
1092 else
Owen Andersona7aed182010-08-06 18:33:48 +00001093 if (const PassInfo *PI =
1094 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001095 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001096 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001097 }
1098}
1099
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001100void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1101 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001102 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001103 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001104 return;
David Greene994e1bb2010-01-05 01:30:02 +00001105 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001106 switch (S1) {
1107 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001108 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001109 break;
1110 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001111 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001112 break;
1113 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001114 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001115 break;
1116 default:
1117 break;
1118 }
1119 switch (S2) {
1120 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001121 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001122 break;
1123 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001124 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001125 break;
1126 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001127 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001128 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001129 case ON_REGION_MSG:
1130 dbgs() << "' on Region '" << Msg << "'...\n";
1131 break;
Devang Patel003a5592007-03-05 20:01:30 +00001132 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001134 break;
1135 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001136 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001137 break;
1138 default:
1139 break;
1140 }
Devang Patel991aeba2006-12-15 20:13:01 +00001141}
1142
Chris Lattner4c1e9542009-03-06 06:45:05 +00001143void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001144 if (PassDebugging < Details)
1145 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001146
Chris Lattner4c493d92008-08-08 15:14:09 +00001147 AnalysisUsage analysisUsage;
1148 P->getAnalysisUsage(analysisUsage);
1149 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1150}
1151
Chris Lattner4c1e9542009-03-06 06:45:05 +00001152void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001153 if (PassDebugging < Details)
1154 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001155
Chris Lattner4c493d92008-08-08 15:14:09 +00001156 AnalysisUsage analysisUsage;
1157 P->getAnalysisUsage(analysisUsage);
1158 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1159}
1160
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001161void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001162 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001163 assert(PassDebugging >= Details);
1164 if (Set.empty())
1165 return;
David Greene994e1bb2010-01-05 01:30:02 +00001166 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001167 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001168 if (i) dbgs() << ',';
Owen Andersona7aed182010-08-06 18:33:48 +00001169 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
1170 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001171 }
David Greene994e1bb2010-01-05 01:30:02 +00001172 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001173}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001174
Devang Patel004937b2007-07-27 20:06:09 +00001175/// Add RequiredPass into list of lower level passes required by pass P.
1176/// RequiredPass is run on the fly by Pass Manager when P requests it
1177/// through getAnalysis interface.
1178/// This should be handled by specific pass manager.
1179void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1180 if (TPM) {
1181 TPM->dumpArguments();
1182 TPM->dumpPasses();
1183 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001184
Dan Gohmande6188a2010-08-12 23:50:08 +00001185 // Module Level pass may required Function Level analysis info
1186 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1187 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001188 // module level pass is requiring lower level analysis info managed by
1189 // lower level pass manager.
1190
1191 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001192 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001193 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001194#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001195 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1196 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001197#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001198 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001199}
1200
Owen Andersona7aed182010-08-06 18:33:48 +00001201Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Dan Gohmanffdee302010-06-21 18:46:45 +00001202 assert(0 && "Unable to find on the fly pass");
1203 return NULL;
1204}
1205
Devang Patele7599552007-01-12 18:52:44 +00001206// Destructor
1207PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001208 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001209 E = PassVector.end(); I != E; ++I)
1210 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001211}
1212
Devang Patel9bdf7d42006-12-08 23:28:54 +00001213//===----------------------------------------------------------------------===//
1214// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001215// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1216Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001217 return PM.findAnalysisPass(ID, dir);
1218}
1219
Dan Gohmande6188a2010-08-12 23:50:08 +00001220Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001221 Function &F) {
1222 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1223}
1224
Devang Patela1514cb2006-12-07 19:39:39 +00001225//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001226// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001227
Dan Gohmande6188a2010-08-12 23:50:08 +00001228/// Execute all of the passes scheduled for execution by invoking
1229/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001230/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001231bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001232 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001233 return false;
1234
Devang Patele9585592006-12-08 01:38:28 +00001235 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001236
Devang Patel6e5a1132006-11-07 21:31:57 +00001237 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001238 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1239 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001240 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001241
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001242 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001243 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001244
Devang Patelabfbe3b2006-12-16 00:56:26 +00001245 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001246
Chris Lattner4c1e9542009-03-06 06:45:05 +00001247 {
1248 // If the pass crashes, remember this.
1249 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001250 TimeRegion PassTimer(getPassTimer(BP));
1251
Dan Gohman74b189f2010-03-01 17:34:28 +00001252 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001253 }
Devang Patel93a197c2006-12-14 00:08:04 +00001254
Dan Gohman74b189f2010-03-01 17:34:28 +00001255 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001256 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001257 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001258 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001259 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001260
Devang Patela273d1c2007-07-19 18:02:32 +00001261 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001262 removeNotPreservedAnalysis(BP);
1263 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001264 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001265 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001266
Bill Wendling6ce6d262009-12-25 13:50:18 +00001267 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001268}
1269
Devang Patel475c4532006-12-08 00:59:05 +00001270// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001271bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001272 bool Changed = false;
1273
Chris Lattner4c1e9542009-03-06 06:45:05 +00001274 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1275 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001276
1277 return Changed;
1278}
1279
Duncan Sands51495602009-02-13 09:42:34 +00001280bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001281 bool Changed = false;
1282
Chris Lattner4c1e9542009-03-06 06:45:05 +00001283 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1284 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001285
1286 return Changed;
1287}
1288
Duncan Sands51495602009-02-13 09:42:34 +00001289bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001290 bool Changed = false;
1291
Devang Patelabfbe3b2006-12-16 00:56:26 +00001292 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1293 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001294 Changed |= BP->doInitialization(F);
1295 }
1296
1297 return Changed;
1298}
1299
Duncan Sands51495602009-02-13 09:42:34 +00001300bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001301 bool Changed = false;
1302
Devang Patelabfbe3b2006-12-16 00:56:26 +00001303 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1304 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001305 Changed |= BP->doFinalization(F);
1306 }
1307
1308 return Changed;
1309}
1310
1311
Devang Patela1514cb2006-12-07 19:39:39 +00001312//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001313// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001314
Devang Patel4e12f862006-11-08 10:44:40 +00001315/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001316FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001317 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001318 // FPM is the top level manager.
1319 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001320
Dan Gohman565df952008-03-13 02:08:36 +00001321 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001322 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001323}
1324
Devang Patelb67904d2006-12-13 02:36:01 +00001325FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001326 delete FPM;
1327}
1328
David Greene103d4b42010-05-10 20:24:27 +00001329/// addImpl - Add a pass to the queue of passes to run, without
1330/// checking whether to add a printer pass.
1331void FunctionPassManager::addImpl(Pass *P) {
1332 FPM->add(P);
1333}
1334
Devang Patel4e12f862006-11-08 10:44:40 +00001335/// add - Add a pass to the queue of passes to run. This passes
1336/// ownership of the Pass to the PassManager. When the
1337/// PassManager_X is destroyed, the pass will be destroyed as well, so
1338/// there is no need to delete the pass. (TODO delete passes.)
1339/// This implies that all passes MUST be allocated with 'new'.
Dan Gohmande6188a2010-08-12 23:50:08 +00001340void FunctionPassManager::add(Pass *P) {
David Greene103d4b42010-05-10 20:24:27 +00001341 // If this is a not a function pass, don't add a printer for it.
Owen Andersona7aed182010-08-06 18:33:48 +00001342 const void *PassID = P->getPassID();
David Greene103d4b42010-05-10 20:24:27 +00001343 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001344 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001345 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1346 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001347
David Greene103d4b42010-05-10 20:24:27 +00001348 addImpl(P);
1349
1350 if (P->getPassKind() == PT_Function)
Owen Andersona7aed182010-08-06 18:33:48 +00001351 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001352 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1353 + P->getPassName() + " ***"));
Devang Patel4e12f862006-11-08 10:44:40 +00001354}
1355
Devang Patel9f3083e2006-11-15 19:39:54 +00001356/// run - Execute all of the passes scheduled for execution. Keep
1357/// track of whether any of the passes modifies the function, and if
1358/// so, return true.
1359///
Devang Patelb67904d2006-12-13 02:36:01 +00001360bool FunctionPassManager::run(Function &F) {
Nick Lewycky94e168f2010-02-15 21:27:56 +00001361 if (F.isMaterializable()) {
1362 std::string errstr;
Chris Lattnerb6166b32010-04-07 22:41:29 +00001363 if (F.Materialize(&errstr))
Benjamin Kramera6769262010-04-08 10:44:28 +00001364 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel9f3083e2006-11-15 19:39:54 +00001365 }
Devang Patel272908d2006-12-08 22:57:48 +00001366 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001367}
1368
1369
Devang Patelff631ae2006-11-15 01:27:05 +00001370/// doInitialization - Run all of the initializers for the function passes.
1371///
Devang Patelb67904d2006-12-13 02:36:01 +00001372bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001373 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001374}
1375
Dan Gohmane6656eb2007-07-30 14:51:13 +00001376/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001377///
Devang Patelb67904d2006-12-13 02:36:01 +00001378bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001379 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001380}
1381
Devang Patela1514cb2006-12-07 19:39:39 +00001382//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001383// FunctionPassManagerImpl implementation
1384//
Duncan Sands51495602009-02-13 09:42:34 +00001385bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001386 bool Changed = false;
1387
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001388 dumpArguments();
1389 dumpPasses();
1390
Chris Lattner4c1e9542009-03-06 06:45:05 +00001391 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1392 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001393
1394 return Changed;
1395}
1396
Duncan Sands51495602009-02-13 09:42:34 +00001397bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001398 bool Changed = false;
1399
Chris Lattner4c1e9542009-03-06 06:45:05 +00001400 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1401 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001402
1403 return Changed;
1404}
1405
Devang Patelec9c58f2009-04-01 22:34:41 +00001406/// cleanup - After running all passes, clean up pass manager cache.
1407void FPPassManager::cleanup() {
1408 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1409 FunctionPass *FP = getContainedPass(Index);
1410 AnalysisResolver *AR = FP->getResolver();
1411 assert(AR && "Analysis Resolver is not set");
1412 AR->clearAnalysisImpls();
1413 }
1414}
1415
Torok Edwin24c78352009-06-29 18:49:09 +00001416void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1417 if (!wasRun)
1418 return;
1419 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1420 FPPassManager *FPPM = getContainedManager(Index);
1421 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1422 FPPM->getContainedPass(Index)->releaseMemory();
1423 }
1424 }
Torok Edwin896556e2009-06-29 21:05:10 +00001425 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001426}
1427
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428// Execute all the passes managed by this top level manager.
1429// Return true if any function is modified by a pass.
1430bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001431 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001432 TimingInfo::createTheTimeInfo();
1433
Devang Patele3068402006-12-21 00:16:50 +00001434 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001435 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1436 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001437
1438 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1439 getContainedManager(Index)->cleanup();
1440
Torok Edwin24c78352009-06-29 18:49:09 +00001441 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442 return Changed;
1443}
1444
1445//===----------------------------------------------------------------------===//
1446// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001447
Devang Patel8c78a0b2007-05-03 01:11:54 +00001448char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001449/// Print passes managed by this manager
1450void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001451 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001452 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1453 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001454 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001455 dumpLastUses(FP, Offset+1);
1456 }
1457}
1458
1459
Dan Gohmande6188a2010-08-12 23:50:08 +00001460/// Execute all of the passes scheduled for execution by invoking
1461/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001462/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001463bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001464 if (F.isDeclaration())
1465 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001466
1467 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001468
Devang Patelcbbf2912008-03-20 01:09:53 +00001469 // Collect inherited analysis from Module level pass manager.
1470 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001471
Devang Patelabfbe3b2006-12-16 00:56:26 +00001472 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1473 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001474 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001475
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001476 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001477 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001478
Devang Patelabfbe3b2006-12-16 00:56:26 +00001479 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001480
Chris Lattner4c1e9542009-03-06 06:45:05 +00001481 {
1482 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001483 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001484
Dan Gohman74b189f2010-03-01 17:34:28 +00001485 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001486 }
Devang Patel93a197c2006-12-14 00:08:04 +00001487
Dan Gohman74b189f2010-03-01 17:34:28 +00001488 Changed |= LocalChanged;
1489 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001490 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001491 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001492
Devang Patela273d1c2007-07-19 18:02:32 +00001493 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001494 removeNotPreservedAnalysis(FP);
1495 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001496 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001497 }
1498 return Changed;
1499}
1500
Devang Patel67d6a5e2006-12-19 19:46:59 +00001501bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001502 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001503
Dan Gohmane7630be2010-05-11 20:30:00 +00001504 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner60987362009-03-06 05:53:14 +00001505 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001506
Bill Wendling6ce6d262009-12-25 13:50:18 +00001507 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001508}
1509
Duncan Sands51495602009-02-13 09:42:34 +00001510bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001511 bool Changed = false;
1512
Chris Lattner4c1e9542009-03-06 06:45:05 +00001513 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1514 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001515
1516 return Changed;
1517}
1518
Duncan Sands51495602009-02-13 09:42:34 +00001519bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001520 bool Changed = false;
1521
Chris Lattner4c1e9542009-03-06 06:45:05 +00001522 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1523 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001524
Devang Patelff631ae2006-11-15 01:27:05 +00001525 return Changed;
1526}
1527
Devang Patela1514cb2006-12-07 19:39:39 +00001528//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001529// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001530
Dan Gohmande6188a2010-08-12 23:50:08 +00001531/// Execute all of the passes scheduled for execution by invoking
1532/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001533/// the module, and if so, return true.
1534bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001536 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001537
Torok Edwin24c78352009-06-29 18:49:09 +00001538 // Initialize on-the-fly passes
1539 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1540 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1541 I != E; ++I) {
1542 FunctionPassManagerImpl *FPP = I->second;
1543 Changed |= FPP->doInitialization(M);
1544 }
1545
Devang Patelabfbe3b2006-12-16 00:56:26 +00001546 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1547 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001548 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001549
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001550 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001551 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001552
Devang Patelabfbe3b2006-12-16 00:56:26 +00001553 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001554
Chris Lattner4c1e9542009-03-06 06:45:05 +00001555 {
1556 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001557 TimeRegion PassTimer(getPassTimer(MP));
1558
Dan Gohman74b189f2010-03-01 17:34:28 +00001559 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001560 }
Devang Patel93a197c2006-12-14 00:08:04 +00001561
Dan Gohman74b189f2010-03-01 17:34:28 +00001562 Changed |= LocalChanged;
1563 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001564 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001565 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001566 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001567
Devang Patela273d1c2007-07-19 18:02:32 +00001568 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001569 removeNotPreservedAnalysis(MP);
1570 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001571 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001572 }
Torok Edwin24c78352009-06-29 18:49:09 +00001573
1574 // Finalize on-the-fly passes
1575 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1576 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1577 I != E; ++I) {
1578 FunctionPassManagerImpl *FPP = I->second;
1579 // We don't know when is the last time an on-the-fly pass is run,
1580 // so we need to releaseMemory / finalize here
1581 FPP->releaseMemoryOnTheFly();
1582 Changed |= FPP->doFinalization(M);
1583 }
Devang Patel05e1a972006-11-07 22:03:15 +00001584 return Changed;
1585}
1586
Devang Patele64d3052007-04-16 20:12:57 +00001587/// Add RequiredPass into list of lower level passes required by pass P.
1588/// RequiredPass is run on the fly by Pass Manager when P requests it
1589/// through getAnalysis interface.
1590void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001591 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1592 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001593 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001594 RequiredPass->getPotentialPassManagerType()) &&
1595 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001596
Devang Patel68f72b12007-04-26 17:50:19 +00001597 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001598 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001599 FPP = new FunctionPassManagerImpl(0);
1600 // FPP is the top level manager.
1601 FPP->setTopLevelManager(FPP);
1602
Devang Patel69e9f6d2007-04-16 20:27:05 +00001603 OnTheFlyManagers[P] = FPP;
1604 }
Devang Patel68f72b12007-04-26 17:50:19 +00001605 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001606
Devang Patel68f72b12007-04-26 17:50:19 +00001607 // Register P as the last user of RequiredPass.
Dan Gohmanc450d2c2010-10-12 00:13:43 +00001608 SmallVector<Pass *, 1> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001609 LU.push_back(RequiredPass);
1610 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001611}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001612
Dan Gohmande6188a2010-08-12 23:50:08 +00001613/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001614/// required by module pass MP. Instantiate analysis pass, by using
1615/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001616Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001617 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001618 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001619
Torok Edwin24c78352009-06-29 18:49:09 +00001620 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001621 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001622 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001623}
1624
1625
Devang Patela1514cb2006-12-07 19:39:39 +00001626//===----------------------------------------------------------------------===//
1627// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001628//
Devang Patelc290c8a2006-11-07 22:23:34 +00001629/// run - Execute all of the passes scheduled for execution. Keep track of
1630/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001631bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001632 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001633 TimingInfo::createTheTimeInfo();
1634
Devang Patelcfd70c42006-12-13 22:10:00 +00001635 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001636 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001637
Devang Patele3068402006-12-21 00:16:50 +00001638 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001639 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1640 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001641 return Changed;
1642}
Devang Patel376fefa2006-11-08 10:29:57 +00001643
Devang Patela1514cb2006-12-07 19:39:39 +00001644//===----------------------------------------------------------------------===//
1645// PassManager implementation
1646
Devang Patel376fefa2006-11-08 10:29:57 +00001647/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001648PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001649 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001650 // PM is the top level manager
1651 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001652}
1653
Devang Patelb67904d2006-12-13 02:36:01 +00001654PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001655 delete PM;
1656}
1657
David Greene103d4b42010-05-10 20:24:27 +00001658/// addImpl - Add a pass to the queue of passes to run, without
1659/// checking whether to add a printer pass.
1660void PassManager::addImpl(Pass *P) {
1661 PM->add(P);
1662}
1663
Devang Patel376fefa2006-11-08 10:29:57 +00001664/// add - Add a pass to the queue of passes to run. This passes ownership of
1665/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1666/// will be destroyed as well, so there is no need to delete the pass. This
1667/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001668void PassManager::add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +00001669 const void* PassID = P->getPassID();
1670 if (ShouldPrintBeforePass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001671 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ")
1672 + P->getPassName() + " ***"));
David Greene9b063df2010-04-02 23:17:14 +00001673
David Greene103d4b42010-05-10 20:24:27 +00001674 addImpl(P);
David Greene9b063df2010-04-02 23:17:14 +00001675
Owen Andersona7aed182010-08-06 18:33:48 +00001676 if (ShouldPrintAfterPass(PassID))
David Greene103d4b42010-05-10 20:24:27 +00001677 addImpl(P->createPrinterPass(dbgs(), std::string("*** IR Dump After ")
1678 + P->getPassName() + " ***"));
Devang Patel376fefa2006-11-08 10:29:57 +00001679}
1680
1681/// run - Execute all of the passes scheduled for execution. Keep track of
1682/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001683bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001684 return PM->run(M);
1685}
1686
Devang Patelb8817b92006-12-14 00:59:42 +00001687//===----------------------------------------------------------------------===//
1688// TimingInfo Class - This class is used to calculate information about the
1689// amount of time each pass takes to execute. This only happens with
1690// -time-passes is enabled on the command line.
1691//
1692bool llvm::TimePassesIsEnabled = false;
1693static cl::opt<bool,true>
1694EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1695 cl::desc("Time each pass, printing elapsed time for each on exit"));
1696
1697// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1698// a non null value (if the -time-passes option is enabled) or it leaves it
1699// null. It may be called multiple times.
1700void TimingInfo::createTheTimeInfo() {
1701 if (!TimePassesIsEnabled || TheTimeInfo) return;
1702
1703 // Constructed the first time this is called, iff -time-passes is enabled.
1704 // This guarantees that the object will be constructed before static globals,
1705 // thus it will be destroyed before them.
1706 static ManagedStatic<TimingInfo> TTI;
1707 TheTimeInfo = &*TTI;
1708}
1709
Devang Patel1c3633e2007-01-29 23:10:37 +00001710/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001711Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001712 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001713 return TheTimeInfo->getPassTimer(P);
Dan Gohman277e7672009-09-28 00:07:05 +00001714 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001715}
1716
Devang Patel1c56a632007-01-08 19:29:38 +00001717//===----------------------------------------------------------------------===//
1718// PMStack implementation
1719//
Devang Patelad98d232007-01-11 22:15:30 +00001720
Devang Patel1c56a632007-01-08 19:29:38 +00001721// Pop Pass Manager from the stack and clear its analysis info.
1722void PMStack::pop() {
1723
1724 PMDataManager *Top = this->top();
1725 Top->initializeAnalysisInfo();
1726
1727 S.pop_back();
1728}
1729
1730// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001731void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001732 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001733
Chris Lattner60987362009-03-06 05:53:14 +00001734 if (!this->empty()) {
1735 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001736
Chris Lattner60987362009-03-06 05:53:14 +00001737 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001738 TPM->addIndirectPassManager(PM);
1739 PM->setTopLevelManager(TPM);
1740 }
1741
Devang Patel15701b52007-01-11 00:19:00 +00001742 S.push_back(PM);
1743}
1744
1745// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001746void PMStack::dump() const {
1747 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001748 E = S.end(); I != E; ++I)
Chris Lattner2fa26e52010-01-22 05:24:46 +00001749 printf("%s ", (*I)->getAsPass()->getPassName());
Chris Lattner60987362009-03-06 05:53:14 +00001750
Devang Patel15701b52007-01-11 00:19:00 +00001751 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001752 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001753}
1754
Devang Patel1c56a632007-01-08 19:29:38 +00001755/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001756/// add self into that manager.
1757void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001758 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001759 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001760 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001761 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1762 if (TopPMType == PreferredType)
1763 break; // We found desired pass manager
1764 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001765 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001766 else
1767 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001768 }
Devang Patel18ff6362008-09-09 21:38:40 +00001769 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001770 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001771}
1772
Devang Patel3312f752007-01-16 21:43:18 +00001773/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001774/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001775void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001776 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001777
Devang Patela3286902008-09-09 17:56:50 +00001778 // Find Module Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001779 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001780 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1781 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001782 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001783 break;
Devang Patel3312f752007-01-16 21:43:18 +00001784 }
Devang Patel3312f752007-01-16 21:43:18 +00001785
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001786 // Create new Function Pass Manager if needed.
1787 FPPassManager *FPP;
1788 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1789 FPP = (FPPassManager *)PMS.top();
1790 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001791 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1792 PMDataManager *PMD = PMS.top();
1793
1794 // [1] Create new Function Pass Manager
1795 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001796 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001797
1798 // [2] Set up new manager's top level manager
1799 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1800 TPM->addIndirectPassManager(FPP);
1801
1802 // [3] Assign manager to manage this new manager. This may create
1803 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001804 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001805
1806 // [4] Push new manager into PMS
1807 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001808 }
1809
Devang Patel3312f752007-01-16 21:43:18 +00001810 // Assign FPP as the manager of this pass.
1811 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001812}
1813
Devang Patel3312f752007-01-16 21:43:18 +00001814/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001815/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001816void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001817 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001818 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001819
Devang Patel15701b52007-01-11 00:19:00 +00001820 // Basic Pass Manager is a leaf pass manager. It does not handle
1821 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001822 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001823 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1824 BBP = (BBPassManager *)PMS.top();
1825 } else {
1826 // If leaf manager is not Basic Block Pass manager then create new
1827 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001828 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1829 PMDataManager *PMD = PMS.top();
1830
1831 // [1] Create new Basic Block Manager
1832 BBP = new BBPassManager(PMD->getDepth() + 1);
1833
1834 // [2] Set up new manager's top level manager
1835 // Basic Block Pass Manager does not live by itself
1836 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1837 TPM->addIndirectPassManager(BBP);
1838
Devang Patel15701b52007-01-11 00:19:00 +00001839 // [3] Assign manager to manage this new manager. This may create
1840 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001841 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001842
Devang Patel3312f752007-01-16 21:43:18 +00001843 // [4] Push new manager into PMS
1844 PMS.push(BBP);
1845 }
Devang Patel1c56a632007-01-08 19:29:38 +00001846
Devang Patel3312f752007-01-16 21:43:18 +00001847 // Assign BBP as the manager of this pass.
1848 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001849}
1850
Dan Gohmand3a20c92008-03-11 16:41:42 +00001851PassManagerBase::~PassManagerBase() {}