blob: 1d461c3d4b37f76e7b7788447c5f742060e855d2 [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"
David Greene994e1bb2010-01-05 01:30:02 +000018#include "llvm/Support/Debug.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000019#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000020#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000021#include "llvm/ModuleProvider.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner4c1e9542009-03-06 06:45:05 +000024#include "llvm/Support/raw_ostream.h"
Owen Anderson0dd39fd2009-06-17 21:28:54 +000025#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000026#include "llvm/System/Threading.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000027#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000029#include <cstdio>
Devang Patelf60b5d92006-11-14 01:59:59 +000030#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000031using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000032
Devang Patele7599552007-01-12 18:52:44 +000033// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000034
Devang Patelf1567a52006-12-13 20:03:48 +000035namespace llvm {
36
37//===----------------------------------------------------------------------===//
38// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
43
Devang Patel03fb5872006-12-13 21:13:31 +000044// Different debug levels that can be enabled...
45enum PassDebugLevel {
46 None, Arguments, Structure, Executions, Details
47};
48
Devang Patelf1567a52006-12-13 20:03:48 +000049static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000050PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000051 cl::desc("Print PassManager debugging information"),
52 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000053 clEnumVal(None , "disable debug output"),
54 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
55 clEnumVal(Structure , "print pass structure before run()"),
56 clEnumVal(Executions, "print pass name before it is executed"),
57 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000058 clEnumValEnd));
59} // End of llvm namespace
60
Chris Lattnerd4d966f2009-09-15 05:03:04 +000061/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
62/// or higher is specified.
63bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
64 return PassDebugging >= Executions;
65}
66
67
68
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.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000109class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000110
111public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000112 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000113 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000114 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000115
Devang Patelca58e352006-11-08 10:05:38 +0000116 /// Execute all of the passes scheduled for execution. Keep track of
117 /// whether any of the passes modifies the function, and if so, return true.
118 bool runOnFunction(Function &F);
119
Devang Patelf9d96b92006-12-07 19:57:52 +0000120 /// Pass Manager itself does not invalidate any analysis info.
121 void getAnalysisUsage(AnalysisUsage &Info) const {
122 Info.setPreservesAll();
123 }
124
Devang Patel475c4532006-12-08 00:59:05 +0000125 bool doInitialization(Module &M);
126 bool doInitialization(Function &F);
127 bool doFinalization(Module &M);
128 bool doFinalization(Function &F);
129
Devang Patele3858e62007-02-01 22:08:25 +0000130 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000131 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000132 }
133
Devang Pateleda56172006-12-12 23:34:33 +0000134 // Print passes managed by this manager
135 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000136 llvm::dbgs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000137 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
138 BasicBlockPass *BP = getContainedPass(Index);
139 BP->dumpPassStructure(Offset + 1);
140 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000141 }
142 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000143
144 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000145 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000146 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
147 return BP;
148 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000149
Devang Patel28349ab2007-02-27 15:00:39 +0000150 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000151 return PMT_BasicBlockPassManager;
152 }
Devang Patelca58e352006-11-08 10:05:38 +0000153};
154
Devang Patel8c78a0b2007-05-03 01:11:54 +0000155char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000156}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000157
Devang Patele7599552007-01-12 18:52:44 +0000158namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000159
Devang Patel10c2ca62006-12-12 22:47:13 +0000160//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000162//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000163/// FunctionPassManagerImpl manages FPPassManagers
164class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000165 public PMDataManager,
166 public PMTopLevelManager {
Torok Edwin24c78352009-06-29 18:49:09 +0000167private:
168 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000169public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000170 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000171 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000172 Pass(&ID), PMDataManager(Depth),
Torok Edwin24c78352009-06-29 18:49:09 +0000173 PMTopLevelManager(TLM_Function), wasRun(false) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000174
175 /// add - Add a pass to the queue of passes to run. This passes ownership of
176 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
177 /// will be destroyed as well, so there is no need to delete the pass. This
178 /// implies that all passes MUST be allocated with 'new'.
179 void add(Pass *P) {
180 schedulePass(P);
181 }
182
Torok Edwin24c78352009-06-29 18:49:09 +0000183 // Prepare for running an on the fly pass, freeing memory if needed
184 // from a previous run.
185 void releaseMemoryOnTheFly();
186
Devang Patel67d6a5e2006-12-19 19:46:59 +0000187 /// run - Execute all of the passes scheduled for execution. Keep track of
188 /// whether any of the passes modifies the module, and if so, return true.
189 bool run(Function &F);
190
191 /// doInitialization - Run all of the initializers for the function passes.
192 ///
193 bool doInitialization(Module &M);
194
Dan Gohmane6656eb2007-07-30 14:51:13 +0000195 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000196 ///
197 bool doFinalization(Module &M);
198
199 /// Pass Manager itself does not invalidate any analysis info.
200 void getAnalysisUsage(AnalysisUsage &Info) const {
201 Info.setPreservesAll();
202 }
203
204 inline void addTopLevelPass(Pass *P) {
205
206 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
207
208 // P is a immutable pass and it will be managed by this
209 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000210 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000211 P->setResolver(AR);
212 initializeAnalysisImpl(P);
213 addImmutablePass(IP);
214 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000215 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000216 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000217 }
Devang Patel0f080042007-01-12 17:23:48 +0000218
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219 }
220
221 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000222 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
224 return FP;
225 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226};
227
Devang Patel8c78a0b2007-05-03 01:11:54 +0000228char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000229//===----------------------------------------------------------------------===//
230// MPPassManager
231//
232/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000233/// It batches all Module passes and function pass managers together and
234/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000235class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000236public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000237 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000238 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000239 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000240
241 // Delete on the fly managers.
242 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000243 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000244 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
245 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000246 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000247 delete FPP;
248 }
249 }
250
Devang Patelca58e352006-11-08 10:05:38 +0000251 /// run - Execute all of the passes scheduled for execution. Keep track of
252 /// whether any of the passes modifies the module, and if so, return true.
253 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000254
Devang Patelf9d96b92006-12-07 19:57:52 +0000255 /// Pass Manager itself does not invalidate any analysis info.
256 void getAnalysisUsage(AnalysisUsage &Info) const {
257 Info.setPreservesAll();
258 }
259
Devang Patele64d3052007-04-16 20:12:57 +0000260 /// Add RequiredPass into list of lower level passes required by pass P.
261 /// RequiredPass is run on the fly by Pass Manager when P requests it
262 /// through getAnalysis interface.
263 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
264
Devang Patel69e9f6d2007-04-16 20:27:05 +0000265 /// Return function pass corresponding to PassInfo PI, that is
266 /// required by module pass MP. Instantiate analysis pass, by using
267 /// its runOnFunction() for function F.
268 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
269
Devang Patele3858e62007-02-01 22:08:25 +0000270 virtual const char *getPassName() const {
271 return "Module Pass Manager";
272 }
273
Devang Pateleda56172006-12-12 23:34:33 +0000274 // Print passes managed by this manager
275 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000276 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000277 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
278 ModulePass *MP = getContainedPass(Index);
279 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000280 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
281 OnTheFlyManagers.find(MP);
282 if (I != OnTheFlyManagers.end())
283 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000284 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000285 }
286 }
287
Devang Patelabfbe3b2006-12-16 00:56:26 +0000288 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000289 assert(N < PassVector.size() && "Pass number out of range!");
290 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000291 }
292
Devang Patel28349ab2007-02-27 15:00:39 +0000293 virtual PassManagerType getPassManagerType() const {
294 return PMT_ModulePassManager;
295 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000296
297 private:
298 /// Collection of on the fly FPPassManagers. These managers manage
299 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000300 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000301};
302
Devang Patel8c78a0b2007-05-03 01:11:54 +0000303char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000304//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000305// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000306//
Devang Patel09f162c2007-05-01 21:15:47 +0000307
Devang Patel67d6a5e2006-12-19 19:46:59 +0000308/// PassManagerImpl manages MPPassManagers
309class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000310 public PMDataManager,
311 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000312
313public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000314 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000315 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000316 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000317
Devang Patel376fefa2006-11-08 10:29:57 +0000318 /// add - Add a pass to the queue of passes to run. This passes ownership of
319 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
320 /// will be destroyed as well, so there is no need to delete the pass. This
321 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000322 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000323 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000324 }
Devang Patel376fefa2006-11-08 10:29:57 +0000325
326 /// run - Execute all of the passes scheduled for execution. Keep track of
327 /// whether any of the passes modifies the module, and if so, return true.
328 bool run(Module &M);
329
Devang Patelf9d96b92006-12-07 19:57:52 +0000330 /// Pass Manager itself does not invalidate any analysis info.
331 void getAnalysisUsage(AnalysisUsage &Info) const {
332 Info.setPreservesAll();
333 }
334
Devang Patelabcd1d32006-12-07 21:27:23 +0000335 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000336 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000337
338 // P is a immutable pass and it will be managed by this
339 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000340 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000341 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000342 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000343 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000344 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000345 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000346 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000347 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000348 }
349
Devang Patel67d6a5e2006-12-19 19:46:59 +0000350 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000351 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000352 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
353 return MP;
354 }
Devang Patel376fefa2006-11-08 10:29:57 +0000355};
356
Devang Patel8c78a0b2007-05-03 01:11:54 +0000357char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000358} // End of llvm namespace
359
360namespace {
361
362//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000363/// TimingInfo Class - This class is used to calculate information about the
364/// amount of time each pass takes to execute. This only happens when
365/// -time-passes is enabled on the command line.
366///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000367
Owen Anderson5a6960f2009-06-18 20:51:00 +0000368static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000369
Nick Lewycky02d5f772009-10-25 06:33:48 +0000370class TimingInfo {
Devang Patel1c3633e2007-01-29 23:10:37 +0000371 std::map<Pass*, Timer> TimingData;
372 TimerGroup TG;
373
374public:
375 // Use 'create' member to get this.
376 TimingInfo() : TG("... Pass execution timing report ...") {}
377
378 // TimingDtor - Print out information about timing information
379 ~TimingInfo() {
380 // Delete all of the timers...
381 TimingData.clear();
382 // TimerGroup is deleted next, printing the report.
383 }
384
385 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
386 // to a non null value (if the -time-passes option is enabled) or it leaves it
387 // null. It may be called multiple times.
388 static void createTheTimeInfo();
389
Dan Gohman277e7672009-09-28 00:07:05 +0000390 /// passStarted - This method creates a timer for the given pass if it doesn't
391 /// already have one, and starts the timer.
392 Timer *passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000393 if (dynamic_cast<PMDataManager *>(P))
Dan Gohman277e7672009-09-28 00:07:05 +0000394 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000395
Owen Anderson5c96ef72009-07-07 18:33:04 +0000396 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000397 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
398 if (I == TimingData.end())
399 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
Dan Gohman277e7672009-09-28 00:07:05 +0000400 Timer *T = &I->second;
401 T->startTimer();
402 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000403 }
404};
405
Devang Patel1c3633e2007-01-29 23:10:37 +0000406} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000407
Dan Gohmand78c4002008-05-13 00:00:25 +0000408static TimingInfo *TheTimeInfo;
409
Devang Patela1514cb2006-12-07 19:39:39 +0000410//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000411// PMTopLevelManager implementation
412
Devang Patel4268fc02007-01-16 02:00:38 +0000413/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000414PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000415 if (t == TLM_Pass) {
416 MPPassManager *MPP = new MPPassManager(1);
417 MPP->setTopLevelManager(this);
418 addPassManager(MPP);
419 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000420 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000421 FPPassManager *FPP = new FPPassManager(1);
422 FPP->setTopLevelManager(this);
423 addPassManager(FPP);
424 activeStack.push(FPP);
425 }
426}
427
Devang Patelafb1f3622006-12-12 22:35:25 +0000428/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000429void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000430 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000431 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000432 E = AnalysisPasses.end(); I != E; ++I) {
433 Pass *AP = *I;
434 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000435
436 if (P == AP)
437 continue;
438
Devang Patelafb1f3622006-12-12 22:35:25 +0000439 // If AP is the last user of other passes then make P last user of
440 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000441 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000442 LUE = LastUser.end(); LUI != LUE; ++LUI) {
443 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000444 // DenseMap iterator is not invalidated here because
445 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000446 LastUser[LUI->first] = P;
447 }
448 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000449}
450
451/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000452void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000453 Pass *P) {
454 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
455 InversedLastUser.find(P);
456 if (DMI == InversedLastUser.end())
457 return;
458
459 SmallPtrSet<Pass *, 8> &LU = DMI->second;
460 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
461 E = LU.end(); I != E; ++I) {
462 LastUses.push_back(*I);
463 }
464
Devang Patelafb1f3622006-12-12 22:35:25 +0000465}
466
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000467AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
468 AnalysisUsage *AnUsage = NULL;
469 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
470 if (DMI != AnUsageMap.end())
471 AnUsage = DMI->second;
472 else {
473 AnUsage = new AnalysisUsage();
474 P->getAnalysisUsage(*AnUsage);
475 AnUsageMap[P] = AnUsage;
476 }
477 return AnUsage;
478}
479
Devang Patelafb1f3622006-12-12 22:35:25 +0000480/// Schedule pass P for execution. Make sure that passes required by
481/// P are run before P is run. Update analysis info maintained by
482/// the manager. Remove dead passes. This is a recursive function.
483void PMTopLevelManager::schedulePass(Pass *P) {
484
Devang Patel3312f752007-01-16 21:43:18 +0000485 // TODO : Allocate function manager for this pass, other wise required set
486 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000487
Devang Pateld74ede72007-03-06 01:06:16 +0000488 // Give pass a chance to prepare the stage.
489 P->preparePassManager(activeStack);
490
Devang Patel864970e2008-03-18 00:39:19 +0000491 // If P is an analysis pass and it is available then do not
492 // generate the analysis again. Stale analysis info should not be
493 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000494 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000495 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
496 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000497 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000498 }
Devang Patel864970e2008-03-18 00:39:19 +0000499
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000500 AnalysisUsage *AnUsage = findAnalysisUsage(P);
501
Devang Patelfdee7032008-08-14 23:07:48 +0000502 bool checkAnalysis = true;
503 while (checkAnalysis) {
504 checkAnalysis = false;
505
506 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
507 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
508 E = RequiredSet.end(); I != E; ++I) {
509
510 Pass *AnalysisPass = findAnalysisPass(*I);
511 if (!AnalysisPass) {
512 AnalysisPass = (*I)->createPass();
513 if (P->getPotentialPassManagerType () ==
514 AnalysisPass->getPotentialPassManagerType())
515 // Schedule analysis pass that is managed by the same pass manager.
516 schedulePass(AnalysisPass);
517 else if (P->getPotentialPassManagerType () >
518 AnalysisPass->getPotentialPassManagerType()) {
519 // Schedule analysis pass that is managed by a new manager.
520 schedulePass(AnalysisPass);
521 // Recheck analysis passes to ensure that required analysises that
522 // are already checked are still available.
523 checkAnalysis = true;
524 }
525 else
526 // Do not schedule this analysis. Lower level analsyis
527 // passes are run on the fly.
528 delete AnalysisPass;
529 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000530 }
531 }
532
533 // Now all required passes are available.
534 addTopLevelPass(P);
535}
536
537/// Find the pass that implements Analysis AID. Search immutable
538/// passes and all pass managers. If desired pass is not found
539/// then return NULL.
540Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
541
542 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000543 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000544 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000545 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000546 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000547 P = PMD->findAnalysisPass(AID, false);
548 }
549
550 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000551 for (SmallVector<PMDataManager *, 8>::iterator
552 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000553 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
554 P = (*I)->findAnalysisPass(AID, false);
555
Devang Patel0d29ae02008-08-12 15:44:31 +0000556 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000557 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
558 const PassInfo *PI = (*I)->getPassInfo();
559 if (PI == AID)
560 P = *I;
561
562 // If Pass not found then check the interfaces implemented by Immutable Pass
563 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000564 const std::vector<const PassInfo*> &ImmPI =
565 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000566 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
567 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000568 }
569 }
570
Devang Patelafb1f3622006-12-12 22:35:25 +0000571 return P;
572}
573
Devang Pateleda56172006-12-12 23:34:33 +0000574// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000575void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000576
Devang Patelfd4184322007-01-17 20:33:36 +0000577 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000578 return;
579
Devang Pateleda56172006-12-12 23:34:33 +0000580 // Print out the immutable passes
581 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
582 ImmutablePasses[i]->dumpPassStructure(0);
583 }
584
Dan Gohman73caf5f2008-03-13 01:48:32 +0000585 // Every class that derives from PMDataManager also derives from Pass
586 // (sometimes indirectly), but there's no inheritance relationship
587 // between PMDataManager and Pass, so we have to dynamic_cast to get
588 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000589 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000590 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000591 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000592}
593
Devang Patel991aeba2006-12-15 20:13:01 +0000594void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000595
Devang Patelfd4184322007-01-17 20:33:36 +0000596 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000597 return;
598
David Greene994e1bb2010-01-05 01:30:02 +0000599 dbgs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000600 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000601 E = PassManagers.end(); I != E; ++I)
602 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000603 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000604}
605
Devang Patele3068402006-12-21 00:16:50 +0000606void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000607 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000608 E = PassManagers.end(); I != E; ++I)
609 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000610
611 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000612 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000613 E = IndirectPassManagers.end(); I != E; ++I)
614 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000615
Chris Lattner60987362009-03-06 05:53:14 +0000616 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000617 DME = LastUser.end(); DMI != DME; ++DMI) {
618 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
619 InversedLastUser.find(DMI->second);
620 if (InvDMI != InversedLastUser.end()) {
621 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
622 L.insert(DMI->first);
623 } else {
624 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
625 InversedLastUser[DMI->second] = L;
626 }
627 }
Devang Patele3068402006-12-21 00:16:50 +0000628}
629
Devang Patele7599552007-01-12 18:52:44 +0000630/// Destructor
631PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000632 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000633 E = PassManagers.end(); I != E; ++I)
634 delete *I;
635
Devang Patel0d29ae02008-08-12 15:44:31 +0000636 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000637 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
638 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000639
640 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000641 DME = AnUsageMap.end(); DMI != DME; ++DMI)
642 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000643}
644
Devang Patelafb1f3622006-12-12 22:35:25 +0000645//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000646// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000647
Devang Patel643676c2006-11-11 01:10:19 +0000648/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000649void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000650 const PassInfo *PI = P->getPassInfo();
651 if (PI == 0) return;
652
653 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000654
Chris Lattner60987362009-03-06 05:53:14 +0000655 //This pass is the current implementation of all of the interfaces it
656 //implements as well.
657 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
658 for (unsigned i = 0, e = II.size(); i != e; ++i)
659 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000660}
661
Devang Patel9d9fc902007-03-06 17:52:53 +0000662// Return true if P preserves high level analysis used by other
663// passes managed by this manager
664bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000665 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000666 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000667 return true;
668
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000669 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000670 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000671 E = HigherLevelAnalysis.end(); I != E; ++I) {
672 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000673 if (!dynamic_cast<ImmutablePass*>(P1) &&
674 std::find(PreservedSet.begin(), PreservedSet.end(),
675 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000676 PreservedSet.end())
677 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000678 }
679
680 return true;
681}
682
Chris Lattner02eb94c2008-08-07 07:34:50 +0000683/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000684void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000685 // Don't do this unless assertions are enabled.
686#ifdef NDEBUG
687 return;
688#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000689 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
690 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000691
Devang Patelef432532007-07-19 05:36:09 +0000692 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000693 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000694 E = PreservedSet.end(); I != E; ++I) {
695 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000696 if (Pass *AP = findAnalysisPass(AID, true)) {
697
698 Timer *T = 0;
699 if (TheTimeInfo) T = TheTimeInfo->passStarted(AP);
Devang Patela273d1c2007-07-19 18:02:32 +0000700 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000701 if (T) T->stopTimer();
702 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000703 }
704}
705
Devang Patel67c79a42008-07-01 19:50:56 +0000706/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000707void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000708 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
709 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000710 return;
711
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000712 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000713 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000714 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000715 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000716 if (!dynamic_cast<ImmutablePass*>(Info->second)
717 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000718 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000719 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000720 if (PassDebugging >= Details) {
721 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000722 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
723 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000724 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000725 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000726 }
Devang Patel349170f2006-11-11 01:24:55 +0000727 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000728
729 // Check inherited analysis also. If P is not preserving analysis
730 // provided by parent manager then remove it here.
731 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
732
733 if (!InheritedAnalysis[Index])
734 continue;
735
736 for (std::map<AnalysisID, Pass*>::iterator
737 I = InheritedAnalysis[Index]->begin(),
738 E = InheritedAnalysis[Index]->end(); I != E; ) {
739 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000740 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
741 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000742 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000743 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000744 if (PassDebugging >= Details) {
745 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000746 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
747 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000748 }
Devang Patel01919d22007-03-08 19:05:01 +0000749 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000750 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000751 }
752 }
Devang Patelf68a3492006-11-07 22:35:17 +0000753}
754
Devang Patelca189262006-11-14 03:05:08 +0000755/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000756void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000757 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000758
Devang Patel8adae862007-07-20 18:04:54 +0000759 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000760
Devang Patel2ff44922007-04-16 20:39:59 +0000761 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000762 if (!TPM)
763 return;
764
Devang Patel17ad0962006-12-08 00:37:52 +0000765 TPM->collectLastUses(DeadPasses, P);
766
Devang Patel656a9172008-06-06 17:50:36 +0000767 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000768 dbgs() << " -*- '" << P->getPassName();
769 dbgs() << "' is the last user of following pass instances.";
770 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000771 }
772
Devang Patel8adae862007-07-20 18:04:54 +0000773 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000774 E = DeadPasses.end(); I != E; ++I)
775 freePass(*I, Msg, DBG_STR);
776}
Devang Patel200d3052006-12-13 23:50:44 +0000777
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000778void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000779 enum PassDebuggingString DBG_STR) {
780 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000781
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000782 {
783 // If the pass crashes releasing memory, remember this.
784 PassManagerPrettyStackEntry X(P);
785
Dan Gohman277e7672009-09-28 00:07:05 +0000786 Timer *T = StartPassTimer(P);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000787 P->releaseMemory();
Dan Gohman277e7672009-09-28 00:07:05 +0000788 StopPassTimer(P, T);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000789 }
790
791 if (const PassInfo *PI = P->getPassInfo()) {
792 // Remove the pass itself (if it is not already removed).
793 AvailableAnalysis.erase(PI);
794
795 // Remove all interfaces this pass implements, for which it is also
796 // listed as the available implementation.
797 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
798 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000799 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000800 AvailableAnalysis.find(II[i]);
801 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000802 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000803 }
Devang Patel17ad0962006-12-08 00:37:52 +0000804 }
Devang Patelca189262006-11-14 03:05:08 +0000805}
806
Devang Patel8f677ce2006-12-07 18:47:25 +0000807/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000808/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000809void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000810 // This manager is going to manage pass P. Set up analysis resolver
811 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000812 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000813 P->setResolver(AR);
814
Devang Patelec2b9a72007-03-05 22:57:49 +0000815 // If a FunctionPass F is the last user of ModulePass info M
816 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000817 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000818
Chris Lattner60987362009-03-06 05:53:14 +0000819 if (!ProcessAnalysis) {
820 // Add pass
821 PassVector.push_back(P);
822 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000823 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000824
Chris Lattner60987362009-03-06 05:53:14 +0000825 // At the moment, this pass is the last user of all required passes.
826 SmallVector<Pass *, 12> LastUses;
827 SmallVector<Pass *, 8> RequiredPasses;
828 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
829
830 unsigned PDepth = this->getDepth();
831
832 collectRequiredAnalysis(RequiredPasses,
833 ReqAnalysisNotAvailable, P);
834 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
835 E = RequiredPasses.end(); I != E; ++I) {
836 Pass *PRequired = *I;
837 unsigned RDepth = 0;
838
839 assert(PRequired->getResolver() && "Analysis Resolver is not set");
840 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
841 RDepth = DM.getDepth();
842
843 if (PDepth == RDepth)
844 LastUses.push_back(PRequired);
845 else if (PDepth > RDepth) {
846 // Let the parent claim responsibility of last use
847 TransferLastUses.push_back(PRequired);
848 // Keep track of higher level analysis used by this manager.
849 HigherLevelAnalysis.push_back(PRequired);
850 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000851 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000852 }
853
854 // Set P as P's last user until someone starts using P.
855 // However, if P is a Pass Manager then it does not need
856 // to record its last user.
857 if (!dynamic_cast<PMDataManager *>(P))
858 LastUses.push_back(P);
859 TPM->setLastUser(LastUses, P);
860
861 if (!TransferLastUses.empty()) {
862 Pass *My_PM = dynamic_cast<Pass *>(this);
863 TPM->setLastUser(TransferLastUses, My_PM);
864 TransferLastUses.clear();
865 }
866
867 // Now, take care of required analysises that are not available.
868 for (SmallVector<AnalysisID, 8>::iterator
869 I = ReqAnalysisNotAvailable.begin(),
870 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
871 Pass *AnalysisPass = (*I)->createPass();
872 this->addLowerLevelRequiredPass(P, AnalysisPass);
873 }
874
875 // Take a note of analysis required and made available by this pass.
876 // Remove the analysis not preserved by this pass
877 removeNotPreservedAnalysis(P);
878 recordAvailableAnalysis(P);
879
Devang Patel8cad70d2006-11-11 01:51:02 +0000880 // Add pass
881 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000882}
883
Devang Patele64d3052007-04-16 20:12:57 +0000884
885/// Populate RP with analysis pass that are required by
886/// pass P and are available. Populate RP_NotAvail with analysis
887/// pass that are required by pass P but are not available.
888void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
889 SmallVector<AnalysisID, 8> &RP_NotAvail,
890 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000891 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
892 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000893 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000894 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000895 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
896 RP.push_back(AnalysisPass);
897 else
Chris Lattner60987362009-03-06 05:53:14 +0000898 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000899 }
Devang Patelf58183d2006-12-12 23:09:32 +0000900
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000901 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000902 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000903 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000904 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
905 RP.push_back(AnalysisPass);
906 else
Chris Lattner60987362009-03-06 05:53:14 +0000907 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000908 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000909}
910
Devang Patel07f4f582006-11-14 21:49:36 +0000911// All Required analyses should be available to the pass as it runs! Here
912// we fill in the AnalysisImpls member of the pass so that it can
913// successfully use the getAnalysis() method to retrieve the
914// implementations it needs.
915//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000916void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000917 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
918
Chris Lattnercbd160f2008-08-08 05:33:04 +0000919 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000920 I = AnUsage->getRequiredSet().begin(),
921 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000922 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000923 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000924 // This may be analysis pass that is initialized on the fly.
925 // If that is not the case then it will raise an assert when it is used.
926 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000927 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000928 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000929 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000930 }
931}
932
Devang Patel640c5bb2006-12-08 22:30:11 +0000933/// Find the pass that implements Analysis AID. If desired pass is not found
934/// then return NULL.
935Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
936
937 // Check if AvailableAnalysis map has one entry.
938 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
939
940 if (I != AvailableAnalysis.end())
941 return I->second;
942
943 // Search Parents through TopLevelManager
944 if (SearchParent)
945 return TPM->findAnalysisPass(AID);
946
Devang Patel9d759b82006-12-09 00:09:12 +0000947 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000948}
949
Devang Patel991aeba2006-12-15 20:13:01 +0000950// Print list of passes that are last used by P.
951void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
952
Devang Patel8adae862007-07-20 18:04:54 +0000953 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000954
955 // If this is a on the fly manager then it does not have TPM.
956 if (!TPM)
957 return;
958
Devang Patel991aeba2006-12-15 20:13:01 +0000959 TPM->collectLastUses(LUses, P);
960
Devang Patel8adae862007-07-20 18:04:54 +0000961 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000962 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +0000963 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +0000964 (*I)->dumpPassStructure(0);
965 }
966}
967
968void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +0000969 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000970 E = PassVector.end(); I != E; ++I) {
971 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
972 PMD->dumpPassArguments();
973 else
974 if (const PassInfo *PI = (*I)->getPassInfo())
975 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +0000976 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +0000977 }
978}
979
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000980void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
981 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000982 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000983 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000984 return;
David Greene994e1bb2010-01-05 01:30:02 +0000985 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000986 switch (S1) {
987 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000988 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000989 break;
990 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000991 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000992 break;
993 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000994 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000995 break;
996 default:
997 break;
998 }
999 switch (S2) {
1000 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001001 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001002 break;
1003 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001004 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001005 break;
1006 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001007 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001008 break;
1009 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001010 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001011 break;
1012 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001013 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001014 break;
1015 default:
1016 break;
1017 }
Devang Patel991aeba2006-12-15 20:13:01 +00001018}
1019
Chris Lattner4c1e9542009-03-06 06:45:05 +00001020void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001021 if (PassDebugging < Details)
1022 return;
1023
1024 AnalysisUsage analysisUsage;
1025 P->getAnalysisUsage(analysisUsage);
1026 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1027}
1028
Chris Lattner4c1e9542009-03-06 06:45:05 +00001029void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001030 if (PassDebugging < Details)
1031 return;
1032
1033 AnalysisUsage analysisUsage;
1034 P->getAnalysisUsage(analysisUsage);
1035 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1036}
1037
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001038void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001039 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001040 assert(PassDebugging >= Details);
1041 if (Set.empty())
1042 return;
David Greene994e1bb2010-01-05 01:30:02 +00001043 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001044 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001045 if (i) dbgs() << ',';
1046 dbgs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001047 }
David Greene994e1bb2010-01-05 01:30:02 +00001048 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001049}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001050
Devang Patel004937b2007-07-27 20:06:09 +00001051/// Add RequiredPass into list of lower level passes required by pass P.
1052/// RequiredPass is run on the fly by Pass Manager when P requests it
1053/// through getAnalysis interface.
1054/// This should be handled by specific pass manager.
1055void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1056 if (TPM) {
1057 TPM->dumpArguments();
1058 TPM->dumpPasses();
1059 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001060
1061 // Module Level pass may required Function Level analysis info
1062 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1063 // to provide this on demand. In that case, in Pass manager terminology,
1064 // module level pass is requiring lower level analysis info managed by
1065 // lower level pass manager.
1066
1067 // When Pass manager is not able to order required analysis info, Pass manager
1068 // checks whether any lower level manager will be able to provide this
1069 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001070#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001071 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1072 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001073#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001074 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001075}
1076
Devang Patele7599552007-01-12 18:52:44 +00001077// Destructor
1078PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001079 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001080 E = PassVector.end(); I != E; ++I)
1081 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001082}
1083
Devang Patel9bdf7d42006-12-08 23:28:54 +00001084//===----------------------------------------------------------------------===//
1085// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001086// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1087Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001088 return PM.findAnalysisPass(ID, dir);
1089}
1090
Devang Patel92942812007-04-16 20:56:24 +00001091Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1092 Function &F) {
1093 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1094}
1095
Devang Patela1514cb2006-12-07 19:39:39 +00001096//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001097// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001098
Devang Patel6e5a1132006-11-07 21:31:57 +00001099/// Execute all of the passes scheduled for execution by invoking
1100/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1101/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001102bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001103 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001104 return false;
1105
Devang Patele9585592006-12-08 01:38:28 +00001106 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001107
Devang Patel6e5a1132006-11-07 21:31:57 +00001108 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001109 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1110 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001111
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001112 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001113 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001114
Devang Patelabfbe3b2006-12-16 00:56:26 +00001115 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001116
Chris Lattner4c1e9542009-03-06 06:45:05 +00001117 {
1118 // If the pass crashes, remember this.
1119 PassManagerPrettyStackEntry X(BP, *I);
1120
Dan Gohman277e7672009-09-28 00:07:05 +00001121 Timer *T = StartPassTimer(BP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001122 Changed |= BP->runOnBasicBlock(*I);
Dan Gohman277e7672009-09-28 00:07:05 +00001123 StopPassTimer(BP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001124 }
Devang Patel93a197c2006-12-14 00:08:04 +00001125
Devang Patel003a5592007-03-05 20:01:30 +00001126 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001127 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001128 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001129 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001130
Devang Patela273d1c2007-07-19 18:02:32 +00001131 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001132 removeNotPreservedAnalysis(BP);
1133 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001134 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001135 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001136
Bill Wendling6ce6d262009-12-25 13:50:18 +00001137 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001138}
1139
Devang Patel475c4532006-12-08 00:59:05 +00001140// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001141bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001142 bool Changed = false;
1143
Chris Lattner4c1e9542009-03-06 06:45:05 +00001144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1145 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001146
1147 return Changed;
1148}
1149
Duncan Sands51495602009-02-13 09:42:34 +00001150bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001151 bool Changed = false;
1152
Chris Lattner4c1e9542009-03-06 06:45:05 +00001153 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1154 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001155
1156 return Changed;
1157}
1158
Duncan Sands51495602009-02-13 09:42:34 +00001159bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001160 bool Changed = false;
1161
Devang Patelabfbe3b2006-12-16 00:56:26 +00001162 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1163 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001164 Changed |= BP->doInitialization(F);
1165 }
1166
1167 return Changed;
1168}
1169
Duncan Sands51495602009-02-13 09:42:34 +00001170bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001171 bool Changed = false;
1172
Devang Patelabfbe3b2006-12-16 00:56:26 +00001173 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1174 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001175 Changed |= BP->doFinalization(F);
1176 }
1177
1178 return Changed;
1179}
1180
1181
Devang Patela1514cb2006-12-07 19:39:39 +00001182//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001183// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001184
Devang Patel4e12f862006-11-08 10:44:40 +00001185/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001186FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001187 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001188 // FPM is the top level manager.
1189 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001190
Dan Gohman565df952008-03-13 02:08:36 +00001191 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001192 FPM->setResolver(AR);
1193
Devang Patel1f653682006-12-08 18:57:16 +00001194 MP = P;
1195}
1196
Devang Patelb67904d2006-12-13 02:36:01 +00001197FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001198 delete FPM;
1199}
1200
Devang Patel4e12f862006-11-08 10:44:40 +00001201/// add - Add a pass to the queue of passes to run. This passes
1202/// ownership of the Pass to the PassManager. When the
1203/// PassManager_X is destroyed, the pass will be destroyed as well, so
1204/// there is no need to delete the pass. (TODO delete passes.)
1205/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001206void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001207 FPM->add(P);
1208}
1209
Devang Patel9f3083e2006-11-15 19:39:54 +00001210/// run - Execute all of the passes scheduled for execution. Keep
1211/// track of whether any of the passes modifies the function, and if
1212/// so, return true.
1213///
Devang Patelb67904d2006-12-13 02:36:01 +00001214bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001215 std::string errstr;
1216 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001217 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001218 }
Devang Patel272908d2006-12-08 22:57:48 +00001219 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001220}
1221
1222
Devang Patelff631ae2006-11-15 01:27:05 +00001223/// doInitialization - Run all of the initializers for the function passes.
1224///
Devang Patelb67904d2006-12-13 02:36:01 +00001225bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001226 return FPM->doInitialization(*MP->getModule());
1227}
1228
Dan Gohmane6656eb2007-07-30 14:51:13 +00001229/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001230///
Devang Patelb67904d2006-12-13 02:36:01 +00001231bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001232 return FPM->doFinalization(*MP->getModule());
1233}
1234
Devang Patela1514cb2006-12-07 19:39:39 +00001235//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001236// FunctionPassManagerImpl implementation
1237//
Duncan Sands51495602009-02-13 09:42:34 +00001238bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001239 bool Changed = false;
1240
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001241 dumpArguments();
1242 dumpPasses();
1243
Chris Lattner4c1e9542009-03-06 06:45:05 +00001244 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1245 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001246
1247 return Changed;
1248}
1249
Duncan Sands51495602009-02-13 09:42:34 +00001250bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001251 bool Changed = false;
1252
Chris Lattner4c1e9542009-03-06 06:45:05 +00001253 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1254 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001255
1256 return Changed;
1257}
1258
Devang Patelec9c58f2009-04-01 22:34:41 +00001259/// cleanup - After running all passes, clean up pass manager cache.
1260void FPPassManager::cleanup() {
1261 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1262 FunctionPass *FP = getContainedPass(Index);
1263 AnalysisResolver *AR = FP->getResolver();
1264 assert(AR && "Analysis Resolver is not set");
1265 AR->clearAnalysisImpls();
1266 }
1267}
1268
Torok Edwin24c78352009-06-29 18:49:09 +00001269void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1270 if (!wasRun)
1271 return;
1272 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1273 FPPassManager *FPPM = getContainedManager(Index);
1274 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1275 FPPM->getContainedPass(Index)->releaseMemory();
1276 }
1277 }
Torok Edwin896556e2009-06-29 21:05:10 +00001278 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001279}
1280
Devang Patel67d6a5e2006-12-19 19:46:59 +00001281// Execute all the passes managed by this top level manager.
1282// Return true if any function is modified by a pass.
1283bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001284 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285 TimingInfo::createTheTimeInfo();
1286
Devang Patele3068402006-12-21 00:16:50 +00001287 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001288 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1289 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001290
1291 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1292 getContainedManager(Index)->cleanup();
1293
Torok Edwin24c78352009-06-29 18:49:09 +00001294 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001295 return Changed;
1296}
1297
1298//===----------------------------------------------------------------------===//
1299// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001300
Devang Patel8c78a0b2007-05-03 01:11:54 +00001301char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001302/// Print passes managed by this manager
1303void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001304 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001305 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1306 FunctionPass *FP = getContainedPass(Index);
1307 FP->dumpPassStructure(Offset + 1);
1308 dumpLastUses(FP, Offset+1);
1309 }
1310}
1311
1312
Devang Patel0c2012f2006-11-07 21:49:50 +00001313/// Execute all of the passes scheduled for execution by invoking
1314/// runOnFunction method. Keep track of whether any of the passes modifies
1315/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001316bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001317 if (F.isDeclaration())
1318 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001319
1320 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001321
Devang Patelcbbf2912008-03-20 01:09:53 +00001322 // Collect inherited analysis from Module level pass manager.
1323 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001324
Devang Patelabfbe3b2006-12-16 00:56:26 +00001325 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1326 FunctionPass *FP = getContainedPass(Index);
1327
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001328 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001329 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001330
Devang Patelabfbe3b2006-12-16 00:56:26 +00001331 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001332
Chris Lattner4c1e9542009-03-06 06:45:05 +00001333 {
1334 PassManagerPrettyStackEntry X(FP, F);
1335
Dan Gohman277e7672009-09-28 00:07:05 +00001336 Timer *T = StartPassTimer(FP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001337 Changed |= FP->runOnFunction(F);
Dan Gohman277e7672009-09-28 00:07:05 +00001338 StopPassTimer(FP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001339 }
Devang Patel93a197c2006-12-14 00:08:04 +00001340
Devang Patel003a5592007-03-05 20:01:30 +00001341 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001342 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001343 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001344
Devang Patela273d1c2007-07-19 18:02:32 +00001345 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001346 removeNotPreservedAnalysis(FP);
1347 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001348 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001349 }
1350 return Changed;
1351}
1352
Devang Patel67d6a5e2006-12-19 19:46:59 +00001353bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001354 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001355
Chris Lattner60987362009-03-06 05:53:14 +00001356 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1357 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001358
Bill Wendling6ce6d262009-12-25 13:50:18 +00001359 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001360}
1361
Duncan Sands51495602009-02-13 09:42:34 +00001362bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001363 bool Changed = false;
1364
Chris Lattner4c1e9542009-03-06 06:45:05 +00001365 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1366 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001367
1368 return Changed;
1369}
1370
Duncan Sands51495602009-02-13 09:42:34 +00001371bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001372 bool Changed = false;
1373
Chris Lattner4c1e9542009-03-06 06:45:05 +00001374 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1375 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001376
Devang Patelff631ae2006-11-15 01:27:05 +00001377 return Changed;
1378}
1379
Devang Patela1514cb2006-12-07 19:39:39 +00001380//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001381// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001382
Devang Patel05e1a972006-11-07 22:03:15 +00001383/// Execute all of the passes scheduled for execution by invoking
1384/// runOnModule method. Keep track of whether any of the passes modifies
1385/// the module, and if so, return true.
1386bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001387MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001388 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001389
Torok Edwin24c78352009-06-29 18:49:09 +00001390 // Initialize on-the-fly passes
1391 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1392 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1393 I != E; ++I) {
1394 FunctionPassManagerImpl *FPP = I->second;
1395 Changed |= FPP->doInitialization(M);
1396 }
1397
Devang Patelabfbe3b2006-12-16 00:56:26 +00001398 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1399 ModulePass *MP = getContainedPass(Index);
1400
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001401 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001402 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001403
Devang Patelabfbe3b2006-12-16 00:56:26 +00001404 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001405
Chris Lattner4c1e9542009-03-06 06:45:05 +00001406 {
1407 PassManagerPrettyStackEntry X(MP, M);
Dan Gohman277e7672009-09-28 00:07:05 +00001408 Timer *T = StartPassTimer(MP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001409 Changed |= MP->runOnModule(M);
Dan Gohman277e7672009-09-28 00:07:05 +00001410 StopPassTimer(MP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001411 }
Devang Patel93a197c2006-12-14 00:08:04 +00001412
Devang Patel003a5592007-03-05 20:01:30 +00001413 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001414 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001415 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001416 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001417
Devang Patela273d1c2007-07-19 18:02:32 +00001418 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001419 removeNotPreservedAnalysis(MP);
1420 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001421 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001422 }
Torok Edwin24c78352009-06-29 18:49:09 +00001423
1424 // Finalize on-the-fly passes
1425 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1426 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1427 I != E; ++I) {
1428 FunctionPassManagerImpl *FPP = I->second;
1429 // We don't know when is the last time an on-the-fly pass is run,
1430 // so we need to releaseMemory / finalize here
1431 FPP->releaseMemoryOnTheFly();
1432 Changed |= FPP->doFinalization(M);
1433 }
Devang Patel05e1a972006-11-07 22:03:15 +00001434 return Changed;
1435}
1436
Devang Patele64d3052007-04-16 20:12:57 +00001437/// Add RequiredPass into list of lower level passes required by pass P.
1438/// RequiredPass is run on the fly by Pass Manager when P requests it
1439/// through getAnalysis interface.
1440void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001441 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1442 "Unable to handle Pass that requires lower level Analysis pass");
1443 assert((P->getPotentialPassManagerType() <
1444 RequiredPass->getPotentialPassManagerType()) &&
1445 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001446
Devang Patel68f72b12007-04-26 17:50:19 +00001447 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001448 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001449 FPP = new FunctionPassManagerImpl(0);
1450 // FPP is the top level manager.
1451 FPP->setTopLevelManager(FPP);
1452
Devang Patel69e9f6d2007-04-16 20:27:05 +00001453 OnTheFlyManagers[P] = FPP;
1454 }
Devang Patel68f72b12007-04-26 17:50:19 +00001455 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001456
Devang Patel68f72b12007-04-26 17:50:19 +00001457 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001458 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001459 LU.push_back(RequiredPass);
1460 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001461}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001462
1463/// Return function pass corresponding to PassInfo PI, that is
1464/// required by module pass MP. Instantiate analysis pass, by using
1465/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001466Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001467 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001468 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001469
Torok Edwin24c78352009-06-29 18:49:09 +00001470 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001471 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001472 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001473}
1474
1475
Devang Patela1514cb2006-12-07 19:39:39 +00001476//===----------------------------------------------------------------------===//
1477// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001478//
Devang Patelc290c8a2006-11-07 22:23:34 +00001479/// run - Execute all of the passes scheduled for execution. Keep track of
1480/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001481bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001482 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001483 TimingInfo::createTheTimeInfo();
1484
Devang Patelcfd70c42006-12-13 22:10:00 +00001485 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001486 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001487
Devang Patele3068402006-12-21 00:16:50 +00001488 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001489 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1490 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001491 return Changed;
1492}
Devang Patel376fefa2006-11-08 10:29:57 +00001493
Devang Patela1514cb2006-12-07 19:39:39 +00001494//===----------------------------------------------------------------------===//
1495// PassManager implementation
1496
Devang Patel376fefa2006-11-08 10:29:57 +00001497/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001498PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001499 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001500 // PM is the top level manager
1501 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001502}
1503
Devang Patelb67904d2006-12-13 02:36:01 +00001504PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001505 delete PM;
1506}
1507
Devang Patel376fefa2006-11-08 10:29:57 +00001508/// add - Add a pass to the queue of passes to run. This passes ownership of
1509/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1510/// will be destroyed as well, so there is no need to delete the pass. This
1511/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001512void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001513 PM->add(P);
1514}
1515
1516/// run - Execute all of the passes scheduled for execution. Keep track of
1517/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001518bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001519 return PM->run(M);
1520}
1521
Devang Patelb8817b92006-12-14 00:59:42 +00001522//===----------------------------------------------------------------------===//
1523// TimingInfo Class - This class is used to calculate information about the
1524// amount of time each pass takes to execute. This only happens with
1525// -time-passes is enabled on the command line.
1526//
1527bool llvm::TimePassesIsEnabled = false;
1528static cl::opt<bool,true>
1529EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1530 cl::desc("Time each pass, printing elapsed time for each on exit"));
1531
1532// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1533// a non null value (if the -time-passes option is enabled) or it leaves it
1534// null. It may be called multiple times.
1535void TimingInfo::createTheTimeInfo() {
1536 if (!TimePassesIsEnabled || TheTimeInfo) return;
1537
1538 // Constructed the first time this is called, iff -time-passes is enabled.
1539 // This guarantees that the object will be constructed before static globals,
1540 // thus it will be destroyed before them.
1541 static ManagedStatic<TimingInfo> TTI;
1542 TheTimeInfo = &*TTI;
1543}
1544
Devang Patel1c3633e2007-01-29 23:10:37 +00001545/// If TimingInfo is enabled then start pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001546Timer *llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001547 if (TheTimeInfo)
Dan Gohman277e7672009-09-28 00:07:05 +00001548 return TheTimeInfo->passStarted(P);
1549 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001550}
1551
1552/// If TimingInfo is enabled then stop pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001553void llvm::StopPassTimer(Pass *P, Timer *T) {
1554 if (T) T->stopTimer();
Devang Patel1c3633e2007-01-29 23:10:37 +00001555}
1556
Devang Patel1c56a632007-01-08 19:29:38 +00001557//===----------------------------------------------------------------------===//
1558// PMStack implementation
1559//
Devang Patelad98d232007-01-11 22:15:30 +00001560
Devang Patel1c56a632007-01-08 19:29:38 +00001561// Pop Pass Manager from the stack and clear its analysis info.
1562void PMStack::pop() {
1563
1564 PMDataManager *Top = this->top();
1565 Top->initializeAnalysisInfo();
1566
1567 S.pop_back();
1568}
1569
1570// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001571void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001572 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001573
Chris Lattner60987362009-03-06 05:53:14 +00001574 if (!this->empty()) {
1575 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001576
Chris Lattner60987362009-03-06 05:53:14 +00001577 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001578 TPM->addIndirectPassManager(PM);
1579 PM->setTopLevelManager(TPM);
1580 }
1581
Devang Patel15701b52007-01-11 00:19:00 +00001582 S.push_back(PM);
1583}
1584
1585// Dump content of the pass manager stack.
1586void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001587 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1588 E = S.end(); I != E; ++I)
1589 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1590
Devang Patel15701b52007-01-11 00:19:00 +00001591 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001592 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001593}
1594
Devang Patel1c56a632007-01-08 19:29:38 +00001595/// Find appropriate Module Pass Manager in the PM Stack and
1596/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001597void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001598 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001599 // Find Module Pass Manager
1600 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001601 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1602 if (TopPMType == PreferredType)
1603 break; // We found desired pass manager
1604 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001605 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001606 else
1607 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001608 }
Devang Patel18ff6362008-09-09 21:38:40 +00001609 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001610 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001611}
1612
Devang Patel3312f752007-01-16 21:43:18 +00001613/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1614/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001615void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001616 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001617
Devang Patela3286902008-09-09 17:56:50 +00001618 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001619 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001620 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1621 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001622 else
Devang Patel3312f752007-01-16 21:43:18 +00001623 break;
1624 }
1625 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1626
1627 // Create new Function Pass Manager
1628 if (!FPP) {
1629 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1630 PMDataManager *PMD = PMS.top();
1631
1632 // [1] Create new Function Pass Manager
1633 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001634 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001635
1636 // [2] Set up new manager's top level manager
1637 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1638 TPM->addIndirectPassManager(FPP);
1639
1640 // [3] Assign manager to manage this new manager. This may create
1641 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001642 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001643
1644 // [4] Push new manager into PMS
1645 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001646 }
1647
Devang Patel3312f752007-01-16 21:43:18 +00001648 // Assign FPP as the manager of this pass.
1649 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001650}
1651
Devang Patel3312f752007-01-16 21:43:18 +00001652/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001653/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001654void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001655 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001656 BBPassManager *BBP = NULL;
1657
Devang Patel15701b52007-01-11 00:19:00 +00001658 // Basic Pass Manager is a leaf pass manager. It does not handle
1659 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001660 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001661 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001662
Devang Patel3312f752007-01-16 21:43:18 +00001663 // If leaf manager is not Basic Block Pass manager then create new
1664 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001665
Devang Patel3312f752007-01-16 21:43:18 +00001666 if (!BBP) {
1667 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1668 PMDataManager *PMD = PMS.top();
1669
1670 // [1] Create new Basic Block Manager
1671 BBP = new BBPassManager(PMD->getDepth() + 1);
1672
1673 // [2] Set up new manager's top level manager
1674 // Basic Block Pass Manager does not live by itself
1675 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1676 TPM->addIndirectPassManager(BBP);
1677
Devang Patel15701b52007-01-11 00:19:00 +00001678 // [3] Assign manager to manage this new manager. This may create
1679 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001680 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001681
Devang Patel3312f752007-01-16 21:43:18 +00001682 // [4] Push new manager into PMS
1683 PMS.push(BBP);
1684 }
Devang Patel1c56a632007-01-08 19:29:38 +00001685
Devang Patel3312f752007-01-16 21:43:18 +00001686 // Assign BBP as the manager of this pass.
1687 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001688}
1689
Dan Gohmand3a20c92008-03-11 16:41:42 +00001690PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001691
1692/*===-- C Bindings --------------------------------------------------------===*/
1693
1694LLVMPassManagerRef LLVMCreatePassManager() {
1695 return wrap(new PassManager());
1696}
1697
1698LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1699 return wrap(new FunctionPassManager(unwrap(P)));
1700}
1701
1702int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1703 return unwrap<PassManager>(PM)->run(*unwrap(M));
1704}
1705
1706int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1707 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1708}
1709
1710int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1711 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1712}
1713
1714int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1715 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1716}
1717
1718void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1719 delete unwrap(PM);
1720}