blob: f98f2e16572000a324587c6dc4a6967cfd14f32a [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Devang Patelf1567a52006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Devang Patel9dbe4d12008-07-01 17:44:24 +000022#include "llvm/Analysis/Dominators.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000023#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000024#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000025#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000026#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000027using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000028
Devang Patele7599552007-01-12 18:52:44 +000029// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000030
Devang Patelf1567a52006-12-13 20:03:48 +000031namespace llvm {
32
33//===----------------------------------------------------------------------===//
34// Pass debugging information. Often it is useful to find out what pass is
35// running when a crash occurs in a utility. When this library is compiled with
36// debugging on, a command line option (--debug-pass) is enabled that causes the
37// pass name to be printed before it executes.
38//
39
Devang Patel03fb5872006-12-13 21:13:31 +000040// Different debug levels that can be enabled...
41enum PassDebugLevel {
42 None, Arguments, Structure, Executions, Details
43};
44
Devang Patel9dbe4d12008-07-01 17:44:24 +000045bool VerifyDomInfo = false;
46static cl::opt<bool,true>
47VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
48 cl::desc("Verify dominator info (time consuming)"));
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
60} // End of llvm namespace
61
Devang Patelffca9102006-12-15 19:39:30 +000062namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000063
Devang Patelf33f3eb2006-12-07 19:21:29 +000064//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000065// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000066//
Devang Patel67d6a5e2006-12-19 19:46:59 +000067/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000068/// pass together and sequence them to process one basic block before
69/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000070class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
71 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000072
73public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000074 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000075 explicit BBPassManager(int Depth)
Devang Patel09f162c2007-05-01 21:15:47 +000076 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000077
Devang Patelca58e352006-11-08 10:05:38 +000078 /// Execute all of the passes scheduled for execution. Keep track of
79 /// whether any of the passes modifies the function, and if so, return true.
80 bool runOnFunction(Function &F);
81
Devang Patelf9d96b92006-12-07 19:57:52 +000082 /// Pass Manager itself does not invalidate any analysis info.
83 void getAnalysisUsage(AnalysisUsage &Info) const {
84 Info.setPreservesAll();
85 }
86
Devang Patel475c4532006-12-08 00:59:05 +000087 bool doInitialization(Module &M);
88 bool doInitialization(Function &F);
89 bool doFinalization(Module &M);
90 bool doFinalization(Function &F);
91
Devang Patele3858e62007-02-01 22:08:25 +000092 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +000093 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000094 }
95
Devang Pateleda56172006-12-12 23:34:33 +000096 // Print passes managed by this manager
97 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000098 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000099 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
100 BasicBlockPass *BP = getContainedPass(Index);
101 BP->dumpPassStructure(Offset + 1);
102 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000103 }
104 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000105
106 BasicBlockPass *getContainedPass(unsigned N) {
107 assert ( N < PassVector.size() && "Pass number out of range!");
108 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
109 return BP;
110 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000111
Devang Patel28349ab2007-02-27 15:00:39 +0000112 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000113 return PMT_BasicBlockPassManager;
114 }
Devang Patelca58e352006-11-08 10:05:38 +0000115};
116
Devang Patel8c78a0b2007-05-03 01:11:54 +0000117char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000118}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000119
Devang Patele7599552007-01-12 18:52:44 +0000120namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000121
Devang Patel10c2ca62006-12-12 22:47:13 +0000122//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000123// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000124//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000125/// FunctionPassManagerImpl manages FPPassManagers
126class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000127 public PMDataManager,
128 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000129public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000130 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000131 explicit FunctionPassManagerImpl(int Depth) :
Devang Patel09f162c2007-05-01 21:15:47 +0000132 Pass((intptr_t)&ID), PMDataManager(Depth),
133 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000134
135 /// add - Add a pass to the queue of passes to run. This passes ownership of
136 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
137 /// will be destroyed as well, so there is no need to delete the pass. This
138 /// implies that all passes MUST be allocated with 'new'.
139 void add(Pass *P) {
140 schedulePass(P);
141 }
142
143 /// run - Execute all of the passes scheduled for execution. Keep track of
144 /// whether any of the passes modifies the module, and if so, return true.
145 bool run(Function &F);
146
147 /// doInitialization - Run all of the initializers for the function passes.
148 ///
149 bool doInitialization(Module &M);
150
Dan Gohmane6656eb2007-07-30 14:51:13 +0000151 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000152 ///
153 bool doFinalization(Module &M);
154
155 /// Pass Manager itself does not invalidate any analysis info.
156 void getAnalysisUsage(AnalysisUsage &Info) const {
157 Info.setPreservesAll();
158 }
159
160 inline void addTopLevelPass(Pass *P) {
161
162 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
163
164 // P is a immutable pass and it will be managed by this
165 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000166 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167 P->setResolver(AR);
168 initializeAnalysisImpl(P);
169 addImmutablePass(IP);
170 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000171 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000172 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173 }
Devang Patel0f080042007-01-12 17:23:48 +0000174
Devang Patel67d6a5e2006-12-19 19:46:59 +0000175 }
176
177 FPPassManager *getContainedManager(unsigned N) {
178 assert ( N < PassManagers.size() && "Pass number out of range!");
179 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
180 return FP;
181 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000182};
183
Devang Patel8c78a0b2007-05-03 01:11:54 +0000184char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000185//===----------------------------------------------------------------------===//
186// MPPassManager
187//
188/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000189/// It batches all Module passes and function pass managers together and
190/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000191class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000192
193public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000194 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000195 explicit MPPassManager(int Depth) :
196 Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000197
198 // Delete on the fly managers.
199 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000200 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000201 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
202 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000203 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000204 delete FPP;
205 }
206 }
207
Devang Patelca58e352006-11-08 10:05:38 +0000208 /// run - Execute all of the passes scheduled for execution. Keep track of
209 /// whether any of the passes modifies the module, and if so, return true.
210 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000211
Devang Patelf9d96b92006-12-07 19:57:52 +0000212 /// Pass Manager itself does not invalidate any analysis info.
213 void getAnalysisUsage(AnalysisUsage &Info) const {
214 Info.setPreservesAll();
215 }
216
Devang Patele64d3052007-04-16 20:12:57 +0000217 /// Add RequiredPass into list of lower level passes required by pass P.
218 /// RequiredPass is run on the fly by Pass Manager when P requests it
219 /// through getAnalysis interface.
220 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
221
Devang Patel69e9f6d2007-04-16 20:27:05 +0000222 /// Return function pass corresponding to PassInfo PI, that is
223 /// required by module pass MP. Instantiate analysis pass, by using
224 /// its runOnFunction() for function F.
225 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
226
Devang Patele3858e62007-02-01 22:08:25 +0000227 virtual const char *getPassName() const {
228 return "Module Pass Manager";
229 }
230
Devang Pateleda56172006-12-12 23:34:33 +0000231 // Print passes managed by this manager
232 void dumpPassStructure(unsigned Offset) {
233 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000234 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
235 ModulePass *MP = getContainedPass(Index);
236 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000237 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000238 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000239 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000240 }
241 }
242
Devang Patelabfbe3b2006-12-16 00:56:26 +0000243 ModulePass *getContainedPass(unsigned N) {
244 assert ( N < PassVector.size() && "Pass number out of range!");
245 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
246 return MP;
247 }
248
Devang Patel28349ab2007-02-27 15:00:39 +0000249 virtual PassManagerType getPassManagerType() const {
250 return PMT_ModulePassManager;
251 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000252
253 private:
254 /// Collection of on the fly FPPassManagers. These managers manage
255 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000256 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000257};
258
Devang Patel8c78a0b2007-05-03 01:11:54 +0000259char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000260//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000261// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000262//
Devang Patel09f162c2007-05-01 21:15:47 +0000263
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264/// PassManagerImpl manages MPPassManagers
265class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000266 public PMDataManager,
267 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000268
269public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000270 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000271 explicit PassManagerImpl(int Depth) :
272 Pass((intptr_t)&ID), PMDataManager(Depth),
273 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000274
Devang Patel376fefa2006-11-08 10:29:57 +0000275 /// add - Add a pass to the queue of passes to run. This passes ownership of
276 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
277 /// will be destroyed as well, so there is no need to delete the pass. This
278 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000279 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000280 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000281 }
Devang Patel376fefa2006-11-08 10:29:57 +0000282
283 /// run - Execute all of the passes scheduled for execution. Keep track of
284 /// whether any of the passes modifies the module, and if so, return true.
285 bool run(Module &M);
286
Devang Patelf9d96b92006-12-07 19:57:52 +0000287 /// Pass Manager itself does not invalidate any analysis info.
288 void getAnalysisUsage(AnalysisUsage &Info) const {
289 Info.setPreservesAll();
290 }
291
Devang Patelabcd1d32006-12-07 21:27:23 +0000292 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000293
Devang Patelfa971cd2006-12-08 23:57:43 +0000294 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000295
296 // P is a immutable pass and it will be managed by this
297 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000298 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000299 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000300 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000301 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000302 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000303 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000304 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000305 }
Devang Patel0f080042007-01-12 17:23:48 +0000306
Devang Patelabcd1d32006-12-07 21:27:23 +0000307 }
308
Devang Patel67d6a5e2006-12-19 19:46:59 +0000309 MPPassManager *getContainedManager(unsigned N) {
310 assert ( N < PassManagers.size() && "Pass number out of range!");
311 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
312 return MP;
313 }
314
Devang Patel376fefa2006-11-08 10:29:57 +0000315};
316
Devang Patel8c78a0b2007-05-03 01:11:54 +0000317char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000318} // End of llvm namespace
319
320namespace {
321
322//===----------------------------------------------------------------------===//
323// TimingInfo Class - This class is used to calculate information about the
324// amount of time each pass takes to execute. This only happens when
325// -time-passes is enabled on the command line.
326//
327
328class VISIBILITY_HIDDEN TimingInfo {
329 std::map<Pass*, Timer> TimingData;
330 TimerGroup TG;
331
332public:
333 // Use 'create' member to get this.
334 TimingInfo() : TG("... Pass execution timing report ...") {}
335
336 // TimingDtor - Print out information about timing information
337 ~TimingInfo() {
338 // Delete all of the timers...
339 TimingData.clear();
340 // TimerGroup is deleted next, printing the report.
341 }
342
343 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
344 // to a non null value (if the -time-passes option is enabled) or it leaves it
345 // null. It may be called multiple times.
346 static void createTheTimeInfo();
347
348 void passStarted(Pass *P) {
349
350 if (dynamic_cast<PMDataManager *>(P))
351 return;
352
353 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
354 if (I == TimingData.end())
355 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
356 I->second.startTimer();
357 }
358 void passEnded(Pass *P) {
359
360 if (dynamic_cast<PMDataManager *>(P))
361 return;
362
363 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
364 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
365 I->second.stopTimer();
366 }
367};
368
Devang Patel1c3633e2007-01-29 23:10:37 +0000369} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000370
Dan Gohmand78c4002008-05-13 00:00:25 +0000371static TimingInfo *TheTimeInfo;
372
Devang Patela1514cb2006-12-07 19:39:39 +0000373//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000374// PMTopLevelManager implementation
375
Devang Patel4268fc02007-01-16 02:00:38 +0000376/// Initialize top level manager. Create first pass manager.
377PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
378
379 if (t == TLM_Pass) {
380 MPPassManager *MPP = new MPPassManager(1);
381 MPP->setTopLevelManager(this);
382 addPassManager(MPP);
383 activeStack.push(MPP);
384 }
385 else if (t == TLM_Function) {
386 FPPassManager *FPP = new FPPassManager(1);
387 FPP->setTopLevelManager(this);
388 addPassManager(FPP);
389 activeStack.push(FPP);
390 }
391}
392
Devang Patelafb1f3622006-12-12 22:35:25 +0000393/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000394void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000395 Pass *P) {
396
Devang Patel8adae862007-07-20 18:04:54 +0000397 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000398 E = AnalysisPasses.end(); I != E; ++I) {
399 Pass *AP = *I;
400 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000401
402 if (P == AP)
403 continue;
404
Devang Patelafb1f3622006-12-12 22:35:25 +0000405 // If AP is the last user of other passes then make P last user of
406 // such passes.
407 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
408 LUE = LastUser.end(); LUI != LUE; ++LUI) {
409 if (LUI->second == AP)
410 LastUser[LUI->first] = P;
411 }
412 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000413}
414
415/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000416void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000417 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000418 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
419 LUE = LastUser.end(); LUI != LUE; ++LUI)
420 if (LUI->second == P)
421 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000422}
423
424/// Schedule pass P for execution. Make sure that passes required by
425/// P are run before P is run. Update analysis info maintained by
426/// the manager. Remove dead passes. This is a recursive function.
427void PMTopLevelManager::schedulePass(Pass *P) {
428
Devang Patel3312f752007-01-16 21:43:18 +0000429 // TODO : Allocate function manager for this pass, other wise required set
430 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000431
Devang Pateld74ede72007-03-06 01:06:16 +0000432 // Give pass a chance to prepare the stage.
433 P->preparePassManager(activeStack);
434
Devang Patel864970e2008-03-18 00:39:19 +0000435 // If P is an analysis pass and it is available then do not
436 // generate the analysis again. Stale analysis info should not be
437 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000438 if (P->getPassInfo() &&
439 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000440 return;
Devang Patel864970e2008-03-18 00:39:19 +0000441
Devang Patelafb1f3622006-12-12 22:35:25 +0000442 AnalysisUsage AnUsage;
443 P->getAnalysisUsage(AnUsage);
444 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
445 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
446 E = RequiredSet.end(); I != E; ++I) {
447
448 Pass *AnalysisPass = findAnalysisPass(*I);
449 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000450 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000451 // Schedule this analysis run first only if it is not a lower level
452 // analysis pass. Lower level analsyis passes are run on the fly.
453 if (P->getPotentialPassManagerType () >=
454 AnalysisPass->getPotentialPassManagerType())
455 schedulePass(AnalysisPass);
456 else
457 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000458 }
459 }
460
461 // Now all required passes are available.
462 addTopLevelPass(P);
463}
464
465/// Find the pass that implements Analysis AID. Search immutable
466/// passes and all pass managers. If desired pass is not found
467/// then return NULL.
468Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
469
470 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000471 // Check pass managers
Dan Gohman73caf5f2008-03-13 01:48:32 +0000472 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000473 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000474 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000475 P = PMD->findAnalysisPass(AID, false);
476 }
477
478 // Check other pass managers
479 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
480 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
481 P = (*I)->findAnalysisPass(AID, false);
482
Devang Patelafb1f3622006-12-12 22:35:25 +0000483 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
484 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
485 const PassInfo *PI = (*I)->getPassInfo();
486 if (PI == AID)
487 P = *I;
488
489 // If Pass not found then check the interfaces implemented by Immutable Pass
490 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000491 const std::vector<const PassInfo*> &ImmPI =
492 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000493 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
494 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000495 }
496 }
497
Devang Patelafb1f3622006-12-12 22:35:25 +0000498 return P;
499}
500
Devang Pateleda56172006-12-12 23:34:33 +0000501// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000502void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000503
Devang Patelfd4184322007-01-17 20:33:36 +0000504 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000505 return;
506
Devang Pateleda56172006-12-12 23:34:33 +0000507 // Print out the immutable passes
508 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
509 ImmutablePasses[i]->dumpPassStructure(0);
510 }
511
Dan Gohman73caf5f2008-03-13 01:48:32 +0000512 // Every class that derives from PMDataManager also derives from Pass
513 // (sometimes indirectly), but there's no inheritance relationship
514 // between PMDataManager and Pass, so we have to dynamic_cast to get
515 // from a PMDataManager* to a Pass*.
516 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000517 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000518 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000519}
520
Devang Patel991aeba2006-12-15 20:13:01 +0000521void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000522
Devang Patelfd4184322007-01-17 20:33:36 +0000523 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000524 return;
525
526 cerr << "Pass Arguments: ";
Dan Gohman73caf5f2008-03-13 01:48:32 +0000527 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000528 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000529 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000530 PMD->dumpPassArguments();
531 }
532 cerr << "\n";
533}
534
Devang Patele3068402006-12-21 00:16:50 +0000535void PMTopLevelManager::initializeAllAnalysisInfo() {
536
Dan Gohman73caf5f2008-03-13 01:48:32 +0000537 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000538 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000539 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000540 PMD->initializeAnalysisInfo();
541 }
542
543 // Initailize other pass managers
544 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
545 E = IndirectPassManagers.end(); I != E; ++I)
546 (*I)->initializeAnalysisInfo();
547}
548
Devang Patele7599552007-01-12 18:52:44 +0000549/// Destructor
550PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000551 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000552 E = PassManagers.end(); I != E; ++I)
553 delete *I;
554
555 for (std::vector<ImmutablePass *>::iterator
556 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
557 delete *I;
Devang Patele7599552007-01-12 18:52:44 +0000558}
559
Devang Patelafb1f3622006-12-12 22:35:25 +0000560//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000561// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000562
Devang Patel643676c2006-11-11 01:10:19 +0000563/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000564void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000565
Devang Patel643676c2006-11-11 01:10:19 +0000566 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000567 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000568
Devang Patele9976aa2006-12-07 19:33:53 +0000569 //This pass is the current implementation of all of the interfaces it
570 //implements as well.
571 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
572 for (unsigned i = 0, e = II.size(); i != e; ++i)
573 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000574 }
575}
576
Devang Patel9d9fc902007-03-06 17:52:53 +0000577// Return true if P preserves high level analysis used by other
578// passes managed by this manager
579bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
580
581 AnalysisUsage AnUsage;
582 P->getAnalysisUsage(AnUsage);
583
584 if (AnUsage.getPreservesAll())
585 return true;
586
587 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
588 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
589 E = HigherLevelAnalysis.end(); I != E; ++I) {
590 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000591 if (!dynamic_cast<ImmutablePass*>(P1) &&
592 std::find(PreservedSet.begin(), PreservedSet.end(),
593 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000594 PreservedSet.end())
595 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000596 }
597
598 return true;
599}
600
Devang Patela273d1c2007-07-19 18:02:32 +0000601/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
602void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000603 AnalysisUsage AnUsage;
604 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000605 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000606
Devang Patelef432532007-07-19 05:36:09 +0000607 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000608 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
609 E = PreservedSet.end(); I != E; ++I) {
610 AnalysisID AID = *I;
611 Pass *AP = findAnalysisPass(AID, true);
612 if (AP)
613 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000614 }
Devang Patela273d1c2007-07-19 18:02:32 +0000615}
616
Devang Patel9dbe4d12008-07-01 17:44:24 +0000617/// verifyDomInfo - Verify dominator information if it is available.
618void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
619
620 if (!VerifyDomInfo || !P.getResolver())
621 return;
622
623 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
624 if (!DT)
625 return;
626
627 DominatorTree OtherDT;
628 OtherDT.getBase().recalculate(F);
629 if (DT->compare(OtherDT)) {
630 cerr << "Dominator Information for " << F.getNameStart() << "\n";
631 cerr << "Pass " << P.getPassName() << "\n";
632 cerr << "----- Valid -----\n";
633 OtherDT.dump();
634 cerr << "----- InValid -----\n";
635 DT->dump();
636 assert (0 && "Invalid dominator info");
637 }
638
639 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
640 if (!DF)
641 return;
642
643 DominanceFrontier OtherDF;
644 std::vector<BasicBlock*> DTRoots = DT->getRoots();
645 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
646 if (DF->compare(OtherDF)) {
647 cerr << "Dominator Information for " << F.getNameStart() << "\n";
648 cerr << "Pass " << P.getPassName() << "\n";
649 cerr << "----- Valid -----\n";
650 OtherDF.dump();
651 cerr << "----- InValid -----\n";
652 DF->dump();
653 assert (0 && "Invalid dominator info");
654 }
655}
656
Devang Patela273d1c2007-07-19 18:02:32 +0000657/// Remove Analyss not preserved by Pass P
658void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
659 AnalysisUsage AnUsage;
660 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000661 if (AnUsage.getPreservesAll())
662 return;
663
Devang Patela273d1c2007-07-19 18:02:32 +0000664 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000665 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000666 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000667 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000668 if (!dynamic_cast<ImmutablePass*>(Info->second)
669 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000670 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000671 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000672 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000673 if (PassDebugging >= Details) {
674 Pass *S = Info->second;
675 cerr << " -- " << P->getPassName() << " is not preserving ";
676 cerr << S->getPassName() << "\n";
677 }
678 }
Devang Patel349170f2006-11-11 01:24:55 +0000679 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000680
681 // Check inherited analysis also. If P is not preserving analysis
682 // provided by parent manager then remove it here.
683 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
684
685 if (!InheritedAnalysis[Index])
686 continue;
687
688 for (std::map<AnalysisID, Pass*>::iterator
689 I = InheritedAnalysis[Index]->begin(),
690 E = InheritedAnalysis[Index]->end(); I != E; ) {
691 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000692 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
693 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000694 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000695 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000696 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000697 }
698 }
Devang Patelf68a3492006-11-07 22:35:17 +0000699}
700
Devang Patelca189262006-11-14 03:05:08 +0000701/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000702void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000703 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000704
Devang Patel8adae862007-07-20 18:04:54 +0000705 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000706
Devang Patel2ff44922007-04-16 20:39:59 +0000707 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000708 if (!TPM)
709 return;
710
Devang Patel17ad0962006-12-08 00:37:52 +0000711 TPM->collectLastUses(DeadPasses, P);
712
Devang Patel656a9172008-06-06 17:50:36 +0000713 if (PassDebugging >= Details && !DeadPasses.empty()) {
Evan Cheng93af6ce2008-06-04 09:13:31 +0000714 cerr << " -*- " << P->getPassName();
Devang Patel656a9172008-06-06 17:50:36 +0000715 cerr << " is the last user of following pass instances.";
716 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000717 }
718
Devang Patel8adae862007-07-20 18:04:54 +0000719 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000720 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000721
Devang Patel003a5592007-03-05 20:01:30 +0000722 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000723
Devang Patel7ebf09d2007-03-05 18:20:51 +0000724 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000725 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000726 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000727
Devang Patel17ad0962006-12-08 00:37:52 +0000728 std::map<AnalysisID, Pass*>::iterator Pos =
729 AvailableAnalysis.find((*I)->getPassInfo());
730
Devang Patel475c4532006-12-08 00:59:05 +0000731 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000732 if (Pos != AvailableAnalysis.end())
733 AvailableAnalysis.erase(Pos);
734 }
Devang Patelca189262006-11-14 03:05:08 +0000735}
736
Devang Patel8f677ce2006-12-07 18:47:25 +0000737/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000738/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000739void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000740 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000741
Devang Pateld440cd92006-12-08 23:53:00 +0000742 // This manager is going to manage pass P. Set up analysis resolver
743 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000744 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000745 P->setResolver(AR);
746
Devang Patelec2b9a72007-03-05 22:57:49 +0000747 // If a FunctionPass F is the last user of ModulePass info M
748 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000749 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000750
Devang Patel90b05e02006-11-11 02:04:19 +0000751 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000752
753 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000754 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000755 SmallVector<Pass *, 8> RequiredPasses;
756 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
757
Devang Patelbc03f132006-12-07 23:55:10 +0000758 unsigned PDepth = this->getDepth();
759
Devang Patele64d3052007-04-16 20:12:57 +0000760 collectRequiredAnalysis(RequiredPasses,
761 ReqAnalysisNotAvailable, P);
762 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000763 E = RequiredPasses.end(); I != E; ++I) {
764 Pass *PRequired = *I;
765 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000766
Devang Patel9dbe4d12008-07-01 17:44:24 +0000767 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000768 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
769 RDepth = DM.getDepth();
770
Devang Patelbc03f132006-12-07 23:55:10 +0000771 if (PDepth == RDepth)
772 LastUses.push_back(PRequired);
773 else if (PDepth > RDepth) {
774 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000775 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000776 // Keep track of higher level analysis used by this manager.
777 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000778 } else
779 assert (0 && "Unable to accomodate Required Pass");
780 }
781
Devang Patel39786a92007-01-15 20:31:54 +0000782 // Set P as P's last user until someone starts using P.
783 // However, if P is a Pass Manager then it does not need
784 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000785 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000786 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000787 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000788
Devang Patelec2b9a72007-03-05 22:57:49 +0000789 if (!TransferLastUses.empty()) {
790 Pass *My_PM = dynamic_cast<Pass *>(this);
791 TPM->setLastUser(TransferLastUses, My_PM);
792 TransferLastUses.clear();
793 }
794
Devang Patel69e9f6d2007-04-16 20:27:05 +0000795 // Now, take care of required analysises that are not available.
796 for (SmallVector<AnalysisID, 8>::iterator
797 I = ReqAnalysisNotAvailable.begin(),
798 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
799 Pass *AnalysisPass = (*I)->createPass();
800 this->addLowerLevelRequiredPass(P, AnalysisPass);
801 }
802
Devang Patel17bff0d2006-12-07 22:09:36 +0000803 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000804 // Remove the analysis not preserved by this pass
805 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000806 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000807 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000808
809 // Add pass
810 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000811}
812
Devang Patele64d3052007-04-16 20:12:57 +0000813
814/// Populate RP with analysis pass that are required by
815/// pass P and are available. Populate RP_NotAvail with analysis
816/// pass that are required by pass P but are not available.
817void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
818 SmallVector<AnalysisID, 8> &RP_NotAvail,
819 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000820 AnalysisUsage AnUsage;
821 P->getAnalysisUsage(AnUsage);
822 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
823 for (std::vector<AnalysisID>::const_iterator
824 I = RequiredSet.begin(), E = RequiredSet.end();
825 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000826 AnalysisID AID = *I;
827 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
828 RP.push_back(AnalysisPass);
829 else
830 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000831 }
Devang Patelf58183d2006-12-12 23:09:32 +0000832
833 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
834 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
835 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000836 AnalysisID AID = *I;
837 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
838 RP.push_back(AnalysisPass);
839 else
840 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000841 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000842}
843
Devang Patel07f4f582006-11-14 21:49:36 +0000844// All Required analyses should be available to the pass as it runs! Here
845// we fill in the AnalysisImpls member of the pass so that it can
846// successfully use the getAnalysis() method to retrieve the
847// implementations it needs.
848//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000849void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000850 AnalysisUsage AnUsage;
851 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000852
853 for (std::vector<const PassInfo *>::const_iterator
854 I = AnUsage.getRequiredSet().begin(),
855 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000856 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000857 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000858 // This may be analysis pass that is initialized on the fly.
859 // If that is not the case then it will raise an assert when it is used.
860 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000861 AnalysisResolver *AR = P->getResolver();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000862 assert (AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000863 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000864 }
865}
866
Devang Patel640c5bb2006-12-08 22:30:11 +0000867/// Find the pass that implements Analysis AID. If desired pass is not found
868/// then return NULL.
869Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
870
871 // Check if AvailableAnalysis map has one entry.
872 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
873
874 if (I != AvailableAnalysis.end())
875 return I->second;
876
877 // Search Parents through TopLevelManager
878 if (SearchParent)
879 return TPM->findAnalysisPass(AID);
880
Devang Patel9d759b82006-12-09 00:09:12 +0000881 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000882}
883
Devang Patel991aeba2006-12-15 20:13:01 +0000884// Print list of passes that are last used by P.
885void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
886
Devang Patel8adae862007-07-20 18:04:54 +0000887 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000888
889 // If this is a on the fly manager then it does not have TPM.
890 if (!TPM)
891 return;
892
Devang Patel991aeba2006-12-15 20:13:01 +0000893 TPM->collectLastUses(LUses, P);
894
Devang Patel8adae862007-07-20 18:04:54 +0000895 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000896 E = LUses.end(); I != E; ++I) {
897 llvm::cerr << "--" << std::string(Offset*2, ' ');
898 (*I)->dumpPassStructure(0);
899 }
900}
901
902void PMDataManager::dumpPassArguments() const {
903 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
904 E = PassVector.end(); I != E; ++I) {
905 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
906 PMD->dumpPassArguments();
907 else
908 if (const PassInfo *PI = (*I)->getPassInfo())
909 if (!PI->isAnalysisGroup())
910 cerr << " -" << PI->getPassArgument();
911 }
912}
913
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000914void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
915 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000916 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000917 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000918 return;
919 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000920 switch (S1) {
921 case EXECUTION_MSG:
922 cerr << "Executing Pass '" << P->getPassName();
923 break;
924 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000925 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000926 break;
927 case FREEING_MSG:
928 cerr << " Freeing Pass '" << P->getPassName();
929 break;
930 default:
931 break;
932 }
933 switch (S2) {
934 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000935 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000936 break;
937 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000938 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000939 break;
940 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000941 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000942 break;
943 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000944 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000945 break;
946 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000947 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000948 break;
949 default:
950 break;
951 }
Devang Patel991aeba2006-12-15 20:13:01 +0000952}
953
954void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
955 const std::vector<AnalysisID> &Set)
956 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000957 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000958 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
959 for (unsigned i = 0; i != Set.size(); ++i) {
960 if (i) cerr << ",";
961 cerr << " " << Set[i]->getPassName();
962 }
963 cerr << "\n";
964 }
965}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000966
Devang Patel004937b2007-07-27 20:06:09 +0000967/// Add RequiredPass into list of lower level passes required by pass P.
968/// RequiredPass is run on the fly by Pass Manager when P requests it
969/// through getAnalysis interface.
970/// This should be handled by specific pass manager.
971void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
972 if (TPM) {
973 TPM->dumpArguments();
974 TPM->dumpPasses();
975 }
Devang Patel8df7cc12008-02-02 01:43:30 +0000976
977 // Module Level pass may required Function Level analysis info
978 // (e.g. dominator info). Pass manager uses on the fly function pass manager
979 // to provide this on demand. In that case, in Pass manager terminology,
980 // module level pass is requiring lower level analysis info managed by
981 // lower level pass manager.
982
983 // When Pass manager is not able to order required analysis info, Pass manager
984 // checks whether any lower level manager will be able to provide this
985 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +0000986#ifndef NDEBUG
987 cerr << "Unable to schedule " << RequiredPass->getPassName();
988 cerr << " required by " << P->getPassName() << "\n";
989#endif
990 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +0000991}
992
Devang Patele7599552007-01-12 18:52:44 +0000993// Destructor
994PMDataManager::~PMDataManager() {
995
996 for (std::vector<Pass *>::iterator I = PassVector.begin(),
997 E = PassVector.end(); I != E; ++I)
998 delete *I;
999
Devang Patele7599552007-01-12 18:52:44 +00001000}
1001
Devang Patel9bdf7d42006-12-08 23:28:54 +00001002//===----------------------------------------------------------------------===//
1003// NOTE: Is this the right place to define this method ?
1004// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001005Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001006 return PM.findAnalysisPass(ID, dir);
1007}
1008
Devang Patel92942812007-04-16 20:56:24 +00001009Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1010 Function &F) {
1011 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1012}
1013
Devang Patela1514cb2006-12-07 19:39:39 +00001014//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001015// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001016
Devang Patel6e5a1132006-11-07 21:31:57 +00001017/// Execute all of the passes scheduled for execution by invoking
1018/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1019/// the function, and if so, return true.
1020bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001021BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001022
Reid Spencer5301e7c2007-01-30 20:08:39 +00001023 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001024 return false;
1025
Devang Patele9585592006-12-08 01:38:28 +00001026 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001027
Devang Patel6e5a1132006-11-07 21:31:57 +00001028 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001029 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1030 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001031 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001032 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +00001033
Devang Pateld305c402007-08-10 18:29:32 +00001034 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001035 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001036
Devang Patelabfbe3b2006-12-16 00:56:26 +00001037 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001038
Devang Patelabfbe3b2006-12-16 00:56:26 +00001039 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001040 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001041 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001042
Devang Patel003a5592007-03-05 20:01:30 +00001043 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001044 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1045 I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001046 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001047
Devang Patela273d1c2007-07-19 18:02:32 +00001048 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001049 removeNotPreservedAnalysis(BP);
1050 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001051 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001052 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001053
Devang Patel56d48ec2006-12-15 22:57:49 +00001054 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001055}
1056
Devang Patel475c4532006-12-08 00:59:05 +00001057// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001058inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001059 bool Changed = false;
1060
Devang Patelabfbe3b2006-12-16 00:56:26 +00001061 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1062 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001063 Changed |= BP->doInitialization(M);
1064 }
1065
1066 return Changed;
1067}
1068
Devang Patel67d6a5e2006-12-19 19:46:59 +00001069inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001070 bool Changed = false;
1071
Devang Patelabfbe3b2006-12-16 00:56:26 +00001072 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1073 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001074 Changed |= BP->doFinalization(M);
1075 }
1076
1077 return Changed;
1078}
1079
Devang Patel67d6a5e2006-12-19 19:46:59 +00001080inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001081 bool Changed = false;
1082
Devang Patelabfbe3b2006-12-16 00:56:26 +00001083 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1084 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001085 Changed |= BP->doInitialization(F);
1086 }
1087
1088 return Changed;
1089}
1090
Devang Patel67d6a5e2006-12-19 19:46:59 +00001091inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001092 bool Changed = false;
1093
Devang Patelabfbe3b2006-12-16 00:56:26 +00001094 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1095 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001096 Changed |= BP->doFinalization(F);
1097 }
1098
1099 return Changed;
1100}
1101
1102
Devang Patela1514cb2006-12-07 19:39:39 +00001103//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001104// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001105
Devang Patel4e12f862006-11-08 10:44:40 +00001106/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001107FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001108 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001109 // FPM is the top level manager.
1110 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001111
Dan Gohman565df952008-03-13 02:08:36 +00001112 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001113 FPM->setResolver(AR);
1114
Devang Patel1f653682006-12-08 18:57:16 +00001115 MP = P;
1116}
1117
Devang Patelb67904d2006-12-13 02:36:01 +00001118FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001119 delete FPM;
1120}
1121
Devang Patel4e12f862006-11-08 10:44:40 +00001122/// add - Add a pass to the queue of passes to run. This passes
1123/// ownership of the Pass to the PassManager. When the
1124/// PassManager_X is destroyed, the pass will be destroyed as well, so
1125/// there is no need to delete the pass. (TODO delete passes.)
1126/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001127void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001128 FPM->add(P);
1129}
1130
Devang Patel9f3083e2006-11-15 19:39:54 +00001131/// run - Execute all of the passes scheduled for execution. Keep
1132/// track of whether any of the passes modifies the function, and if
1133/// so, return true.
1134///
Devang Patelb67904d2006-12-13 02:36:01 +00001135bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001136 std::string errstr;
1137 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001138 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001139 abort();
1140 }
Devang Patel272908d2006-12-08 22:57:48 +00001141 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001142}
1143
1144
Devang Patelff631ae2006-11-15 01:27:05 +00001145/// doInitialization - Run all of the initializers for the function passes.
1146///
Devang Patelb67904d2006-12-13 02:36:01 +00001147bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001148 return FPM->doInitialization(*MP->getModule());
1149}
1150
Dan Gohmane6656eb2007-07-30 14:51:13 +00001151/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001152///
Devang Patelb67904d2006-12-13 02:36:01 +00001153bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001154 return FPM->doFinalization(*MP->getModule());
1155}
1156
Devang Patela1514cb2006-12-07 19:39:39 +00001157//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001158// FunctionPassManagerImpl implementation
1159//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001160inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1161 bool Changed = false;
1162
1163 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1164 FPPassManager *FP = getContainedManager(Index);
1165 Changed |= FP->doInitialization(M);
1166 }
1167
1168 return Changed;
1169}
1170
1171inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1172 bool Changed = false;
1173
1174 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1175 FPPassManager *FP = getContainedManager(Index);
1176 Changed |= FP->doFinalization(M);
1177 }
1178
1179 return Changed;
1180}
1181
1182// Execute all the passes managed by this top level manager.
1183// Return true if any function is modified by a pass.
1184bool FunctionPassManagerImpl::run(Function &F) {
1185
1186 bool Changed = false;
1187
1188 TimingInfo::createTheTimeInfo();
1189
1190 dumpArguments();
1191 dumpPasses();
1192
Devang Patele3068402006-12-21 00:16:50 +00001193 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001194 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1195 FPPassManager *FP = getContainedManager(Index);
1196 Changed |= FP->runOnFunction(F);
1197 }
1198 return Changed;
1199}
1200
1201//===----------------------------------------------------------------------===//
1202// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001203
Devang Patel8c78a0b2007-05-03 01:11:54 +00001204char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001205/// Print passes managed by this manager
1206void FPPassManager::dumpPassStructure(unsigned Offset) {
1207 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1208 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1209 FunctionPass *FP = getContainedPass(Index);
1210 FP->dumpPassStructure(Offset + 1);
1211 dumpLastUses(FP, Offset+1);
1212 }
1213}
1214
1215
Devang Patel0c2012f2006-11-07 21:49:50 +00001216/// Execute all of the passes scheduled for execution by invoking
1217/// runOnFunction method. Keep track of whether any of the passes modifies
1218/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001219bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001220
1221 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001222
Reid Spencer5301e7c2007-01-30 20:08:39 +00001223 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001224 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001225
1226 // Collect inherited analysis from Module level pass manager.
1227 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001228
Devang Patelabfbe3b2006-12-16 00:56:26 +00001229 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1230 FunctionPass *FP = getContainedPass(Index);
1231
Devang Patelf6d1d212006-12-14 00:25:06 +00001232 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001233 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001234
Devang Pateld305c402007-08-10 18:29:32 +00001235 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001236 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001237
Devang Patelabfbe3b2006-12-16 00:56:26 +00001238 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001239
Devang Patelabfbe3b2006-12-16 00:56:26 +00001240 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001241 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001242 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001243
Devang Patel003a5592007-03-05 20:01:30 +00001244 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001245 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001246 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001247
Devang Patela273d1c2007-07-19 18:02:32 +00001248 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001249 removeNotPreservedAnalysis(FP);
1250 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001251 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001252
1253 // Verify dominator information if it is available and preserved.
1254 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001255 }
1256 return Changed;
1257}
1258
Devang Patel67d6a5e2006-12-19 19:46:59 +00001259bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001260
Devang Patel67d6a5e2006-12-19 19:46:59 +00001261 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001262
1263 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1264 this->runOnFunction(*I);
1265
1266 return Changed |= doFinalization(M);
1267}
1268
1269inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001270 bool Changed = false;
1271
Devang Patelabfbe3b2006-12-16 00:56:26 +00001272 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1273 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001274 Changed |= FP->doInitialization(M);
1275 }
1276
1277 return Changed;
1278}
1279
Devang Patel67d6a5e2006-12-19 19:46:59 +00001280inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001281 bool Changed = false;
1282
Devang Patelabfbe3b2006-12-16 00:56:26 +00001283 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1284 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001285 Changed |= FP->doFinalization(M);
1286 }
1287
Devang Patelff631ae2006-11-15 01:27:05 +00001288 return Changed;
1289}
1290
Devang Patela1514cb2006-12-07 19:39:39 +00001291//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001292// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001293
Devang Patel05e1a972006-11-07 22:03:15 +00001294/// Execute all of the passes scheduled for execution by invoking
1295/// runOnModule method. Keep track of whether any of the passes modifies
1296/// the module, and if so, return true.
1297bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001298MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001299 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001300
Devang Patelabfbe3b2006-12-16 00:56:26 +00001301 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1302 ModulePass *MP = getContainedPass(Index);
1303
Devang Patelf6d1d212006-12-14 00:25:06 +00001304 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001305 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001306
Dan Gohman929391a2008-01-29 12:09:55 +00001307 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1308 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001309 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001310
Devang Patelabfbe3b2006-12-16 00:56:26 +00001311 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001312
Devang Patelabfbe3b2006-12-16 00:56:26 +00001313 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001314 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001315 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001316
Devang Patel003a5592007-03-05 20:01:30 +00001317 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001318 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1319 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001320 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001321
Devang Patela273d1c2007-07-19 18:02:32 +00001322 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001323 removeNotPreservedAnalysis(MP);
1324 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001325 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001326 }
1327 return Changed;
1328}
1329
Devang Patele64d3052007-04-16 20:12:57 +00001330/// Add RequiredPass into list of lower level passes required by pass P.
1331/// RequiredPass is run on the fly by Pass Manager when P requests it
1332/// through getAnalysis interface.
1333void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1334
1335 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1336 && "Unable to handle Pass that requires lower level Analysis pass");
1337 assert ((P->getPotentialPassManagerType() <
1338 RequiredPass->getPotentialPassManagerType())
1339 && "Unable to handle Pass that requires lower level Analysis pass");
1340
Devang Patel68f72b12007-04-26 17:50:19 +00001341 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001342 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001343 FPP = new FunctionPassManagerImpl(0);
1344 // FPP is the top level manager.
1345 FPP->setTopLevelManager(FPP);
1346
Devang Patel69e9f6d2007-04-16 20:27:05 +00001347 OnTheFlyManagers[P] = FPP;
1348 }
Devang Patel68f72b12007-04-26 17:50:19 +00001349 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001350
Devang Patel68f72b12007-04-26 17:50:19 +00001351 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001352 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001353 LU.push_back(RequiredPass);
1354 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001355}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001356
1357/// Return function pass corresponding to PassInfo PI, that is
1358/// required by module pass MP. Instantiate analysis pass, by using
1359/// its runOnFunction() for function F.
1360Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1361 Function &F) {
1362 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001363 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001364 assert (FPP && "Unable to find on the fly pass");
1365
Devang Patel68f72b12007-04-26 17:50:19 +00001366 FPP->run(F);
1367 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001368}
1369
1370
Devang Patela1514cb2006-12-07 19:39:39 +00001371//===----------------------------------------------------------------------===//
1372// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001373//
Devang Patelc290c8a2006-11-07 22:23:34 +00001374/// run - Execute all of the passes scheduled for execution. Keep track of
1375/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001376bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001377
Devang Patelc290c8a2006-11-07 22:23:34 +00001378 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001379
Devang Patelb8817b92006-12-14 00:59:42 +00001380 TimingInfo::createTheTimeInfo();
1381
Devang Patelcfd70c42006-12-13 22:10:00 +00001382 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001383 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001384
Devang Patele3068402006-12-21 00:16:50 +00001385 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001386 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1387 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001388 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001389 }
1390 return Changed;
1391}
Devang Patel376fefa2006-11-08 10:29:57 +00001392
Devang Patela1514cb2006-12-07 19:39:39 +00001393//===----------------------------------------------------------------------===//
1394// PassManager implementation
1395
Devang Patel376fefa2006-11-08 10:29:57 +00001396/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001397PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001398 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001399 // PM is the top level manager
1400 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001401}
1402
Devang Patelb67904d2006-12-13 02:36:01 +00001403PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001404 delete PM;
1405}
1406
Devang Patel376fefa2006-11-08 10:29:57 +00001407/// add - Add a pass to the queue of passes to run. This passes ownership of
1408/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1409/// will be destroyed as well, so there is no need to delete the pass. This
1410/// implies that all passes MUST be allocated with 'new'.
1411void
Devang Patelb67904d2006-12-13 02:36:01 +00001412PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001413 PM->add(P);
1414}
1415
1416/// run - Execute all of the passes scheduled for execution. Keep track of
1417/// whether any of the passes modifies the module, and if so, return true.
1418bool
Devang Patelb67904d2006-12-13 02:36:01 +00001419PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001420 return PM->run(M);
1421}
1422
Devang Patelb8817b92006-12-14 00:59:42 +00001423//===----------------------------------------------------------------------===//
1424// TimingInfo Class - This class is used to calculate information about the
1425// amount of time each pass takes to execute. This only happens with
1426// -time-passes is enabled on the command line.
1427//
1428bool llvm::TimePassesIsEnabled = false;
1429static cl::opt<bool,true>
1430EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1431 cl::desc("Time each pass, printing elapsed time for each on exit"));
1432
1433// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1434// a non null value (if the -time-passes option is enabled) or it leaves it
1435// null. It may be called multiple times.
1436void TimingInfo::createTheTimeInfo() {
1437 if (!TimePassesIsEnabled || TheTimeInfo) return;
1438
1439 // Constructed the first time this is called, iff -time-passes is enabled.
1440 // This guarantees that the object will be constructed before static globals,
1441 // thus it will be destroyed before them.
1442 static ManagedStatic<TimingInfo> TTI;
1443 TheTimeInfo = &*TTI;
1444}
1445
Devang Patel1c3633e2007-01-29 23:10:37 +00001446/// If TimingInfo is enabled then start pass timer.
1447void StartPassTimer(Pass *P) {
1448 if (TheTimeInfo)
1449 TheTimeInfo->passStarted(P);
1450}
1451
1452/// If TimingInfo is enabled then stop pass timer.
1453void StopPassTimer(Pass *P) {
1454 if (TheTimeInfo)
1455 TheTimeInfo->passEnded(P);
1456}
1457
Devang Patel1c56a632007-01-08 19:29:38 +00001458//===----------------------------------------------------------------------===//
1459// PMStack implementation
1460//
Devang Patelad98d232007-01-11 22:15:30 +00001461
Devang Patel1c56a632007-01-08 19:29:38 +00001462// Pop Pass Manager from the stack and clear its analysis info.
1463void PMStack::pop() {
1464
1465 PMDataManager *Top = this->top();
1466 Top->initializeAnalysisInfo();
1467
1468 S.pop_back();
1469}
1470
1471// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001472void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001473
Devang Patel15701b52007-01-11 00:19:00 +00001474 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001475 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001476
Devang Patel15701b52007-01-11 00:19:00 +00001477 if (this->empty()) {
1478 Top = PM;
1479 }
1480 else {
1481 Top = this->top();
1482 PMTopLevelManager *TPM = Top->getTopLevelManager();
1483
1484 assert (TPM && "Unable to find top level manager");
1485 TPM->addIndirectPassManager(PM);
1486 PM->setTopLevelManager(TPM);
1487 }
1488
Devang Patel15701b52007-01-11 00:19:00 +00001489 S.push_back(PM);
1490}
1491
1492// Dump content of the pass manager stack.
1493void PMStack::dump() {
1494 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1495 E = S.end(); I != E; ++I) {
1496 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001497 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001498 }
1499 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001500 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001501}
1502
Devang Patel1c56a632007-01-08 19:29:38 +00001503/// Find appropriate Module Pass Manager in the PM Stack and
1504/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001505void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001506 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001507
Devang Patel1c56a632007-01-08 19:29:38 +00001508 // Find Module Pass Manager
1509 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001510 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1511 if (TopPMType == PreferredType)
1512 break; // We found desired pass manager
1513 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001514 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001515 else
1516 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001517 }
1518
Devang Patel23f8aa92007-01-17 21:19:23 +00001519 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001520}
1521
Devang Patel3312f752007-01-16 21:43:18 +00001522/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1523/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001524void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001525 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001526
Devang Patel15701b52007-01-11 00:19:00 +00001527 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001528 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001529 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1530 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001531 else
Devang Patel3312f752007-01-16 21:43:18 +00001532 break;
1533 }
1534 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1535
1536 // Create new Function Pass Manager
1537 if (!FPP) {
1538 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1539 PMDataManager *PMD = PMS.top();
1540
1541 // [1] Create new Function Pass Manager
1542 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001543 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001544
1545 // [2] Set up new manager's top level manager
1546 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1547 TPM->addIndirectPassManager(FPP);
1548
1549 // [3] Assign manager to manage this new manager. This may create
1550 // and push new managers into PMS
Devang Pateldffca632007-01-17 20:30:17 +00001551
1552 // If Call Graph Pass Manager is active then use it to manage
1553 // this new Function Pass manager.
1554 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
Dan Gohman565df952008-03-13 02:08:36 +00001555 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
Devang Pateldffca632007-01-17 20:30:17 +00001556 else
Dan Gohman565df952008-03-13 02:08:36 +00001557 FPP->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001558
1559 // [4] Push new manager into PMS
1560 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001561 }
1562
Devang Patel3312f752007-01-16 21:43:18 +00001563 // Assign FPP as the manager of this pass.
1564 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001565}
1566
Devang Patel3312f752007-01-16 21:43:18 +00001567/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001568/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001569void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001570 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001571
1572 BBPassManager *BBP = NULL;
1573
Devang Patel15701b52007-01-11 00:19:00 +00001574 // Basic Pass Manager is a leaf pass manager. It does not handle
1575 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001576 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001577 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001578
Devang Patel3312f752007-01-16 21:43:18 +00001579 // If leaf manager is not Basic Block Pass manager then create new
1580 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001581
Devang Patel3312f752007-01-16 21:43:18 +00001582 if (!BBP) {
1583 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1584 PMDataManager *PMD = PMS.top();
1585
1586 // [1] Create new Basic Block Manager
1587 BBP = new BBPassManager(PMD->getDepth() + 1);
1588
1589 // [2] Set up new manager's top level manager
1590 // Basic Block Pass Manager does not live by itself
1591 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1592 TPM->addIndirectPassManager(BBP);
1593
Devang Patel15701b52007-01-11 00:19:00 +00001594 // [3] Assign manager to manage this new manager. This may create
1595 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001596 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001597
Devang Patel3312f752007-01-16 21:43:18 +00001598 // [4] Push new manager into PMS
1599 PMS.push(BBP);
1600 }
Devang Patel1c56a632007-01-08 19:29:38 +00001601
Devang Patel3312f752007-01-16 21:43:18 +00001602 // Assign BBP as the manager of this pass.
1603 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001604}
1605
Dan Gohmand3a20c92008-03-11 16:41:42 +00001606PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001607
1608/*===-- C Bindings --------------------------------------------------------===*/
1609
1610LLVMPassManagerRef LLVMCreatePassManager() {
1611 return wrap(new PassManager());
1612}
1613
1614LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1615 return wrap(new FunctionPassManager(unwrap(P)));
1616}
1617
1618int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1619 return unwrap<PassManager>(PM)->run(*unwrap(M));
1620}
1621
1622int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1623 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1624}
1625
1626int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1627 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1628}
1629
1630int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1631 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1632}
1633
1634void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1635 delete unwrap(PM);
1636}