blob: e3d1c5364e95f5e8869e952a845a7c7528a8e7f0 [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 Patel99ad4ba2008-07-01 21:36:11 +000045bool VerifyDomInfo = false;
Devang Patel9dbe4d12008-07-01 17:44:24 +000046static 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)
Dan Gohmana79db302008-09-04 17:05:41 +000076 : PMDataManager(Depth), FunctionPass(&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) :
Dan Gohmana79db302008-09-04 17:05:41 +0000132 Pass(&ID), PMDataManager(Depth),
Devang Patel09f162c2007-05-01 21:15:47 +0000133 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) :
Dan Gohmana79db302008-09-04 17:05:41 +0000196 Pass(&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) :
Dan Gohmana79db302008-09-04 17:05:41 +0000272 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000273
Devang Patel376fefa2006-11-08 10:29:57 +0000274 /// add - Add a pass to the queue of passes to run. This passes ownership of
275 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
276 /// will be destroyed as well, so there is no need to delete the pass. This
277 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000278 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000279 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000280 }
Devang Patel376fefa2006-11-08 10:29:57 +0000281
282 /// run - Execute all of the passes scheduled for execution. Keep track of
283 /// whether any of the passes modifies the module, and if so, return true.
284 bool run(Module &M);
285
Devang Patelf9d96b92006-12-07 19:57:52 +0000286 /// Pass Manager itself does not invalidate any analysis info.
287 void getAnalysisUsage(AnalysisUsage &Info) const {
288 Info.setPreservesAll();
289 }
290
Devang Patelabcd1d32006-12-07 21:27:23 +0000291 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000292
Devang Patelfa971cd2006-12-08 23:57:43 +0000293 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000294
295 // P is a immutable pass and it will be managed by this
296 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000297 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000298 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000299 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000300 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000301 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000302 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000303 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000304 }
Devang Patel0f080042007-01-12 17:23:48 +0000305
Devang Patelabcd1d32006-12-07 21:27:23 +0000306 }
307
Devang Patel67d6a5e2006-12-19 19:46:59 +0000308 MPPassManager *getContainedManager(unsigned N) {
309 assert ( N < PassManagers.size() && "Pass number out of range!");
310 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
311 return MP;
312 }
313
Devang Patel376fefa2006-11-08 10:29:57 +0000314};
315
Devang Patel8c78a0b2007-05-03 01:11:54 +0000316char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000317} // End of llvm namespace
318
319namespace {
320
321//===----------------------------------------------------------------------===//
322// TimingInfo Class - This class is used to calculate information about the
323// amount of time each pass takes to execute. This only happens when
324// -time-passes is enabled on the command line.
325//
326
327class VISIBILITY_HIDDEN TimingInfo {
328 std::map<Pass*, Timer> TimingData;
329 TimerGroup TG;
330
331public:
332 // Use 'create' member to get this.
333 TimingInfo() : TG("... Pass execution timing report ...") {}
334
335 // TimingDtor - Print out information about timing information
336 ~TimingInfo() {
337 // Delete all of the timers...
338 TimingData.clear();
339 // TimerGroup is deleted next, printing the report.
340 }
341
342 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
343 // to a non null value (if the -time-passes option is enabled) or it leaves it
344 // null. It may be called multiple times.
345 static void createTheTimeInfo();
346
347 void passStarted(Pass *P) {
348
349 if (dynamic_cast<PMDataManager *>(P))
350 return;
351
352 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
353 if (I == TimingData.end())
354 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
355 I->second.startTimer();
356 }
357 void passEnded(Pass *P) {
358
359 if (dynamic_cast<PMDataManager *>(P))
360 return;
361
362 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
363 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
364 I->second.stopTimer();
365 }
366};
367
Devang Patel1c3633e2007-01-29 23:10:37 +0000368} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000369
Dan Gohmand78c4002008-05-13 00:00:25 +0000370static TimingInfo *TheTimeInfo;
371
Devang Patela1514cb2006-12-07 19:39:39 +0000372//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000373// PMTopLevelManager implementation
374
Devang Patel4268fc02007-01-16 02:00:38 +0000375/// Initialize top level manager. Create first pass manager.
376PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
377
378 if (t == TLM_Pass) {
379 MPPassManager *MPP = new MPPassManager(1);
380 MPP->setTopLevelManager(this);
381 addPassManager(MPP);
382 activeStack.push(MPP);
383 }
384 else if (t == TLM_Function) {
385 FPPassManager *FPP = new FPPassManager(1);
386 FPP->setTopLevelManager(this);
387 addPassManager(FPP);
388 activeStack.push(FPP);
389 }
390}
391
Devang Patelafb1f3622006-12-12 22:35:25 +0000392/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000393void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000394 Pass *P) {
395
Devang Patel8adae862007-07-20 18:04:54 +0000396 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000397 E = AnalysisPasses.end(); I != E; ++I) {
398 Pass *AP = *I;
399 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000400
401 if (P == AP)
402 continue;
403
Devang Patelafb1f3622006-12-12 22:35:25 +0000404 // If AP is the last user of other passes then make P last user of
405 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000406 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000407 LUE = LastUser.end(); LUI != LUE; ++LUI) {
408 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000409 // DenseMap iterator is not invalidated here because
410 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000411 LastUser[LUI->first] = P;
412 }
413 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000414}
415
416/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000417void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000418 Pass *P) {
419 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
420 InversedLastUser.find(P);
421 if (DMI == InversedLastUser.end())
422 return;
423
424 SmallPtrSet<Pass *, 8> &LU = DMI->second;
425 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
426 E = LU.end(); I != E; ++I) {
427 LastUses.push_back(*I);
428 }
429
Devang Patelafb1f3622006-12-12 22:35:25 +0000430}
431
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000432AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
433 AnalysisUsage *AnUsage = NULL;
434 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
435 if (DMI != AnUsageMap.end())
436 AnUsage = DMI->second;
437 else {
438 AnUsage = new AnalysisUsage();
439 P->getAnalysisUsage(*AnUsage);
440 AnUsageMap[P] = AnUsage;
441 }
442 return AnUsage;
443}
444
Devang Patelafb1f3622006-12-12 22:35:25 +0000445/// Schedule pass P for execution. Make sure that passes required by
446/// P are run before P is run. Update analysis info maintained by
447/// the manager. Remove dead passes. This is a recursive function.
448void PMTopLevelManager::schedulePass(Pass *P) {
449
Devang Patel3312f752007-01-16 21:43:18 +0000450 // TODO : Allocate function manager for this pass, other wise required set
451 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000452
Devang Pateld74ede72007-03-06 01:06:16 +0000453 // Give pass a chance to prepare the stage.
454 P->preparePassManager(activeStack);
455
Devang Patel864970e2008-03-18 00:39:19 +0000456 // If P is an analysis pass and it is available then do not
457 // generate the analysis again. Stale analysis info should not be
458 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000459 if (P->getPassInfo() &&
460 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000461 return;
Devang Patel864970e2008-03-18 00:39:19 +0000462
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000463 AnalysisUsage *AnUsage = findAnalysisUsage(P);
464
Devang Patelfdee7032008-08-14 23:07:48 +0000465 bool checkAnalysis = true;
466 while (checkAnalysis) {
467 checkAnalysis = false;
468
469 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
470 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
471 E = RequiredSet.end(); I != E; ++I) {
472
473 Pass *AnalysisPass = findAnalysisPass(*I);
474 if (!AnalysisPass) {
475 AnalysisPass = (*I)->createPass();
476 if (P->getPotentialPassManagerType () ==
477 AnalysisPass->getPotentialPassManagerType())
478 // Schedule analysis pass that is managed by the same pass manager.
479 schedulePass(AnalysisPass);
480 else if (P->getPotentialPassManagerType () >
481 AnalysisPass->getPotentialPassManagerType()) {
482 // Schedule analysis pass that is managed by a new manager.
483 schedulePass(AnalysisPass);
484 // Recheck analysis passes to ensure that required analysises that
485 // are already checked are still available.
486 checkAnalysis = true;
487 }
488 else
489 // Do not schedule this analysis. Lower level analsyis
490 // passes are run on the fly.
491 delete AnalysisPass;
492 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000493 }
494 }
495
496 // Now all required passes are available.
497 addTopLevelPass(P);
498}
499
500/// Find the pass that implements Analysis AID. Search immutable
501/// passes and all pass managers. If desired pass is not found
502/// then return NULL.
503Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
504
505 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000506 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000507 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000508 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000509 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000510 P = PMD->findAnalysisPass(AID, false);
511 }
512
513 // Check other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000514 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000515 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
516 P = (*I)->findAnalysisPass(AID, false);
517
Devang Patel0d29ae02008-08-12 15:44:31 +0000518 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
520 const PassInfo *PI = (*I)->getPassInfo();
521 if (PI == AID)
522 P = *I;
523
524 // If Pass not found then check the interfaces implemented by Immutable Pass
525 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000526 const std::vector<const PassInfo*> &ImmPI =
527 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000528 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
529 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000530 }
531 }
532
Devang Patelafb1f3622006-12-12 22:35:25 +0000533 return P;
534}
535
Devang Pateleda56172006-12-12 23:34:33 +0000536// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000537void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000538
Devang Patelfd4184322007-01-17 20:33:36 +0000539 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000540 return;
541
Devang Pateleda56172006-12-12 23:34:33 +0000542 // Print out the immutable passes
543 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
544 ImmutablePasses[i]->dumpPassStructure(0);
545 }
546
Dan Gohman73caf5f2008-03-13 01:48:32 +0000547 // Every class that derives from PMDataManager also derives from Pass
548 // (sometimes indirectly), but there's no inheritance relationship
549 // between PMDataManager and Pass, so we have to dynamic_cast to get
550 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000551 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000552 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000553 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000554}
555
Devang Patel991aeba2006-12-15 20:13:01 +0000556void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000557
Devang Patelfd4184322007-01-17 20:33:36 +0000558 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000559 return;
560
561 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000562 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000563 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000564 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000565 PMD->dumpPassArguments();
566 }
567 cerr << "\n";
568}
569
Devang Patele3068402006-12-21 00:16:50 +0000570void PMTopLevelManager::initializeAllAnalysisInfo() {
571
Devang Patel0d29ae02008-08-12 15:44:31 +0000572 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000573 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000574 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000575 PMD->initializeAnalysisInfo();
576 }
577
578 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000579 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000580 E = IndirectPassManagers.end(); I != E; ++I)
581 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000582
583 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
584 DME = LastUser.end(); DMI != DME; ++DMI) {
585 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
586 InversedLastUser.find(DMI->second);
587 if (InvDMI != InversedLastUser.end()) {
588 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
589 L.insert(DMI->first);
590 } else {
591 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
592 InversedLastUser[DMI->second] = L;
593 }
594 }
Devang Patele3068402006-12-21 00:16:50 +0000595}
596
Devang Patele7599552007-01-12 18:52:44 +0000597/// Destructor
598PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000599 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000600 E = PassManagers.end(); I != E; ++I)
601 delete *I;
602
Devang Patel0d29ae02008-08-12 15:44:31 +0000603 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000604 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
605 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000606
607 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
608 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
609 AnalysisUsage *AU = DMI->second;
610 delete AU;
611 }
612
Devang Patele7599552007-01-12 18:52:44 +0000613}
614
Devang Patelafb1f3622006-12-12 22:35:25 +0000615//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000616// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000617
Devang Patel643676c2006-11-11 01:10:19 +0000618/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000619void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000620
Devang Patel643676c2006-11-11 01:10:19 +0000621 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000622 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000623
Devang Patele9976aa2006-12-07 19:33:53 +0000624 //This pass is the current implementation of all of the interfaces it
625 //implements as well.
626 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
627 for (unsigned i = 0, e = II.size(); i != e; ++i)
628 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000629 }
630}
631
Devang Patel9d9fc902007-03-06 17:52:53 +0000632// Return true if P preserves high level analysis used by other
633// passes managed by this manager
634bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
635
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000636 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel9d9fc902007-03-06 17:52:53 +0000637
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000638 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000639 return true;
640
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000641 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000642 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000643 E = HigherLevelAnalysis.end(); I != E; ++I) {
644 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000645 if (!dynamic_cast<ImmutablePass*>(P1) &&
646 std::find(PreservedSet.begin(), PreservedSet.end(),
647 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000648 PreservedSet.end())
649 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000650 }
651
652 return true;
653}
654
Chris Lattner02eb94c2008-08-07 07:34:50 +0000655/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000656void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000657 // Don't do this unless assertions are enabled.
658#ifdef NDEBUG
659 return;
660#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000661 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
662 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000663
Devang Patelef432532007-07-19 05:36:09 +0000664 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000665 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000666 E = PreservedSet.end(); I != E; ++I) {
667 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000668 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000669 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000670 }
Devang Patela273d1c2007-07-19 18:02:32 +0000671}
672
Devang Patel9dbe4d12008-07-01 17:44:24 +0000673/// verifyDomInfo - Verify dominator information if it is available.
674void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
675
676 if (!VerifyDomInfo || !P.getResolver())
677 return;
678
679 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
680 if (!DT)
681 return;
682
683 DominatorTree OtherDT;
684 OtherDT.getBase().recalculate(F);
685 if (DT->compare(OtherDT)) {
686 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000687 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000688 cerr << "----- Valid -----\n";
689 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000690 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000691 DT->dump();
692 assert (0 && "Invalid dominator info");
693 }
694
695 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
696 if (!DF)
697 return;
698
699 DominanceFrontier OtherDF;
700 std::vector<BasicBlock*> DTRoots = DT->getRoots();
701 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
702 if (DF->compare(OtherDF)) {
703 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000704 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000705 cerr << "----- Valid -----\n";
706 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000707 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000708 DF->dump();
709 assert (0 && "Invalid dominator info");
710 }
711}
712
Devang Patel67c79a42008-07-01 19:50:56 +0000713/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000714void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000715 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
716 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000717 return;
718
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000719 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000720 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000721 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000722 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000723 if (!dynamic_cast<ImmutablePass*>(Info->second)
724 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000725 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000726 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000727 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000728 if (PassDebugging >= Details) {
729 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000730 cerr << " -- '" << P->getPassName() << "' is not preserving '";
731 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000732 }
733 }
Devang Patel349170f2006-11-11 01:24:55 +0000734 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000735
736 // Check inherited analysis also. If P is not preserving analysis
737 // provided by parent manager then remove it here.
738 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
739
740 if (!InheritedAnalysis[Index])
741 continue;
742
743 for (std::map<AnalysisID, Pass*>::iterator
744 I = InheritedAnalysis[Index]->begin(),
745 E = InheritedAnalysis[Index]->end(); I != E; ) {
746 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000747 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
748 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000749 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000750 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000751 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000752 }
753 }
Devang Patelf68a3492006-11-07 22:35:17 +0000754}
755
Devang Patelca189262006-11-14 03:05:08 +0000756/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000757void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000758 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000759
Devang Patel8adae862007-07-20 18:04:54 +0000760 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000761
Devang Patel2ff44922007-04-16 20:39:59 +0000762 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000763 if (!TPM)
764 return;
765
Devang Patel17ad0962006-12-08 00:37:52 +0000766 TPM->collectLastUses(DeadPasses, P);
767
Devang Patel656a9172008-06-06 17:50:36 +0000768 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000769 cerr << " -*- '" << P->getPassName();
770 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000771 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000772 }
773
Devang Patel8adae862007-07-20 18:04:54 +0000774 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000775 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000776
Devang Patel003a5592007-03-05 20:01:30 +0000777 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000778
Devang Patel7ebf09d2007-03-05 18:20:51 +0000779 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000780 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000781 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000782 if (const PassInfo *PI = (*I)->getPassInfo()) {
783 std::map<AnalysisID, Pass*>::iterator Pos =
784 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000785
Devang Patelc3e3ca92008-10-06 20:36:36 +0000786 // It is possible that pass is already removed from the AvailableAnalysis
787 if (Pos != AvailableAnalysis.end())
788 AvailableAnalysis.erase(Pos);
789
790 // Remove all interfaces this pass implements, for which it is also
791 // listed as the available implementation.
792 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
793 for (unsigned i = 0, e = II.size(); i != e; ++i) {
794 Pos = AvailableAnalysis.find(II[i]);
795 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
796 AvailableAnalysis.erase(Pos);
797 }
798 }
Devang Patel17ad0962006-12-08 00:37:52 +0000799 }
Devang Patelca189262006-11-14 03:05:08 +0000800}
801
Devang Patel8f677ce2006-12-07 18:47:25 +0000802/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000803/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000804void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000805 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000806
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
Devang Patel90b05e02006-11-11 02:04:19 +0000816 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000817
818 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000819 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000820 SmallVector<Pass *, 8> RequiredPasses;
821 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
822
Devang Patelbc03f132006-12-07 23:55:10 +0000823 unsigned PDepth = this->getDepth();
824
Devang Patele64d3052007-04-16 20:12:57 +0000825 collectRequiredAnalysis(RequiredPasses,
826 ReqAnalysisNotAvailable, P);
827 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000828 E = RequiredPasses.end(); I != E; ++I) {
829 Pass *PRequired = *I;
830 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000831
Devang Patel9dbe4d12008-07-01 17:44:24 +0000832 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000833 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
834 RDepth = DM.getDepth();
835
Devang Patelbc03f132006-12-07 23:55:10 +0000836 if (PDepth == RDepth)
837 LastUses.push_back(PRequired);
838 else if (PDepth > RDepth) {
839 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000840 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000841 // Keep track of higher level analysis used by this manager.
842 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000843 } else
844 assert (0 && "Unable to accomodate Required Pass");
845 }
846
Devang Patel39786a92007-01-15 20:31:54 +0000847 // Set P as P's last user until someone starts using P.
848 // However, if P is a Pass Manager then it does not need
849 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000850 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000851 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000852 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000853
Devang Patelec2b9a72007-03-05 22:57:49 +0000854 if (!TransferLastUses.empty()) {
855 Pass *My_PM = dynamic_cast<Pass *>(this);
856 TPM->setLastUser(TransferLastUses, My_PM);
857 TransferLastUses.clear();
858 }
859
Devang Patel69e9f6d2007-04-16 20:27:05 +0000860 // Now, take care of required analysises that are not available.
861 for (SmallVector<AnalysisID, 8>::iterator
862 I = ReqAnalysisNotAvailable.begin(),
863 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
864 Pass *AnalysisPass = (*I)->createPass();
865 this->addLowerLevelRequiredPass(P, AnalysisPass);
866 }
867
Devang Patel17bff0d2006-12-07 22:09:36 +0000868 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000869 // Remove the analysis not preserved by this pass
870 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000871 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000872 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000873
874 // Add pass
875 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000876}
877
Devang Patele64d3052007-04-16 20:12:57 +0000878
879/// Populate RP with analysis pass that are required by
880/// pass P and are available. Populate RP_NotAvail with analysis
881/// pass that are required by pass P but are not available.
882void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
883 SmallVector<AnalysisID, 8> &RP_NotAvail,
884 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000885 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
886 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000887 for (AnalysisUsage::VectorType::const_iterator
Devang Patel1d6267c2006-12-07 23:05:44 +0000888 I = RequiredSet.begin(), E = RequiredSet.end();
889 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000890 AnalysisID AID = *I;
891 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
892 RP.push_back(AnalysisPass);
893 else
894 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000895 }
Devang Patelf58183d2006-12-12 23:09:32 +0000896
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000897 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000898 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000899 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000900 AnalysisID AID = *I;
901 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
902 RP.push_back(AnalysisPass);
903 else
904 RP_NotAvail.push_back(AID);
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();
Devang Patel9dbe4d12008-07-01 17:44:24 +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) {
960 llvm::cerr << "--" << std::string(Offset*2, ' ');
961 (*I)->dumpPassStructure(0);
962 }
963}
964
965void PMDataManager::dumpPassArguments() const {
Devang Patel0d29ae02008-08-12 15:44:31 +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())
973 cerr << " -" << PI->getPassArgument();
974 }
975}
976
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000977void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
978 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000979 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000980 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000981 return;
982 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000983 switch (S1) {
984 case EXECUTION_MSG:
985 cerr << "Executing Pass '" << P->getPassName();
986 break;
987 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000988 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000989 break;
990 case FREEING_MSG:
991 cerr << " Freeing Pass '" << P->getPassName();
992 break;
993 default:
994 break;
995 }
996 switch (S2) {
997 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000998 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000999 break;
1000 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001001 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001002 break;
1003 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001004 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001005 break;
1006 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001007 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001008 break;
1009 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001010 cerr << "' on Call Graph " << 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 Lattner4c493d92008-08-08 15:14:09 +00001017void PMDataManager::dumpRequiredSet(const Pass *P)
1018 const {
1019 if (PassDebugging < Details)
1020 return;
1021
1022 AnalysisUsage analysisUsage;
1023 P->getAnalysisUsage(analysisUsage);
1024 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1025}
1026
1027void PMDataManager::dumpPreservedSet(const Pass *P)
1028 const {
1029 if (PassDebugging < Details)
1030 return;
1031
1032 AnalysisUsage analysisUsage;
1033 P->getAnalysisUsage(analysisUsage);
1034 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1035}
1036
1037void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattnercbd160f2008-08-08 05:33:04 +00001038 const AnalysisUsage::VectorType &Set)
Devang Patel991aeba2006-12-15 20:13:01 +00001039 const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001040 assert(PassDebugging >= Details);
1041 if (Set.empty())
1042 return;
1043 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1044 for (unsigned i = 0; i != Set.size(); ++i) {
1045 if (i) cerr << ",";
1046 cerr << " " << Set[i]->getPassName();
1047 }
1048 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001049}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001050
Devang Patel004937b2007-07-27 20:06:09 +00001051/// Add RequiredPass into list of lower level passes required by pass P.
1052/// RequiredPass is run on the fly by Pass Manager when P requests it
1053/// through getAnalysis interface.
1054/// This should be handled by specific pass manager.
1055void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1056 if (TPM) {
1057 TPM->dumpArguments();
1058 TPM->dumpPasses();
1059 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001060
1061 // Module Level pass may required Function Level analysis info
1062 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1063 // to provide this on demand. In that case, in Pass manager terminology,
1064 // module level pass is requiring lower level analysis info managed by
1065 // lower level pass manager.
1066
1067 // When Pass manager is not able to order required analysis info, Pass manager
1068 // checks whether any lower level manager will be able to provide this
1069 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001070#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001071 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1072 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001073#endif
1074 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001075}
1076
Devang Patele7599552007-01-12 18:52:44 +00001077// Destructor
1078PMDataManager::~PMDataManager() {
1079
Devang Patel0d29ae02008-08-12 15:44:31 +00001080 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001081 E = PassVector.end(); I != E; ++I)
1082 delete *I;
1083
Devang Patele7599552007-01-12 18:52:44 +00001084}
1085
Devang Patel9bdf7d42006-12-08 23:28:54 +00001086//===----------------------------------------------------------------------===//
1087// NOTE: Is this the right place to define this method ?
1088// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001089Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001090 return PM.findAnalysisPass(ID, dir);
1091}
1092
Devang Patel92942812007-04-16 20:56:24 +00001093Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1094 Function &F) {
1095 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1096}
1097
Devang Patela1514cb2006-12-07 19:39:39 +00001098//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001099// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001100
Devang Patel6e5a1132006-11-07 21:31:57 +00001101/// Execute all of the passes scheduled for execution by invoking
1102/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1103/// the function, and if so, return true.
1104bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001105BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001106
Reid Spencer5301e7c2007-01-30 20:08:39 +00001107 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001108 return false;
1109
Devang Patele9585592006-12-08 01:38:28 +00001110 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001111
Devang Patel6e5a1132006-11-07 21:31:57 +00001112 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001113 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1114 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001115
Devang Pateld305c402007-08-10 18:29:32 +00001116 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001117 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001118
Devang Patelabfbe3b2006-12-16 00:56:26 +00001119 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001120
Devang Patelabfbe3b2006-12-16 00:56:26 +00001121 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001122 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001123 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001124
Devang Patel003a5592007-03-05 20:01:30 +00001125 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001126 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1127 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001128 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001129
Devang Patela273d1c2007-07-19 18:02:32 +00001130 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001131 removeNotPreservedAnalysis(BP);
1132 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001133 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001134 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001135
Devang Patel56d48ec2006-12-15 22:57:49 +00001136 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001137}
1138
Devang Patel475c4532006-12-08 00:59:05 +00001139// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001140inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001141 bool Changed = false;
1142
Devang Patelabfbe3b2006-12-16 00:56:26 +00001143 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1144 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001145 Changed |= BP->doInitialization(M);
1146 }
1147
1148 return Changed;
1149}
1150
Devang Patel67d6a5e2006-12-19 19:46:59 +00001151inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001152 bool Changed = false;
1153
Devang Patelabfbe3b2006-12-16 00:56:26 +00001154 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1155 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001156 Changed |= BP->doFinalization(M);
1157 }
1158
1159 return Changed;
1160}
1161
Devang Patel67d6a5e2006-12-19 19:46:59 +00001162inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001163 bool Changed = false;
1164
Devang Patelabfbe3b2006-12-16 00:56:26 +00001165 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1166 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001167 Changed |= BP->doInitialization(F);
1168 }
1169
1170 return Changed;
1171}
1172
Devang Patel67d6a5e2006-12-19 19:46:59 +00001173inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001174 bool Changed = false;
1175
Devang Patelabfbe3b2006-12-16 00:56:26 +00001176 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1177 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001178 Changed |= BP->doFinalization(F);
1179 }
1180
1181 return Changed;
1182}
1183
1184
Devang Patela1514cb2006-12-07 19:39:39 +00001185//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001186// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001187
Devang Patel4e12f862006-11-08 10:44:40 +00001188/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001189FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001190 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001191 // FPM is the top level manager.
1192 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001193
Dan Gohman565df952008-03-13 02:08:36 +00001194 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001195 FPM->setResolver(AR);
1196
Devang Patel1f653682006-12-08 18:57:16 +00001197 MP = P;
1198}
1199
Devang Patelb67904d2006-12-13 02:36:01 +00001200FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001201 delete FPM;
1202}
1203
Devang Patel4e12f862006-11-08 10:44:40 +00001204/// add - Add a pass to the queue of passes to run. This passes
1205/// ownership of the Pass to the PassManager. When the
1206/// PassManager_X is destroyed, the pass will be destroyed as well, so
1207/// there is no need to delete the pass. (TODO delete passes.)
1208/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001209void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001210 FPM->add(P);
1211}
1212
Devang Patel9f3083e2006-11-15 19:39:54 +00001213/// run - Execute all of the passes scheduled for execution. Keep
1214/// track of whether any of the passes modifies the function, and if
1215/// so, return true.
1216///
Devang Patelb67904d2006-12-13 02:36:01 +00001217bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001218 std::string errstr;
1219 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001220 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001221 abort();
1222 }
Devang Patel272908d2006-12-08 22:57:48 +00001223 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001224}
1225
1226
Devang Patelff631ae2006-11-15 01:27:05 +00001227/// doInitialization - Run all of the initializers for the function passes.
1228///
Devang Patelb67904d2006-12-13 02:36:01 +00001229bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001230 return FPM->doInitialization(*MP->getModule());
1231}
1232
Dan Gohmane6656eb2007-07-30 14:51:13 +00001233/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001234///
Devang Patelb67904d2006-12-13 02:36:01 +00001235bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001236 return FPM->doFinalization(*MP->getModule());
1237}
1238
Devang Patela1514cb2006-12-07 19:39:39 +00001239//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001240// FunctionPassManagerImpl implementation
1241//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001242inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1243 bool Changed = false;
1244
1245 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1246 FPPassManager *FP = getContainedManager(Index);
1247 Changed |= FP->doInitialization(M);
1248 }
1249
1250 return Changed;
1251}
1252
1253inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1254 bool Changed = false;
1255
1256 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1257 FPPassManager *FP = getContainedManager(Index);
1258 Changed |= FP->doFinalization(M);
1259 }
1260
1261 return Changed;
1262}
1263
1264// Execute all the passes managed by this top level manager.
1265// Return true if any function is modified by a pass.
1266bool FunctionPassManagerImpl::run(Function &F) {
1267
1268 bool Changed = false;
1269
1270 TimingInfo::createTheTimeInfo();
1271
1272 dumpArguments();
1273 dumpPasses();
1274
Devang Patele3068402006-12-21 00:16:50 +00001275 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001276 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1277 FPPassManager *FP = getContainedManager(Index);
1278 Changed |= FP->runOnFunction(F);
1279 }
1280 return Changed;
1281}
1282
1283//===----------------------------------------------------------------------===//
1284// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001285
Devang Patel8c78a0b2007-05-03 01:11:54 +00001286char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001287/// Print passes managed by this manager
1288void FPPassManager::dumpPassStructure(unsigned Offset) {
1289 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1290 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1291 FunctionPass *FP = getContainedPass(Index);
1292 FP->dumpPassStructure(Offset + 1);
1293 dumpLastUses(FP, Offset+1);
1294 }
1295}
1296
1297
Devang Patel0c2012f2006-11-07 21:49:50 +00001298/// Execute all of the passes scheduled for execution by invoking
1299/// runOnFunction method. Keep track of whether any of the passes modifies
1300/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001301bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001302
1303 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001304
Reid Spencer5301e7c2007-01-30 20:08:39 +00001305 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001306 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001307
1308 // Collect inherited analysis from Module level pass manager.
1309 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001310
Devang Patelabfbe3b2006-12-16 00:56:26 +00001311 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1312 FunctionPass *FP = getContainedPass(Index);
1313
Devang Pateld305c402007-08-10 18:29:32 +00001314 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001315 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001316
Devang Patelabfbe3b2006-12-16 00:56:26 +00001317 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001318
Devang Patelabfbe3b2006-12-16 00:56:26 +00001319 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001320 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001321 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001322
Devang Patel003a5592007-03-05 20:01:30 +00001323 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001324 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001325 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001326
Devang Patela273d1c2007-07-19 18:02:32 +00001327 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 removeNotPreservedAnalysis(FP);
1329 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001330 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001331
Devang Patel67c79a42008-07-01 19:50:56 +00001332 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001333 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001334 }
1335 return Changed;
1336}
1337
Devang Patel67d6a5e2006-12-19 19:46:59 +00001338bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001339
Devang Patel67d6a5e2006-12-19 19:46:59 +00001340 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001341
1342 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1343 this->runOnFunction(*I);
1344
1345 return Changed |= doFinalization(M);
1346}
1347
1348inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001349 bool Changed = false;
1350
Devang Patelabfbe3b2006-12-16 00:56:26 +00001351 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1352 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001353 Changed |= FP->doInitialization(M);
1354 }
1355
1356 return Changed;
1357}
1358
Devang Patel67d6a5e2006-12-19 19:46:59 +00001359inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001360 bool Changed = false;
1361
Devang Patelabfbe3b2006-12-16 00:56:26 +00001362 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1363 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001364 Changed |= FP->doFinalization(M);
1365 }
1366
Devang Patelff631ae2006-11-15 01:27:05 +00001367 return Changed;
1368}
1369
Devang Patela1514cb2006-12-07 19:39:39 +00001370//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001371// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001372
Devang Patel05e1a972006-11-07 22:03:15 +00001373/// Execute all of the passes scheduled for execution by invoking
1374/// runOnModule method. Keep track of whether any of the passes modifies
1375/// the module, and if so, return true.
1376bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001377MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001378 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001379
Devang Patelabfbe3b2006-12-16 00:56:26 +00001380 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1381 ModulePass *MP = getContainedPass(Index);
1382
Dan Gohman929391a2008-01-29 12:09:55 +00001383 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1384 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001385 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001386
Devang Patelabfbe3b2006-12-16 00:56:26 +00001387 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001388
Devang Patelabfbe3b2006-12-16 00:56:26 +00001389 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001390 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001391 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001392
Devang Patel003a5592007-03-05 20:01:30 +00001393 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001394 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1395 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001396 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001397
Devang Patela273d1c2007-07-19 18:02:32 +00001398 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001399 removeNotPreservedAnalysis(MP);
1400 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001401 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001402 }
1403 return Changed;
1404}
1405
Devang Patele64d3052007-04-16 20:12:57 +00001406/// Add RequiredPass into list of lower level passes required by pass P.
1407/// RequiredPass is run on the fly by Pass Manager when P requests it
1408/// through getAnalysis interface.
1409void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1410
1411 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1412 && "Unable to handle Pass that requires lower level Analysis pass");
1413 assert ((P->getPotentialPassManagerType() <
1414 RequiredPass->getPotentialPassManagerType())
1415 && "Unable to handle Pass that requires lower level Analysis pass");
1416
Devang Patel68f72b12007-04-26 17:50:19 +00001417 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001418 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001419 FPP = new FunctionPassManagerImpl(0);
1420 // FPP is the top level manager.
1421 FPP->setTopLevelManager(FPP);
1422
Devang Patel69e9f6d2007-04-16 20:27:05 +00001423 OnTheFlyManagers[P] = FPP;
1424 }
Devang Patel68f72b12007-04-26 17:50:19 +00001425 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001426
Devang Patel68f72b12007-04-26 17:50:19 +00001427 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001428 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001429 LU.push_back(RequiredPass);
1430 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001431}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001432
1433/// Return function pass corresponding to PassInfo PI, that is
1434/// required by module pass MP. Instantiate analysis pass, by using
1435/// its runOnFunction() for function F.
1436Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1437 Function &F) {
1438 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001439 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001440 assert (FPP && "Unable to find on the fly pass");
1441
Devang Patel68f72b12007-04-26 17:50:19 +00001442 FPP->run(F);
1443 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001444}
1445
1446
Devang Patela1514cb2006-12-07 19:39:39 +00001447//===----------------------------------------------------------------------===//
1448// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001449//
Devang Patelc290c8a2006-11-07 22:23:34 +00001450/// run - Execute all of the passes scheduled for execution. Keep track of
1451/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001452bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001453
Devang Patelc290c8a2006-11-07 22:23:34 +00001454 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001455
Devang Patelb8817b92006-12-14 00:59:42 +00001456 TimingInfo::createTheTimeInfo();
1457
Devang Patelcfd70c42006-12-13 22:10:00 +00001458 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001459 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001460
Devang Patele3068402006-12-21 00:16:50 +00001461 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001462 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1463 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001464 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001465 }
1466 return Changed;
1467}
Devang Patel376fefa2006-11-08 10:29:57 +00001468
Devang Patela1514cb2006-12-07 19:39:39 +00001469//===----------------------------------------------------------------------===//
1470// PassManager implementation
1471
Devang Patel376fefa2006-11-08 10:29:57 +00001472/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001473PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001474 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001475 // PM is the top level manager
1476 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001477}
1478
Devang Patelb67904d2006-12-13 02:36:01 +00001479PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001480 delete PM;
1481}
1482
Devang Patel376fefa2006-11-08 10:29:57 +00001483/// add - Add a pass to the queue of passes to run. This passes ownership of
1484/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1485/// will be destroyed as well, so there is no need to delete the pass. This
1486/// implies that all passes MUST be allocated with 'new'.
1487void
Devang Patelb67904d2006-12-13 02:36:01 +00001488PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001489 PM->add(P);
1490}
1491
1492/// run - Execute all of the passes scheduled for execution. Keep track of
1493/// whether any of the passes modifies the module, and if so, return true.
1494bool
Devang Patelb67904d2006-12-13 02:36:01 +00001495PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001496 return PM->run(M);
1497}
1498
Devang Patelb8817b92006-12-14 00:59:42 +00001499//===----------------------------------------------------------------------===//
1500// TimingInfo Class - This class is used to calculate information about the
1501// amount of time each pass takes to execute. This only happens with
1502// -time-passes is enabled on the command line.
1503//
1504bool llvm::TimePassesIsEnabled = false;
1505static cl::opt<bool,true>
1506EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1507 cl::desc("Time each pass, printing elapsed time for each on exit"));
1508
1509// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1510// a non null value (if the -time-passes option is enabled) or it leaves it
1511// null. It may be called multiple times.
1512void TimingInfo::createTheTimeInfo() {
1513 if (!TimePassesIsEnabled || TheTimeInfo) return;
1514
1515 // Constructed the first time this is called, iff -time-passes is enabled.
1516 // This guarantees that the object will be constructed before static globals,
1517 // thus it will be destroyed before them.
1518 static ManagedStatic<TimingInfo> TTI;
1519 TheTimeInfo = &*TTI;
1520}
1521
Devang Patel1c3633e2007-01-29 23:10:37 +00001522/// If TimingInfo is enabled then start pass timer.
1523void StartPassTimer(Pass *P) {
1524 if (TheTimeInfo)
1525 TheTimeInfo->passStarted(P);
1526}
1527
1528/// If TimingInfo is enabled then stop pass timer.
1529void StopPassTimer(Pass *P) {
1530 if (TheTimeInfo)
1531 TheTimeInfo->passEnded(P);
1532}
1533
Devang Patel1c56a632007-01-08 19:29:38 +00001534//===----------------------------------------------------------------------===//
1535// PMStack implementation
1536//
Devang Patelad98d232007-01-11 22:15:30 +00001537
Devang Patel1c56a632007-01-08 19:29:38 +00001538// Pop Pass Manager from the stack and clear its analysis info.
1539void PMStack::pop() {
1540
1541 PMDataManager *Top = this->top();
1542 Top->initializeAnalysisInfo();
1543
1544 S.pop_back();
1545}
1546
1547// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001548void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001549
Devang Patel15701b52007-01-11 00:19:00 +00001550 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001551 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001552
Devang Patel15701b52007-01-11 00:19:00 +00001553 if (this->empty()) {
1554 Top = PM;
1555 }
1556 else {
1557 Top = this->top();
1558 PMTopLevelManager *TPM = Top->getTopLevelManager();
1559
1560 assert (TPM && "Unable to find top level manager");
1561 TPM->addIndirectPassManager(PM);
1562 PM->setTopLevelManager(TPM);
1563 }
1564
Devang Patel15701b52007-01-11 00:19:00 +00001565 S.push_back(PM);
1566}
1567
1568// Dump content of the pass manager stack.
1569void PMStack::dump() {
1570 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1571 E = S.end(); I != E; ++I) {
1572 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001573 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001574 }
1575 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001576 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001577}
1578
Devang Patel1c56a632007-01-08 19:29:38 +00001579/// Find appropriate Module Pass Manager in the PM Stack and
1580/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001581void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001582 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001583
Devang Patel1c56a632007-01-08 19:29:38 +00001584 // Find Module Pass Manager
1585 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001586 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1587 if (TopPMType == PreferredType)
1588 break; // We found desired pass manager
1589 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001590 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001591 else
1592 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001593 }
Devang Patel18ff6362008-09-09 21:38:40 +00001594 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001595 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001596}
1597
Devang Patel3312f752007-01-16 21:43:18 +00001598/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1599/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001600void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001601 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001602
Devang Patela3286902008-09-09 17:56:50 +00001603 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001604 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001605 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1606 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001607 else
Devang Patel3312f752007-01-16 21:43:18 +00001608 break;
1609 }
1610 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1611
1612 // Create new Function Pass Manager
1613 if (!FPP) {
1614 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1615 PMDataManager *PMD = PMS.top();
1616
1617 // [1] Create new Function Pass Manager
1618 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001619 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001620
1621 // [2] Set up new manager's top level manager
1622 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1623 TPM->addIndirectPassManager(FPP);
1624
1625 // [3] Assign manager to manage this new manager. This may create
1626 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001627 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001628
1629 // [4] Push new manager into PMS
1630 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001631 }
1632
Devang Patel3312f752007-01-16 21:43:18 +00001633 // Assign FPP as the manager of this pass.
1634 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001635}
1636
Devang Patel3312f752007-01-16 21:43:18 +00001637/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001638/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001639void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001640 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001641
1642 BBPassManager *BBP = NULL;
1643
Devang Patel15701b52007-01-11 00:19:00 +00001644 // Basic Pass Manager is a leaf pass manager. It does not handle
1645 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001646 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001647 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001648
Devang Patel3312f752007-01-16 21:43:18 +00001649 // If leaf manager is not Basic Block Pass manager then create new
1650 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001651
Devang Patel3312f752007-01-16 21:43:18 +00001652 if (!BBP) {
1653 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1654 PMDataManager *PMD = PMS.top();
1655
1656 // [1] Create new Basic Block Manager
1657 BBP = new BBPassManager(PMD->getDepth() + 1);
1658
1659 // [2] Set up new manager's top level manager
1660 // Basic Block Pass Manager does not live by itself
1661 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1662 TPM->addIndirectPassManager(BBP);
1663
Devang Patel15701b52007-01-11 00:19:00 +00001664 // [3] Assign manager to manage this new manager. This may create
1665 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001666 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001667
Devang Patel3312f752007-01-16 21:43:18 +00001668 // [4] Push new manager into PMS
1669 PMS.push(BBP);
1670 }
Devang Patel1c56a632007-01-08 19:29:38 +00001671
Devang Patel3312f752007-01-16 21:43:18 +00001672 // Assign BBP as the manager of this pass.
1673 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001674}
1675
Dan Gohmand3a20c92008-03-11 16:41:42 +00001676PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001677
1678/*===-- C Bindings --------------------------------------------------------===*/
1679
1680LLVMPassManagerRef LLVMCreatePassManager() {
1681 return wrap(new PassManager());
1682}
1683
1684LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1685 return wrap(new FunctionPassManager(unwrap(P)));
1686}
1687
1688int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1689 return unwrap<PassManager>(PM)->run(*unwrap(M));
1690}
1691
1692int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1693 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1694}
1695
1696int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1697 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1698}
1699
1700int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1701 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1702}
1703
1704void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1705 delete unwrap(PM);
1706}