blob: 83045cd4ab50b614bc2709e213763e7aaaeac981 [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 Lattner4c1e9542009-03-06 06:45:05 +000070void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
71 if (V == 0 && M == 0)
72 OS << "Releasing pass '";
73 else
74 OS << "Running pass '";
75
76 OS << P->getPassName() << "'";
77
78 if (M) {
79 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
80 return;
81 }
82 if (V == 0) {
83 OS << '\n';
84 return;
85 }
86
Dan Gohman79fc0e92009-03-10 18:47:59 +000087 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +000088 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000089 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +000090 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000091 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +000092 else
Dan Gohman79fc0e92009-03-10 18:47:59 +000093 OS << "value";
94
95 OS << " '";
96 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
97 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +000098}
99
100
Devang Patelffca9102006-12-15 19:39:30 +0000101namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000102
Devang Patelf33f3eb2006-12-07 19:21:29 +0000103//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000104// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000105//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000106/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000107/// pass together and sequence them to process one basic block before
108/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000109class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
110 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000111
112public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000113 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000114 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000115 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000116
Devang Patelca58e352006-11-08 10:05:38 +0000117 /// Execute all of the passes scheduled for execution. Keep track of
118 /// whether any of the passes modifies the function, and if so, return true.
119 bool runOnFunction(Function &F);
120
Devang Patelf9d96b92006-12-07 19:57:52 +0000121 /// Pass Manager itself does not invalidate any analysis info.
122 void getAnalysisUsage(AnalysisUsage &Info) const {
123 Info.setPreservesAll();
124 }
125
Devang Patel475c4532006-12-08 00:59:05 +0000126 bool doInitialization(Module &M);
127 bool doInitialization(Function &F);
128 bool doFinalization(Module &M);
129 bool doFinalization(Function &F);
130
Devang Patele3858e62007-02-01 22:08:25 +0000131 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000132 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000133 }
134
Devang Pateleda56172006-12-12 23:34:33 +0000135 // Print passes managed by this manager
136 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000137 llvm::errs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000138 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
139 BasicBlockPass *BP = getContainedPass(Index);
140 BP->dumpPassStructure(Offset + 1);
141 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000142 }
143 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000144
145 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000146 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000147 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
148 return BP;
149 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000150
Devang Patel28349ab2007-02-27 15:00:39 +0000151 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000152 return PMT_BasicBlockPassManager;
153 }
Devang Patelca58e352006-11-08 10:05:38 +0000154};
155
Devang Patel8c78a0b2007-05-03 01:11:54 +0000156char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000157}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158
Devang Patele7599552007-01-12 18:52:44 +0000159namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000160
Devang Patel10c2ca62006-12-12 22:47:13 +0000161//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000162// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000163//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000164/// FunctionPassManagerImpl manages FPPassManagers
165class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000166 public PMDataManager,
167 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000168private:
169 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000170public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000171 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000172 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000173 Pass(&ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000174 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000175
176 /// add - Add a pass to the queue of passes to run. This passes ownership of
177 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
178 /// will be destroyed as well, so there is no need to delete the pass. This
179 /// implies that all passes MUST be allocated with 'new'.
180 void add(Pass *P) {
181 schedulePass(P);
182 }
183
Torok Edwin24c78352009-06-29 18:49:09 +0000184 // Prepare for running an on the fly pass, freeing memory if needed
185 // from a previous run.
186 void releaseMemoryOnTheFly();
187
Devang Patel67d6a5e2006-12-19 19:46:59 +0000188 /// run - Execute all of the passes scheduled for execution. Keep track of
189 /// whether any of the passes modifies the module, and if so, return true.
190 bool run(Function &F);
191
192 /// doInitialization - Run all of the initializers for the function passes.
193 ///
194 bool doInitialization(Module &M);
195
Dan Gohmane6656eb2007-07-30 14:51:13 +0000196 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000197 ///
198 bool doFinalization(Module &M);
199
200 /// Pass Manager itself does not invalidate any analysis info.
201 void getAnalysisUsage(AnalysisUsage &Info) const {
202 Info.setPreservesAll();
203 }
204
205 inline void addTopLevelPass(Pass *P) {
206
207 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
208
209 // P is a immutable pass and it will be managed by this
210 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000211 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000212 P->setResolver(AR);
213 initializeAnalysisImpl(P);
214 addImmutablePass(IP);
215 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000216 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000217 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218 }
Devang Patel0f080042007-01-12 17:23:48 +0000219
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220 }
221
222 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000223 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000224 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
225 return FP;
226 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227};
228
Devang Patel8c78a0b2007-05-03 01:11:54 +0000229char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000230//===----------------------------------------------------------------------===//
231// MPPassManager
232//
233/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000234/// It batches all Module passes and function pass managers together and
235/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000236class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000237public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000238 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000239 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000240 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000241
242 // Delete on the fly managers.
243 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000244 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000245 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
246 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000247 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000248 delete FPP;
249 }
250 }
251
Devang Patelca58e352006-11-08 10:05:38 +0000252 /// run - Execute all of the passes scheduled for execution. Keep track of
253 /// whether any of the passes modifies the module, and if so, return true.
254 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000255
Devang Patelf9d96b92006-12-07 19:57:52 +0000256 /// Pass Manager itself does not invalidate any analysis info.
257 void getAnalysisUsage(AnalysisUsage &Info) const {
258 Info.setPreservesAll();
259 }
260
Devang Patele64d3052007-04-16 20:12:57 +0000261 /// Add RequiredPass into list of lower level passes required by pass P.
262 /// RequiredPass is run on the fly by Pass Manager when P requests it
263 /// through getAnalysis interface.
264 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
265
Devang Patel69e9f6d2007-04-16 20:27:05 +0000266 /// Return function pass corresponding to PassInfo PI, that is
267 /// required by module pass MP. Instantiate analysis pass, by using
268 /// its runOnFunction() for function F.
269 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
270
Devang Patele3858e62007-02-01 22:08:25 +0000271 virtual const char *getPassName() const {
272 return "Module Pass Manager";
273 }
274
Devang Pateleda56172006-12-12 23:34:33 +0000275 // Print passes managed by this manager
276 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000277 llvm::errs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000278 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
279 ModulePass *MP = getContainedPass(Index);
280 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000281 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
282 OnTheFlyManagers.find(MP);
283 if (I != OnTheFlyManagers.end())
284 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000285 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000286 }
287 }
288
Devang Patelabfbe3b2006-12-16 00:56:26 +0000289 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000290 assert(N < PassVector.size() && "Pass number out of range!");
291 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000292 }
293
Devang Patel28349ab2007-02-27 15:00:39 +0000294 virtual PassManagerType getPassManagerType() const {
295 return PMT_ModulePassManager;
296 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000297
298 private:
299 /// Collection of on the fly FPPassManagers. These managers manage
300 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000301 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000302};
303
Devang Patel8c78a0b2007-05-03 01:11:54 +0000304char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000305//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000306// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000307//
Devang Patel09f162c2007-05-01 21:15:47 +0000308
Devang Patel67d6a5e2006-12-19 19:46:59 +0000309/// PassManagerImpl manages MPPassManagers
310class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000311 public PMDataManager,
312 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000313
314public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000315 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000316 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000317 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000318
Devang Patel376fefa2006-11-08 10:29:57 +0000319 /// add - Add a pass to the queue of passes to run. This passes ownership of
320 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
321 /// will be destroyed as well, so there is no need to delete the pass. This
322 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000323 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000324 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000325 }
Devang Patel376fefa2006-11-08 10:29:57 +0000326
327 /// run - Execute all of the passes scheduled for execution. Keep track of
328 /// whether any of the passes modifies the module, and if so, return true.
329 bool run(Module &M);
330
Devang Patelf9d96b92006-12-07 19:57:52 +0000331 /// Pass Manager itself does not invalidate any analysis info.
332 void getAnalysisUsage(AnalysisUsage &Info) const {
333 Info.setPreservesAll();
334 }
335
Devang Patelabcd1d32006-12-07 21:27:23 +0000336 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000337 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000338
339 // P is a immutable pass and it will be managed by this
340 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000341 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000342 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000343 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000344 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000345 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000346 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000347 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000348 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000349 }
350
Devang Patel67d6a5e2006-12-19 19:46:59 +0000351 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000352 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000353 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
354 return MP;
355 }
Devang Patel376fefa2006-11-08 10:29:57 +0000356};
357
Devang Patel8c78a0b2007-05-03 01:11:54 +0000358char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000359} // End of llvm namespace
360
361namespace {
362
363//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000364/// TimingInfo Class - This class is used to calculate information about the
365/// amount of time each pass takes to execute. This only happens when
366/// -time-passes is enabled on the command line.
367///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000368
Owen Anderson5a6960f2009-06-18 20:51:00 +0000369static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000370
Devang Patel1c3633e2007-01-29 23:10:37 +0000371class VISIBILITY_HIDDEN TimingInfo {
372 std::map<Pass*, Timer> TimingData;
373 TimerGroup TG;
374
375public:
376 // Use 'create' member to get this.
377 TimingInfo() : TG("... Pass execution timing report ...") {}
378
379 // TimingDtor - Print out information about timing information
380 ~TimingInfo() {
381 // Delete all of the timers...
382 TimingData.clear();
383 // TimerGroup is deleted next, printing the report.
384 }
385
386 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
387 // to a non null value (if the -time-passes option is enabled) or it leaves it
388 // null. It may be called multiple times.
389 static void createTheTimeInfo();
390
391 void passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000392 if (dynamic_cast<PMDataManager *>(P))
393 return;
394
Owen Anderson5c96ef72009-07-07 18:33:04 +0000395 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000396 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
397 if (I == TimingData.end())
398 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
399 I->second.startTimer();
400 }
Owen Anderson5a6960f2009-06-18 20:51:00 +0000401
Devang Patel1c3633e2007-01-29 23:10:37 +0000402 void passEnded(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000403 if (dynamic_cast<PMDataManager *>(P))
404 return;
405
Owen Anderson5c96ef72009-07-07 18:33:04 +0000406 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000407 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
Chris Lattner60987362009-03-06 05:53:14 +0000408 assert(I != TimingData.end() && "passStarted/passEnded not nested right!");
Devang Patel1c3633e2007-01-29 23:10:37 +0000409 I->second.stopTimer();
410 }
411};
412
Devang Patel1c3633e2007-01-29 23:10:37 +0000413} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000414
Dan Gohmand78c4002008-05-13 00:00:25 +0000415static TimingInfo *TheTimeInfo;
416
Devang Patela1514cb2006-12-07 19:39:39 +0000417//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000418// PMTopLevelManager implementation
419
Devang Patel4268fc02007-01-16 02:00:38 +0000420/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000421PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000422 if (t == TLM_Pass) {
423 MPPassManager *MPP = new MPPassManager(1);
424 MPP->setTopLevelManager(this);
425 addPassManager(MPP);
426 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000427 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000428 FPPassManager *FPP = new FPPassManager(1);
429 FPP->setTopLevelManager(this);
430 addPassManager(FPP);
431 activeStack.push(FPP);
432 }
433}
434
Devang Patelafb1f3622006-12-12 22:35:25 +0000435/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000436void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000437 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000438 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000439 E = AnalysisPasses.end(); I != E; ++I) {
440 Pass *AP = *I;
441 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000442
443 if (P == AP)
444 continue;
445
Devang Patelafb1f3622006-12-12 22:35:25 +0000446 // If AP is the last user of other passes then make P last user of
447 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000448 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000449 LUE = LastUser.end(); LUI != LUE; ++LUI) {
450 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000451 // DenseMap iterator is not invalidated here because
452 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000453 LastUser[LUI->first] = P;
454 }
455 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000456}
457
458/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000459void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000460 Pass *P) {
461 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
462 InversedLastUser.find(P);
463 if (DMI == InversedLastUser.end())
464 return;
465
466 SmallPtrSet<Pass *, 8> &LU = DMI->second;
467 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
468 E = LU.end(); I != E; ++I) {
469 LastUses.push_back(*I);
470 }
471
Devang Patelafb1f3622006-12-12 22:35:25 +0000472}
473
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000474AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
475 AnalysisUsage *AnUsage = NULL;
476 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
477 if (DMI != AnUsageMap.end())
478 AnUsage = DMI->second;
479 else {
480 AnUsage = new AnalysisUsage();
481 P->getAnalysisUsage(*AnUsage);
482 AnUsageMap[P] = AnUsage;
483 }
484 return AnUsage;
485}
486
Devang Patelafb1f3622006-12-12 22:35:25 +0000487/// Schedule pass P for execution. Make sure that passes required by
488/// P are run before P is run. Update analysis info maintained by
489/// the manager. Remove dead passes. This is a recursive function.
490void PMTopLevelManager::schedulePass(Pass *P) {
491
Devang Patel3312f752007-01-16 21:43:18 +0000492 // TODO : Allocate function manager for this pass, other wise required set
493 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000494
Devang Pateld74ede72007-03-06 01:06:16 +0000495 // Give pass a chance to prepare the stage.
496 P->preparePassManager(activeStack);
497
Devang Patel864970e2008-03-18 00:39:19 +0000498 // If P is an analysis pass and it is available then do not
499 // generate the analysis again. Stale analysis info should not be
500 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000501 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000502 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
503 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000504 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000505 }
Devang Patel864970e2008-03-18 00:39:19 +0000506
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000507 AnalysisUsage *AnUsage = findAnalysisUsage(P);
508
Devang Patelfdee7032008-08-14 23:07:48 +0000509 bool checkAnalysis = true;
510 while (checkAnalysis) {
511 checkAnalysis = false;
512
513 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
514 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
515 E = RequiredSet.end(); I != E; ++I) {
516
517 Pass *AnalysisPass = findAnalysisPass(*I);
518 if (!AnalysisPass) {
519 AnalysisPass = (*I)->createPass();
520 if (P->getPotentialPassManagerType () ==
521 AnalysisPass->getPotentialPassManagerType())
522 // Schedule analysis pass that is managed by the same pass manager.
523 schedulePass(AnalysisPass);
524 else if (P->getPotentialPassManagerType () >
525 AnalysisPass->getPotentialPassManagerType()) {
526 // Schedule analysis pass that is managed by a new manager.
527 schedulePass(AnalysisPass);
528 // Recheck analysis passes to ensure that required analysises that
529 // are already checked are still available.
530 checkAnalysis = true;
531 }
532 else
533 // Do not schedule this analysis. Lower level analsyis
534 // passes are run on the fly.
535 delete AnalysisPass;
536 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000537 }
538 }
539
540 // Now all required passes are available.
541 addTopLevelPass(P);
542}
543
544/// Find the pass that implements Analysis AID. Search immutable
545/// passes and all pass managers. If desired pass is not found
546/// then return NULL.
547Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
548
549 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000550 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000551 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000552 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000553 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000554 P = PMD->findAnalysisPass(AID, false);
555 }
556
557 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000558 for (SmallVector<PMDataManager *, 8>::iterator
559 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000560 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
561 P = (*I)->findAnalysisPass(AID, false);
562
Devang Patel0d29ae02008-08-12 15:44:31 +0000563 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000564 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
565 const PassInfo *PI = (*I)->getPassInfo();
566 if (PI == AID)
567 P = *I;
568
569 // If Pass not found then check the interfaces implemented by Immutable Pass
570 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000571 const std::vector<const PassInfo*> &ImmPI =
572 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000573 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
574 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000575 }
576 }
577
Devang Patelafb1f3622006-12-12 22:35:25 +0000578 return P;
579}
580
Devang Pateleda56172006-12-12 23:34:33 +0000581// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000582void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000583
Devang Patelfd4184322007-01-17 20:33:36 +0000584 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000585 return;
586
Devang Pateleda56172006-12-12 23:34:33 +0000587 // Print out the immutable passes
588 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
589 ImmutablePasses[i]->dumpPassStructure(0);
590 }
591
Dan Gohman73caf5f2008-03-13 01:48:32 +0000592 // Every class that derives from PMDataManager also derives from Pass
593 // (sometimes indirectly), but there's no inheritance relationship
594 // between PMDataManager and Pass, so we have to dynamic_cast to get
595 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000596 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000597 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000598 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000599}
600
Devang Patel991aeba2006-12-15 20:13:01 +0000601void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000602
Devang Patelfd4184322007-01-17 20:33:36 +0000603 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000604 return;
605
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000606 errs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000607 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000608 E = PassManagers.end(); I != E; ++I)
609 (*I)->dumpPassArguments();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000610 errs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000611}
612
Devang Patele3068402006-12-21 00:16:50 +0000613void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000614 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000615 E = PassManagers.end(); I != E; ++I)
616 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000617
618 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000619 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000620 E = IndirectPassManagers.end(); I != E; ++I)
621 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000622
Chris Lattner60987362009-03-06 05:53:14 +0000623 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000624 DME = LastUser.end(); DMI != DME; ++DMI) {
625 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
626 InversedLastUser.find(DMI->second);
627 if (InvDMI != InversedLastUser.end()) {
628 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
629 L.insert(DMI->first);
630 } else {
631 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
632 InversedLastUser[DMI->second] = L;
633 }
634 }
Devang Patele3068402006-12-21 00:16:50 +0000635}
636
Devang Patele7599552007-01-12 18:52:44 +0000637/// Destructor
638PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000639 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000640 E = PassManagers.end(); I != E; ++I)
641 delete *I;
642
Devang Patel0d29ae02008-08-12 15:44:31 +0000643 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000644 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
645 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000646
647 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000648 DME = AnUsageMap.end(); DMI != DME; ++DMI)
649 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000650}
651
Devang Patelafb1f3622006-12-12 22:35:25 +0000652//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000653// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000654
Devang Patel643676c2006-11-11 01:10:19 +0000655/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000656void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000657 const PassInfo *PI = P->getPassInfo();
658 if (PI == 0) return;
659
660 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000661
Chris Lattner60987362009-03-06 05:53:14 +0000662 //This pass is the current implementation of all of the interfaces it
663 //implements as well.
664 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
665 for (unsigned i = 0, e = II.size(); i != e; ++i)
666 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000667}
668
Devang Patel9d9fc902007-03-06 17:52:53 +0000669// Return true if P preserves high level analysis used by other
670// passes managed by this manager
671bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000672 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000673 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000674 return true;
675
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000676 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000677 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000678 E = HigherLevelAnalysis.end(); I != E; ++I) {
679 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000680 if (!dynamic_cast<ImmutablePass*>(P1) &&
681 std::find(PreservedSet.begin(), PreservedSet.end(),
682 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000683 PreservedSet.end())
684 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000685 }
686
687 return true;
688}
689
Chris Lattner02eb94c2008-08-07 07:34:50 +0000690/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000691void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000692 // Don't do this unless assertions are enabled.
693#ifdef NDEBUG
694 return;
695#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000696 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
697 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000698
Devang Patelef432532007-07-19 05:36:09 +0000699 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000700 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000701 E = PreservedSet.end(); I != E; ++I) {
702 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000703 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000704 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000705 }
Devang Patela273d1c2007-07-19 18:02:32 +0000706}
707
Devang Patel9dbe4d12008-07-01 17:44:24 +0000708/// verifyDomInfo - Verify dominator information if it is available.
709void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000710 if (!VerifyDomInfo || !P.getResolver())
711 return;
712
Duncan Sands5a913d62009-01-28 13:14:17 +0000713 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000714 if (!DT)
715 return;
716
717 DominatorTree OtherDT;
718 OtherDT.getBase().recalculate(F);
719 if (DT->compare(OtherDT)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000720 errs() << "Dominator Information for " << F.getName() << "\n";
721 errs() << "Pass '" << P.getPassName() << "'\n";
722 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000723 OtherDT.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000724 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000725 DT->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000726 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000727 }
728
Duncan Sands5a913d62009-01-28 13:14:17 +0000729 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000730 if (!DF)
731 return;
732
733 DominanceFrontier OtherDF;
734 std::vector<BasicBlock*> DTRoots = DT->getRoots();
735 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
736 if (DF->compare(OtherDF)) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000737 errs() << "Dominator Information for " << F.getName() << "\n";
738 errs() << "Pass '" << P.getPassName() << "'\n";
739 errs() << "----- Valid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000740 OtherDF.dump();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000741 errs() << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000742 DF->dump();
Torok Edwinfbcc6632009-07-14 16:55:14 +0000743 llvm_unreachable("Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000744 }
745}
746
Devang Patel67c79a42008-07-01 19:50:56 +0000747/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000748void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000749 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
750 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000751 return;
752
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000753 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000754 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000755 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000756 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000757 if (!dynamic_cast<ImmutablePass*>(Info->second)
758 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000759 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000760 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000761 if (PassDebugging >= Details) {
762 Pass *S = Info->second;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000763 errs() << " -- '" << P->getPassName() << "' is not preserving '";
764 errs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000765 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000766 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000767 }
Devang Patel349170f2006-11-11 01:24:55 +0000768 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000769
770 // Check inherited analysis also. If P is not preserving analysis
771 // provided by parent manager then remove it here.
772 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
773
774 if (!InheritedAnalysis[Index])
775 continue;
776
777 for (std::map<AnalysisID, Pass*>::iterator
778 I = InheritedAnalysis[Index]->begin(),
779 E = InheritedAnalysis[Index]->end(); I != E; ) {
780 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000781 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
782 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000783 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000784 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000785 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000786 }
787 }
Devang Patelf68a3492006-11-07 22:35:17 +0000788}
789
Devang Patelca189262006-11-14 03:05:08 +0000790/// Remove analysis passes that are not used any longer
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000791void PMDataManager::removeDeadPasses(Pass *P, const StringRef &Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000792 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000793
Devang Patel8adae862007-07-20 18:04:54 +0000794 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000795
Devang Patel2ff44922007-04-16 20:39:59 +0000796 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000797 if (!TPM)
798 return;
799
Devang Patel17ad0962006-12-08 00:37:52 +0000800 TPM->collectLastUses(DeadPasses, P);
801
Devang Patel656a9172008-06-06 17:50:36 +0000802 if (PassDebugging >= Details && !DeadPasses.empty()) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000803 errs() << " -*- '" << P->getPassName();
804 errs() << "' is the last user of following pass instances.";
805 errs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000806 }
807
Devang Patel8adae862007-07-20 18:04:54 +0000808 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000809 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000810
Devang Patel003a5592007-03-05 20:01:30 +0000811 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000812
Chris Lattner4c1e9542009-03-06 06:45:05 +0000813 {
814 // If the pass crashes releasing memory, remember this.
815 PassManagerPrettyStackEntry X(*I);
816
817 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
818 (*I)->releaseMemory();
819 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
820 }
Devang Patelc3e3ca92008-10-06 20:36:36 +0000821 if (const PassInfo *PI = (*I)->getPassInfo()) {
822 std::map<AnalysisID, Pass*>::iterator Pos =
823 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000824
Devang Patelc3e3ca92008-10-06 20:36:36 +0000825 // It is possible that pass is already removed from the AvailableAnalysis
826 if (Pos != AvailableAnalysis.end())
827 AvailableAnalysis.erase(Pos);
828
829 // Remove all interfaces this pass implements, for which it is also
830 // listed as the available implementation.
831 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
832 for (unsigned i = 0, e = II.size(); i != e; ++i) {
833 Pos = AvailableAnalysis.find(II[i]);
834 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
835 AvailableAnalysis.erase(Pos);
836 }
837 }
Devang Patel17ad0962006-12-08 00:37:52 +0000838 }
Devang Patelca189262006-11-14 03:05:08 +0000839}
840
Devang Patel8f677ce2006-12-07 18:47:25 +0000841/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000842/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000843void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000844 // This manager is going to manage pass P. Set up analysis resolver
845 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000846 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000847 P->setResolver(AR);
848
Devang Patelec2b9a72007-03-05 22:57:49 +0000849 // If a FunctionPass F is the last user of ModulePass info M
850 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000851 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000852
Chris Lattner60987362009-03-06 05:53:14 +0000853 if (!ProcessAnalysis) {
854 // Add pass
855 PassVector.push_back(P);
856 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000857 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000858
Chris Lattner60987362009-03-06 05:53:14 +0000859 // At the moment, this pass is the last user of all required passes.
860 SmallVector<Pass *, 12> LastUses;
861 SmallVector<Pass *, 8> RequiredPasses;
862 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
863
864 unsigned PDepth = this->getDepth();
865
866 collectRequiredAnalysis(RequiredPasses,
867 ReqAnalysisNotAvailable, P);
868 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
869 E = RequiredPasses.end(); I != E; ++I) {
870 Pass *PRequired = *I;
871 unsigned RDepth = 0;
872
873 assert(PRequired->getResolver() && "Analysis Resolver is not set");
874 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
875 RDepth = DM.getDepth();
876
877 if (PDepth == RDepth)
878 LastUses.push_back(PRequired);
879 else if (PDepth > RDepth) {
880 // Let the parent claim responsibility of last use
881 TransferLastUses.push_back(PRequired);
882 // Keep track of higher level analysis used by this manager.
883 HigherLevelAnalysis.push_back(PRequired);
884 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000885 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000886 }
887
888 // Set P as P's last user until someone starts using P.
889 // However, if P is a Pass Manager then it does not need
890 // to record its last user.
891 if (!dynamic_cast<PMDataManager *>(P))
892 LastUses.push_back(P);
893 TPM->setLastUser(LastUses, P);
894
895 if (!TransferLastUses.empty()) {
896 Pass *My_PM = dynamic_cast<Pass *>(this);
897 TPM->setLastUser(TransferLastUses, My_PM);
898 TransferLastUses.clear();
899 }
900
901 // Now, take care of required analysises that are not available.
902 for (SmallVector<AnalysisID, 8>::iterator
903 I = ReqAnalysisNotAvailable.begin(),
904 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
905 Pass *AnalysisPass = (*I)->createPass();
906 this->addLowerLevelRequiredPass(P, AnalysisPass);
907 }
908
909 // Take a note of analysis required and made available by this pass.
910 // Remove the analysis not preserved by this pass
911 removeNotPreservedAnalysis(P);
912 recordAvailableAnalysis(P);
913
Devang Patel8cad70d2006-11-11 01:51:02 +0000914 // Add pass
915 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000916}
917
Devang Patele64d3052007-04-16 20:12:57 +0000918
919/// Populate RP with analysis pass that are required by
920/// pass P and are available. Populate RP_NotAvail with analysis
921/// pass that are required by pass P but are not available.
922void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
923 SmallVector<AnalysisID, 8> &RP_NotAvail,
924 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000925 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
926 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000927 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000928 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000929 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
930 RP.push_back(AnalysisPass);
931 else
Chris Lattner60987362009-03-06 05:53:14 +0000932 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000933 }
Devang Patelf58183d2006-12-12 23:09:32 +0000934
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000935 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000936 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000937 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000938 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
939 RP.push_back(AnalysisPass);
940 else
Chris Lattner60987362009-03-06 05:53:14 +0000941 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000942 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000943}
944
Devang Patel07f4f582006-11-14 21:49:36 +0000945// All Required analyses should be available to the pass as it runs! Here
946// we fill in the AnalysisImpls member of the pass so that it can
947// successfully use the getAnalysis() method to retrieve the
948// implementations it needs.
949//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000950void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000951 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
952
Chris Lattnercbd160f2008-08-08 05:33:04 +0000953 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000954 I = AnUsage->getRequiredSet().begin(),
955 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000956 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000957 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000958 // This may be analysis pass that is initialized on the fly.
959 // If that is not the case then it will raise an assert when it is used.
960 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000961 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000962 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000963 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000964 }
965}
966
Devang Patel640c5bb2006-12-08 22:30:11 +0000967/// Find the pass that implements Analysis AID. If desired pass is not found
968/// then return NULL.
969Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
970
971 // Check if AvailableAnalysis map has one entry.
972 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
973
974 if (I != AvailableAnalysis.end())
975 return I->second;
976
977 // Search Parents through TopLevelManager
978 if (SearchParent)
979 return TPM->findAnalysisPass(AID);
980
Devang Patel9d759b82006-12-09 00:09:12 +0000981 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000982}
983
Devang Patel991aeba2006-12-15 20:13:01 +0000984// Print list of passes that are last used by P.
985void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
986
Devang Patel8adae862007-07-20 18:04:54 +0000987 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000988
989 // If this is a on the fly manager then it does not have TPM.
990 if (!TPM)
991 return;
992
Devang Patel991aeba2006-12-15 20:13:01 +0000993 TPM->collectLastUses(LUses, P);
994
Devang Patel8adae862007-07-20 18:04:54 +0000995 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000996 E = LUses.end(); I != E; ++I) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000997 llvm::errs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +0000998 (*I)->dumpPassStructure(0);
999 }
1000}
1001
1002void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001003 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001004 E = PassVector.end(); I != E; ++I) {
1005 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1006 PMD->dumpPassArguments();
1007 else
1008 if (const PassInfo *PI = (*I)->getPassInfo())
1009 if (!PI->isAnalysisGroup())
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001010 errs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001011 }
1012}
1013
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001014void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1015 enum PassDebuggingString S2,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001016 const StringRef &Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001017 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001018 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001019 errs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001020 switch (S1) {
1021 case EXECUTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001022 errs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001023 break;
1024 case MODIFICATION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001025 errs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001026 break;
1027 case FREEING_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001028 errs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001029 break;
1030 default:
1031 break;
1032 }
1033 switch (S2) {
1034 case ON_BASICBLOCK_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001035 errs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001036 break;
1037 case ON_FUNCTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001038 errs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001039 break;
1040 case ON_MODULE_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001041 errs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001042 break;
1043 case ON_LOOP_MSG:
Chris Lattnerf82f27b2009-09-15 04:45:26 +00001044 errs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001045 break;
1046 case ON_CG_MSG:
Chris Lattnerf82f27b2009-09-15 04:45:26 +00001047 errs() << "' on Call Graph '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001048 break;
1049 default:
1050 break;
1051 }
Devang Patel991aeba2006-12-15 20:13:01 +00001052}
1053
Chris Lattner4c1e9542009-03-06 06:45:05 +00001054void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001055 if (PassDebugging < Details)
1056 return;
1057
1058 AnalysisUsage analysisUsage;
1059 P->getAnalysisUsage(analysisUsage);
1060 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1061}
1062
Chris Lattner4c1e9542009-03-06 06:45:05 +00001063void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001064 if (PassDebugging < Details)
1065 return;
1066
1067 AnalysisUsage analysisUsage;
1068 P->getAnalysisUsage(analysisUsage);
1069 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1070}
1071
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001072void PMDataManager::dumpAnalysisUsage(const StringRef &Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001073 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001074 assert(PassDebugging >= Details);
1075 if (Set.empty())
1076 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001077 errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001078 for (unsigned i = 0; i != Set.size(); ++i) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001079 if (i) errs() << ",";
1080 errs() << " " << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001081 }
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001082 errs() << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001083}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001084
Devang Patel004937b2007-07-27 20:06:09 +00001085/// Add RequiredPass into list of lower level passes required by pass P.
1086/// RequiredPass is run on the fly by Pass Manager when P requests it
1087/// through getAnalysis interface.
1088/// This should be handled by specific pass manager.
1089void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1090 if (TPM) {
1091 TPM->dumpArguments();
1092 TPM->dumpPasses();
1093 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001094
1095 // Module Level pass may required Function Level analysis info
1096 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1097 // to provide this on demand. In that case, in Pass manager terminology,
1098 // module level pass is requiring lower level analysis info managed by
1099 // lower level pass manager.
1100
1101 // When Pass manager is not able to order required analysis info, Pass manager
1102 // checks whether any lower level manager will be able to provide this
1103 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001104#ifndef NDEBUG
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001105 errs() << "Unable to schedule '" << RequiredPass->getPassName();
1106 errs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001107#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001108 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001109}
1110
Devang Patele7599552007-01-12 18:52:44 +00001111// Destructor
1112PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001113 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001114 E = PassVector.end(); I != E; ++I)
1115 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001116}
1117
Devang Patel9bdf7d42006-12-08 23:28:54 +00001118//===----------------------------------------------------------------------===//
1119// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001120// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1121Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001122 return PM.findAnalysisPass(ID, dir);
1123}
1124
Devang Patel92942812007-04-16 20:56:24 +00001125Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1126 Function &F) {
1127 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1128}
1129
Devang Patela1514cb2006-12-07 19:39:39 +00001130//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001131// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001132
Devang Patel6e5a1132006-11-07 21:31:57 +00001133/// Execute all of the passes scheduled for execution by invoking
1134/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1135/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001136bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001137 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001138 return false;
1139
Devang Patele9585592006-12-08 01:38:28 +00001140 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001141
Devang Patel6e5a1132006-11-07 21:31:57 +00001142 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001143 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1144 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001145
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001146 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001147 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001148
Devang Patelabfbe3b2006-12-16 00:56:26 +00001149 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001150
Chris Lattner4c1e9542009-03-06 06:45:05 +00001151 {
1152 // If the pass crashes, remember this.
1153 PassManagerPrettyStackEntry X(BP, *I);
1154
1155 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1156 Changed |= BP->runOnBasicBlock(*I);
1157 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1158 }
Devang Patel93a197c2006-12-14 00:08:04 +00001159
Devang Patel003a5592007-03-05 20:01:30 +00001160 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001161 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001162 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001163 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001164
Devang Patela273d1c2007-07-19 18:02:32 +00001165 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 removeNotPreservedAnalysis(BP);
1167 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001168 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001169 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001170
Devang Patel56d48ec2006-12-15 22:57:49 +00001171 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001172}
1173
Devang Patel475c4532006-12-08 00:59:05 +00001174// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001175bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001176 bool Changed = false;
1177
Chris Lattner4c1e9542009-03-06 06:45:05 +00001178 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1179 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001180
1181 return Changed;
1182}
1183
Duncan Sands51495602009-02-13 09:42:34 +00001184bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001185 bool Changed = false;
1186
Chris Lattner4c1e9542009-03-06 06:45:05 +00001187 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1188 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001189
1190 return Changed;
1191}
1192
Duncan Sands51495602009-02-13 09:42:34 +00001193bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001194 bool Changed = false;
1195
Devang Patelabfbe3b2006-12-16 00:56:26 +00001196 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1197 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001198 Changed |= BP->doInitialization(F);
1199 }
1200
1201 return Changed;
1202}
1203
Duncan Sands51495602009-02-13 09:42:34 +00001204bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001205 bool Changed = false;
1206
Devang Patelabfbe3b2006-12-16 00:56:26 +00001207 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1208 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001209 Changed |= BP->doFinalization(F);
1210 }
1211
1212 return Changed;
1213}
1214
1215
Devang Patela1514cb2006-12-07 19:39:39 +00001216//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001217// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001218
Devang Patel4e12f862006-11-08 10:44:40 +00001219/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001220FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001221 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001222 // FPM is the top level manager.
1223 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001224
Dan Gohman565df952008-03-13 02:08:36 +00001225 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001226 FPM->setResolver(AR);
1227
Devang Patel1f653682006-12-08 18:57:16 +00001228 MP = P;
1229}
1230
Devang Patelb67904d2006-12-13 02:36:01 +00001231FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001232 delete FPM;
1233}
1234
Devang Patel4e12f862006-11-08 10:44:40 +00001235/// add - Add a pass to the queue of passes to run. This passes
1236/// ownership of the Pass to the PassManager. When the
1237/// PassManager_X is destroyed, the pass will be destroyed as well, so
1238/// there is no need to delete the pass. (TODO delete passes.)
1239/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001240void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001241 FPM->add(P);
1242}
1243
Devang Patel9f3083e2006-11-15 19:39:54 +00001244/// run - Execute all of the passes scheduled for execution. Keep
1245/// track of whether any of the passes modifies the function, and if
1246/// so, return true.
1247///
Devang Patelb67904d2006-12-13 02:36:01 +00001248bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001249 std::string errstr;
1250 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001251 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001252 }
Devang Patel272908d2006-12-08 22:57:48 +00001253 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001254}
1255
1256
Devang Patelff631ae2006-11-15 01:27:05 +00001257/// doInitialization - Run all of the initializers for the function passes.
1258///
Devang Patelb67904d2006-12-13 02:36:01 +00001259bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001260 return FPM->doInitialization(*MP->getModule());
1261}
1262
Dan Gohmane6656eb2007-07-30 14:51:13 +00001263/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001264///
Devang Patelb67904d2006-12-13 02:36:01 +00001265bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001266 return FPM->doFinalization(*MP->getModule());
1267}
1268
Devang Patela1514cb2006-12-07 19:39:39 +00001269//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001270// FunctionPassManagerImpl implementation
1271//
Duncan Sands51495602009-02-13 09:42:34 +00001272bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001273 bool Changed = false;
1274
Chris Lattner4c1e9542009-03-06 06:45:05 +00001275 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1276 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001277
1278 return Changed;
1279}
1280
Duncan Sands51495602009-02-13 09:42:34 +00001281bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001282 bool Changed = false;
1283
Chris Lattner4c1e9542009-03-06 06:45:05 +00001284 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1285 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001286
1287 return Changed;
1288}
1289
Devang Patelec9c58f2009-04-01 22:34:41 +00001290/// cleanup - After running all passes, clean up pass manager cache.
1291void FPPassManager::cleanup() {
1292 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1293 FunctionPass *FP = getContainedPass(Index);
1294 AnalysisResolver *AR = FP->getResolver();
1295 assert(AR && "Analysis Resolver is not set");
1296 AR->clearAnalysisImpls();
1297 }
1298}
1299
Torok Edwin24c78352009-06-29 18:49:09 +00001300void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1301 if (!wasRun)
1302 return;
1303 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1304 FPPassManager *FPPM = getContainedManager(Index);
1305 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1306 FPPM->getContainedPass(Index)->releaseMemory();
1307 }
1308 }
Torok Edwin896556e2009-06-29 21:05:10 +00001309 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001310}
1311
Devang Patel67d6a5e2006-12-19 19:46:59 +00001312// Execute all the passes managed by this top level manager.
1313// Return true if any function is modified by a pass.
1314bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001315 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001316 TimingInfo::createTheTimeInfo();
1317
1318 dumpArguments();
1319 dumpPasses();
1320
Devang Patele3068402006-12-21 00:16:50 +00001321 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001322 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1323 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001324
1325 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1326 getContainedManager(Index)->cleanup();
1327
Torok Edwin24c78352009-06-29 18:49:09 +00001328 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001329 return Changed;
1330}
1331
1332//===----------------------------------------------------------------------===//
1333// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001334
Devang Patel8c78a0b2007-05-03 01:11:54 +00001335char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001336/// Print passes managed by this manager
1337void FPPassManager::dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001338 llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001339 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1340 FunctionPass *FP = getContainedPass(Index);
1341 FP->dumpPassStructure(Offset + 1);
1342 dumpLastUses(FP, Offset+1);
1343 }
1344}
1345
1346
Devang Patel0c2012f2006-11-07 21:49:50 +00001347/// Execute all of the passes scheduled for execution by invoking
1348/// runOnFunction method. Keep track of whether any of the passes modifies
1349/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001350bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001351 if (F.isDeclaration())
1352 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001353
1354 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001355
Devang Patelcbbf2912008-03-20 01:09:53 +00001356 // Collect inherited analysis from Module level pass manager.
1357 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001358
Devang Patelabfbe3b2006-12-16 00:56:26 +00001359 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1360 FunctionPass *FP = getContainedPass(Index);
1361
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001362 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001363 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001364
Devang Patelabfbe3b2006-12-16 00:56:26 +00001365 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001366
Chris Lattner4c1e9542009-03-06 06:45:05 +00001367 {
1368 PassManagerPrettyStackEntry X(FP, F);
1369
1370 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1371 Changed |= FP->runOnFunction(F);
1372 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1373 }
Devang Patel93a197c2006-12-14 00:08:04 +00001374
Devang Patel003a5592007-03-05 20:01:30 +00001375 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001376 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001377 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001378
Devang Patela273d1c2007-07-19 18:02:32 +00001379 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001380 removeNotPreservedAnalysis(FP);
1381 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001382 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001383
Devang Patel67c79a42008-07-01 19:50:56 +00001384 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001385 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001386 }
1387 return Changed;
1388}
1389
Devang Patel67d6a5e2006-12-19 19:46:59 +00001390bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001391 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001392
Chris Lattner60987362009-03-06 05:53:14 +00001393 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1394 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001395
1396 return Changed |= doFinalization(M);
1397}
1398
Duncan Sands51495602009-02-13 09:42:34 +00001399bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001400 bool Changed = false;
1401
Chris Lattner4c1e9542009-03-06 06:45:05 +00001402 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1403 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001404
1405 return Changed;
1406}
1407
Duncan Sands51495602009-02-13 09:42:34 +00001408bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001409 bool Changed = false;
1410
Chris Lattner4c1e9542009-03-06 06:45:05 +00001411 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1412 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001413
Devang Patelff631ae2006-11-15 01:27:05 +00001414 return Changed;
1415}
1416
Devang Patela1514cb2006-12-07 19:39:39 +00001417//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001418// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001419
Devang Patel05e1a972006-11-07 22:03:15 +00001420/// Execute all of the passes scheduled for execution by invoking
1421/// runOnModule method. Keep track of whether any of the passes modifies
1422/// the module, and if so, return true.
1423bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001425 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001426
Torok Edwin24c78352009-06-29 18:49:09 +00001427 // Initialize on-the-fly passes
1428 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1429 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1430 I != E; ++I) {
1431 FunctionPassManagerImpl *FPP = I->second;
1432 Changed |= FPP->doInitialization(M);
1433 }
1434
Devang Patelabfbe3b2006-12-16 00:56:26 +00001435 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1436 ModulePass *MP = getContainedPass(Index);
1437
Dan Gohman929391a2008-01-29 12:09:55 +00001438 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1439 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001440 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001441
Devang Patelabfbe3b2006-12-16 00:56:26 +00001442 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001443
Chris Lattner4c1e9542009-03-06 06:45:05 +00001444 {
1445 PassManagerPrettyStackEntry X(MP, M);
1446 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1447 Changed |= MP->runOnModule(M);
1448 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1449 }
Devang Patel93a197c2006-12-14 00:08:04 +00001450
Devang Patel003a5592007-03-05 20:01:30 +00001451 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001452 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1453 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001454 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001455
Devang Patela273d1c2007-07-19 18:02:32 +00001456 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001457 removeNotPreservedAnalysis(MP);
1458 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001459 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001460 }
Torok Edwin24c78352009-06-29 18:49:09 +00001461
1462 // Finalize on-the-fly passes
1463 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1464 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1465 I != E; ++I) {
1466 FunctionPassManagerImpl *FPP = I->second;
1467 // We don't know when is the last time an on-the-fly pass is run,
1468 // so we need to releaseMemory / finalize here
1469 FPP->releaseMemoryOnTheFly();
1470 Changed |= FPP->doFinalization(M);
1471 }
Devang Patel05e1a972006-11-07 22:03:15 +00001472 return Changed;
1473}
1474
Devang Patele64d3052007-04-16 20:12:57 +00001475/// Add RequiredPass into list of lower level passes required by pass P.
1476/// RequiredPass is run on the fly by Pass Manager when P requests it
1477/// through getAnalysis interface.
1478void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001479 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1480 "Unable to handle Pass that requires lower level Analysis pass");
1481 assert((P->getPotentialPassManagerType() <
1482 RequiredPass->getPotentialPassManagerType()) &&
1483 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001484
Devang Patel68f72b12007-04-26 17:50:19 +00001485 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001486 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001487 FPP = new FunctionPassManagerImpl(0);
1488 // FPP is the top level manager.
1489 FPP->setTopLevelManager(FPP);
1490
Devang Patel69e9f6d2007-04-16 20:27:05 +00001491 OnTheFlyManagers[P] = FPP;
1492 }
Devang Patel68f72b12007-04-26 17:50:19 +00001493 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001494
Devang Patel68f72b12007-04-26 17:50:19 +00001495 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001496 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001497 LU.push_back(RequiredPass);
1498 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001499}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001500
1501/// Return function pass corresponding to PassInfo PI, that is
1502/// required by module pass MP. Instantiate analysis pass, by using
1503/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001504Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001505 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001506 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001507
Torok Edwin24c78352009-06-29 18:49:09 +00001508 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001509 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001510 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001511}
1512
1513
Devang Patela1514cb2006-12-07 19:39:39 +00001514//===----------------------------------------------------------------------===//
1515// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001516//
Devang Patelc290c8a2006-11-07 22:23:34 +00001517/// run - Execute all of the passes scheduled for execution. Keep track of
1518/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001519bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001520 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001521 TimingInfo::createTheTimeInfo();
1522
Devang Patelcfd70c42006-12-13 22:10:00 +00001523 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001524 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001525
Devang Patele3068402006-12-21 00:16:50 +00001526 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001527 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1528 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001529 return Changed;
1530}
Devang Patel376fefa2006-11-08 10:29:57 +00001531
Devang Patela1514cb2006-12-07 19:39:39 +00001532//===----------------------------------------------------------------------===//
1533// PassManager implementation
1534
Devang Patel376fefa2006-11-08 10:29:57 +00001535/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001536PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001538 // PM is the top level manager
1539 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001540}
1541
Devang Patelb67904d2006-12-13 02:36:01 +00001542PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001543 delete PM;
1544}
1545
Devang Patel376fefa2006-11-08 10:29:57 +00001546/// add - Add a pass to the queue of passes to run. This passes ownership of
1547/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1548/// will be destroyed as well, so there is no need to delete the pass. This
1549/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001550void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001551 PM->add(P);
1552}
1553
1554/// run - Execute all of the passes scheduled for execution. Keep track of
1555/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001556bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001557 return PM->run(M);
1558}
1559
Devang Patelb8817b92006-12-14 00:59:42 +00001560//===----------------------------------------------------------------------===//
1561// TimingInfo Class - This class is used to calculate information about the
1562// amount of time each pass takes to execute. This only happens with
1563// -time-passes is enabled on the command line.
1564//
1565bool llvm::TimePassesIsEnabled = false;
1566static cl::opt<bool,true>
1567EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1568 cl::desc("Time each pass, printing elapsed time for each on exit"));
1569
1570// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1571// a non null value (if the -time-passes option is enabled) or it leaves it
1572// null. It may be called multiple times.
1573void TimingInfo::createTheTimeInfo() {
1574 if (!TimePassesIsEnabled || TheTimeInfo) return;
1575
1576 // Constructed the first time this is called, iff -time-passes is enabled.
1577 // This guarantees that the object will be constructed before static globals,
1578 // thus it will be destroyed before them.
1579 static ManagedStatic<TimingInfo> TTI;
1580 TheTimeInfo = &*TTI;
1581}
1582
Devang Patel1c3633e2007-01-29 23:10:37 +00001583/// If TimingInfo is enabled then start pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001584void llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001585 if (TheTimeInfo)
1586 TheTimeInfo->passStarted(P);
1587}
1588
1589/// If TimingInfo is enabled then stop pass timer.
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001590void llvm::StopPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001591 if (TheTimeInfo)
1592 TheTimeInfo->passEnded(P);
1593}
1594
Devang Patel1c56a632007-01-08 19:29:38 +00001595//===----------------------------------------------------------------------===//
1596// PMStack implementation
1597//
Devang Patelad98d232007-01-11 22:15:30 +00001598
Devang Patel1c56a632007-01-08 19:29:38 +00001599// Pop Pass Manager from the stack and clear its analysis info.
1600void PMStack::pop() {
1601
1602 PMDataManager *Top = this->top();
1603 Top->initializeAnalysisInfo();
1604
1605 S.pop_back();
1606}
1607
1608// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001609void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001610 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001611
Chris Lattner60987362009-03-06 05:53:14 +00001612 if (!this->empty()) {
1613 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001614
Chris Lattner60987362009-03-06 05:53:14 +00001615 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001616 TPM->addIndirectPassManager(PM);
1617 PM->setTopLevelManager(TPM);
1618 }
1619
Devang Patel15701b52007-01-11 00:19:00 +00001620 S.push_back(PM);
1621}
1622
1623// Dump content of the pass manager stack.
1624void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001625 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1626 E = S.end(); I != E; ++I)
1627 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1628
Devang Patel15701b52007-01-11 00:19:00 +00001629 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001630 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001631}
1632
Devang Patel1c56a632007-01-08 19:29:38 +00001633/// Find appropriate Module Pass Manager in the PM Stack and
1634/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001635void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001636 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001637 // Find Module Pass Manager
1638 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001639 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1640 if (TopPMType == PreferredType)
1641 break; // We found desired pass manager
1642 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001643 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001644 else
1645 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001646 }
Devang Patel18ff6362008-09-09 21:38:40 +00001647 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001648 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001649}
1650
Devang Patel3312f752007-01-16 21:43:18 +00001651/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1652/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001653void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001654 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001655
Devang Patela3286902008-09-09 17:56:50 +00001656 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001657 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001658 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1659 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001660 else
Devang Patel3312f752007-01-16 21:43:18 +00001661 break;
1662 }
1663 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1664
1665 // Create new Function Pass Manager
1666 if (!FPP) {
1667 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1668 PMDataManager *PMD = PMS.top();
1669
1670 // [1] Create new Function Pass Manager
1671 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001672 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001673
1674 // [2] Set up new manager's top level manager
1675 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1676 TPM->addIndirectPassManager(FPP);
1677
1678 // [3] Assign manager to manage this new manager. This may create
1679 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001680 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001681
1682 // [4] Push new manager into PMS
1683 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001684 }
1685
Devang Patel3312f752007-01-16 21:43:18 +00001686 // Assign FPP as the manager of this pass.
1687 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001688}
1689
Devang Patel3312f752007-01-16 21:43:18 +00001690/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001691/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001692void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001693 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001694 BBPassManager *BBP = NULL;
1695
Devang Patel15701b52007-01-11 00:19:00 +00001696 // Basic Pass Manager is a leaf pass manager. It does not handle
1697 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001698 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001699 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001700
Devang Patel3312f752007-01-16 21:43:18 +00001701 // If leaf manager is not Basic Block Pass manager then create new
1702 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001703
Devang Patel3312f752007-01-16 21:43:18 +00001704 if (!BBP) {
1705 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1706 PMDataManager *PMD = PMS.top();
1707
1708 // [1] Create new Basic Block Manager
1709 BBP = new BBPassManager(PMD->getDepth() + 1);
1710
1711 // [2] Set up new manager's top level manager
1712 // Basic Block Pass Manager does not live by itself
1713 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1714 TPM->addIndirectPassManager(BBP);
1715
Devang Patel15701b52007-01-11 00:19:00 +00001716 // [3] Assign manager to manage this new manager. This may create
1717 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001718 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001719
Devang Patel3312f752007-01-16 21:43:18 +00001720 // [4] Push new manager into PMS
1721 PMS.push(BBP);
1722 }
Devang Patel1c56a632007-01-08 19:29:38 +00001723
Devang Patel3312f752007-01-16 21:43:18 +00001724 // Assign BBP as the manager of this pass.
1725 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001726}
1727
Dan Gohmand3a20c92008-03-11 16:41:42 +00001728PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001729
1730/*===-- C Bindings --------------------------------------------------------===*/
1731
1732LLVMPassManagerRef LLVMCreatePassManager() {
1733 return wrap(new PassManager());
1734}
1735
1736LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1737 return wrap(new FunctionPassManager(unwrap(P)));
1738}
1739
1740int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1741 return unwrap<PassManager>(PM)->run(*unwrap(M));
1742}
1743
1744int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1745 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1746}
1747
1748int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1749 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1750}
1751
1752int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1753 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1754}
1755
1756void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1757 delete unwrap(PM);
1758}