blob: 79c30aa480c86c1133cb738e89cd237877a261e7 [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
400 void passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000401 if (dynamic_cast<PMDataManager *>(P))
402 return;
403
Owen Anderson5c96ef72009-07-07 18:33:04 +0000404 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000405 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
406 if (I == TimingData.end())
407 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
408 I->second.startTimer();
409 }
Owen Anderson5a6960f2009-06-18 20:51:00 +0000410
Devang Patel1c3633e2007-01-29 23:10:37 +0000411 void passEnded(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000412 if (dynamic_cast<PMDataManager *>(P))
413 return;
414
Owen Anderson5c96ef72009-07-07 18:33:04 +0000415 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000416 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
Chris Lattner60987362009-03-06 05:53:14 +0000417 assert(I != TimingData.end() && "passStarted/passEnded not nested right!");
Devang Patel1c3633e2007-01-29 23:10:37 +0000418 I->second.stopTimer();
419 }
420};
421
Devang Patel1c3633e2007-01-29 23:10:37 +0000422} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000423
Dan Gohmand78c4002008-05-13 00:00:25 +0000424static TimingInfo *TheTimeInfo;
425
Devang Patela1514cb2006-12-07 19:39:39 +0000426//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000427// PMTopLevelManager implementation
428
Devang Patel4268fc02007-01-16 02:00:38 +0000429/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000430PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000431 if (t == TLM_Pass) {
432 MPPassManager *MPP = new MPPassManager(1);
433 MPP->setTopLevelManager(this);
434 addPassManager(MPP);
435 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000436 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000437 FPPassManager *FPP = new FPPassManager(1);
438 FPP->setTopLevelManager(this);
439 addPassManager(FPP);
440 activeStack.push(FPP);
441 }
442}
443
Devang Patelafb1f3622006-12-12 22:35:25 +0000444/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000445void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000446 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000447 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000448 E = AnalysisPasses.end(); I != E; ++I) {
449 Pass *AP = *I;
450 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000451
452 if (P == AP)
453 continue;
454
Devang Patelafb1f3622006-12-12 22:35:25 +0000455 // If AP is the last user of other passes then make P last user of
456 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000457 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000458 LUE = LastUser.end(); LUI != LUE; ++LUI) {
459 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000460 // DenseMap iterator is not invalidated here because
461 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000462 LastUser[LUI->first] = P;
463 }
464 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000465}
466
467/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000468void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000469 Pass *P) {
470 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
471 InversedLastUser.find(P);
472 if (DMI == InversedLastUser.end())
473 return;
474
475 SmallPtrSet<Pass *, 8> &LU = DMI->second;
476 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
477 E = LU.end(); I != E; ++I) {
478 LastUses.push_back(*I);
479 }
480
Devang Patelafb1f3622006-12-12 22:35:25 +0000481}
482
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000483AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
484 AnalysisUsage *AnUsage = NULL;
485 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
486 if (DMI != AnUsageMap.end())
487 AnUsage = DMI->second;
488 else {
489 AnUsage = new AnalysisUsage();
490 P->getAnalysisUsage(*AnUsage);
491 AnUsageMap[P] = AnUsage;
492 }
493 return AnUsage;
494}
495
Devang Patelafb1f3622006-12-12 22:35:25 +0000496/// Schedule pass P for execution. Make sure that passes required by
497/// P are run before P is run. Update analysis info maintained by
498/// the manager. Remove dead passes. This is a recursive function.
499void PMTopLevelManager::schedulePass(Pass *P) {
500
Devang Patel3312f752007-01-16 21:43:18 +0000501 // TODO : Allocate function manager for this pass, other wise required set
502 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000503
Devang Pateld74ede72007-03-06 01:06:16 +0000504 // Give pass a chance to prepare the stage.
505 P->preparePassManager(activeStack);
506
Devang Patel864970e2008-03-18 00:39:19 +0000507 // If P is an analysis pass and it is available then do not
508 // generate the analysis again. Stale analysis info should not be
509 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000510 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000511 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
512 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000513 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000514 }
Devang Patel864970e2008-03-18 00:39:19 +0000515
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000516 AnalysisUsage *AnUsage = findAnalysisUsage(P);
517
Devang Patelfdee7032008-08-14 23:07:48 +0000518 bool checkAnalysis = true;
519 while (checkAnalysis) {
520 checkAnalysis = false;
521
522 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
523 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
524 E = RequiredSet.end(); I != E; ++I) {
525
526 Pass *AnalysisPass = findAnalysisPass(*I);
527 if (!AnalysisPass) {
528 AnalysisPass = (*I)->createPass();
529 if (P->getPotentialPassManagerType () ==
530 AnalysisPass->getPotentialPassManagerType())
531 // Schedule analysis pass that is managed by the same pass manager.
532 schedulePass(AnalysisPass);
533 else if (P->getPotentialPassManagerType () >
534 AnalysisPass->getPotentialPassManagerType()) {
535 // Schedule analysis pass that is managed by a new manager.
536 schedulePass(AnalysisPass);
537 // Recheck analysis passes to ensure that required analysises that
538 // are already checked are still available.
539 checkAnalysis = true;
540 }
541 else
542 // Do not schedule this analysis. Lower level analsyis
543 // passes are run on the fly.
544 delete AnalysisPass;
545 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000546 }
547 }
548
549 // Now all required passes are available.
550 addTopLevelPass(P);
551}
552
553/// Find the pass that implements Analysis AID. Search immutable
554/// passes and all pass managers. If desired pass is not found
555/// then return NULL.
556Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
557
558 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000559 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000560 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000561 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000562 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000563 P = PMD->findAnalysisPass(AID, false);
564 }
565
566 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000567 for (SmallVector<PMDataManager *, 8>::iterator
568 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000569 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
570 P = (*I)->findAnalysisPass(AID, false);
571
Devang Patel0d29ae02008-08-12 15:44:31 +0000572 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000573 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
574 const PassInfo *PI = (*I)->getPassInfo();
575 if (PI == AID)
576 P = *I;
577
578 // If Pass not found then check the interfaces implemented by Immutable Pass
579 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000580 const std::vector<const PassInfo*> &ImmPI =
581 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000582 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
583 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000584 }
585 }
586
Devang Patelafb1f3622006-12-12 22:35:25 +0000587 return P;
588}
589
Devang Pateleda56172006-12-12 23:34:33 +0000590// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000591void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000592
Devang Patelfd4184322007-01-17 20:33:36 +0000593 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000594 return;
595
Devang Pateleda56172006-12-12 23:34:33 +0000596 // Print out the immutable passes
597 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
598 ImmutablePasses[i]->dumpPassStructure(0);
599 }
600
Dan Gohman73caf5f2008-03-13 01:48:32 +0000601 // Every class that derives from PMDataManager also derives from Pass
602 // (sometimes indirectly), but there's no inheritance relationship
603 // between PMDataManager and Pass, so we have to dynamic_cast to get
604 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000605 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000606 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000607 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000608}
609
Devang Patel991aeba2006-12-15 20:13:01 +0000610void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000611
Devang Patelfd4184322007-01-17 20:33:36 +0000612 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000613 return;
614
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000615 errs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000616 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000617 E = PassManagers.end(); I != E; ++I)
618 (*I)->dumpPassArguments();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000619 errs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000620}
621
Devang Patele3068402006-12-21 00:16:50 +0000622void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000623 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000624 E = PassManagers.end(); I != E; ++I)
625 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000626
627 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000628 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000629 E = IndirectPassManagers.end(); I != E; ++I)
630 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000631
Chris Lattner60987362009-03-06 05:53:14 +0000632 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000633 DME = LastUser.end(); DMI != DME; ++DMI) {
634 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
635 InversedLastUser.find(DMI->second);
636 if (InvDMI != InversedLastUser.end()) {
637 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
638 L.insert(DMI->first);
639 } else {
640 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
641 InversedLastUser[DMI->second] = L;
642 }
643 }
Devang Patele3068402006-12-21 00:16:50 +0000644}
645
Devang Patele7599552007-01-12 18:52:44 +0000646/// Destructor
647PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000648 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000649 E = PassManagers.end(); I != E; ++I)
650 delete *I;
651
Devang Patel0d29ae02008-08-12 15:44:31 +0000652 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000653 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
654 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000655
656 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000657 DME = AnUsageMap.end(); DMI != DME; ++DMI)
658 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000659}
660
Devang Patelafb1f3622006-12-12 22:35:25 +0000661//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000662// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000663
Devang Patel643676c2006-11-11 01:10:19 +0000664/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000665void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000666 const PassInfo *PI = P->getPassInfo();
667 if (PI == 0) return;
668
669 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000670
Chris Lattner60987362009-03-06 05:53:14 +0000671 //This pass is the current implementation of all of the interfaces it
672 //implements as well.
673 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
674 for (unsigned i = 0, e = II.size(); i != e; ++i)
675 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000676}
677
Devang Patel9d9fc902007-03-06 17:52:53 +0000678// Return true if P preserves high level analysis used by other
679// passes managed by this manager
680bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000681 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000682 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000683 return true;
684
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000685 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000686 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000687 E = HigherLevelAnalysis.end(); I != E; ++I) {
688 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000689 if (!dynamic_cast<ImmutablePass*>(P1) &&
690 std::find(PreservedSet.begin(), PreservedSet.end(),
691 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000692 PreservedSet.end())
693 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000694 }
695
696 return true;
697}
698
Chris Lattner02eb94c2008-08-07 07:34:50 +0000699/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000700void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000701 // Don't do this unless assertions are enabled.
702#ifdef NDEBUG
703 return;
704#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000705 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
706 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000707
Devang Patelef432532007-07-19 05:36:09 +0000708 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000709 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000710 E = PreservedSet.end(); I != E; ++I) {
711 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000712 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000713 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000714 }
Devang Patela273d1c2007-07-19 18:02:32 +0000715}
716
Devang Patel9dbe4d12008-07-01 17:44:24 +0000717/// verifyDomInfo - Verify dominator information if it is available.
718void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000719 if (!VerifyDomInfo || !P.getResolver())
720 return;
721
Duncan Sands5a913d62009-01-28 13:14:17 +0000722 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000723 if (!DT)
724 return;
725
726 DominatorTree OtherDT;
727 OtherDT.getBase().recalculate(F);
728 if (DT->compare(OtherDT)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000729 errs() << "Dominator Information for " << F.getName() << "\n";
730 errs() << "Pass '" << P.getPassName() << "'\n";
731 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000732 OtherDT.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000733 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000734 DT->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000735 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000736 }
737
Duncan Sands5a913d62009-01-28 13:14:17 +0000738 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000739 if (!DF)
740 return;
741
742 DominanceFrontier OtherDF;
743 std::vector<BasicBlock*> DTRoots = DT->getRoots();
744 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
745 if (DF->compare(OtherDF)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000746 errs() << "Dominator Information for " << F.getName() << "\n";
747 errs() << "Pass '" << P.getPassName() << "'\n";
748 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000749 OtherDF.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000750 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000751 DF->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000752 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000753 }
754}
755
Devang Patel67c79a42008-07-01 19:50:56 +0000756/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000757void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000758 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
759 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000760 return;
761
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000762 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000763 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000764 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000765 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000766 if (!dynamic_cast<ImmutablePass*>(Info->second)
767 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000768 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000769 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000770 if (PassDebugging >= Details) {
771 Pass *S = Info->second;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000772 errs() << " -- '" << P->getPassName() << "' is not preserving '";
773 errs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000774 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000775 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000776 }
Devang Patel349170f2006-11-11 01:24:55 +0000777 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000778
779 // Check inherited analysis also. If P is not preserving analysis
780 // provided by parent manager then remove it here.
781 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
782
783 if (!InheritedAnalysis[Index])
784 continue;
785
786 for (std::map<AnalysisID, Pass*>::iterator
787 I = InheritedAnalysis[Index]->begin(),
788 E = InheritedAnalysis[Index]->end(); I != E; ) {
789 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000790 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
791 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000792 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000793 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000794 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000795 }
796 }
Devang Patelf68a3492006-11-07 22:35:17 +0000797}
798
Devang Patelca189262006-11-14 03:05:08 +0000799/// Remove analysis passes that are not used any longer
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000800void PMDataManager::removeDeadPasses(Pass *P, const StringRef &Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000801 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000802
Devang Patel8adae862007-07-20 18:04:54 +0000803 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000804
Devang Patel2ff44922007-04-16 20:39:59 +0000805 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000806 if (!TPM)
807 return;
808
Devang Patel17ad0962006-12-08 00:37:52 +0000809 TPM->collectLastUses(DeadPasses, P);
810
Devang Patel656a9172008-06-06 17:50:36 +0000811 if (PassDebugging >= Details && !DeadPasses.empty()) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000812 errs() << " -*- '" << P->getPassName();
813 errs() << "' is the last user of following pass instances.";
814 errs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000815 }
816
Devang Patel8adae862007-07-20 18:04:54 +0000817 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000818 E = DeadPasses.end(); I != E; ++I)
819 freePass(*I, Msg, DBG_STR);
820}
Devang Patel200d3052006-12-13 23:50:44 +0000821
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000822void PMDataManager::freePass(Pass *P, const StringRef &Msg,
823 enum PassDebuggingString DBG_STR) {
824 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000825
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000826 {
827 // If the pass crashes releasing memory, remember this.
828 PassManagerPrettyStackEntry X(P);
829
830 if (TheTimeInfo) TheTimeInfo->passStarted(P);
831 P->releaseMemory();
832 if (TheTimeInfo) TheTimeInfo->passEnded(P);
833 }
834
835 if (const PassInfo *PI = P->getPassInfo()) {
836 // Remove the pass itself (if it is not already removed).
837 AvailableAnalysis.erase(PI);
838
839 // Remove all interfaces this pass implements, for which it is also
840 // listed as the available implementation.
841 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
842 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000843 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000844 AvailableAnalysis.find(II[i]);
845 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000846 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000847 }
Devang Patel17ad0962006-12-08 00:37:52 +0000848 }
Devang Patelca189262006-11-14 03:05:08 +0000849}
850
Devang Patel8f677ce2006-12-07 18:47:25 +0000851/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000852/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000853void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000854 // This manager is going to manage pass P. Set up analysis resolver
855 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000856 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000857 P->setResolver(AR);
858
Devang Patelec2b9a72007-03-05 22:57:49 +0000859 // If a FunctionPass F is the last user of ModulePass info M
860 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000861 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000862
Chris Lattner60987362009-03-06 05:53:14 +0000863 if (!ProcessAnalysis) {
864 // Add pass
865 PassVector.push_back(P);
866 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000867 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000868
Chris Lattner60987362009-03-06 05:53:14 +0000869 // At the moment, this pass is the last user of all required passes.
870 SmallVector<Pass *, 12> LastUses;
871 SmallVector<Pass *, 8> RequiredPasses;
872 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
873
874 unsigned PDepth = this->getDepth();
875
876 collectRequiredAnalysis(RequiredPasses,
877 ReqAnalysisNotAvailable, P);
878 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
879 E = RequiredPasses.end(); I != E; ++I) {
880 Pass *PRequired = *I;
881 unsigned RDepth = 0;
882
883 assert(PRequired->getResolver() && "Analysis Resolver is not set");
884 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
885 RDepth = DM.getDepth();
886
887 if (PDepth == RDepth)
888 LastUses.push_back(PRequired);
889 else if (PDepth > RDepth) {
890 // Let the parent claim responsibility of last use
891 TransferLastUses.push_back(PRequired);
892 // Keep track of higher level analysis used by this manager.
893 HigherLevelAnalysis.push_back(PRequired);
894 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000895 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000896 }
897
898 // Set P as P's last user until someone starts using P.
899 // However, if P is a Pass Manager then it does not need
900 // to record its last user.
901 if (!dynamic_cast<PMDataManager *>(P))
902 LastUses.push_back(P);
903 TPM->setLastUser(LastUses, P);
904
905 if (!TransferLastUses.empty()) {
906 Pass *My_PM = dynamic_cast<Pass *>(this);
907 TPM->setLastUser(TransferLastUses, My_PM);
908 TransferLastUses.clear();
909 }
910
911 // Now, take care of required analysises that are not available.
912 for (SmallVector<AnalysisID, 8>::iterator
913 I = ReqAnalysisNotAvailable.begin(),
914 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
915 Pass *AnalysisPass = (*I)->createPass();
916 this->addLowerLevelRequiredPass(P, AnalysisPass);
917 }
918
919 // Take a note of analysis required and made available by this pass.
920 // Remove the analysis not preserved by this pass
921 removeNotPreservedAnalysis(P);
922 recordAvailableAnalysis(P);
923
Devang Patel8cad70d2006-11-11 01:51:02 +0000924 // Add pass
925 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000926}
927
Devang Patele64d3052007-04-16 20:12:57 +0000928
929/// Populate RP with analysis pass that are required by
930/// pass P and are available. Populate RP_NotAvail with analysis
931/// pass that are required by pass P but are not available.
932void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
933 SmallVector<AnalysisID, 8> &RP_NotAvail,
934 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000935 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
936 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000937 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000938 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000939 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
940 RP.push_back(AnalysisPass);
941 else
Chris Lattner60987362009-03-06 05:53:14 +0000942 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000943 }
Devang Patelf58183d2006-12-12 23:09:32 +0000944
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000945 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000946 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000947 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000948 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
949 RP.push_back(AnalysisPass);
950 else
Chris Lattner60987362009-03-06 05:53:14 +0000951 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000952 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000953}
954
Devang Patel07f4f582006-11-14 21:49:36 +0000955// All Required analyses should be available to the pass as it runs! Here
956// we fill in the AnalysisImpls member of the pass so that it can
957// successfully use the getAnalysis() method to retrieve the
958// implementations it needs.
959//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000960void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000961 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
962
Chris Lattnercbd160f2008-08-08 05:33:04 +0000963 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000964 I = AnUsage->getRequiredSet().begin(),
965 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000966 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000967 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000968 // This may be analysis pass that is initialized on the fly.
969 // If that is not the case then it will raise an assert when it is used.
970 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000971 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000972 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000973 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000974 }
975}
976
Devang Patel640c5bb2006-12-08 22:30:11 +0000977/// Find the pass that implements Analysis AID. If desired pass is not found
978/// then return NULL.
979Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
980
981 // Check if AvailableAnalysis map has one entry.
982 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
983
984 if (I != AvailableAnalysis.end())
985 return I->second;
986
987 // Search Parents through TopLevelManager
988 if (SearchParent)
989 return TPM->findAnalysisPass(AID);
990
Devang Patel9d759b82006-12-09 00:09:12 +0000991 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000992}
993
Devang Patel991aeba2006-12-15 20:13:01 +0000994// Print list of passes that are last used by P.
995void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
996
Devang Patel8adae862007-07-20 18:04:54 +0000997 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000998
999 // If this is a on the fly manager then it does not have TPM.
1000 if (!TPM)
1001 return;
1002
Devang Patel991aeba2006-12-15 20:13:01 +00001003 TPM->collectLastUses(LUses, P);
1004
Devang Patel8adae862007-07-20 18:04:54 +00001005 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001006 E = LUses.end(); I != E; ++I) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001007 llvm::errs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +00001008 (*I)->dumpPassStructure(0);
1009 }
1010}
1011
1012void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001013 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001014 E = PassVector.end(); I != E; ++I) {
1015 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1016 PMD->dumpPassArguments();
1017 else
1018 if (const PassInfo *PI = (*I)->getPassInfo())
1019 if (!PI->isAnalysisGroup())
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001020 errs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001021 }
1022}
1023
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001024void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1025 enum PassDebuggingString S2,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001026 const StringRef &Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001027 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001028 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001029 errs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001030 switch (S1) {
1031 case EXECUTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001032 errs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001033 break;
1034 case MODIFICATION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001035 errs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001036 break;
1037 case FREEING_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001038 errs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001039 break;
1040 default:
1041 break;
1042 }
1043 switch (S2) {
1044 case ON_BASICBLOCK_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001045 errs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001046 break;
1047 case ON_FUNCTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001048 errs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001049 break;
1050 case ON_MODULE_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001051 errs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001052 break;
1053 case ON_LOOP_MSG:
Chris Lattnerf82f27b2009-09-15 04:45:26 +00001054 errs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001055 break;
1056 case ON_CG_MSG:
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001057 errs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001058 break;
1059 default:
1060 break;
1061 }
Devang Patel991aeba2006-12-15 20:13:01 +00001062}
1063
Chris Lattner4c1e9542009-03-06 06:45:05 +00001064void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001065 if (PassDebugging < Details)
1066 return;
1067
1068 AnalysisUsage analysisUsage;
1069 P->getAnalysisUsage(analysisUsage);
1070 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1071}
1072
Chris Lattner4c1e9542009-03-06 06:45:05 +00001073void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001074 if (PassDebugging < Details)
1075 return;
1076
1077 AnalysisUsage analysisUsage;
1078 P->getAnalysisUsage(analysisUsage);
1079 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1080}
1081
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001082void PMDataManager::dumpAnalysisUsage(const StringRef &Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001083 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001084 assert(PassDebugging >= Details);
1085 if (Set.empty())
1086 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001087 errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001088 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001089 if (i) errs() << ',';
1090 errs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001091 }
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001092 errs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001093}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001094
Devang Patel004937b2007-07-27 20:06:09 +00001095/// Add RequiredPass into list of lower level passes required by pass P.
1096/// RequiredPass is run on the fly by Pass Manager when P requests it
1097/// through getAnalysis interface.
1098/// This should be handled by specific pass manager.
1099void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1100 if (TPM) {
1101 TPM->dumpArguments();
1102 TPM->dumpPasses();
1103 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001104
1105 // Module Level pass may required Function Level analysis info
1106 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1107 // to provide this on demand. In that case, in Pass manager terminology,
1108 // module level pass is requiring lower level analysis info managed by
1109 // lower level pass manager.
1110
1111 // When Pass manager is not able to order required analysis info, Pass manager
1112 // checks whether any lower level manager will be able to provide this
1113 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001114#ifndef NDEBUG
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001115 errs() << "Unable to schedule '" << RequiredPass->getPassName();
1116 errs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001117#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001118 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001119}
1120
Devang Patele7599552007-01-12 18:52:44 +00001121// Destructor
1122PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001123 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001124 E = PassVector.end(); I != E; ++I)
1125 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001126}
1127
Devang Patel9bdf7d42006-12-08 23:28:54 +00001128//===----------------------------------------------------------------------===//
1129// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001130// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1131Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001132 return PM.findAnalysisPass(ID, dir);
1133}
1134
Devang Patel92942812007-04-16 20:56:24 +00001135Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1136 Function &F) {
1137 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1138}
1139
Devang Patela1514cb2006-12-07 19:39:39 +00001140//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001141// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001142
Devang Patel6e5a1132006-11-07 21:31:57 +00001143/// Execute all of the passes scheduled for execution by invoking
1144/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1145/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001146bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001147 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001148 return false;
1149
Devang Patele9585592006-12-08 01:38:28 +00001150 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001151
Devang Patel6e5a1132006-11-07 21:31:57 +00001152 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001153 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1154 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001155
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001156 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001157 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001158
Devang Patelabfbe3b2006-12-16 00:56:26 +00001159 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001160
Chris Lattner4c1e9542009-03-06 06:45:05 +00001161 {
1162 // If the pass crashes, remember this.
1163 PassManagerPrettyStackEntry X(BP, *I);
1164
1165 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1166 Changed |= BP->runOnBasicBlock(*I);
1167 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1168 }
Devang Patel93a197c2006-12-14 00:08:04 +00001169
Devang Patel003a5592007-03-05 20:01:30 +00001170 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001171 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001172 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001173 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001174
Devang Patela273d1c2007-07-19 18:02:32 +00001175 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001176 removeNotPreservedAnalysis(BP);
1177 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001178 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001179 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001180
Devang Patel56d48ec2006-12-15 22:57:49 +00001181 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001182}
1183
Devang Patel475c4532006-12-08 00:59:05 +00001184// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001185bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001186 bool Changed = false;
1187
Chris Lattner4c1e9542009-03-06 06:45:05 +00001188 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1189 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001190
1191 return Changed;
1192}
1193
Duncan Sands51495602009-02-13 09:42:34 +00001194bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001195 bool Changed = false;
1196
Chris Lattner4c1e9542009-03-06 06:45:05 +00001197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1198 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001199
1200 return Changed;
1201}
1202
Duncan Sands51495602009-02-13 09:42:34 +00001203bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001204 bool Changed = false;
1205
Devang Patelabfbe3b2006-12-16 00:56:26 +00001206 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1207 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001208 Changed |= BP->doInitialization(F);
1209 }
1210
1211 return Changed;
1212}
1213
Duncan Sands51495602009-02-13 09:42:34 +00001214bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001215 bool Changed = false;
1216
Devang Patelabfbe3b2006-12-16 00:56:26 +00001217 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1218 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001219 Changed |= BP->doFinalization(F);
1220 }
1221
1222 return Changed;
1223}
1224
1225
Devang Patela1514cb2006-12-07 19:39:39 +00001226//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001227// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001228
Devang Patel4e12f862006-11-08 10:44:40 +00001229/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001230FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001231 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001232 // FPM is the top level manager.
1233 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001234
Dan Gohman565df952008-03-13 02:08:36 +00001235 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001236 FPM->setResolver(AR);
1237
Devang Patel1f653682006-12-08 18:57:16 +00001238 MP = P;
1239}
1240
Devang Patelb67904d2006-12-13 02:36:01 +00001241FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001242 delete FPM;
1243}
1244
Devang Patel4e12f862006-11-08 10:44:40 +00001245/// add - Add a pass to the queue of passes to run. This passes
1246/// ownership of the Pass to the PassManager. When the
1247/// PassManager_X is destroyed, the pass will be destroyed as well, so
1248/// there is no need to delete the pass. (TODO delete passes.)
1249/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001250void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001251 FPM->add(P);
1252}
1253
Devang Patel9f3083e2006-11-15 19:39:54 +00001254/// run - Execute all of the passes scheduled for execution. Keep
1255/// track of whether any of the passes modifies the function, and if
1256/// so, return true.
1257///
Devang Patelb67904d2006-12-13 02:36:01 +00001258bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001259 std::string errstr;
1260 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001261 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001262 }
Devang Patel272908d2006-12-08 22:57:48 +00001263 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001264}
1265
1266
Devang Patelff631ae2006-11-15 01:27:05 +00001267/// doInitialization - Run all of the initializers for the function passes.
1268///
Devang Patelb67904d2006-12-13 02:36:01 +00001269bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001270 return FPM->doInitialization(*MP->getModule());
1271}
1272
Dan Gohmane6656eb2007-07-30 14:51:13 +00001273/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001274///
Devang Patelb67904d2006-12-13 02:36:01 +00001275bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001276 return FPM->doFinalization(*MP->getModule());
1277}
1278
Devang Patela1514cb2006-12-07 19:39:39 +00001279//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001280// FunctionPassManagerImpl implementation
1281//
Duncan Sands51495602009-02-13 09:42:34 +00001282bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001283 bool Changed = false;
1284
Chris Lattner4c1e9542009-03-06 06:45:05 +00001285 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1286 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287
1288 return Changed;
1289}
1290
Duncan Sands51495602009-02-13 09:42:34 +00001291bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001292 bool Changed = false;
1293
Chris Lattner4c1e9542009-03-06 06:45:05 +00001294 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1295 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001296
1297 return Changed;
1298}
1299
Devang Patelec9c58f2009-04-01 22:34:41 +00001300/// cleanup - After running all passes, clean up pass manager cache.
1301void FPPassManager::cleanup() {
1302 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1303 FunctionPass *FP = getContainedPass(Index);
1304 AnalysisResolver *AR = FP->getResolver();
1305 assert(AR && "Analysis Resolver is not set");
1306 AR->clearAnalysisImpls();
1307 }
1308}
1309
Torok Edwin24c78352009-06-29 18:49:09 +00001310void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1311 if (!wasRun)
1312 return;
1313 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1314 FPPassManager *FPPM = getContainedManager(Index);
1315 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1316 FPPM->getContainedPass(Index)->releaseMemory();
1317 }
1318 }
Torok Edwin896556e2009-06-29 21:05:10 +00001319 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001320}
1321
Devang Patel67d6a5e2006-12-19 19:46:59 +00001322// Execute all the passes managed by this top level manager.
1323// Return true if any function is modified by a pass.
1324bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001325 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001326 TimingInfo::createTheTimeInfo();
1327
1328 dumpArguments();
1329 dumpPasses();
1330
Devang Patele3068402006-12-21 00:16:50 +00001331 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001332 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1333 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001334
1335 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1336 getContainedManager(Index)->cleanup();
1337
Torok Edwin24c78352009-06-29 18:49:09 +00001338 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001339 return Changed;
1340}
1341
1342//===----------------------------------------------------------------------===//
1343// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001344
Devang Patel8c78a0b2007-05-03 01:11:54 +00001345char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001346/// Print passes managed by this manager
1347void FPPassManager::dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001348 llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001349 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1350 FunctionPass *FP = getContainedPass(Index);
1351 FP->dumpPassStructure(Offset + 1);
1352 dumpLastUses(FP, Offset+1);
1353 }
1354}
1355
1356
Devang Patel0c2012f2006-11-07 21:49:50 +00001357/// Execute all of the passes scheduled for execution by invoking
1358/// runOnFunction method. Keep track of whether any of the passes modifies
1359/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001360bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001361 if (F.isDeclaration())
1362 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001363
1364 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001365
Devang Patelcbbf2912008-03-20 01:09:53 +00001366 // Collect inherited analysis from Module level pass manager.
1367 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001368
Devang Patelabfbe3b2006-12-16 00:56:26 +00001369 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1370 FunctionPass *FP = getContainedPass(Index);
1371
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001372 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001373 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001374
Devang Patelabfbe3b2006-12-16 00:56:26 +00001375 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001376
Chris Lattner4c1e9542009-03-06 06:45:05 +00001377 {
1378 PassManagerPrettyStackEntry X(FP, F);
1379
1380 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1381 Changed |= FP->runOnFunction(F);
1382 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1383 }
Devang Patel93a197c2006-12-14 00:08:04 +00001384
Devang Patel003a5592007-03-05 20:01:30 +00001385 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001386 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001387 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001388
Devang Patela273d1c2007-07-19 18:02:32 +00001389 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001390 removeNotPreservedAnalysis(FP);
1391 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001392 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001393
Devang Patel67c79a42008-07-01 19:50:56 +00001394 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001395 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001396 }
1397 return Changed;
1398}
1399
Devang Patel67d6a5e2006-12-19 19:46:59 +00001400bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001401 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001402
Chris Lattner60987362009-03-06 05:53:14 +00001403 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1404 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001405
1406 return Changed |= doFinalization(M);
1407}
1408
Duncan Sands51495602009-02-13 09:42:34 +00001409bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001410 bool Changed = false;
1411
Chris Lattner4c1e9542009-03-06 06:45:05 +00001412 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1413 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001414
1415 return Changed;
1416}
1417
Duncan Sands51495602009-02-13 09:42:34 +00001418bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001419 bool Changed = false;
1420
Chris Lattner4c1e9542009-03-06 06:45:05 +00001421 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1422 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001423
Devang Patelff631ae2006-11-15 01:27:05 +00001424 return Changed;
1425}
1426
Devang Patela1514cb2006-12-07 19:39:39 +00001427//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001429
Devang Patel05e1a972006-11-07 22:03:15 +00001430/// Execute all of the passes scheduled for execution by invoking
1431/// runOnModule method. Keep track of whether any of the passes modifies
1432/// the module, and if so, return true.
1433bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001434MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001435 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001436
Torok Edwin24c78352009-06-29 18:49:09 +00001437 // Initialize on-the-fly passes
1438 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1439 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1440 I != E; ++I) {
1441 FunctionPassManagerImpl *FPP = I->second;
1442 Changed |= FPP->doInitialization(M);
1443 }
1444
Devang Patelabfbe3b2006-12-16 00:56:26 +00001445 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1446 ModulePass *MP = getContainedPass(Index);
1447
Dan Gohman929391a2008-01-29 12:09:55 +00001448 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1449 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001450 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001451
Devang Patelabfbe3b2006-12-16 00:56:26 +00001452 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001453
Chris Lattner4c1e9542009-03-06 06:45:05 +00001454 {
1455 PassManagerPrettyStackEntry X(MP, M);
1456 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1457 Changed |= MP->runOnModule(M);
1458 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1459 }
Devang Patel93a197c2006-12-14 00:08:04 +00001460
Devang Patel003a5592007-03-05 20:01:30 +00001461 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001462 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1463 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001464 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001465
Devang Patela273d1c2007-07-19 18:02:32 +00001466 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001467 removeNotPreservedAnalysis(MP);
1468 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001469 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001470 }
Torok Edwin24c78352009-06-29 18:49:09 +00001471
1472 // Finalize on-the-fly passes
1473 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1474 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1475 I != E; ++I) {
1476 FunctionPassManagerImpl *FPP = I->second;
1477 // We don't know when is the last time an on-the-fly pass is run,
1478 // so we need to releaseMemory / finalize here
1479 FPP->releaseMemoryOnTheFly();
1480 Changed |= FPP->doFinalization(M);
1481 }
Devang Patel05e1a972006-11-07 22:03:15 +00001482 return Changed;
1483}
1484
Devang Patele64d3052007-04-16 20:12:57 +00001485/// Add RequiredPass into list of lower level passes required by pass P.
1486/// RequiredPass is run on the fly by Pass Manager when P requests it
1487/// through getAnalysis interface.
1488void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001489 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1490 "Unable to handle Pass that requires lower level Analysis pass");
1491 assert((P->getPotentialPassManagerType() <
1492 RequiredPass->getPotentialPassManagerType()) &&
1493 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001494
Devang Patel68f72b12007-04-26 17:50:19 +00001495 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001496 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001497 FPP = new FunctionPassManagerImpl(0);
1498 // FPP is the top level manager.
1499 FPP->setTopLevelManager(FPP);
1500
Devang Patel69e9f6d2007-04-16 20:27:05 +00001501 OnTheFlyManagers[P] = FPP;
1502 }
Devang Patel68f72b12007-04-26 17:50:19 +00001503 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001504
Devang Patel68f72b12007-04-26 17:50:19 +00001505 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001506 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001507 LU.push_back(RequiredPass);
1508 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001509}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001510
1511/// Return function pass corresponding to PassInfo PI, that is
1512/// required by module pass MP. Instantiate analysis pass, by using
1513/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001514Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001515 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001516 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001517
Torok Edwin24c78352009-06-29 18:49:09 +00001518 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001519 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001520 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001521}
1522
1523
Devang Patela1514cb2006-12-07 19:39:39 +00001524//===----------------------------------------------------------------------===//
1525// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001526//
Devang Patelc290c8a2006-11-07 22:23:34 +00001527/// run - Execute all of the passes scheduled for execution. Keep track of
1528/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001529bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001530 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001531 TimingInfo::createTheTimeInfo();
1532
Devang Patelcfd70c42006-12-13 22:10:00 +00001533 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001534 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001535
Devang Patele3068402006-12-21 00:16:50 +00001536 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001537 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1538 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001539 return Changed;
1540}
Devang Patel376fefa2006-11-08 10:29:57 +00001541
Devang Patela1514cb2006-12-07 19:39:39 +00001542//===----------------------------------------------------------------------===//
1543// PassManager implementation
1544
Devang Patel376fefa2006-11-08 10:29:57 +00001545/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001546PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001547 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001548 // PM is the top level manager
1549 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001550}
1551
Devang Patelb67904d2006-12-13 02:36:01 +00001552PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001553 delete PM;
1554}
1555
Devang Patel376fefa2006-11-08 10:29:57 +00001556/// add - Add a pass to the queue of passes to run. This passes ownership of
1557/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1558/// will be destroyed as well, so there is no need to delete the pass. This
1559/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001560void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001561 PM->add(P);
1562}
1563
1564/// run - Execute all of the passes scheduled for execution. Keep track of
1565/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001566bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001567 return PM->run(M);
1568}
1569
Devang Patelb8817b92006-12-14 00:59:42 +00001570//===----------------------------------------------------------------------===//
1571// TimingInfo Class - This class is used to calculate information about the
1572// amount of time each pass takes to execute. This only happens with
1573// -time-passes is enabled on the command line.
1574//
1575bool llvm::TimePassesIsEnabled = false;
1576static cl::opt<bool,true>
1577EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1578 cl::desc("Time each pass, printing elapsed time for each on exit"));
1579
1580// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1581// a non null value (if the -time-passes option is enabled) or it leaves it
1582// null. It may be called multiple times.
1583void TimingInfo::createTheTimeInfo() {
1584 if (!TimePassesIsEnabled || TheTimeInfo) return;
1585
1586 // Constructed the first time this is called, iff -time-passes is enabled.
1587 // This guarantees that the object will be constructed before static globals,
1588 // thus it will be destroyed before them.
1589 static ManagedStatic<TimingInfo> TTI;
1590 TheTimeInfo = &*TTI;
1591}
1592
Devang Patel1c3633e2007-01-29 23:10:37 +00001593/// If TimingInfo is enabled then start pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001594void llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001595 if (TheTimeInfo)
1596 TheTimeInfo->passStarted(P);
1597}
1598
1599/// If TimingInfo is enabled then stop pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001600void llvm::StopPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001601 if (TheTimeInfo)
1602 TheTimeInfo->passEnded(P);
1603}
1604
Devang Patel1c56a632007-01-08 19:29:38 +00001605//===----------------------------------------------------------------------===//
1606// PMStack implementation
1607//
Devang Patelad98d232007-01-11 22:15:30 +00001608
Devang Patel1c56a632007-01-08 19:29:38 +00001609// Pop Pass Manager from the stack and clear its analysis info.
1610void PMStack::pop() {
1611
1612 PMDataManager *Top = this->top();
1613 Top->initializeAnalysisInfo();
1614
1615 S.pop_back();
1616}
1617
1618// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001619void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001620 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001621
Chris Lattner60987362009-03-06 05:53:14 +00001622 if (!this->empty()) {
1623 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001624
Chris Lattner60987362009-03-06 05:53:14 +00001625 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001626 TPM->addIndirectPassManager(PM);
1627 PM->setTopLevelManager(TPM);
1628 }
1629
Devang Patel15701b52007-01-11 00:19:00 +00001630 S.push_back(PM);
1631}
1632
1633// Dump content of the pass manager stack.
1634void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001635 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1636 E = S.end(); I != E; ++I)
1637 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1638
Devang Patel15701b52007-01-11 00:19:00 +00001639 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001640 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001641}
1642
Devang Patel1c56a632007-01-08 19:29:38 +00001643/// Find appropriate Module Pass Manager in the PM Stack and
1644/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001645void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001646 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001647 // Find Module Pass Manager
1648 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001649 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1650 if (TopPMType == PreferredType)
1651 break; // We found desired pass manager
1652 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001653 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001654 else
1655 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001656 }
Devang Patel18ff6362008-09-09 21:38:40 +00001657 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001658 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001659}
1660
Devang Patel3312f752007-01-16 21:43:18 +00001661/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1662/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001663void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001664 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001665
Devang Patela3286902008-09-09 17:56:50 +00001666 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001667 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001668 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1669 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001670 else
Devang Patel3312f752007-01-16 21:43:18 +00001671 break;
1672 }
1673 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1674
1675 // Create new Function Pass Manager
1676 if (!FPP) {
1677 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1678 PMDataManager *PMD = PMS.top();
1679
1680 // [1] Create new Function Pass Manager
1681 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001682 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001683
1684 // [2] Set up new manager's top level manager
1685 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1686 TPM->addIndirectPassManager(FPP);
1687
1688 // [3] Assign manager to manage this new manager. This may create
1689 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001690 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001691
1692 // [4] Push new manager into PMS
1693 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001694 }
1695
Devang Patel3312f752007-01-16 21:43:18 +00001696 // Assign FPP as the manager of this pass.
1697 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001698}
1699
Devang Patel3312f752007-01-16 21:43:18 +00001700/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001701/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001702void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001703 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001704 BBPassManager *BBP = NULL;
1705
Devang Patel15701b52007-01-11 00:19:00 +00001706 // Basic Pass Manager is a leaf pass manager. It does not handle
1707 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001708 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001709 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001710
Devang Patel3312f752007-01-16 21:43:18 +00001711 // If leaf manager is not Basic Block Pass manager then create new
1712 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001713
Devang Patel3312f752007-01-16 21:43:18 +00001714 if (!BBP) {
1715 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1716 PMDataManager *PMD = PMS.top();
1717
1718 // [1] Create new Basic Block Manager
1719 BBP = new BBPassManager(PMD->getDepth() + 1);
1720
1721 // [2] Set up new manager's top level manager
1722 // Basic Block Pass Manager does not live by itself
1723 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1724 TPM->addIndirectPassManager(BBP);
1725
Devang Patel15701b52007-01-11 00:19:00 +00001726 // [3] Assign manager to manage this new manager. This may create
1727 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001728 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001729
Devang Patel3312f752007-01-16 21:43:18 +00001730 // [4] Push new manager into PMS
1731 PMS.push(BBP);
1732 }
Devang Patel1c56a632007-01-08 19:29:38 +00001733
Devang Patel3312f752007-01-16 21:43:18 +00001734 // Assign BBP as the manager of this pass.
1735 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001736}
1737
Dan Gohmand3a20c92008-03-11 16:41:42 +00001738PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001739
1740/*===-- C Bindings --------------------------------------------------------===*/
1741
1742LLVMPassManagerRef LLVMCreatePassManager() {
1743 return wrap(new PassManager());
1744}
1745
1746LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1747 return wrap(new FunctionPassManager(unwrap(P)));
1748}
1749
1750int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1751 return unwrap<PassManager>(PM)->run(*unwrap(M));
1752}
1753
1754int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1755 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1756}
1757
1758int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1759 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1760}
1761
1762int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1763 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1764}
1765
1766void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1767 delete unwrap(PM);
1768}