blob: ef90aace3dab03d3e07f5ff6bf435934f9c28fa3 [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>
Duncan Sands26ff6f92008-10-08 07:23:46 +000025#include <cstdio>
Devang Patela9844592006-11-11 01:31:05 +000026#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000027#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000028using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000029
Devang Patele7599552007-01-12 18:52:44 +000030// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000031
Devang Patelf1567a52006-12-13 20:03:48 +000032namespace llvm {
33
34//===----------------------------------------------------------------------===//
35// Pass debugging information. Often it is useful to find out what pass is
36// running when a crash occurs in a utility. When this library is compiled with
37// debugging on, a command line option (--debug-pass) is enabled that causes the
38// pass name to be printed before it executes.
39//
40
Devang Patel03fb5872006-12-13 21:13:31 +000041// Different debug levels that can be enabled...
42enum PassDebugLevel {
43 None, Arguments, Structure, Executions, Details
44};
45
Devang Patel99ad4ba2008-07-01 21:36:11 +000046bool VerifyDomInfo = false;
Devang Patel9dbe4d12008-07-01 17:44:24 +000047static cl::opt<bool,true>
48VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
49 cl::desc("Verify dominator info (time consuming)"));
50
Devang Patelf1567a52006-12-13 20:03:48 +000051static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000052PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000053 cl::desc("Print PassManager debugging information"),
54 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000055 clEnumVal(None , "disable debug output"),
56 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
57 clEnumVal(Structure , "print pass structure before run()"),
58 clEnumVal(Executions, "print pass name before it is executed"),
59 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000060 clEnumValEnd));
61} // End of llvm namespace
62
Devang Patelffca9102006-12-15 19:39:30 +000063namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000064
Devang Patelf33f3eb2006-12-07 19:21:29 +000065//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000066// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000067//
Devang Patel67d6a5e2006-12-19 19:46:59 +000068/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000069/// pass together and sequence them to process one basic block before
70/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000071class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
72 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000073
74public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000075 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000076 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +000077 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000078
Devang Patelca58e352006-11-08 10:05:38 +000079 /// Execute all of the passes scheduled for execution. Keep track of
80 /// whether any of the passes modifies the function, and if so, return true.
81 bool runOnFunction(Function &F);
82
Devang Patelf9d96b92006-12-07 19:57:52 +000083 /// Pass Manager itself does not invalidate any analysis info.
84 void getAnalysisUsage(AnalysisUsage &Info) const {
85 Info.setPreservesAll();
86 }
87
Devang Patel475c4532006-12-08 00:59:05 +000088 bool doInitialization(Module &M);
89 bool doInitialization(Function &F);
90 bool doFinalization(Module &M);
91 bool doFinalization(Function &F);
92
Devang Patele3858e62007-02-01 22:08:25 +000093 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +000094 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000095 }
96
Devang Pateleda56172006-12-12 23:34:33 +000097 // Print passes managed by this manager
98 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000099 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000100 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
101 BasicBlockPass *BP = getContainedPass(Index);
102 BP->dumpPassStructure(Offset + 1);
103 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000104 }
105 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000106
107 BasicBlockPass *getContainedPass(unsigned N) {
108 assert ( N < PassVector.size() && "Pass number out of range!");
109 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
110 return BP;
111 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000112
Devang Patel28349ab2007-02-27 15:00:39 +0000113 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000114 return PMT_BasicBlockPassManager;
115 }
Devang Patelca58e352006-11-08 10:05:38 +0000116};
117
Devang Patel8c78a0b2007-05-03 01:11:54 +0000118char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000119}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000120
Devang Patele7599552007-01-12 18:52:44 +0000121namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000122
Devang Patel10c2ca62006-12-12 22:47:13 +0000123//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000124// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000125//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000126/// FunctionPassManagerImpl manages FPPassManagers
127class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000128 public PMDataManager,
129 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000130public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000131 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000132 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000133 Pass(&ID), PMDataManager(Depth),
Devang Patel09f162c2007-05-01 21:15:47 +0000134 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000135
136 /// add - Add a pass to the queue of passes to run. This passes ownership of
137 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
138 /// will be destroyed as well, so there is no need to delete the pass. This
139 /// implies that all passes MUST be allocated with 'new'.
140 void add(Pass *P) {
141 schedulePass(P);
142 }
143
144 /// run - Execute all of the passes scheduled for execution. Keep track of
145 /// whether any of the passes modifies the module, and if so, return true.
146 bool run(Function &F);
147
148 /// doInitialization - Run all of the initializers for the function passes.
149 ///
150 bool doInitialization(Module &M);
151
Dan Gohmane6656eb2007-07-30 14:51:13 +0000152 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000153 ///
154 bool doFinalization(Module &M);
155
156 /// Pass Manager itself does not invalidate any analysis info.
157 void getAnalysisUsage(AnalysisUsage &Info) const {
158 Info.setPreservesAll();
159 }
160
161 inline void addTopLevelPass(Pass *P) {
162
163 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
164
165 // P is a immutable pass and it will be managed by this
166 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000167 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000168 P->setResolver(AR);
169 initializeAnalysisImpl(P);
170 addImmutablePass(IP);
171 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000172 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000173 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000174 }
Devang Patel0f080042007-01-12 17:23:48 +0000175
Devang Patel67d6a5e2006-12-19 19:46:59 +0000176 }
177
178 FPPassManager *getContainedManager(unsigned N) {
179 assert ( N < PassManagers.size() && "Pass number out of range!");
180 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
181 return FP;
182 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000183};
184
Devang Patel8c78a0b2007-05-03 01:11:54 +0000185char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000186//===----------------------------------------------------------------------===//
187// MPPassManager
188//
189/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000190/// It batches all Module passes and function pass managers together and
191/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000192class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000193
194public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000195 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000196 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000197 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000198
199 // Delete on the fly managers.
200 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000201 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000202 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
203 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000204 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000205 delete FPP;
206 }
207 }
208
Devang Patelca58e352006-11-08 10:05:38 +0000209 /// run - Execute all of the passes scheduled for execution. Keep track of
210 /// whether any of the passes modifies the module, and if so, return true.
211 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000212
Devang Patelf9d96b92006-12-07 19:57:52 +0000213 /// Pass Manager itself does not invalidate any analysis info.
214 void getAnalysisUsage(AnalysisUsage &Info) const {
215 Info.setPreservesAll();
216 }
217
Devang Patele64d3052007-04-16 20:12:57 +0000218 /// Add RequiredPass into list of lower level passes required by pass P.
219 /// RequiredPass is run on the fly by Pass Manager when P requests it
220 /// through getAnalysis interface.
221 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
222
Devang Patel69e9f6d2007-04-16 20:27:05 +0000223 /// Return function pass corresponding to PassInfo PI, that is
224 /// required by module pass MP. Instantiate analysis pass, by using
225 /// its runOnFunction() for function F.
226 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
227
Devang Patele3858e62007-02-01 22:08:25 +0000228 virtual const char *getPassName() const {
229 return "Module Pass Manager";
230 }
231
Devang Pateleda56172006-12-12 23:34:33 +0000232 // Print passes managed by this manager
233 void dumpPassStructure(unsigned Offset) {
234 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000235 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
236 ModulePass *MP = getContainedPass(Index);
237 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000238 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000239 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000240 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000241 }
242 }
243
Devang Patelabfbe3b2006-12-16 00:56:26 +0000244 ModulePass *getContainedPass(unsigned N) {
245 assert ( N < PassVector.size() && "Pass number out of range!");
246 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
247 return MP;
248 }
249
Devang Patel28349ab2007-02-27 15:00:39 +0000250 virtual PassManagerType getPassManagerType() const {
251 return PMT_ModulePassManager;
252 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000253
254 private:
255 /// Collection of on the fly FPPassManagers. These managers manage
256 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000257 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000258};
259
Devang Patel8c78a0b2007-05-03 01:11:54 +0000260char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000261//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000262// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000263//
Devang Patel09f162c2007-05-01 21:15:47 +0000264
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265/// PassManagerImpl manages MPPassManagers
266class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000267 public PMDataManager,
268 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000269
270public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000271 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000272 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000273 Pass(&ID), PMDataManager(Depth), 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.
Devang Patelc68a0b62008-08-12 00:26:16 +0000407 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000408 LUE = LastUser.end(); LUI != LUE; ++LUI) {
409 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000410 // DenseMap iterator is not invalidated here because
411 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000412 LastUser[LUI->first] = P;
413 }
414 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000415}
416
417/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000418void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000419 Pass *P) {
420 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
421 InversedLastUser.find(P);
422 if (DMI == InversedLastUser.end())
423 return;
424
425 SmallPtrSet<Pass *, 8> &LU = DMI->second;
426 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
427 E = LU.end(); I != E; ++I) {
428 LastUses.push_back(*I);
429 }
430
Devang Patelafb1f3622006-12-12 22:35:25 +0000431}
432
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000433AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
434 AnalysisUsage *AnUsage = NULL;
435 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
436 if (DMI != AnUsageMap.end())
437 AnUsage = DMI->second;
438 else {
439 AnUsage = new AnalysisUsage();
440 P->getAnalysisUsage(*AnUsage);
441 AnUsageMap[P] = AnUsage;
442 }
443 return AnUsage;
444}
445
Devang Patelafb1f3622006-12-12 22:35:25 +0000446/// Schedule pass P for execution. Make sure that passes required by
447/// P are run before P is run. Update analysis info maintained by
448/// the manager. Remove dead passes. This is a recursive function.
449void PMTopLevelManager::schedulePass(Pass *P) {
450
Devang Patel3312f752007-01-16 21:43:18 +0000451 // TODO : Allocate function manager for this pass, other wise required set
452 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000453
Devang Pateld74ede72007-03-06 01:06:16 +0000454 // Give pass a chance to prepare the stage.
455 P->preparePassManager(activeStack);
456
Devang Patel864970e2008-03-18 00:39:19 +0000457 // If P is an analysis pass and it is available then do not
458 // generate the analysis again. Stale analysis info should not be
459 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000460 if (P->getPassInfo() &&
461 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000462 return;
Devang Patel864970e2008-03-18 00:39:19 +0000463
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000464 AnalysisUsage *AnUsage = findAnalysisUsage(P);
465
Devang Patelfdee7032008-08-14 23:07:48 +0000466 bool checkAnalysis = true;
467 while (checkAnalysis) {
468 checkAnalysis = false;
469
470 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
471 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
472 E = RequiredSet.end(); I != E; ++I) {
473
474 Pass *AnalysisPass = findAnalysisPass(*I);
475 if (!AnalysisPass) {
476 AnalysisPass = (*I)->createPass();
477 if (P->getPotentialPassManagerType () ==
478 AnalysisPass->getPotentialPassManagerType())
479 // Schedule analysis pass that is managed by the same pass manager.
480 schedulePass(AnalysisPass);
481 else if (P->getPotentialPassManagerType () >
482 AnalysisPass->getPotentialPassManagerType()) {
483 // Schedule analysis pass that is managed by a new manager.
484 schedulePass(AnalysisPass);
485 // Recheck analysis passes to ensure that required analysises that
486 // are already checked are still available.
487 checkAnalysis = true;
488 }
489 else
490 // Do not schedule this analysis. Lower level analsyis
491 // passes are run on the fly.
492 delete AnalysisPass;
493 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000494 }
495 }
496
497 // Now all required passes are available.
498 addTopLevelPass(P);
499}
500
501/// Find the pass that implements Analysis AID. Search immutable
502/// passes and all pass managers. If desired pass is not found
503/// then return NULL.
504Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
505
506 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000507 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000508 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000509 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000510 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000511 P = PMD->findAnalysisPass(AID, false);
512 }
513
514 // Check other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000515 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000516 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
517 P = (*I)->findAnalysisPass(AID, false);
518
Devang Patel0d29ae02008-08-12 15:44:31 +0000519 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000520 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
521 const PassInfo *PI = (*I)->getPassInfo();
522 if (PI == AID)
523 P = *I;
524
525 // If Pass not found then check the interfaces implemented by Immutable Pass
526 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000527 const std::vector<const PassInfo*> &ImmPI =
528 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000529 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
530 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000531 }
532 }
533
Devang Patelafb1f3622006-12-12 22:35:25 +0000534 return P;
535}
536
Devang Pateleda56172006-12-12 23:34:33 +0000537// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000538void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000539
Devang Patelfd4184322007-01-17 20:33:36 +0000540 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000541 return;
542
Devang Pateleda56172006-12-12 23:34:33 +0000543 // Print out the immutable passes
544 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
545 ImmutablePasses[i]->dumpPassStructure(0);
546 }
547
Dan Gohman73caf5f2008-03-13 01:48:32 +0000548 // Every class that derives from PMDataManager also derives from Pass
549 // (sometimes indirectly), but there's no inheritance relationship
550 // between PMDataManager and Pass, so we have to dynamic_cast to get
551 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000552 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000553 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000554 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000555}
556
Devang Patel991aeba2006-12-15 20:13:01 +0000557void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000558
Devang Patelfd4184322007-01-17 20:33:36 +0000559 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000560 return;
561
562 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000563 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000564 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000565 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000566 PMD->dumpPassArguments();
567 }
568 cerr << "\n";
569}
570
Devang Patele3068402006-12-21 00:16:50 +0000571void PMTopLevelManager::initializeAllAnalysisInfo() {
572
Devang Patel0d29ae02008-08-12 15:44:31 +0000573 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000574 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000575 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000576 PMD->initializeAnalysisInfo();
577 }
578
579 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000580 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000581 E = IndirectPassManagers.end(); I != E; ++I)
582 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000583
584 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
585 DME = LastUser.end(); DMI != DME; ++DMI) {
586 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
587 InversedLastUser.find(DMI->second);
588 if (InvDMI != InversedLastUser.end()) {
589 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
590 L.insert(DMI->first);
591 } else {
592 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
593 InversedLastUser[DMI->second] = L;
594 }
595 }
Devang Patele3068402006-12-21 00:16:50 +0000596}
597
Devang Patele7599552007-01-12 18:52:44 +0000598/// Destructor
599PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000600 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000601 E = PassManagers.end(); I != E; ++I)
602 delete *I;
603
Devang Patel0d29ae02008-08-12 15:44:31 +0000604 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000605 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
606 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000607
608 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
609 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
610 AnalysisUsage *AU = DMI->second;
611 delete AU;
612 }
613
Devang Patele7599552007-01-12 18:52:44 +0000614}
615
Devang Patelafb1f3622006-12-12 22:35:25 +0000616//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000617// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000618
Devang Patel643676c2006-11-11 01:10:19 +0000619/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000620void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000621
Devang Patel643676c2006-11-11 01:10:19 +0000622 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000623 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000624
Devang Patele9976aa2006-12-07 19:33:53 +0000625 //This pass is the current implementation of all of the interfaces it
626 //implements as well.
627 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
628 for (unsigned i = 0, e = II.size(); i != e; ++i)
629 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000630 }
631}
632
Devang Patel9d9fc902007-03-06 17:52:53 +0000633// Return true if P preserves high level analysis used by other
634// passes managed by this manager
635bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
636
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000637 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel9d9fc902007-03-06 17:52:53 +0000638
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000639 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000640 return true;
641
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000642 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000643 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000644 E = HigherLevelAnalysis.end(); I != E; ++I) {
645 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000646 if (!dynamic_cast<ImmutablePass*>(P1) &&
647 std::find(PreservedSet.begin(), PreservedSet.end(),
648 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000649 PreservedSet.end())
650 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000651 }
652
653 return true;
654}
655
Chris Lattner02eb94c2008-08-07 07:34:50 +0000656/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000657void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000658 // Don't do this unless assertions are enabled.
659#ifdef NDEBUG
660 return;
661#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000662 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
663 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000664
Devang Patelef432532007-07-19 05:36:09 +0000665 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000666 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000667 E = PreservedSet.end(); I != E; ++I) {
668 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000669 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000670 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000671 }
Devang Patela273d1c2007-07-19 18:02:32 +0000672}
673
Devang Patel9dbe4d12008-07-01 17:44:24 +0000674/// verifyDomInfo - Verify dominator information if it is available.
675void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
676
677 if (!VerifyDomInfo || !P.getResolver())
678 return;
679
680 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
681 if (!DT)
682 return;
683
684 DominatorTree OtherDT;
685 OtherDT.getBase().recalculate(F);
686 if (DT->compare(OtherDT)) {
687 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000688 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000689 cerr << "----- Valid -----\n";
690 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000691 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000692 DT->dump();
693 assert (0 && "Invalid dominator info");
694 }
695
696 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
697 if (!DF)
698 return;
699
700 DominanceFrontier OtherDF;
701 std::vector<BasicBlock*> DTRoots = DT->getRoots();
702 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
703 if (DF->compare(OtherDF)) {
704 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000705 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000706 cerr << "----- Valid -----\n";
707 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000708 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000709 DF->dump();
710 assert (0 && "Invalid dominator info");
711 }
712}
713
Devang Patel67c79a42008-07-01 19:50:56 +0000714/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000715void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000716 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
717 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000718 return;
719
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000720 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000721 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000722 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000723 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000724 if (!dynamic_cast<ImmutablePass*>(Info->second)
725 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000726 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000727 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000728 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000729 if (PassDebugging >= Details) {
730 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000731 cerr << " -- '" << P->getPassName() << "' is not preserving '";
732 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000733 }
734 }
Devang Patel349170f2006-11-11 01:24:55 +0000735 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000736
737 // Check inherited analysis also. If P is not preserving analysis
738 // provided by parent manager then remove it here.
739 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
740
741 if (!InheritedAnalysis[Index])
742 continue;
743
744 for (std::map<AnalysisID, Pass*>::iterator
745 I = InheritedAnalysis[Index]->begin(),
746 E = InheritedAnalysis[Index]->end(); I != E; ) {
747 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000748 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
749 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000750 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000751 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000752 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000753 }
754 }
Devang Patelf68a3492006-11-07 22:35:17 +0000755}
756
Devang Patelca189262006-11-14 03:05:08 +0000757/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000758void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000759 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000760
Devang Patel8adae862007-07-20 18:04:54 +0000761 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000762
Devang Patel2ff44922007-04-16 20:39:59 +0000763 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000764 if (!TPM)
765 return;
766
Devang Patel17ad0962006-12-08 00:37:52 +0000767 TPM->collectLastUses(DeadPasses, P);
768
Devang Patel656a9172008-06-06 17:50:36 +0000769 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000770 cerr << " -*- '" << P->getPassName();
771 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000772 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000773 }
774
Devang Patel8adae862007-07-20 18:04:54 +0000775 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000776 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000777
Devang Patel003a5592007-03-05 20:01:30 +0000778 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000779
Devang Patel7ebf09d2007-03-05 18:20:51 +0000780 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000781 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000782 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000783 if (const PassInfo *PI = (*I)->getPassInfo()) {
784 std::map<AnalysisID, Pass*>::iterator Pos =
785 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000786
Devang Patelc3e3ca92008-10-06 20:36:36 +0000787 // It is possible that pass is already removed from the AvailableAnalysis
788 if (Pos != AvailableAnalysis.end())
789 AvailableAnalysis.erase(Pos);
790
791 // Remove all interfaces this pass implements, for which it is also
792 // listed as the available implementation.
793 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
794 for (unsigned i = 0, e = II.size(); i != e; ++i) {
795 Pos = AvailableAnalysis.find(II[i]);
796 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
797 AvailableAnalysis.erase(Pos);
798 }
799 }
Devang Patel17ad0962006-12-08 00:37:52 +0000800 }
Devang Patelca189262006-11-14 03:05:08 +0000801}
802
Devang Patel8f677ce2006-12-07 18:47:25 +0000803/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000804/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000805void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000806 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000807
Devang Pateld440cd92006-12-08 23:53:00 +0000808 // This manager is going to manage pass P. Set up analysis resolver
809 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000810 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000811 P->setResolver(AR);
812
Devang Patelec2b9a72007-03-05 22:57:49 +0000813 // If a FunctionPass F is the last user of ModulePass info M
814 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000815 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000816
Devang Patel90b05e02006-11-11 02:04:19 +0000817 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000818
819 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000820 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000821 SmallVector<Pass *, 8> RequiredPasses;
822 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
823
Devang Patelbc03f132006-12-07 23:55:10 +0000824 unsigned PDepth = this->getDepth();
825
Devang Patele64d3052007-04-16 20:12:57 +0000826 collectRequiredAnalysis(RequiredPasses,
827 ReqAnalysisNotAvailable, P);
828 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000829 E = RequiredPasses.end(); I != E; ++I) {
830 Pass *PRequired = *I;
831 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000832
Devang Patel9dbe4d12008-07-01 17:44:24 +0000833 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000834 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
835 RDepth = DM.getDepth();
836
Devang Patelbc03f132006-12-07 23:55:10 +0000837 if (PDepth == RDepth)
838 LastUses.push_back(PRequired);
839 else if (PDepth > RDepth) {
840 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000841 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000842 // Keep track of higher level analysis used by this manager.
843 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000844 } else
845 assert (0 && "Unable to accomodate Required Pass");
846 }
847
Devang Patel39786a92007-01-15 20:31:54 +0000848 // Set P as P's last user until someone starts using P.
849 // However, if P is a Pass Manager then it does not need
850 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000851 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000852 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000853 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000854
Devang Patelec2b9a72007-03-05 22:57:49 +0000855 if (!TransferLastUses.empty()) {
856 Pass *My_PM = dynamic_cast<Pass *>(this);
857 TPM->setLastUser(TransferLastUses, My_PM);
858 TransferLastUses.clear();
859 }
860
Devang Patel69e9f6d2007-04-16 20:27:05 +0000861 // Now, take care of required analysises that are not available.
862 for (SmallVector<AnalysisID, 8>::iterator
863 I = ReqAnalysisNotAvailable.begin(),
864 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
865 Pass *AnalysisPass = (*I)->createPass();
866 this->addLowerLevelRequiredPass(P, AnalysisPass);
867 }
868
Devang Patel17bff0d2006-12-07 22:09:36 +0000869 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000870 // Remove the analysis not preserved by this pass
871 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000872 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000873 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000874
875 // Add pass
876 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000877}
878
Devang Patele64d3052007-04-16 20:12:57 +0000879
880/// Populate RP with analysis pass that are required by
881/// pass P and are available. Populate RP_NotAvail with analysis
882/// pass that are required by pass P but are not available.
883void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
884 SmallVector<AnalysisID, 8> &RP_NotAvail,
885 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000886 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
887 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000888 for (AnalysisUsage::VectorType::const_iterator
Devang Patel1d6267c2006-12-07 23:05:44 +0000889 I = RequiredSet.begin(), E = RequiredSet.end();
890 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000891 AnalysisID AID = *I;
892 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
893 RP.push_back(AnalysisPass);
894 else
895 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000896 }
Devang Patelf58183d2006-12-12 23:09:32 +0000897
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000898 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000899 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000900 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000901 AnalysisID AID = *I;
902 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
903 RP.push_back(AnalysisPass);
904 else
905 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000906 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000907}
908
Devang Patel07f4f582006-11-14 21:49:36 +0000909// All Required analyses should be available to the pass as it runs! Here
910// we fill in the AnalysisImpls member of the pass so that it can
911// successfully use the getAnalysis() method to retrieve the
912// implementations it needs.
913//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000914void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000915 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
916
Chris Lattnercbd160f2008-08-08 05:33:04 +0000917 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000918 I = AnUsage->getRequiredSet().begin(),
919 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000920 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000921 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000922 // This may be analysis pass that is initialized on the fly.
923 // If that is not the case then it will raise an assert when it is used.
924 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000925 AnalysisResolver *AR = P->getResolver();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000926 assert (AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000927 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000928 }
929}
930
Devang Patel640c5bb2006-12-08 22:30:11 +0000931/// Find the pass that implements Analysis AID. If desired pass is not found
932/// then return NULL.
933Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
934
935 // Check if AvailableAnalysis map has one entry.
936 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
937
938 if (I != AvailableAnalysis.end())
939 return I->second;
940
941 // Search Parents through TopLevelManager
942 if (SearchParent)
943 return TPM->findAnalysisPass(AID);
944
Devang Patel9d759b82006-12-09 00:09:12 +0000945 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000946}
947
Devang Patel991aeba2006-12-15 20:13:01 +0000948// Print list of passes that are last used by P.
949void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
950
Devang Patel8adae862007-07-20 18:04:54 +0000951 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000952
953 // If this is a on the fly manager then it does not have TPM.
954 if (!TPM)
955 return;
956
Devang Patel991aeba2006-12-15 20:13:01 +0000957 TPM->collectLastUses(LUses, P);
958
Devang Patel8adae862007-07-20 18:04:54 +0000959 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000960 E = LUses.end(); I != E; ++I) {
961 llvm::cerr << "--" << std::string(Offset*2, ' ');
962 (*I)->dumpPassStructure(0);
963 }
964}
965
966void PMDataManager::dumpPassArguments() const {
Devang Patel0d29ae02008-08-12 15:44:31 +0000967 for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000968 E = PassVector.end(); I != E; ++I) {
969 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
970 PMD->dumpPassArguments();
971 else
972 if (const PassInfo *PI = (*I)->getPassInfo())
973 if (!PI->isAnalysisGroup())
974 cerr << " -" << PI->getPassArgument();
975 }
976}
977
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000978void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
979 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000980 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000981 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000982 return;
983 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000984 switch (S1) {
985 case EXECUTION_MSG:
986 cerr << "Executing Pass '" << P->getPassName();
987 break;
988 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000989 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000990 break;
991 case FREEING_MSG:
992 cerr << " Freeing Pass '" << P->getPassName();
993 break;
994 default:
995 break;
996 }
997 switch (S2) {
998 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000999 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001000 break;
1001 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001002 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001003 break;
1004 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001005 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001006 break;
1007 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001008 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001009 break;
1010 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001011 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001012 break;
1013 default:
1014 break;
1015 }
Devang Patel991aeba2006-12-15 20:13:01 +00001016}
1017
Chris Lattner4c493d92008-08-08 15:14:09 +00001018void PMDataManager::dumpRequiredSet(const Pass *P)
1019 const {
1020 if (PassDebugging < Details)
1021 return;
1022
1023 AnalysisUsage analysisUsage;
1024 P->getAnalysisUsage(analysisUsage);
1025 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1026}
1027
1028void PMDataManager::dumpPreservedSet(const Pass *P)
1029 const {
1030 if (PassDebugging < Details)
1031 return;
1032
1033 AnalysisUsage analysisUsage;
1034 P->getAnalysisUsage(analysisUsage);
1035 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1036}
1037
1038void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattnercbd160f2008-08-08 05:33:04 +00001039 const AnalysisUsage::VectorType &Set)
Devang Patel991aeba2006-12-15 20:13:01 +00001040 const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001041 assert(PassDebugging >= Details);
1042 if (Set.empty())
1043 return;
1044 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1045 for (unsigned i = 0; i != Set.size(); ++i) {
1046 if (i) cerr << ",";
1047 cerr << " " << Set[i]->getPassName();
1048 }
1049 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001050}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001051
Devang Patel004937b2007-07-27 20:06:09 +00001052/// Add RequiredPass into list of lower level passes required by pass P.
1053/// RequiredPass is run on the fly by Pass Manager when P requests it
1054/// through getAnalysis interface.
1055/// This should be handled by specific pass manager.
1056void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1057 if (TPM) {
1058 TPM->dumpArguments();
1059 TPM->dumpPasses();
1060 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001061
1062 // Module Level pass may required Function Level analysis info
1063 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1064 // to provide this on demand. In that case, in Pass manager terminology,
1065 // module level pass is requiring lower level analysis info managed by
1066 // lower level pass manager.
1067
1068 // When Pass manager is not able to order required analysis info, Pass manager
1069 // checks whether any lower level manager will be able to provide this
1070 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001071#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001072 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1073 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001074#endif
1075 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001076}
1077
Devang Patele7599552007-01-12 18:52:44 +00001078// Destructor
1079PMDataManager::~PMDataManager() {
1080
Devang Patel0d29ae02008-08-12 15:44:31 +00001081 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001082 E = PassVector.end(); I != E; ++I)
1083 delete *I;
1084
Devang Patele7599552007-01-12 18:52:44 +00001085}
1086
Devang Patel9bdf7d42006-12-08 23:28:54 +00001087//===----------------------------------------------------------------------===//
1088// NOTE: Is this the right place to define this method ?
1089// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001090Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001091 return PM.findAnalysisPass(ID, dir);
1092}
1093
Devang Patel92942812007-04-16 20:56:24 +00001094Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1095 Function &F) {
1096 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1097}
1098
Devang Patela1514cb2006-12-07 19:39:39 +00001099//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001100// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001101
Devang Patel6e5a1132006-11-07 21:31:57 +00001102/// Execute all of the passes scheduled for execution by invoking
1103/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1104/// the function, and if so, return true.
1105bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001106BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001107
Reid Spencer5301e7c2007-01-30 20:08:39 +00001108 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001109 return false;
1110
Devang Patele9585592006-12-08 01:38:28 +00001111 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001112
Devang Patel6e5a1132006-11-07 21:31:57 +00001113 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001114 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1115 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001116
Devang Pateld305c402007-08-10 18:29:32 +00001117 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001118 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001119
Devang Patelabfbe3b2006-12-16 00:56:26 +00001120 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001121
Devang Patelabfbe3b2006-12-16 00:56:26 +00001122 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001123 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001124 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001125
Devang Patel003a5592007-03-05 20:01:30 +00001126 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001127 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1128 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001129 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001130
Devang Patela273d1c2007-07-19 18:02:32 +00001131 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001132 removeNotPreservedAnalysis(BP);
1133 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001134 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001135 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001136
Devang Patel56d48ec2006-12-15 22:57:49 +00001137 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001138}
1139
Devang Patel475c4532006-12-08 00:59:05 +00001140// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001141inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001142 bool Changed = false;
1143
Devang Patelabfbe3b2006-12-16 00:56:26 +00001144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1145 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001146 Changed |= BP->doInitialization(M);
1147 }
1148
1149 return Changed;
1150}
1151
Devang Patel67d6a5e2006-12-19 19:46:59 +00001152inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001153 bool Changed = false;
1154
Devang Patelabfbe3b2006-12-16 00:56:26 +00001155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001157 Changed |= BP->doFinalization(M);
1158 }
1159
1160 return Changed;
1161}
1162
Devang Patel67d6a5e2006-12-19 19:46:59 +00001163inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001164 bool Changed = false;
1165
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1167 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001168 Changed |= BP->doInitialization(F);
1169 }
1170
1171 return Changed;
1172}
1173
Devang Patel67d6a5e2006-12-19 19:46:59 +00001174inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001175 bool Changed = false;
1176
Devang Patelabfbe3b2006-12-16 00:56:26 +00001177 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1178 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001179 Changed |= BP->doFinalization(F);
1180 }
1181
1182 return Changed;
1183}
1184
1185
Devang Patela1514cb2006-12-07 19:39:39 +00001186//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001187// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001188
Devang Patel4e12f862006-11-08 10:44:40 +00001189/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001190FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001191 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001192 // FPM is the top level manager.
1193 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001194
Dan Gohman565df952008-03-13 02:08:36 +00001195 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001196 FPM->setResolver(AR);
1197
Devang Patel1f653682006-12-08 18:57:16 +00001198 MP = P;
1199}
1200
Devang Patelb67904d2006-12-13 02:36:01 +00001201FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001202 delete FPM;
1203}
1204
Devang Patel4e12f862006-11-08 10:44:40 +00001205/// add - Add a pass to the queue of passes to run. This passes
1206/// ownership of the Pass to the PassManager. When the
1207/// PassManager_X is destroyed, the pass will be destroyed as well, so
1208/// there is no need to delete the pass. (TODO delete passes.)
1209/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001210void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001211 FPM->add(P);
1212}
1213
Devang Patel9f3083e2006-11-15 19:39:54 +00001214/// run - Execute all of the passes scheduled for execution. Keep
1215/// track of whether any of the passes modifies the function, and if
1216/// so, return true.
1217///
Devang Patelb67904d2006-12-13 02:36:01 +00001218bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001219 std::string errstr;
1220 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001221 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001222 abort();
1223 }
Devang Patel272908d2006-12-08 22:57:48 +00001224 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001225}
1226
1227
Devang Patelff631ae2006-11-15 01:27:05 +00001228/// doInitialization - Run all of the initializers for the function passes.
1229///
Devang Patelb67904d2006-12-13 02:36:01 +00001230bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001231 return FPM->doInitialization(*MP->getModule());
1232}
1233
Dan Gohmane6656eb2007-07-30 14:51:13 +00001234/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001235///
Devang Patelb67904d2006-12-13 02:36:01 +00001236bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001237 return FPM->doFinalization(*MP->getModule());
1238}
1239
Devang Patela1514cb2006-12-07 19:39:39 +00001240//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001241// FunctionPassManagerImpl implementation
1242//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001243inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1244 bool Changed = false;
1245
1246 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1247 FPPassManager *FP = getContainedManager(Index);
1248 Changed |= FP->doInitialization(M);
1249 }
1250
1251 return Changed;
1252}
1253
1254inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1255 bool Changed = false;
1256
1257 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1258 FPPassManager *FP = getContainedManager(Index);
1259 Changed |= FP->doFinalization(M);
1260 }
1261
1262 return Changed;
1263}
1264
1265// Execute all the passes managed by this top level manager.
1266// Return true if any function is modified by a pass.
1267bool FunctionPassManagerImpl::run(Function &F) {
1268
1269 bool Changed = false;
1270
1271 TimingInfo::createTheTimeInfo();
1272
1273 dumpArguments();
1274 dumpPasses();
1275
Devang Patele3068402006-12-21 00:16:50 +00001276 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001277 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1278 FPPassManager *FP = getContainedManager(Index);
1279 Changed |= FP->runOnFunction(F);
1280 }
1281 return Changed;
1282}
1283
1284//===----------------------------------------------------------------------===//
1285// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001286
Devang Patel8c78a0b2007-05-03 01:11:54 +00001287char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001288/// Print passes managed by this manager
1289void FPPassManager::dumpPassStructure(unsigned Offset) {
1290 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1291 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1292 FunctionPass *FP = getContainedPass(Index);
1293 FP->dumpPassStructure(Offset + 1);
1294 dumpLastUses(FP, Offset+1);
1295 }
1296}
1297
1298
Devang Patel0c2012f2006-11-07 21:49:50 +00001299/// Execute all of the passes scheduled for execution by invoking
1300/// runOnFunction method. Keep track of whether any of the passes modifies
1301/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001302bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001303
1304 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001305
Reid Spencer5301e7c2007-01-30 20:08:39 +00001306 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001307 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001308
1309 // Collect inherited analysis from Module level pass manager.
1310 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001311
Devang Patelabfbe3b2006-12-16 00:56:26 +00001312 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1313 FunctionPass *FP = getContainedPass(Index);
1314
Devang Pateld305c402007-08-10 18:29:32 +00001315 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001316 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001317
Devang Patelabfbe3b2006-12-16 00:56:26 +00001318 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001319
Devang Patelabfbe3b2006-12-16 00:56:26 +00001320 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001321 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001322 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001323
Devang Patel003a5592007-03-05 20:01:30 +00001324 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001325 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001326 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001327
Devang Patela273d1c2007-07-19 18:02:32 +00001328 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001329 removeNotPreservedAnalysis(FP);
1330 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001331 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001332
Devang Patel67c79a42008-07-01 19:50:56 +00001333 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001334 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001335 }
1336 return Changed;
1337}
1338
Devang Patel67d6a5e2006-12-19 19:46:59 +00001339bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001340
Devang Patel67d6a5e2006-12-19 19:46:59 +00001341 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001342
1343 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1344 this->runOnFunction(*I);
1345
1346 return Changed |= doFinalization(M);
1347}
1348
1349inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001350 bool Changed = false;
1351
Devang Patelabfbe3b2006-12-16 00:56:26 +00001352 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1353 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001354 Changed |= FP->doInitialization(M);
1355 }
1356
1357 return Changed;
1358}
1359
Devang Patel67d6a5e2006-12-19 19:46:59 +00001360inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001361 bool Changed = false;
1362
Devang Patelabfbe3b2006-12-16 00:56:26 +00001363 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1364 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001365 Changed |= FP->doFinalization(M);
1366 }
1367
Devang Patelff631ae2006-11-15 01:27:05 +00001368 return Changed;
1369}
1370
Devang Patela1514cb2006-12-07 19:39:39 +00001371//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001372// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001373
Devang Patel05e1a972006-11-07 22:03:15 +00001374/// Execute all of the passes scheduled for execution by invoking
1375/// runOnModule method. Keep track of whether any of the passes modifies
1376/// the module, and if so, return true.
1377bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001378MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001379 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001380
Devang Patelabfbe3b2006-12-16 00:56:26 +00001381 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1382 ModulePass *MP = getContainedPass(Index);
1383
Dan Gohman929391a2008-01-29 12:09:55 +00001384 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1385 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001386 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001387
Devang Patelabfbe3b2006-12-16 00:56:26 +00001388 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001389
Devang Patelabfbe3b2006-12-16 00:56:26 +00001390 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001391 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001392 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001393
Devang Patel003a5592007-03-05 20:01:30 +00001394 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001395 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1396 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001397 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001398
Devang Patela273d1c2007-07-19 18:02:32 +00001399 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001400 removeNotPreservedAnalysis(MP);
1401 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001402 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001403 }
1404 return Changed;
1405}
1406
Devang Patele64d3052007-04-16 20:12:57 +00001407/// Add RequiredPass into list of lower level passes required by pass P.
1408/// RequiredPass is run on the fly by Pass Manager when P requests it
1409/// through getAnalysis interface.
1410void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1411
1412 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1413 && "Unable to handle Pass that requires lower level Analysis pass");
1414 assert ((P->getPotentialPassManagerType() <
1415 RequiredPass->getPotentialPassManagerType())
1416 && "Unable to handle Pass that requires lower level Analysis pass");
1417
Devang Patel68f72b12007-04-26 17:50:19 +00001418 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001419 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001420 FPP = new FunctionPassManagerImpl(0);
1421 // FPP is the top level manager.
1422 FPP->setTopLevelManager(FPP);
1423
Devang Patel69e9f6d2007-04-16 20:27:05 +00001424 OnTheFlyManagers[P] = FPP;
1425 }
Devang Patel68f72b12007-04-26 17:50:19 +00001426 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001427
Devang Patel68f72b12007-04-26 17:50:19 +00001428 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001429 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001430 LU.push_back(RequiredPass);
1431 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001432}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001433
1434/// Return function pass corresponding to PassInfo PI, that is
1435/// required by module pass MP. Instantiate analysis pass, by using
1436/// its runOnFunction() for function F.
1437Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1438 Function &F) {
1439 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001440 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001441 assert (FPP && "Unable to find on the fly pass");
1442
Devang Patel68f72b12007-04-26 17:50:19 +00001443 FPP->run(F);
1444 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001445}
1446
1447
Devang Patela1514cb2006-12-07 19:39:39 +00001448//===----------------------------------------------------------------------===//
1449// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001450//
Devang Patelc290c8a2006-11-07 22:23:34 +00001451/// run - Execute all of the passes scheduled for execution. Keep track of
1452/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001453bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001454
Devang Patelc290c8a2006-11-07 22:23:34 +00001455 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001456
Devang Patelb8817b92006-12-14 00:59:42 +00001457 TimingInfo::createTheTimeInfo();
1458
Devang Patelcfd70c42006-12-13 22:10:00 +00001459 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001460 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001461
Devang Patele3068402006-12-21 00:16:50 +00001462 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001463 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1464 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001465 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001466 }
1467 return Changed;
1468}
Devang Patel376fefa2006-11-08 10:29:57 +00001469
Devang Patela1514cb2006-12-07 19:39:39 +00001470//===----------------------------------------------------------------------===//
1471// PassManager implementation
1472
Devang Patel376fefa2006-11-08 10:29:57 +00001473/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001474PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001475 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001476 // PM is the top level manager
1477 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001478}
1479
Devang Patelb67904d2006-12-13 02:36:01 +00001480PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001481 delete PM;
1482}
1483
Devang Patel376fefa2006-11-08 10:29:57 +00001484/// add - Add a pass to the queue of passes to run. This passes ownership of
1485/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1486/// will be destroyed as well, so there is no need to delete the pass. This
1487/// implies that all passes MUST be allocated with 'new'.
1488void
Devang Patelb67904d2006-12-13 02:36:01 +00001489PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001490 PM->add(P);
1491}
1492
1493/// run - Execute all of the passes scheduled for execution. Keep track of
1494/// whether any of the passes modifies the module, and if so, return true.
1495bool
Devang Patelb67904d2006-12-13 02:36:01 +00001496PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001497 return PM->run(M);
1498}
1499
Devang Patelb8817b92006-12-14 00:59:42 +00001500//===----------------------------------------------------------------------===//
1501// TimingInfo Class - This class is used to calculate information about the
1502// amount of time each pass takes to execute. This only happens with
1503// -time-passes is enabled on the command line.
1504//
1505bool llvm::TimePassesIsEnabled = false;
1506static cl::opt<bool,true>
1507EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1508 cl::desc("Time each pass, printing elapsed time for each on exit"));
1509
1510// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1511// a non null value (if the -time-passes option is enabled) or it leaves it
1512// null. It may be called multiple times.
1513void TimingInfo::createTheTimeInfo() {
1514 if (!TimePassesIsEnabled || TheTimeInfo) return;
1515
1516 // Constructed the first time this is called, iff -time-passes is enabled.
1517 // This guarantees that the object will be constructed before static globals,
1518 // thus it will be destroyed before them.
1519 static ManagedStatic<TimingInfo> TTI;
1520 TheTimeInfo = &*TTI;
1521}
1522
Devang Patel1c3633e2007-01-29 23:10:37 +00001523/// If TimingInfo is enabled then start pass timer.
1524void StartPassTimer(Pass *P) {
1525 if (TheTimeInfo)
1526 TheTimeInfo->passStarted(P);
1527}
1528
1529/// If TimingInfo is enabled then stop pass timer.
1530void StopPassTimer(Pass *P) {
1531 if (TheTimeInfo)
1532 TheTimeInfo->passEnded(P);
1533}
1534
Devang Patel1c56a632007-01-08 19:29:38 +00001535//===----------------------------------------------------------------------===//
1536// PMStack implementation
1537//
Devang Patelad98d232007-01-11 22:15:30 +00001538
Devang Patel1c56a632007-01-08 19:29:38 +00001539// Pop Pass Manager from the stack and clear its analysis info.
1540void PMStack::pop() {
1541
1542 PMDataManager *Top = this->top();
1543 Top->initializeAnalysisInfo();
1544
1545 S.pop_back();
1546}
1547
1548// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001549void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001550
Devang Patel15701b52007-01-11 00:19:00 +00001551 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001552 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001553
Devang Patel15701b52007-01-11 00:19:00 +00001554 if (this->empty()) {
1555 Top = PM;
1556 }
1557 else {
1558 Top = this->top();
1559 PMTopLevelManager *TPM = Top->getTopLevelManager();
1560
1561 assert (TPM && "Unable to find top level manager");
1562 TPM->addIndirectPassManager(PM);
1563 PM->setTopLevelManager(TPM);
1564 }
1565
Devang Patel15701b52007-01-11 00:19:00 +00001566 S.push_back(PM);
1567}
1568
1569// Dump content of the pass manager stack.
1570void PMStack::dump() {
1571 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1572 E = S.end(); I != E; ++I) {
1573 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001574 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001575 }
1576 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001577 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001578}
1579
Devang Patel1c56a632007-01-08 19:29:38 +00001580/// Find appropriate Module Pass Manager in the PM Stack and
1581/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001582void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001583 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001584
Devang Patel1c56a632007-01-08 19:29:38 +00001585 // Find Module Pass Manager
1586 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001587 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1588 if (TopPMType == PreferredType)
1589 break; // We found desired pass manager
1590 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001591 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001592 else
1593 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001594 }
Devang Patel18ff6362008-09-09 21:38:40 +00001595 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001596 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001597}
1598
Devang Patel3312f752007-01-16 21:43:18 +00001599/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1600/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001601void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001602 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001603
Devang Patela3286902008-09-09 17:56:50 +00001604 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001605 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001606 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1607 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001608 else
Devang Patel3312f752007-01-16 21:43:18 +00001609 break;
1610 }
1611 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1612
1613 // Create new Function Pass Manager
1614 if (!FPP) {
1615 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1616 PMDataManager *PMD = PMS.top();
1617
1618 // [1] Create new Function Pass Manager
1619 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001620 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001621
1622 // [2] Set up new manager's top level manager
1623 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1624 TPM->addIndirectPassManager(FPP);
1625
1626 // [3] Assign manager to manage this new manager. This may create
1627 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001628 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001629
1630 // [4] Push new manager into PMS
1631 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001632 }
1633
Devang Patel3312f752007-01-16 21:43:18 +00001634 // Assign FPP as the manager of this pass.
1635 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001636}
1637
Devang Patel3312f752007-01-16 21:43:18 +00001638/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001639/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001640void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001641 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001642
1643 BBPassManager *BBP = NULL;
1644
Devang Patel15701b52007-01-11 00:19:00 +00001645 // Basic Pass Manager is a leaf pass manager. It does not handle
1646 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001647 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001648 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001649
Devang Patel3312f752007-01-16 21:43:18 +00001650 // If leaf manager is not Basic Block Pass manager then create new
1651 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001652
Devang Patel3312f752007-01-16 21:43:18 +00001653 if (!BBP) {
1654 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1655 PMDataManager *PMD = PMS.top();
1656
1657 // [1] Create new Basic Block Manager
1658 BBP = new BBPassManager(PMD->getDepth() + 1);
1659
1660 // [2] Set up new manager's top level manager
1661 // Basic Block Pass Manager does not live by itself
1662 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1663 TPM->addIndirectPassManager(BBP);
1664
Devang Patel15701b52007-01-11 00:19:00 +00001665 // [3] Assign manager to manage this new manager. This may create
1666 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001667 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001668
Devang Patel3312f752007-01-16 21:43:18 +00001669 // [4] Push new manager into PMS
1670 PMS.push(BBP);
1671 }
Devang Patel1c56a632007-01-08 19:29:38 +00001672
Devang Patel3312f752007-01-16 21:43:18 +00001673 // Assign BBP as the manager of this pass.
1674 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001675}
1676
Dan Gohmand3a20c92008-03-11 16:41:42 +00001677PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001678
1679/*===-- C Bindings --------------------------------------------------------===*/
1680
1681LLVMPassManagerRef LLVMCreatePassManager() {
1682 return wrap(new PassManager());
1683}
1684
1685LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1686 return wrap(new FunctionPassManager(unwrap(P)));
1687}
1688
1689int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1690 return unwrap<PassManager>(PM)->run(*unwrap(M));
1691}
1692
1693int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1694 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1695}
1696
1697int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1698 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1699}
1700
1701int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1702 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1703}
1704
1705void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1706 delete unwrap(PM);
1707}