blob: 690c485fe20d65aa54d4b3fb82510afc4234bbfb [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() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000461 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
462 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000463 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000464 }
Devang Patel864970e2008-03-18 00:39:19 +0000465
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000466 AnalysisUsage *AnUsage = findAnalysisUsage(P);
467
Devang Patelfdee7032008-08-14 23:07:48 +0000468 bool checkAnalysis = true;
469 while (checkAnalysis) {
470 checkAnalysis = false;
471
472 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
473 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
474 E = RequiredSet.end(); I != E; ++I) {
475
476 Pass *AnalysisPass = findAnalysisPass(*I);
477 if (!AnalysisPass) {
478 AnalysisPass = (*I)->createPass();
479 if (P->getPotentialPassManagerType () ==
480 AnalysisPass->getPotentialPassManagerType())
481 // Schedule analysis pass that is managed by the same pass manager.
482 schedulePass(AnalysisPass);
483 else if (P->getPotentialPassManagerType () >
484 AnalysisPass->getPotentialPassManagerType()) {
485 // Schedule analysis pass that is managed by a new manager.
486 schedulePass(AnalysisPass);
487 // Recheck analysis passes to ensure that required analysises that
488 // are already checked are still available.
489 checkAnalysis = true;
490 }
491 else
492 // Do not schedule this analysis. Lower level analsyis
493 // passes are run on the fly.
494 delete AnalysisPass;
495 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000496 }
497 }
498
499 // Now all required passes are available.
500 addTopLevelPass(P);
501}
502
503/// Find the pass that implements Analysis AID. Search immutable
504/// passes and all pass managers. If desired pass is not found
505/// then return NULL.
506Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
507
508 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000509 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000510 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000511 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000512 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000513 P = PMD->findAnalysisPass(AID, false);
514 }
515
516 // Check other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000517 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000518 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
519 P = (*I)->findAnalysisPass(AID, false);
520
Devang Patel0d29ae02008-08-12 15:44:31 +0000521 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000522 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
523 const PassInfo *PI = (*I)->getPassInfo();
524 if (PI == AID)
525 P = *I;
526
527 // If Pass not found then check the interfaces implemented by Immutable Pass
528 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000529 const std::vector<const PassInfo*> &ImmPI =
530 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000531 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
532 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000533 }
534 }
535
Devang Patelafb1f3622006-12-12 22:35:25 +0000536 return P;
537}
538
Devang Pateleda56172006-12-12 23:34:33 +0000539// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000540void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000541
Devang Patelfd4184322007-01-17 20:33:36 +0000542 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000543 return;
544
Devang Pateleda56172006-12-12 23:34:33 +0000545 // Print out the immutable passes
546 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
547 ImmutablePasses[i]->dumpPassStructure(0);
548 }
549
Dan Gohman73caf5f2008-03-13 01:48:32 +0000550 // Every class that derives from PMDataManager also derives from Pass
551 // (sometimes indirectly), but there's no inheritance relationship
552 // between PMDataManager and Pass, so we have to dynamic_cast to get
553 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000554 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000555 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000556 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000557}
558
Devang Patel991aeba2006-12-15 20:13:01 +0000559void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000560
Devang Patelfd4184322007-01-17 20:33:36 +0000561 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000562 return;
563
564 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000565 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000566 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000567 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000568 PMD->dumpPassArguments();
569 }
570 cerr << "\n";
571}
572
Devang Patele3068402006-12-21 00:16:50 +0000573void PMTopLevelManager::initializeAllAnalysisInfo() {
574
Devang Patel0d29ae02008-08-12 15:44:31 +0000575 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000576 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000577 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000578 PMD->initializeAnalysisInfo();
579 }
580
581 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000582 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000583 E = IndirectPassManagers.end(); I != E; ++I)
584 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000585
586 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
587 DME = LastUser.end(); DMI != DME; ++DMI) {
588 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
589 InversedLastUser.find(DMI->second);
590 if (InvDMI != InversedLastUser.end()) {
591 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
592 L.insert(DMI->first);
593 } else {
594 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
595 InversedLastUser[DMI->second] = L;
596 }
597 }
Devang Patele3068402006-12-21 00:16:50 +0000598}
599
Devang Patele7599552007-01-12 18:52:44 +0000600/// Destructor
601PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000602 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000603 E = PassManagers.end(); I != E; ++I)
604 delete *I;
605
Devang Patel0d29ae02008-08-12 15:44:31 +0000606 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000607 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
608 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000609
610 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
611 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
612 AnalysisUsage *AU = DMI->second;
613 delete AU;
614 }
615
Devang Patele7599552007-01-12 18:52:44 +0000616}
617
Devang Patelafb1f3622006-12-12 22:35:25 +0000618//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000619// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000620
Devang Patel643676c2006-11-11 01:10:19 +0000621/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000622void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000623
Devang Patel643676c2006-11-11 01:10:19 +0000624 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000625 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000626
Devang Patele9976aa2006-12-07 19:33:53 +0000627 //This pass is the current implementation of all of the interfaces it
628 //implements as well.
629 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
630 for (unsigned i = 0, e = II.size(); i != e; ++i)
631 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000632 }
633}
634
Devang Patel9d9fc902007-03-06 17:52:53 +0000635// Return true if P preserves high level analysis used by other
636// passes managed by this manager
637bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
638
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000639 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel9d9fc902007-03-06 17:52:53 +0000640
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000641 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000642 return true;
643
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000644 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000645 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000646 E = HigherLevelAnalysis.end(); I != E; ++I) {
647 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000648 if (!dynamic_cast<ImmutablePass*>(P1) &&
649 std::find(PreservedSet.begin(), PreservedSet.end(),
650 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000651 PreservedSet.end())
652 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000653 }
654
655 return true;
656}
657
Chris Lattner02eb94c2008-08-07 07:34:50 +0000658/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000659void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000660 // Don't do this unless assertions are enabled.
661#ifdef NDEBUG
662 return;
663#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000664 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
665 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000666
Devang Patelef432532007-07-19 05:36:09 +0000667 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000668 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000669 E = PreservedSet.end(); I != E; ++I) {
670 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000671 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000672 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000673 }
Devang Patela273d1c2007-07-19 18:02:32 +0000674}
675
Devang Patel9dbe4d12008-07-01 17:44:24 +0000676/// verifyDomInfo - Verify dominator information if it is available.
677void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
678
679 if (!VerifyDomInfo || !P.getResolver())
680 return;
681
682 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
683 if (!DT)
684 return;
685
686 DominatorTree OtherDT;
687 OtherDT.getBase().recalculate(F);
688 if (DT->compare(OtherDT)) {
689 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000690 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000691 cerr << "----- Valid -----\n";
692 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000693 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000694 DT->dump();
695 assert (0 && "Invalid dominator info");
696 }
697
698 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
699 if (!DF)
700 return;
701
702 DominanceFrontier OtherDF;
703 std::vector<BasicBlock*> DTRoots = DT->getRoots();
704 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
705 if (DF->compare(OtherDF)) {
706 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000707 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000708 cerr << "----- Valid -----\n";
709 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000710 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000711 DF->dump();
712 assert (0 && "Invalid dominator info");
713 }
714}
715
Devang Patel67c79a42008-07-01 19:50:56 +0000716/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000717void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000718 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
719 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000720 return;
721
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000722 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000723 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000724 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000725 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000726 if (!dynamic_cast<ImmutablePass*>(Info->second)
727 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000728 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000729 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000730 if (PassDebugging >= Details) {
731 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000732 cerr << " -- '" << P->getPassName() << "' is not preserving '";
733 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000734 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000735 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000736 }
Devang Patel349170f2006-11-11 01:24:55 +0000737 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000738
739 // Check inherited analysis also. If P is not preserving analysis
740 // provided by parent manager then remove it here.
741 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
742
743 if (!InheritedAnalysis[Index])
744 continue;
745
746 for (std::map<AnalysisID, Pass*>::iterator
747 I = InheritedAnalysis[Index]->begin(),
748 E = InheritedAnalysis[Index]->end(); I != E; ) {
749 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000750 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
751 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000752 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000753 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000754 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000755 }
756 }
Devang Patelf68a3492006-11-07 22:35:17 +0000757}
758
Devang Patelca189262006-11-14 03:05:08 +0000759/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000760void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000761 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000762
Devang Patel8adae862007-07-20 18:04:54 +0000763 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000764
Devang Patel2ff44922007-04-16 20:39:59 +0000765 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000766 if (!TPM)
767 return;
768
Devang Patel17ad0962006-12-08 00:37:52 +0000769 TPM->collectLastUses(DeadPasses, P);
770
Devang Patel656a9172008-06-06 17:50:36 +0000771 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000772 cerr << " -*- '" << P->getPassName();
773 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000774 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000775 }
776
Devang Patel8adae862007-07-20 18:04:54 +0000777 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000778 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000779
Devang Patel003a5592007-03-05 20:01:30 +0000780 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000781
Devang Patel7ebf09d2007-03-05 18:20:51 +0000782 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000783 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000784 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000785 if (const PassInfo *PI = (*I)->getPassInfo()) {
786 std::map<AnalysisID, Pass*>::iterator Pos =
787 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000788
Devang Patelc3e3ca92008-10-06 20:36:36 +0000789 // It is possible that pass is already removed from the AvailableAnalysis
790 if (Pos != AvailableAnalysis.end())
791 AvailableAnalysis.erase(Pos);
792
793 // Remove all interfaces this pass implements, for which it is also
794 // listed as the available implementation.
795 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
796 for (unsigned i = 0, e = II.size(); i != e; ++i) {
797 Pos = AvailableAnalysis.find(II[i]);
798 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
799 AvailableAnalysis.erase(Pos);
800 }
801 }
Devang Patel17ad0962006-12-08 00:37:52 +0000802 }
Devang Patelca189262006-11-14 03:05:08 +0000803}
804
Devang Patel8f677ce2006-12-07 18:47:25 +0000805/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000806/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000807void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000808 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000809
Devang Pateld440cd92006-12-08 23:53:00 +0000810 // This manager is going to manage pass P. Set up analysis resolver
811 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000812 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000813 P->setResolver(AR);
814
Devang Patelec2b9a72007-03-05 22:57:49 +0000815 // If a FunctionPass F is the last user of ModulePass info M
816 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000817 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000818
Devang Patel90b05e02006-11-11 02:04:19 +0000819 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000820
821 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000822 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000823 SmallVector<Pass *, 8> RequiredPasses;
824 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
825
Devang Patelbc03f132006-12-07 23:55:10 +0000826 unsigned PDepth = this->getDepth();
827
Devang Patele64d3052007-04-16 20:12:57 +0000828 collectRequiredAnalysis(RequiredPasses,
829 ReqAnalysisNotAvailable, P);
830 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000831 E = RequiredPasses.end(); I != E; ++I) {
832 Pass *PRequired = *I;
833 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000834
Devang Patel9dbe4d12008-07-01 17:44:24 +0000835 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000836 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
837 RDepth = DM.getDepth();
838
Devang Patelbc03f132006-12-07 23:55:10 +0000839 if (PDepth == RDepth)
840 LastUses.push_back(PRequired);
841 else if (PDepth > RDepth) {
842 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000843 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000844 // Keep track of higher level analysis used by this manager.
845 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000846 } else
847 assert (0 && "Unable to accomodate Required Pass");
848 }
849
Devang Patel39786a92007-01-15 20:31:54 +0000850 // Set P as P's last user until someone starts using P.
851 // However, if P is a Pass Manager then it does not need
852 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000853 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000854 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000855 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000856
Devang Patelec2b9a72007-03-05 22:57:49 +0000857 if (!TransferLastUses.empty()) {
858 Pass *My_PM = dynamic_cast<Pass *>(this);
859 TPM->setLastUser(TransferLastUses, My_PM);
860 TransferLastUses.clear();
861 }
862
Devang Patel69e9f6d2007-04-16 20:27:05 +0000863 // Now, take care of required analysises that are not available.
864 for (SmallVector<AnalysisID, 8>::iterator
865 I = ReqAnalysisNotAvailable.begin(),
866 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
867 Pass *AnalysisPass = (*I)->createPass();
868 this->addLowerLevelRequiredPass(P, AnalysisPass);
869 }
870
Devang Patel17bff0d2006-12-07 22:09:36 +0000871 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000872 // Remove the analysis not preserved by this pass
873 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000874 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000875 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000876
877 // Add pass
878 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000879}
880
Devang Patele64d3052007-04-16 20:12:57 +0000881
882/// Populate RP with analysis pass that are required by
883/// pass P and are available. Populate RP_NotAvail with analysis
884/// pass that are required by pass P but are not available.
885void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
886 SmallVector<AnalysisID, 8> &RP_NotAvail,
887 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000888 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
889 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000890 for (AnalysisUsage::VectorType::const_iterator
Devang Patel1d6267c2006-12-07 23:05:44 +0000891 I = RequiredSet.begin(), E = RequiredSet.end();
892 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000893 AnalysisID AID = *I;
894 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
895 RP.push_back(AnalysisPass);
896 else
897 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000898 }
Devang Patelf58183d2006-12-12 23:09:32 +0000899
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000900 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000901 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000902 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000903 AnalysisID AID = *I;
904 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
905 RP.push_back(AnalysisPass);
906 else
907 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000908 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000909}
910
Devang Patel07f4f582006-11-14 21:49:36 +0000911// All Required analyses should be available to the pass as it runs! Here
912// we fill in the AnalysisImpls member of the pass so that it can
913// successfully use the getAnalysis() method to retrieve the
914// implementations it needs.
915//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000916void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000917 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
918
Chris Lattnercbd160f2008-08-08 05:33:04 +0000919 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000920 I = AnUsage->getRequiredSet().begin(),
921 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000922 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000923 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000924 // This may be analysis pass that is initialized on the fly.
925 // If that is not the case then it will raise an assert when it is used.
926 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000927 AnalysisResolver *AR = P->getResolver();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000928 assert (AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000929 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000930 }
931}
932
Devang Patel640c5bb2006-12-08 22:30:11 +0000933/// Find the pass that implements Analysis AID. If desired pass is not found
934/// then return NULL.
935Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
936
937 // Check if AvailableAnalysis map has one entry.
938 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
939
940 if (I != AvailableAnalysis.end())
941 return I->second;
942
943 // Search Parents through TopLevelManager
944 if (SearchParent)
945 return TPM->findAnalysisPass(AID);
946
Devang Patel9d759b82006-12-09 00:09:12 +0000947 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000948}
949
Devang Patel991aeba2006-12-15 20:13:01 +0000950// Print list of passes that are last used by P.
951void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
952
Devang Patel8adae862007-07-20 18:04:54 +0000953 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000954
955 // If this is a on the fly manager then it does not have TPM.
956 if (!TPM)
957 return;
958
Devang Patel991aeba2006-12-15 20:13:01 +0000959 TPM->collectLastUses(LUses, P);
960
Devang Patel8adae862007-07-20 18:04:54 +0000961 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000962 E = LUses.end(); I != E; ++I) {
963 llvm::cerr << "--" << std::string(Offset*2, ' ');
964 (*I)->dumpPassStructure(0);
965 }
966}
967
968void PMDataManager::dumpPassArguments() const {
Devang Patel0d29ae02008-08-12 15:44:31 +0000969 for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000970 E = PassVector.end(); I != E; ++I) {
971 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
972 PMD->dumpPassArguments();
973 else
974 if (const PassInfo *PI = (*I)->getPassInfo())
975 if (!PI->isAnalysisGroup())
976 cerr << " -" << PI->getPassArgument();
977 }
978}
979
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000980void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
981 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000982 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000983 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000984 return;
985 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000986 switch (S1) {
987 case EXECUTION_MSG:
988 cerr << "Executing Pass '" << P->getPassName();
989 break;
990 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000991 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000992 break;
993 case FREEING_MSG:
994 cerr << " Freeing Pass '" << P->getPassName();
995 break;
996 default:
997 break;
998 }
999 switch (S2) {
1000 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001001 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001002 break;
1003 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001004 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001005 break;
1006 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001007 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001008 break;
1009 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001010 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001011 break;
1012 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001013 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001014 break;
1015 default:
1016 break;
1017 }
Devang Patel991aeba2006-12-15 20:13:01 +00001018}
1019
Chris Lattner4c493d92008-08-08 15:14:09 +00001020void PMDataManager::dumpRequiredSet(const Pass *P)
1021 const {
1022 if (PassDebugging < Details)
1023 return;
1024
1025 AnalysisUsage analysisUsage;
1026 P->getAnalysisUsage(analysisUsage);
1027 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1028}
1029
1030void PMDataManager::dumpPreservedSet(const Pass *P)
1031 const {
1032 if (PassDebugging < Details)
1033 return;
1034
1035 AnalysisUsage analysisUsage;
1036 P->getAnalysisUsage(analysisUsage);
1037 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1038}
1039
1040void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattnercbd160f2008-08-08 05:33:04 +00001041 const AnalysisUsage::VectorType &Set)
Devang Patel991aeba2006-12-15 20:13:01 +00001042 const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001043 assert(PassDebugging >= Details);
1044 if (Set.empty())
1045 return;
1046 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1047 for (unsigned i = 0; i != Set.size(); ++i) {
1048 if (i) cerr << ",";
1049 cerr << " " << Set[i]->getPassName();
1050 }
1051 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001052}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001053
Devang Patel004937b2007-07-27 20:06:09 +00001054/// Add RequiredPass into list of lower level passes required by pass P.
1055/// RequiredPass is run on the fly by Pass Manager when P requests it
1056/// through getAnalysis interface.
1057/// This should be handled by specific pass manager.
1058void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1059 if (TPM) {
1060 TPM->dumpArguments();
1061 TPM->dumpPasses();
1062 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001063
1064 // Module Level pass may required Function Level analysis info
1065 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1066 // to provide this on demand. In that case, in Pass manager terminology,
1067 // module level pass is requiring lower level analysis info managed by
1068 // lower level pass manager.
1069
1070 // When Pass manager is not able to order required analysis info, Pass manager
1071 // checks whether any lower level manager will be able to provide this
1072 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001073#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001074 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1075 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001076#endif
1077 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001078}
1079
Devang Patele7599552007-01-12 18:52:44 +00001080// Destructor
1081PMDataManager::~PMDataManager() {
1082
Devang Patel0d29ae02008-08-12 15:44:31 +00001083 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001084 E = PassVector.end(); I != E; ++I)
1085 delete *I;
1086
Devang Patele7599552007-01-12 18:52:44 +00001087}
1088
Devang Patel9bdf7d42006-12-08 23:28:54 +00001089//===----------------------------------------------------------------------===//
1090// NOTE: Is this the right place to define this method ?
1091// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001092Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001093 return PM.findAnalysisPass(ID, dir);
1094}
1095
Devang Patel92942812007-04-16 20:56:24 +00001096Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1097 Function &F) {
1098 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1099}
1100
Devang Patela1514cb2006-12-07 19:39:39 +00001101//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001102// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001103
Devang Patel6e5a1132006-11-07 21:31:57 +00001104/// Execute all of the passes scheduled for execution by invoking
1105/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1106/// the function, and if so, return true.
1107bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001108BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001109
Reid Spencer5301e7c2007-01-30 20:08:39 +00001110 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001111 return false;
1112
Devang Patele9585592006-12-08 01:38:28 +00001113 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001114
Devang Patel6e5a1132006-11-07 21:31:57 +00001115 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001116 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1117 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001118
Devang Pateld305c402007-08-10 18:29:32 +00001119 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001120 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001121
Devang Patelabfbe3b2006-12-16 00:56:26 +00001122 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001123
Devang Patelabfbe3b2006-12-16 00:56:26 +00001124 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001125 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001126 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001127
Devang Patel003a5592007-03-05 20:01:30 +00001128 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001129 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1130 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001131 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001132
Devang Patela273d1c2007-07-19 18:02:32 +00001133 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001134 removeNotPreservedAnalysis(BP);
1135 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001136 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001137 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001138
Devang Patel56d48ec2006-12-15 22:57:49 +00001139 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001140}
1141
Devang Patel475c4532006-12-08 00:59:05 +00001142// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001143inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001144 bool Changed = false;
1145
Devang Patelabfbe3b2006-12-16 00:56:26 +00001146 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1147 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001148 Changed |= BP->doInitialization(M);
1149 }
1150
1151 return Changed;
1152}
1153
Devang Patel67d6a5e2006-12-19 19:46:59 +00001154inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001155 bool Changed = false;
1156
Devang Patelabfbe3b2006-12-16 00:56:26 +00001157 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1158 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001159 Changed |= BP->doFinalization(M);
1160 }
1161
1162 return Changed;
1163}
1164
Devang Patel67d6a5e2006-12-19 19:46:59 +00001165inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001166 bool Changed = false;
1167
Devang Patelabfbe3b2006-12-16 00:56:26 +00001168 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1169 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001170 Changed |= BP->doInitialization(F);
1171 }
1172
1173 return Changed;
1174}
1175
Devang Patel67d6a5e2006-12-19 19:46:59 +00001176inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001177 bool Changed = false;
1178
Devang Patelabfbe3b2006-12-16 00:56:26 +00001179 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1180 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001181 Changed |= BP->doFinalization(F);
1182 }
1183
1184 return Changed;
1185}
1186
1187
Devang Patela1514cb2006-12-07 19:39:39 +00001188//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001189// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001190
Devang Patel4e12f862006-11-08 10:44:40 +00001191/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001192FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001193 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001194 // FPM is the top level manager.
1195 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001196
Dan Gohman565df952008-03-13 02:08:36 +00001197 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001198 FPM->setResolver(AR);
1199
Devang Patel1f653682006-12-08 18:57:16 +00001200 MP = P;
1201}
1202
Devang Patelb67904d2006-12-13 02:36:01 +00001203FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001204 delete FPM;
1205}
1206
Devang Patel4e12f862006-11-08 10:44:40 +00001207/// add - Add a pass to the queue of passes to run. This passes
1208/// ownership of the Pass to the PassManager. When the
1209/// PassManager_X is destroyed, the pass will be destroyed as well, so
1210/// there is no need to delete the pass. (TODO delete passes.)
1211/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001212void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001213 FPM->add(P);
1214}
1215
Devang Patel9f3083e2006-11-15 19:39:54 +00001216/// run - Execute all of the passes scheduled for execution. Keep
1217/// track of whether any of the passes modifies the function, and if
1218/// so, return true.
1219///
Devang Patelb67904d2006-12-13 02:36:01 +00001220bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001221 std::string errstr;
1222 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001223 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001224 abort();
1225 }
Devang Patel272908d2006-12-08 22:57:48 +00001226 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001227}
1228
1229
Devang Patelff631ae2006-11-15 01:27:05 +00001230/// doInitialization - Run all of the initializers for the function passes.
1231///
Devang Patelb67904d2006-12-13 02:36:01 +00001232bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001233 return FPM->doInitialization(*MP->getModule());
1234}
1235
Dan Gohmane6656eb2007-07-30 14:51:13 +00001236/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001237///
Devang Patelb67904d2006-12-13 02:36:01 +00001238bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001239 return FPM->doFinalization(*MP->getModule());
1240}
1241
Devang Patela1514cb2006-12-07 19:39:39 +00001242//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001243// FunctionPassManagerImpl implementation
1244//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001245inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1246 bool Changed = false;
1247
1248 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1249 FPPassManager *FP = getContainedManager(Index);
1250 Changed |= FP->doInitialization(M);
1251 }
1252
1253 return Changed;
1254}
1255
1256inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1257 bool Changed = false;
1258
1259 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1260 FPPassManager *FP = getContainedManager(Index);
1261 Changed |= FP->doFinalization(M);
1262 }
1263
1264 return Changed;
1265}
1266
1267// Execute all the passes managed by this top level manager.
1268// Return true if any function is modified by a pass.
1269bool FunctionPassManagerImpl::run(Function &F) {
1270
1271 bool Changed = false;
1272
1273 TimingInfo::createTheTimeInfo();
1274
1275 dumpArguments();
1276 dumpPasses();
1277
Devang Patele3068402006-12-21 00:16:50 +00001278 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001279 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1280 FPPassManager *FP = getContainedManager(Index);
1281 Changed |= FP->runOnFunction(F);
1282 }
1283 return Changed;
1284}
1285
1286//===----------------------------------------------------------------------===//
1287// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001288
Devang Patel8c78a0b2007-05-03 01:11:54 +00001289char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001290/// Print passes managed by this manager
1291void FPPassManager::dumpPassStructure(unsigned Offset) {
1292 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1293 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1294 FunctionPass *FP = getContainedPass(Index);
1295 FP->dumpPassStructure(Offset + 1);
1296 dumpLastUses(FP, Offset+1);
1297 }
1298}
1299
1300
Devang Patel0c2012f2006-11-07 21:49:50 +00001301/// Execute all of the passes scheduled for execution by invoking
1302/// runOnFunction method. Keep track of whether any of the passes modifies
1303/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001304bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001305
1306 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001307
Reid Spencer5301e7c2007-01-30 20:08:39 +00001308 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001309 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001310
1311 // Collect inherited analysis from Module level pass manager.
1312 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001313
Devang Patelabfbe3b2006-12-16 00:56:26 +00001314 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1315 FunctionPass *FP = getContainedPass(Index);
1316
Devang Pateld305c402007-08-10 18:29:32 +00001317 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001318 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001319
Devang Patelabfbe3b2006-12-16 00:56:26 +00001320 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001321
Devang Patelabfbe3b2006-12-16 00:56:26 +00001322 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001323 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001324 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001325
Devang Patel003a5592007-03-05 20:01:30 +00001326 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001327 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001328 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001329
Devang Patela273d1c2007-07-19 18:02:32 +00001330 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001331 removeNotPreservedAnalysis(FP);
1332 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001333 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001334
Devang Patel67c79a42008-07-01 19:50:56 +00001335 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001336 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001337 }
1338 return Changed;
1339}
1340
Devang Patel67d6a5e2006-12-19 19:46:59 +00001341bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001342
Devang Patel67d6a5e2006-12-19 19:46:59 +00001343 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001344
1345 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1346 this->runOnFunction(*I);
1347
1348 return Changed |= doFinalization(M);
1349}
1350
1351inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001352 bool Changed = false;
1353
Devang Patelabfbe3b2006-12-16 00:56:26 +00001354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1355 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001356 Changed |= FP->doInitialization(M);
1357 }
1358
1359 return Changed;
1360}
1361
Devang Patel67d6a5e2006-12-19 19:46:59 +00001362inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001363 bool Changed = false;
1364
Devang Patelabfbe3b2006-12-16 00:56:26 +00001365 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1366 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001367 Changed |= FP->doFinalization(M);
1368 }
1369
Devang Patelff631ae2006-11-15 01:27:05 +00001370 return Changed;
1371}
1372
Devang Patela1514cb2006-12-07 19:39:39 +00001373//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001374// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001375
Devang Patel05e1a972006-11-07 22:03:15 +00001376/// Execute all of the passes scheduled for execution by invoking
1377/// runOnModule method. Keep track of whether any of the passes modifies
1378/// the module, and if so, return true.
1379bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001380MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001381 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001382
Devang Patelabfbe3b2006-12-16 00:56:26 +00001383 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1384 ModulePass *MP = getContainedPass(Index);
1385
Dan Gohman929391a2008-01-29 12:09:55 +00001386 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1387 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001388 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001389
Devang Patelabfbe3b2006-12-16 00:56:26 +00001390 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001391
Devang Patelabfbe3b2006-12-16 00:56:26 +00001392 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001393 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001394 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001395
Devang Patel003a5592007-03-05 20:01:30 +00001396 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001397 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1398 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001399 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001400
Devang Patela273d1c2007-07-19 18:02:32 +00001401 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001402 removeNotPreservedAnalysis(MP);
1403 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001404 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001405 }
1406 return Changed;
1407}
1408
Devang Patele64d3052007-04-16 20:12:57 +00001409/// Add RequiredPass into list of lower level passes required by pass P.
1410/// RequiredPass is run on the fly by Pass Manager when P requests it
1411/// through getAnalysis interface.
1412void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1413
1414 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1415 && "Unable to handle Pass that requires lower level Analysis pass");
1416 assert ((P->getPotentialPassManagerType() <
1417 RequiredPass->getPotentialPassManagerType())
1418 && "Unable to handle Pass that requires lower level Analysis pass");
1419
Devang Patel68f72b12007-04-26 17:50:19 +00001420 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001421 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001422 FPP = new FunctionPassManagerImpl(0);
1423 // FPP is the top level manager.
1424 FPP->setTopLevelManager(FPP);
1425
Devang Patel69e9f6d2007-04-16 20:27:05 +00001426 OnTheFlyManagers[P] = FPP;
1427 }
Devang Patel68f72b12007-04-26 17:50:19 +00001428 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001429
Devang Patel68f72b12007-04-26 17:50:19 +00001430 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001431 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001432 LU.push_back(RequiredPass);
1433 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001434}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001435
1436/// Return function pass corresponding to PassInfo PI, that is
1437/// required by module pass MP. Instantiate analysis pass, by using
1438/// its runOnFunction() for function F.
1439Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1440 Function &F) {
1441 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001442 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001443 assert (FPP && "Unable to find on the fly pass");
1444
Devang Patel68f72b12007-04-26 17:50:19 +00001445 FPP->run(F);
1446 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001447}
1448
1449
Devang Patela1514cb2006-12-07 19:39:39 +00001450//===----------------------------------------------------------------------===//
1451// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001452//
Devang Patelc290c8a2006-11-07 22:23:34 +00001453/// run - Execute all of the passes scheduled for execution. Keep track of
1454/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001456
Devang Patelc290c8a2006-11-07 22:23:34 +00001457 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001458
Devang Patelb8817b92006-12-14 00:59:42 +00001459 TimingInfo::createTheTimeInfo();
1460
Devang Patelcfd70c42006-12-13 22:10:00 +00001461 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001462 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001463
Devang Patele3068402006-12-21 00:16:50 +00001464 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001465 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1466 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001467 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001468 }
1469 return Changed;
1470}
Devang Patel376fefa2006-11-08 10:29:57 +00001471
Devang Patela1514cb2006-12-07 19:39:39 +00001472//===----------------------------------------------------------------------===//
1473// PassManager implementation
1474
Devang Patel376fefa2006-11-08 10:29:57 +00001475/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001476PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001477 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001478 // PM is the top level manager
1479 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001480}
1481
Devang Patelb67904d2006-12-13 02:36:01 +00001482PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001483 delete PM;
1484}
1485
Devang Patel376fefa2006-11-08 10:29:57 +00001486/// add - Add a pass to the queue of passes to run. This passes ownership of
1487/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1488/// will be destroyed as well, so there is no need to delete the pass. This
1489/// implies that all passes MUST be allocated with 'new'.
1490void
Devang Patelb67904d2006-12-13 02:36:01 +00001491PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001492 PM->add(P);
1493}
1494
1495/// run - Execute all of the passes scheduled for execution. Keep track of
1496/// whether any of the passes modifies the module, and if so, return true.
1497bool
Devang Patelb67904d2006-12-13 02:36:01 +00001498PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001499 return PM->run(M);
1500}
1501
Devang Patelb8817b92006-12-14 00:59:42 +00001502//===----------------------------------------------------------------------===//
1503// TimingInfo Class - This class is used to calculate information about the
1504// amount of time each pass takes to execute. This only happens with
1505// -time-passes is enabled on the command line.
1506//
1507bool llvm::TimePassesIsEnabled = false;
1508static cl::opt<bool,true>
1509EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1510 cl::desc("Time each pass, printing elapsed time for each on exit"));
1511
1512// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1513// a non null value (if the -time-passes option is enabled) or it leaves it
1514// null. It may be called multiple times.
1515void TimingInfo::createTheTimeInfo() {
1516 if (!TimePassesIsEnabled || TheTimeInfo) return;
1517
1518 // Constructed the first time this is called, iff -time-passes is enabled.
1519 // This guarantees that the object will be constructed before static globals,
1520 // thus it will be destroyed before them.
1521 static ManagedStatic<TimingInfo> TTI;
1522 TheTimeInfo = &*TTI;
1523}
1524
Devang Patel1c3633e2007-01-29 23:10:37 +00001525/// If TimingInfo is enabled then start pass timer.
1526void StartPassTimer(Pass *P) {
1527 if (TheTimeInfo)
1528 TheTimeInfo->passStarted(P);
1529}
1530
1531/// If TimingInfo is enabled then stop pass timer.
1532void StopPassTimer(Pass *P) {
1533 if (TheTimeInfo)
1534 TheTimeInfo->passEnded(P);
1535}
1536
Devang Patel1c56a632007-01-08 19:29:38 +00001537//===----------------------------------------------------------------------===//
1538// PMStack implementation
1539//
Devang Patelad98d232007-01-11 22:15:30 +00001540
Devang Patel1c56a632007-01-08 19:29:38 +00001541// Pop Pass Manager from the stack and clear its analysis info.
1542void PMStack::pop() {
1543
1544 PMDataManager *Top = this->top();
1545 Top->initializeAnalysisInfo();
1546
1547 S.pop_back();
1548}
1549
1550// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001551void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001552
Devang Patel15701b52007-01-11 00:19:00 +00001553 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001554 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001555
Devang Patel15701b52007-01-11 00:19:00 +00001556 if (this->empty()) {
1557 Top = PM;
1558 }
1559 else {
1560 Top = this->top();
1561 PMTopLevelManager *TPM = Top->getTopLevelManager();
1562
1563 assert (TPM && "Unable to find top level manager");
1564 TPM->addIndirectPassManager(PM);
1565 PM->setTopLevelManager(TPM);
1566 }
1567
Devang Patel15701b52007-01-11 00:19:00 +00001568 S.push_back(PM);
1569}
1570
1571// Dump content of the pass manager stack.
1572void PMStack::dump() {
1573 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1574 E = S.end(); I != E; ++I) {
1575 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001576 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001577 }
1578 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001579 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001580}
1581
Devang Patel1c56a632007-01-08 19:29:38 +00001582/// Find appropriate Module Pass Manager in the PM Stack and
1583/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001584void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001585 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001586
Devang Patel1c56a632007-01-08 19:29:38 +00001587 // Find Module Pass Manager
1588 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001589 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1590 if (TopPMType == PreferredType)
1591 break; // We found desired pass manager
1592 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001593 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001594 else
1595 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001596 }
Devang Patel18ff6362008-09-09 21:38:40 +00001597 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001598 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001599}
1600
Devang Patel3312f752007-01-16 21:43:18 +00001601/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1602/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001603void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001604 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001605
Devang Patela3286902008-09-09 17:56:50 +00001606 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001607 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001608 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1609 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001610 else
Devang Patel3312f752007-01-16 21:43:18 +00001611 break;
1612 }
1613 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1614
1615 // Create new Function Pass Manager
1616 if (!FPP) {
1617 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1618 PMDataManager *PMD = PMS.top();
1619
1620 // [1] Create new Function Pass Manager
1621 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001622 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001623
1624 // [2] Set up new manager's top level manager
1625 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1626 TPM->addIndirectPassManager(FPP);
1627
1628 // [3] Assign manager to manage this new manager. This may create
1629 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001630 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001631
1632 // [4] Push new manager into PMS
1633 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001634 }
1635
Devang Patel3312f752007-01-16 21:43:18 +00001636 // Assign FPP as the manager of this pass.
1637 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001638}
1639
Devang Patel3312f752007-01-16 21:43:18 +00001640/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001641/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001642void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001643 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001644
1645 BBPassManager *BBP = NULL;
1646
Devang Patel15701b52007-01-11 00:19:00 +00001647 // Basic Pass Manager is a leaf pass manager. It does not handle
1648 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001649 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001650 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001651
Devang Patel3312f752007-01-16 21:43:18 +00001652 // If leaf manager is not Basic Block Pass manager then create new
1653 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001654
Devang Patel3312f752007-01-16 21:43:18 +00001655 if (!BBP) {
1656 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1657 PMDataManager *PMD = PMS.top();
1658
1659 // [1] Create new Basic Block Manager
1660 BBP = new BBPassManager(PMD->getDepth() + 1);
1661
1662 // [2] Set up new manager's top level manager
1663 // Basic Block Pass Manager does not live by itself
1664 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1665 TPM->addIndirectPassManager(BBP);
1666
Devang Patel15701b52007-01-11 00:19:00 +00001667 // [3] Assign manager to manage this new manager. This may create
1668 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001669 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001670
Devang Patel3312f752007-01-16 21:43:18 +00001671 // [4] Push new manager into PMS
1672 PMS.push(BBP);
1673 }
Devang Patel1c56a632007-01-08 19:29:38 +00001674
Devang Patel3312f752007-01-16 21:43:18 +00001675 // Assign BBP as the manager of this pass.
1676 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001677}
1678
Dan Gohmand3a20c92008-03-11 16:41:42 +00001679PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001680
1681/*===-- C Bindings --------------------------------------------------------===*/
1682
1683LLVMPassManagerRef LLVMCreatePassManager() {
1684 return wrap(new PassManager());
1685}
1686
1687LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1688 return wrap(new FunctionPassManager(unwrap(P)));
1689}
1690
1691int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1692 return unwrap<PassManager>(PM)->run(*unwrap(M));
1693}
1694
1695int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1696 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1697}
1698
1699int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1700 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1701}
1702
1703int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1704 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1705}
1706
1707void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1708 delete unwrap(PM);
1709}