blob: 24fb35c215c80f720f3050cc20a36e8a02c404c2 [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"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.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) {
Devang Patelffca9102006-12-15 19:39:30 +0000137 llvm::cerr << 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) {
277 llvm::cerr << 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);
Devang Patel68f72b12007-04-26 17:50:19 +0000281 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000282 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000283 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000284 }
285 }
286
Devang Patelabfbe3b2006-12-16 00:56:26 +0000287 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000288 assert(N < PassVector.size() && "Pass number out of range!");
289 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000290 }
291
Devang Patel28349ab2007-02-27 15:00:39 +0000292 virtual PassManagerType getPassManagerType() const {
293 return PMT_ModulePassManager;
294 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000295
296 private:
297 /// Collection of on the fly FPPassManagers. These managers manage
298 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000299 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000300};
301
Devang Patel8c78a0b2007-05-03 01:11:54 +0000302char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000303//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000304// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000305//
Devang Patel09f162c2007-05-01 21:15:47 +0000306
Devang Patel67d6a5e2006-12-19 19:46:59 +0000307/// PassManagerImpl manages MPPassManagers
308class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000309 public PMDataManager,
310 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000311
312public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000313 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000314 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000315 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000316
Devang Patel376fefa2006-11-08 10:29:57 +0000317 /// add - Add a pass to the queue of passes to run. This passes ownership of
318 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
319 /// will be destroyed as well, so there is no need to delete the pass. This
320 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000321 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000322 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000323 }
Devang Patel376fefa2006-11-08 10:29:57 +0000324
325 /// run - Execute all of the passes scheduled for execution. Keep track of
326 /// whether any of the passes modifies the module, and if so, return true.
327 bool run(Module &M);
328
Devang Patelf9d96b92006-12-07 19:57:52 +0000329 /// Pass Manager itself does not invalidate any analysis info.
330 void getAnalysisUsage(AnalysisUsage &Info) const {
331 Info.setPreservesAll();
332 }
333
Devang Patelabcd1d32006-12-07 21:27:23 +0000334 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000335 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000336
337 // P is a immutable pass and it will be managed by this
338 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000339 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000340 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000341 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000342 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000343 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000344 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000345 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000346 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000347 }
348
Devang Patel67d6a5e2006-12-19 19:46:59 +0000349 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000350 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000351 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
352 return MP;
353 }
Devang Patel376fefa2006-11-08 10:29:57 +0000354};
355
Devang Patel8c78a0b2007-05-03 01:11:54 +0000356char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000357} // End of llvm namespace
358
359namespace {
360
361//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000362/// TimingInfo Class - This class is used to calculate information about the
363/// amount of time each pass takes to execute. This only happens when
364/// -time-passes is enabled on the command line.
365///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000366
Owen Anderson5a6960f2009-06-18 20:51:00 +0000367static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000368
Devang Patel1c3633e2007-01-29 23:10:37 +0000369class VISIBILITY_HIDDEN TimingInfo {
370 std::map<Pass*, Timer> TimingData;
371 TimerGroup TG;
372
373public:
374 // Use 'create' member to get this.
375 TimingInfo() : TG("... Pass execution timing report ...") {}
376
377 // TimingDtor - Print out information about timing information
378 ~TimingInfo() {
379 // Delete all of the timers...
380 TimingData.clear();
381 // TimerGroup is deleted next, printing the report.
382 }
383
384 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
385 // to a non null value (if the -time-passes option is enabled) or it leaves it
386 // null. It may be called multiple times.
387 static void createTheTimeInfo();
388
389 void passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000390 if (dynamic_cast<PMDataManager *>(P))
391 return;
392
Owen Anderson5a6960f2009-06-18 20:51:00 +0000393 sys::SmartScopedLock<true> Lock(&*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000394 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
395 if (I == TimingData.end())
396 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
397 I->second.startTimer();
398 }
Owen Anderson5a6960f2009-06-18 20:51:00 +0000399
Devang Patel1c3633e2007-01-29 23:10:37 +0000400 void passEnded(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000401 if (dynamic_cast<PMDataManager *>(P))
402 return;
403
Owen Anderson5a6960f2009-06-18 20:51:00 +0000404 sys::SmartScopedLock<true> Lock(&*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000405 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
Chris Lattner60987362009-03-06 05:53:14 +0000406 assert(I != TimingData.end() && "passStarted/passEnded not nested right!");
Devang Patel1c3633e2007-01-29 23:10:37 +0000407 I->second.stopTimer();
408 }
409};
410
Devang Patel1c3633e2007-01-29 23:10:37 +0000411} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000412
Dan Gohmand78c4002008-05-13 00:00:25 +0000413static TimingInfo *TheTimeInfo;
414
Devang Patela1514cb2006-12-07 19:39:39 +0000415//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000416// PMTopLevelManager implementation
417
Devang Patel4268fc02007-01-16 02:00:38 +0000418/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000419PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000420 if (t == TLM_Pass) {
421 MPPassManager *MPP = new MPPassManager(1);
422 MPP->setTopLevelManager(this);
423 addPassManager(MPP);
424 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000425 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000426 FPPassManager *FPP = new FPPassManager(1);
427 FPP->setTopLevelManager(this);
428 addPassManager(FPP);
429 activeStack.push(FPP);
430 }
431}
432
Devang Patelafb1f3622006-12-12 22:35:25 +0000433/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000434void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000435 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000436 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000437 E = AnalysisPasses.end(); I != E; ++I) {
438 Pass *AP = *I;
439 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000440
441 if (P == AP)
442 continue;
443
Devang Patelafb1f3622006-12-12 22:35:25 +0000444 // If AP is the last user of other passes then make P last user of
445 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000446 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000447 LUE = LastUser.end(); LUI != LUE; ++LUI) {
448 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000449 // DenseMap iterator is not invalidated here because
450 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000451 LastUser[LUI->first] = P;
452 }
453 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000454}
455
456/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000457void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000458 Pass *P) {
459 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
460 InversedLastUser.find(P);
461 if (DMI == InversedLastUser.end())
462 return;
463
464 SmallPtrSet<Pass *, 8> &LU = DMI->second;
465 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
466 E = LU.end(); I != E; ++I) {
467 LastUses.push_back(*I);
468 }
469
Devang Patelafb1f3622006-12-12 22:35:25 +0000470}
471
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000472AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
473 AnalysisUsage *AnUsage = NULL;
474 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
475 if (DMI != AnUsageMap.end())
476 AnUsage = DMI->second;
477 else {
478 AnUsage = new AnalysisUsage();
479 P->getAnalysisUsage(*AnUsage);
480 AnUsageMap[P] = AnUsage;
481 }
482 return AnUsage;
483}
484
Devang Patelafb1f3622006-12-12 22:35:25 +0000485/// Schedule pass P for execution. Make sure that passes required by
486/// P are run before P is run. Update analysis info maintained by
487/// the manager. Remove dead passes. This is a recursive function.
488void PMTopLevelManager::schedulePass(Pass *P) {
489
Devang Patel3312f752007-01-16 21:43:18 +0000490 // TODO : Allocate function manager for this pass, other wise required set
491 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000492
Devang Pateld74ede72007-03-06 01:06:16 +0000493 // Give pass a chance to prepare the stage.
494 P->preparePassManager(activeStack);
495
Devang Patel864970e2008-03-18 00:39:19 +0000496 // If P is an analysis pass and it is available then do not
497 // generate the analysis again. Stale analysis info should not be
498 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000499 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000500 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
501 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000502 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000503 }
Devang Patel864970e2008-03-18 00:39:19 +0000504
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000505 AnalysisUsage *AnUsage = findAnalysisUsage(P);
506
Devang Patelfdee7032008-08-14 23:07:48 +0000507 bool checkAnalysis = true;
508 while (checkAnalysis) {
509 checkAnalysis = false;
510
511 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
512 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
513 E = RequiredSet.end(); I != E; ++I) {
514
515 Pass *AnalysisPass = findAnalysisPass(*I);
516 if (!AnalysisPass) {
517 AnalysisPass = (*I)->createPass();
518 if (P->getPotentialPassManagerType () ==
519 AnalysisPass->getPotentialPassManagerType())
520 // Schedule analysis pass that is managed by the same pass manager.
521 schedulePass(AnalysisPass);
522 else if (P->getPotentialPassManagerType () >
523 AnalysisPass->getPotentialPassManagerType()) {
524 // Schedule analysis pass that is managed by a new manager.
525 schedulePass(AnalysisPass);
526 // Recheck analysis passes to ensure that required analysises that
527 // are already checked are still available.
528 checkAnalysis = true;
529 }
530 else
531 // Do not schedule this analysis. Lower level analsyis
532 // passes are run on the fly.
533 delete AnalysisPass;
534 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000535 }
536 }
537
538 // Now all required passes are available.
539 addTopLevelPass(P);
540}
541
542/// Find the pass that implements Analysis AID. Search immutable
543/// passes and all pass managers. If desired pass is not found
544/// then return NULL.
545Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
546
547 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000548 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000549 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000550 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000551 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000552 P = PMD->findAnalysisPass(AID, false);
553 }
554
555 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000556 for (SmallVector<PMDataManager *, 8>::iterator
557 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000558 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
559 P = (*I)->findAnalysisPass(AID, false);
560
Devang Patel0d29ae02008-08-12 15:44:31 +0000561 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000562 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
563 const PassInfo *PI = (*I)->getPassInfo();
564 if (PI == AID)
565 P = *I;
566
567 // If Pass not found then check the interfaces implemented by Immutable Pass
568 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000569 const std::vector<const PassInfo*> &ImmPI =
570 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000571 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
572 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000573 }
574 }
575
Devang Patelafb1f3622006-12-12 22:35:25 +0000576 return P;
577}
578
Devang Pateleda56172006-12-12 23:34:33 +0000579// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000580void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000581
Devang Patelfd4184322007-01-17 20:33:36 +0000582 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000583 return;
584
Devang Pateleda56172006-12-12 23:34:33 +0000585 // Print out the immutable passes
586 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
587 ImmutablePasses[i]->dumpPassStructure(0);
588 }
589
Dan Gohman73caf5f2008-03-13 01:48:32 +0000590 // Every class that derives from PMDataManager also derives from Pass
591 // (sometimes indirectly), but there's no inheritance relationship
592 // between PMDataManager and Pass, so we have to dynamic_cast to get
593 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000594 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000595 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000596 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000597}
598
Devang Patel991aeba2006-12-15 20:13:01 +0000599void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000600
Devang Patelfd4184322007-01-17 20:33:36 +0000601 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000602 return;
603
604 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000605 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000606 E = PassManagers.end(); I != E; ++I)
607 (*I)->dumpPassArguments();
Devang Patelcfd70c42006-12-13 22:10:00 +0000608 cerr << "\n";
609}
610
Devang Patele3068402006-12-21 00:16:50 +0000611void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000612 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000613 E = PassManagers.end(); I != E; ++I)
614 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000615
616 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000617 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000618 E = IndirectPassManagers.end(); I != E; ++I)
619 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000620
Chris Lattner60987362009-03-06 05:53:14 +0000621 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000622 DME = LastUser.end(); DMI != DME; ++DMI) {
623 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
624 InversedLastUser.find(DMI->second);
625 if (InvDMI != InversedLastUser.end()) {
626 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
627 L.insert(DMI->first);
628 } else {
629 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
630 InversedLastUser[DMI->second] = L;
631 }
632 }
Devang Patele3068402006-12-21 00:16:50 +0000633}
634
Devang Patele7599552007-01-12 18:52:44 +0000635/// Destructor
636PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000637 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000638 E = PassManagers.end(); I != E; ++I)
639 delete *I;
640
Devang Patel0d29ae02008-08-12 15:44:31 +0000641 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000642 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
643 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000644
645 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000646 DME = AnUsageMap.end(); DMI != DME; ++DMI)
647 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000648}
649
Devang Patelafb1f3622006-12-12 22:35:25 +0000650//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000651// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000652
Devang Patel643676c2006-11-11 01:10:19 +0000653/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000654void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000655 const PassInfo *PI = P->getPassInfo();
656 if (PI == 0) return;
657
658 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000659
Chris Lattner60987362009-03-06 05:53:14 +0000660 //This pass is the current implementation of all of the interfaces it
661 //implements as well.
662 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
663 for (unsigned i = 0, e = II.size(); i != e; ++i)
664 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000665}
666
Devang Patel9d9fc902007-03-06 17:52:53 +0000667// Return true if P preserves high level analysis used by other
668// passes managed by this manager
669bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000670 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000671 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000672 return true;
673
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000674 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000675 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000676 E = HigherLevelAnalysis.end(); I != E; ++I) {
677 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000678 if (!dynamic_cast<ImmutablePass*>(P1) &&
679 std::find(PreservedSet.begin(), PreservedSet.end(),
680 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000681 PreservedSet.end())
682 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000683 }
684
685 return true;
686}
687
Chris Lattner02eb94c2008-08-07 07:34:50 +0000688/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000689void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000690 // Don't do this unless assertions are enabled.
691#ifdef NDEBUG
692 return;
693#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000694 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
695 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000696
Devang Patelef432532007-07-19 05:36:09 +0000697 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000698 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000699 E = PreservedSet.end(); I != E; ++I) {
700 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000701 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000702 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000703 }
Devang Patela273d1c2007-07-19 18:02:32 +0000704}
705
Devang Patel9dbe4d12008-07-01 17:44:24 +0000706/// verifyDomInfo - Verify dominator information if it is available.
707void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000708 if (!VerifyDomInfo || !P.getResolver())
709 return;
710
Duncan Sands5a913d62009-01-28 13:14:17 +0000711 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000712 if (!DT)
713 return;
714
715 DominatorTree OtherDT;
716 OtherDT.getBase().recalculate(F);
717 if (DT->compare(OtherDT)) {
718 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000719 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000720 cerr << "----- Valid -----\n";
721 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000722 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000723 DT->dump();
Chris Lattner60987362009-03-06 05:53:14 +0000724 assert(0 && "Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000725 }
726
Duncan Sands5a913d62009-01-28 13:14:17 +0000727 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000728 if (!DF)
729 return;
730
731 DominanceFrontier OtherDF;
732 std::vector<BasicBlock*> DTRoots = DT->getRoots();
733 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
734 if (DF->compare(OtherDF)) {
735 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000736 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000737 cerr << "----- Valid -----\n";
738 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000739 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000740 DF->dump();
Chris Lattner60987362009-03-06 05:53:14 +0000741 assert(0 && "Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000742 }
743}
744
Devang Patel67c79a42008-07-01 19:50:56 +0000745/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000746void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000747 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
748 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000749 return;
750
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000751 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000752 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000753 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000754 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000755 if (!dynamic_cast<ImmutablePass*>(Info->second)
756 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000757 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000758 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000759 if (PassDebugging >= Details) {
760 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000761 cerr << " -- '" << P->getPassName() << "' is not preserving '";
762 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000763 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000764 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000765 }
Devang Patel349170f2006-11-11 01:24:55 +0000766 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000767
768 // Check inherited analysis also. If P is not preserving analysis
769 // provided by parent manager then remove it here.
770 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
771
772 if (!InheritedAnalysis[Index])
773 continue;
774
775 for (std::map<AnalysisID, Pass*>::iterator
776 I = InheritedAnalysis[Index]->begin(),
777 E = InheritedAnalysis[Index]->end(); I != E; ) {
778 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000779 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
780 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000781 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000782 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000783 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000784 }
785 }
Devang Patelf68a3492006-11-07 22:35:17 +0000786}
787
Devang Patelca189262006-11-14 03:05:08 +0000788/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000789void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000790 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000791
Devang Patel8adae862007-07-20 18:04:54 +0000792 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000793
Devang Patel2ff44922007-04-16 20:39:59 +0000794 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000795 if (!TPM)
796 return;
797
Devang Patel17ad0962006-12-08 00:37:52 +0000798 TPM->collectLastUses(DeadPasses, P);
799
Devang Patel656a9172008-06-06 17:50:36 +0000800 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000801 cerr << " -*- '" << P->getPassName();
802 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000803 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000804 }
805
Devang Patel8adae862007-07-20 18:04:54 +0000806 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000807 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000808
Devang Patel003a5592007-03-05 20:01:30 +0000809 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000810
Chris Lattner4c1e9542009-03-06 06:45:05 +0000811 {
812 // If the pass crashes releasing memory, remember this.
813 PassManagerPrettyStackEntry X(*I);
814
815 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
816 (*I)->releaseMemory();
817 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
818 }
Devang Patelc3e3ca92008-10-06 20:36:36 +0000819 if (const PassInfo *PI = (*I)->getPassInfo()) {
820 std::map<AnalysisID, Pass*>::iterator Pos =
821 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000822
Devang Patelc3e3ca92008-10-06 20:36:36 +0000823 // It is possible that pass is already removed from the AvailableAnalysis
824 if (Pos != AvailableAnalysis.end())
825 AvailableAnalysis.erase(Pos);
826
827 // Remove all interfaces this pass implements, for which it is also
828 // listed as the available implementation.
829 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
830 for (unsigned i = 0, e = II.size(); i != e; ++i) {
831 Pos = AvailableAnalysis.find(II[i]);
832 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
833 AvailableAnalysis.erase(Pos);
834 }
835 }
Devang Patel17ad0962006-12-08 00:37:52 +0000836 }
Devang Patelca189262006-11-14 03:05:08 +0000837}
838
Devang Patel8f677ce2006-12-07 18:47:25 +0000839/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000840/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000841void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000842 // This manager is going to manage pass P. Set up analysis resolver
843 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000844 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000845 P->setResolver(AR);
846
Devang Patelec2b9a72007-03-05 22:57:49 +0000847 // If a FunctionPass F is the last user of ModulePass info M
848 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000849 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000850
Chris Lattner60987362009-03-06 05:53:14 +0000851 if (!ProcessAnalysis) {
852 // Add pass
853 PassVector.push_back(P);
854 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000855 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000856
Chris Lattner60987362009-03-06 05:53:14 +0000857 // At the moment, this pass is the last user of all required passes.
858 SmallVector<Pass *, 12> LastUses;
859 SmallVector<Pass *, 8> RequiredPasses;
860 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
861
862 unsigned PDepth = this->getDepth();
863
864 collectRequiredAnalysis(RequiredPasses,
865 ReqAnalysisNotAvailable, P);
866 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
867 E = RequiredPasses.end(); I != E; ++I) {
868 Pass *PRequired = *I;
869 unsigned RDepth = 0;
870
871 assert(PRequired->getResolver() && "Analysis Resolver is not set");
872 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
873 RDepth = DM.getDepth();
874
875 if (PDepth == RDepth)
876 LastUses.push_back(PRequired);
877 else if (PDepth > RDepth) {
878 // Let the parent claim responsibility of last use
879 TransferLastUses.push_back(PRequired);
880 // Keep track of higher level analysis used by this manager.
881 HigherLevelAnalysis.push_back(PRequired);
882 } else
883 assert(0 && "Unable to accomodate Required Pass");
884 }
885
886 // Set P as P's last user until someone starts using P.
887 // However, if P is a Pass Manager then it does not need
888 // to record its last user.
889 if (!dynamic_cast<PMDataManager *>(P))
890 LastUses.push_back(P);
891 TPM->setLastUser(LastUses, P);
892
893 if (!TransferLastUses.empty()) {
894 Pass *My_PM = dynamic_cast<Pass *>(this);
895 TPM->setLastUser(TransferLastUses, My_PM);
896 TransferLastUses.clear();
897 }
898
899 // Now, take care of required analysises that are not available.
900 for (SmallVector<AnalysisID, 8>::iterator
901 I = ReqAnalysisNotAvailable.begin(),
902 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
903 Pass *AnalysisPass = (*I)->createPass();
904 this->addLowerLevelRequiredPass(P, AnalysisPass);
905 }
906
907 // Take a note of analysis required and made available by this pass.
908 // Remove the analysis not preserved by this pass
909 removeNotPreservedAnalysis(P);
910 recordAvailableAnalysis(P);
911
Devang Patel8cad70d2006-11-11 01:51:02 +0000912 // Add pass
913 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000914}
915
Devang Patele64d3052007-04-16 20:12:57 +0000916
917/// Populate RP with analysis pass that are required by
918/// pass P and are available. Populate RP_NotAvail with analysis
919/// pass that are required by pass P but are not available.
920void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
921 SmallVector<AnalysisID, 8> &RP_NotAvail,
922 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000923 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
924 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000925 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000926 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000927 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
928 RP.push_back(AnalysisPass);
929 else
Chris Lattner60987362009-03-06 05:53:14 +0000930 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000931 }
Devang Patelf58183d2006-12-12 23:09:32 +0000932
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000933 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000934 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000935 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000936 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
937 RP.push_back(AnalysisPass);
938 else
Chris Lattner60987362009-03-06 05:53:14 +0000939 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000940 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000941}
942
Devang Patel07f4f582006-11-14 21:49:36 +0000943// All Required analyses should be available to the pass as it runs! Here
944// we fill in the AnalysisImpls member of the pass so that it can
945// successfully use the getAnalysis() method to retrieve the
946// implementations it needs.
947//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000948void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000949 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
950
Chris Lattnercbd160f2008-08-08 05:33:04 +0000951 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000952 I = AnUsage->getRequiredSet().begin(),
953 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000954 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000955 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000956 // This may be analysis pass that is initialized on the fly.
957 // If that is not the case then it will raise an assert when it is used.
958 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000959 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000960 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000961 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000962 }
963}
964
Devang Patel640c5bb2006-12-08 22:30:11 +0000965/// Find the pass that implements Analysis AID. If desired pass is not found
966/// then return NULL.
967Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
968
969 // Check if AvailableAnalysis map has one entry.
970 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
971
972 if (I != AvailableAnalysis.end())
973 return I->second;
974
975 // Search Parents through TopLevelManager
976 if (SearchParent)
977 return TPM->findAnalysisPass(AID);
978
Devang Patel9d759b82006-12-09 00:09:12 +0000979 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000980}
981
Devang Patel991aeba2006-12-15 20:13:01 +0000982// Print list of passes that are last used by P.
983void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
984
Devang Patel8adae862007-07-20 18:04:54 +0000985 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000986
987 // If this is a on the fly manager then it does not have TPM.
988 if (!TPM)
989 return;
990
Devang Patel991aeba2006-12-15 20:13:01 +0000991 TPM->collectLastUses(LUses, P);
992
Devang Patel8adae862007-07-20 18:04:54 +0000993 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000994 E = LUses.end(); I != E; ++I) {
995 llvm::cerr << "--" << std::string(Offset*2, ' ');
996 (*I)->dumpPassStructure(0);
997 }
998}
999
1000void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +00001001 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001002 E = PassVector.end(); I != E; ++I) {
1003 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1004 PMD->dumpPassArguments();
1005 else
1006 if (const PassInfo *PI = (*I)->getPassInfo())
1007 if (!PI->isAnalysisGroup())
1008 cerr << " -" << PI->getPassArgument();
1009 }
1010}
1011
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001012void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1013 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +00001014 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +00001015 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001016 return;
1017 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001018 switch (S1) {
1019 case EXECUTION_MSG:
1020 cerr << "Executing Pass '" << P->getPassName();
1021 break;
1022 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001023 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001024 break;
1025 case FREEING_MSG:
1026 cerr << " Freeing Pass '" << P->getPassName();
1027 break;
1028 default:
1029 break;
1030 }
1031 switch (S2) {
1032 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001033 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001034 break;
1035 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001036 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001037 break;
1038 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001039 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001040 break;
1041 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001042 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001043 break;
1044 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001045 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001046 break;
1047 default:
1048 break;
1049 }
Devang Patel991aeba2006-12-15 20:13:01 +00001050}
1051
Chris Lattner4c1e9542009-03-06 06:45:05 +00001052void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001053 if (PassDebugging < Details)
1054 return;
1055
1056 AnalysisUsage analysisUsage;
1057 P->getAnalysisUsage(analysisUsage);
1058 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1059}
1060
Chris Lattner4c1e9542009-03-06 06:45:05 +00001061void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001062 if (PassDebugging < Details)
1063 return;
1064
1065 AnalysisUsage analysisUsage;
1066 P->getAnalysisUsage(analysisUsage);
1067 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1068}
1069
1070void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001071 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001072 assert(PassDebugging >= Details);
1073 if (Set.empty())
1074 return;
1075 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001076 for (unsigned i = 0; i != Set.size(); ++i) {
1077 if (i) cerr << ",";
1078 cerr << " " << Set[i]->getPassName();
1079 }
1080 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001081}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001082
Devang Patel004937b2007-07-27 20:06:09 +00001083/// Add RequiredPass into list of lower level passes required by pass P.
1084/// RequiredPass is run on the fly by Pass Manager when P requests it
1085/// through getAnalysis interface.
1086/// This should be handled by specific pass manager.
1087void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1088 if (TPM) {
1089 TPM->dumpArguments();
1090 TPM->dumpPasses();
1091 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001092
1093 // Module Level pass may required Function Level analysis info
1094 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1095 // to provide this on demand. In that case, in Pass manager terminology,
1096 // module level pass is requiring lower level analysis info managed by
1097 // lower level pass manager.
1098
1099 // When Pass manager is not able to order required analysis info, Pass manager
1100 // checks whether any lower level manager will be able to provide this
1101 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001102#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001103 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1104 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001105#endif
Chris Lattner60987362009-03-06 05:53:14 +00001106 assert(0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001107}
1108
Devang Patele7599552007-01-12 18:52:44 +00001109// Destructor
1110PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001111 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001112 E = PassVector.end(); I != E; ++I)
1113 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001114}
1115
Devang Patel9bdf7d42006-12-08 23:28:54 +00001116//===----------------------------------------------------------------------===//
1117// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001118// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1119Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001120 return PM.findAnalysisPass(ID, dir);
1121}
1122
Devang Patel92942812007-04-16 20:56:24 +00001123Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1124 Function &F) {
1125 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1126}
1127
Devang Patela1514cb2006-12-07 19:39:39 +00001128//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001129// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001130
Devang Patel6e5a1132006-11-07 21:31:57 +00001131/// Execute all of the passes scheduled for execution by invoking
1132/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1133/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001134bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001135 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001136 return false;
1137
Devang Patele9585592006-12-08 01:38:28 +00001138 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001139
Devang Patel6e5a1132006-11-07 21:31:57 +00001140 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001141 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1142 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001143
Devang Pateld305c402007-08-10 18:29:32 +00001144 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001145 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001146
Devang Patelabfbe3b2006-12-16 00:56:26 +00001147 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001148
Chris Lattner4c1e9542009-03-06 06:45:05 +00001149 {
1150 // If the pass crashes, remember this.
1151 PassManagerPrettyStackEntry X(BP, *I);
1152
1153 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1154 Changed |= BP->runOnBasicBlock(*I);
1155 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1156 }
Devang Patel93a197c2006-12-14 00:08:04 +00001157
Devang Patel003a5592007-03-05 20:01:30 +00001158 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001159 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1160 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001161 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001162
Devang Patela273d1c2007-07-19 18:02:32 +00001163 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001164 removeNotPreservedAnalysis(BP);
1165 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001166 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001167 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001168
Devang Patel56d48ec2006-12-15 22:57:49 +00001169 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001170}
1171
Devang Patel475c4532006-12-08 00:59:05 +00001172// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001173bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001174 bool Changed = false;
1175
Chris Lattner4c1e9542009-03-06 06:45:05 +00001176 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1177 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001178
1179 return Changed;
1180}
1181
Duncan Sands51495602009-02-13 09:42:34 +00001182bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001183 bool Changed = false;
1184
Chris Lattner4c1e9542009-03-06 06:45:05 +00001185 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1186 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001187
1188 return Changed;
1189}
1190
Duncan Sands51495602009-02-13 09:42:34 +00001191bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001192 bool Changed = false;
1193
Devang Patelabfbe3b2006-12-16 00:56:26 +00001194 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1195 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001196 Changed |= BP->doInitialization(F);
1197 }
1198
1199 return Changed;
1200}
1201
Duncan Sands51495602009-02-13 09:42:34 +00001202bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001203 bool Changed = false;
1204
Devang Patelabfbe3b2006-12-16 00:56:26 +00001205 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1206 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001207 Changed |= BP->doFinalization(F);
1208 }
1209
1210 return Changed;
1211}
1212
1213
Devang Patela1514cb2006-12-07 19:39:39 +00001214//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001215// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001216
Devang Patel4e12f862006-11-08 10:44:40 +00001217/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001218FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001219 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001220 // FPM is the top level manager.
1221 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001222
Dan Gohman565df952008-03-13 02:08:36 +00001223 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001224 FPM->setResolver(AR);
1225
Devang Patel1f653682006-12-08 18:57:16 +00001226 MP = P;
1227}
1228
Devang Patelb67904d2006-12-13 02:36:01 +00001229FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001230 delete FPM;
1231}
1232
Devang Patel4e12f862006-11-08 10:44:40 +00001233/// add - Add a pass to the queue of passes to run. This passes
1234/// ownership of the Pass to the PassManager. When the
1235/// PassManager_X is destroyed, the pass will be destroyed as well, so
1236/// there is no need to delete the pass. (TODO delete passes.)
1237/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001238void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001239 FPM->add(P);
1240}
1241
Devang Patel9f3083e2006-11-15 19:39:54 +00001242/// run - Execute all of the passes scheduled for execution. Keep
1243/// track of whether any of the passes modifies the function, and if
1244/// so, return true.
1245///
Devang Patelb67904d2006-12-13 02:36:01 +00001246bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001247 std::string errstr;
1248 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001249 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001250 abort();
1251 }
Devang Patel272908d2006-12-08 22:57:48 +00001252 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001253}
1254
1255
Devang Patelff631ae2006-11-15 01:27:05 +00001256/// doInitialization - Run all of the initializers for the function passes.
1257///
Devang Patelb67904d2006-12-13 02:36:01 +00001258bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001259 return FPM->doInitialization(*MP->getModule());
1260}
1261
Dan Gohmane6656eb2007-07-30 14:51:13 +00001262/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001263///
Devang Patelb67904d2006-12-13 02:36:01 +00001264bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001265 return FPM->doFinalization(*MP->getModule());
1266}
1267
Devang Patela1514cb2006-12-07 19:39:39 +00001268//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001269// FunctionPassManagerImpl implementation
1270//
Duncan Sands51495602009-02-13 09:42:34 +00001271bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001272 bool Changed = false;
1273
Chris Lattner4c1e9542009-03-06 06:45:05 +00001274 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1275 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001276
1277 return Changed;
1278}
1279
Duncan Sands51495602009-02-13 09:42:34 +00001280bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001281 bool Changed = false;
1282
Chris Lattner4c1e9542009-03-06 06:45:05 +00001283 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1284 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285
1286 return Changed;
1287}
1288
Devang Patelec9c58f2009-04-01 22:34:41 +00001289/// cleanup - After running all passes, clean up pass manager cache.
1290void FPPassManager::cleanup() {
1291 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1292 FunctionPass *FP = getContainedPass(Index);
1293 AnalysisResolver *AR = FP->getResolver();
1294 assert(AR && "Analysis Resolver is not set");
1295 AR->clearAnalysisImpls();
1296 }
1297}
1298
Torok Edwin24c78352009-06-29 18:49:09 +00001299void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1300 if (!wasRun)
1301 return;
1302 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1303 FPPassManager *FPPM = getContainedManager(Index);
1304 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1305 FPPM->getContainedPass(Index)->releaseMemory();
1306 }
1307 }
1308}
1309
Devang Patel67d6a5e2006-12-19 19:46:59 +00001310// Execute all the passes managed by this top level manager.
1311// Return true if any function is modified by a pass.
1312bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001313 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001314 TimingInfo::createTheTimeInfo();
1315
1316 dumpArguments();
1317 dumpPasses();
1318
Devang Patele3068402006-12-21 00:16:50 +00001319 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001320 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1321 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001322
1323 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1324 getContainedManager(Index)->cleanup();
1325
Torok Edwin24c78352009-06-29 18:49:09 +00001326 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001327 return Changed;
1328}
1329
1330//===----------------------------------------------------------------------===//
1331// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001332
Devang Patel8c78a0b2007-05-03 01:11:54 +00001333char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001334/// Print passes managed by this manager
1335void FPPassManager::dumpPassStructure(unsigned Offset) {
1336 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1337 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1338 FunctionPass *FP = getContainedPass(Index);
1339 FP->dumpPassStructure(Offset + 1);
1340 dumpLastUses(FP, Offset+1);
1341 }
1342}
1343
1344
Devang Patel0c2012f2006-11-07 21:49:50 +00001345/// Execute all of the passes scheduled for execution by invoking
1346/// runOnFunction method. Keep track of whether any of the passes modifies
1347/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001348bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001349 if (F.isDeclaration())
1350 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001351
1352 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001353
Devang Patelcbbf2912008-03-20 01:09:53 +00001354 // Collect inherited analysis from Module level pass manager.
1355 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001356
Devang Patelabfbe3b2006-12-16 00:56:26 +00001357 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1358 FunctionPass *FP = getContainedPass(Index);
1359
Devang Pateld305c402007-08-10 18:29:32 +00001360 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001361 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001362
Devang Patelabfbe3b2006-12-16 00:56:26 +00001363 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001364
Chris Lattner4c1e9542009-03-06 06:45:05 +00001365 {
1366 PassManagerPrettyStackEntry X(FP, F);
1367
1368 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1369 Changed |= FP->runOnFunction(F);
1370 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1371 }
Devang Patel93a197c2006-12-14 00:08:04 +00001372
Devang Patel003a5592007-03-05 20:01:30 +00001373 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001374 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001375 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001376
Devang Patela273d1c2007-07-19 18:02:32 +00001377 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001378 removeNotPreservedAnalysis(FP);
1379 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001380 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001381
Devang Patel67c79a42008-07-01 19:50:56 +00001382 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001383 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001384 }
1385 return Changed;
1386}
1387
Devang Patel67d6a5e2006-12-19 19:46:59 +00001388bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001389 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001390
Chris Lattner60987362009-03-06 05:53:14 +00001391 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1392 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001393
1394 return Changed |= doFinalization(M);
1395}
1396
Duncan Sands51495602009-02-13 09:42:34 +00001397bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001398 bool Changed = false;
1399
Chris Lattner4c1e9542009-03-06 06:45:05 +00001400 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1401 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001402
1403 return Changed;
1404}
1405
Duncan Sands51495602009-02-13 09:42:34 +00001406bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001407 bool Changed = false;
1408
Chris Lattner4c1e9542009-03-06 06:45:05 +00001409 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1410 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001411
Devang Patelff631ae2006-11-15 01:27:05 +00001412 return Changed;
1413}
1414
Devang Patela1514cb2006-12-07 19:39:39 +00001415//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001416// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001417
Devang Patel05e1a972006-11-07 22:03:15 +00001418/// Execute all of the passes scheduled for execution by invoking
1419/// runOnModule method. Keep track of whether any of the passes modifies
1420/// the module, and if so, return true.
1421bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001423 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001424
Torok Edwin24c78352009-06-29 18:49:09 +00001425 // Initialize on-the-fly passes
1426 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1427 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1428 I != E; ++I) {
1429 FunctionPassManagerImpl *FPP = I->second;
1430 Changed |= FPP->doInitialization(M);
1431 }
1432
Devang Patelabfbe3b2006-12-16 00:56:26 +00001433 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1434 ModulePass *MP = getContainedPass(Index);
1435
Dan Gohman929391a2008-01-29 12:09:55 +00001436 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1437 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001438 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001439
Devang Patelabfbe3b2006-12-16 00:56:26 +00001440 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001441
Chris Lattner4c1e9542009-03-06 06:45:05 +00001442 {
1443 PassManagerPrettyStackEntry X(MP, M);
1444 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1445 Changed |= MP->runOnModule(M);
1446 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1447 }
Devang Patel93a197c2006-12-14 00:08:04 +00001448
Devang Patel003a5592007-03-05 20:01:30 +00001449 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001450 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1451 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001452 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001453
Devang Patela273d1c2007-07-19 18:02:32 +00001454 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001455 removeNotPreservedAnalysis(MP);
1456 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001457 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001458 }
Torok Edwin24c78352009-06-29 18:49:09 +00001459
1460 // Finalize on-the-fly passes
1461 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1462 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1463 I != E; ++I) {
1464 FunctionPassManagerImpl *FPP = I->second;
1465 // We don't know when is the last time an on-the-fly pass is run,
1466 // so we need to releaseMemory / finalize here
1467 FPP->releaseMemoryOnTheFly();
1468 Changed |= FPP->doFinalization(M);
1469 }
Devang Patel05e1a972006-11-07 22:03:15 +00001470 return Changed;
1471}
1472
Devang Patele64d3052007-04-16 20:12:57 +00001473/// Add RequiredPass into list of lower level passes required by pass P.
1474/// RequiredPass is run on the fly by Pass Manager when P requests it
1475/// through getAnalysis interface.
1476void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001477 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1478 "Unable to handle Pass that requires lower level Analysis pass");
1479 assert((P->getPotentialPassManagerType() <
1480 RequiredPass->getPotentialPassManagerType()) &&
1481 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001482
Devang Patel68f72b12007-04-26 17:50:19 +00001483 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001484 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001485 FPP = new FunctionPassManagerImpl(0);
1486 // FPP is the top level manager.
1487 FPP->setTopLevelManager(FPP);
1488
Devang Patel69e9f6d2007-04-16 20:27:05 +00001489 OnTheFlyManagers[P] = FPP;
1490 }
Devang Patel68f72b12007-04-26 17:50:19 +00001491 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001492
Devang Patel68f72b12007-04-26 17:50:19 +00001493 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001494 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001495 LU.push_back(RequiredPass);
1496 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001497}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001498
1499/// Return function pass corresponding to PassInfo PI, that is
1500/// required by module pass MP. Instantiate analysis pass, by using
1501/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001502Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001503 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001504 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001505
Torok Edwin24c78352009-06-29 18:49:09 +00001506 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001507 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001508 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001509}
1510
1511
Devang Patela1514cb2006-12-07 19:39:39 +00001512//===----------------------------------------------------------------------===//
1513// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001514//
Devang Patelc290c8a2006-11-07 22:23:34 +00001515/// run - Execute all of the passes scheduled for execution. Keep track of
1516/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001517bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001518 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001519 TimingInfo::createTheTimeInfo();
1520
Devang Patelcfd70c42006-12-13 22:10:00 +00001521 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001522 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001523
Devang Patele3068402006-12-21 00:16:50 +00001524 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001525 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1526 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001527 return Changed;
1528}
Devang Patel376fefa2006-11-08 10:29:57 +00001529
Devang Patela1514cb2006-12-07 19:39:39 +00001530//===----------------------------------------------------------------------===//
1531// PassManager implementation
1532
Devang Patel376fefa2006-11-08 10:29:57 +00001533/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001534PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001536 // PM is the top level manager
1537 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001538}
1539
Devang Patelb67904d2006-12-13 02:36:01 +00001540PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001541 delete PM;
1542}
1543
Devang Patel376fefa2006-11-08 10:29:57 +00001544/// add - Add a pass to the queue of passes to run. This passes ownership of
1545/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1546/// will be destroyed as well, so there is no need to delete the pass. This
1547/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001548void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001549 PM->add(P);
1550}
1551
1552/// run - Execute all of the passes scheduled for execution. Keep track of
1553/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001554bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001555 return PM->run(M);
1556}
1557
Devang Patelb8817b92006-12-14 00:59:42 +00001558//===----------------------------------------------------------------------===//
1559// TimingInfo Class - This class is used to calculate information about the
1560// amount of time each pass takes to execute. This only happens with
1561// -time-passes is enabled on the command line.
1562//
1563bool llvm::TimePassesIsEnabled = false;
1564static cl::opt<bool,true>
1565EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1566 cl::desc("Time each pass, printing elapsed time for each on exit"));
1567
1568// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1569// a non null value (if the -time-passes option is enabled) or it leaves it
1570// null. It may be called multiple times.
1571void TimingInfo::createTheTimeInfo() {
1572 if (!TimePassesIsEnabled || TheTimeInfo) return;
1573
1574 // Constructed the first time this is called, iff -time-passes is enabled.
1575 // This guarantees that the object will be constructed before static globals,
1576 // thus it will be destroyed before them.
1577 static ManagedStatic<TimingInfo> TTI;
1578 TheTimeInfo = &*TTI;
1579}
1580
Devang Patel1c3633e2007-01-29 23:10:37 +00001581/// If TimingInfo is enabled then start pass timer.
1582void StartPassTimer(Pass *P) {
1583 if (TheTimeInfo)
1584 TheTimeInfo->passStarted(P);
1585}
1586
1587/// If TimingInfo is enabled then stop pass timer.
1588void StopPassTimer(Pass *P) {
1589 if (TheTimeInfo)
1590 TheTimeInfo->passEnded(P);
1591}
1592
Devang Patel1c56a632007-01-08 19:29:38 +00001593//===----------------------------------------------------------------------===//
1594// PMStack implementation
1595//
Devang Patelad98d232007-01-11 22:15:30 +00001596
Devang Patel1c56a632007-01-08 19:29:38 +00001597// Pop Pass Manager from the stack and clear its analysis info.
1598void PMStack::pop() {
1599
1600 PMDataManager *Top = this->top();
1601 Top->initializeAnalysisInfo();
1602
1603 S.pop_back();
1604}
1605
1606// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001607void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001608 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001609
Chris Lattner60987362009-03-06 05:53:14 +00001610 if (!this->empty()) {
1611 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001612
Chris Lattner60987362009-03-06 05:53:14 +00001613 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001614 TPM->addIndirectPassManager(PM);
1615 PM->setTopLevelManager(TPM);
1616 }
1617
Devang Patel15701b52007-01-11 00:19:00 +00001618 S.push_back(PM);
1619}
1620
1621// Dump content of the pass manager stack.
1622void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001623 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1624 E = S.end(); I != E; ++I)
1625 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1626
Devang Patel15701b52007-01-11 00:19:00 +00001627 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001628 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001629}
1630
Devang Patel1c56a632007-01-08 19:29:38 +00001631/// Find appropriate Module Pass Manager in the PM Stack and
1632/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001633void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001634 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001635 // Find Module Pass Manager
1636 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001637 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1638 if (TopPMType == PreferredType)
1639 break; // We found desired pass manager
1640 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001641 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001642 else
1643 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001644 }
Devang Patel18ff6362008-09-09 21:38:40 +00001645 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001646 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001647}
1648
Devang Patel3312f752007-01-16 21:43:18 +00001649/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1650/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001651void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001652 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001653
Devang Patela3286902008-09-09 17:56:50 +00001654 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001655 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001656 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1657 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001658 else
Devang Patel3312f752007-01-16 21:43:18 +00001659 break;
1660 }
1661 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1662
1663 // Create new Function Pass Manager
1664 if (!FPP) {
1665 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1666 PMDataManager *PMD = PMS.top();
1667
1668 // [1] Create new Function Pass Manager
1669 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001670 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001671
1672 // [2] Set up new manager's top level manager
1673 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1674 TPM->addIndirectPassManager(FPP);
1675
1676 // [3] Assign manager to manage this new manager. This may create
1677 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001678 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001679
1680 // [4] Push new manager into PMS
1681 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001682 }
1683
Devang Patel3312f752007-01-16 21:43:18 +00001684 // Assign FPP as the manager of this pass.
1685 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001686}
1687
Devang Patel3312f752007-01-16 21:43:18 +00001688/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001689/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001690void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001691 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001692 BBPassManager *BBP = NULL;
1693
Devang Patel15701b52007-01-11 00:19:00 +00001694 // Basic Pass Manager is a leaf pass manager. It does not handle
1695 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001696 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001697 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001698
Devang Patel3312f752007-01-16 21:43:18 +00001699 // If leaf manager is not Basic Block Pass manager then create new
1700 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001701
Devang Patel3312f752007-01-16 21:43:18 +00001702 if (!BBP) {
1703 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1704 PMDataManager *PMD = PMS.top();
1705
1706 // [1] Create new Basic Block Manager
1707 BBP = new BBPassManager(PMD->getDepth() + 1);
1708
1709 // [2] Set up new manager's top level manager
1710 // Basic Block Pass Manager does not live by itself
1711 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1712 TPM->addIndirectPassManager(BBP);
1713
Devang Patel15701b52007-01-11 00:19:00 +00001714 // [3] Assign manager to manage this new manager. This may create
1715 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001716 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001717
Devang Patel3312f752007-01-16 21:43:18 +00001718 // [4] Push new manager into PMS
1719 PMS.push(BBP);
1720 }
Devang Patel1c56a632007-01-08 19:29:38 +00001721
Devang Patel3312f752007-01-16 21:43:18 +00001722 // Assign BBP as the manager of this pass.
1723 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001724}
1725
Dan Gohmand3a20c92008-03-11 16:41:42 +00001726PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001727
1728/*===-- C Bindings --------------------------------------------------------===*/
1729
1730LLVMPassManagerRef LLVMCreatePassManager() {
1731 return wrap(new PassManager());
1732}
1733
1734LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1735 return wrap(new FunctionPassManager(unwrap(P)));
1736}
1737
1738int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1739 return unwrap<PassManager>(PM)->run(*unwrap(M));
1740}
1741
1742int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1743 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1744}
1745
1746int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1747 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1748}
1749
1750int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1751 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1752}
1753
1754void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1755 delete unwrap(PM);
1756}