blob: 7aa8cb3b01012035da76e6d9c4bbf9df163a39a4 [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) {
Chris Lattner21889d72010-01-22 04:55:08 +0000205 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000206 // P is a immutable pass and it will be managed by this
207 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000208 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000209 P->setResolver(AR);
210 initializeAnalysisImpl(P);
211 addImmutablePass(IP);
212 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000213 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000214 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000215 }
Devang Patel0f080042007-01-12 17:23:48 +0000216
Devang Patel67d6a5e2006-12-19 19:46:59 +0000217 }
218
219 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000220 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
222 return FP;
223 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000224};
225
Devang Patel8c78a0b2007-05-03 01:11:54 +0000226char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227//===----------------------------------------------------------------------===//
228// MPPassManager
229//
230/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000231/// It batches all Module passes and function pass managers together and
232/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000234public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000235 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000236 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000237 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000238
239 // Delete on the fly managers.
240 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000241 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000242 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
243 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000244 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000245 delete FPP;
246 }
247 }
248
Devang Patelca58e352006-11-08 10:05:38 +0000249 /// run - Execute all of the passes scheduled for execution. Keep track of
250 /// whether any of the passes modifies the module, and if so, return true.
251 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000252
Devang Patelf9d96b92006-12-07 19:57:52 +0000253 /// Pass Manager itself does not invalidate any analysis info.
254 void getAnalysisUsage(AnalysisUsage &Info) const {
255 Info.setPreservesAll();
256 }
257
Devang Patele64d3052007-04-16 20:12:57 +0000258 /// Add RequiredPass into list of lower level passes required by pass P.
259 /// RequiredPass is run on the fly by Pass Manager when P requests it
260 /// through getAnalysis interface.
261 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
262
Devang Patel69e9f6d2007-04-16 20:27:05 +0000263 /// Return function pass corresponding to PassInfo PI, that is
264 /// required by module pass MP. Instantiate analysis pass, by using
265 /// its runOnFunction() for function F.
266 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
267
Devang Patele3858e62007-02-01 22:08:25 +0000268 virtual const char *getPassName() const {
269 return "Module Pass Manager";
270 }
271
Devang Pateleda56172006-12-12 23:34:33 +0000272 // Print passes managed by this manager
273 void dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +0000274 llvm::dbgs() << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000275 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
276 ModulePass *MP = getContainedPass(Index);
277 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000278 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
279 OnTheFlyManagers.find(MP);
280 if (I != OnTheFlyManagers.end())
281 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000282 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000283 }
284 }
285
Devang Patelabfbe3b2006-12-16 00:56:26 +0000286 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000287 assert(N < PassVector.size() && "Pass number out of range!");
288 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000289 }
290
Devang Patel28349ab2007-02-27 15:00:39 +0000291 virtual PassManagerType getPassManagerType() const {
292 return PMT_ModulePassManager;
293 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000294
295 private:
296 /// Collection of on the fly FPPassManagers. These managers manage
297 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000298 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000299};
300
Devang Patel8c78a0b2007-05-03 01:11:54 +0000301char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000302//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000303// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000304//
Devang Patel09f162c2007-05-01 21:15:47 +0000305
Devang Patel67d6a5e2006-12-19 19:46:59 +0000306/// PassManagerImpl manages MPPassManagers
307class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000308 public PMDataManager,
309 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000310
311public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000312 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000313 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000314 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000315
Devang Patel376fefa2006-11-08 10:29:57 +0000316 /// add - Add a pass to the queue of passes to run. This passes ownership of
317 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
318 /// will be destroyed as well, so there is no need to delete the pass. This
319 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000320 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000321 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000322 }
Devang Patel376fefa2006-11-08 10:29:57 +0000323
324 /// run - Execute all of the passes scheduled for execution. Keep track of
325 /// whether any of the passes modifies the module, and if so, return true.
326 bool run(Module &M);
327
Devang Patelf9d96b92006-12-07 19:57:52 +0000328 /// Pass Manager itself does not invalidate any analysis info.
329 void getAnalysisUsage(AnalysisUsage &Info) const {
330 Info.setPreservesAll();
331 }
332
Devang Patelabcd1d32006-12-07 21:27:23 +0000333 inline void addTopLevelPass(Pass *P) {
Chris Lattner21889d72010-01-22 04:55:08 +0000334 if (ImmutablePass *IP = P->getAsImmutablePass()) {
Devang Pateld440cd92006-12-08 23:53:00 +0000335 // P is a immutable pass and it will be managed by this
336 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000337 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000338 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000339 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000340 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000341 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000342 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000343 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000344 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000345 }
346
Devang Patel67d6a5e2006-12-19 19:46:59 +0000347 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000348 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000349 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
350 return MP;
351 }
Devang Patel376fefa2006-11-08 10:29:57 +0000352};
353
Devang Patel8c78a0b2007-05-03 01:11:54 +0000354char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000355} // End of llvm namespace
356
357namespace {
358
359//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000360/// TimingInfo Class - This class is used to calculate information about the
361/// amount of time each pass takes to execute. This only happens when
362/// -time-passes is enabled on the command line.
363///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000364
Owen Anderson5a6960f2009-06-18 20:51:00 +0000365static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000366
Nick Lewycky02d5f772009-10-25 06:33:48 +0000367class TimingInfo {
Devang Patel1c3633e2007-01-29 23:10:37 +0000368 std::map<Pass*, Timer> TimingData;
369 TimerGroup TG;
370
371public:
372 // Use 'create' member to get this.
373 TimingInfo() : TG("... Pass execution timing report ...") {}
374
375 // TimingDtor - Print out information about timing information
376 ~TimingInfo() {
377 // Delete all of the timers...
378 TimingData.clear();
379 // TimerGroup is deleted next, printing the report.
380 }
381
382 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
383 // to a non null value (if the -time-passes option is enabled) or it leaves it
384 // null. It may be called multiple times.
385 static void createTheTimeInfo();
386
Dan Gohman277e7672009-09-28 00:07:05 +0000387 /// passStarted - This method creates a timer for the given pass if it doesn't
388 /// already have one, and starts the timer.
389 Timer *passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000390 if (dynamic_cast<PMDataManager *>(P))
Dan Gohman277e7672009-09-28 00:07:05 +0000391 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000392
Owen Anderson5c96ef72009-07-07 18:33:04 +0000393 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Devang Patel1c3633e2007-01-29 23:10:37 +0000394 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
395 if (I == TimingData.end())
396 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
Dan Gohman277e7672009-09-28 00:07:05 +0000397 Timer *T = &I->second;
398 T->startTimer();
399 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000400 }
401};
402
Devang Patel1c3633e2007-01-29 23:10:37 +0000403} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000404
Dan Gohmand78c4002008-05-13 00:00:25 +0000405static TimingInfo *TheTimeInfo;
406
Devang Patela1514cb2006-12-07 19:39:39 +0000407//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000408// PMTopLevelManager implementation
409
Devang Patel4268fc02007-01-16 02:00:38 +0000410/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000411PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000412 if (t == TLM_Pass) {
413 MPPassManager *MPP = new MPPassManager(1);
414 MPP->setTopLevelManager(this);
415 addPassManager(MPP);
416 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000417 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000418 FPPassManager *FPP = new FPPassManager(1);
419 FPP->setTopLevelManager(this);
420 addPassManager(FPP);
421 activeStack.push(FPP);
422 }
423}
424
Devang Patelafb1f3622006-12-12 22:35:25 +0000425/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000426void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000427 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000428 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000429 E = AnalysisPasses.end(); I != E; ++I) {
430 Pass *AP = *I;
431 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000432
433 if (P == AP)
434 continue;
435
Devang Patelafb1f3622006-12-12 22:35:25 +0000436 // If AP is the last user of other passes then make P last user of
437 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000438 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000439 LUE = LastUser.end(); LUI != LUE; ++LUI) {
440 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000441 // DenseMap iterator is not invalidated here because
442 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000443 LastUser[LUI->first] = P;
444 }
445 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000446}
447
448/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000449void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000450 Pass *P) {
451 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
452 InversedLastUser.find(P);
453 if (DMI == InversedLastUser.end())
454 return;
455
456 SmallPtrSet<Pass *, 8> &LU = DMI->second;
457 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
458 E = LU.end(); I != E; ++I) {
459 LastUses.push_back(*I);
460 }
461
Devang Patelafb1f3622006-12-12 22:35:25 +0000462}
463
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000464AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
465 AnalysisUsage *AnUsage = NULL;
466 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
467 if (DMI != AnUsageMap.end())
468 AnUsage = DMI->second;
469 else {
470 AnUsage = new AnalysisUsage();
471 P->getAnalysisUsage(*AnUsage);
472 AnUsageMap[P] = AnUsage;
473 }
474 return AnUsage;
475}
476
Devang Patelafb1f3622006-12-12 22:35:25 +0000477/// Schedule pass P for execution. Make sure that passes required by
478/// P are run before P is run. Update analysis info maintained by
479/// the manager. Remove dead passes. This is a recursive function.
480void PMTopLevelManager::schedulePass(Pass *P) {
481
Devang Patel3312f752007-01-16 21:43:18 +0000482 // TODO : Allocate function manager for this pass, other wise required set
483 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000484
Devang Pateld74ede72007-03-06 01:06:16 +0000485 // Give pass a chance to prepare the stage.
486 P->preparePassManager(activeStack);
487
Devang Patel864970e2008-03-18 00:39:19 +0000488 // If P is an analysis pass and it is available then do not
489 // generate the analysis again. Stale analysis info should not be
490 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000491 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000492 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
493 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000494 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000495 }
Devang Patel864970e2008-03-18 00:39:19 +0000496
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000497 AnalysisUsage *AnUsage = findAnalysisUsage(P);
498
Devang Patelfdee7032008-08-14 23:07:48 +0000499 bool checkAnalysis = true;
500 while (checkAnalysis) {
501 checkAnalysis = false;
502
503 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
504 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
505 E = RequiredSet.end(); I != E; ++I) {
506
507 Pass *AnalysisPass = findAnalysisPass(*I);
508 if (!AnalysisPass) {
509 AnalysisPass = (*I)->createPass();
510 if (P->getPotentialPassManagerType () ==
511 AnalysisPass->getPotentialPassManagerType())
512 // Schedule analysis pass that is managed by the same pass manager.
513 schedulePass(AnalysisPass);
514 else if (P->getPotentialPassManagerType () >
515 AnalysisPass->getPotentialPassManagerType()) {
516 // Schedule analysis pass that is managed by a new manager.
517 schedulePass(AnalysisPass);
518 // Recheck analysis passes to ensure that required analysises that
519 // are already checked are still available.
520 checkAnalysis = true;
521 }
522 else
523 // Do not schedule this analysis. Lower level analsyis
524 // passes are run on the fly.
525 delete AnalysisPass;
526 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000527 }
528 }
529
530 // Now all required passes are available.
531 addTopLevelPass(P);
532}
533
534/// Find the pass that implements Analysis AID. Search immutable
535/// passes and all pass managers. If desired pass is not found
536/// then return NULL.
537Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
538
539 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000540 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000541 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000542 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000543 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000544 P = PMD->findAnalysisPass(AID, false);
545 }
546
547 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000548 for (SmallVector<PMDataManager *, 8>::iterator
549 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000550 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
551 P = (*I)->findAnalysisPass(AID, false);
552
Devang Patel0d29ae02008-08-12 15:44:31 +0000553 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000554 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
555 const PassInfo *PI = (*I)->getPassInfo();
556 if (PI == AID)
557 P = *I;
558
559 // If Pass not found then check the interfaces implemented by Immutable Pass
560 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000561 const std::vector<const PassInfo*> &ImmPI =
562 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000563 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
564 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000565 }
566 }
567
Devang Patelafb1f3622006-12-12 22:35:25 +0000568 return P;
569}
570
Devang Pateleda56172006-12-12 23:34:33 +0000571// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000572void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000573
Devang Patelfd4184322007-01-17 20:33:36 +0000574 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000575 return;
576
Devang Pateleda56172006-12-12 23:34:33 +0000577 // Print out the immutable passes
578 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
579 ImmutablePasses[i]->dumpPassStructure(0);
580 }
581
Dan Gohman73caf5f2008-03-13 01:48:32 +0000582 // Every class that derives from PMDataManager also derives from Pass
583 // (sometimes indirectly), but there's no inheritance relationship
584 // between PMDataManager and Pass, so we have to dynamic_cast to get
585 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000586 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000587 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000588 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000589}
590
Devang Patel991aeba2006-12-15 20:13:01 +0000591void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000592
Devang Patelfd4184322007-01-17 20:33:36 +0000593 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000594 return;
595
David Greene994e1bb2010-01-05 01:30:02 +0000596 dbgs() << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000597 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000598 E = PassManagers.end(); I != E; ++I)
599 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000600 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000601}
602
Devang Patele3068402006-12-21 00:16:50 +0000603void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000604 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000605 E = PassManagers.end(); I != E; ++I)
606 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000607
608 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000609 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000610 E = IndirectPassManagers.end(); I != E; ++I)
611 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000612
Chris Lattner60987362009-03-06 05:53:14 +0000613 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000614 DME = LastUser.end(); DMI != DME; ++DMI) {
615 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
616 InversedLastUser.find(DMI->second);
617 if (InvDMI != InversedLastUser.end()) {
618 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
619 L.insert(DMI->first);
620 } else {
621 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
622 InversedLastUser[DMI->second] = L;
623 }
624 }
Devang Patele3068402006-12-21 00:16:50 +0000625}
626
Devang Patele7599552007-01-12 18:52:44 +0000627/// Destructor
628PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000629 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000630 E = PassManagers.end(); I != E; ++I)
631 delete *I;
632
Devang Patel0d29ae02008-08-12 15:44:31 +0000633 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000634 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
635 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000636
637 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000638 DME = AnUsageMap.end(); DMI != DME; ++DMI)
639 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000640}
641
Devang Patelafb1f3622006-12-12 22:35:25 +0000642//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000643// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000644
Devang Patel643676c2006-11-11 01:10:19 +0000645/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000646void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000647 const PassInfo *PI = P->getPassInfo();
648 if (PI == 0) return;
649
650 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000651
Chris Lattner60987362009-03-06 05:53:14 +0000652 //This pass is the current implementation of all of the interfaces it
653 //implements as well.
654 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
655 for (unsigned i = 0, e = II.size(); i != e; ++i)
656 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000657}
658
Devang Patel9d9fc902007-03-06 17:52:53 +0000659// Return true if P preserves high level analysis used by other
660// passes managed by this manager
661bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000662 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000663 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000664 return true;
665
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000666 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000667 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000668 E = HigherLevelAnalysis.end(); I != E; ++I) {
669 Pass *P1 = *I;
Chris Lattner21889d72010-01-22 04:55:08 +0000670 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000671 std::find(PreservedSet.begin(), PreservedSet.end(),
672 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000673 PreservedSet.end())
674 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000675 }
676
677 return true;
678}
679
Chris Lattner02eb94c2008-08-07 07:34:50 +0000680/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000681void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000682 // Don't do this unless assertions are enabled.
683#ifdef NDEBUG
684 return;
685#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000686 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
687 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000688
Devang Patelef432532007-07-19 05:36:09 +0000689 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000690 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000691 E = PreservedSet.end(); I != E; ++I) {
692 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000693 if (Pass *AP = findAnalysisPass(AID, true)) {
694
695 Timer *T = 0;
696 if (TheTimeInfo) T = TheTimeInfo->passStarted(AP);
Devang Patela273d1c2007-07-19 18:02:32 +0000697 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000698 if (T) T->stopTimer();
699 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000700 }
701}
702
Devang Patel67c79a42008-07-01 19:50:56 +0000703/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000704void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000705 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
706 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000707 return;
708
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000709 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000710 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000711 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000712 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000713 if (Info->second->getAsImmutablePass() == 0 &&
714 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000715 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000716 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000717 if (PassDebugging >= Details) {
718 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000719 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
720 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000721 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000722 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000723 }
Devang Patel349170f2006-11-11 01:24:55 +0000724 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000725
726 // Check inherited analysis also. If P is not preserving analysis
727 // provided by parent manager then remove it here.
728 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
729
730 if (!InheritedAnalysis[Index])
731 continue;
732
733 for (std::map<AnalysisID, Pass*>::iterator
734 I = InheritedAnalysis[Index]->begin(),
735 E = InheritedAnalysis[Index]->end(); I != E; ) {
736 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner21889d72010-01-22 04:55:08 +0000737 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman929391a2008-01-29 12:09:55 +0000738 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000739 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000740 // Remove this analysis
Andreas Neustifter46651412009-12-04 06:58:24 +0000741 if (PassDebugging >= Details) {
742 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000743 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
744 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000745 }
Devang Patel01919d22007-03-08 19:05:01 +0000746 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000747 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000748 }
749 }
Devang Patelf68a3492006-11-07 22:35:17 +0000750}
751
Devang Patelca189262006-11-14 03:05:08 +0000752/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000753void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000754 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000755
Devang Patel8adae862007-07-20 18:04:54 +0000756 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000757
Devang Patel2ff44922007-04-16 20:39:59 +0000758 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000759 if (!TPM)
760 return;
761
Devang Patel17ad0962006-12-08 00:37:52 +0000762 TPM->collectLastUses(DeadPasses, P);
763
Devang Patel656a9172008-06-06 17:50:36 +0000764 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000765 dbgs() << " -*- '" << P->getPassName();
766 dbgs() << "' is the last user of following pass instances.";
767 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000768 }
769
Devang Patel8adae862007-07-20 18:04:54 +0000770 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000771 E = DeadPasses.end(); I != E; ++I)
772 freePass(*I, Msg, DBG_STR);
773}
Devang Patel200d3052006-12-13 23:50:44 +0000774
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000775void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000776 enum PassDebuggingString DBG_STR) {
777 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000778
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000779 {
780 // If the pass crashes releasing memory, remember this.
781 PassManagerPrettyStackEntry X(P);
782
Dan Gohman277e7672009-09-28 00:07:05 +0000783 Timer *T = StartPassTimer(P);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000784 P->releaseMemory();
Dan Gohman277e7672009-09-28 00:07:05 +0000785 StopPassTimer(P, T);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000786 }
787
788 if (const PassInfo *PI = P->getPassInfo()) {
789 // Remove the pass itself (if it is not already removed).
790 AvailableAnalysis.erase(PI);
791
792 // Remove all interfaces this pass implements, for which it is also
793 // listed as the available implementation.
794 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
795 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patelc3e3ca92008-10-06 20:36:36 +0000796 std::map<AnalysisID, Pass*>::iterator Pos =
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000797 AvailableAnalysis.find(II[i]);
798 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000799 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000800 }
Devang Patel17ad0962006-12-08 00:37:52 +0000801 }
Devang Patelca189262006-11-14 03:05:08 +0000802}
803
Devang Patel8f677ce2006-12-07 18:47:25 +0000804/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000805/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000806void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000807 // This manager is going to manage pass P. Set up analysis resolver
808 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000809 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000810 P->setResolver(AR);
811
Devang Patelec2b9a72007-03-05 22:57:49 +0000812 // If a FunctionPass F is the last user of ModulePass info M
813 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000814 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000815
Chris Lattner60987362009-03-06 05:53:14 +0000816 if (!ProcessAnalysis) {
817 // Add pass
818 PassVector.push_back(P);
819 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000820 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000821
Chris Lattner60987362009-03-06 05:53:14 +0000822 // At the moment, this pass is the last user of all required passes.
823 SmallVector<Pass *, 12> LastUses;
824 SmallVector<Pass *, 8> RequiredPasses;
825 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
826
827 unsigned PDepth = this->getDepth();
828
829 collectRequiredAnalysis(RequiredPasses,
830 ReqAnalysisNotAvailable, P);
831 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
832 E = RequiredPasses.end(); I != E; ++I) {
833 Pass *PRequired = *I;
834 unsigned RDepth = 0;
835
836 assert(PRequired->getResolver() && "Analysis Resolver is not set");
837 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
838 RDepth = DM.getDepth();
839
840 if (PDepth == RDepth)
841 LastUses.push_back(PRequired);
842 else if (PDepth > RDepth) {
843 // Let the parent claim responsibility of last use
844 TransferLastUses.push_back(PRequired);
845 // Keep track of higher level analysis used by this manager.
846 HigherLevelAnalysis.push_back(PRequired);
847 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000848 llvm_unreachable("Unable to accomodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000849 }
850
851 // Set P as P's last user until someone starts using P.
852 // However, if P is a Pass Manager then it does not need
853 // to record its last user.
854 if (!dynamic_cast<PMDataManager *>(P))
855 LastUses.push_back(P);
856 TPM->setLastUser(LastUses, P);
857
858 if (!TransferLastUses.empty()) {
859 Pass *My_PM = dynamic_cast<Pass *>(this);
860 TPM->setLastUser(TransferLastUses, My_PM);
861 TransferLastUses.clear();
862 }
863
864 // Now, take care of required analysises that are not available.
865 for (SmallVector<AnalysisID, 8>::iterator
866 I = ReqAnalysisNotAvailable.begin(),
867 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
868 Pass *AnalysisPass = (*I)->createPass();
869 this->addLowerLevelRequiredPass(P, AnalysisPass);
870 }
871
872 // Take a note of analysis required and made available by this pass.
873 // Remove the analysis not preserved by this pass
874 removeNotPreservedAnalysis(P);
875 recordAvailableAnalysis(P);
876
Devang Patel8cad70d2006-11-11 01:51:02 +0000877 // Add pass
878 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000879}
880
Devang Patele64d3052007-04-16 20:12:57 +0000881
882/// Populate RP with analysis pass that are required by
883/// pass P and are available. Populate RP_NotAvail with analysis
884/// pass that are required by pass P but are not available.
885void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
886 SmallVector<AnalysisID, 8> &RP_NotAvail,
887 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000888 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
889 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000890 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000891 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000892 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
893 RP.push_back(AnalysisPass);
894 else
Chris Lattner60987362009-03-06 05:53:14 +0000895 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000896 }
Devang Patelf58183d2006-12-12 23:09:32 +0000897
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000898 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000899 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000900 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000901 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
902 RP.push_back(AnalysisPass);
903 else
Chris Lattner60987362009-03-06 05:53:14 +0000904 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000905 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000906}
907
Devang Patel07f4f582006-11-14 21:49:36 +0000908// All Required analyses should be available to the pass as it runs! Here
909// we fill in the AnalysisImpls member of the pass so that it can
910// successfully use the getAnalysis() method to retrieve the
911// implementations it needs.
912//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000913void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000914 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
915
Chris Lattnercbd160f2008-08-08 05:33:04 +0000916 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000917 I = AnUsage->getRequiredSet().begin(),
918 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000919 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000920 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000921 // This may be analysis pass that is initialized on the fly.
922 // If that is not the case then it will raise an assert when it is used.
923 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000924 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000925 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000926 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000927 }
928}
929
Devang Patel640c5bb2006-12-08 22:30:11 +0000930/// Find the pass that implements Analysis AID. If desired pass is not found
931/// then return NULL.
932Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
933
934 // Check if AvailableAnalysis map has one entry.
935 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
936
937 if (I != AvailableAnalysis.end())
938 return I->second;
939
940 // Search Parents through TopLevelManager
941 if (SearchParent)
942 return TPM->findAnalysisPass(AID);
943
Devang Patel9d759b82006-12-09 00:09:12 +0000944 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000945}
946
Devang Patel991aeba2006-12-15 20:13:01 +0000947// Print list of passes that are last used by P.
948void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
949
Devang Patel8adae862007-07-20 18:04:54 +0000950 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000951
952 // If this is a on the fly manager then it does not have TPM.
953 if (!TPM)
954 return;
955
Devang Patel991aeba2006-12-15 20:13:01 +0000956 TPM->collectLastUses(LUses, P);
957
Devang Patel8adae862007-07-20 18:04:54 +0000958 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000959 E = LUses.end(); I != E; ++I) {
David Greene994e1bb2010-01-05 01:30:02 +0000960 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Devang Patel991aeba2006-12-15 20:13:01 +0000961 (*I)->dumpPassStructure(0);
962 }
963}
964
965void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +0000966 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000967 E = PassVector.end(); I != E; ++I) {
968 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
969 PMD->dumpPassArguments();
970 else
971 if (const PassInfo *PI = (*I)->getPassInfo())
972 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +0000973 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +0000974 }
975}
976
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000977void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
978 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000979 StringRef Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000980 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000981 return;
David Greene994e1bb2010-01-05 01:30:02 +0000982 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000983 switch (S1) {
984 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000985 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000986 break;
987 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000988 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000989 break;
990 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000991 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000992 break;
993 default:
994 break;
995 }
996 switch (S2) {
997 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +0000998 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000999 break;
1000 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001001 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001002 break;
1003 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001004 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001005 break;
1006 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001007 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001008 break;
1009 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001010 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001011 break;
1012 default:
1013 break;
1014 }
Devang Patel991aeba2006-12-15 20:13:01 +00001015}
1016
Chris Lattner4c1e9542009-03-06 06:45:05 +00001017void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001018 if (PassDebugging < Details)
1019 return;
1020
1021 AnalysisUsage analysisUsage;
1022 P->getAnalysisUsage(analysisUsage);
1023 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1024}
1025
Chris Lattner4c1e9542009-03-06 06:45:05 +00001026void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001027 if (PassDebugging < Details)
1028 return;
1029
1030 AnalysisUsage analysisUsage;
1031 P->getAnalysisUsage(analysisUsage);
1032 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1033}
1034
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001035void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001036 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001037 assert(PassDebugging >= Details);
1038 if (Set.empty())
1039 return;
David Greene994e1bb2010-01-05 01:30:02 +00001040 dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001041 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001042 if (i) dbgs() << ',';
1043 dbgs() << ' ' << Set[i]->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001044 }
David Greene994e1bb2010-01-05 01:30:02 +00001045 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001046}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001047
Devang Patel004937b2007-07-27 20:06:09 +00001048/// Add RequiredPass into list of lower level passes required by pass P.
1049/// RequiredPass is run on the fly by Pass Manager when P requests it
1050/// through getAnalysis interface.
1051/// This should be handled by specific pass manager.
1052void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1053 if (TPM) {
1054 TPM->dumpArguments();
1055 TPM->dumpPasses();
1056 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001057
1058 // Module Level pass may required Function Level analysis info
1059 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1060 // to provide this on demand. In that case, in Pass manager terminology,
1061 // module level pass is requiring lower level analysis info managed by
1062 // lower level pass manager.
1063
1064 // When Pass manager is not able to order required analysis info, Pass manager
1065 // checks whether any lower level manager will be able to provide this
1066 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001067#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001068 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1069 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001070#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001071 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001072}
1073
Devang Patele7599552007-01-12 18:52:44 +00001074// Destructor
1075PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001076 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001077 E = PassVector.end(); I != E; ++I)
1078 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001079}
1080
Devang Patel9bdf7d42006-12-08 23:28:54 +00001081//===----------------------------------------------------------------------===//
1082// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001083// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1084Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001085 return PM.findAnalysisPass(ID, dir);
1086}
1087
Devang Patel92942812007-04-16 20:56:24 +00001088Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1089 Function &F) {
1090 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1091}
1092
Devang Patela1514cb2006-12-07 19:39:39 +00001093//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001094// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001095
Devang Patel6e5a1132006-11-07 21:31:57 +00001096/// Execute all of the passes scheduled for execution by invoking
1097/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1098/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001099bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001100 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001101 return false;
1102
Devang Patele9585592006-12-08 01:38:28 +00001103 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001104
Devang Patel6e5a1132006-11-07 21:31:57 +00001105 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001106 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1107 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001108
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001109 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001110 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001111
Devang Patelabfbe3b2006-12-16 00:56:26 +00001112 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001113
Chris Lattner4c1e9542009-03-06 06:45:05 +00001114 {
1115 // If the pass crashes, remember this.
1116 PassManagerPrettyStackEntry X(BP, *I);
1117
Dan Gohman277e7672009-09-28 00:07:05 +00001118 Timer *T = StartPassTimer(BP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001119 Changed |= BP->runOnBasicBlock(*I);
Dan Gohman277e7672009-09-28 00:07:05 +00001120 StopPassTimer(BP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001121 }
Devang Patel93a197c2006-12-14 00:08:04 +00001122
Devang Patel003a5592007-03-05 20:01:30 +00001123 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001124 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001125 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001126 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001127
Devang Patela273d1c2007-07-19 18:02:32 +00001128 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001129 removeNotPreservedAnalysis(BP);
1130 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001131 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001132 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001133
Bill Wendling6ce6d262009-12-25 13:50:18 +00001134 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001135}
1136
Devang Patel475c4532006-12-08 00:59:05 +00001137// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001138bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001139 bool Changed = false;
1140
Chris Lattner4c1e9542009-03-06 06:45:05 +00001141 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1142 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001143
1144 return Changed;
1145}
1146
Duncan Sands51495602009-02-13 09:42:34 +00001147bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001148 bool Changed = false;
1149
Chris Lattner4c1e9542009-03-06 06:45:05 +00001150 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1151 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001152
1153 return Changed;
1154}
1155
Duncan Sands51495602009-02-13 09:42:34 +00001156bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001157 bool Changed = false;
1158
Devang Patelabfbe3b2006-12-16 00:56:26 +00001159 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1160 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001161 Changed |= BP->doInitialization(F);
1162 }
1163
1164 return Changed;
1165}
1166
Duncan Sands51495602009-02-13 09:42:34 +00001167bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001168 bool Changed = false;
1169
Devang Patelabfbe3b2006-12-16 00:56:26 +00001170 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1171 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001172 Changed |= BP->doFinalization(F);
1173 }
1174
1175 return Changed;
1176}
1177
1178
Devang Patela1514cb2006-12-07 19:39:39 +00001179//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001180// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001181
Devang Patel4e12f862006-11-08 10:44:40 +00001182/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001183FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001184 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001185 // FPM is the top level manager.
1186 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001187
Dan Gohman565df952008-03-13 02:08:36 +00001188 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001189 FPM->setResolver(AR);
1190
Devang Patel1f653682006-12-08 18:57:16 +00001191 MP = P;
1192}
1193
Devang Patelb67904d2006-12-13 02:36:01 +00001194FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001195 delete FPM;
1196}
1197
Devang Patel4e12f862006-11-08 10:44:40 +00001198/// add - Add a pass to the queue of passes to run. This passes
1199/// ownership of the Pass to the PassManager. When the
1200/// PassManager_X is destroyed, the pass will be destroyed as well, so
1201/// there is no need to delete the pass. (TODO delete passes.)
1202/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001203void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001204 FPM->add(P);
1205}
1206
Devang Patel9f3083e2006-11-15 19:39:54 +00001207/// run - Execute all of the passes scheduled for execution. Keep
1208/// track of whether any of the passes modifies the function, and if
1209/// so, return true.
1210///
Devang Patelb67904d2006-12-13 02:36:01 +00001211bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001212 std::string errstr;
1213 if (MP->materializeFunction(&F, &errstr)) {
Torok Edwin6dd27302009-07-08 18:01:40 +00001214 llvm_report_error("Error reading bitcode file: " + errstr);
Devang Patel9f3083e2006-11-15 19:39:54 +00001215 }
Devang Patel272908d2006-12-08 22:57:48 +00001216 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001217}
1218
1219
Devang Patelff631ae2006-11-15 01:27:05 +00001220/// doInitialization - Run all of the initializers for the function passes.
1221///
Devang Patelb67904d2006-12-13 02:36:01 +00001222bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001223 return FPM->doInitialization(*MP->getModule());
1224}
1225
Dan Gohmane6656eb2007-07-30 14:51:13 +00001226/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001227///
Devang Patelb67904d2006-12-13 02:36:01 +00001228bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001229 return FPM->doFinalization(*MP->getModule());
1230}
1231
Devang Patela1514cb2006-12-07 19:39:39 +00001232//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001233// FunctionPassManagerImpl implementation
1234//
Duncan Sands51495602009-02-13 09:42:34 +00001235bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001236 bool Changed = false;
1237
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001238 dumpArguments();
1239 dumpPasses();
1240
Chris Lattner4c1e9542009-03-06 06:45:05 +00001241 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1242 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001243
1244 return Changed;
1245}
1246
Duncan Sands51495602009-02-13 09:42:34 +00001247bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001248 bool Changed = false;
1249
Chris Lattner4c1e9542009-03-06 06:45:05 +00001250 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1251 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001252
1253 return Changed;
1254}
1255
Devang Patelec9c58f2009-04-01 22:34:41 +00001256/// cleanup - After running all passes, clean up pass manager cache.
1257void FPPassManager::cleanup() {
1258 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1259 FunctionPass *FP = getContainedPass(Index);
1260 AnalysisResolver *AR = FP->getResolver();
1261 assert(AR && "Analysis Resolver is not set");
1262 AR->clearAnalysisImpls();
1263 }
1264}
1265
Torok Edwin24c78352009-06-29 18:49:09 +00001266void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1267 if (!wasRun)
1268 return;
1269 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1270 FPPassManager *FPPM = getContainedManager(Index);
1271 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1272 FPPM->getContainedPass(Index)->releaseMemory();
1273 }
1274 }
Torok Edwin896556e2009-06-29 21:05:10 +00001275 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001276}
1277
Devang Patel67d6a5e2006-12-19 19:46:59 +00001278// Execute all the passes managed by this top level manager.
1279// Return true if any function is modified by a pass.
1280bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001281 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001282 TimingInfo::createTheTimeInfo();
1283
Devang Patele3068402006-12-21 00:16:50 +00001284 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001285 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1286 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patelec9c58f2009-04-01 22:34:41 +00001287
1288 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1289 getContainedManager(Index)->cleanup();
1290
Torok Edwin24c78352009-06-29 18:49:09 +00001291 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001292 return Changed;
1293}
1294
1295//===----------------------------------------------------------------------===//
1296// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001297
Devang Patel8c78a0b2007-05-03 01:11:54 +00001298char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001299/// Print passes managed by this manager
1300void FPPassManager::dumpPassStructure(unsigned Offset) {
David Greene994e1bb2010-01-05 01:30:02 +00001301 llvm::dbgs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001302 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1303 FunctionPass *FP = getContainedPass(Index);
1304 FP->dumpPassStructure(Offset + 1);
1305 dumpLastUses(FP, Offset+1);
1306 }
1307}
1308
1309
Devang Patel0c2012f2006-11-07 21:49:50 +00001310/// Execute all of the passes scheduled for execution by invoking
1311/// runOnFunction method. Keep track of whether any of the passes modifies
1312/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001313bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001314 if (F.isDeclaration())
1315 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001316
1317 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001318
Devang Patelcbbf2912008-03-20 01:09:53 +00001319 // Collect inherited analysis from Module level pass manager.
1320 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001321
Devang Patelabfbe3b2006-12-16 00:56:26 +00001322 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1323 FunctionPass *FP = getContainedPass(Index);
1324
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001325 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001326 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001327
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001329
Chris Lattner4c1e9542009-03-06 06:45:05 +00001330 {
1331 PassManagerPrettyStackEntry X(FP, F);
1332
Dan Gohman277e7672009-09-28 00:07:05 +00001333 Timer *T = StartPassTimer(FP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001334 Changed |= FP->runOnFunction(F);
Dan Gohman277e7672009-09-28 00:07:05 +00001335 StopPassTimer(FP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001336 }
Devang Patel93a197c2006-12-14 00:08:04 +00001337
Devang Patel003a5592007-03-05 20:01:30 +00001338 if (Changed)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001339 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001340 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001341
Devang Patela273d1c2007-07-19 18:02:32 +00001342 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001343 removeNotPreservedAnalysis(FP);
1344 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001345 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001346 }
1347 return Changed;
1348}
1349
Devang Patel67d6a5e2006-12-19 19:46:59 +00001350bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001351 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001352
Chris Lattner60987362009-03-06 05:53:14 +00001353 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1354 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001355
Bill Wendling6ce6d262009-12-25 13:50:18 +00001356 return doFinalization(M) || Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001357}
1358
Duncan Sands51495602009-02-13 09:42:34 +00001359bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001360 bool Changed = false;
1361
Chris Lattner4c1e9542009-03-06 06:45:05 +00001362 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1363 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001364
1365 return Changed;
1366}
1367
Duncan Sands51495602009-02-13 09:42:34 +00001368bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001369 bool Changed = false;
1370
Chris Lattner4c1e9542009-03-06 06:45:05 +00001371 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1372 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001373
Devang Patelff631ae2006-11-15 01:27:05 +00001374 return Changed;
1375}
1376
Devang Patela1514cb2006-12-07 19:39:39 +00001377//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001378// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001379
Devang Patel05e1a972006-11-07 22:03:15 +00001380/// Execute all of the passes scheduled for execution by invoking
1381/// runOnModule method. Keep track of whether any of the passes modifies
1382/// the module, and if so, return true.
1383bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001384MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001385 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001386
Torok Edwin24c78352009-06-29 18:49:09 +00001387 // Initialize on-the-fly passes
1388 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1389 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1390 I != E; ++I) {
1391 FunctionPassManagerImpl *FPP = I->second;
1392 Changed |= FPP->doInitialization(M);
1393 }
1394
Devang Patelabfbe3b2006-12-16 00:56:26 +00001395 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1396 ModulePass *MP = getContainedPass(Index);
1397
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001398 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001399 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001400
Devang Patelabfbe3b2006-12-16 00:56:26 +00001401 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001402
Chris Lattner4c1e9542009-03-06 06:45:05 +00001403 {
1404 PassManagerPrettyStackEntry X(MP, M);
Dan Gohman277e7672009-09-28 00:07:05 +00001405 Timer *T = StartPassTimer(MP);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001406 Changed |= MP->runOnModule(M);
Dan Gohman277e7672009-09-28 00:07:05 +00001407 StopPassTimer(MP, T);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001408 }
Devang Patel93a197c2006-12-14 00:08:04 +00001409
Devang Patel003a5592007-03-05 20:01:30 +00001410 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001411 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001412 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001413 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001414
Devang Patela273d1c2007-07-19 18:02:32 +00001415 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001416 removeNotPreservedAnalysis(MP);
1417 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001418 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001419 }
Torok Edwin24c78352009-06-29 18:49:09 +00001420
1421 // Finalize on-the-fly passes
1422 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1423 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1424 I != E; ++I) {
1425 FunctionPassManagerImpl *FPP = I->second;
1426 // We don't know when is the last time an on-the-fly pass is run,
1427 // so we need to releaseMemory / finalize here
1428 FPP->releaseMemoryOnTheFly();
1429 Changed |= FPP->doFinalization(M);
1430 }
Devang Patel05e1a972006-11-07 22:03:15 +00001431 return Changed;
1432}
1433
Devang Patele64d3052007-04-16 20:12:57 +00001434/// Add RequiredPass into list of lower level passes required by pass P.
1435/// RequiredPass is run on the fly by Pass Manager when P requests it
1436/// through getAnalysis interface.
1437void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001438 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1439 "Unable to handle Pass that requires lower level Analysis pass");
1440 assert((P->getPotentialPassManagerType() <
1441 RequiredPass->getPotentialPassManagerType()) &&
1442 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001443
Devang Patel68f72b12007-04-26 17:50:19 +00001444 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001445 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001446 FPP = new FunctionPassManagerImpl(0);
1447 // FPP is the top level manager.
1448 FPP->setTopLevelManager(FPP);
1449
Devang Patel69e9f6d2007-04-16 20:27:05 +00001450 OnTheFlyManagers[P] = FPP;
1451 }
Devang Patel68f72b12007-04-26 17:50:19 +00001452 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001453
Devang Patel68f72b12007-04-26 17:50:19 +00001454 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001455 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001456 LU.push_back(RequiredPass);
1457 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001458}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001459
1460/// Return function pass corresponding to PassInfo PI, that is
1461/// required by module pass MP. Instantiate analysis pass, by using
1462/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001463Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001464 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001465 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001466
Torok Edwin24c78352009-06-29 18:49:09 +00001467 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001468 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001469 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001470}
1471
1472
Devang Patela1514cb2006-12-07 19:39:39 +00001473//===----------------------------------------------------------------------===//
1474// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001475//
Devang Patelc290c8a2006-11-07 22:23:34 +00001476/// run - Execute all of the passes scheduled for execution. Keep track of
1477/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001478bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001479 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001480 TimingInfo::createTheTimeInfo();
1481
Devang Patelcfd70c42006-12-13 22:10:00 +00001482 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001483 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001484
Devang Patele3068402006-12-21 00:16:50 +00001485 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001486 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1487 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001488 return Changed;
1489}
Devang Patel376fefa2006-11-08 10:29:57 +00001490
Devang Patela1514cb2006-12-07 19:39:39 +00001491//===----------------------------------------------------------------------===//
1492// PassManager implementation
1493
Devang Patel376fefa2006-11-08 10:29:57 +00001494/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001495PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001496 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001497 // PM is the top level manager
1498 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001499}
1500
Devang Patelb67904d2006-12-13 02:36:01 +00001501PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001502 delete PM;
1503}
1504
Devang Patel376fefa2006-11-08 10:29:57 +00001505/// add - Add a pass to the queue of passes to run. This passes ownership of
1506/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1507/// will be destroyed as well, so there is no need to delete the pass. This
1508/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001509void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001510 PM->add(P);
1511}
1512
1513/// run - Execute all of the passes scheduled for execution. Keep track of
1514/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001515bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001516 return PM->run(M);
1517}
1518
Devang Patelb8817b92006-12-14 00:59:42 +00001519//===----------------------------------------------------------------------===//
1520// TimingInfo Class - This class is used to calculate information about the
1521// amount of time each pass takes to execute. This only happens with
1522// -time-passes is enabled on the command line.
1523//
1524bool llvm::TimePassesIsEnabled = false;
1525static cl::opt<bool,true>
1526EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1527 cl::desc("Time each pass, printing elapsed time for each on exit"));
1528
1529// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1530// a non null value (if the -time-passes option is enabled) or it leaves it
1531// null. It may be called multiple times.
1532void TimingInfo::createTheTimeInfo() {
1533 if (!TimePassesIsEnabled || TheTimeInfo) return;
1534
1535 // Constructed the first time this is called, iff -time-passes is enabled.
1536 // This guarantees that the object will be constructed before static globals,
1537 // thus it will be destroyed before them.
1538 static ManagedStatic<TimingInfo> TTI;
1539 TheTimeInfo = &*TTI;
1540}
1541
Devang Patel1c3633e2007-01-29 23:10:37 +00001542/// If TimingInfo is enabled then start pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001543Timer *llvm::StartPassTimer(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +00001544 if (TheTimeInfo)
Dan Gohman277e7672009-09-28 00:07:05 +00001545 return TheTimeInfo->passStarted(P);
1546 return 0;
Devang Patel1c3633e2007-01-29 23:10:37 +00001547}
1548
1549/// If TimingInfo is enabled then stop pass timer.
Dan Gohman277e7672009-09-28 00:07:05 +00001550void llvm::StopPassTimer(Pass *P, Timer *T) {
1551 if (T) T->stopTimer();
Devang Patel1c3633e2007-01-29 23:10:37 +00001552}
1553
Devang Patel1c56a632007-01-08 19:29:38 +00001554//===----------------------------------------------------------------------===//
1555// PMStack implementation
1556//
Devang Patelad98d232007-01-11 22:15:30 +00001557
Devang Patel1c56a632007-01-08 19:29:38 +00001558// Pop Pass Manager from the stack and clear its analysis info.
1559void PMStack::pop() {
1560
1561 PMDataManager *Top = this->top();
1562 Top->initializeAnalysisInfo();
1563
1564 S.pop_back();
1565}
1566
1567// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001568void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001569 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001570
Chris Lattner60987362009-03-06 05:53:14 +00001571 if (!this->empty()) {
1572 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001573
Chris Lattner60987362009-03-06 05:53:14 +00001574 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001575 TPM->addIndirectPassManager(PM);
1576 PM->setTopLevelManager(TPM);
1577 }
1578
Devang Patel15701b52007-01-11 00:19:00 +00001579 S.push_back(PM);
1580}
1581
1582// Dump content of the pass manager stack.
1583void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001584 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1585 E = S.end(); I != E; ++I)
1586 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1587
Devang Patel15701b52007-01-11 00:19:00 +00001588 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001589 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001590}
1591
Devang Patel1c56a632007-01-08 19:29:38 +00001592/// Find appropriate Module Pass Manager in the PM Stack and
1593/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001594void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001595 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001596 // Find Module Pass Manager
1597 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001598 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1599 if (TopPMType == PreferredType)
1600 break; // We found desired pass manager
1601 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001602 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001603 else
1604 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001605 }
Devang Patel18ff6362008-09-09 21:38:40 +00001606 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001607 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001608}
1609
Devang Patel3312f752007-01-16 21:43:18 +00001610/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1611/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001612void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001613 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001614
Devang Patela3286902008-09-09 17:56:50 +00001615 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001616 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001617 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1618 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001619 else
Devang Patel3312f752007-01-16 21:43:18 +00001620 break;
1621 }
1622 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1623
1624 // Create new Function Pass Manager
1625 if (!FPP) {
1626 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1627 PMDataManager *PMD = PMS.top();
1628
1629 // [1] Create new Function Pass Manager
1630 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001631 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001632
1633 // [2] Set up new manager's top level manager
1634 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1635 TPM->addIndirectPassManager(FPP);
1636
1637 // [3] Assign manager to manage this new manager. This may create
1638 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001639 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001640
1641 // [4] Push new manager into PMS
1642 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001643 }
1644
Devang Patel3312f752007-01-16 21:43:18 +00001645 // Assign FPP as the manager of this pass.
1646 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001647}
1648
Devang Patel3312f752007-01-16 21:43:18 +00001649/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001650/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001651void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001652 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001653 BBPassManager *BBP = NULL;
1654
Devang Patel15701b52007-01-11 00:19:00 +00001655 // Basic Pass Manager is a leaf pass manager. It does not handle
1656 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001657 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001658 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001659
Devang Patel3312f752007-01-16 21:43:18 +00001660 // If leaf manager is not Basic Block Pass manager then create new
1661 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001662
Devang Patel3312f752007-01-16 21:43:18 +00001663 if (!BBP) {
1664 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1665 PMDataManager *PMD = PMS.top();
1666
1667 // [1] Create new Basic Block Manager
1668 BBP = new BBPassManager(PMD->getDepth() + 1);
1669
1670 // [2] Set up new manager's top level manager
1671 // Basic Block Pass Manager does not live by itself
1672 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1673 TPM->addIndirectPassManager(BBP);
1674
Devang Patel15701b52007-01-11 00:19:00 +00001675 // [3] Assign manager to manage this new manager. This may create
1676 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001677 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001678
Devang Patel3312f752007-01-16 21:43:18 +00001679 // [4] Push new manager into PMS
1680 PMS.push(BBP);
1681 }
Devang Patel1c56a632007-01-08 19:29:38 +00001682
Devang Patel3312f752007-01-16 21:43:18 +00001683 // Assign BBP as the manager of this pass.
1684 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001685}
1686
Dan Gohmand3a20c92008-03-11 16:41:42 +00001687PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001688
1689/*===-- C Bindings --------------------------------------------------------===*/
1690
1691LLVMPassManagerRef LLVMCreatePassManager() {
1692 return wrap(new PassManager());
1693}
1694
1695LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1696 return wrap(new FunctionPassManager(unwrap(P)));
1697}
1698
Chris Lattner25963c62010-01-09 22:27:07 +00001699LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001700 return unwrap<PassManager>(PM)->run(*unwrap(M));
1701}
1702
Chris Lattner25963c62010-01-09 22:27:07 +00001703LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001704 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1705}
1706
Chris Lattner25963c62010-01-09 22:27:07 +00001707LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001708 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1709}
1710
Chris Lattner25963c62010-01-09 22:27:07 +00001711LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
Gordon Henriksen878114b2008-03-16 04:20:44 +00001712 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1713}
1714
1715void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1716 delete unwrap(PM);
1717}