blob: a3496ed9b4f93629021a849810a38f58efc85b1a [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"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000022#include "llvm/Support/raw_ostream.h"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000023#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000024#include "llvm/System/Threading.h"
Devang Patel9dbe4d12008-07-01 17:44:24 +000025#include "llvm/Analysis/Dominators.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000026#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000027#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000028#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000029#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000030using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000031
Devang Patele7599552007-01-12 18:52:44 +000032// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000033
Devang Patelf1567a52006-12-13 20:03:48 +000034namespace llvm {
35
36//===----------------------------------------------------------------------===//
37// Pass debugging information. Often it is useful to find out what pass is
38// running when a crash occurs in a utility. When this library is compiled with
39// debugging on, a command line option (--debug-pass) is enabled that causes the
40// pass name to be printed before it executes.
41//
42
Devang Patel03fb5872006-12-13 21:13:31 +000043// Different debug levels that can be enabled...
44enum PassDebugLevel {
45 None, Arguments, Structure, Executions, Details
46};
47
Duncan Sandse5e9f092009-05-22 08:52:53 +000048// Always verify dominfo if expensive checking is enabled.
49#ifdef XDEBUG
50bool VerifyDomInfo = true;
51#else
Devang Patel99ad4ba2008-07-01 21:36:11 +000052bool VerifyDomInfo = false;
Duncan Sandse5e9f092009-05-22 08:52:53 +000053#endif
Devang Patel9dbe4d12008-07-01 17:44:24 +000054static cl::opt<bool,true>
55VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
56 cl::desc("Verify dominator info (time consuming)"));
57
Devang Patelf1567a52006-12-13 20:03:48 +000058static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000059PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000060 cl::desc("Print PassManager debugging information"),
61 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000062 clEnumVal(None , "disable debug output"),
63 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
64 clEnumVal(Structure , "print pass structure before run()"),
65 clEnumVal(Executions, "print pass name before it is executed"),
66 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000067 clEnumValEnd));
68} // End of llvm namespace
69
Chris Lattnerd4d966f2009-09-15 05:03:04 +000070/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
71/// or higher is specified.
72bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
73 return PassDebugging >= Executions;
74}
75
76
77
78
Chris Lattner4c1e9542009-03-06 06:45:05 +000079void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
80 if (V == 0 && M == 0)
81 OS << "Releasing pass '";
82 else
83 OS << "Running pass '";
84
85 OS << P->getPassName() << "'";
86
87 if (M) {
88 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
89 return;
90 }
91 if (V == 0) {
92 OS << '\n';
93 return;
94 }
95
Dan Gohman79fc0e92009-03-10 18:47:59 +000096 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +000097 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000098 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +000099 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000100 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000101 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000102 OS << "value";
103
104 OS << " '";
105 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
106 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000107}
108
109
Devang Patelffca9102006-12-15 19:39:30 +0000110namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000111
Devang Patelf33f3eb2006-12-07 19:21:29 +0000112//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000114//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000115/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000116/// pass together and sequence them to process one basic block before
117/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000118class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
119 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000120
121public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000122 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000123 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000124 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000125
Devang Patelca58e352006-11-08 10:05:38 +0000126 /// Execute all of the passes scheduled for execution. Keep track of
127 /// whether any of the passes modifies the function, and if so, return true.
128 bool runOnFunction(Function &F);
129
Devang Patelf9d96b92006-12-07 19:57:52 +0000130 /// Pass Manager itself does not invalidate any analysis info.
131 void getAnalysisUsage(AnalysisUsage &Info) const {
132 Info.setPreservesAll();
133 }
134
Devang Patel475c4532006-12-08 00:59:05 +0000135 bool doInitialization(Module &M);
136 bool doInitialization(Function &F);
137 bool doFinalization(Module &M);
138 bool doFinalization(Function &F);
139
Devang Patele3858e62007-02-01 22:08:25 +0000140 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000141 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000142 }
143
Devang Pateleda56172006-12-12 23:34:33 +0000144 // Print passes managed by this manager
145 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000146 llvm::errs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000147 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
148 BasicBlockPass *BP = getContainedPass(Index);
149 BP->dumpPassStructure(Offset + 1);
150 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000151 }
152 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000153
154 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000155 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000156 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
157 return BP;
158 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000159
Devang Patel28349ab2007-02-27 15:00:39 +0000160 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000161 return PMT_BasicBlockPassManager;
162 }
Devang Patelca58e352006-11-08 10:05:38 +0000163};
164
Devang Patel8c78a0b2007-05-03 01:11:54 +0000165char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000166}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167
Devang Patele7599552007-01-12 18:52:44 +0000168namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000169
Devang Patel10c2ca62006-12-12 22:47:13 +0000170//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000171// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000172//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173/// FunctionPassManagerImpl manages FPPassManagers
174class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000175 public PMDataManager,
176 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000177private:
178 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000179public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000180 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000181 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000182 Pass(&ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000183 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000184
185 /// add - Add a pass to the queue of passes to run. This passes ownership of
186 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
187 /// will be destroyed as well, so there is no need to delete the pass. This
188 /// implies that all passes MUST be allocated with 'new'.
189 void add(Pass *P) {
190 schedulePass(P);
191 }
192
Torok Edwin24c78352009-06-29 18:49:09 +0000193 // Prepare for running an on the fly pass, freeing memory if needed
194 // from a previous run.
195 void releaseMemoryOnTheFly();
196
Devang Patel67d6a5e2006-12-19 19:46:59 +0000197 /// run - Execute all of the passes scheduled for execution. Keep track of
198 /// whether any of the passes modifies the module, and if so, return true.
199 bool run(Function &F);
200
201 /// doInitialization - Run all of the initializers for the function passes.
202 ///
203 bool doInitialization(Module &M);
204
Dan Gohmane6656eb2007-07-30 14:51:13 +0000205 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000206 ///
207 bool doFinalization(Module &M);
208
209 /// Pass Manager itself does not invalidate any analysis info.
210 void getAnalysisUsage(AnalysisUsage &Info) const {
211 Info.setPreservesAll();
212 }
213
214 inline void addTopLevelPass(Pass *P) {
215
216 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
217
218 // P is a immutable pass and it will be managed by this
219 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000220 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221 P->setResolver(AR);
222 initializeAnalysisImpl(P);
223 addImmutablePass(IP);
224 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000225 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000226 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227 }
Devang Patel0f080042007-01-12 17:23:48 +0000228
Devang Patel67d6a5e2006-12-19 19:46:59 +0000229 }
230
231 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000232 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
234 return FP;
235 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000236};
237
Devang Patel8c78a0b2007-05-03 01:11:54 +0000238char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000239//===----------------------------------------------------------------------===//
240// MPPassManager
241//
242/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000243/// It batches all Module passes and function pass managers together and
244/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000245class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000246public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000247 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000248 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000249 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000250
251 // Delete on the fly managers.
252 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000253 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000254 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
255 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000256 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000257 delete FPP;
258 }
259 }
260
Devang Patelca58e352006-11-08 10:05:38 +0000261 /// run - Execute all of the passes scheduled for execution. Keep track of
262 /// whether any of the passes modifies the module, and if so, return true.
263 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000264
Devang Patelf9d96b92006-12-07 19:57:52 +0000265 /// Pass Manager itself does not invalidate any analysis info.
266 void getAnalysisUsage(AnalysisUsage &Info) const {
267 Info.setPreservesAll();
268 }
269
Devang Patele64d3052007-04-16 20:12:57 +0000270 /// Add RequiredPass into list of lower level passes required by pass P.
271 /// RequiredPass is run on the fly by Pass Manager when P requests it
272 /// through getAnalysis interface.
273 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
274
Devang Patel69e9f6d2007-04-16 20:27:05 +0000275 /// Return function pass corresponding to PassInfo PI, that is
276 /// required by module pass MP. Instantiate analysis pass, by using
277 /// its runOnFunction() for function F.
278 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
279
Devang Patele3858e62007-02-01 22:08:25 +0000280 virtual const char *getPassName() const {
281 return "Module Pass Manager";
282 }
283
Devang Pateleda56172006-12-12 23:34:33 +0000284 // Print passes managed by this manager
285 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000286 llvm::errs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000287 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
288 ModulePass *MP = getContainedPass(Index);
289 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000290 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
291 OnTheFlyManagers.find(MP);
292 if (I != OnTheFlyManagers.end())
293 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000294 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000295 }
296 }
297
Devang Patelabfbe3b2006-12-16 00:56:26 +0000298 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000299 assert(N < PassVector.size() && "Pass number out of range!");
300 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000301 }
302
Devang Patel28349ab2007-02-27 15:00:39 +0000303 virtual PassManagerType getPassManagerType() const {
304 return PMT_ModulePassManager;
305 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000306
307 private:
308 /// Collection of on the fly FPPassManagers. These managers manage
309 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000310 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000311};
312
Devang Patel8c78a0b2007-05-03 01:11:54 +0000313char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000314//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000315// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000316//
Devang Patel09f162c2007-05-01 21:15:47 +0000317
Devang Patel67d6a5e2006-12-19 19:46:59 +0000318/// PassManagerImpl manages MPPassManagers
319class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000320 public PMDataManager,
321 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000322
323public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000324 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000325 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000326 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000327
Devang Patel376fefa2006-11-08 10:29:57 +0000328 /// add - Add a pass to the queue of passes to run. This passes ownership of
329 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
330 /// will be destroyed as well, so there is no need to delete the pass. This
331 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000332 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000333 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000334 }
Devang Patel376fefa2006-11-08 10:29:57 +0000335
336 /// run - Execute all of the passes scheduled for execution. Keep track of
337 /// whether any of the passes modifies the module, and if so, return true.
338 bool run(Module &M);
339
Devang Patelf9d96b92006-12-07 19:57:52 +0000340 /// Pass Manager itself does not invalidate any analysis info.
341 void getAnalysisUsage(AnalysisUsage &Info) const {
342 Info.setPreservesAll();
343 }
344
Devang Patelabcd1d32006-12-07 21:27:23 +0000345 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000346 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000347
348 // P is a immutable pass and it will be managed by this
349 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000350 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000351 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000352 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000353 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000354 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000355 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000356 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000357 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000358 }
359
Devang Patel67d6a5e2006-12-19 19:46:59 +0000360 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000361 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000362 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
363 return MP;
364 }
Devang Patel376fefa2006-11-08 10:29:57 +0000365};
366
Devang Patel8c78a0b2007-05-03 01:11:54 +0000367char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000368} // End of llvm namespace
369
370namespace {
371
372//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000373/// TimingInfo Class - This class is used to calculate information about the
374/// amount of time each pass takes to execute. This only happens when
375/// -time-passes is enabled on the command line.
376///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000377
Owen Anderson5a6960f2009-06-18 20:51:00 +0000378static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000379
Devang Patel1c3633e2007-01-29 23:10:37 +0000380class VISIBILITY_HIDDEN TimingInfo {
381 std::map<Pass*, Timer> TimingData;
382 TimerGroup TG;
383
384public:
385 // Use 'create' member to get this.
386 TimingInfo() : TG("... Pass execution timing report ...") {}
387
388 // TimingDtor - Print out information about timing information
389 ~TimingInfo() {
390 // Delete all of the timers...
391 TimingData.clear();
392 // TimerGroup is deleted next, printing the report.
393 }
394
395 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
396 // to a non null value (if the -time-passes option is enabled) or it leaves it
397 // null. It may be called multiple times.
398 static void createTheTimeInfo();
399
Dan Gohman277e7672009-09-28 00:07:05 +0000400 /// passStarted - This method creates a timer for the given pass if it doesn't
401 /// already have one, and starts the timer.
402 Timer *passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000403 if (dynamic_cast<PMDataManager *>(P))
Dan Gohman277e7672009-09-28 00:07:05 +0000404 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000405
Owen Anderson5c96ef72009-07-07 18:33:04 +0000406 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000407 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
408 if (I == TimingData.end())
409 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
Dan Gohman277e7672009-09-28 00:07:05 +0000410 Timer *T = &I->second;
411 T->startTimer();
412 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000413 }
414};
415
Devang Patel1c3633e2007-01-29 23:10:37 +0000416} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000417
Dan Gohmand78c4002008-05-13 00:00:25 +0000418static TimingInfo *TheTimeInfo;
419
Devang Patela1514cb2006-12-07 19:39:39 +0000420//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000421// PMTopLevelManager implementation
422
Devang Patel4268fc02007-01-16 02:00:38 +0000423/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000424PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000425 if (t == TLM_Pass) {
426 MPPassManager *MPP = new MPPassManager(1);
427 MPP->setTopLevelManager(this);
428 addPassManager(MPP);
429 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000430 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000431 FPPassManager *FPP = new FPPassManager(1);
432 FPP->setTopLevelManager(this);
433 addPassManager(FPP);
434 activeStack.push(FPP);
435 }
436}
437
Devang Patelafb1f3622006-12-12 22:35:25 +0000438/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000439void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000440 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000441 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000442 E = AnalysisPasses.end(); I != E; ++I) {
443 Pass *AP = *I;
444 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000445
446 if (P == AP)
447 continue;
448
Devang Patelafb1f3622006-12-12 22:35:25 +0000449 // If AP is the last user of other passes then make P last user of
450 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000451 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000452 LUE = LastUser.end(); LUI != LUE; ++LUI) {
453 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000454 // DenseMap iterator is not invalidated here because
455 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000456 LastUser[LUI->first] = P;
457 }
458 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000459}
460
461/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000462void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000463 Pass *P) {
464 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
465 InversedLastUser.find(P);
466 if (DMI == InversedLastUser.end())
467 return;
468
469 SmallPtrSet<Pass *, 8> &LU = DMI->second;
470 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
471 E = LU.end(); I != E; ++I) {
472 LastUses.push_back(*I);
473 }
474
Devang Patelafb1f3622006-12-12 22:35:25 +0000475}
476
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000477AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
478 AnalysisUsage *AnUsage = NULL;
479 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
480 if (DMI != AnUsageMap.end())
481 AnUsage = DMI->second;
482 else {
483 AnUsage = new AnalysisUsage();
484 P->getAnalysisUsage(*AnUsage);
485 AnUsageMap[P] = AnUsage;
486 }
487 return AnUsage;
488}
489
Devang Patelafb1f3622006-12-12 22:35:25 +0000490/// Schedule pass P for execution. Make sure that passes required by
491/// P are run before P is run. Update analysis info maintained by
492/// the manager. Remove dead passes. This is a recursive function.
493void PMTopLevelManager::schedulePass(Pass *P) {
494
Devang Patel3312f752007-01-16 21:43:18 +0000495 // TODO : Allocate function manager for this pass, other wise required set
496 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000497
Devang Pateld74ede72007-03-06 01:06:16 +0000498 // Give pass a chance to prepare the stage.
499 P->preparePassManager(activeStack);
500
Devang Patel864970e2008-03-18 00:39:19 +0000501 // If P is an analysis pass and it is available then do not
502 // generate the analysis again. Stale analysis info should not be
503 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000504 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000505 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
506 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000507 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000508 }
Devang Patel864970e2008-03-18 00:39:19 +0000509
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000510 AnalysisUsage *AnUsage = findAnalysisUsage(P);
511
Devang Patelfdee7032008-08-14 23:07:48 +0000512 bool checkAnalysis = true;
513 while (checkAnalysis) {
514 checkAnalysis = false;
515
516 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
517 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
518 E = RequiredSet.end(); I != E; ++I) {
519
520 Pass *AnalysisPass = findAnalysisPass(*I);
521 if (!AnalysisPass) {
522 AnalysisPass = (*I)->createPass();
523 if (P->getPotentialPassManagerType () ==
524 AnalysisPass->getPotentialPassManagerType())
525 // Schedule analysis pass that is managed by the same pass manager.
526 schedulePass(AnalysisPass);
527 else if (P->getPotentialPassManagerType () >
528 AnalysisPass->getPotentialPassManagerType()) {
529 // Schedule analysis pass that is managed by a new manager.
530 schedulePass(AnalysisPass);
531 // Recheck analysis passes to ensure that required analysises that
532 // are already checked are still available.
533 checkAnalysis = true;
534 }
535 else
536 // Do not schedule this analysis. Lower level analsyis
537 // passes are run on the fly.
538 delete AnalysisPass;
539 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000540 }
541 }
542
543 // Now all required passes are available.
544 addTopLevelPass(P);
545}
546
547/// Find the pass that implements Analysis AID. Search immutable
548/// passes and all pass managers. If desired pass is not found
549/// then return NULL.
550Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
551
552 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000553 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000554 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000555 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000556 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000557 P = PMD->findAnalysisPass(AID, false);
558 }
559
560 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000561 for (SmallVector<PMDataManager *, 8>::iterator
562 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000563 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
564 P = (*I)->findAnalysisPass(AID, false);
565
Devang Patel0d29ae02008-08-12 15:44:31 +0000566 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000567 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
568 const PassInfo *PI = (*I)->getPassInfo();
569 if (PI == AID)
570 P = *I;
571
572 // If Pass not found then check the interfaces implemented by Immutable Pass
573 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000574 const std::vector<const PassInfo*> &ImmPI =
575 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000576 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
577 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000578 }
579 }
580
Devang Patelafb1f3622006-12-12 22:35:25 +0000581 return P;
582}
583
Devang Pateleda56172006-12-12 23:34:33 +0000584// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000585void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000586
Devang Patelfd4184322007-01-17 20:33:36 +0000587 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000588 return;
589
Devang Pateleda56172006-12-12 23:34:33 +0000590 // Print out the immutable passes
591 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
592 ImmutablePasses[i]->dumpPassStructure(0);
593 }
594
Dan Gohman73caf5f2008-03-13 01:48:32 +0000595 // Every class that derives from PMDataManager also derives from Pass
596 // (sometimes indirectly), but there's no inheritance relationship
597 // between PMDataManager and Pass, so we have to dynamic_cast to get
598 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000599 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000600 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000601 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000602}
603
Devang Patel991aeba2006-12-15 20:13:01 +0000604void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000605
Devang Patelfd4184322007-01-17 20:33:36 +0000606 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000607 return;
608
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000609 errs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000610 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000611 E = PassManagers.end(); I != E; ++I)
612 (*I)->dumpPassArguments();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000613 errs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000614}
615
Devang Patele3068402006-12-21 00:16:50 +0000616void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000617 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000618 E = PassManagers.end(); I != E; ++I)
619 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000620
621 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000622 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000623 E = IndirectPassManagers.end(); I != E; ++I)
624 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000625
Chris Lattner60987362009-03-06 05:53:14 +0000626 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000627 DME = LastUser.end(); DMI != DME; ++DMI) {
628 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
629 InversedLastUser.find(DMI->second);
630 if (InvDMI != InversedLastUser.end()) {
631 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
632 L.insert(DMI->first);
633 } else {
634 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
635 InversedLastUser[DMI->second] = L;
636 }
637 }
Devang Patele3068402006-12-21 00:16:50 +0000638}
639
Devang Patele7599552007-01-12 18:52:44 +0000640/// Destructor
641PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000642 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000643 E = PassManagers.end(); I != E; ++I)
644 delete *I;
645
Devang Patel0d29ae02008-08-12 15:44:31 +0000646 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000647 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
648 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000649
650 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000651 DME = AnUsageMap.end(); DMI != DME; ++DMI)
652 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000653}
654
Devang Patelafb1f3622006-12-12 22:35:25 +0000655//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000656// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000657
Devang Patel643676c2006-11-11 01:10:19 +0000658/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000659void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000660 const PassInfo *PI = P->getPassInfo();
661 if (PI == 0) return;
662
663 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000664
Chris Lattner60987362009-03-06 05:53:14 +0000665 //This pass is the current implementation of all of the interfaces it
666 //implements as well.
667 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
668 for (unsigned i = 0, e = II.size(); i != e; ++i)
669 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000670}
671
Devang Patel9d9fc902007-03-06 17:52:53 +0000672// Return true if P preserves high level analysis used by other
673// passes managed by this manager
674bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000675 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000676 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000677 return true;
678
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000679 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000680 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000681 E = HigherLevelAnalysis.end(); I != E; ++I) {
682 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000683 if (!dynamic_cast<ImmutablePass*>(P1) &&
684 std::find(PreservedSet.begin(), PreservedSet.end(),
685 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000686 PreservedSet.end())
687 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000688 }
689
690 return true;
691}
692
Chris Lattner02eb94c2008-08-07 07:34:50 +0000693/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000694void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000695 // Don't do this unless assertions are enabled.
696#ifdef NDEBUG
697 return;
698#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000699 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
700 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000701
Devang Patelef432532007-07-19 05:36:09 +0000702 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000703 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000704 E = PreservedSet.end(); I != E; ++I) {
705 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000706 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000707 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000708 }
Devang Patela273d1c2007-07-19 18:02:32 +0000709}
710
Devang Patel9dbe4d12008-07-01 17:44:24 +0000711/// verifyDomInfo - Verify dominator information if it is available.
712void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000713 if (!VerifyDomInfo || !P.getResolver())
714 return;
715
Duncan Sands5a913d62009-01-28 13:14:17 +0000716 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000717 if (!DT)
718 return;
719
720 DominatorTree OtherDT;
721 OtherDT.getBase().recalculate(F);
722 if (DT->compare(OtherDT)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000723 errs() << "Dominator Information for " << F.getName() << "\n";
724 errs() << "Pass '" << P.getPassName() << "'\n";
725 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000726 OtherDT.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000727 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000728 DT->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000729 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000730 }
731
Duncan Sands5a913d62009-01-28 13:14:17 +0000732 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000733 if (!DF)
734 return;
735
736 DominanceFrontier OtherDF;
737 std::vector<BasicBlock*> DTRoots = DT->getRoots();
738 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
739 if (DF->compare(OtherDF)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000740 errs() << "Dominator Information for " << F.getName() << "\n";
741 errs() << "Pass '" << P.getPassName() << "'\n";
742 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000743 OtherDF.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000744 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000745 DF->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000746 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000747 }
748}
749
Devang Patel67c79a42008-07-01 19:50:56 +0000750/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000751void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000752 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
753 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000754 return;
755
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000756 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000757 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000758 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000759 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000760 if (!dynamic_cast<ImmutablePass*>(Info->second)
761 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000762 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000763 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000764 if (PassDebugging >= Details) {
765 Pass *S = Info->second;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000766 errs() << " -- '" << P->getPassName() << "' is not preserving '";
767 errs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000768 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000769 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000770 }
Devang Patel349170f2006-11-11 01:24:55 +0000771 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000772
773 // Check inherited analysis also. If P is not preserving analysis
774 // provided by parent manager then remove it here.
775 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
776
777 if (!InheritedAnalysis[Index])
778 continue;
779
780 for (std::map<AnalysisID, Pass*>::iterator
781 I = InheritedAnalysis[Index]->begin(),
782 E = InheritedAnalysis[Index]->end(); I != E; ) {
783 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000784 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
785 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000786 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000787 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000788 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000789 }
790 }
Devang Patelf68a3492006-11-07 22:35:17 +0000791}
792
Devang Patelca189262006-11-14 03:05:08 +0000793/// Remove analysis passes that are not used any longer
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000794void PMDataManager::removeDeadPasses(Pass *P, const StringRef &Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000795 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000796
Devang Patel8adae862007-07-20 18:04:54 +0000797 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000798
Devang Patel2ff44922007-04-16 20:39:59 +0000799 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000800 if (!TPM)
801 return;
802
Devang Patel17ad0962006-12-08 00:37:52 +0000803 TPM->collectLastUses(DeadPasses, P);
804
Devang Patel656a9172008-06-06 17:50:36 +0000805 if (PassDebugging >= Details && !DeadPasses.empty()) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000806 errs() << " -*- '" << P->getPassName();
807 errs() << "' is the last user of following pass instances.";
808 errs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000809 }
810
Devang Patel8adae862007-07-20 18:04:54 +0000811 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000812 E = DeadPasses.end(); I != E; ++I)
813 freePass(*I, Msg, DBG_STR);
814}
Devang Patel200d3052006-12-13 23:50:44 +0000815
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000816void PMDataManager::freePass(Pass *P, const StringRef &Msg,
817 enum PassDebuggingString DBG_STR) {
818 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000819
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000820 {
821 // If the pass crashes releasing memory, remember this.
822 PassManagerPrettyStackEntry X(P);
823
Dan Gohman277e7672009-09-28 00:07:05 +0000824 Timer *T = StartPassTimer(P);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000825 P->releaseMemory();
Dan Gohman277e7672009-09-28 00:07:05 +0000826 StopPassTimer(P, T);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000827 }
828
829 if (const PassInfo *PI = P->getPassInfo()) {
830 // Remove the pass itself (if it is not already removed).
831 AvailableAnalysis.erase(PI);
832
833 // Remove all interfaces this pass implements, for which it is also
834 // listed as the available implementation.
835 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
836 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000837 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000838 AvailableAnalysis.find(II[i]);
839 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000840 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000841 }
Devang Patel17ad0962006-12-08 00:37:52 +0000842 }
Devang Patelca189262006-11-14 03:05:08 +0000843}
844
Devang Patel8f677ce2006-12-07 18:47:25 +0000845/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000846/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000847void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000848 // This manager is going to manage pass P. Set up analysis resolver
849 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000850 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000851 P->setResolver(AR);
852
Devang Patelec2b9a72007-03-05 22:57:49 +0000853 // If a FunctionPass F is the last user of ModulePass info M
854 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000855 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000856
Chris Lattner60987362009-03-06 05:53:14 +0000857 if (!ProcessAnalysis) {
858 // Add pass
859 PassVector.push_back(P);
860 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000861 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000862
Chris Lattner60987362009-03-06 05:53:14 +0000863 // At the moment, this pass is the last user of all required passes.
864 SmallVector<Pass *, 12> LastUses;
865 SmallVector<Pass *, 8> RequiredPasses;
866 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
867
868 unsigned PDepth = this->getDepth();
869
870 collectRequiredAnalysis(RequiredPasses,
871 ReqAnalysisNotAvailable, P);
872 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
873 E = RequiredPasses.end(); I != E; ++I) {
874 Pass *PRequired = *I;
875 unsigned RDepth = 0;
876
877 assert(PRequired->getResolver() && "Analysis Resolver is not set");
878 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
879 RDepth = DM.getDepth();
880
881 if (PDepth == RDepth)
882 LastUses.push_back(PRequired);
883 else if (PDepth > RDepth) {
884 // Let the parent claim responsibility of last use
885 TransferLastUses.push_back(PRequired);
886 // Keep track of higher level analysis used by this manager.
887 HigherLevelAnalysis.push_back(PRequired);
888 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000889 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000890 }
891
892 // Set P as P's last user until someone starts using P.
893 // However, if P is a Pass Manager then it does not need
894 // to record its last user.
895 if (!dynamic_cast<PMDataManager *>(P))
896 LastUses.push_back(P);
897 TPM->setLastUser(LastUses, P);
898
899 if (!TransferLastUses.empty()) {
900 Pass *My_PM = dynamic_cast<Pass *>(this);
901 TPM->setLastUser(TransferLastUses, My_PM);
902 TransferLastUses.clear();
903 }
904
905 // Now, take care of required analysises that are not available.
906 for (SmallVector<AnalysisID, 8>::iterator
907 I = ReqAnalysisNotAvailable.begin(),
908 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
909 Pass *AnalysisPass = (*I)->createPass();
910 this->addLowerLevelRequiredPass(P, AnalysisPass);
911 }
912
913 // Take a note of analysis required and made available by this pass.
914 // Remove the analysis not preserved by this pass
915 removeNotPreservedAnalysis(P);
916 recordAvailableAnalysis(P);
917
Devang Patel8cad70d2006-11-11 01:51:02 +0000918 // Add pass
919 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000920}
921
Devang Patele64d3052007-04-16 20:12:57 +0000922
923/// Populate RP with analysis pass that are required by
924/// pass P and are available. Populate RP_NotAvail with analysis
925/// pass that are required by pass P but are not available.
926void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
927 SmallVector<AnalysisID, 8> &RP_NotAvail,
928 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000929 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
930 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000931 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000932 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000933 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
934 RP.push_back(AnalysisPass);
935 else
Chris Lattner60987362009-03-06 05:53:14 +0000936 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000937 }
Devang Patelf58183d2006-12-12 23:09:32 +0000938
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000939 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000940 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000941 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000942 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
943 RP.push_back(AnalysisPass);
944 else
Chris Lattner60987362009-03-06 05:53:14 +0000945 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000946 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000947}
948
Devang Patel07f4f582006-11-14 21:49:36 +0000949// All Required analyses should be available to the pass as it runs! Here
950// we fill in the AnalysisImpls member of the pass so that it can
951// successfully use the getAnalysis() method to retrieve the
952// implementations it needs.
953//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000954void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000955 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
956
Chris Lattnercbd160f2008-08-08 05:33:04 +0000957 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000958 I = AnUsage->getRequiredSet().begin(),
959 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000960 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000961 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000962 // This may be analysis pass that is initialized on the fly.
963 // If that is not the case then it will raise an assert when it is used.
964 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000965 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000966 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000967 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000968 }
969}
970
Devang Patel640c5bb2006-12-08 22:30:11 +0000971/// Find the pass that implements Analysis AID. If desired pass is not found
972/// then return NULL.
973Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
974
975 // Check if AvailableAnalysis map has one entry.
976 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
977
978 if (I != AvailableAnalysis.end())
979 return I->second;
980
981 // Search Parents through TopLevelManager
982 if (SearchParent)
983 return TPM->findAnalysisPass(AID);
984
Devang Patel9d759b82006-12-09 00:09:12 +0000985 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000986}
987
Devang Patel991aeba2006-12-15 20:13:01 +0000988// Print list of passes that are last used by P.
989void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
990
Devang Patel8adae862007-07-20 18:04:54 +0000991 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000992
993 // If this is a on the fly manager then it does not have TPM.
994 if (!TPM)
995 return;
996
Devang Patel991aeba2006-12-15 20:13:01 +0000997 TPM->collectLastUses(LUses, P);
998
Devang Patel8adae862007-07-20 18:04:54 +0000999 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001000 E = LUses.end(); I != E; ++I) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001001 llvm::errs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +00001002 (*I)->dumpPassStructure(0);
1003 }
1004}
1005
1006void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001007 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001008 E = PassVector.end(); I != E; ++I) {
1009 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1010 PMD->dumpPassArguments();
1011 else
1012 if (const PassInfo *PI = (*I)->getPassInfo())
1013 if (!PI->isAnalysisGroup())
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001014 errs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001015 }
1016}
1017
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001018void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1019 enum PassDebuggingString S2,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001020 const StringRef &Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001021 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001022 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001023 errs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001024 switch (S1) {
1025 case EXECUTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001026 errs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001027 break;
1028 case MODIFICATION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001029 errs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001030 break;
1031 case FREEING_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001032 errs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001033 break;
1034 default:
1035 break;
1036 }
1037 switch (S2) {
1038 case ON_BASICBLOCK_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001039 errs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001040 break;
1041 case ON_FUNCTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001042 errs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001043 break;
1044 case ON_MODULE_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001045 errs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001046 break;
1047 case ON_LOOP_MSG:
Chris Lattnerf82f27b2009-09-15 04:45:26 +00001048 errs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001049 break;
1050 case ON_CG_MSG:
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001051 errs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001052 break;
1053 default:
1054 break;
1055 }
Devang Patel991aeba2006-12-15 20:13:01 +00001056}
1057
Chris Lattner4c1e9542009-03-06 06:45:05 +00001058void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001059 if (PassDebugging < Details)
1060 return;
1061
1062 AnalysisUsage analysisUsage;
1063 P->getAnalysisUsage(analysisUsage);
1064 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1065}
1066
Chris Lattner4c1e9542009-03-06 06:45:05 +00001067void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001068 if (PassDebugging < Details)
1069 return;
1070
1071 AnalysisUsage analysisUsage;
1072 P->getAnalysisUsage(analysisUsage);
1073 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1074}
1075
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001076void PMDataManager::dumpAnalysisUsage(const StringRef &Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001077 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001078 assert(PassDebugging >= Details);
1079 if (Set.empty())
1080 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001081 errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001082 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001083 if (i) errs() << ',';
1084 errs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001085 }
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001086 errs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001087}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001088
Devang Patel004937b2007-07-27 20:06:09 +00001089/// Add RequiredPass into list of lower level passes required by pass P.
1090/// RequiredPass is run on the fly by Pass Manager when P requests it
1091/// through getAnalysis interface.
1092/// This should be handled by specific pass manager.
1093void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1094 if (TPM) {
1095 TPM->dumpArguments();
1096 TPM->dumpPasses();
1097 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001098
1099 // Module Level pass may required Function Level analysis info
1100 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1101 // to provide this on demand. In that case, in Pass manager terminology,
1102 // module level pass is requiring lower level analysis info managed by
1103 // lower level pass manager.
1104
1105 // When Pass manager is not able to order required analysis info, Pass manager
1106 // checks whether any lower level manager will be able to provide this
1107 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001108#ifndef NDEBUG
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001109 errs() << "Unable to schedule '" << RequiredPass->getPassName();
1110 errs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001111#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001112 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001113}
1114
Devang Patele7599552007-01-12 18:52:44 +00001115// Destructor
1116PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001117 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001118 E = PassVector.end(); I != E; ++I)
1119 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001120}
1121
Devang Patel9bdf7d42006-12-08 23:28:54 +00001122//===----------------------------------------------------------------------===//
1123// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001124// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1125Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001126 return PM.findAnalysisPass(ID, dir);
1127}
1128
Devang Patel92942812007-04-16 20:56:24 +00001129Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1130 Function &F) {
1131 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1132}
1133
Devang Patela1514cb2006-12-07 19:39:39 +00001134//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001135// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001136
Devang Patel6e5a1132006-11-07 21:31:57 +00001137/// Execute all of the passes scheduled for execution by invoking
1138/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1139/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001140bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001141 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001142 return false;
1143
Devang Patele9585592006-12-08 01:38:28 +00001144 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001145
Devang Patel6e5a1132006-11-07 21:31:57 +00001146 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001147 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1148 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001149
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001150 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001151 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001152
Devang Patelabfbe3b2006-12-16 00:56:26 +00001153 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001154
Chris Lattner4c1e9542009-03-06 06:45:05 +00001155 {
1156 // If the pass crashes, remember this.
1157 PassManagerPrettyStackEntry X(BP, *I);
1158
Dan Gohman277e7672009-09-28 00:07:05 +00001159 Timer *T = StartPassTimer(BP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001160 Changed |= BP->runOnBasicBlock(*I);
Dan Gohman277e7672009-09-28 00:07:05 +00001161 StopPassTimer(BP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001162 }
Devang Patel93a197c2006-12-14 00:08:04 +00001163
Devang Patel003a5592007-03-05 20:01:30 +00001164 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001165 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001166 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001167 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001168
Devang Patela273d1c2007-07-19 18:02:32 +00001169 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001170 removeNotPreservedAnalysis(BP);
1171 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001172 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001173 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001174
Devang Patel56d48ec2006-12-15 22:57:49 +00001175 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001176}
1177
Devang Patel475c4532006-12-08 00:59:05 +00001178// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001179bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001180 bool Changed = false;
1181
Chris Lattner4c1e9542009-03-06 06:45:05 +00001182 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1183 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001184
1185 return Changed;
1186}
1187
Duncan Sands51495602009-02-13 09:42:34 +00001188bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001189 bool Changed = false;
1190
Chris Lattner4c1e9542009-03-06 06:45:05 +00001191 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1192 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001193
1194 return Changed;
1195}
1196
Duncan Sands51495602009-02-13 09:42:34 +00001197bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001198 bool Changed = false;
1199
Devang Patelabfbe3b2006-12-16 00:56:26 +00001200 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1201 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001202 Changed |= BP->doInitialization(F);
1203 }
1204
1205 return Changed;
1206}
1207
Duncan Sands51495602009-02-13 09:42:34 +00001208bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001209 bool Changed = false;
1210
Devang Patelabfbe3b2006-12-16 00:56:26 +00001211 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1212 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001213 Changed |= BP->doFinalization(F);
1214 }
1215
1216 return Changed;
1217}
1218
1219
Devang Patela1514cb2006-12-07 19:39:39 +00001220//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001221// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001222
Devang Patel4e12f862006-11-08 10:44:40 +00001223/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001224FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001225 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001226 // FPM is the top level manager.
1227 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001228
Dan Gohman565df952008-03-13 02:08:36 +00001229 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001230 FPM->setResolver(AR);
1231
Devang Patel1f653682006-12-08 18:57:16 +00001232 MP = P;
1233}
1234
Devang Patelb67904d2006-12-13 02:36:01 +00001235FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001236 delete FPM;
1237}
1238
Devang Patel4e12f862006-11-08 10:44:40 +00001239/// add - Add a pass to the queue of passes to run. This passes
1240/// ownership of the Pass to the PassManager. When the
1241/// PassManager_X is destroyed, the pass will be destroyed as well, so
1242/// there is no need to delete the pass. (TODO delete passes.)
1243/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001244void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001245 FPM->add(P);
1246}
1247
Devang Patel9f3083e2006-11-15 19:39:54 +00001248/// run - Execute all of the passes scheduled for execution. Keep
1249/// track of whether any of the passes modifies the function, and if
1250/// so, return true.
1251///
Devang Patelb67904d2006-12-13 02:36:01 +00001252bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001253 std::string errstr;
1254 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001255 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001256 }
Devang Patel272908d2006-12-08 22:57:48 +00001257 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001258}
1259
1260
Devang Patelff631ae2006-11-15 01:27:05 +00001261/// doInitialization - Run all of the initializers for the function passes.
1262///
Devang Patelb67904d2006-12-13 02:36:01 +00001263bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001264 return FPM->doInitialization(*MP->getModule());
1265}
1266
Dan Gohmane6656eb2007-07-30 14:51:13 +00001267/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001268///
Devang Patelb67904d2006-12-13 02:36:01 +00001269bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001270 return FPM->doFinalization(*MP->getModule());
1271}
1272
Devang Patela1514cb2006-12-07 19:39:39 +00001273//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001274// FunctionPassManagerImpl implementation
1275//
Duncan Sands51495602009-02-13 09:42:34 +00001276bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001277 bool Changed = false;
1278
Chris Lattner4c1e9542009-03-06 06:45:05 +00001279 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1280 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001281
1282 return Changed;
1283}
1284
Duncan Sands51495602009-02-13 09:42:34 +00001285bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001286 bool Changed = false;
1287
Chris Lattner4c1e9542009-03-06 06:45:05 +00001288 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1289 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001290
1291 return Changed;
1292}
1293
Devang Patelec9c58f2009-04-01 22:34:41 +00001294/// cleanup - After running all passes, clean up pass manager cache.
1295void FPPassManager::cleanup() {
1296 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1297 FunctionPass *FP = getContainedPass(Index);
1298 AnalysisResolver *AR = FP->getResolver();
1299 assert(AR && "Analysis Resolver is not set");
1300 AR->clearAnalysisImpls();
1301 }
1302}
1303
Torok Edwin24c78352009-06-29 18:49:09 +00001304void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1305 if (!wasRun)
1306 return;
1307 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1308 FPPassManager *FPPM = getContainedManager(Index);
1309 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1310 FPPM->getContainedPass(Index)->releaseMemory();
1311 }
1312 }
Torok Edwin896556e2009-06-29 21:05:10 +00001313 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001314}
1315
Devang Patel67d6a5e2006-12-19 19:46:59 +00001316// Execute all the passes managed by this top level manager.
1317// Return true if any function is modified by a pass.
1318bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001319 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001320 TimingInfo::createTheTimeInfo();
1321
1322 dumpArguments();
1323 dumpPasses();
1324
Devang Patele3068402006-12-21 00:16:50 +00001325 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001326 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1327 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001328
1329 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1330 getContainedManager(Index)->cleanup();
1331
Torok Edwin24c78352009-06-29 18:49:09 +00001332 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001333 return Changed;
1334}
1335
1336//===----------------------------------------------------------------------===//
1337// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001338
Devang Patel8c78a0b2007-05-03 01:11:54 +00001339char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001340/// Print passes managed by this manager
1341void FPPassManager::dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001342 llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001343 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1344 FunctionPass *FP = getContainedPass(Index);
1345 FP->dumpPassStructure(Offset + 1);
1346 dumpLastUses(FP, Offset+1);
1347 }
1348}
1349
1350
Devang Patel0c2012f2006-11-07 21:49:50 +00001351/// Execute all of the passes scheduled for execution by invoking
1352/// runOnFunction method. Keep track of whether any of the passes modifies
1353/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001354bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001355 if (F.isDeclaration())
1356 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001357
1358 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001359
Devang Patelcbbf2912008-03-20 01:09:53 +00001360 // Collect inherited analysis from Module level pass manager.
1361 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001362
Devang Patelabfbe3b2006-12-16 00:56:26 +00001363 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1364 FunctionPass *FP = getContainedPass(Index);
1365
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001366 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001367 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001368
Devang Patelabfbe3b2006-12-16 00:56:26 +00001369 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001370
Chris Lattner4c1e9542009-03-06 06:45:05 +00001371 {
1372 PassManagerPrettyStackEntry X(FP, F);
1373
Dan Gohman277e7672009-09-28 00:07:05 +00001374 Timer *T = StartPassTimer(FP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001375 Changed |= FP->runOnFunction(F);
Dan Gohman277e7672009-09-28 00:07:05 +00001376 StopPassTimer(FP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001377 }
Devang Patel93a197c2006-12-14 00:08:04 +00001378
Devang Patel003a5592007-03-05 20:01:30 +00001379 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001380 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001381 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001382
Devang Patela273d1c2007-07-19 18:02:32 +00001383 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001384 removeNotPreservedAnalysis(FP);
1385 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001386 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001387
Devang Patel67c79a42008-07-01 19:50:56 +00001388 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001389 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001390 }
1391 return Changed;
1392}
1393
Devang Patel67d6a5e2006-12-19 19:46:59 +00001394bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001395 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001396
Chris Lattner60987362009-03-06 05:53:14 +00001397 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1398 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001399
1400 return Changed |= doFinalization(M);
1401}
1402
Duncan Sands51495602009-02-13 09:42:34 +00001403bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001404 bool Changed = false;
1405
Chris Lattner4c1e9542009-03-06 06:45:05 +00001406 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1407 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001408
1409 return Changed;
1410}
1411
Duncan Sands51495602009-02-13 09:42:34 +00001412bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001413 bool Changed = false;
1414
Chris Lattner4c1e9542009-03-06 06:45:05 +00001415 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1416 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001417
Devang Patelff631ae2006-11-15 01:27:05 +00001418 return Changed;
1419}
1420
Devang Patela1514cb2006-12-07 19:39:39 +00001421//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001423
Devang Patel05e1a972006-11-07 22:03:15 +00001424/// Execute all of the passes scheduled for execution by invoking
1425/// runOnModule method. Keep track of whether any of the passes modifies
1426/// the module, and if so, return true.
1427bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001429 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001430
Torok Edwin24c78352009-06-29 18:49:09 +00001431 // Initialize on-the-fly passes
1432 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1433 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1434 I != E; ++I) {
1435 FunctionPassManagerImpl *FPP = I->second;
1436 Changed |= FPP->doInitialization(M);
1437 }
1438
Devang Patelabfbe3b2006-12-16 00:56:26 +00001439 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1440 ModulePass *MP = getContainedPass(Index);
1441
Dan Gohman929391a2008-01-29 12:09:55 +00001442 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1443 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001444 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001445
Devang Patelabfbe3b2006-12-16 00:56:26 +00001446 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001447
Chris Lattner4c1e9542009-03-06 06:45:05 +00001448 {
1449 PassManagerPrettyStackEntry X(MP, M);
Dan Gohman277e7672009-09-28 00:07:05 +00001450 Timer *T = StartPassTimer(MP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001451 Changed |= MP->runOnModule(M);
Dan Gohman277e7672009-09-28 00:07:05 +00001452 StopPassTimer(MP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001453 }
Devang Patel93a197c2006-12-14 00:08:04 +00001454
Devang Patel003a5592007-03-05 20:01:30 +00001455 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001456 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1457 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001458 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001459
Devang Patela273d1c2007-07-19 18:02:32 +00001460 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001461 removeNotPreservedAnalysis(MP);
1462 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001463 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001464 }
Torok Edwin24c78352009-06-29 18:49:09 +00001465
1466 // Finalize on-the-fly passes
1467 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1468 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1469 I != E; ++I) {
1470 FunctionPassManagerImpl *FPP = I->second;
1471 // We don't know when is the last time an on-the-fly pass is run,
1472 // so we need to releaseMemory / finalize here
1473 FPP->releaseMemoryOnTheFly();
1474 Changed |= FPP->doFinalization(M);
1475 }
Devang Patel05e1a972006-11-07 22:03:15 +00001476 return Changed;
1477}
1478
Devang Patele64d3052007-04-16 20:12:57 +00001479/// Add RequiredPass into list of lower level passes required by pass P.
1480/// RequiredPass is run on the fly by Pass Manager when P requests it
1481/// through getAnalysis interface.
1482void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001483 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1484 "Unable to handle Pass that requires lower level Analysis pass");
1485 assert((P->getPotentialPassManagerType() <
1486 RequiredPass->getPotentialPassManagerType()) &&
1487 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001488
Devang Patel68f72b12007-04-26 17:50:19 +00001489 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001490 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001491 FPP = new FunctionPassManagerImpl(0);
1492 // FPP is the top level manager.
1493 FPP->setTopLevelManager(FPP);
1494
Devang Patel69e9f6d2007-04-16 20:27:05 +00001495 OnTheFlyManagers[P] = FPP;
1496 }
Devang Patel68f72b12007-04-26 17:50:19 +00001497 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001498
Devang Patel68f72b12007-04-26 17:50:19 +00001499 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001500 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001501 LU.push_back(RequiredPass);
1502 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001503}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001504
1505/// Return function pass corresponding to PassInfo PI, that is
1506/// required by module pass MP. Instantiate analysis pass, by using
1507/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001508Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001509 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001510 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001511
Torok Edwin24c78352009-06-29 18:49:09 +00001512 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001513 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001514 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001515}
1516
1517
Devang Patela1514cb2006-12-07 19:39:39 +00001518//===----------------------------------------------------------------------===//
1519// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001520//
Devang Patelc290c8a2006-11-07 22:23:34 +00001521/// run - Execute all of the passes scheduled for execution. Keep track of
1522/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001523bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001524 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001525 TimingInfo::createTheTimeInfo();
1526
Devang Patelcfd70c42006-12-13 22:10:00 +00001527 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001529
Devang Patele3068402006-12-21 00:16:50 +00001530 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001531 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1532 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001533 return Changed;
1534}
Devang Patel376fefa2006-11-08 10:29:57 +00001535
Devang Patela1514cb2006-12-07 19:39:39 +00001536//===----------------------------------------------------------------------===//
1537// PassManager implementation
1538
Devang Patel376fefa2006-11-08 10:29:57 +00001539/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001540PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001541 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001542 // PM is the top level manager
1543 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001544}
1545
Devang Patelb67904d2006-12-13 02:36:01 +00001546PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001547 delete PM;
1548}
1549
Devang Patel376fefa2006-11-08 10:29:57 +00001550/// add - Add a pass to the queue of passes to run. This passes ownership of
1551/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1552/// will be destroyed as well, so there is no need to delete the pass. This
1553/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001554void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001555 PM->add(P);
1556}
1557
1558/// run - Execute all of the passes scheduled for execution. Keep track of
1559/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001560bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001561 return PM->run(M);
1562}
1563
Devang Patelb8817b92006-12-14 00:59:42 +00001564//===----------------------------------------------------------------------===//
1565// TimingInfo Class - This class is used to calculate information about the
1566// amount of time each pass takes to execute. This only happens with
1567// -time-passes is enabled on the command line.
1568//
1569bool llvm::TimePassesIsEnabled = false;
1570static cl::opt<bool,true>
1571EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1572 cl::desc("Time each pass, printing elapsed time for each on exit"));
1573
1574// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1575// a non null value (if the -time-passes option is enabled) or it leaves it
1576// null. It may be called multiple times.
1577void TimingInfo::createTheTimeInfo() {
1578 if (!TimePassesIsEnabled || TheTimeInfo) return;
1579
1580 // Constructed the first time this is called, iff -time-passes is enabled.
1581 // This guarantees that the object will be constructed before static globals,
1582 // thus it will be destroyed before them.
1583 static ManagedStatic<TimingInfo> TTI;
1584 TheTimeInfo = &*TTI;
1585}
1586
Devang Patel1c3633e2007-01-29 23:10:37 +00001587/// If TimingInfo is enabled then start pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001588Timer *llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001589 if (TheTimeInfo)
Dan Gohman277e7672009-09-28 00:07:05 +00001590 return TheTimeInfo->passStarted(P);
1591 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001592}
1593
1594/// If TimingInfo is enabled then stop pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001595void llvm::StopPassTimer(Pass *P, Timer *T) {
1596 if (T) T->stopTimer();
Devang Patel1c3633e2007-01-29 23:10:37 +00001597}
1598
Devang Patel1c56a632007-01-08 19:29:38 +00001599//===----------------------------------------------------------------------===//
1600// PMStack implementation
1601//
Devang Patelad98d232007-01-11 22:15:30 +00001602
Devang Patel1c56a632007-01-08 19:29:38 +00001603// Pop Pass Manager from the stack and clear its analysis info.
1604void PMStack::pop() {
1605
1606 PMDataManager *Top = this->top();
1607 Top->initializeAnalysisInfo();
1608
1609 S.pop_back();
1610}
1611
1612// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001613void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001614 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001615
Chris Lattner60987362009-03-06 05:53:14 +00001616 if (!this->empty()) {
1617 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001618
Chris Lattner60987362009-03-06 05:53:14 +00001619 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001620 TPM->addIndirectPassManager(PM);
1621 PM->setTopLevelManager(TPM);
1622 }
1623
Devang Patel15701b52007-01-11 00:19:00 +00001624 S.push_back(PM);
1625}
1626
1627// Dump content of the pass manager stack.
1628void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001629 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1630 E = S.end(); I != E; ++I)
1631 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1632
Devang Patel15701b52007-01-11 00:19:00 +00001633 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001634 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001635}
1636
Devang Patel1c56a632007-01-08 19:29:38 +00001637/// Find appropriate Module Pass Manager in the PM Stack and
1638/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001639void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001640 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001641 // Find Module Pass Manager
1642 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001643 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1644 if (TopPMType == PreferredType)
1645 break; // We found desired pass manager
1646 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001647 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001648 else
1649 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001650 }
Devang Patel18ff6362008-09-09 21:38:40 +00001651 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001652 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001653}
1654
Devang Patel3312f752007-01-16 21:43:18 +00001655/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1656/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001657void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001658 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001659
Devang Patela3286902008-09-09 17:56:50 +00001660 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001661 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001662 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1663 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001664 else
Devang Patel3312f752007-01-16 21:43:18 +00001665 break;
1666 }
1667 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1668
1669 // Create new Function Pass Manager
1670 if (!FPP) {
1671 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1672 PMDataManager *PMD = PMS.top();
1673
1674 // [1] Create new Function Pass Manager
1675 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001676 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001677
1678 // [2] Set up new manager's top level manager
1679 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1680 TPM->addIndirectPassManager(FPP);
1681
1682 // [3] Assign manager to manage this new manager. This may create
1683 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001684 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001685
1686 // [4] Push new manager into PMS
1687 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001688 }
1689
Devang Patel3312f752007-01-16 21:43:18 +00001690 // Assign FPP as the manager of this pass.
1691 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001692}
1693
Devang Patel3312f752007-01-16 21:43:18 +00001694/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001695/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001696void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001697 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001698 BBPassManager *BBP = NULL;
1699
Devang Patel15701b52007-01-11 00:19:00 +00001700 // Basic Pass Manager is a leaf pass manager. It does not handle
1701 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001702 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001703 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001704
Devang Patel3312f752007-01-16 21:43:18 +00001705 // If leaf manager is not Basic Block Pass manager then create new
1706 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001707
Devang Patel3312f752007-01-16 21:43:18 +00001708 if (!BBP) {
1709 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1710 PMDataManager *PMD = PMS.top();
1711
1712 // [1] Create new Basic Block Manager
1713 BBP = new BBPassManager(PMD->getDepth() + 1);
1714
1715 // [2] Set up new manager's top level manager
1716 // Basic Block Pass Manager does not live by itself
1717 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1718 TPM->addIndirectPassManager(BBP);
1719
Devang Patel15701b52007-01-11 00:19:00 +00001720 // [3] Assign manager to manage this new manager. This may create
1721 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001722 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001723
Devang Patel3312f752007-01-16 21:43:18 +00001724 // [4] Push new manager into PMS
1725 PMS.push(BBP);
1726 }
Devang Patel1c56a632007-01-08 19:29:38 +00001727
Devang Patel3312f752007-01-16 21:43:18 +00001728 // Assign BBP as the manager of this pass.
1729 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001730}
1731
Dan Gohmand3a20c92008-03-11 16:41:42 +00001732PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001733
1734/*===-- C Bindings --------------------------------------------------------===*/
1735
1736LLVMPassManagerRef LLVMCreatePassManager() {
1737 return wrap(new PassManager());
1738}
1739
1740LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1741 return wrap(new FunctionPassManager(unwrap(P)));
1742}
1743
1744int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1745 return unwrap<PassManager>(PM)->run(*unwrap(M));
1746}
1747
1748int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1749 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1750}
1751
1752int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1753 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1754}
1755
1756int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1757 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1758}
1759
1760void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1761 delete unwrap(PM);
1762}