blob: 8c427849e71c38f67da0ad9a63d30a9e70906082 [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Devang Patelf1567a52006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000021#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000022#include "llvm/Support/ManagedStatic.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000023#include "llvm/Support/raw_ostream.h"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000024#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000025#include "llvm/System/Threading.h"
Devang Patel9dbe4d12008-07-01 17:44:24 +000026#include "llvm/Analysis/Dominators.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000027#include "llvm-c/Core.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
Duncan Sandse5e9f092009-05-22 08:52:53 +000049// Always verify dominfo if expensive checking is enabled.
50#ifdef XDEBUG
51bool VerifyDomInfo = true;
52#else
Devang Patel99ad4ba2008-07-01 21:36:11 +000053bool VerifyDomInfo = false;
Duncan Sandse5e9f092009-05-22 08:52:53 +000054#endif
Devang Patel9dbe4d12008-07-01 17:44:24 +000055static cl::opt<bool,true>
56VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
57 cl::desc("Verify dominator info (time consuming)"));
58
Devang Patelf1567a52006-12-13 20:03:48 +000059static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000060PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000061 cl::desc("Print PassManager debugging information"),
62 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000063 clEnumVal(None , "disable debug output"),
64 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
65 clEnumVal(Structure , "print pass structure before run()"),
66 clEnumVal(Executions, "print pass name before it is executed"),
67 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000068 clEnumValEnd));
69} // End of llvm namespace
70
Chris Lattner4c1e9542009-03-06 06:45:05 +000071void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
72 if (V == 0 && M == 0)
73 OS << "Releasing pass '";
74 else
75 OS << "Running pass '";
76
77 OS << P->getPassName() << "'";
78
79 if (M) {
80 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
81 return;
82 }
83 if (V == 0) {
84 OS << '\n';
85 return;
86 }
87
Dan Gohman79fc0e92009-03-10 18:47:59 +000088 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +000089 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000090 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +000091 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000092 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +000093 else
Dan Gohman79fc0e92009-03-10 18:47:59 +000094 OS << "value";
95
96 OS << " '";
97 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
98 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +000099}
100
101
Devang Patelffca9102006-12-15 19:39:30 +0000102namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000103
Devang Patelf33f3eb2006-12-07 19:21:29 +0000104//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000105// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000106//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000107/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000108/// pass together and sequence them to process one basic block before
109/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000110class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
111 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000112
113public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000114 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000115 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000116 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000117
Devang Patelca58e352006-11-08 10:05:38 +0000118 /// Execute all of the passes scheduled for execution. Keep track of
119 /// whether any of the passes modifies the function, and if so, return true.
120 bool runOnFunction(Function &F);
121
Devang Patelf9d96b92006-12-07 19:57:52 +0000122 /// Pass Manager itself does not invalidate any analysis info.
123 void getAnalysisUsage(AnalysisUsage &Info) const {
124 Info.setPreservesAll();
125 }
126
Devang Patel475c4532006-12-08 00:59:05 +0000127 bool doInitialization(Module &M);
128 bool doInitialization(Function &F);
129 bool doFinalization(Module &M);
130 bool doFinalization(Function &F);
131
Devang Patele3858e62007-02-01 22:08:25 +0000132 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000133 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000134 }
135
Devang Pateleda56172006-12-12 23:34:33 +0000136 // Print passes managed by this manager
137 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000138 llvm::errs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
140 BasicBlockPass *BP = getContainedPass(Index);
141 BP->dumpPassStructure(Offset + 1);
142 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000143 }
144 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000145
146 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000147 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000148 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
149 return BP;
150 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000151
Devang Patel28349ab2007-02-27 15:00:39 +0000152 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000153 return PMT_BasicBlockPassManager;
154 }
Devang Patelca58e352006-11-08 10:05:38 +0000155};
156
Devang Patel8c78a0b2007-05-03 01:11:54 +0000157char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000158}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000159
Devang Patele7599552007-01-12 18:52:44 +0000160namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000161
Devang Patel10c2ca62006-12-12 22:47:13 +0000162//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000163// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000164//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000165/// FunctionPassManagerImpl manages FPPassManagers
166class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000167 public PMDataManager,
168 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000169private:
170 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000171public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000172 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000173 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000174 Pass(&ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000175 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000176
177 /// add - Add a pass to the queue of passes to run. This passes ownership of
178 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
179 /// will be destroyed as well, so there is no need to delete the pass. This
180 /// implies that all passes MUST be allocated with 'new'.
181 void add(Pass *P) {
182 schedulePass(P);
183 }
184
Torok Edwin24c78352009-06-29 18:49:09 +0000185 // Prepare for running an on the fly pass, freeing memory if needed
186 // from a previous run.
187 void releaseMemoryOnTheFly();
188
Devang Patel67d6a5e2006-12-19 19:46:59 +0000189 /// run - Execute all of the passes scheduled for execution. Keep track of
190 /// whether any of the passes modifies the module, and if so, return true.
191 bool run(Function &F);
192
193 /// doInitialization - Run all of the initializers for the function passes.
194 ///
195 bool doInitialization(Module &M);
196
Dan Gohmane6656eb2007-07-30 14:51:13 +0000197 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000198 ///
199 bool doFinalization(Module &M);
200
201 /// Pass Manager itself does not invalidate any analysis info.
202 void getAnalysisUsage(AnalysisUsage &Info) const {
203 Info.setPreservesAll();
204 }
205
206 inline void addTopLevelPass(Pass *P) {
207
208 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
209
210 // P is a immutable pass and it will be managed by this
211 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000212 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000213 P->setResolver(AR);
214 initializeAnalysisImpl(P);
215 addImmutablePass(IP);
216 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000217 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000218 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219 }
Devang Patel0f080042007-01-12 17:23:48 +0000220
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221 }
222
223 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000224 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
226 return FP;
227 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228};
229
Devang Patel8c78a0b2007-05-03 01:11:54 +0000230char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000231//===----------------------------------------------------------------------===//
232// MPPassManager
233//
234/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000235/// It batches all Module passes and function pass managers together and
236/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000237class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000238public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000239 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000240 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000241 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000242
243 // Delete on the fly managers.
244 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000245 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000246 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
247 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000248 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000249 delete FPP;
250 }
251 }
252
Devang Patelca58e352006-11-08 10:05:38 +0000253 /// run - Execute all of the passes scheduled for execution. Keep track of
254 /// whether any of the passes modifies the module, and if so, return true.
255 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000256
Devang Patelf9d96b92006-12-07 19:57:52 +0000257 /// Pass Manager itself does not invalidate any analysis info.
258 void getAnalysisUsage(AnalysisUsage &Info) const {
259 Info.setPreservesAll();
260 }
261
Devang Patele64d3052007-04-16 20:12:57 +0000262 /// Add RequiredPass into list of lower level passes required by pass P.
263 /// RequiredPass is run on the fly by Pass Manager when P requests it
264 /// through getAnalysis interface.
265 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
266
Devang Patel69e9f6d2007-04-16 20:27:05 +0000267 /// Return function pass corresponding to PassInfo PI, that is
268 /// required by module pass MP. Instantiate analysis pass, by using
269 /// its runOnFunction() for function F.
270 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
271
Devang Patele3858e62007-02-01 22:08:25 +0000272 virtual const char *getPassName() const {
273 return "Module Pass Manager";
274 }
275
Devang Pateleda56172006-12-12 23:34:33 +0000276 // Print passes managed by this manager
277 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000278 llvm::errs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000279 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
280 ModulePass *MP = getContainedPass(Index);
281 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000282 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
283 OnTheFlyManagers.find(MP);
284 if (I != OnTheFlyManagers.end())
285 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000286 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000287 }
288 }
289
Devang Patelabfbe3b2006-12-16 00:56:26 +0000290 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000291 assert(N < PassVector.size() && "Pass number out of range!");
292 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000293 }
294
Devang Patel28349ab2007-02-27 15:00:39 +0000295 virtual PassManagerType getPassManagerType() const {
296 return PMT_ModulePassManager;
297 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000298
299 private:
300 /// Collection of on the fly FPPassManagers. These managers manage
301 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000302 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000303};
304
Devang Patel8c78a0b2007-05-03 01:11:54 +0000305char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000306//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000307// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000308//
Devang Patel09f162c2007-05-01 21:15:47 +0000309
Devang Patel67d6a5e2006-12-19 19:46:59 +0000310/// PassManagerImpl manages MPPassManagers
311class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000312 public PMDataManager,
313 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000314
315public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000316 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000317 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000318 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000319
Devang Patel376fefa2006-11-08 10:29:57 +0000320 /// add - Add a pass to the queue of passes to run. This passes ownership of
321 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
322 /// will be destroyed as well, so there is no need to delete the pass. This
323 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000324 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000325 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000326 }
Devang Patel376fefa2006-11-08 10:29:57 +0000327
328 /// run - Execute all of the passes scheduled for execution. Keep track of
329 /// whether any of the passes modifies the module, and if so, return true.
330 bool run(Module &M);
331
Devang Patelf9d96b92006-12-07 19:57:52 +0000332 /// Pass Manager itself does not invalidate any analysis info.
333 void getAnalysisUsage(AnalysisUsage &Info) const {
334 Info.setPreservesAll();
335 }
336
Devang Patelabcd1d32006-12-07 21:27:23 +0000337 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000338 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000339
340 // P is a immutable pass and it will be managed by this
341 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000342 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000343 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000344 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000345 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000346 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000347 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000348 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000349 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000350 }
351
Devang Patel67d6a5e2006-12-19 19:46:59 +0000352 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000353 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000354 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
355 return MP;
356 }
Devang Patel376fefa2006-11-08 10:29:57 +0000357};
358
Devang Patel8c78a0b2007-05-03 01:11:54 +0000359char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000360} // End of llvm namespace
361
362namespace {
363
364//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000365/// TimingInfo Class - This class is used to calculate information about the
366/// amount of time each pass takes to execute. This only happens when
367/// -time-passes is enabled on the command line.
368///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000369
Owen Anderson5a6960f2009-06-18 20:51:00 +0000370static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000371
Devang Patel1c3633e2007-01-29 23:10:37 +0000372class VISIBILITY_HIDDEN TimingInfo {
373 std::map<Pass*, Timer> TimingData;
374 TimerGroup TG;
375
376public:
377 // Use 'create' member to get this.
378 TimingInfo() : TG("... Pass execution timing report ...") {}
379
380 // TimingDtor - Print out information about timing information
381 ~TimingInfo() {
382 // Delete all of the timers...
383 TimingData.clear();
384 // TimerGroup is deleted next, printing the report.
385 }
386
387 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
388 // to a non null value (if the -time-passes option is enabled) or it leaves it
389 // null. It may be called multiple times.
390 static void createTheTimeInfo();
391
392 void passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000393 if (dynamic_cast<PMDataManager *>(P))
394 return;
395
Owen Anderson5c96ef72009-07-07 18:33:04 +0000396 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000397 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
398 if (I == TimingData.end())
399 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
400 I->second.startTimer();
401 }
Owen Anderson5a6960f2009-06-18 20:51:00 +0000402
Devang Patel1c3633e2007-01-29 23:10:37 +0000403 void passEnded(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000404 if (dynamic_cast<PMDataManager *>(P))
405 return;
406
Owen Anderson5c96ef72009-07-07 18:33:04 +0000407 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000408 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
Chris Lattner60987362009-03-06 05:53:14 +0000409 assert(I != TimingData.end() && "passStarted/passEnded not nested right!");
Devang Patel1c3633e2007-01-29 23:10:37 +0000410 I->second.stopTimer();
411 }
412};
413
Devang Patel1c3633e2007-01-29 23:10:37 +0000414} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000415
Dan Gohmand78c4002008-05-13 00:00:25 +0000416static TimingInfo *TheTimeInfo;
417
Devang Patela1514cb2006-12-07 19:39:39 +0000418//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000419// PMTopLevelManager implementation
420
Devang Patel4268fc02007-01-16 02:00:38 +0000421/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000422PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000423 if (t == TLM_Pass) {
424 MPPassManager *MPP = new MPPassManager(1);
425 MPP->setTopLevelManager(this);
426 addPassManager(MPP);
427 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000428 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000429 FPPassManager *FPP = new FPPassManager(1);
430 FPP->setTopLevelManager(this);
431 addPassManager(FPP);
432 activeStack.push(FPP);
433 }
434}
435
Devang Patelafb1f3622006-12-12 22:35:25 +0000436/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000437void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000438 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000439 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000440 E = AnalysisPasses.end(); I != E; ++I) {
441 Pass *AP = *I;
442 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000443
444 if (P == AP)
445 continue;
446
Devang Patelafb1f3622006-12-12 22:35:25 +0000447 // If AP is the last user of other passes then make P last user of
448 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000449 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000450 LUE = LastUser.end(); LUI != LUE; ++LUI) {
451 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000452 // DenseMap iterator is not invalidated here because
453 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000454 LastUser[LUI->first] = P;
455 }
456 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000457}
458
459/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000460void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000461 Pass *P) {
462 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
463 InversedLastUser.find(P);
464 if (DMI == InversedLastUser.end())
465 return;
466
467 SmallPtrSet<Pass *, 8> &LU = DMI->second;
468 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
469 E = LU.end(); I != E; ++I) {
470 LastUses.push_back(*I);
471 }
472
Devang Patelafb1f3622006-12-12 22:35:25 +0000473}
474
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000475AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
476 AnalysisUsage *AnUsage = NULL;
477 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
478 if (DMI != AnUsageMap.end())
479 AnUsage = DMI->second;
480 else {
481 AnUsage = new AnalysisUsage();
482 P->getAnalysisUsage(*AnUsage);
483 AnUsageMap[P] = AnUsage;
484 }
485 return AnUsage;
486}
487
Devang Patelafb1f3622006-12-12 22:35:25 +0000488/// Schedule pass P for execution. Make sure that passes required by
489/// P are run before P is run. Update analysis info maintained by
490/// the manager. Remove dead passes. This is a recursive function.
491void PMTopLevelManager::schedulePass(Pass *P) {
492
Devang Patel3312f752007-01-16 21:43:18 +0000493 // TODO : Allocate function manager for this pass, other wise required set
494 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000495
Devang Pateld74ede72007-03-06 01:06:16 +0000496 // Give pass a chance to prepare the stage.
497 P->preparePassManager(activeStack);
498
Devang Patel864970e2008-03-18 00:39:19 +0000499 // If P is an analysis pass and it is available then do not
500 // generate the analysis again. Stale analysis info should not be
501 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000502 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000503 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
504 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000505 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000506 }
Devang Patel864970e2008-03-18 00:39:19 +0000507
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000508 AnalysisUsage *AnUsage = findAnalysisUsage(P);
509
Devang Patelfdee7032008-08-14 23:07:48 +0000510 bool checkAnalysis = true;
511 while (checkAnalysis) {
512 checkAnalysis = false;
513
514 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
515 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
516 E = RequiredSet.end(); I != E; ++I) {
517
518 Pass *AnalysisPass = findAnalysisPass(*I);
519 if (!AnalysisPass) {
520 AnalysisPass = (*I)->createPass();
521 if (P->getPotentialPassManagerType () ==
522 AnalysisPass->getPotentialPassManagerType())
523 // Schedule analysis pass that is managed by the same pass manager.
524 schedulePass(AnalysisPass);
525 else if (P->getPotentialPassManagerType () >
526 AnalysisPass->getPotentialPassManagerType()) {
527 // Schedule analysis pass that is managed by a new manager.
528 schedulePass(AnalysisPass);
529 // Recheck analysis passes to ensure that required analysises that
530 // are already checked are still available.
531 checkAnalysis = true;
532 }
533 else
534 // Do not schedule this analysis. Lower level analsyis
535 // passes are run on the fly.
536 delete AnalysisPass;
537 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000538 }
539 }
540
541 // Now all required passes are available.
542 addTopLevelPass(P);
543}
544
545/// Find the pass that implements Analysis AID. Search immutable
546/// passes and all pass managers. If desired pass is not found
547/// then return NULL.
548Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
549
550 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000551 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000552 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000553 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000554 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000555 P = PMD->findAnalysisPass(AID, false);
556 }
557
558 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000559 for (SmallVector<PMDataManager *, 8>::iterator
560 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000561 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
562 P = (*I)->findAnalysisPass(AID, false);
563
Devang Patel0d29ae02008-08-12 15:44:31 +0000564 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000565 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
566 const PassInfo *PI = (*I)->getPassInfo();
567 if (PI == AID)
568 P = *I;
569
570 // If Pass not found then check the interfaces implemented by Immutable Pass
571 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000572 const std::vector<const PassInfo*> &ImmPI =
573 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000574 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
575 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000576 }
577 }
578
Devang Patelafb1f3622006-12-12 22:35:25 +0000579 return P;
580}
581
Devang Pateleda56172006-12-12 23:34:33 +0000582// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000583void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000584
Devang Patelfd4184322007-01-17 20:33:36 +0000585 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000586 return;
587
Devang Pateleda56172006-12-12 23:34:33 +0000588 // Print out the immutable passes
589 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
590 ImmutablePasses[i]->dumpPassStructure(0);
591 }
592
Dan Gohman73caf5f2008-03-13 01:48:32 +0000593 // Every class that derives from PMDataManager also derives from Pass
594 // (sometimes indirectly), but there's no inheritance relationship
595 // between PMDataManager and Pass, so we have to dynamic_cast to get
596 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000597 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000598 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000599 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000600}
601
Devang Patel991aeba2006-12-15 20:13:01 +0000602void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000603
Devang Patelfd4184322007-01-17 20:33:36 +0000604 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000605 return;
606
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000607 errs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000608 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000609 E = PassManagers.end(); I != E; ++I)
610 (*I)->dumpPassArguments();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000611 errs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000612}
613
Devang Patele3068402006-12-21 00:16:50 +0000614void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000615 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000616 E = PassManagers.end(); I != E; ++I)
617 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000618
619 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000620 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000621 E = IndirectPassManagers.end(); I != E; ++I)
622 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000623
Chris Lattner60987362009-03-06 05:53:14 +0000624 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000625 DME = LastUser.end(); DMI != DME; ++DMI) {
626 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
627 InversedLastUser.find(DMI->second);
628 if (InvDMI != InversedLastUser.end()) {
629 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
630 L.insert(DMI->first);
631 } else {
632 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
633 InversedLastUser[DMI->second] = L;
634 }
635 }
Devang Patele3068402006-12-21 00:16:50 +0000636}
637
Devang Patele7599552007-01-12 18:52:44 +0000638/// Destructor
639PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000640 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000641 E = PassManagers.end(); I != E; ++I)
642 delete *I;
643
Devang Patel0d29ae02008-08-12 15:44:31 +0000644 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000645 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
646 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000647
648 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000649 DME = AnUsageMap.end(); DMI != DME; ++DMI)
650 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000651}
652
Devang Patelafb1f3622006-12-12 22:35:25 +0000653//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000654// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000655
Devang Patel643676c2006-11-11 01:10:19 +0000656/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000657void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000658 const PassInfo *PI = P->getPassInfo();
659 if (PI == 0) return;
660
661 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000662
Chris Lattner60987362009-03-06 05:53:14 +0000663 //This pass is the current implementation of all of the interfaces it
664 //implements as well.
665 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
666 for (unsigned i = 0, e = II.size(); i != e; ++i)
667 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000668}
669
Devang Patel9d9fc902007-03-06 17:52:53 +0000670// Return true if P preserves high level analysis used by other
671// passes managed by this manager
672bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000673 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000674 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000675 return true;
676
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000677 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000678 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000679 E = HigherLevelAnalysis.end(); I != E; ++I) {
680 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000681 if (!dynamic_cast<ImmutablePass*>(P1) &&
682 std::find(PreservedSet.begin(), PreservedSet.end(),
683 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000684 PreservedSet.end())
685 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000686 }
687
688 return true;
689}
690
Chris Lattner02eb94c2008-08-07 07:34:50 +0000691/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000692void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000693 // Don't do this unless assertions are enabled.
694#ifdef NDEBUG
695 return;
696#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000697 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
698 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000699
Devang Patelef432532007-07-19 05:36:09 +0000700 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000701 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000702 E = PreservedSet.end(); I != E; ++I) {
703 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000704 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000705 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000706 }
Devang Patela273d1c2007-07-19 18:02:32 +0000707}
708
Devang Patel9dbe4d12008-07-01 17:44:24 +0000709/// verifyDomInfo - Verify dominator information if it is available.
710void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000711 if (!VerifyDomInfo || !P.getResolver())
712 return;
713
Duncan Sands5a913d62009-01-28 13:14:17 +0000714 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000715 if (!DT)
716 return;
717
718 DominatorTree OtherDT;
719 OtherDT.getBase().recalculate(F);
720 if (DT->compare(OtherDT)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000721 errs() << "Dominator Information for " << F.getName() << "\n";
722 errs() << "Pass '" << P.getPassName() << "'\n";
723 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000724 OtherDT.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000725 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000726 DT->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000727 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000728 }
729
Duncan Sands5a913d62009-01-28 13:14:17 +0000730 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000731 if (!DF)
732 return;
733
734 DominanceFrontier OtherDF;
735 std::vector<BasicBlock*> DTRoots = DT->getRoots();
736 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
737 if (DF->compare(OtherDF)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000738 errs() << "Dominator Information for " << F.getName() << "\n";
739 errs() << "Pass '" << P.getPassName() << "'\n";
740 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000741 OtherDF.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000742 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000743 DF->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000744 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000745 }
746}
747
Devang Patel67c79a42008-07-01 19:50:56 +0000748/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000749void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000750 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
751 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000752 return;
753
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000754 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000755 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000756 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000757 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000758 if (!dynamic_cast<ImmutablePass*>(Info->second)
759 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000760 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000761 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000762 if (PassDebugging >= Details) {
763 Pass *S = Info->second;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000764 errs() << " -- '" << P->getPassName() << "' is not preserving '";
765 errs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000766 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000767 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000768 }
Devang Patel349170f2006-11-11 01:24:55 +0000769 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000770
771 // Check inherited analysis also. If P is not preserving analysis
772 // provided by parent manager then remove it here.
773 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
774
775 if (!InheritedAnalysis[Index])
776 continue;
777
778 for (std::map<AnalysisID, Pass*>::iterator
779 I = InheritedAnalysis[Index]->begin(),
780 E = InheritedAnalysis[Index]->end(); I != E; ) {
781 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000782 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
783 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000784 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000785 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000786 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000787 }
788 }
Devang Patelf68a3492006-11-07 22:35:17 +0000789}
790
Devang Patelca189262006-11-14 03:05:08 +0000791/// Remove analysis passes that are not used any longer
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000792void PMDataManager::removeDeadPasses(Pass *P, const StringRef &Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000793 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000794
Devang Patel8adae862007-07-20 18:04:54 +0000795 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000796
Devang Patel2ff44922007-04-16 20:39:59 +0000797 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000798 if (!TPM)
799 return;
800
Devang Patel17ad0962006-12-08 00:37:52 +0000801 TPM->collectLastUses(DeadPasses, P);
802
Devang Patel656a9172008-06-06 17:50:36 +0000803 if (PassDebugging >= Details && !DeadPasses.empty()) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000804 errs() << " -*- '" << P->getPassName();
805 errs() << "' is the last user of following pass instances.";
806 errs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000807 }
808
Devang Patel8adae862007-07-20 18:04:54 +0000809 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000810 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000811
Devang Patel003a5592007-03-05 20:01:30 +0000812 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000813
Chris Lattner4c1e9542009-03-06 06:45:05 +0000814 {
815 // If the pass crashes releasing memory, remember this.
816 PassManagerPrettyStackEntry X(*I);
817
818 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
819 (*I)->releaseMemory();
820 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
821 }
Devang Patelc3e3ca92008-10-06 20:36:36 +0000822 if (const PassInfo *PI = (*I)->getPassInfo()) {
823 std::map<AnalysisID, Pass*>::iterator Pos =
824 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000825
Devang Patelc3e3ca92008-10-06 20:36:36 +0000826 // It is possible that pass is already removed from the AvailableAnalysis
827 if (Pos != AvailableAnalysis.end())
828 AvailableAnalysis.erase(Pos);
829
830 // Remove all interfaces this pass implements, for which it is also
831 // listed as the available implementation.
832 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
833 for (unsigned i = 0, e = II.size(); i != e; ++i) {
834 Pos = AvailableAnalysis.find(II[i]);
835 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
836 AvailableAnalysis.erase(Pos);
837 }
838 }
Devang Patel17ad0962006-12-08 00:37:52 +0000839 }
Devang Patelca189262006-11-14 03:05:08 +0000840}
841
Devang Patel8f677ce2006-12-07 18:47:25 +0000842/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000843/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000844void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000845 // This manager is going to manage pass P. Set up analysis resolver
846 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000847 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000848 P->setResolver(AR);
849
Devang Patelec2b9a72007-03-05 22:57:49 +0000850 // If a FunctionPass F is the last user of ModulePass info M
851 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000852 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000853
Chris Lattner60987362009-03-06 05:53:14 +0000854 if (!ProcessAnalysis) {
855 // Add pass
856 PassVector.push_back(P);
857 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000858 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000859
Chris Lattner60987362009-03-06 05:53:14 +0000860 // At the moment, this pass is the last user of all required passes.
861 SmallVector<Pass *, 12> LastUses;
862 SmallVector<Pass *, 8> RequiredPasses;
863 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
864
865 unsigned PDepth = this->getDepth();
866
867 collectRequiredAnalysis(RequiredPasses,
868 ReqAnalysisNotAvailable, P);
869 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
870 E = RequiredPasses.end(); I != E; ++I) {
871 Pass *PRequired = *I;
872 unsigned RDepth = 0;
873
874 assert(PRequired->getResolver() && "Analysis Resolver is not set");
875 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
876 RDepth = DM.getDepth();
877
878 if (PDepth == RDepth)
879 LastUses.push_back(PRequired);
880 else if (PDepth > RDepth) {
881 // Let the parent claim responsibility of last use
882 TransferLastUses.push_back(PRequired);
883 // Keep track of higher level analysis used by this manager.
884 HigherLevelAnalysis.push_back(PRequired);
885 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000886 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000887 }
888
889 // Set P as P's last user until someone starts using P.
890 // However, if P is a Pass Manager then it does not need
891 // to record its last user.
892 if (!dynamic_cast<PMDataManager *>(P))
893 LastUses.push_back(P);
894 TPM->setLastUser(LastUses, P);
895
896 if (!TransferLastUses.empty()) {
897 Pass *My_PM = dynamic_cast<Pass *>(this);
898 TPM->setLastUser(TransferLastUses, My_PM);
899 TransferLastUses.clear();
900 }
901
902 // Now, take care of required analysises that are not available.
903 for (SmallVector<AnalysisID, 8>::iterator
904 I = ReqAnalysisNotAvailable.begin(),
905 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
906 Pass *AnalysisPass = (*I)->createPass();
907 this->addLowerLevelRequiredPass(P, AnalysisPass);
908 }
909
910 // Take a note of analysis required and made available by this pass.
911 // Remove the analysis not preserved by this pass
912 removeNotPreservedAnalysis(P);
913 recordAvailableAnalysis(P);
914
Devang Patel8cad70d2006-11-11 01:51:02 +0000915 // Add pass
916 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000917}
918
Devang Patele64d3052007-04-16 20:12:57 +0000919
920/// Populate RP with analysis pass that are required by
921/// pass P and are available. Populate RP_NotAvail with analysis
922/// pass that are required by pass P but are not available.
923void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
924 SmallVector<AnalysisID, 8> &RP_NotAvail,
925 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000926 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
927 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000928 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000929 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000930 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
931 RP.push_back(AnalysisPass);
932 else
Chris Lattner60987362009-03-06 05:53:14 +0000933 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000934 }
Devang Patelf58183d2006-12-12 23:09:32 +0000935
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000936 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000937 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000938 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000939 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
940 RP.push_back(AnalysisPass);
941 else
Chris Lattner60987362009-03-06 05:53:14 +0000942 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000943 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000944}
945
Devang Patel07f4f582006-11-14 21:49:36 +0000946// All Required analyses should be available to the pass as it runs! Here
947// we fill in the AnalysisImpls member of the pass so that it can
948// successfully use the getAnalysis() method to retrieve the
949// implementations it needs.
950//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000951void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000952 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
953
Chris Lattnercbd160f2008-08-08 05:33:04 +0000954 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000955 I = AnUsage->getRequiredSet().begin(),
956 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000957 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000958 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000959 // This may be analysis pass that is initialized on the fly.
960 // If that is not the case then it will raise an assert when it is used.
961 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000962 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000963 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000964 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000965 }
966}
967
Devang Patel640c5bb2006-12-08 22:30:11 +0000968/// Find the pass that implements Analysis AID. If desired pass is not found
969/// then return NULL.
970Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
971
972 // Check if AvailableAnalysis map has one entry.
973 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
974
975 if (I != AvailableAnalysis.end())
976 return I->second;
977
978 // Search Parents through TopLevelManager
979 if (SearchParent)
980 return TPM->findAnalysisPass(AID);
981
Devang Patel9d759b82006-12-09 00:09:12 +0000982 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000983}
984
Devang Patel991aeba2006-12-15 20:13:01 +0000985// Print list of passes that are last used by P.
986void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
987
Devang Patel8adae862007-07-20 18:04:54 +0000988 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000989
990 // If this is a on the fly manager then it does not have TPM.
991 if (!TPM)
992 return;
993
Devang Patel991aeba2006-12-15 20:13:01 +0000994 TPM->collectLastUses(LUses, P);
995
Devang Patel8adae862007-07-20 18:04:54 +0000996 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000997 E = LUses.end(); I != E; ++I) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000998 llvm::errs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +0000999 (*I)->dumpPassStructure(0);
1000 }
1001}
1002
1003void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001004 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001005 E = PassVector.end(); I != E; ++I) {
1006 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1007 PMD->dumpPassArguments();
1008 else
1009 if (const PassInfo *PI = (*I)->getPassInfo())
1010 if (!PI->isAnalysisGroup())
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001011 errs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001012 }
1013}
1014
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001015void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1016 enum PassDebuggingString S2,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001017 const StringRef &Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001018 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001019 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001020 errs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001021 switch (S1) {
1022 case EXECUTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001023 errs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001024 break;
1025 case MODIFICATION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001026 errs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001027 break;
1028 case FREEING_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001029 errs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001030 break;
1031 default:
1032 break;
1033 }
1034 switch (S2) {
1035 case ON_BASICBLOCK_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001036 errs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001037 break;
1038 case ON_FUNCTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001039 errs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001040 break;
1041 case ON_MODULE_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001042 errs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001043 break;
1044 case ON_LOOP_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001045 errs() << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001046 break;
1047 case ON_CG_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001048 errs() << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001049 break;
1050 default:
1051 break;
1052 }
Devang Patel991aeba2006-12-15 20:13:01 +00001053}
1054
Chris Lattner4c1e9542009-03-06 06:45:05 +00001055void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001056 if (PassDebugging < Details)
1057 return;
1058
1059 AnalysisUsage analysisUsage;
1060 P->getAnalysisUsage(analysisUsage);
1061 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1062}
1063
Chris Lattner4c1e9542009-03-06 06:45:05 +00001064void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001065 if (PassDebugging < Details)
1066 return;
1067
1068 AnalysisUsage analysisUsage;
1069 P->getAnalysisUsage(analysisUsage);
1070 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1071}
1072
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001073void PMDataManager::dumpAnalysisUsage(const StringRef &Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001074 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001075 assert(PassDebugging >= Details);
1076 if (Set.empty())
1077 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001078 errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001079 for (unsigned i = 0; i != Set.size(); ++i) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001080 if (i) errs() << ",";
1081 errs() << " " << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001082 }
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001083 errs() << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001084}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001085
Devang Patel004937b2007-07-27 20:06:09 +00001086/// Add RequiredPass into list of lower level passes required by pass P.
1087/// RequiredPass is run on the fly by Pass Manager when P requests it
1088/// through getAnalysis interface.
1089/// This should be handled by specific pass manager.
1090void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1091 if (TPM) {
1092 TPM->dumpArguments();
1093 TPM->dumpPasses();
1094 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001095
1096 // Module Level pass may required Function Level analysis info
1097 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1098 // to provide this on demand. In that case, in Pass manager terminology,
1099 // module level pass is requiring lower level analysis info managed by
1100 // lower level pass manager.
1101
1102 // When Pass manager is not able to order required analysis info, Pass manager
1103 // checks whether any lower level manager will be able to provide this
1104 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001105#ifndef NDEBUG
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001106 errs() << "Unable to schedule '" << RequiredPass->getPassName();
1107 errs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001108#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001109 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001110}
1111
Devang Patele7599552007-01-12 18:52:44 +00001112// Destructor
1113PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001114 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001115 E = PassVector.end(); I != E; ++I)
1116 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001117}
1118
Devang Patel9bdf7d42006-12-08 23:28:54 +00001119//===----------------------------------------------------------------------===//
1120// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001121// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1122Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001123 return PM.findAnalysisPass(ID, dir);
1124}
1125
Devang Patel92942812007-04-16 20:56:24 +00001126Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1127 Function &F) {
1128 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1129}
1130
Devang Patela1514cb2006-12-07 19:39:39 +00001131//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001132// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001133
Devang Patel6e5a1132006-11-07 21:31:57 +00001134/// Execute all of the passes scheduled for execution by invoking
1135/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1136/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001137bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001138 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001139 return false;
1140
Devang Patele9585592006-12-08 01:38:28 +00001141 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001142
Devang Patel6e5a1132006-11-07 21:31:57 +00001143 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1145 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001146
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001147 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001148 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001149
Devang Patelabfbe3b2006-12-16 00:56:26 +00001150 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001151
Chris Lattner4c1e9542009-03-06 06:45:05 +00001152 {
1153 // If the pass crashes, remember this.
1154 PassManagerPrettyStackEntry X(BP, *I);
1155
1156 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1157 Changed |= BP->runOnBasicBlock(*I);
1158 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1159 }
Devang Patel93a197c2006-12-14 00:08:04 +00001160
Devang Patel003a5592007-03-05 20:01:30 +00001161 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001162 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001163 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001164 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001165
Devang Patela273d1c2007-07-19 18:02:32 +00001166 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001167 removeNotPreservedAnalysis(BP);
1168 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001169 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001170 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001171
Devang Patel56d48ec2006-12-15 22:57:49 +00001172 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001173}
1174
Devang Patel475c4532006-12-08 00:59:05 +00001175// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001176bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001177 bool Changed = false;
1178
Chris Lattner4c1e9542009-03-06 06:45:05 +00001179 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1180 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001181
1182 return Changed;
1183}
1184
Duncan Sands51495602009-02-13 09:42:34 +00001185bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001186 bool Changed = false;
1187
Chris Lattner4c1e9542009-03-06 06:45:05 +00001188 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1189 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001190
1191 return Changed;
1192}
1193
Duncan Sands51495602009-02-13 09:42:34 +00001194bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001195 bool Changed = false;
1196
Devang Patelabfbe3b2006-12-16 00:56:26 +00001197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1198 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001199 Changed |= BP->doInitialization(F);
1200 }
1201
1202 return Changed;
1203}
1204
Duncan Sands51495602009-02-13 09:42:34 +00001205bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001206 bool Changed = false;
1207
Devang Patelabfbe3b2006-12-16 00:56:26 +00001208 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1209 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001210 Changed |= BP->doFinalization(F);
1211 }
1212
1213 return Changed;
1214}
1215
1216
Devang Patela1514cb2006-12-07 19:39:39 +00001217//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001218// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001219
Devang Patel4e12f862006-11-08 10:44:40 +00001220/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001221FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001222 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001223 // FPM is the top level manager.
1224 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001225
Dan Gohman565df952008-03-13 02:08:36 +00001226 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001227 FPM->setResolver(AR);
1228
Devang Patel1f653682006-12-08 18:57:16 +00001229 MP = P;
1230}
1231
Devang Patelb67904d2006-12-13 02:36:01 +00001232FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001233 delete FPM;
1234}
1235
Devang Patel4e12f862006-11-08 10:44:40 +00001236/// add - Add a pass to the queue of passes to run. This passes
1237/// ownership of the Pass to the PassManager. When the
1238/// PassManager_X is destroyed, the pass will be destroyed as well, so
1239/// there is no need to delete the pass. (TODO delete passes.)
1240/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001241void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001242 FPM->add(P);
1243}
1244
Devang Patel9f3083e2006-11-15 19:39:54 +00001245/// run - Execute all of the passes scheduled for execution. Keep
1246/// track of whether any of the passes modifies the function, and if
1247/// so, return true.
1248///
Devang Patelb67904d2006-12-13 02:36:01 +00001249bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001250 std::string errstr;
1251 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001252 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001253 }
Devang Patel272908d2006-12-08 22:57:48 +00001254 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001255}
1256
1257
Devang Patelff631ae2006-11-15 01:27:05 +00001258/// doInitialization - Run all of the initializers for the function passes.
1259///
Devang Patelb67904d2006-12-13 02:36:01 +00001260bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001261 return FPM->doInitialization(*MP->getModule());
1262}
1263
Dan Gohmane6656eb2007-07-30 14:51:13 +00001264/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001265///
Devang Patelb67904d2006-12-13 02:36:01 +00001266bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001267 return FPM->doFinalization(*MP->getModule());
1268}
1269
Devang Patela1514cb2006-12-07 19:39:39 +00001270//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001271// FunctionPassManagerImpl implementation
1272//
Duncan Sands51495602009-02-13 09:42:34 +00001273bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001274 bool Changed = false;
1275
Chris Lattner4c1e9542009-03-06 06:45:05 +00001276 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1277 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001278
1279 return Changed;
1280}
1281
Duncan Sands51495602009-02-13 09:42:34 +00001282bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001283 bool Changed = false;
1284
Chris Lattner4c1e9542009-03-06 06:45:05 +00001285 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1286 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287
1288 return Changed;
1289}
1290
Devang Patelec9c58f2009-04-01 22:34:41 +00001291/// cleanup - After running all passes, clean up pass manager cache.
1292void FPPassManager::cleanup() {
1293 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1294 FunctionPass *FP = getContainedPass(Index);
1295 AnalysisResolver *AR = FP->getResolver();
1296 assert(AR && "Analysis Resolver is not set");
1297 AR->clearAnalysisImpls();
1298 }
1299}
1300
Torok Edwin24c78352009-06-29 18:49:09 +00001301void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1302 if (!wasRun)
1303 return;
1304 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1305 FPPassManager *FPPM = getContainedManager(Index);
1306 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1307 FPPM->getContainedPass(Index)->releaseMemory();
1308 }
1309 }
Torok Edwin896556e2009-06-29 21:05:10 +00001310 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001311}
1312
Devang Patel67d6a5e2006-12-19 19:46:59 +00001313// Execute all the passes managed by this top level manager.
1314// Return true if any function is modified by a pass.
1315bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001316 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001317 TimingInfo::createTheTimeInfo();
1318
1319 dumpArguments();
1320 dumpPasses();
1321
Devang Patele3068402006-12-21 00:16:50 +00001322 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001323 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1324 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001325
1326 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1327 getContainedManager(Index)->cleanup();
1328
Torok Edwin24c78352009-06-29 18:49:09 +00001329 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001330 return Changed;
1331}
1332
1333//===----------------------------------------------------------------------===//
1334// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001335
Devang Patel8c78a0b2007-05-03 01:11:54 +00001336char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001337/// Print passes managed by this manager
1338void FPPassManager::dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001339 llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001340 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1341 FunctionPass *FP = getContainedPass(Index);
1342 FP->dumpPassStructure(Offset + 1);
1343 dumpLastUses(FP, Offset+1);
1344 }
1345}
1346
1347
Devang Patel0c2012f2006-11-07 21:49:50 +00001348/// Execute all of the passes scheduled for execution by invoking
1349/// runOnFunction method. Keep track of whether any of the passes modifies
1350/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001351bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001352 if (F.isDeclaration())
1353 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001354
1355 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001356
Devang Patelcbbf2912008-03-20 01:09:53 +00001357 // Collect inherited analysis from Module level pass manager.
1358 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001359
Devang Patelabfbe3b2006-12-16 00:56:26 +00001360 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1361 FunctionPass *FP = getContainedPass(Index);
1362
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001363 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001364 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001365
Devang Patelabfbe3b2006-12-16 00:56:26 +00001366 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001367
Chris Lattner4c1e9542009-03-06 06:45:05 +00001368 {
1369 PassManagerPrettyStackEntry X(FP, F);
1370
1371 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1372 Changed |= FP->runOnFunction(F);
1373 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1374 }
Devang Patel93a197c2006-12-14 00:08:04 +00001375
Devang Patel003a5592007-03-05 20:01:30 +00001376 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001377 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001378 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001379
Devang Patela273d1c2007-07-19 18:02:32 +00001380 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001381 removeNotPreservedAnalysis(FP);
1382 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001383 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001384
Devang Patel67c79a42008-07-01 19:50:56 +00001385 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001386 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001387 }
1388 return Changed;
1389}
1390
Devang Patel67d6a5e2006-12-19 19:46:59 +00001391bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001392 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001393
Chris Lattner60987362009-03-06 05:53:14 +00001394 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1395 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396
1397 return Changed |= doFinalization(M);
1398}
1399
Duncan Sands51495602009-02-13 09:42:34 +00001400bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001401 bool Changed = false;
1402
Chris Lattner4c1e9542009-03-06 06:45:05 +00001403 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1404 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001405
1406 return Changed;
1407}
1408
Duncan Sands51495602009-02-13 09:42:34 +00001409bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001410 bool Changed = false;
1411
Chris Lattner4c1e9542009-03-06 06:45:05 +00001412 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1413 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001414
Devang Patelff631ae2006-11-15 01:27:05 +00001415 return Changed;
1416}
1417
Devang Patela1514cb2006-12-07 19:39:39 +00001418//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001419// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001420
Devang Patel05e1a972006-11-07 22:03:15 +00001421/// Execute all of the passes scheduled for execution by invoking
1422/// runOnModule method. Keep track of whether any of the passes modifies
1423/// the module, and if so, return true.
1424bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001426 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001427
Torok Edwin24c78352009-06-29 18:49:09 +00001428 // Initialize on-the-fly passes
1429 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1430 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1431 I != E; ++I) {
1432 FunctionPassManagerImpl *FPP = I->second;
1433 Changed |= FPP->doInitialization(M);
1434 }
1435
Devang Patelabfbe3b2006-12-16 00:56:26 +00001436 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1437 ModulePass *MP = getContainedPass(Index);
1438
Dan Gohman929391a2008-01-29 12:09:55 +00001439 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1440 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001441 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001442
Devang Patelabfbe3b2006-12-16 00:56:26 +00001443 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001444
Chris Lattner4c1e9542009-03-06 06:45:05 +00001445 {
1446 PassManagerPrettyStackEntry X(MP, M);
1447 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1448 Changed |= MP->runOnModule(M);
1449 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1450 }
Devang Patel93a197c2006-12-14 00:08:04 +00001451
Devang Patel003a5592007-03-05 20:01:30 +00001452 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001453 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1454 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001455 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001456
Devang Patela273d1c2007-07-19 18:02:32 +00001457 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001458 removeNotPreservedAnalysis(MP);
1459 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001460 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001461 }
Torok Edwin24c78352009-06-29 18:49:09 +00001462
1463 // Finalize on-the-fly passes
1464 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1465 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1466 I != E; ++I) {
1467 FunctionPassManagerImpl *FPP = I->second;
1468 // We don't know when is the last time an on-the-fly pass is run,
1469 // so we need to releaseMemory / finalize here
1470 FPP->releaseMemoryOnTheFly();
1471 Changed |= FPP->doFinalization(M);
1472 }
Devang Patel05e1a972006-11-07 22:03:15 +00001473 return Changed;
1474}
1475
Devang Patele64d3052007-04-16 20:12:57 +00001476/// Add RequiredPass into list of lower level passes required by pass P.
1477/// RequiredPass is run on the fly by Pass Manager when P requests it
1478/// through getAnalysis interface.
1479void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001480 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1481 "Unable to handle Pass that requires lower level Analysis pass");
1482 assert((P->getPotentialPassManagerType() <
1483 RequiredPass->getPotentialPassManagerType()) &&
1484 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001485
Devang Patel68f72b12007-04-26 17:50:19 +00001486 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001487 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001488 FPP = new FunctionPassManagerImpl(0);
1489 // FPP is the top level manager.
1490 FPP->setTopLevelManager(FPP);
1491
Devang Patel69e9f6d2007-04-16 20:27:05 +00001492 OnTheFlyManagers[P] = FPP;
1493 }
Devang Patel68f72b12007-04-26 17:50:19 +00001494 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001495
Devang Patel68f72b12007-04-26 17:50:19 +00001496 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001497 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001498 LU.push_back(RequiredPass);
1499 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001500}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001501
1502/// Return function pass corresponding to PassInfo PI, that is
1503/// required by module pass MP. Instantiate analysis pass, by using
1504/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001505Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001506 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001507 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001508
Torok Edwin24c78352009-06-29 18:49:09 +00001509 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001510 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001511 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001512}
1513
1514
Devang Patela1514cb2006-12-07 19:39:39 +00001515//===----------------------------------------------------------------------===//
1516// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001517//
Devang Patelc290c8a2006-11-07 22:23:34 +00001518/// run - Execute all of the passes scheduled for execution. Keep track of
1519/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001520bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001521 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001522 TimingInfo::createTheTimeInfo();
1523
Devang Patelcfd70c42006-12-13 22:10:00 +00001524 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001525 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001526
Devang Patele3068402006-12-21 00:16:50 +00001527 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001528 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1529 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001530 return Changed;
1531}
Devang Patel376fefa2006-11-08 10:29:57 +00001532
Devang Patela1514cb2006-12-07 19:39:39 +00001533//===----------------------------------------------------------------------===//
1534// PassManager implementation
1535
Devang Patel376fefa2006-11-08 10:29:57 +00001536/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001537PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001538 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001539 // PM is the top level manager
1540 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001541}
1542
Devang Patelb67904d2006-12-13 02:36:01 +00001543PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001544 delete PM;
1545}
1546
Devang Patel376fefa2006-11-08 10:29:57 +00001547/// add - Add a pass to the queue of passes to run. This passes ownership of
1548/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1549/// will be destroyed as well, so there is no need to delete the pass. This
1550/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001551void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001552 PM->add(P);
1553}
1554
1555/// run - Execute all of the passes scheduled for execution. Keep track of
1556/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001557bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001558 return PM->run(M);
1559}
1560
Devang Patelb8817b92006-12-14 00:59:42 +00001561//===----------------------------------------------------------------------===//
1562// TimingInfo Class - This class is used to calculate information about the
1563// amount of time each pass takes to execute. This only happens with
1564// -time-passes is enabled on the command line.
1565//
1566bool llvm::TimePassesIsEnabled = false;
1567static cl::opt<bool,true>
1568EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1569 cl::desc("Time each pass, printing elapsed time for each on exit"));
1570
1571// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1572// a non null value (if the -time-passes option is enabled) or it leaves it
1573// null. It may be called multiple times.
1574void TimingInfo::createTheTimeInfo() {
1575 if (!TimePassesIsEnabled || TheTimeInfo) return;
1576
1577 // Constructed the first time this is called, iff -time-passes is enabled.
1578 // This guarantees that the object will be constructed before static globals,
1579 // thus it will be destroyed before them.
1580 static ManagedStatic<TimingInfo> TTI;
1581 TheTimeInfo = &*TTI;
1582}
1583
Devang Patel1c3633e2007-01-29 23:10:37 +00001584/// If TimingInfo is enabled then start pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001585void llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001586 if (TheTimeInfo)
1587 TheTimeInfo->passStarted(P);
1588}
1589
1590/// If TimingInfo is enabled then stop pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001591void llvm::StopPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001592 if (TheTimeInfo)
1593 TheTimeInfo->passEnded(P);
1594}
1595
Devang Patel1c56a632007-01-08 19:29:38 +00001596//===----------------------------------------------------------------------===//
1597// PMStack implementation
1598//
Devang Patelad98d232007-01-11 22:15:30 +00001599
Devang Patel1c56a632007-01-08 19:29:38 +00001600// Pop Pass Manager from the stack and clear its analysis info.
1601void PMStack::pop() {
1602
1603 PMDataManager *Top = this->top();
1604 Top->initializeAnalysisInfo();
1605
1606 S.pop_back();
1607}
1608
1609// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001610void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001611 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001612
Chris Lattner60987362009-03-06 05:53:14 +00001613 if (!this->empty()) {
1614 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001615
Chris Lattner60987362009-03-06 05:53:14 +00001616 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001617 TPM->addIndirectPassManager(PM);
1618 PM->setTopLevelManager(TPM);
1619 }
1620
Devang Patel15701b52007-01-11 00:19:00 +00001621 S.push_back(PM);
1622}
1623
1624// Dump content of the pass manager stack.
1625void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001626 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1627 E = S.end(); I != E; ++I)
1628 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1629
Devang Patel15701b52007-01-11 00:19:00 +00001630 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001631 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001632}
1633
Devang Patel1c56a632007-01-08 19:29:38 +00001634/// Find appropriate Module Pass Manager in the PM Stack and
1635/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001636void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001637 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001638 // Find Module Pass Manager
1639 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001640 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1641 if (TopPMType == PreferredType)
1642 break; // We found desired pass manager
1643 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001644 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001645 else
1646 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001647 }
Devang Patel18ff6362008-09-09 21:38:40 +00001648 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001649 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001650}
1651
Devang Patel3312f752007-01-16 21:43:18 +00001652/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1653/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001654void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001655 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001656
Devang Patela3286902008-09-09 17:56:50 +00001657 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001658 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001659 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1660 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001661 else
Devang Patel3312f752007-01-16 21:43:18 +00001662 break;
1663 }
1664 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1665
1666 // Create new Function Pass Manager
1667 if (!FPP) {
1668 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1669 PMDataManager *PMD = PMS.top();
1670
1671 // [1] Create new Function Pass Manager
1672 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001673 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001674
1675 // [2] Set up new manager's top level manager
1676 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1677 TPM->addIndirectPassManager(FPP);
1678
1679 // [3] Assign manager to manage this new manager. This may create
1680 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001681 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001682
1683 // [4] Push new manager into PMS
1684 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001685 }
1686
Devang Patel3312f752007-01-16 21:43:18 +00001687 // Assign FPP as the manager of this pass.
1688 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001689}
1690
Devang Patel3312f752007-01-16 21:43:18 +00001691/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001692/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001693void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001694 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001695 BBPassManager *BBP = NULL;
1696
Devang Patel15701b52007-01-11 00:19:00 +00001697 // Basic Pass Manager is a leaf pass manager. It does not handle
1698 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001699 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001700 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001701
Devang Patel3312f752007-01-16 21:43:18 +00001702 // If leaf manager is not Basic Block Pass manager then create new
1703 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001704
Devang Patel3312f752007-01-16 21:43:18 +00001705 if (!BBP) {
1706 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1707 PMDataManager *PMD = PMS.top();
1708
1709 // [1] Create new Basic Block Manager
1710 BBP = new BBPassManager(PMD->getDepth() + 1);
1711
1712 // [2] Set up new manager's top level manager
1713 // Basic Block Pass Manager does not live by itself
1714 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1715 TPM->addIndirectPassManager(BBP);
1716
Devang Patel15701b52007-01-11 00:19:00 +00001717 // [3] Assign manager to manage this new manager. This may create
1718 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001719 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001720
Devang Patel3312f752007-01-16 21:43:18 +00001721 // [4] Push new manager into PMS
1722 PMS.push(BBP);
1723 }
Devang Patel1c56a632007-01-08 19:29:38 +00001724
Devang Patel3312f752007-01-16 21:43:18 +00001725 // Assign BBP as the manager of this pass.
1726 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001727}
1728
Dan Gohmand3a20c92008-03-11 16:41:42 +00001729PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001730
1731/*===-- C Bindings --------------------------------------------------------===*/
1732
1733LLVMPassManagerRef LLVMCreatePassManager() {
1734 return wrap(new PassManager());
1735}
1736
1737LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1738 return wrap(new FunctionPassManager(unwrap(P)));
1739}
1740
1741int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1742 return unwrap<PassManager>(PM)->run(*unwrap(M));
1743}
1744
1745int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1746 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1747}
1748
1749int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1750 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1751}
1752
1753int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1754 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1755}
1756
1757void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1758 delete unwrap(PM);
1759}