blob: d3d61f5a5eb5061afd37e28538ae020d0ce270b5 [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"
Dan Gohman4dbb3012009-09-28 00:27:48 +000016#include "llvm/Assembly/Writer.h"
Devang Patelf1567a52006-12-13 20:03:48 +000017#include "llvm/Support/CommandLine.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000018#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000019#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000020#include "llvm/ModuleProvider.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000022#include "llvm/Support/ManagedStatic.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000023#include "llvm/Support/raw_ostream.h"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000024#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000025#include "llvm/System/Threading.h"
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
Devang Patelf1567a52006-12-13 20:03:48 +000048static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000049PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000050 cl::desc("Print PassManager debugging information"),
51 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000052 clEnumVal(None , "disable debug output"),
53 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
54 clEnumVal(Structure , "print pass structure before run()"),
55 clEnumVal(Executions, "print pass name before it is executed"),
56 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000057 clEnumValEnd));
58} // End of llvm namespace
59
Chris Lattnerd4d966f2009-09-15 05:03:04 +000060/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
61/// or higher is specified.
62bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
63 return PassDebugging >= Executions;
64}
65
66
67
68
Chris Lattner4c1e9542009-03-06 06:45:05 +000069void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
70 if (V == 0 && M == 0)
71 OS << "Releasing pass '";
72 else
73 OS << "Running pass '";
74
75 OS << P->getPassName() << "'";
76
77 if (M) {
78 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
79 return;
80 }
81 if (V == 0) {
82 OS << '\n';
83 return;
84 }
85
Dan Gohman79fc0e92009-03-10 18:47:59 +000086 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +000087 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000088 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +000089 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +000090 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +000091 else
Dan Gohman79fc0e92009-03-10 18:47:59 +000092 OS << "value";
93
94 OS << " '";
95 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
96 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +000097}
98
99
Devang Patelffca9102006-12-15 19:39:30 +0000100namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +0000101
Devang Patelf33f3eb2006-12-07 19:21:29 +0000102//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000103// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000104//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000105/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000106/// pass together and sequence them to process one basic block before
107/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000108class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000109
110public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000111 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000112 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000113 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000114
Devang Patelca58e352006-11-08 10:05:38 +0000115 /// Execute all of the passes scheduled for execution. Keep track of
116 /// whether any of the passes modifies the function, and if so, return true.
117 bool runOnFunction(Function &F);
118
Devang Patelf9d96b92006-12-07 19:57:52 +0000119 /// Pass Manager itself does not invalidate any analysis info.
120 void getAnalysisUsage(AnalysisUsage &Info) const {
121 Info.setPreservesAll();
122 }
123
Devang Patel475c4532006-12-08 00:59:05 +0000124 bool doInitialization(Module &M);
125 bool doInitialization(Function &F);
126 bool doFinalization(Module &M);
127 bool doFinalization(Function &F);
128
Devang Patele3858e62007-02-01 22:08:25 +0000129 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000130 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000131 }
132
Devang Pateleda56172006-12-12 23:34:33 +0000133 // Print passes managed by this manager
134 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000135 llvm::errs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000136 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
137 BasicBlockPass *BP = getContainedPass(Index);
138 BP->dumpPassStructure(Offset + 1);
139 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000140 }
141 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000142
143 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000144 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000145 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
146 return BP;
147 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000148
Devang Patel28349ab2007-02-27 15:00:39 +0000149 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000150 return PMT_BasicBlockPassManager;
151 }
Devang Patelca58e352006-11-08 10:05:38 +0000152};
153
Devang Patel8c78a0b2007-05-03 01:11:54 +0000154char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000155}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156
Devang Patele7599552007-01-12 18:52:44 +0000157namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000158
Devang Patel10c2ca62006-12-12 22:47:13 +0000159//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000160// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000161//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000162/// FunctionPassManagerImpl manages FPPassManagers
163class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000164 public PMDataManager,
165 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000166private:
167 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000168public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000169 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000170 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000171 Pass(&ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000172 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173
174 /// add - Add a pass to the queue of passes to run. This passes ownership of
175 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
176 /// will be destroyed as well, so there is no need to delete the pass. This
177 /// implies that all passes MUST be allocated with 'new'.
178 void add(Pass *P) {
179 schedulePass(P);
180 }
181
Torok Edwin24c78352009-06-29 18:49:09 +0000182 // Prepare for running an on the fly pass, freeing memory if needed
183 // from a previous run.
184 void releaseMemoryOnTheFly();
185
Devang Patel67d6a5e2006-12-19 19:46:59 +0000186 /// run - Execute all of the passes scheduled for execution. Keep track of
187 /// whether any of the passes modifies the module, and if so, return true.
188 bool run(Function &F);
189
190 /// doInitialization - Run all of the initializers for the function passes.
191 ///
192 bool doInitialization(Module &M);
193
Dan Gohmane6656eb2007-07-30 14:51:13 +0000194 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000195 ///
196 bool doFinalization(Module &M);
197
198 /// Pass Manager itself does not invalidate any analysis info.
199 void getAnalysisUsage(AnalysisUsage &Info) const {
200 Info.setPreservesAll();
201 }
202
203 inline void addTopLevelPass(Pass *P) {
204
205 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
206
207 // P is a immutable pass and it will be managed by this
208 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000209 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210 P->setResolver(AR);
211 initializeAnalysisImpl(P);
212 addImmutablePass(IP);
213 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000214 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000215 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216 }
Devang Patel0f080042007-01-12 17:23:48 +0000217
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218 }
219
220 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000221 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000222 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
223 return FP;
224 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225};
226
Devang Patel8c78a0b2007-05-03 01:11:54 +0000227char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228//===----------------------------------------------------------------------===//
229// MPPassManager
230//
231/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000232/// It batches all Module passes and function pass managers together and
233/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000234class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000235public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000236 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000237 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000238 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000239
240 // Delete on the fly managers.
241 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000242 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000243 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
244 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000245 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000246 delete FPP;
247 }
248 }
249
Devang Patelca58e352006-11-08 10:05:38 +0000250 /// run - Execute all of the passes scheduled for execution. Keep track of
251 /// whether any of the passes modifies the module, and if so, return true.
252 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000253
Devang Patelf9d96b92006-12-07 19:57:52 +0000254 /// Pass Manager itself does not invalidate any analysis info.
255 void getAnalysisUsage(AnalysisUsage &Info) const {
256 Info.setPreservesAll();
257 }
258
Devang Patele64d3052007-04-16 20:12:57 +0000259 /// Add RequiredPass into list of lower level passes required by pass P.
260 /// RequiredPass is run on the fly by Pass Manager when P requests it
261 /// through getAnalysis interface.
262 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
263
Devang Patel69e9f6d2007-04-16 20:27:05 +0000264 /// Return function pass corresponding to PassInfo PI, that is
265 /// required by module pass MP. Instantiate analysis pass, by using
266 /// its runOnFunction() for function F.
267 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
268
Devang Patele3858e62007-02-01 22:08:25 +0000269 virtual const char *getPassName() const {
270 return "Module Pass Manager";
271 }
272
Devang Pateleda56172006-12-12 23:34:33 +0000273 // Print passes managed by this manager
274 void dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000275 llvm::errs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000276 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
277 ModulePass *MP = getContainedPass(Index);
278 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000279 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
280 OnTheFlyManagers.find(MP);
281 if (I != OnTheFlyManagers.end())
282 I->second->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
Nick Lewycky02d5f772009-10-25 06:33:48 +0000369class TimingInfo {
Devang Patel1c3633e2007-01-29 23:10:37 +0000370 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
Dan Gohman277e7672009-09-28 00:07:05 +0000389 /// passStarted - This method creates a timer for the given pass if it doesn't
390 /// already have one, and starts the timer.
391 Timer *passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000392 if (dynamic_cast<PMDataManager *>(P))
Dan Gohman277e7672009-09-28 00:07:05 +0000393 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000394
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;
Dan Gohman277e7672009-09-28 00:07:05 +0000399 Timer *T = &I->second;
400 T->startTimer();
401 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000402 }
403};
404
Devang Patel1c3633e2007-01-29 23:10:37 +0000405} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000406
Dan Gohmand78c4002008-05-13 00:00:25 +0000407static TimingInfo *TheTimeInfo;
408
Devang Patela1514cb2006-12-07 19:39:39 +0000409//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000410// PMTopLevelManager implementation
411
Devang Patel4268fc02007-01-16 02:00:38 +0000412/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000413PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000414 if (t == TLM_Pass) {
415 MPPassManager *MPP = new MPPassManager(1);
416 MPP->setTopLevelManager(this);
417 addPassManager(MPP);
418 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000419 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000420 FPPassManager *FPP = new FPPassManager(1);
421 FPP->setTopLevelManager(this);
422 addPassManager(FPP);
423 activeStack.push(FPP);
424 }
425}
426
Devang Patelafb1f3622006-12-12 22:35:25 +0000427/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000428void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000429 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000430 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000431 E = AnalysisPasses.end(); I != E; ++I) {
432 Pass *AP = *I;
433 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000434
435 if (P == AP)
436 continue;
437
Devang Patelafb1f3622006-12-12 22:35:25 +0000438 // If AP is the last user of other passes then make P last user of
439 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000440 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000441 LUE = LastUser.end(); LUI != LUE; ++LUI) {
442 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000443 // DenseMap iterator is not invalidated here because
444 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000445 LastUser[LUI->first] = P;
446 }
447 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000448}
449
450/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000451void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000452 Pass *P) {
453 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
454 InversedLastUser.find(P);
455 if (DMI == InversedLastUser.end())
456 return;
457
458 SmallPtrSet<Pass *, 8> &LU = DMI->second;
459 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
460 E = LU.end(); I != E; ++I) {
461 LastUses.push_back(*I);
462 }
463
Devang Patelafb1f3622006-12-12 22:35:25 +0000464}
465
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000466AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
467 AnalysisUsage *AnUsage = NULL;
468 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
469 if (DMI != AnUsageMap.end())
470 AnUsage = DMI->second;
471 else {
472 AnUsage = new AnalysisUsage();
473 P->getAnalysisUsage(*AnUsage);
474 AnUsageMap[P] = AnUsage;
475 }
476 return AnUsage;
477}
478
Devang Patelafb1f3622006-12-12 22:35:25 +0000479/// Schedule pass P for execution. Make sure that passes required by
480/// P are run before P is run. Update analysis info maintained by
481/// the manager. Remove dead passes. This is a recursive function.
482void PMTopLevelManager::schedulePass(Pass *P) {
483
Devang Patel3312f752007-01-16 21:43:18 +0000484 // TODO : Allocate function manager for this pass, other wise required set
485 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000486
Devang Pateld74ede72007-03-06 01:06:16 +0000487 // Give pass a chance to prepare the stage.
488 P->preparePassManager(activeStack);
489
Devang Patel864970e2008-03-18 00:39:19 +0000490 // If P is an analysis pass and it is available then do not
491 // generate the analysis again. Stale analysis info should not be
492 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000493 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000494 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
495 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000496 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000497 }
Devang Patel864970e2008-03-18 00:39:19 +0000498
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000499 AnalysisUsage *AnUsage = findAnalysisUsage(P);
500
Devang Patelfdee7032008-08-14 23:07:48 +0000501 bool checkAnalysis = true;
502 while (checkAnalysis) {
503 checkAnalysis = false;
504
505 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
506 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
507 E = RequiredSet.end(); I != E; ++I) {
508
509 Pass *AnalysisPass = findAnalysisPass(*I);
510 if (!AnalysisPass) {
511 AnalysisPass = (*I)->createPass();
512 if (P->getPotentialPassManagerType () ==
513 AnalysisPass->getPotentialPassManagerType())
514 // Schedule analysis pass that is managed by the same pass manager.
515 schedulePass(AnalysisPass);
516 else if (P->getPotentialPassManagerType () >
517 AnalysisPass->getPotentialPassManagerType()) {
518 // Schedule analysis pass that is managed by a new manager.
519 schedulePass(AnalysisPass);
520 // Recheck analysis passes to ensure that required analysises that
521 // are already checked are still available.
522 checkAnalysis = true;
523 }
524 else
525 // Do not schedule this analysis. Lower level analsyis
526 // passes are run on the fly.
527 delete AnalysisPass;
528 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000529 }
530 }
531
532 // Now all required passes are available.
533 addTopLevelPass(P);
534}
535
536/// Find the pass that implements Analysis AID. Search immutable
537/// passes and all pass managers. If desired pass is not found
538/// then return NULL.
539Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
540
541 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000542 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000543 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000544 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000545 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000546 P = PMD->findAnalysisPass(AID, false);
547 }
548
549 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000550 for (SmallVector<PMDataManager *, 8>::iterator
551 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000552 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
553 P = (*I)->findAnalysisPass(AID, false);
554
Devang Patel0d29ae02008-08-12 15:44:31 +0000555 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000556 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
557 const PassInfo *PI = (*I)->getPassInfo();
558 if (PI == AID)
559 P = *I;
560
561 // If Pass not found then check the interfaces implemented by Immutable Pass
562 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000563 const std::vector<const PassInfo*> &ImmPI =
564 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000565 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
566 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000567 }
568 }
569
Devang Patelafb1f3622006-12-12 22:35:25 +0000570 return P;
571}
572
Devang Pateleda56172006-12-12 23:34:33 +0000573// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000574void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000575
Devang Patelfd4184322007-01-17 20:33:36 +0000576 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000577 return;
578
Devang Pateleda56172006-12-12 23:34:33 +0000579 // Print out the immutable passes
580 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
581 ImmutablePasses[i]->dumpPassStructure(0);
582 }
583
Dan Gohman73caf5f2008-03-13 01:48:32 +0000584 // Every class that derives from PMDataManager also derives from Pass
585 // (sometimes indirectly), but there's no inheritance relationship
586 // between PMDataManager and Pass, so we have to dynamic_cast to get
587 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000588 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000589 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000590 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000591}
592
Devang Patel991aeba2006-12-15 20:13:01 +0000593void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000594
Devang Patelfd4184322007-01-17 20:33:36 +0000595 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000596 return;
597
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000598 errs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000599 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000600 E = PassManagers.end(); I != E; ++I)
601 (*I)->dumpPassArguments();
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000602 errs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000603}
604
Devang Patele3068402006-12-21 00:16:50 +0000605void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000606 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000607 E = PassManagers.end(); I != E; ++I)
608 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000609
610 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000611 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000612 E = IndirectPassManagers.end(); I != E; ++I)
613 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000614
Chris Lattner60987362009-03-06 05:53:14 +0000615 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000616 DME = LastUser.end(); DMI != DME; ++DMI) {
617 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
618 InversedLastUser.find(DMI->second);
619 if (InvDMI != InversedLastUser.end()) {
620 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
621 L.insert(DMI->first);
622 } else {
623 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
624 InversedLastUser[DMI->second] = L;
625 }
626 }
Devang Patele3068402006-12-21 00:16:50 +0000627}
628
Devang Patele7599552007-01-12 18:52:44 +0000629/// Destructor
630PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000631 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000632 E = PassManagers.end(); I != E; ++I)
633 delete *I;
634
Devang Patel0d29ae02008-08-12 15:44:31 +0000635 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000636 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
637 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000638
639 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000640 DME = AnUsageMap.end(); DMI != DME; ++DMI)
641 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000642}
643
Devang Patelafb1f3622006-12-12 22:35:25 +0000644//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000645// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000646
Devang Patel643676c2006-11-11 01:10:19 +0000647/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000648void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000649 const PassInfo *PI = P->getPassInfo();
650 if (PI == 0) return;
651
652 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000653
Chris Lattner60987362009-03-06 05:53:14 +0000654 //This pass is the current implementation of all of the interfaces it
655 //implements as well.
656 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
657 for (unsigned i = 0, e = II.size(); i != e; ++i)
658 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000659}
660
Devang Patel9d9fc902007-03-06 17:52:53 +0000661// Return true if P preserves high level analysis used by other
662// passes managed by this manager
663bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000664 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000665 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000666 return true;
667
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000668 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000669 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000670 E = HigherLevelAnalysis.end(); I != E; ++I) {
671 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000672 if (!dynamic_cast<ImmutablePass*>(P1) &&
673 std::find(PreservedSet.begin(), PreservedSet.end(),
674 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000675 PreservedSet.end())
676 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000677 }
678
679 return true;
680}
681
Chris Lattner02eb94c2008-08-07 07:34:50 +0000682/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000683void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000684 // Don't do this unless assertions are enabled.
685#ifdef NDEBUG
686 return;
687#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000688 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
689 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000690
Devang Patelef432532007-07-19 05:36:09 +0000691 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000692 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000693 E = PreservedSet.end(); I != E; ++I) {
694 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000695 if (Pass *AP = findAnalysisPass(AID, true)) {
696
697 Timer *T = 0;
698 if (TheTimeInfo) T = TheTimeInfo->passStarted(AP);
Devang Patela273d1c2007-07-19 18:02:32 +0000699 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000700 if (T) T->stopTimer();
701 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000702 }
703}
704
Devang Patel67c79a42008-07-01 19:50:56 +0000705/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000706void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000707 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
708 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000709 return;
710
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000711 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000712 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000713 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000714 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000715 if (!dynamic_cast<ImmutablePass*>(Info->second)
716 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000717 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000718 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000719 if (PassDebugging >= Details) {
720 Pass *S = Info->second;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000721 errs() << " -- '" << P->getPassName() << "' is not preserving '";
722 errs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000723 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000724 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000725 }
Devang Patel349170f2006-11-11 01:24:55 +0000726 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000727
728 // Check inherited analysis also. If P is not preserving analysis
729 // provided by parent manager then remove it here.
730 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
731
732 if (!InheritedAnalysis[Index])
733 continue;
734
735 for (std::map<AnalysisID, Pass*>::iterator
736 I = InheritedAnalysis[Index]->begin(),
737 E = InheritedAnalysis[Index]->end(); I != E; ) {
738 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000739 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
740 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000741 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000742 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000743 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000744 }
745 }
Devang Patelf68a3492006-11-07 22:35:17 +0000746}
747
Devang Patelca189262006-11-14 03:05:08 +0000748/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000749void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000750 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000751
Devang Patel8adae862007-07-20 18:04:54 +0000752 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000753
Devang Patel2ff44922007-04-16 20:39:59 +0000754 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000755 if (!TPM)
756 return;
757
Devang Patel17ad0962006-12-08 00:37:52 +0000758 TPM->collectLastUses(DeadPasses, P);
759
Devang Patel656a9172008-06-06 17:50:36 +0000760 if (PassDebugging >= Details && !DeadPasses.empty()) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000761 errs() << " -*- '" << P->getPassName();
762 errs() << "' is the last user of following pass instances.";
763 errs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000764 }
765
Devang Patel8adae862007-07-20 18:04:54 +0000766 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000767 E = DeadPasses.end(); I != E; ++I)
768 freePass(*I, Msg, DBG_STR);
769}
Devang Patel200d3052006-12-13 23:50:44 +0000770
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000771void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000772 enum PassDebuggingString DBG_STR) {
773 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000774
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000775 {
776 // If the pass crashes releasing memory, remember this.
777 PassManagerPrettyStackEntry X(P);
778
Dan Gohman277e7672009-09-28 00:07:05 +0000779 Timer *T = StartPassTimer(P);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000780 P->releaseMemory();
Dan Gohman277e7672009-09-28 00:07:05 +0000781 StopPassTimer(P, T);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000782 }
783
784 if (const PassInfo *PI = P->getPassInfo()) {
785 // Remove the pass itself (if it is not already removed).
786 AvailableAnalysis.erase(PI);
787
788 // Remove all interfaces this pass implements, for which it is also
789 // listed as the available implementation.
790 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
791 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000792 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000793 AvailableAnalysis.find(II[i]);
794 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000795 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000796 }
Devang Patel17ad0962006-12-08 00:37:52 +0000797 }
Devang Patelca189262006-11-14 03:05:08 +0000798}
799
Devang Patel8f677ce2006-12-07 18:47:25 +0000800/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000801/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000802void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000803 // This manager is going to manage pass P. Set up analysis resolver
804 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000805 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000806 P->setResolver(AR);
807
Devang Patelec2b9a72007-03-05 22:57:49 +0000808 // If a FunctionPass F is the last user of ModulePass info M
809 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000810 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000811
Chris Lattner60987362009-03-06 05:53:14 +0000812 if (!ProcessAnalysis) {
813 // Add pass
814 PassVector.push_back(P);
815 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000816 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000817
Chris Lattner60987362009-03-06 05:53:14 +0000818 // At the moment, this pass is the last user of all required passes.
819 SmallVector<Pass *, 12> LastUses;
820 SmallVector<Pass *, 8> RequiredPasses;
821 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
822
823 unsigned PDepth = this->getDepth();
824
825 collectRequiredAnalysis(RequiredPasses,
826 ReqAnalysisNotAvailable, P);
827 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
828 E = RequiredPasses.end(); I != E; ++I) {
829 Pass *PRequired = *I;
830 unsigned RDepth = 0;
831
832 assert(PRequired->getResolver() && "Analysis Resolver is not set");
833 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
834 RDepth = DM.getDepth();
835
836 if (PDepth == RDepth)
837 LastUses.push_back(PRequired);
838 else if (PDepth > RDepth) {
839 // Let the parent claim responsibility of last use
840 TransferLastUses.push_back(PRequired);
841 // Keep track of higher level analysis used by this manager.
842 HigherLevelAnalysis.push_back(PRequired);
843 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000844 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000845 }
846
847 // Set P as P's last user until someone starts using P.
848 // However, if P is a Pass Manager then it does not need
849 // to record its last user.
850 if (!dynamic_cast<PMDataManager *>(P))
851 LastUses.push_back(P);
852 TPM->setLastUser(LastUses, P);
853
854 if (!TransferLastUses.empty()) {
855 Pass *My_PM = dynamic_cast<Pass *>(this);
856 TPM->setLastUser(TransferLastUses, My_PM);
857 TransferLastUses.clear();
858 }
859
860 // Now, take care of required analysises that are not available.
861 for (SmallVector<AnalysisID, 8>::iterator
862 I = ReqAnalysisNotAvailable.begin(),
863 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
864 Pass *AnalysisPass = (*I)->createPass();
865 this->addLowerLevelRequiredPass(P, AnalysisPass);
866 }
867
868 // Take a note of analysis required and made available by this pass.
869 // Remove the analysis not preserved by this pass
870 removeNotPreservedAnalysis(P);
871 recordAvailableAnalysis(P);
872
Devang Patel8cad70d2006-11-11 01:51:02 +0000873 // Add pass
874 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000875}
876
Devang Patele64d3052007-04-16 20:12:57 +0000877
878/// Populate RP with analysis pass that are required by
879/// pass P and are available. Populate RP_NotAvail with analysis
880/// pass that are required by pass P but are not available.
881void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
882 SmallVector<AnalysisID, 8> &RP_NotAvail,
883 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000884 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
885 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000886 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000887 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000888 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
889 RP.push_back(AnalysisPass);
890 else
Chris Lattner60987362009-03-06 05:53:14 +0000891 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000892 }
Devang Patelf58183d2006-12-12 23:09:32 +0000893
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000894 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000895 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000896 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000897 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
898 RP.push_back(AnalysisPass);
899 else
Chris Lattner60987362009-03-06 05:53:14 +0000900 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000901 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000902}
903
Devang Patel07f4f582006-11-14 21:49:36 +0000904// All Required analyses should be available to the pass as it runs! Here
905// we fill in the AnalysisImpls member of the pass so that it can
906// successfully use the getAnalysis() method to retrieve the
907// implementations it needs.
908//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000909void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000910 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
911
Chris Lattnercbd160f2008-08-08 05:33:04 +0000912 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000913 I = AnUsage->getRequiredSet().begin(),
914 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000915 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000916 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000917 // This may be analysis pass that is initialized on the fly.
918 // If that is not the case then it will raise an assert when it is used.
919 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000920 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000921 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000922 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000923 }
924}
925
Devang Patel640c5bb2006-12-08 22:30:11 +0000926/// Find the pass that implements Analysis AID. If desired pass is not found
927/// then return NULL.
928Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
929
930 // Check if AvailableAnalysis map has one entry.
931 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
932
933 if (I != AvailableAnalysis.end())
934 return I->second;
935
936 // Search Parents through TopLevelManager
937 if (SearchParent)
938 return TPM->findAnalysisPass(AID);
939
Devang Patel9d759b82006-12-09 00:09:12 +0000940 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000941}
942
Devang Patel991aeba2006-12-15 20:13:01 +0000943// Print list of passes that are last used by P.
944void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
945
Devang Patel8adae862007-07-20 18:04:54 +0000946 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000947
948 // If this is a on the fly manager then it does not have TPM.
949 if (!TPM)
950 return;
951
Devang Patel991aeba2006-12-15 20:13:01 +0000952 TPM->collectLastUses(LUses, P);
953
Devang Patel8adae862007-07-20 18:04:54 +0000954 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000955 E = LUses.end(); I != E; ++I) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000956 llvm::errs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +0000957 (*I)->dumpPassStructure(0);
958 }
959}
960
961void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +0000962 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000963 E = PassVector.end(); I != E; ++I) {
964 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
965 PMD->dumpPassArguments();
966 else
967 if (const PassInfo *PI = (*I)->getPassInfo())
968 if (!PI->isAnalysisGroup())
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000969 errs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +0000970 }
971}
972
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000973void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
974 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000975 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000976 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000977 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000978 errs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000979 switch (S1) {
980 case EXECUTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000981 errs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000982 break;
983 case MODIFICATION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000984 errs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000985 break;
986 case FREEING_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000987 errs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000988 break;
989 default:
990 break;
991 }
992 switch (S2) {
993 case ON_BASICBLOCK_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000994 errs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000995 break;
996 case ON_FUNCTION_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000997 errs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000998 break;
999 case ON_MODULE_MSG:
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001000 errs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001001 break;
1002 case ON_LOOP_MSG:
Chris Lattnerf82f27b2009-09-15 04:45:26 +00001003 errs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001004 break;
1005 case ON_CG_MSG:
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001006 errs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001007 break;
1008 default:
1009 break;
1010 }
Devang Patel991aeba2006-12-15 20:13:01 +00001011}
1012
Chris Lattner4c1e9542009-03-06 06:45:05 +00001013void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001014 if (PassDebugging < Details)
1015 return;
1016
1017 AnalysisUsage analysisUsage;
1018 P->getAnalysisUsage(analysisUsage);
1019 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1020}
1021
Chris Lattner4c1e9542009-03-06 06:45:05 +00001022void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001023 if (PassDebugging < Details)
1024 return;
1025
1026 AnalysisUsage analysisUsage;
1027 P->getAnalysisUsage(analysisUsage);
1028 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1029}
1030
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001031void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001032 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001033 assert(PassDebugging >= Details);
1034 if (Set.empty())
1035 return;
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001036 errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001037 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001038 if (i) errs() << ',';
1039 errs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001040 }
Chris Lattnerd4d966f2009-09-15 05:03:04 +00001041 errs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001042}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001043
Devang Patel004937b2007-07-27 20:06:09 +00001044/// Add RequiredPass into list of lower level passes required by pass P.
1045/// RequiredPass is run on the fly by Pass Manager when P requests it
1046/// through getAnalysis interface.
1047/// This should be handled by specific pass manager.
1048void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1049 if (TPM) {
1050 TPM->dumpArguments();
1051 TPM->dumpPasses();
1052 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001053
1054 // Module Level pass may required Function Level analysis info
1055 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1056 // to provide this on demand. In that case, in Pass manager terminology,
1057 // module level pass is requiring lower level analysis info managed by
1058 // lower level pass manager.
1059
1060 // When Pass manager is not able to order required analysis info, Pass manager
1061 // checks whether any lower level manager will be able to provide this
1062 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001063#ifndef NDEBUG
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001064 errs() << "Unable to schedule '" << RequiredPass->getPassName();
1065 errs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001066#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001067 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001068}
1069
Devang Patele7599552007-01-12 18:52:44 +00001070// Destructor
1071PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001072 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001073 E = PassVector.end(); I != E; ++I)
1074 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001075}
1076
Devang Patel9bdf7d42006-12-08 23:28:54 +00001077//===----------------------------------------------------------------------===//
1078// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001079// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1080Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001081 return PM.findAnalysisPass(ID, dir);
1082}
1083
Devang Patel92942812007-04-16 20:56:24 +00001084Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1085 Function &F) {
1086 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1087}
1088
Devang Patela1514cb2006-12-07 19:39:39 +00001089//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001090// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001091
Devang Patel6e5a1132006-11-07 21:31:57 +00001092/// Execute all of the passes scheduled for execution by invoking
1093/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1094/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001095bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001096 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001097 return false;
1098
Devang Patele9585592006-12-08 01:38:28 +00001099 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001100
Devang Patel6e5a1132006-11-07 21:31:57 +00001101 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001102 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1103 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001104
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001105 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001106 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001107
Devang Patelabfbe3b2006-12-16 00:56:26 +00001108 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001109
Chris Lattner4c1e9542009-03-06 06:45:05 +00001110 {
1111 // If the pass crashes, remember this.
1112 PassManagerPrettyStackEntry X(BP, *I);
1113
Dan Gohman277e7672009-09-28 00:07:05 +00001114 Timer *T = StartPassTimer(BP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001115 Changed |= BP->runOnBasicBlock(*I);
Dan Gohman277e7672009-09-28 00:07:05 +00001116 StopPassTimer(BP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001117 }
Devang Patel93a197c2006-12-14 00:08:04 +00001118
Devang Patel003a5592007-03-05 20:01:30 +00001119 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001120 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001121 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001122 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001123
Devang Patela273d1c2007-07-19 18:02:32 +00001124 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001125 removeNotPreservedAnalysis(BP);
1126 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001127 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001128 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001129
Devang Patel56d48ec2006-12-15 22:57:49 +00001130 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001131}
1132
Devang Patel475c4532006-12-08 00:59:05 +00001133// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001134bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001135 bool Changed = false;
1136
Chris Lattner4c1e9542009-03-06 06:45:05 +00001137 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1138 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001139
1140 return Changed;
1141}
1142
Duncan Sands51495602009-02-13 09:42:34 +00001143bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001144 bool Changed = false;
1145
Chris Lattner4c1e9542009-03-06 06:45:05 +00001146 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1147 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001148
1149 return Changed;
1150}
1151
Duncan Sands51495602009-02-13 09:42:34 +00001152bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001153 bool Changed = false;
1154
Devang Patelabfbe3b2006-12-16 00:56:26 +00001155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001157 Changed |= BP->doInitialization(F);
1158 }
1159
1160 return Changed;
1161}
1162
Duncan Sands51495602009-02-13 09:42:34 +00001163bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001164 bool Changed = false;
1165
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1167 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001168 Changed |= BP->doFinalization(F);
1169 }
1170
1171 return Changed;
1172}
1173
1174
Devang Patela1514cb2006-12-07 19:39:39 +00001175//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001176// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001177
Devang Patel4e12f862006-11-08 10:44:40 +00001178/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001179FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001180 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001181 // FPM is the top level manager.
1182 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001183
Dan Gohman565df952008-03-13 02:08:36 +00001184 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001185 FPM->setResolver(AR);
1186
Devang Patel1f653682006-12-08 18:57:16 +00001187 MP = P;
1188}
1189
Devang Patelb67904d2006-12-13 02:36:01 +00001190FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001191 delete FPM;
1192}
1193
Devang Patel4e12f862006-11-08 10:44:40 +00001194/// add - Add a pass to the queue of passes to run. This passes
1195/// ownership of the Pass to the PassManager. When the
1196/// PassManager_X is destroyed, the pass will be destroyed as well, so
1197/// there is no need to delete the pass. (TODO delete passes.)
1198/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001199void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001200 FPM->add(P);
1201}
1202
Devang Patel9f3083e2006-11-15 19:39:54 +00001203/// run - Execute all of the passes scheduled for execution. Keep
1204/// track of whether any of the passes modifies the function, and if
1205/// so, return true.
1206///
Devang Patelb67904d2006-12-13 02:36:01 +00001207bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001208 std::string errstr;
1209 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001210 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001211 }
Devang Patel272908d2006-12-08 22:57:48 +00001212 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001213}
1214
1215
Devang Patelff631ae2006-11-15 01:27:05 +00001216/// doInitialization - Run all of the initializers for the function passes.
1217///
Devang Patelb67904d2006-12-13 02:36:01 +00001218bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001219 return FPM->doInitialization(*MP->getModule());
1220}
1221
Dan Gohmane6656eb2007-07-30 14:51:13 +00001222/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001223///
Devang Patelb67904d2006-12-13 02:36:01 +00001224bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001225 return FPM->doFinalization(*MP->getModule());
1226}
1227
Devang Patela1514cb2006-12-07 19:39:39 +00001228//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001229// FunctionPassManagerImpl implementation
1230//
Duncan Sands51495602009-02-13 09:42:34 +00001231bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001232 bool Changed = false;
1233
Chris Lattner4c1e9542009-03-06 06:45:05 +00001234 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1235 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001236
1237 return Changed;
1238}
1239
Duncan Sands51495602009-02-13 09:42:34 +00001240bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001241 bool Changed = false;
1242
Chris Lattner4c1e9542009-03-06 06:45:05 +00001243 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1244 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001245
1246 return Changed;
1247}
1248
Devang Patelec9c58f2009-04-01 22:34:41 +00001249/// cleanup - After running all passes, clean up pass manager cache.
1250void FPPassManager::cleanup() {
1251 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1252 FunctionPass *FP = getContainedPass(Index);
1253 AnalysisResolver *AR = FP->getResolver();
1254 assert(AR && "Analysis Resolver is not set");
1255 AR->clearAnalysisImpls();
1256 }
1257}
1258
Torok Edwin24c78352009-06-29 18:49:09 +00001259void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1260 if (!wasRun)
1261 return;
1262 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1263 FPPassManager *FPPM = getContainedManager(Index);
1264 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1265 FPPM->getContainedPass(Index)->releaseMemory();
1266 }
1267 }
Torok Edwin896556e2009-06-29 21:05:10 +00001268 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001269}
1270
Devang Patel67d6a5e2006-12-19 19:46:59 +00001271// Execute all the passes managed by this top level manager.
1272// Return true if any function is modified by a pass.
1273bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001274 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001275 TimingInfo::createTheTimeInfo();
1276
1277 dumpArguments();
1278 dumpPasses();
1279
Devang Patele3068402006-12-21 00:16:50 +00001280 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001281 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1282 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001283
1284 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1285 getContainedManager(Index)->cleanup();
1286
Torok Edwin24c78352009-06-29 18:49:09 +00001287 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001288 return Changed;
1289}
1290
1291//===----------------------------------------------------------------------===//
1292// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001293
Devang Patel8c78a0b2007-05-03 01:11:54 +00001294char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001295/// Print passes managed by this manager
1296void FPPassManager::dumpPassStructure(unsigned Offset) {
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001297 llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001298 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1299 FunctionPass *FP = getContainedPass(Index);
1300 FP->dumpPassStructure(Offset + 1);
1301 dumpLastUses(FP, Offset+1);
1302 }
1303}
1304
1305
Devang Patel0c2012f2006-11-07 21:49:50 +00001306/// Execute all of the passes scheduled for execution by invoking
1307/// runOnFunction method. Keep track of whether any of the passes modifies
1308/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001309bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001310 if (F.isDeclaration())
1311 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001312
1313 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001314
Devang Patelcbbf2912008-03-20 01:09:53 +00001315 // Collect inherited analysis from Module level pass manager.
1316 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001317
Devang Patelabfbe3b2006-12-16 00:56:26 +00001318 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1319 FunctionPass *FP = getContainedPass(Index);
1320
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001321 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001322 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001323
Devang Patelabfbe3b2006-12-16 00:56:26 +00001324 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001325
Chris Lattner4c1e9542009-03-06 06:45:05 +00001326 {
1327 PassManagerPrettyStackEntry X(FP, F);
1328
Dan Gohman277e7672009-09-28 00:07:05 +00001329 Timer *T = StartPassTimer(FP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001330 Changed |= FP->runOnFunction(F);
Dan Gohman277e7672009-09-28 00:07:05 +00001331 StopPassTimer(FP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001332 }
Devang Patel93a197c2006-12-14 00:08:04 +00001333
Devang Patel003a5592007-03-05 20:01:30 +00001334 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001335 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001336 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001337
Devang Patela273d1c2007-07-19 18:02:32 +00001338 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001339 removeNotPreservedAnalysis(FP);
1340 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001341 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001342 }
1343 return Changed;
1344}
1345
Devang Patel67d6a5e2006-12-19 19:46:59 +00001346bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001347 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001348
Chris Lattner60987362009-03-06 05:53:14 +00001349 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1350 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001351
1352 return Changed |= doFinalization(M);
1353}
1354
Duncan Sands51495602009-02-13 09:42:34 +00001355bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001356 bool Changed = false;
1357
Chris Lattner4c1e9542009-03-06 06:45:05 +00001358 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1359 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001360
1361 return Changed;
1362}
1363
Duncan Sands51495602009-02-13 09:42:34 +00001364bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001365 bool Changed = false;
1366
Chris Lattner4c1e9542009-03-06 06:45:05 +00001367 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1368 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001369
Devang Patelff631ae2006-11-15 01:27:05 +00001370 return Changed;
1371}
1372
Devang Patela1514cb2006-12-07 19:39:39 +00001373//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001374// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001375
Devang Patel05e1a972006-11-07 22:03:15 +00001376/// Execute all of the passes scheduled for execution by invoking
1377/// runOnModule method. Keep track of whether any of the passes modifies
1378/// the module, and if so, return true.
1379bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001380MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001381 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001382
Torok Edwin24c78352009-06-29 18:49:09 +00001383 // Initialize on-the-fly passes
1384 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1385 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1386 I != E; ++I) {
1387 FunctionPassManagerImpl *FPP = I->second;
1388 Changed |= FPP->doInitialization(M);
1389 }
1390
Devang Patelabfbe3b2006-12-16 00:56:26 +00001391 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1392 ModulePass *MP = getContainedPass(Index);
1393
Dan Gohman929391a2008-01-29 12:09:55 +00001394 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1395 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001396 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001397
Devang Patelabfbe3b2006-12-16 00:56:26 +00001398 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001399
Chris Lattner4c1e9542009-03-06 06:45:05 +00001400 {
1401 PassManagerPrettyStackEntry X(MP, M);
Dan Gohman277e7672009-09-28 00:07:05 +00001402 Timer *T = StartPassTimer(MP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001403 Changed |= MP->runOnModule(M);
Dan Gohman277e7672009-09-28 00:07:05 +00001404 StopPassTimer(MP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001405 }
Devang Patel93a197c2006-12-14 00:08:04 +00001406
Devang Patel003a5592007-03-05 20:01:30 +00001407 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001408 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1409 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001410 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001411
Devang Patela273d1c2007-07-19 18:02:32 +00001412 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001413 removeNotPreservedAnalysis(MP);
1414 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001415 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001416 }
Torok Edwin24c78352009-06-29 18:49:09 +00001417
1418 // Finalize on-the-fly passes
1419 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1420 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1421 I != E; ++I) {
1422 FunctionPassManagerImpl *FPP = I->second;
1423 // We don't know when is the last time an on-the-fly pass is run,
1424 // so we need to releaseMemory / finalize here
1425 FPP->releaseMemoryOnTheFly();
1426 Changed |= FPP->doFinalization(M);
1427 }
Devang Patel05e1a972006-11-07 22:03:15 +00001428 return Changed;
1429}
1430
Devang Patele64d3052007-04-16 20:12:57 +00001431/// Add RequiredPass into list of lower level passes required by pass P.
1432/// RequiredPass is run on the fly by Pass Manager when P requests it
1433/// through getAnalysis interface.
1434void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001435 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1436 "Unable to handle Pass that requires lower level Analysis pass");
1437 assert((P->getPotentialPassManagerType() <
1438 RequiredPass->getPotentialPassManagerType()) &&
1439 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001440
Devang Patel68f72b12007-04-26 17:50:19 +00001441 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001442 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001443 FPP = new FunctionPassManagerImpl(0);
1444 // FPP is the top level manager.
1445 FPP->setTopLevelManager(FPP);
1446
Devang Patel69e9f6d2007-04-16 20:27:05 +00001447 OnTheFlyManagers[P] = FPP;
1448 }
Devang Patel68f72b12007-04-26 17:50:19 +00001449 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001450
Devang Patel68f72b12007-04-26 17:50:19 +00001451 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001452 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001453 LU.push_back(RequiredPass);
1454 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001455}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001456
1457/// Return function pass corresponding to PassInfo PI, that is
1458/// required by module pass MP. Instantiate analysis pass, by using
1459/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001460Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001461 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001462 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001463
Torok Edwin24c78352009-06-29 18:49:09 +00001464 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001465 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001466 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001467}
1468
1469
Devang Patela1514cb2006-12-07 19:39:39 +00001470//===----------------------------------------------------------------------===//
1471// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001472//
Devang Patelc290c8a2006-11-07 22:23:34 +00001473/// run - Execute all of the passes scheduled for execution. Keep track of
1474/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001475bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001476 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001477 TimingInfo::createTheTimeInfo();
1478
Devang Patelcfd70c42006-12-13 22:10:00 +00001479 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001480 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001481
Devang Patele3068402006-12-21 00:16:50 +00001482 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001483 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1484 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001485 return Changed;
1486}
Devang Patel376fefa2006-11-08 10:29:57 +00001487
Devang Patela1514cb2006-12-07 19:39:39 +00001488//===----------------------------------------------------------------------===//
1489// PassManager implementation
1490
Devang Patel376fefa2006-11-08 10:29:57 +00001491/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001492PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001493 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001494 // PM is the top level manager
1495 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001496}
1497
Devang Patelb67904d2006-12-13 02:36:01 +00001498PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001499 delete PM;
1500}
1501
Devang Patel376fefa2006-11-08 10:29:57 +00001502/// add - Add a pass to the queue of passes to run. This passes ownership of
1503/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1504/// will be destroyed as well, so there is no need to delete the pass. This
1505/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001506void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001507 PM->add(P);
1508}
1509
1510/// run - Execute all of the passes scheduled for execution. Keep track of
1511/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001512bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001513 return PM->run(M);
1514}
1515
Devang Patelb8817b92006-12-14 00:59:42 +00001516//===----------------------------------------------------------------------===//
1517// TimingInfo Class - This class is used to calculate information about the
1518// amount of time each pass takes to execute. This only happens with
1519// -time-passes is enabled on the command line.
1520//
1521bool llvm::TimePassesIsEnabled = false;
1522static cl::opt<bool,true>
1523EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1524 cl::desc("Time each pass, printing elapsed time for each on exit"));
1525
1526// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1527// a non null value (if the -time-passes option is enabled) or it leaves it
1528// null. It may be called multiple times.
1529void TimingInfo::createTheTimeInfo() {
1530 if (!TimePassesIsEnabled || TheTimeInfo) return;
1531
1532 // Constructed the first time this is called, iff -time-passes is enabled.
1533 // This guarantees that the object will be constructed before static globals,
1534 // thus it will be destroyed before them.
1535 static ManagedStatic<TimingInfo> TTI;
1536 TheTimeInfo = &*TTI;
1537}
1538
Devang Patel1c3633e2007-01-29 23:10:37 +00001539/// If TimingInfo is enabled then start pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001540Timer *llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001541 if (TheTimeInfo)
Dan Gohman277e7672009-09-28 00:07:05 +00001542 return TheTimeInfo->passStarted(P);
1543 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001544}
1545
1546/// If TimingInfo is enabled then stop pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001547void llvm::StopPassTimer(Pass *P, Timer *T) {
1548 if (T) T->stopTimer();
Devang Patel1c3633e2007-01-29 23:10:37 +00001549}
1550
Devang Patel1c56a632007-01-08 19:29:38 +00001551//===----------------------------------------------------------------------===//
1552// PMStack implementation
1553//
Devang Patelad98d232007-01-11 22:15:30 +00001554
Devang Patel1c56a632007-01-08 19:29:38 +00001555// Pop Pass Manager from the stack and clear its analysis info.
1556void PMStack::pop() {
1557
1558 PMDataManager *Top = this->top();
1559 Top->initializeAnalysisInfo();
1560
1561 S.pop_back();
1562}
1563
1564// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001565void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001566 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001567
Chris Lattner60987362009-03-06 05:53:14 +00001568 if (!this->empty()) {
1569 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001570
Chris Lattner60987362009-03-06 05:53:14 +00001571 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001572 TPM->addIndirectPassManager(PM);
1573 PM->setTopLevelManager(TPM);
1574 }
1575
Devang Patel15701b52007-01-11 00:19:00 +00001576 S.push_back(PM);
1577}
1578
1579// Dump content of the pass manager stack.
1580void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001581 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1582 E = S.end(); I != E; ++I)
1583 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1584
Devang Patel15701b52007-01-11 00:19:00 +00001585 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001586 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001587}
1588
Devang Patel1c56a632007-01-08 19:29:38 +00001589/// Find appropriate Module Pass Manager in the PM Stack and
1590/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001591void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001592 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001593 // Find Module Pass Manager
1594 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001595 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1596 if (TopPMType == PreferredType)
1597 break; // We found desired pass manager
1598 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001599 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001600 else
1601 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001602 }
Devang Patel18ff6362008-09-09 21:38:40 +00001603 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001604 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001605}
1606
Devang Patel3312f752007-01-16 21:43:18 +00001607/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1608/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001609void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001610 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001611
Devang Patela3286902008-09-09 17:56:50 +00001612 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001613 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001614 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1615 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001616 else
Devang Patel3312f752007-01-16 21:43:18 +00001617 break;
1618 }
1619 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1620
1621 // Create new Function Pass Manager
1622 if (!FPP) {
1623 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1624 PMDataManager *PMD = PMS.top();
1625
1626 // [1] Create new Function Pass Manager
1627 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001628 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001629
1630 // [2] Set up new manager's top level manager
1631 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1632 TPM->addIndirectPassManager(FPP);
1633
1634 // [3] Assign manager to manage this new manager. This may create
1635 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001636 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001637
1638 // [4] Push new manager into PMS
1639 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001640 }
1641
Devang Patel3312f752007-01-16 21:43:18 +00001642 // Assign FPP as the manager of this pass.
1643 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001644}
1645
Devang Patel3312f752007-01-16 21:43:18 +00001646/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001647/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001648void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001649 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001650 BBPassManager *BBP = NULL;
1651
Devang Patel15701b52007-01-11 00:19:00 +00001652 // Basic Pass Manager is a leaf pass manager. It does not handle
1653 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001654 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001655 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001656
Devang Patel3312f752007-01-16 21:43:18 +00001657 // If leaf manager is not Basic Block Pass manager then create new
1658 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001659
Devang Patel3312f752007-01-16 21:43:18 +00001660 if (!BBP) {
1661 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1662 PMDataManager *PMD = PMS.top();
1663
1664 // [1] Create new Basic Block Manager
1665 BBP = new BBPassManager(PMD->getDepth() + 1);
1666
1667 // [2] Set up new manager's top level manager
1668 // Basic Block Pass Manager does not live by itself
1669 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1670 TPM->addIndirectPassManager(BBP);
1671
Devang Patel15701b52007-01-11 00:19:00 +00001672 // [3] Assign manager to manage this new manager. This may create
1673 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001674 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001675
Devang Patel3312f752007-01-16 21:43:18 +00001676 // [4] Push new manager into PMS
1677 PMS.push(BBP);
1678 }
Devang Patel1c56a632007-01-08 19:29:38 +00001679
Devang Patel3312f752007-01-16 21:43:18 +00001680 // Assign BBP as the manager of this pass.
1681 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001682}
1683
Dan Gohmand3a20c92008-03-11 16:41:42 +00001684PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001685
1686/*===-- C Bindings --------------------------------------------------------===*/
1687
1688LLVMPassManagerRef LLVMCreatePassManager() {
1689 return wrap(new PassManager());
1690}
1691
1692LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1693 return wrap(new FunctionPassManager(unwrap(P)));
1694}
1695
1696int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1697 return unwrap<PassManager>(PM)->run(*unwrap(M));
1698}
1699
1700int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1701 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1702}
1703
1704int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1705 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1706}
1707
1708int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1709 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1710}
1711
1712void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1713 delete unwrap(PM);
1714}